# NMaximize : constraint violation [duplicate]

NMinimize[{x^2 + (y - .5)^2, y >= 0 && y >= x + 1}, {x, y}]
NMinimize[{x^2 + (y - .5)^2, y >= 0, y >= x + 1}, {x, y}]
NMinimize[{x^2 + (y - .5)^2, {y >= 0, y >= x + 1}}, {x, y}]
NMinimize[{x^2 + (y - .5)^2, {y >= 0 && y >= x + 1}}, {x, y}]


All above runs fine. I think the 'cons' are exactly the same.But is there any 'reason' for using a particular way to write constraints?

Not that I dont want to put a minimal example, but I don't think I can upload a CSV file here. (or I dont know how.) But check this:

ClearAll["Global*"]

(*4C*)

FourC[xij_] :=
Module[{phiL, phiH, pL, pH, K, ind, fi, li, j, ans1, ans2, ans3,
ans4, ans}, K = Length[xij];
phiL = Table[1/(1 + Exp[-Subscript[\[Phi], j]]), {j, 1, K - 1}];
phiH = Table[
1/(1 + Exp[-Subscript[\[Phi], j] -
Subscript[\[Eta], \[Phi]]]), {j, 1, K - 1}];
phiL = Append[phiL, 0];
phiH = Append[phiH, 0];
pL = Table[1/(1 + Exp[-Subscript[p, j]]), {j, 2, K}];
pH = Table[
1/(1 + Exp[-Subscript[p, j] - Subscript[\[Eta], p]]), {j, 2, K}];
pL = Prepend[pL, 1];
pH = Prepend[pH, 1];
fi = First[Position[xij, 1]][[1]];
li = Last[Position[xij, 1]][[1]];

ans1 = w1*
Sum[Product[phiL[[j]], {j, fi, d - 1}]*(1 - phiL[[d]])*
Product[pL[[j]]^xij[[j]]*(1 - pL[[j]])^(1 - xij[[j]]), {j,
fi + 1, d}], {d, li, K}];

ans2 = w2*
Sum[Product[phiL[[j]], {j, fi, d - 1}]*(1 - phiL[[d]])*
Product[pH[[j]]^xij[[j]]*(1 - pH[[j]])^(1 - xij[[j]]), {j,
fi + 1, d}], {d, li, K}];

ans3 = w3*
Sum[Product[phiH[[j]], {j, fi, d - 1}]*(1 - phiH[[d]])*
Product[pL[[j]]^xij[[j]]*(1 - pL[[j]])^(1 - xij[[j]]), {j,
fi + 1, d}], {d, li, K}];

ans4 = (1 - w1 - w2 - w3)*
Sum[Product[phiH[[j]], {j, fi, d - 1}]*(1 - phiH[[d]])*
Product[pH[[j]]^xij[[j]]*(1 - pH[[j]])^(1 - xij[[j]]), {j,
fi + 1, d}], {d, li, K}];

ans = ans1 + ans2 + ans3 + ans4
]

data = Import["female_unique_sort.csv", "CSV"];
xij = data[[All, 1 ;; 9]];
feq = data[[All, 10]];

kappa = FourC[#] & /@ xij;
loglik = feq.Log[kappa];
pars = Variables[Level[kappa, {-2}]]
Length[pars]

cons = {
Subscript[\[Eta], \[Phi]] >= 0,
Subscript[\[Eta], p] >= 0,
0 < w1 < 1,
0 < w2 < 1,
0 < w3 < 1,
0 < w1 + w2 + w3 < 1
}

ans = NMaximize[
{loglik, cons}, pars
]


This is the full code. And when it runs, it has a warning:

Clearly, it's trying something, that does not satisfy the constraints (0<w1+w2+w3<1). I am not sure whether it stopped due to the violation (of the function value not being a real number) OR it stopped because the optimization is done, and the warning is just to note.

But I am really confused by the ways to input constraints. Is there a way to force these constraints, BEFORE it evaluates the 'loglik' (target function)?

Thanks!

• You are neither showing complete input nor output. Impossible to say what might be going on, or whether it is expected behavior. – Daniel Lichtblau Oct 3 '14 at 15:53
• @DanielLichtblau Sorry if that was not clear for you. My main question is, I want to know, how to put constraints 'correctly'. The screenshot should have shown that [Out12]` is the 'cons', I put. But during the optimization, it is using the values, w1+w2+w3>1. That is something I don't quit understand. How should I put constraints correctly? Is there a way to upload CSV file?So I can use my full example. Hope you would get what I am asking for. Thanks. – Chen Stats Yu Oct 3 '14 at 16:02
• I think your constraints are probably being interpreted correctly. Generally one should use either a list or a conjunction (your second variant has both), but it will probably get parsed regardless. – Daniel Lichtblau Oct 3 '14 at 16:07
• @DanielLichtblau Assuming all the ways are acceptable, why during the optimization, it would evaluate the function, when the constraint 0<w1+w2+w3<1 is NOT true? That's why I am confused. – Chen Stats Yu Oct 3 '14 at 16:09
• @Artes I dont think it's a duplicate. In my case, the relationship is not exactly equivalent. I can't replace w3=1-w1-w2 as this is not true in this case. – Chen Stats Yu Oct 3 '14 at 16:22