# Minimize overall schedule end date mdl.add(mdl.minimize(mdl.max([mdl.end_of(t) for t in tasks.values()]))) #----------------------------------------------------------------------------- # 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))
def get_deadline_area(task_deadline): f_ = CpoStepFunction() f_.set_value(0, DAYS_IN_SPRINT * HOURS_IN_DAY, 100) f_.set_value(task_deadline * HOURS_IN_DAY + 1, DAYS_IN_SPRINT * HOURS_IN_DAY, 0) return f_
def compact(name): # Example: H3-garden -> G3 # ^ ^ loc, task = name[1:].split('-', 1) 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')
# Продолжительность задач DURATIONS = [TASKS[t]["duration"] for t in range(NB_TASKS)] # Требования задач к ресурсам - тут всё просто - 1 разраб = 1 задача DEMANDS = [ make_resource_row(TASKS[t]["rnd"], NB_RESOURCES, DURATIONS[t]) for t in range(NB_TASKS) ] # построим список функций "присутсвия на работе" RND_CALENDAR = [] for i in range(NB_RESOURCES): print("Calendar function for rnd {}".format(i)) cnst_pr = [0, 0] last_val = sign(MM_DEV_ACTIVE_HOURS[i][0][0]) fs = CpoStepFunction() fs.set_value(0, DAYS_IN_SPRINT * HOURS_IN_DAY, 100) for fdays, farrhr in enumerate(MM_DEV_ACTIVE_HOURS[i]): for fhours, fval in enumerate(farrhr): if fval == last_val == 0: cnst_pr[1] = fdays * HOURS_IN_DAY + fhours elif fval == 1 and last_val == 0: fs.set_value(cnst_pr[0], cnst_pr[1], 0) print("find interval from {} to {}".format( cnst_pr[0], cnst_pr[1])) cnst_pr[0] = cnst_pr[1] = fdays * HOURS_IN_DAY + fhours elif fval == 0 and last_val == 1: cnst_pr[0] = cnst_pr[1] = fdays * HOURS_IN_DAY + fhours last_val = fval RND_CALENDAR.append(fs)
# Minimize end of all tasks mdl.add(mdl.minimize(mdl.max([mdl.end_of(t) for t in tasks]))) #----------------------------------------------------------------------------- # Solve the model and display the result #----------------------------------------------------------------------------- # Solve model print("Solving model....") msol = mdl.solve(FailLimit=100000, TimeLimit=10) print("Solution: ") msol.print_solution() if msol and visu.is_visu_enabled(): load = [CpoStepFunction() for j in range(NB_RESOURCES)] for i in range(NB_TASKS): itv = msol.get_var_solution(tasks[i]) for j in range(NB_RESOURCES): if 0 < DEMANDS[i][j]: load[j].add_value(itv.get_start(), itv.get_end(), DEMANDS[i][j]) visu.timeline("Solution for RCPSP " + filename) visu.panel("Tasks") for i in range(NB_TASKS): visu.interval(msol.get_var_solution(tasks[i]), i, tasks[i].get_name()) for j in range(NB_RESOURCES): visu.panel("R " + str(j + 1)) visu.function(segments=[(INTERVAL_MIN, INTERVAL_MAX, CAPACITIES[j])], style='area', color='lightgrey') visu.function(segments=load[j], style='area', color=j) visu.show()
def compact(name): # Example: H3-garden -> G3 # ^ ^ loc, task = name[1:].split('-', 1) return task[0].upper() + loc # Solve model print("Solving model....") msol = mdl.solve(TimeLimit=10, FailLimit=10000) print("Solution: ") msol.print_solution() 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()
JOE_HOLYDAYS = ((5, 12), (124, 131), (215, 236), (369, 376), (495, 502), (579, 600)) JIM_HOLYDAYS = ((26, 40), (201, 225), (306, 313), (397, 411), (565, 579)) #----------------------------------------------------------------------------- # Prepare the data for modeling #----------------------------------------------------------------------------- # Assign an index to tasks ALL_TASKS = (MASONRY, CARPENTRY, PLUMBING, CEILING, ROOFING, PAINTING, WINDOWS, FACADE, GARDEN, MOVING) for i in range(len(ALL_TASKS)): ALL_TASKS[i].id = i # Initialize availability calendar for workers joe_calendar = CpoStepFunction() jim_calendar = CpoStepFunction() joe_calendar.set_value(0, MAX_YEARS * 365, 100) jim_calendar.set_value(0, MAX_YEARS * 365, 100) # Remove week ends for w in range(MAX_YEARS * 52): joe_calendar.set_value(5 + (7 * w), 7 + (7 * w), 0) jim_calendar.set_value(5 + (7 * w), 7 + (7 * w), 0) # Remove holidays for b, e in JOE_HOLYDAYS: joe_calendar.set_value(b, e, 0) for b, e in JIM_HOLYDAYS: jim_calendar.set_value(b, e, 0)
def get_allowable_area(shls, ax_attr, temperature): f_ = CpoStepFunction() if ax_attr == 'x': f_.set_value(0, SIZE_SHELF["x"] * SHELF_COUNT, 100) for ih in range(len(shls)): f_.set_value(shls[ih].x_start, shls[ih].x_start + 1, 0) f_.set_value(shls[ih].x_stop - 1, shls[ih].x_stop, 0) if not shls[ih].t_min <= temperature <= shls[ih].t_max: f_.set_value(shls[ih].x_start, shls[ih].x_stop, 0) elif ax_attr == 'y': f_.set_value(0, SIZE_SHELF["y"] * SHELF_COUNT, 100) for ih in range(len(shls)): f_.set_value(shls[ih].y_start, shls[ih].y_start + 1, 0) f_.set_value(shls[ih].y_stop - 1, shls[ih].y_stop, 0) if not shls[ih].t_min <= temperature <= shls[ih].t_max: f_.set_value(shls[ih].y_start, shls[ih].y_stop, 0) return f_