89 lines
3.3 KiB
Python
89 lines
3.3 KiB
Python
import math
|
|
import random
|
|
from model.base_model import Model
|
|
|
|
class SimAnneal(Model):
|
|
def __init__(self, T=-1, alpha=-1, stopping_T=-1):
|
|
super().__init__()
|
|
|
|
self.iteration = 0
|
|
|
|
self.T = T
|
|
self.alpha = 0.995 if alpha == -1 else alpha
|
|
self.stopping_temperature = 1e-8 if stopping_T == -1 else stopping_T
|
|
|
|
def init(self, coords):
|
|
super().init(coords)
|
|
|
|
if (self.T == -1):
|
|
self.T = math.sqrt(self.N)
|
|
self.T_save = self.T # save inital T to reset if batch annealing is used
|
|
|
|
def initial_solution(self):
|
|
"""
|
|
Greedy algorithm to get an initial solution (closest-neighbour).
|
|
"""
|
|
cur_node = random.choice(self.nodes) # start from a random node
|
|
solution = [cur_node]
|
|
|
|
free_nodes = set(self.nodes)
|
|
free_nodes.remove(cur_node)
|
|
while free_nodes:
|
|
next_node = min(free_nodes, key=lambda x: self.dist(cur_node, x)) # nearest neighbour
|
|
free_nodes.remove(next_node)
|
|
solution.append(next_node)
|
|
cur_node = next_node
|
|
|
|
cur_fit = self.fitness(solution)
|
|
if cur_fit < self.best_fitness: # If best found so far, update best fitness
|
|
self.best_fitness = cur_fit
|
|
self.best_solution = solution
|
|
self.fitness_list.append(cur_fit)
|
|
return solution, cur_fit
|
|
|
|
def p_accept(self, candidate_fitness):
|
|
"""
|
|
Probability of accepting if the candidate is worse than current.
|
|
Depends on the current temperature and difference between candidate and current.
|
|
"""
|
|
return math.exp(-abs(candidate_fitness - self.cur_fitness) / self.T)
|
|
|
|
def accept(self, candidate):
|
|
"""
|
|
Accept with probability 1 if candidate is better than current.
|
|
Accept with probabilty p_accept(..) if candidate is worse.
|
|
"""
|
|
candidate_fitness = self.fitness(candidate)
|
|
if candidate_fitness < self.cur_fitness:
|
|
self.cur_fitness, self.cur_solution = candidate_fitness, candidate
|
|
if candidate_fitness < self.best_fitness:
|
|
self.best_fitness, self.best_solution = candidate_fitness, candidate
|
|
else:
|
|
if random.random() < self.p_accept(candidate_fitness):
|
|
self.cur_fitness, self.cur_solution = candidate_fitness, candidate
|
|
|
|
def fit(self, max_it=1000):
|
|
"""
|
|
Execute simulated annealing algorithm.
|
|
"""
|
|
# Initialize with the greedy solution.
|
|
self.cur_solution, self.cur_fitness = self.initial_solution()
|
|
|
|
self.log("Starting annealing.")
|
|
while self.T >= self.stopping_temperature and self.iteration < max_it:
|
|
candidate = list(self.cur_solution)
|
|
l = random.randint(2, self.N - 1)
|
|
i = random.randint(0, self.N - l)
|
|
candidate[i : (i + l)] = reversed(candidate[i : (i + l)])
|
|
self.accept(candidate)
|
|
self.T *= self.alpha
|
|
self.iteration += 1
|
|
|
|
self.fitness_list.append(self.cur_fitness)
|
|
|
|
self.log(f"Best fitness obtained: {self.best_fitness}")
|
|
improvement = 100 * (self.fitness_list[0] - self.best_fitness) / (self.fitness_list[0])
|
|
self.log(f"Improvement over greedy heuristic: {improvement : .2f}%")
|
|
|
|
return self.best_solution, self.fitness_list
|