def makeSchedule(content): horizon = content["horizon"] print(horizon) # Define all the players MyScenario = Scenario('manufacturing_schedule', horizon=horizon) MyResources = {} MyTasks = {} # Define the resources (manufacturing lines) manufacturing_lines = content["manufacturing_lines"] print(manufacturing_lines) for manufacturing_line in manufacturing_lines: MyResources[manufacturing_line] = MyScenario.Resource( str(manufacturing_line)) # Define tasks which are already present which must remain in the same location # Known as blocking tasks blocks = content["blocks"] print(blocks) for block in blocks: blockid = str(block["goal_name"] + "_" + str(block["sku_number"])) manufacturing_line_name = block["manufacturing_line_name"] start = block["start"] end = block["end"] # Create task and add the resource that will execute it task = MyScenario.Task(blockid, length=end - start) task += MyResources[manufacturing_line_name] # Define the bounds MyScenario += task > start, task < end # New tasks which must be scheduled tasks = content["tasks"] print(tasks) for task in tasks: taskid = str(task["goal_name"] + "_" + str(task["sku_number"])) duration = task["duration"] manufacturing_line_names = task["manufacturing_line_names"] deadline = task["deadline"] t = MyScenario.Task(taskid, length=duration, delay_cost=1) resources = MyResources[manufacturing_line_names[0]] for i in range(1, len(manufacturing_line_names)): resources = resources | MyResources[manufacturing_line_names[i]] t += resources MyScenario += t > 0, t < deadline solvers.mip.solve(MyScenario, msg=1) plotters.matplotlib.plot(MyScenario, img_filename='household.png') return MyScenario.solution()
from pyschedule import plotters, Scenario, solvers S = Scenario('asdf', horizon=20) r = S.Resource('r', periods=range(20)) t2 = S.Task('CE2', length=20) t2 += r res = solvers.mip.solve_tsp(S, msg=0) print(S.solution()) print(S)
def solve(solver_data): # create scenario scenario = Scenario(name='scenario', horizon=int(solver_data['horizon'])) # resources list resources = {} resources_map = {} resources_mapi = {} resources_ai = 0 # tasks list tasks = {} tasks_map = {} tasks_mapi = {} tasks_ai = 0 # blocks ai blocks_ai = 0 # # enter resources print('[INFO] Importing resources...') for i, resource in enumerate(solver_data['resources']): print('[INFO] Importing resources... ({}/{})'.format( i + 1, len(solver_data['resources'])), end='\r') # convert to [name, size = 1] if not isinstance(resource, list): resource = [resource, 1] # label given to the solver backend rname = 'r' + str(resources_ai) has_parallel = False if resource[0].startswith('teacher_'): for task in solver_data['tasks']: if resource[0] in task['resources'] and task['tags'][ 'block_value'] != task['length']: has_parallel = True print('Found parallel', resource, ' ') break size = 2 if has_parallel else 1 resources[rname] = scenario.Resource(rname, size=size) resources_map[rname] = resource[0] resources_mapi[resource[0]] = rname resources_ai += 1 if has_parallel: scenario += resources[rname]['block_value'][ 0:int(solver_data['horizon']):1] <= 1 # # enter tasks print('[INFO] Importing tasks...') for task in solver_data['tasks']: tname = 't' + str(tasks_ai) tasks[tname] = scenario.Task(tname, length=task['length'], **task['tags']) tasks_map[tname] = task['label'] tasks_mapi[task['label']] = tname tasks_ai += 1 tasks[tname]['r_' + tname] = 1 if 'period' in task and task['period'] != -1: tasks[tname].periods = [task['period']] else: if task['length'] == 4: tasks[tname].periods = [ i for i in range(0, solver_data['horizon']) if i % 4 == 0 ] else: tasks[tname].periods = [ i for i in range(0, solver_data['horizon']) if i % 2 == 0 ] # add resources to task for res_name in task['resources']: tasks[tname] += resources[resources_mapi[res_name]] # # enter blocks print('[INFO] Importing blocks...') for block in solver_data['blocks']: bname = 'b' + str(blocks_ai) task = scenario.Task(bname, length=1, periods=[block["start"]], plot_color='#000000', block_value=1) task += resources[resources_mapi[block["resource"]]] blocks_ai += 1 print('[INFO] Importing sblocks...') for block in solver_data['sblocks']: bname = 'b' + str(blocks_ai) task = scenario.Task(bname, length=1, periods=[block["start"]], plot_color='#000000', schedule_cost=block["cost"], block_value=1) task += resources[resources_mapi[block["resource"]]] blocks_ai += 1 # # enter sync constraints print('[INFO] Importing sync constraints...') for constraint in solver_data['constraints']['sync']: scenario += tasks[tasks_mapi[constraint['tasks'][0]]] <= tasks[ tasks_mapi[constraint['tasks'][1]]] + tasks[tasks_mapi[ constraint['tasks'][0]]].length # # enter cap constraints print(solver_data['constraints']['cap']) # solver_data['constraints']['cap'] = [ # ['group_118', 'group_117'], ['group_118', 'group_116']] for i, constraint in enumerate(solver_data['constraints']['cap']): print('[INFO] Importing capacity constraints... ({}/{})'.format( i + 1, len(solver_data['constraints']['cap']))) for res in resources.values(): if res.size == 1: continue cond = res[constraint[0]][0:int(solver_data['horizon']):1].max for t in constraint[1:]: cond = cond + res[t][0:int(solver_data['horizon']):1].max scenario += cond <= 1 # # solve if solvers.mip.solve(scenario, msg=1): # plotters.matplotlib.plot(scenario, img_filename='out.png', fig_size=( # resources_ai / 3, resources_ai / 2)) solution = scenario.solution() real_solution = [[str(l[0]), str(l[1]), l[2], l[3]] for l in solution] for item in real_solution: if item[0][0] == 'b': continue item[0] = tasks_map[str(item[0])] item[1] = resources_map[str(item[1])] return {'solved': True, 'data': real_solution} else: return {'solved': False, 'error': 'Impossible'}
periods=[ getPeriodFromTime(t) for t in data2['times'][i]['availableTimes'] ])) name_perms = list(combinations(range(len(name_list)), 2)) final = [[S.resources()[x[0]], S.resources()[x[1]]] for x in name_perms] for t in task_list: random.shuffle(resource_list) t += alt(resource_list[:10]), alt(resource_list[10:]) # compute and print a schedule solvers.mip.solve(S, random_seed=random.randint(1, 1000)) soln = S.solution() interviewList = [] for s in range(0, len(soln), 2): # assert names & times match before merging assert (soln[s][0] == soln[s + 1][0]) assert (soln[s][2] == soln[s + 1][2]) interview = {} interview["name"] = str(soln[s][0]) interview['interviewers'] = [str(soln[s][1]), str(soln[s + 1][1])] interview['start_time'] = data['allTimes'][soln[s][2]] interview['duration'] = data['interviewDuration'] interviewList.append(interview) print(json.dumps(interviewList)) plotters.matplotlib.plot(S, img_filename='test4.png')