I implemented simulated annealing to find global minima of a given function using
https://perso.crans.org/besson/publis/notebooks/Simulated_annealing_in_Python.html
but although the temperature is high at first and then decreases slowly (cause of steps), but it some times gives me the wrong answer.(local minima)
I have to add I tried to solve the problem using random start hill climbing and below is the list of local minimums in given interval:
x = 0.55 0.75 0.95 1.15 1.35 1.54 1.74 1.94 2.14 2.34 2.5
y = -0.23 -0.37 -0.47 -0.57 -0.66 -0.68 -0.55 -0.16 0.65 2.10 5.06
and optimize.basinhopping()
prove that the global minima is (1.54, -.68)
here is the code:
import math
import numpy as np
import numpy.random as rn
import matplotlib.pyplot as plt # to plot
from scipy import optimize # to compare
import seaborn as sns
def annealing(random_start, func, func_interval, random_neighbour, acceptance, temperature, maxsteps=1000, debug=True):
""" Optimize the black-box function 'func' with the simulated annealing algorithm."""
x = random_start(func_interval)
y = func(x)
x_list, y_list = [x], [y]
for step in range(maxsteps):
fraction = step / float(maxsteps)
T = temperature(fraction)
new_x = random_neighbour(x, func_interval, fraction)
new_y = func(new_x)
if debug: print("Step #{:>2}/{:>2} : T = {:>4.3g}, x = {:>4.3g}, y = {:>4.3g}, new_x = {:>4.3g}, new_y = {:>4.3g} ...".format(step, maxsteps, T, x, y, new_x, new_y))
if acceptance_probability(y, new_y, T) > rn.random():
x, y = new_x, new_y
x_list.append(x)
y_list.append(y)
# print(" ==> Accept it!")
# else:
# print(" ==> Reject it...")
return x, func(x), x_list, y_list
def clip(x, func_interval):
""" Force x to be in the interval."""
a, b = func_interval
return max(min(x, b), a)
def random_start(func_interval):
""" Random point in the interval."""
a, b = func_interval
return a + (b - a) * rn.random_sample()
def random_neighbour(x, func_interval, fraction=1):
"""Move a little bit x, from the left or the right."""
amplitude = (max(func_interval) - min(func_interval)) * fraction / 10
delta = (-amplitude/2.) + amplitude * rn.random_sample()
return clip(x + delta, func_interval)
def acceptance_probability(y, new_y, temperature):
if new_y < y:
# print(" - Acceptance probabilty = 1 as new_y = {} < y = {}...".format(new_y, y))
return 1
else:
p = np.exp(- (new_y - y) / temperature)
# print(" - Acceptance probabilty = {:.3g}...".format(p))
return p
def temperature(fraction):
""" Example of temperature dicreasing as the process goes on."""
return max(0.01, min(1, 1 - fraction))
def see_annealing(x, y, x_list, y_list):
sns.set(context="talk", style="darkgrid", palette="hls", font="sans-serif", font_scale=1.05)
xs = np.linspace(func_interval[0], func_interval[1], 1000) # Get 1000 evenly spaced numbers between .5 and 2.5
plt.plot(xs, np.vectorize(func)(xs))
plt.scatter(x_list, y_list, c="b")
plt.scatter(x, y, c="r")
plt.title("Simulated annealing")
plt.show()
if __name__ == '__main__':
func = lambda x: math.sin(10 * math.pi * x) / 2 * x + (x - 1) ** 4
func_interval = (.5, 2.5)
x, y, x_list, y_list = annealing(random_start, func, func_interval, random_neighbour, acceptance_probability, temperature, maxsteps=1000, debug=False)
see_annealing(x, y, x_list, y_list)
print(x, y)
print(optimize.basinhopping(lambda x: math.sin(10*math.pi*x)/2*x + (x-1)**4, [random_start(func_interval)]))
But What's wrong?
Edit:
@user3184950 you are right, the algorithm is working better now but here is the pseudo code from AIMA third edition
next is just a random selected successor of current.
In addition I wrote a note from my AI course that simulated annealing is guaranteed to converge to the global maximum if we start T high and decrease it slowly enough.(I mean my professor didn't say anything about the "next point" or I missed it somehow or maybe it just doesn't matter).
By the way, I was thinking the problem is with the chance of taking "next point", if both y and new_y be negative then the probability of taking next point is high even if T be small enough. for example
as you can see at the step 891 both y and new_y are negative and we take the new_y, however T is 0.109
Again the problem is, the probability formula given in the pseudo code is same as the probability formula which I used in my code
It seems the neighbour is not optimal.
def random_neighbour(x, func_interval, fraction=1):
"""Move a little bit x, from the left or the right."""
amplitude = (max(func_interval) - min(func_interval)) * 1 / (fraction + 0.1)
delta = 1 * amplitude * (.5 - rn.random_sample())
print(delta)
return clip(x + delta, func_interval)
You need something that will move to left/right with equal probability, but might move more random at beginning of the annealing, and less towards end.
The above is just a proposal that seem to work better.