Commit f2d89939 authored by Grégoire GRZECZKOWICZ's avatar Grégoire GRZECZKOWICZ
Browse files

Add temp classes

parent a6f88b16
......@@ -36,13 +36,12 @@ def greedy(func, init, neighb, again):
return best_val, best_sol
def simulated_annealing(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
i = 1
T = 1
while again(i, best_val, best_sol):
sol = neighb(best_sol)
val = func(sol)
......@@ -50,7 +49,7 @@ def simulated_annealing(func, init, neighb, again):
if val >= best_val:
best_val = val
best_sol = sol
elif np.random.random() < np.exp(-(best_val-val)/T):
elif np.random.random() < np.exp(-(best_val-val)/temp(i, best_val, best_sol)):
best_val = val
best_sol = sol
i += 1
......
########################################################################
# Temperature programme
########################################################################
class continus:
"""Continus decrease temperature from T_0 and with T_{i+1} = \alpha x T_i"""
def __init__(self, T_0, alpha):
self.T = T_0
self.alpha = alpha
def __call__(self, i, val, sol):
T = self.T
self.T *= self.alpha
return T
class level:
"""Level decrease temperature from T_0 and with T_{i+1} = T_i - \alpha after \beta changes"""
def __init__(self, T_0, alpha, beta):
self.T = T_0
self.alpha = alpha
self.beta = beta
self.val = None
self.count = 0
def __call__(self, i, val, sol):
T = self.T
if val != self.val:
self.count += 1
self.val = val
if self.count > self.beta:
self.count = 0
self.T -= self.alpha
print()
print("T = "+str(T))
return T
\ No newline at end of file
......@@ -3,7 +3,7 @@ import math
import numpy as np
import matplotlib.pyplot as plt
from sho import make, algo, iters, plot, num, bit, pb
from sho import make, algo, iters, plot, num, bit, pb, temp
########################################################################
# Interface
......@@ -133,6 +133,7 @@ if __name__=="__main__":
make.neig(num.neighb_square,
scale = the.variation_scale,
domain_width = the.domain_width),
temp.continus(100,0.99),
iters
)
sensors = num.to_sensors(sol)
......@@ -149,6 +150,7 @@ if __name__=="__main__":
make.neig(bit.neighb_square,
scale = the.variation_scale,
domain_width = the.domain_width),
temp.continus(100,0.99),
iters
)
sensors = bit.to_sensors(sol)
......
Markdown is supported
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