2
$\begingroup$

I have problems with some applications for finding convergence - below is one example of failure for {x=0.35,y=0.35,z=0.3}, but it works fine for {x=0.3,y=0.3,z=0.4}.

How can I increase the iterations or change the precision in this structure?

Clear["Global`*"]
SetDirectory[NotebookDirectory[]]
f[a_, b_, c_, x1_, x2_, x3_, y1_, y2_, y3_, z1_, z2_, z3_] := (a*x1)/(
  a*x1 + b*y1 + c*z1) + (a*x2)/(a*x2 + b*y2 + c*z2) + (a*x3)/(
  a*x3 + b*y3 + c*z3)
x = 0.35
y = 0.35
z = 0.3

g1[x1_, x2_, x3_] := x1 + x2 + x3 - 3*x
g2[y1_, y2_, y3_] := y1 + y2 + y3 - 3*y
g3[z1_, z2_, z3_] := z1 + z2 + z3 - 3*z
g4[x1_, y1_, z1_] := x1 + y1 + z1 - 1
g5[x2_, y2_, z2_] := x2 + y2 + z2 - 1
g6[x3_, y3_, z3_] := x3 + y3 + z3 - 1
inputs = {
    {1, 5, 1},
    {3, 5, 1},
    {5, 5, 1},
    {7, 5, 1},
    {10, 5, 1},
    {1, 5, 3},
    {3, 5, 3},
    {5, 5, 3},
    {7, 5, 3},
    {10, 5, 3},
    {1, 5, 5},
    {3, 5, 5},
    {5, 5, 5},
    {7, 5, 5},
    {10, 5, 7},
    {1, 5, 7},
    {3, 5, 7},
    {5, 5, 7},
    {7, 5, 7},
    {10, 5, 7},
    {1, 5, 10},
    {3, 5, 10},
    {5, 5, 10},
    {7, 5, 10},
    {10, 5, 10}
   } // Rationalize;

(table = 
   Prepend[Flatten[{#, {#[[1]], Values[#[[2]]]} &@
         Maximize[{f[#[[1]], #[[2]], #[[3]], x1, x2, x3, y1, y2, y3, z1, z2, z3],
           g1[x1, x2, x3] == 0, g2[y1, y2, y3] == 0, g3[z1, z2, z3] == 0,
           g4[x1, y1, z1] == 0, g5[x2, y2, z2] == 0, g6[x3, y3, z3] == 0,
           x1 >= 0, x2 >= 0, x3 >= 0, y1 >= 0, y2 >= 0, y3 >= 0, 
           z1 >= 0, z2 >= 0, z3 >= 0, x1 <= 1, x2 <= 1, x3 <= 1, 
           y1 <= 1, y2 <= 1, y3 <= 1, z1 <= 1, z2 <= 1, z3 <= 1},
          {x1, y1, z1, x2, y2, z2, x3, y3, z3}, Reals]}]
               & /@ inputs, {"a", "b", "c", "f", "x1", "y1", "z1", 
     "x2", "y2", "z2", "x3", "y3", "z3"}]) //
          Grid[#, Frame -> All] &

Export["Output/table.xls", table /. r_Rational :> N[r, 2], "xls"]

Error message -- NMaximize: Failed to converge the requested accuracy or precision within 100 iterations.

Thank you!

$\endgroup$
2
  • 1
    $\begingroup$ reference.wolfram.com/language/ref/NMaximize.html and click on the orange Details and Options and scroll down a bit to find MaxIterations. Then you can try inserting something like MaxIterations->10^3and see if that helps. You can also change your decimal constants (which are limited to MachinePrecision) to rationals (which have infinite precision) which will allow you to insert something like WorkingPrecision->64 and see if that helps. Bump these up or down a little at a time, not thinking a billion iterations should be better or a million digits of precision should be better $\endgroup$
    – Bill
    Commented Dec 19, 2022 at 19:52
  • $\begingroup$ It works after changing from Maximize to NMaximize and 500 iterations - thank you! $\endgroup$
    – Tom
    Commented Dec 19, 2022 at 21:54

1 Answer 1

3
$\begingroup$

Only the input {a,b,c}={5,5,5} results in an error, and we can understand why: In that case the function is constant, given the constraints, and that can lead to numerical issues for NMaximize.

One solution is to simplify the function in the case a===b===c:

max[{x_,y_,z_}][{a_,b_,c_}]:=NMaximize[{
    If[a===b===c,
        3*x,
        (a*x1)/(a*x1+b*y1+c*z1)+(a*x2)/(a*x2+b*y2+c*z2)+(a*x3)/(a*x3+b*y3+c*z3)],
    Thread[{x1+x2+x3,y1+y2+y3,z1+z2+z3}==3*{x,y,z}],
    Thread[{x1+y1+z1,x2+y2+z2,x3+y3+z3}==1],
    Thread[0<={x1,y1,z1,x2,y2,z2,x3,y3,z3}<=1]},
    {x1,y1,z1,x2,y2,z2,x3,y3,z3}];

Then

Map[max[{.35,.35,.3}],
   {{1,5,1},{3,5,1},{5,5,1},{7,5,1},{10,5,1},
    {1,5,3},{3,5,3},{5,5,3},{7,5,3},{10,5,3},
    {1,5,5},{3,5,5},{5,5,5},{7,5,5},{10,5,7},
    {1,5,7},{3,5,7},{5,5,7},{7,5,7},{10,5,7},
    {1,5,10},{3,5,10},{5,5,10},{7,5,10},{10,5,10}}]

runs without error. OP should be aware of this sentence in the documentation:

If f is linear or concave and cons are linear or convex, the result given by NMaximize will be the global maximum, over both real and integer values; otherwise, the result may sometimes only be a local maximum.

$\endgroup$
1
  • $\begingroup$ I deleted the {5,5,5} in inputs, and it works now for the parameter values above. But it doesn't converge for {0.25,.4,0.35}. So there must be something else causing it (too). $\endgroup$
    – Tom
    Commented Dec 19, 2022 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.