from Parser import JobShopProblem as Problem import random def pull_fwd(solution): """ 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. """ 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) def accept(solution): """ Accept the current generated solution and evaluate it. Maybe skip this during the first step to generate a more random solution. """ return 3 def rectify(solution, idx): """ Transform solution by adapting the begin times and delaying tasks on the same machine if affected. """ 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] def generate(solution, steps): """Generate a new solution from an existing solution with a specified number of max steps. """ options = [pull_fwd, accept] option = random.choice(options) return option(solution) def mock(): """ Reads a mock problem and creates the corresponding enumerated solution. Should clean up the namespace afterwards. """ 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