我有一个Employee
和一个Mission
的列表。
每个任务都有开始时间和持续时间。
在cp模型(Google CpSat,来自or-tools软件包)中,我定义了shifts = Dictionary<(int,int),IntVar>
,其中shifts[(missionId, employeeId)] == 1
仅当且仅当此员工实现了此任务时。
我需要将每个任务分配给一个员工,显然,一个员工不能同时实现两个任务。我已经写了这两个硬约束,它们工作正常。
现在,某些任务已“链接”在一起,应该由同一位员工来实现。它们存储如下:
linkedMissions = {{1,2}, {3,4,5}}
在这里,任务1和2必须由同一位员工共同实现,并且任务3、4和5也是相同的。
要写出最后一个约束,我为每个员工收集了所有应该联系在一起的班次列表,然后使它们都相等。
foreach (var employee in listEmployeesIds)
foreach (var missionGroup in linkedMissionsIds)
{
var linkedShifts = shifts
.Where(o => o.Key.Item2 == employee
&& missionGroup.Contains(o.Key.Item1))
.Select(o => o.Value)
.ToList();
for (var i = 0; i < linkedShifts.Count - 1; i++)
model.Add(linkedShifts[i] == linkedShifts[i + 1]);
}
但是,求解器告诉我该模型是不可行的,但是用纸和笔我可以轻松地找到一个可行的计划。我有35名员工和25个任务,链接在一起的任务不会重叠,因此应该没有任何问题。
作为另一种方法,如@Laurent Perron所建议的,我尝试对必须在一起的所有班次使用相同的布尔变量:
var constraintBools = new List<IntVar>();
foreach (var missionGroup in linkedMissionsIds) {
var constraintBools = new List<IntVar>();
foreach (var employee in listEmployeesIds)
{
var linkedShifts = shifts
.Where(o => o.Key.Item2 == employee
&& missionGroup.Contains(o.Key.Item1))
.Select(o => o.Value)
.ToList();
var constraint = model.NewBoolVar($"{linkedShifts.GetHashCode()}");
model.AddBoolAnd(linkedShifts).OnlyEnforceIf(constraint);
constraintBools.Add(constraint);
}
model.AddBoolOr(constraintBools);
}
但是现在,约束根本不起作用:链接的班次不能由同一位员工实现。
我的推理有什么问题?为什么我的模型不可行?
答案 0 :(得分:0)
问题中描述的推理似乎很好,但是如果没有最少的工作示例就很难验证。
我能够(在Python中)实现您的方法,并且似乎可以正常工作,因此问题似乎出在代码的其他部分,或者在实现中的某些技术问题上,与解决方法没有直接关系和条件(例如,与@Ian Mercer的评论中建议的延迟函数调用有关)。
根据您的描述,这是一个有效的示例:
model = cp_model.CpModel()
employees = 35
tasks = 25
# 3 non overlapping groups of linked tasks (as an example)
linkedTasks = [[t+1 for t in range(tasks) if t%5 == 0],
[t for t in range(tasks) if t%9 == 0],
[22, 23, 24]]
#semi random durations, 1-6
task_durations = [t%6+1 for t in range(tasks)]
MAX_TIME = sum(task_durations)
#employee shift assignment: shifts[e,t] == 1 iff task t is assigned to employee e
shifts = {}
for e in range(employees):
for t in range(tasks):
shifts[e, t] = model.NewBoolVar('shift_%i_%i' % (e, t))
# task intervals. Intervals are optional - interval [e, t] is only in effect if
# task t is performed by employee e
task_starts = {}
task_ends = {}
task_intervals = {}
for e in range(employees):
for t in range(tasks):
task_starts[e, t] = model.NewIntVar(0, MAX_TIME, 'task_starts_%i_%i' % (e, t))
task_ends[e, t] = model.NewIntVar(0, MAX_TIME, 'task_ends_%i_%i' % (e, t))
task_intervals[e, t] = model.NewOptionalIntervalVar(task_starts[e, t], task_durations[t], task_ends[e, t], shifts[e, t], 'interval_%i_%i' % (e, t))
# employees tasks cannot overlap
for e in range(employees):
model.AddNoOverlap(task_intervals[e, t] for t in range(tasks))
# all tasks must be realized
model.Add(sum(shifts[e, t] for e in range(employees) for t in range(tasks)) == tasks)
# each task is assigned exactly once
for t in range(tasks):
model.Add(sum(shifts[e, t] for e in range(employees)) == 1)
# make sure linked tasks are performed by the same employee (each consecutive pair of tasks in l, l[t] and l[t+1],
# must both be performed by the same user e, or not both not performed by the user)
# Note: this condition can be written more elegantly, but I tried to stick to the way the question was framed
for l in linkedTasks:
for t in range(len(l)-1):
for e in range(employees):
model.Add(shifts[e, l[t]] == shifts[e, l[t+1]])
# Goal: makespan (end of last task)
obj_var = model.NewIntVar(0, MAX_TIME, 'makespan')
model.AddMaxEquality(obj_var, [
task_ends[e, t] for e in range(employees) for t in range(tasks)
])
model.Minimize(obj_var)
solver = cp_model.CpSolver()
solver.parameters.log_search_progress = True
solver.parameters.num_search_workers = 8
solver.parameters.max_time_in_seconds = 30
result_status = solver.Solve(model)
if (result_status == cp_model.INFEASIBLE):
print('No feasible solution under constraints')
elif (result_status == cp_model.OPTIMAL):
print('Optimal result found, makespan=%i' % (solver.ObjectiveValue()))
elif (result_status == cp_model.FEASIBLE):
print('Feasible (non optimal) result found')
else:
print('No feasible solution found under constraints within time')
for e in range(employees):
for t in range(tasks):
if (solver.Value(shifts[e, t]) > 0):
print('employee %i-> task %i (start: %i, end: %i)' % (e, t, solver.Value(task_starts[e, t]), solver.Value(task_ends[e, t])))
此代码以可行的分配结果(最佳makepan = 18)完成,其中链接的任务由所需的同一位员工执行。
总而言之,虽然我无法查明问题所在,但是如上面的代码所示,这种方法似乎是合理的。