Random Search

A computational method for optimizing a function of two variables over a rectangle in the plane

M. Meerschaert's Mathematical Modeling, 2nd Edition

Define the rectangle over which we optimize

a=0;  (*lower limit on x*)
b=6;  (*upper limit on x*)
c=0;  (*lower limit on y*)
d=6;  (*upper limit on y*)

Define the function to be optimized

f[x_,y_]:=3.2 + 1.7/84*(6( (x-1)^2+(y-5)^2  )^.455
+8( (x-3)^2+(y-5)^2)^.455
+ 8( (x-5)^2+(y-5)^2)^.455
+ 21( (x-1)^2+(y-3)^2)^.455
+ 6( (x-3)^2+(y-3)^2)^.455
+ 3( (x-5)^2+(y-3)^2)^.455
+ 18( (x-1)^2+(y-1)^2)^.455
+ 8( (x-3)^2+(y-1)^2)^.455
+ 6( (x-5)^2+(y-1)^2)^.455 )

A manual effort so we can see what's happening.

Somehow assign an approximation of the x and y locations for the minimum value of z.

In this case, we randomly choose the initial values.

index=0
numIterations=0
xmin[index]=RandomReal[{a,b}]
ymin[index]=RandomReal[{c,d}]
zmin[index]=f[xmin[index],ymin[index]]

Here are some iterations by hand.

numIterations=numIterations+1
x=RandomReal[{a,b}]
y=RandomReal[{c,d}]
z=f[x,y]

If[z<zmin[index],
	index=index+1;
	xmin[index]=x;
	ymin[index]=y;
	zmin[index]=z]
index
xmin[index]
ymin[index]
zmin[index]

numIterations=numIterations+1
x=RandomReal[{a,b}]
y=RandomReal[{c,d}]
z=f[x,y]
 If[z<zmin[index],
    index=index+1;
    xmin[index]=x;
    ymin[index]=y;
    zmin[index]=z] 
index 
xmin[index] 
ymin[index] 
zmin[index]

numIterations=numIterations+1
x=RandomReal[{a,b}]
y=RandomReal[{c,d}]
z=f[x,y]
 If[z<zmin[index],
    index=index+1;
    xmin[index]=x;
    ymin[index]=y;
    zmin[index]=z] 
index 
xmin[index] 
ymin[index] 
zmin[index]

numIterations=numIterations+1
x=RandomReal[{a,b}]
y=RandomReal[{c,d}]
z=f[x,y]
 If[z<zmin[index],
    index=index+1;
    xmin[index]=x;
    ymin[index]=y;
    zmin[index]=z] 
index 
xmin[index] 
ymin[index] 
zmin[index]

numIterations=numIterations+1
x=RandomReal[{a,b}]
y=RandomReal[{c,d}]
z=f[x,y]
 If[z<zmin[index],
    index=index+1;
    xmin[index]=x;
    ymin[index]=y;
    zmin[index]=z] 
index 
xmin[index] 
ymin[index] 
zmin[index]

numIterations=numIterations+1
x=RandomReal[{a,b}]
y=RandomReal[{c,d}]
z=f[x,y]
 If[z<zmin[index],
    index=index+1;
    xmin[index]=x;
    ymin[index]=y;
    zmin[index]=z] 
index 
xmin[index] 
ymin[index] 
zmin[index]

numIterations=numIterations+1
x=RandomReal[{a,b}]
y=RandomReal[{c,d}]
z=f[x,y]
 If[z<zmin[index],
    index=index+1;
    xmin[index]=x;
    ymin[index]=y;
    zmin[index]=z] 
index 
xmin[index] 
ymin[index] 
zmin[index]

Is this working?  Let's plug in the optimal values found in the text just to see.

numIterations=numIterations+1
x=RandomReal[{a,b}]
y=RandomReal[{c,d}]
z=f[x,y]
 If[z<zmin[index],
    index=index+1;
    xmin[index]=x;
    ymin[index]=y;
    zmin[index]=z] 
index 
xmin[index] 
ymin[index] 
zmin[index]

That seemed to work.  

The text used 1000 iterations.  That's hard to do by hand.

Here's a more automated effort.

Somehow assign an approximation of the x and y locations for the minimum value of z.

index=0
xmin[index]=RandomReal[{a,b}]
ymin[index]=RandomReal[{c,d}]
zmin[index]=f[xmin[index],ymin[index]]

One thousand iterations and a list of the estimates of the minimum.

Note that in 1000 tries, the estimate was updated only a few times.

For[numIterations=0,numIterations≤1000,numIterations=numIterations+1;x=RandomReal[{a,b}];
y=RandomReal[{c,d}];
z=f[x,y];
If[z<zmin[index],
    index=index+1;
    xmin[index]=x;
    ymin[index]=y;
    zmin[index]=z]
]

TableForm[Table[{i,xmin[i],ymin[i],zmin[i]},{i,0,index}]]

Show[{Plot3D[f[x,y],{x,a,b},{y,c,d},AxesLabel->{"x","y","z"}],
ListPointPlot3D[{{1.469,2.8,6.466}},PlotStyle->{PointSize[.03],Red}]  }]

Show[{ContourPlot[f[x,y],{x,a,b},{y,c,d},AxesLabel->{"x","y","z"}],
ListPlot[{{1.469,2.8}},PlotStyle->{PointSize[.03],Red}]  }]