simulated annealing
This commit is contained in:
parent
049e64c675
commit
991b7f4175
2 changed files with 46 additions and 5 deletions
|
@ -0,0 +1,36 @@
|
|||
from Generator.generator import generate
|
||||
from Generator.generator import init as gen_init
|
||||
from SchedulingAlgorithms.enumerate import enumerate as enum
|
||||
from math import e
|
||||
from random import random
|
||||
|
||||
def anneal(max_temp = 1000, max_steps = 1000):
|
||||
global problem
|
||||
gen_init(problem)
|
||||
temp = max_temp
|
||||
initial = enum(problem)
|
||||
current = generate(initial, problem.machines * problem.jobs * 10)
|
||||
del initial
|
||||
for step in range(0, max_steps):
|
||||
new = generate(current, problem.machines * problem.jobs)
|
||||
new_end = rate(new)
|
||||
curr_end = rate(current)
|
||||
p = 1 / ( 1 + (e ** ((curr_end - new_end)/temp)))
|
||||
if (new_end < curr_end) or (p < random()):
|
||||
current = new
|
||||
print("Old: " + str(curr_end) + " New: " + str(new_end))
|
||||
temp = ((max_temp-1)/(max_steps**2))*(step-max_steps)**2+1
|
||||
print("Iteration: "+ str(step) + " Temperature: " + str(temp))
|
||||
return current
|
||||
|
||||
def rate(solution):
|
||||
global problem
|
||||
last_tasks = []
|
||||
for i in range(0,problem.jobs):
|
||||
last_tasks += [next(( x for x in solution[::-1] if x[1][0] == i), [])]
|
||||
end_times = [ problem[x[1]][0] + x[0] for x in last_tasks]
|
||||
return max(end_times)
|
||||
|
||||
def init(in_problem):
|
||||
global problem
|
||||
problem = in_problem
|
Loading…
Add table
Add a link
Reference in a new issue