Search code examples
pythonc++or-toolscp-sat

or-tools: Trivial problem is infeasible in cpp, but works in python


I have run into an issue in an optimization problem where trivially feasible constraints cause my cpp program to return "infeasible" when trying to solve.

To demonstrate, I have created a nurse schedule optimization program with 3 nurses and 5 slots.

I have two trivial constraints: 1) that the first nurse takes the first slot and 2) that at most one nurse is allowed in each slot.

When engaged one at a time, these constraints cause or-tools to return a feasible solution, but when I engage both constraints, I get an infeasible solution. The exact same problem works fine in the python API even with both constraints engaged.

I suspect I am misusing AddEquality somehow when I set the first constraint (cp_model.AddEquality(LinearExpr(slots[0][0]), 1);), but I cannot figure out what the problem is.

Please help.

#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]

The same program that works fine in 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)

Solution

  • You have too many nurses.

    This:

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

    creates a vector with num_nurses elements.
    Then you push_back another num_nurses elements, giving you twice as many as you want.

    Either start with an empty vector and push_back into it:

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

    or start with a "full" vector and assign into it:

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