Indicator constraints in linear solver #3907
-
Hi, I am trying to implement a linear model with indicator constraints. While the MPSolver class lacks the methods to do so, the definitions in linear_solver.proto imply that it is possible if I work directly with protobuf. I managed to implement a small working example (code below), but it's very cumbersome, error-prone and unsafe (for example, all variable pointers are invalidated after doing this protobuf trick), so I'm looking for a better way. Can anyone help me? Code#include <assert.h>
#include <iostream>
#include "ortools/linear_solver/linear_solver.h"
using namespace operations_research;
int main() {
auto solver = MPSolver::CreateSolver("SCIP");
auto x = solver->MakeIntVar(0, 10, "");
auto y = solver->MakeIntVar(0, 10, "");
auto indicator1 = solver->MakeBoolVar("");
auto indicator2 = solver->MakeBoolVar("");
auto exactly_one = solver->MakeRowConstraint(1, 1);
exactly_one->SetCoefficient(indicator1, 1);
exactly_one->SetCoefficient(indicator2, 1);
int x_index = x->index();
int y_index = y->index();
int indicator1_index = indicator1->index();
int indicator2_index = indicator2->index();
MPModelProto proto;
solver->ExportModelToProto(&proto);
auto constraint1 = new MPConstraintProto;
constraint1->add_var_index(x_index);
constraint1->add_coefficient(3);
constraint1->add_var_index(y_index);
constraint1->add_coefficient(5);
constraint1->set_lower_bound(10);
constraint1->set_upper_bound(20);
auto general1 = proto.add_general_constraint();
auto ind1 = general1->mutable_indicator_constraint();
ind1->set_allocated_constraint(constraint1);
ind1->set_var_index(indicator1_index);
ind1->set_var_value(1);
auto constraint2 = new MPConstraintProto;
constraint2->add_var_index(x_index);
constraint2->add_coefficient(3);
constraint2->add_var_index(y_index);
constraint2->add_coefficient(5);
constraint2->set_lower_bound(25);
constraint2->set_upper_bound(30);
auto general2 = proto.add_general_constraint();
auto ind2 = general2->mutable_indicator_constraint();
ind2->set_allocated_constraint(constraint2);
ind2->set_var_index(indicator2_index);
ind2->set_var_value(1);
std::string error;
solver->LoadModelFromProto(proto, &error);
assert(error.empty());
auto status = solver->Solve();
std::cout << status << std::endl;
assert(status == MPSolver::OPTIMAL);
std::cout << int(solver->variable(indicator1_index)->solution_value()) << std::endl;
std::cout << int(solver->variable(indicator2_index)->solution_value()) << std::endl;
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Currently, the MPSolver interface does not offer it. If your problem only uses integer or Boolean variables, you can use the native CP-SAT API. It supports indicator constraints natively, in all languages. |
Beta Was this translation helpful? Give feedback.
Currently, the MPSolver interface does not offer it.
Math-opt (in development) will offers this natively. Currently, it is C++ only using Bazel (and not cmake). We will soon support CMake and python.
If your problem only uses integer or Boolean variables, you can use the native CP-SAT API. It supports indicator constraints natively, in all languages.