Commit cbc9cdea authored by Grégoire Grzeczkowicz's avatar Grégoire Grzeczkowicz
Browse files

Keep best value for simulated_annealing

parent 11e008b5
......@@ -38,20 +38,23 @@ def greedy(func, init, neighb, again):
def simulated_annealing(func, init, neighb, temp, again):
"""Iterative randomized simulated-annealing template."""
best_sol = init()
best_val = func(best_sol)
val,sol = best_val,best_sol
old_sol = init()
old_val = func(old_sol)
best_val,best_sol = old_val,old_sol
i = 1
while again(i, best_val, best_sol):
sol = neighb(best_sol)
val = func(sol)
while again(i, old_val, old_sol):
new_sol = neighb(old_sol)
new_val = func(new_sol)
# Use >= and not >, so as to avoid random walk on plateus.
if val >= best_val:
best_val = val
best_sol = sol
elif np.random.random() < np.exp(-(best_val-val)/temp(i, best_val, best_sol)):
best_val = val
best_sol = sol
if new_val >= old_val:
old_val = new_val
old_sol = new_sol
elif np.random.random() < np.exp(-(old_val-new_val)/temp(i, old_val, old_sol)):
old_val = new_val
old_sol = new_sol
if new_val >= best_val:
best_val = new_val
best_sol = new_sol
i += 1
return best_val, best_sol
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment