25#include "absl/algorithm/container.h"
26#include "absl/container/flat_hash_map.h"
27#include "absl/log/check.h"
28#include "absl/status/status.h"
29#include "absl/status/statusor.h"
30#include "absl/strings/string_view.h"
31#include "absl/types/span.h"
33#include "ortools/linear_solver/linear_solver.pb.h"
37#include "ortools/math_opt/model.pb.h"
38#include "ortools/math_opt/model_parameters.pb.h"
39#include "ortools/math_opt/sparse_containers.pb.h"
46absl::Status IsSupported(
const MPModelProto&
model) {
48 if (!validity_string.empty()) {
49 return absl::InvalidArgumentError(validity_string);
51 for (
const MPGeneralConstraintProto& general_constraint :
52 model.general_constraint()) {
53 if (!(general_constraint.has_quadratic_constraint() ||
54 general_constraint.has_sos_constraint() ||
55 general_constraint.has_indicator_constraint())) {
56 return absl::InvalidArgumentError(
"Unsupported general constraint");
59 return absl::OkStatus();
62bool AnyVarNamed(
const MPModelProto&
model) {
63 for (
const MPVariableProto&
var :
model.variable()) {
64 if (
var.name().length() > 0) {
71bool AnyConstraintNamed(
const MPModelProto&
model) {
72 for (
const MPConstraintProto& constraint :
model.constraint()) {
73 if (constraint.name().length() > 0) {
80void LinearTermsFromMPModelToMathOpt(
81 const absl::Span<const int32_t> in_ids,
82 const absl::Span<const double> in_coeffs,
83 google::protobuf::RepeatedField<int64_t>& out_ids,
84 google::protobuf::RepeatedField<double>& out_coeffs) {
85 CHECK_EQ(in_ids.size(), in_coeffs.size());
86 const int num_terms =
static_cast<int>(in_ids.size());
87 std::vector<std::pair<int, double>> linear_terms_in_order;
88 for (
int i = 0;
i < num_terms; ++
i) {
89 linear_terms_in_order.push_back({in_ids[
i], in_coeffs[
i]});
91 absl::c_sort(linear_terms_in_order);
92 out_ids.Resize(num_terms, -1);
93 out_coeffs.Resize(num_terms, std::numeric_limits<double>::quiet_NaN());
94 for (
int i = 0;
i < num_terms; ++
i) {
95 out_ids.Set(i, linear_terms_in_order[i].first);
96 out_coeffs.Set(i, linear_terms_in_order[i].second);
105SparseDoubleMatrixProto QuadraticTermsFromMPModelToMathOpt(
106 const absl::Span<const int32_t> in_row_var_indices,
107 const absl::Span<const int32_t> in_col_var_indices,
108 const absl::Span<const double> in_coefficients) {
109 CHECK_EQ(in_row_var_indices.size(), in_col_var_indices.size());
110 CHECK_EQ(in_row_var_indices.size(), in_coefficients.size());
112 SparseDoubleMatrixProto out_expression;
113 std::vector<std::pair<std::pair<int32_t, int32_t>,
double>> qp_terms_in_order;
114 for (
int k = 0; k < in_row_var_indices.size(); ++k) {
115 int32_t first_index = in_row_var_indices[k];
116 int32_t second_index = in_col_var_indices[k];
117 if (first_index > second_index) {
118 std::swap(first_index, second_index);
120 qp_terms_in_order.push_back(
121 {{first_index, second_index}, in_coefficients[k]});
123 absl::c_sort(qp_terms_in_order);
124 std::pair<int32_t, int32_t> previous = {-1, -1};
125 for (
const auto& [indices, coeff] : qp_terms_in_order) {
126 if (indices == previous) {
127 *out_expression.mutable_coefficients()->rbegin() += coeff;
129 out_expression.add_row_ids(indices.first);
130 out_expression.add_column_ids(indices.second);
131 out_expression.add_coefficients(coeff);
135 return out_expression;
138QuadraticConstraintProto QuadraticConstraintFromMPModelToMathOpt(
140 QuadraticConstraintProto out_constraint;
143 out_constraint.set_name(std::string(
name));
144 LinearTermsFromMPModelToMathOpt(
146 *out_constraint.mutable_linear_terms()->mutable_ids(),
147 *out_constraint.mutable_linear_terms()->mutable_values());
148 *out_constraint.mutable_quadratic_terms() =
149 QuadraticTermsFromMPModelToMathOpt(
in_constraint.qvar1_index(),
152 return out_constraint;
155SosConstraintProto SosConstraintFromMPModelToMathOpt(
157 SosConstraintProto out_constraint;
158 out_constraint.set_name(std::string(
name));
160 LinearExpressionProto& expr = *out_constraint.add_expressions();
162 expr.add_coefficients(1.0);
165 out_constraint.add_weights(
weight);
167 return out_constraint;
171absl::StatusOr<IndicatorConstraintProto>
172IndicatorConstraintFromMPModelToMathOpt(
174 IndicatorConstraintProto out_constraint;
175 out_constraint.set_name(std::string(
name));
177 out_constraint.set_activate_on_zero(
in_constraint.has_var_value() &&
179 out_constraint.set_lower_bound(
in_constraint.constraint().lower_bound());
180 out_constraint.set_upper_bound(
in_constraint.constraint().upper_bound());
181 LinearTermsFromMPModelToMathOpt(
184 *out_constraint.mutable_expression()->mutable_ids(),
185 *out_constraint.mutable_expression()->mutable_values());
186 return out_constraint;
189absl::StatusOr<MPGeneralConstraintProto> SosConstraintFromMathOptToMPModel(
191 const MPSosConstraint::Type sos_type,
192 const absl::flat_hash_map<int64_t, int>& variable_id_to_mp_position) {
193 MPGeneralConstraintProto out_general_constraint;
195 MPSosConstraint& out_constraint =
196 *out_general_constraint.mutable_sos_constraint();
197 out_constraint.set_type(sos_type);
199 out_constraint.add_weight(
weight);
201 for (
const LinearExpressionProto& expression :
in_constraint.expressions()) {
202 if (expression.ids_size() != 1 || expression.coefficients(0) != 1.0 ||
203 expression.offset() != 0.0) {
204 return absl::InvalidArgumentError(
205 "MPModelProto does not support SOS constraints with "
206 "expressions that are not equivalent to a single variable");
208 out_constraint.add_var_index(
209 variable_id_to_mp_position.at(expression.ids(0)));
211 return out_general_constraint;
216absl::StatusOr<::operations_research::math_opt::ModelProto>
221 output.set_name(
model.name());
223 math_opt::VariablesProto*
const vars = output.mutable_variables();
224 int linear_objective_non_zeros = 0;
225 const int num_vars =
model.variable_size();
226 const bool vars_have_name = AnyVarNamed(
model);
227 vars->mutable_lower_bounds()->Reserve(num_vars);
228 vars->mutable_upper_bounds()->Reserve(num_vars);
229 vars->mutable_integers()->Reserve(num_vars);
230 if (vars_have_name) {
231 vars->mutable_names()->Reserve(num_vars);
233 for (
int i = 0; i <
model.variable_size(); ++i) {
234 const MPVariableProto&
var =
model.variable(i);
235 if (
var.objective_coefficient() != 0.0) {
236 ++linear_objective_non_zeros;
239 vars->add_lower_bounds(
var.lower_bound());
240 vars->add_upper_bounds(
var.upper_bound());
241 vars->add_integers(
var.is_integer());
242 if (vars_have_name) {
243 vars->add_names(
var.name());
247 math_opt::ObjectiveProto*
const objective = output.mutable_objective();
248 if (linear_objective_non_zeros > 0) {
249 objective->mutable_linear_coefficients()->mutable_ids()->Reserve(
250 linear_objective_non_zeros);
251 objective->mutable_linear_coefficients()->mutable_values()->Reserve(
252 linear_objective_non_zeros);
253 for (
int j = 0; j < num_vars; ++j) {
254 const double value =
model.variable(j).objective_coefficient();
255 if (
value == 0.0)
continue;
256 objective->mutable_linear_coefficients()->add_ids(j);
257 objective->mutable_linear_coefficients()->add_values(
value);
260 const MPQuadraticObjective& origin_qp_terms =
model.quadratic_objective();
261 const int num_qp_terms = origin_qp_terms.coefficient().size();
262 if (num_qp_terms > 0) {
263 *objective->mutable_quadratic_coefficients() =
264 QuadraticTermsFromMPModelToMathOpt(origin_qp_terms.qvar1_index(),
265 origin_qp_terms.qvar2_index(),
266 origin_qp_terms.coefficient());
268 objective->set_maximize(
model.maximize());
269 objective->set_offset(
model.objective_offset());
271 math_opt::LinearConstraintsProto*
const constraints =
272 output.mutable_linear_constraints();
273 const int num_constraints =
model.constraint_size();
274 const bool constraints_have_name = AnyConstraintNamed(
model);
275 int num_non_zeros = 0;
276 constraints->mutable_lower_bounds()->Reserve(num_constraints);
277 constraints->mutable_upper_bounds()->Reserve(num_constraints);
278 if (constraints_have_name) {
279 constraints->mutable_names()->Reserve(num_constraints);
281 for (
int i = 0; i < num_constraints; ++i) {
282 const MPConstraintProto& constraint =
model.constraint(i);
283 constraints->add_ids(i);
284 constraints->add_lower_bounds(constraint.lower_bound());
285 constraints->add_upper_bounds(constraint.upper_bound());
286 if (constraints_have_name) {
287 constraints->add_names(constraint.name());
289 num_non_zeros += constraint.var_index_size();
292 SparseDoubleMatrixProto*
const matrix =
293 output.mutable_linear_constraint_matrix();
294 matrix->mutable_column_ids()->Reserve(num_non_zeros);
295 matrix->mutable_row_ids()->Reserve(num_non_zeros);
296 matrix->mutable_coefficients()->Reserve(num_non_zeros);
298 std::vector<std::pair<int, double>> terms_in_order;
299 for (
int i = 0; i < num_constraints; ++i) {
300 const MPConstraintProto& constraint =
model.constraint(i);
301 const int constraint_non_zeros = constraint.var_index_size();
302 for (
int k = 0; k < constraint_non_zeros; ++k) {
303 const double coefficient = constraint.coefficient(k);
307 terms_in_order.emplace_back(constraint.var_index(k),
coefficient);
309 std::sort(terms_in_order.begin(), terms_in_order.end());
310 for (
const auto& term : terms_in_order) {
311 matrix->add_row_ids(i);
312 matrix->add_column_ids(term.first);
313 matrix->add_coefficients(term.second);
315 terms_in_order.clear();
318 for (
const MPGeneralConstraintProto& general_constraint :
319 model.general_constraint()) {
320 const std::string& in_name = general_constraint.name();
321 switch (general_constraint.general_constraint_case()) {
322 case MPGeneralConstraintProto::kQuadraticConstraint: {
323 (*output.mutable_quadratic_constraints())
324 [output.quadratic_constraints_size()] =
325 QuadraticConstraintFromMPModelToMathOpt(
326 general_constraint.quadratic_constraint(), in_name);
329 case MPGeneralConstraintProto::kSosConstraint: {
331 general_constraint.sos_constraint();
333 case operations_research::MPSosConstraint::SOS1_DEFAULT: {
335 .mutable_sos1_constraints())[output.sos1_constraints_size()] =
339 case operations_research::MPSosConstraint::SOS2: {
341 .mutable_sos2_constraints())[output.sos2_constraints_size()] =
348 case MPGeneralConstraintProto::kIndicatorConstraint: {
351 auto& new_indicator_constraint =
352 (*output.mutable_indicator_constraints())
353 [output.indicator_constraints_size()];
355 new_indicator_constraint,
356 IndicatorConstraintFromMPModelToMathOpt(
357 general_constraint.indicator_constraint(), in_name));
361 return absl::InternalError(
362 "Reached unrecognized general constraint in MPModelProto");
369absl::StatusOr<std::optional<SolutionHintProto>>
372 if (!validity_string.empty()) {
373 return absl::InvalidArgumentError(validity_string);
376 if (
model.solution_hint().var_index_size() == 0) {
380 SolutionHintProto
hint;
381 auto& variable_values = *
hint.mutable_variable_values();
382 LinearTermsFromMPModelToMathOpt(
383 model.solution_hint().var_index(),
model.solution_hint().var_value(),
384 *variable_values.mutable_ids(), *variable_values.mutable_values());
390 const ::operations_research::math_opt::ModelProto&
model) {
392 if (!
model.second_order_cone_constraints().empty()) {
393 return absl::InvalidArgumentError(
394 "translating models with second-order cone constraints is not "
398 const bool vars_have_name =
model.variables().names_size() > 0;
399 const bool constraints_have_name =
400 model.linear_constraints().names_size() > 0;
401 absl::flat_hash_map<int64_t, int> variable_id_to_mp_position;
402 absl::flat_hash_map<int64_t, MPConstraintProto*>
403 constraint_id_to_mp_constraint;
406 output.set_name(
model.name());
409 output.mutable_variable()->Reserve(num_vars);
410 for (
int j = 0; j < num_vars; ++j) {
411 MPVariableProto*
const variable = output.add_variable();
412 variable_id_to_mp_position.emplace(
model.variables().ids(j), j);
413 variable->set_lower_bound(
model.variables().lower_bounds(j));
414 variable->set_upper_bound(
model.variables().upper_bounds(j));
415 variable->set_is_integer(
model.variables().integers(j));
416 if (vars_have_name) {
417 variable->set_name(
model.variables().names(j));
422 output.mutable_constraint()->Reserve(num_constraints);
423 for (
int i = 0; i < num_constraints; ++i) {
424 MPConstraintProto*
const constraint = output.add_constraint();
425 constraint_id_to_mp_constraint.emplace(
model.linear_constraints().ids(i),
427 constraint->set_lower_bound(
model.linear_constraints().lower_bounds(i));
428 constraint->set_upper_bound(
model.linear_constraints().upper_bounds(i));
429 if (constraints_have_name) {
430 constraint->set_name(
model.linear_constraints().names(i));
434 output.set_maximize(
model.objective().maximize());
435 output.set_objective_offset(
model.objective().offset());
438 const int var_position = variable_id_to_mp_position[
var];
439 MPVariableProto*
const variable = output.mutable_variable(var_position);
440 variable->set_objective_coefficient(
coef);
442 const SparseDoubleMatrixProto& origin_qp_terms =
443 model.objective().quadratic_coefficients();
444 if (!origin_qp_terms.coefficients().empty()) {
445 MPQuadraticObjective& destination_qp_terms =
446 *output.mutable_quadratic_objective();
447 for (
int k = 0; k < origin_qp_terms.coefficients().
size(); ++k) {
448 destination_qp_terms.add_qvar1_index(
449 variable_id_to_mp_position[origin_qp_terms.row_ids(k)]);
450 destination_qp_terms.add_qvar2_index(
451 variable_id_to_mp_position[origin_qp_terms.column_ids(k)]);
452 destination_qp_terms.add_coefficient(origin_qp_terms.coefficients(k));
457 const int constraint_non_zeros =
458 model.linear_constraint_matrix().coefficients_size();
459 for (
int k = 0; k < constraint_non_zeros; ++k) {
460 const int64_t constraint_id =
model.linear_constraint_matrix().row_ids(k);
461 MPConstraintProto*
const constraint =
462 constraint_id_to_mp_constraint[constraint_id];
463 const int64_t variable_id =
model.linear_constraint_matrix().column_ids(k);
464 const int variable_position = variable_id_to_mp_position[variable_id];
465 constraint->add_var_index(variable_position);
466 const double value =
model.linear_constraint_matrix().coefficients(k);
467 constraint->add_coefficient(
value);
471 MPGeneralConstraintProto& out_general_constraint =
472 *output.add_general_constraint();
474 MPQuadraticConstraint& out_constraint =
475 *out_general_constraint.mutable_quadratic_constraint();
479 out_constraint.add_var_index(variable_id_to_mp_position[
index]);
480 out_constraint.add_coefficient(coeff);
482 for (
int k = 0; k <
in_constraint.quadratic_terms().row_ids_size(); ++k) {
483 out_constraint.add_qvar1_index(
484 variable_id_to_mp_position[
in_constraint.quadratic_terms().row_ids(
486 out_constraint.add_qvar2_index(
487 variable_id_to_mp_position[
in_constraint.quadratic_terms().column_ids(
489 out_constraint.add_qcoefficient(
495 SosConstraintFromMathOptToMPModel(
497 variable_id_to_mp_position));
502 *output.add_general_constraint(),
503 SosConstraintFromMathOptToMPModel(
in_constraint, MPSosConstraint::SOS2,
504 variable_id_to_mp_position));
511 MPGeneralConstraintProto& out_general_constraint =
512 *output.add_general_constraint();
514 MPIndicatorConstraint& out_constraint =
515 *out_general_constraint.mutable_indicator_constraint();
516 out_constraint.set_var_index(
518 out_constraint.set_var_value(
in_constraint.activate_on_zero() ? 0 : 1);
519 out_constraint.mutable_constraint()->set_lower_bound(
521 out_constraint.mutable_constraint()->set_upper_bound(
524 out_constraint.mutable_constraint()->add_var_index(
525 variable_id_to_mp_position[
index]);
526 out_constraint.mutable_constraint()->add_coefficient(coeff);
#define ASSIGN_OR_RETURN(lhs, rexpr)
#define RETURN_IF_ERROR(expr)
const std::string name
A name for logging purposes.
std::optional< ModelSolveParameters::SolutionHint > hint
absl::StatusOr< std::optional< SolutionHintProto > > MPModelProtoSolutionHintToMathOptHint(const MPModelProto &model)
int NumVariables(const VariablesProto &variables)
absl::StatusOr<::operations_research::MPModelProto > MathOptModelToMPModelProto(const ::operations_research::math_opt::ModelProto &model)
SparseVectorView< T > MakeView(absl::Span< const int64_t > ids, const Collection &values)
int NumConstraints(const LinearConstraintsProto &linear_constraints)
absl::StatusOr<::operations_research::math_opt::ModelProto > MPModelProtoToMathOptModel(const ::operations_research::MPModelProto &model)
absl::StatusOr< ModelSummary > ValidateModel(const ModelProto &model, const bool check_names)
In SWIG mode, we don't want anything besides these top-level includes.
std::string FindErrorInMPModelProto(const MPModelProto &model, double abs_value_threshold, const bool accept_trivially_infeasible_bounds)