Problem :
Let x,y,z be non-negative real number whose sum is 1.
Find maximum of 0.2x + 0.2y + x^2*z + y*z^2
What I did first was reducing variable.
0.2x + 0.2y + x^2(1-x-y) + y(1-x-y)^2 with constraint
x>=0, y>=0, x+y<=1
Mathematica code is
NMaximize[{0.2x+0.2y+x^2(1-x-y)+y(1-x-y)^2,0<=x<=1&&0<=y<=1&&x+y<=1},{x,y}]
and I got
{0.226147,{x->0.,y->0.455858}}
But it is not a global maximum.
In fact, global maximum is
{0.290656, {x->0.754955, y->0.}}
I know that NMaximize tries to give us global maximum, but it can fail. And if fail, mathematica gives us local maximum.
But, I didn't expect NMaximize could fail to find the global maximum for such a easy problem.
Q1) Is it possible to output an additional message:
{0.226147,{x->0.,y->0.455858}}
Bad luck for NMaximize : Failed to ensure this is the global maximum
Q2) Is there a sure way even if it takes more time ? (finding global maximum)
Below is a helpful code... but they are like seeing the results first and fitting things
In[1]:= NMaximize[{0.2x+0.2y+x^2(1-x-y)+y (1-x-y)^2,0<=x<=1&&0<=y<=1&&x+y<=1},{x,y}]
Out[1]= {0.226147,{x->0.,y->0.455858}}
(*Failed*)
In[2]:= NMaximize[{0.2x+0.2y+x^2z+y z^2,0<=x<=1&&0<=y<=1&&0<=z<=1&&x+y+z==1},{x,y,z}]
Out[2]= {0.290656,{x->0.754955,y->0.,z->0.245045}}
(*Wow, it works for 3 variables! The original problem*)
In[3]:= NMaximize[{0.2x+0.2y+x^2(1-x-y)+y (1-x-y)^2,0.01<=x<=1&&0<=y<=1&&x+y<=1},{x,y}]
Out[3]= {0.290656,{x->0.754974,y->0.}}
(*Changing 0 to 0.01 works! *)
In[4]:= NMaximize[{0.2x+0.2y+x^2(1-x-y)+y (1-x-y)^2,0.001<=x<=1&&0<=y<=1&&x+y<=1},{x,y}]
Out[4]= {0.225852,{x->0.001,y->0.455708}}
(*0.001 not works*)
======================================= After a getting a comment from from Akku14 :
Thank you, WorkingPrecision works, but surprisingly it is
not true
that "the higher, the better".
See below. I recommend to try with some range of numbers (for WorkingPrecision)
Below is a photo magnified :
NMaximize[{0.2 x + 0.2 y + x^2 (1 - x - y) + y (1 - x - y)^2, 0 <= x <= 1 && 0 <= y <= 1 && x + y <= 1}, {x, y}, WorkingPrecision -> 15]
yields{0.290656125830494, {x -> 0.754985264404849, y -> 0}}
$\endgroup$Plot3D[ConditionalExpression[0.2 x + 0.2 y + x^2 (1 - x - y) + y (1 - x - y)^2, 0 <= x <= 1 && 0 <= y <= 1 && x + y <= 1], {x, 0, 1}, {y, 0, 1}]
show why it's reasonable thatNMaximize
could find either point. Also, it suggests thatWorkingPrecision
is irrelevant. More relevant are the random points it starts with, as well as the method and the method's tuning parameters. CompareMethod -> {"NelderMead", "RandomSeed" -> 1}
with the method used by default here,Method -> {"NelderMead"}
.... $\endgroup$SeedRandom[0]; RandomReal[1, 3]
andSeedRandom[0]; RandomReal[1, 3, WorkingPrecision -> 15]
shows thatWorkingPrecision
affects random number generation, which accounts for it affecting the result. $\endgroup$