Hill-climbing optimisation
Global maximum
Let’s try it with the function
$$ f(x,y) = e^{-\left(x^2+y^2\right)} $$
import numpy as np
import matplotlib.pyplot as plt
from plotutils import *
x = np.linspace(-2.0, 2.0, 1000)
y = np.linspace(-2.0, 2.0, 1000)
X, Y = np.meshgrid(x, y)
Z = np.exp(-(X ** 2 + Y ** 2))
fig, ax = plt.subplots(1, 1)
cp = ax.contourf(X, Y, Z, cmap=cmaps[1])
ax.set_title("f(x,y)")
ax.set_xlabel("x")
ax.set_ylabel("y")
plt.show()
from gradient_free_optimizers import HillClimbingOptimizer
search_space = {
"x": x,
"y": y,
}
opt = HillClimbingOptimizer(search_space)
def f(pos):
x = pos["x"]
y = pos["y"]
z = np.exp(-(x**2 + y**2))
return z
result = opt.search(f,
n_iter=30000,
verbosity=['print_times'])
Evaluation time : 0.9117169380187988 sec [28.97 %]
Optimization time : 2.235219955444336 sec [71.03 %]
Iteration time : 3.1469368934631348 sec [9533.08 iter/sec]
opt.best_para
{'x': -0.002002002002001957, 'y': 0.002002002002002179}
Local maximum
Let’s try it with the function
$$ f(x,y) = e^{-\left(x^2+y^2\right)}+2e^{-\left((x-1.7)^2+(y-1.7)^2\right)} $$
x = np.linspace(-1.0, 3.0, 1000)
y = np.linspace(-1.0, 3.0, 1000)
X, Y = np.meshgrid(x, y)
Z = np.exp(-(X**2 + Y**2))+2*np.exp(-((X-1.7)**2+(Y-1.7)**2))
fig,ax=plt.subplots(1,1)
cp = ax.contourf(X, Y, Z, cmap=cmaps[1])
ax.set_title('f(x,y)')
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()
opt = HillClimbingOptimizer(search_space)
def f(pos):
x = pos["x"]
y = pos["y"]
z = np.exp(-(x**2 + y**2))+2*np.exp(-((x-1.7)**2+(y-1.7)**2))
return z
result = opt.search(f,
n_iter=30000,
verbosity=['print_times'])
Evaluation time : 0.9092590808868408 sec [29.73 %]
Optimization time : 2.148698091506958 sec [70.27 %]
Iteration time : 3.057957172393799 sec [9810.47 iter/sec]
opt.best_para
{'x': 1.6956956956956954, 'y': 1.6956956956956954}