c++ or-tools:琐碎的问题在cpp中不可行,但在python中有效

4zcjmb1e  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(195)

我在一个优化问题中遇到了一个问题,在这个问题中,简单可行的约束导致我的cpp程序在试图求解时返回“不可行”。
为了进行演示,我创建了一个护士排班优化程序,其中包含3名护士和5个插槽。
我有两个微不足道的约束:1)第一个护士占用第一个插槽,2)每个插槽最多允许一个护士。
当一次使用一个约束时,这些约束会导致or-tools返回一个可行的解,但是当我同时使用两个约束时,我得到的是一个不可行的解。
当我设置第一个约束(cp_model.AddEquality(LinearExpr(slots[0][0]), 1);)时,我怀疑我在某种程度上误用了AddEquality,但我无法找出问题所在。
请帮帮我

#include <iostream>
#include <vector>

#include "ortools/sat/cp_model.h"
#include "ortools/sat/sat_parameters.pb.h"

namespace operations_research {

namespace sat {
void slots(bool add_sum, bool add_const) {

  CpModelBuilder cp_model;

  const int num_nurses = 3;
  const int num_slots = 5;

  std::vector<std::vector<IntVar>> slots(num_nurses);

  for (int n = 0; n < num_nurses; n++) {
    for (int d = 0; d < num_slots; d++) {
      const IntVar var = cp_model.NewIntVar({0, 1});
      slots[n].push_back(var);
    }
  }

  if (add_const) {
    // trival constraint
    cp_model.AddEquality(LinearExpr(slots[0][0]), 1);
  }

  if (add_sum) {
    // make the first row sum to one; should be trivial too
    std::vector<IntVar> this_nurse_vals(num_nurses);
    for (int n = 0; n < num_nurses; n++) {
      const IntVar var = slots[n][0];
      this_nurse_vals.push_back(var);
    }
    cp_model.AddEquality(LinearExpr::Sum(this_nurse_vals), 1);
  }

  // solve
  const CpSolverResponse response = Solve(cp_model.Build());
  LOG(INFO) << CpSolverResponseStats(response);

  for (int d = 0; d < num_slots; d++) {
    for (int n = 0; n < num_nurses; n++) {
      std::cout << SolutionIntegerValue(response, slots[n][d]);
    }
    std::cout << std::endl;
  }
  std::cout << std::endl;

  // [END solve]
}

} // namespace sat
} // namespace operations_research

// ----- MAIN -----
int main(int argc, char **argv) {
  operations_research::sat::slots(false, true); // works
  operations_research::sat::slots(true, false); // works
  operations_research::sat::slots(true, true);  // infeasible

  return EXIT_SUCCESS;
}
// [END program]

字符串
在python中运行良好的相同程序:

from ortools.sat.python import cp_model
num_nurses = 3
num_slots = 5

model = cp_model.CpModel()

# make vars
slots = {}
for n in range(num_nurses):
    for d in range(num_slots):
        slots[(n, d)] = model.NewIntVar(0, 1, "slot")

model.Add(slots[(0, 0)] == 1)

model.Add(sum(slots[(n, 0)] for n in range(num_nurses)) == 1)

solver = cp_model.CpSolver()

solver.Solve(model)

solution = []

for d in range(num_slots):
    solution.append([])
    for n in range(num_nurses):
        solution[d].append(solver.Value(slots[(n, d)]))

print(solution)

eiee3dmh

eiee3dmh1#

你的护士太多了。
这一点:

std::vector<IntVar> this_nurse_vals(num_nurses);

字符串
创建一个包含num_nurses元素的向量。
然后你push_back另一个num_nurses元素,给你两倍于你想要的。
或者从一个空的vector开始,并将push_back插入其中:

std::vector<IntVar> this_nurse_vals;
for (int n = 0; n < num_nurses; n++) {
    this_nurse_vals.push_back(IntVar(slots[n][0]));
}


或者从一个“完整的”向量开始,并将其赋值为:

std::vector<IntVar> this_nurse_vals(num_nurses);
for (int n = 0; n < num_nurses; n++) {
    this_nurse_vals[n] = IntVar(slots[n][0]);
}

相关问题