NMaximize not solving my problem

Check the following, integer problem:

NMaximize[{3*0.5*(80 - P)*Subscript[Q, 1]*z1 +
2*0.5*(125 - P)*Subscript[Q, 2]*z2 +
1*0.5*(180 - 2*P)*Subscript[Q, 3]*z3,
Subscript[Q, 1] == (80 - P) y1,
Subscript[Q, 2] == (100 - 0.8 P) y2,
Subscript[Q, 3] == (90 - 0.5 P) y3, 20 <= P <= 90, 1 >= y1 >= 0,
1 >= y2 >= 0, 1 >= y3 >= 0, 3 >= y1 + y2 + y3 >= 2, 1 >= z1 >= 0,
1 >= z2 >= 0, 1 >= z3 >= 0, z1 + z2 + z3 == 1,
3 <= y1 + y2 + y3 + z1 + z2 + z3 <= 4, y1 + y2 + y3 + z2 <= 3,
y1 + y2 + y3 + z3 <= 3, Subscript[Q, 1] >= 0, Subscript[Q, 2] >= 10,
Subscript[Q, 3] >= 10, {y1, y2, y3, z1, z2, z3} \[Element]
Integers}, {P, Subscript[Q, 2], Subscript[Q, 3], Subscript[Q, 1],
y1, y2, y3, z1, z2, z3}]


Mathematica fails to find the optimal solution, which is

P->20, Q2->84, Q3->80, Y2->1, Y3->1, Z2->1

LINGO finds the global optimal solution automatically. I have tried with all methods provided in tutorial, but it did not help, unless I increased the lower bound to about Q2 > 60 and Q3 >60.

Any help?

I noticed that if I did not use Q1, Q2, or Q3 as intermediate values in the maximization then it worked:

NMaximize[{
3*0.5*(80-P)*(80-P)*y1*z1 + 2*0.5*(125-P)*(100-0.8P)*y2*z2 + 1*0.5*(180-2P)*(90 - 0.5)*y3*z3,
20 <= P <= 90,
0 <= y1 <= 1,
0 <= y2 <= 1,
0 <= y3 <= 1,
2 <= y1 + y2 + y3 <= 3,
0 <= z1 <= 1,
0 <= z2 <= 1,
0 <= z3 <= 1,
z1 + z2 + z3 == 1,
3 <= y1 + y2 + y3 + z1 + z2 + z3 <= 4,
y1 + y2 + y3 + z2 <= 3,
y1 + y2 + y3 + z3 <= 3,
(80 - P) y1 >= 0,
(100 - 0.8 P) y2 >= 10,
(90 - 0.5 P) y3 >= 10,
{y1, y2, y3, z1, z2, z3} \[Element] Integers},
{P, y1, y2, y3, z1, z2, z3}
]


This gave me the value:

{8820., {P->20, y1->0, y2->1, y3->1, z1->0, z2->1, z3->0}}


It appears that the constraints should only contain independent variables, and no dependent variables.

• Michael, thanks very much for that. It helps a lot, but still the problem was not to find out the integer values, but the Q-values. I formulated it so that the integer (binary) will force to obtain the correct Q-values. I am surprised though that LINGO finds both the independent and dependent variables in a millisecond! – AC Milan Jan 16 at 9:14
• @ACMilan max=NMaximize[...];{P, y1, y2, y3, z1, z2, z3} = {P, y1, y2, y3, z1, z2, z3} /. Last@max;Q1 = (80 - P) y1 Q2 = (100 - 0.8 P) y2 Q3 = (90 - 0.5 P) y3 gives exactly the LINGO's solution. – OkkesDulgerci Jan 16 at 13:41

Avoid using subscripts!

NMaximize[{3*0.5*(80 - P)*Q1*z1 + 2*0.5*(125 -P)*Q2*z2 +
1*0.5*(180 - 2*P)*Q3*z3, Q1 == (80 - P) y1, Q2 ==
(100 - 0.8 P) y2,
Q3 == (90 - 0.5 P) y3, 20 <= P <= 90, 1 >= y1 >= 0, 1
>= y2 >= 0,
1 >= y3 >= 0, 3 >= y1 + y2 + y3 >= 2, 1 >= z1 >= 0, 1
>= z2 >= 0,
1 >= z3 >= 0, z1 + z2 + z3 == 1,
3  <= y1 + y2 + y3 + z1 + z2 + z3 <= 4, y1 + y2 + y3
+ z2 <= 3,
y1 + y2 + y3 + z3 <= 3, Q1 >= 0, Q2 >= 10,
Q3 >= 10, {y1, y2, y3, z1, z2, z3} \[Element]
Integers}, {P, Q2, Q3,
Q1, y1, y2, y3, z1, z2, z3}]

(*{0.0741345, {P -> 79.7791, Q2 -> 10., Q3 -> 10., Q1-> 0.22375,y1 -> 1, y2 -> 0, y3 -> 0, z1 -> 1, z2 -> 0, z3 ->0}}*)

• Ulrich,Subscripts make no difference; that is not the optimal solution! It is exactly the one NMaximize gives, which is wrong. – AC Milan Jan 16 at 9:27
• @ACMilan Obviously we get a different solution without subscripts... – Ulrich Neumann Jan 16 at 9:45

Since Binary domain is small we can use this approach.

pts = Select[
Tuples[{0, 1},
6], ((2 <= #[[1]] + #[[2]] + #[[3]] <=
3) && (#[[4]] + #[[5]] + #[[6]] == 1) && (3 <= Total@# <=
4) && (#[[1]] + #[[2]] + #[[3]] + #[[5]] <=
3) && (#[[1]] + #[[2]] + #[[3]] + #[[6]] <= 3)) &]


This gives All tuples with the constrains.

{y1, y2, y3, z1, z2, z3}={{0, 1, 1, 0, 0, 1}, {0, 1, 1, 0, 1, 0}, {0, 1, 1, 1, 0, 0}, {1, 0, 1, 0, 0, 1}, {1, 0, 1, 0, 1, 0}, {1, 0, 1, 1, 0, 0}, {1, 1, 0, 0, 0, 1}, {1, 1, 0, 0, 1, 0}, {1, 1, 0, 1, 0, 0}, {1, 1, 1, 1, 0, 0}}

Now we Maximize the obj func for each set of {y1, y2, y3, z1, z2, z3}

Table[ClearAll[y1, y2, y3, z1, z2, z3];
{y1, y2, y3, z1, z2, z3} = pts[[i]];
NMaximize[{3/2*(80 - P)*Q1*z1 + (125 - P)*Q2*z2 +
1/2*(180 - 2*P)*Q3*z3, Q1 == (80 - P) y1, Q2 == (100 - 4/5 P) y2,
Q3 == (90 - 1/2 P) y3, 20 <= P <= 90, Q1 >= 0, Q2 >= 10,
Q3 >= 10}, {P, Q1, Q2, Q3}], {i, Length@pts}] // Quiet


{{5600., {P -> 20., Q1 -> 0, Q2 -> 84., Q3 -> 80.}}, {8820., {P -> 20., Q1 -> 0, Q2 -> 84., Q3 -> 80.}}, {0., {P -> 20.092, Q1 -> 0, Q2 -> 83.9264, Q3 -> 79.954}}, {-[Infinity], {P -> Indeterminate, Q1 -> Indeterminate, Q2 -> Indeterminate, Q3 -> Indeterminate}}, {-[Infinity], {P -> Indeterminate, Q1 -> Indeterminate, Q2 -> Indeterminate, Q3 -> Indeterminate}}, {-[Infinity], {P -> Indeterminate, Q1 -> Indeterminate, Q2 -> Indeterminate, Q3 -> Indeterminate}}, {-[Infinity], {P -> Indeterminate, Q1 -> Indeterminate, Q2 -> Indeterminate, Q3 -> Indeterminate}}, {-[Infinity], {P -> Indeterminate, Q1 -> Indeterminate, Q2 -> Indeterminate, Q3 -> Indeterminate}}, {-[Infinity], {P -> Indeterminate, Q1 -> Indeterminate, Q2 -> Indeterminate, Q3 -> Indeterminate}}, {5400., {P -> 20., Q1 -> 60., Q2 -> 84., Q3 -> 80.}}}

It is clear that Max value is 8820 with the values {P -> 20., Q1 -> 0, Q2 -> 84., Q3 -> 80.} which is second solution. So {y1, y2, y3, z1, z2, z3} ={0, 1, 1, 0, 1, 0}

• Good approach (+1). I am not sure, though, is the OP question about (i) finding a solution with Mathematica or (ii) explaining or tweaking the behavior of NMaximize in order to find a solution. – Anton Antonov Jan 16 at 15:22
• Hi Anton. Exactly, my question was why NMaximize fails for such a small problem. I love Mathematica by the way! – AC Milan Jan 17 at 21:05