mdl.max([ mdl.end_of(job_operations[i][NB_MACHINES - 1]) for i in range(NB_JOBS) ]))) #----------------------------------------------------------------------------- # Solve the model and display the result #----------------------------------------------------------------------------- # Solve model print("Solving model....") msol = mdl.solve(FailLimit=10000, TimeLimit=10) print("Solution: ") msol.print_solution() # Display solution if msol and visu.is_visu_enabled(): visu.timeline("Solution for flow-shop " + filename) visu.panel("Jobs") for i in range(NB_JOBS): visu.sequence(name='J' + str(i), intervals=[(msol.get_var_solution(job_operations[i][j]), j, 'M' + str(j)) for j in range(NB_MACHINES)]) visu.panel("Machines") for j in range(NB_MACHINES): visu.sequence(name='M' + str(j), intervals=[(msol.get_var_solution(job_operations[i][j]), j, 'J' + str(i)) for i in range(NB_JOBS)]) visu.show()
#----------------------------------------------------------------------------- # Solve the model and display the result #----------------------------------------------------------------------------- # Solve model print("Solving model....") msol = mdl.solve(FailLimit=30000, TimeLimit=10) print("Solution: ") msol.print_solution() if msol and visu.is_visu_enabled(): load = [CpoStepFunction() for j in range(NB_RENEWABLE)] for m in MODES: itv = msol.get_var_solution(modes[m['id']]) if itv.is_present(): for j in range(NB_RENEWABLE): dem = m['demandRenewable'][j] if dem > 0: load[j].add_value(itv.get_start(), itv.get_end(), dem) visu.timeline("Solution for RCPSPMM " + filename) visu.panel("Tasks") for t in TASKS: tid = t['id'] visu.interval(msol.get_var_solution(tasks[tid]), tid, str(tid)) for j in range(NB_RENEWABLE): visu.panel("R " + str(j + 1)) visu.function(segments=[(INTERVAL_MIN, INTERVAL_MAX, CAPACITIES_RENEWABLE[j])], style='area', color='lightgrey') visu.function(segments=load[j], style='area', color=j) visu.show()
# Minimize cost mdl.add(mdl.minimize(mdl.sum(cost))) #----------------------------------------------------------------------------- # Solve the model and display the result #----------------------------------------------------------------------------- print("Solving model....") msol = mdl.solve(TimeLimit=10) print("Solution: ") msol.print_solution() if msol and visu.is_visu_enabled(): visu.timeline("Solution SchedTime", origin=10, horizon=120) visu.panel("Schedule") for t in ALL_TASKS: visu.interval(msol.get_var_solution(tasks[t.id]), t.id, t.name) for t in ALL_TASKS: if t.release_date is not None: visu.panel('Earliness') itvsol = msol.get_var_solution(tasks[t.id]) cost = fearliness[t].get_value(itvsol.get_start()) visu.function(segments=[(itvsol, cost, t.name)], color=t.id, style='interval') visu.function(segments=fearliness[t], color=t.id) if t.due_date is not None: visu.panel('Tardiness') itvsol = msol.get_var_solution(tasks[t.id]) cost = ftardiness[t].get_value(itvsol.get_end())
# Solve model print("Solving model....") msol = mdl.solve(FailLimit=30000, TimeLimit=10) print("Solution: ") msol.print_solution() ############################################################################## # Display result ############################################################################## if msol and visu.is_visu_enabled(): load = [CpoStepFunction() for j in range(nb_renewable)] for m in modes_data: itv = msol.get_var_solution(modes[m]) if itv.is_present(): for j in range(nb_renewable): if 0 < m.demand_renewable[j]: load[j].add_value(itv.get_start(), itv.get_end(), m.demand_renewable[j]) visu.timeline("Solution for RCPSPMM " + filename) visu.panel("Tasks") for t in tasks_data: visu.interval(msol.get_var_solution(tasks[t]), int(t.name[1:]), t.name) for j in range(nb_renewable): visu.panel("R " + str(j + 1)) visu.function(segments=[(INTERVAL_MIN, INTERVAL_MAX, cap_renewables[j])], style='area', color='lightgrey') visu.function(segments=load[j], style='area', color=j) visu.show()
mdl.add(minimize(max([end_of(OPS[o[0]]) for o in ops]))) ############################################################################## # Model solving ############################################################################## # Solve model print("Solving model....") msol = mdl.solve(FailLimit=100000, TimeLimit=10) print("Solution: ") msol.print_solution() ############################################################################## # Display result ############################################################################## # Draw solution if msol and visu.is_visu_enabled(): visu.timeline("Solution for flexible job-shop " + filename) visu.panel("Machines") for j in range(nb_mchs): visu.sequence(name='M' + str(j)) for v in MACHS[j]: itv = msol.get_var_solution(v) if itv.is_present(): job = Job[v.get_name()] visu.interval(itv, job, 'J' + str(job)) visu.show()
return task[0].upper() + loc # Solve model print("Solving model....") msol = mdl.solve(FailLimit=10000, TimeLimit=10) print("Solution: ") msol.print_solution() # Display result if msol and visu.is_visu_enabled(): workersF = CpoStepFunction() cashF = CpoStepFunction() for p in range(5): cashF.add_value(60 * p, INT_MAX, 30000) for task in all_tasks: itv = msol.get_var_solution(task) workersF.add_value(itv.get_start(), itv.get_end(), 1) cashF.add_value(itv.start, INT_MAX, -200 * desc[task].duration) visu.timeline('Solution SchedCumul') visu.panel(name="Schedule") for task in all_tasks: visu.interval(msol.get_var_solution(task), house[task], compact(task.get_name())) visu.panel(name="Workers") visu.function(segments=workersF, style='area') visu.panel(name="Cash") visu.function(segments=cashF, style='area', color='gold') visu.show()
print("Solution: ") msol.print_solution() ############################################################################## # Display result ############################################################################## def compact(name): # Example: H3-garden -> G3 # ^ ^ loc, task = name[1:].split('-', 1) return task[0].upper() + loc if msol and visu.is_visu_enabled(): workers_function = CpoStepFunction() for v in all_tasks: itv = msol.get_var_solution(v) workers_function.add_value(itv.get_start(), itv.get_end(), 1) visu.timeline('Solution SchedState') visu.panel(name="Schedule") for v in all_tasks: visu.interval(msol.get_var_solution(v), house[v], compact(v.get_name())) visu.panel(name="Houses state") for f in all_state_functions: visu.sequence(name=f.get_name(), segments=msol.get_var_solution(f)) visu.panel(name="Nb of workers") visu.function(segments=workers_function, style='line') visu.show()
print("\nSolving model....") msol3 = mdl3.solve(FailLimit=30000) print("done") if msol3: print("Cost will be " + str(msol3.get_objective_values()[0])) # Allocate tasks to workers tasks = {w: [] for w in WorkerNames} for k, v in Worker.items(): tasks[v].append(k) types = {t: i for i, t in enumerate(TaskNames)} import docplex.cp.utils_visu as visu import matplotlib.pyplot as plt #matplotlib inline #Change the plot size from pylab import rcParams rcParams['figure.figsize'] = 15, 3 visu.timeline('Solution SchedCalendar') for w in WorkerNames: visu.panel() visu.pause(Calendar[w]) visu.sequence(name=w, intervals=[(msol3.get_var_solution(itvs[h, t]), types[t], t) for t in tasks[w] for h in Houses]) visu.show() else: print("No solution found")
# mdl.export_as_cpo() # Solve model print("Solving model....") msol = mdl.solve(TimeLimit=10) print("Solution: ") msol.print_solution() ############################################################################## # Display result ############################################################################## def compact(name): # Example: H3-garden -> G3 # ^ ^ loc, task = name[1:].split('-', 1) return task[0].upper() + loc if msol and visu.is_visu_enabled(): visu.timeline('Solution SchedCalendar') visu.panel() visu.pause(joe_calendar) visu.sequence(name='Joe', intervals=[(msol.get_var_solution(t), type[t], compact(t.name)) for t in joe_tasks]) visu.panel() visu.pause(jim_calendar) visu.sequence(name='Jim', intervals=[(msol.get_var_solution(t), type[t], compact(t.name)) for t in jim_tasks]) visu.show()
tasks = [] for task, interval in task_intervals_on_machines[m_id]: val = msol.get_value(interval) if val != (): tasks.append( (msol.get_var_solution(interval), 1, interval.get_name())) cost_sum += energy_intervals_array[val[2] - 1].get_value( val[0]) * task['power_consumption'] # Add segments to cost function for i in range(val[0], val[1]): cost_i = energy_prices[i] * task['power_consumption'] energy_costs.add_value(i, i + 1, cost_i) # Do not show this machine if no task if assigned to it if tasks and ons: visu.timeline("Machine " + str(m_id), 0, int(TIMESLOTS)) visu.panel("Tasks") visu.sequence(name='Machine', intervals=ons) visu.sequence(name='Tasks', intervals=tasks) visu.function(name='Cost={}'.format(cost_sum), segments=energy_costs) for j in range(NUM_RESOURCES): visu.panel('resources_{}'.format(j)) res = CpoStepFunction() for task, interval in task_intervals_on_machines[m_id]: val = msol.get_value(interval) if val != (): res.add_value(val[0], val[1], task['resource_usage'][j]) visu.function(segments=res, color=j)
############################################################################## # Display result ############################################################################## def compact(name): # Example: H3-garden -> G3 # ^ ^ loc, task = name[1:].split('-', 1) return task[0].upper() + loc if msol and visu.is_visu_enabled(): workersF = CpoStepFunction() cashF = CpoStepFunction() for p in range(5): cashF.add_value(60 * p, INT_MAX, 30000) for task in all_tasks: itv = msol.get_var_solution(task) workersF.add_value(itv.get_start(), itv.get_end(), 1) cashF.add_value(itv.start, INT_MAX, -200 * desc[task].duration) visu.timeline('Solution SchedCumul') visu.panel(name="Schedule") for task in all_tasks: visu.interval(msol.get_var_solution(task), house[task], compact(task.get_name())) visu.panel(name="Workers") visu.function(segments=workersF, style='area') visu.panel(name="Cash") visu.function(segments=cashF, style='area', color='gold') visu.show()
# 1. Calling the solve print("\nSolving model....") msol = mdl.solve(url=None, key=None, FailLimit=30000) print("done") # 2. Displaying the objective and solution print("Cost will be " + str(msol.get_objective_values()[0])) # 3. Viewing the results of sequencing problems in a Gantt chart rcParams['figure.figsize'] = 15, 3 workers_function = CpoStepFunction() for h in Houses: for t in TaskNames: itv = msol.get_var_solution(task[h, t]) workers_function.add_value(itv.get_start(), itv.get_end(), 1) visu.timeline('Solution SchedState') visu.panel(name="Schedule") for h in Houses: for t in TaskNames: visu.interval(msol.get_var_solution(task[h, t]), h, t) visu.panel(name="Houses state") for h in Houses: f = state[h] visu.sequence(name=f.get_name(), segments=msol.get_var_solution(f)) visu.panel(name="Nb of workers") visu.function(segments=workers_function, style='line') visu.show()
for i in range(len(pj_bl_list)): print(pj_bl_list[i]) print(sol.get_var_solution(pj_bl_var[pj_bl_list[i]])) for key, value in acts.items(): if str(acts[key].project_block) == str(pj_bl_list[i]): print(sol.get_var_solution(act_var[key])) if last_end < sol.get_var_solution(act_var[key]).get_end(): last_end = sol.get_var_solution(act_var[key]).get_end() # 탐색 결과 가시화 # 블록 별 스케줄링 결과 rcParams['figure.figsize'] = 15, 60 for i in range(len(pj_bl_list)): visu.panel(name=pj_bl_list[i]) for key, value in acts.items(): if str(acts[key].project_block) == str(pj_bl_list[i]): temp1 = sol.get_var_solution(act_var[key]) visu.interval(temp1,'lightblue',value.id[4:]) visu.show(pngfile="scheduling") # 전체 인력 사용 수준 그래프 print("workforce level for" + str(team)) rcParams['figure.figsize'] = 15, 3 workforce=CpoStepFunction() workforce_standard=CpoStepFunction() workforce_whole=CpoStepFunction() for key, value in acts.items(): wf=sol.get_var_solution(act_var[key]) workforce.add_value(wf.get_start(),wf.get_end(), acts[key].worker)
now = datetime.datetime.now().strftime("%Y-%m-%d ") df = [ dict(Task = Buses[b].Name, Start = now + s(sol.get_var_solution(outwardTrip[b][i]).start), Finish = now + s(sol.get_var_solution(outwardTrip[b][i]).end), Seats = str(Buses[b].Seats) + " Seats") for b in range(NbBuses) for i in range(NbMaxTrips) if (sol.get_var_solution(outwardTrip[b][i]).is_present()) ] for i in df: print(i) visu.timeline('Buses') visu.panel(name="Schedule") for b in range(NbBuses): for i in range(NbMaxTrips): if (sol.get_var_solution(outwardTrip[b][i]).is_present()): visu.interval(sol.get_var_solution(outwardTrip[b][i]), int(Buses[b].Seats), sol.get_var_solution(outwardTrip[b][i]).length) visu.show() """ which gives {'Task': 'A40', 'Start': '2021-03-22 09:30', 'Finish': '2021-03-22 10:00', 'Seats': '40 Seats'} {'Task': 'D40', 'Start': '2021-03-22 09:30', 'Finish': '2021-03-22 10:00', 'Seats': '40 Seats'} {'Task': 'E40', 'Start': '2021-03-22 09:30', 'Finish': '2021-03-22 10:00', 'Seats': '40 Seats'} {'Task': 'F30', 'Start': '2021-03-22 08:45', 'Finish': '2021-03-22 09:10', 'Seats': '30 Seats'} {'Task': 'G30', 'Start': '2021-03-22 08:00', 'Finish': '2021-03-22 08:25', 'Seats': '30 Seats'}
############################################################################## # Solving ############################################################################## print("Solving model....") msol = mdl.solve(TimeLimit=10) print("Solution: ") msol.print_solution() ############################################################################## # Display result ############################################################################## if msol and visu.is_visu_enabled(): visu.timeline("Solution SchedTime", origin=10, horizon=120) visu.panel("Schedule") for t in ALL_TASKS: visu.interval(msol.get_var_solution(tasks[t.id]), t.id, t.name) for t in ALL_TASKS: if t.release_date is not None: visu.panel("Earliness") itvsol = msol.get_var_solution(tasks[t.id]) cost = fearliness[t].get_value(itvsol.get_start()) visu.function(segments=[(itvsol, cost, t.name)], color=t.id, style="interval") visu.function(segments=fearliness[t], color=t.id) if t.due_date is not None: visu.panel("Tardiness") itvsol = msol.get_var_solution(tasks[t.id]) cost = ftardiness[t].get_value(itvsol.get_end()) visu.function(segments=[(itvsol, cost, t.name)], color=t.id, style="interval") visu.function(segments=ftardiness[t], color=t.id)