In [ 58 ]: def fo((x, y)):
z = np.sin(x) + 0.05 * x ** 2 + np.sin(y) + 0.05 * y ** 2
if output == True:
print ‘%8.4f %8.4f %8.4f’ % (x, y, z)
return z
This allows us to keep track of all relevant information for the procedure, as the following
code with its respective output illustrates. brute takes the parameter ranges as input. For
example, providing parameter range (-10, 10.1, 5) for the x value will lead to “tested”
values of -10, -5, 0, 5, 10:
In [ 59 ]: output = True
spo.brute(fo, ((- 10 , 10.1, 5 ), (- 10 , 10.1, 5 )), finish=None)
Out[59]: -10.0000 -10.0000 11.0880
-10.0000 -10.0000 11.0880
-10.0000 -5.0000 7.7529
-10.0000 0.0000 5.5440
-10.0000 5.0000 5.8351
-10.0000 10.0000 10.0000
-5.0000 -10.0000 7.7529
-5.0000 -5.0000 4.4178
-5.0000 0.0000 2.2089
-5.0000 5.0000 2.5000
-5.0000 10.0000 6.6649
0.0000 -10.0000 5.5440
0.0000 -5.0000 2.2089
0.0000 0.0000 0.0000
0.0000 5.0000 0.2911
0.0000 10.0000 4.4560
5.0000 -10.0000 5.8351
5.0000 -5.0000 2.5000
5.0000 0.0000 0.2911
5.0000 5.0000 0.5822
5.0000 10.0000 4.7471
10.0000 -10.0000 10.0000
10.0000 -5.0000 6.6649
10.0000 0.0000 4.4560
10.0000 5.0000 4.7471
10.0000 10.0000 8.9120
array([ 0., 0.])
The optimal parameter values, given the initial parameterization of the function, are x = y
= 0. The resulting function value is also 0, as a quick review of the preceding output
reveals. The first parameterization here is quite rough, in that we used steps of width 5 for
both input parameters. This can of course be refined considerably, leading to better results
in this case:
In [ 60 ]: output = False
opt1 = spo.brute(fo, ((- 10 , 10.1, 0.1), (- 10 , 10.1, 0.1)), finish=None)
opt1
Out[60]: array([-1.4, -1.4])
In [ 61 ]: fm(opt1)
Out[61]: -1.7748994599769203
The optimal parameter values are now x = y = –1.4 and the minimal function value for the
global minimization is about –1.7749.
Local Optimization
For the local convex optimization we want to draw on the results from the global
optimization. The function fmin takes as input the function to minimize and the starting
parameter values. In addition, you can define levels for the input parameter tolerance and
the function value tolerance, as well as for the maximum number of iterations and function
calls: