Simulated Annealing is an optimization algorithm that simulates the annealing process in the thermodynamics energetics. The basic ideas is that any object will go through the state transition from the high temperature to the low one. And accordingly the inner energy will decrease to a low level, with a stable state. In the SA, the target function is recognized as the energy function. The search of the optimal solution is a simulation that the system temperature lows from a high level, and the energy function becomes stable too.
In SA, there is an important criterion, namely Metropolis. In the high temperature, the system can accept the new state with large difference from current one; while in the low temperature, the system almost accepts the new state with little difference from the current one. When the temperature approaches zero, then any state with a higher energy than current one will be rejected. This properties ensure that SA can accept the inferior solution, and avoid getting trapped in the local optimal solution. SA is a simple heuristic algorithm with a powerful area search and local search, and presents a developing trends.
# -*- coding: utf-8 -*-
"""
Created on 2017/3/16 20:16 2017
@author: Randolph.Lee
"""
import random
import copy
import numpy as np
import sys
class SimulatedAnnealing:
def __init__(self, graph_mat, param, inner_iter, T_start, T_stop, initial_solution):
self.graph = graph_mat
self.T_start = T_start
self.T_stop = T_stop
self.param = param
self.inner_iter = inner_iter
self.current_temperature = T_start
self.current_solution = initial_solution
self.best_solution = []
self.best_fit = sys.maxint
def cal_fitness(self, solution):
# calculate the total length of path
return reduce(lambda x, y: x + y, [self.graph[i, j] for i in solution[:-1] for j in solution[1:]])
def generate_candidate(self):
# adopt the interpolation method
select_index = random.randint(0, len(self.current_solution)-1)
copy_solution = copy.deepcopy(self.current_solution)
del copy_solution[select_index]
insert_position = random.sample(filter(lambda x: x != select_index, range(len(self.current_solution))), 1)
return copy_solution[:insert_position] + [self.current_solution[select_index]] + copy_solution[insert_position:]
def update_temperature(self):
# adopt the scale-down strategy
self.current_temperature *= self.param
def inner_loop(self):
step = 0
while step < self.inner_iter:
# generate a new variable, therefore it won't influence the self.current_solution
candidate = self.generate_candidate()
delta = self.cal_fitness(candidate) - self.cal_fitness(self.current_solution)
if delta < 0:
self.current_solution = candidate
step += 1
else:
if random.random() < np.exp(-delta / self.current_temperature):
self.current_solution = candidate
step += 1
self.keep_best()
def keep_best(self):
# record the history optimal solution
current_fit = self.cal_fitness(self.current_solution)
if current_fit < self.best_fit:
self.best_fit = current_fit
# Here the current_solution is a fixed variable, so the deepcopy is a must
self.best_solution = copy.deepcopy(self.current_solution)
if __name__ == "__main__":
total_nodes = 8
graph_mat = abs(np.random.randn(total_nodes, total_nodes))
inner_times = 10
T_start = 100
T_end = 2
param = 0.96
initial_solution = range(total_nodes)
random.shuffle(initial_solution)
SA = SimulatedAnnealing(graph_mat, param, inner_times, T_start, T_end, initial_solution)
SA.keep_best()
while SA.current_temperature < T_end:
SA.inner_loop()
SA.update_temperature()
print SA.best_solution
print SA.best_fit