mock() to generate mockenv for the generator, pull_fwd() to compute new solutions. Rectify needs to be implemented before generator works.

simanneal
Maximilian Schlosser 2017-07-02 19:32:26 +02:00
parent 23e6d12754
commit a8c4a11b20
1 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,77 @@
from Parser import JobShopProblem as Problem
import random
""" Pull a task from a pseudo-random position to the position of
a random task forward. If the task directly in front is part
of the same job, pull that instead. The first task can never
be pulled forward. Will not rectify solutions.
Returns the modified solution and the tasks index.
"""
def pull_fwd(solution):
old_idx = random.randint(1, len(solution)-1)
while(solution[old_idx][1][0] == solution[old_idx-1][1][0]):
old_idx -= 1
new_idx = random.randint(0, old_idx-1)
for task in solution[new:old_idx]:
if(task[1][0] == solution[old_idx][1][0]):
break
else:
new_solution = solution[:]
task = solution[old_idx]
new_solution.remove(task)
new_solution.insert(new_idx, task)
return (new_solution, new_idx)
""" Accept the current generated solution and evaluate it.
Maybe skip this during the first step to generate a more
random solution.
"""
def accept(solution):
return 3
""" Transform solution by adapting the begin times and delaying
tasks on the same machine if affected.
"""
def rectify(solution, idx):
global problem
#move previous task on the same machine back if clash
""" task = solution[idx][1]
prev_task = solution[idx-1][1]
while(problem[task[0]][task[1]][1] == problem[prev_task[0]][prev_task[1]][1]):
solution[idx-1][0] += problem[task[0]][task[1]][0]
solution[idx], solution[idx-1] = solution[idx-1], solution[idx]
idx -= 1
task = solution[idx][1]
prev_task = solution[idx-1][1]
del prev_task
"""
#make parallel
solution[idx][0] = solution[idx-1][0]
"""Generate a new solution from an existing solution with a
specified number of max steps.
"""
def generate(solution, steps):
options = [pull_fwd, accept]
option = random.choice(options)
return option(solution)
""" Reads a mock problem and creates the corresponding enumerated
solution. Should clean up the namespace afterwards.
"""
def mock():
global problem
global solution
from Parser.js2_style import parse_file as mockload
from SchedulingAlgorithms.enumerate import enumerate as mockenum
problem = mockload('../inputdata/sample')
solution = mockenum(problem)
del mockload
del mockenum