使用ortools约束求解器进行护士排班

时间:2018-08-18 21:26:31

标签: python python-3.x optimization or-tools

我经历了tutorial from google,而且我似乎了解大多数代码。我的问题是,他们仅根据严格的约束条件选择解决方案。大多数论文还使用软约束,每个约束都有它的系数。所有约束的总和乘以其系数会产生花名册的费用,因此目标是使该值最小化。我的问题是,如何将其添加到代码中?

# Create the decision builder.
db = solver.Phase(shifts_flat, solver.CHOOSE_FIRST_UNBOUND,
                solver.ASSIGN_MIN_VALUE)

# Create the solution collector.
solution = solver.Assignment()
solution.Add(shifts_flat)
collector = solver.AllSolutionCollector(solution)
solver.Solve(db, [collector])

我不确定决策制定者的工作(或其参数),也不确定solver.Assignment()solver.AllSolutionCollector(solution)

我发现的只有this,但我不确定如何使用它。 (也许叫solver.Minimize(cost, ?)而不是分配?)

1 个答案:

答案 0 :(得分:0)

0

如果您查看:

https://github.com/google/or-tools/blob/stable/examples/python/shift_scheduling_sat.py

数据定义了员工的要求:

https://github.com/google/or-tools/blob/stable/examples/python/shift_scheduling_sat.py#L219

模型直接为每个元组(员工,工作日,班次)创建一个布尔变量。

因此将其添加到目标很简单:

# Employee requests
for e, s, d, w in requests:
    obj_bool_vars.append(work[e, s, d])
    obj_bool_coeffs.append(w)

这在最小化代码中使用:

# Objective
model.Minimize(
    sum(obj_bool_vars[i] * obj_bool_coeffs[i]
        for i in range(len(obj_bool_vars))) + sum(
            obj_int_vars[i] * obj_int_coeffs[i]
            for i in range(len(obj_int_vars))))