17#ifndef OR_TOOLS_MATH_OPT_CPP_MODEL_H_
18#define OR_TOOLS_MATH_OPT_CPP_MODEL_H_
26#include "absl/base/nullability.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"
133 explicit Model(absl::string_view
name =
"");
140 explicit Model(absl::Nonnull<std::unique_ptr<ModelStorage>>
storage);
162 absl::Nonnull<std::unique_ptr<Model>>
Clone(
163 std::optional<absl::string_view> new_name = std::nullopt)
const;
165 inline absl::string_view
name()
const;
183 absl::string_view
name =
"");
188 absl::string_view
name =
"");
271 absl::string_view
name =
"");
378 absl::string_view
name =
"");
430 absl::Span<const LinearExpression> arguments_to_norm,
454 SecondOrderConeConstraintId
id)
const;
462 SecondOrderConeConstraintId
id)
const;
471 inline std::vector<SecondOrderConeConstraint>
495 const std::vector<LinearExpression>& expressions,
496 std::vector<double> weights = {}, absl::string_view
name =
"");
554 const std::vector<LinearExpression>& expressions,
555 std::vector<double> weights = {}, absl::string_view
name =
"");
615 const BoundedLinearExpression& implied_constraint,
616 bool activate_on_zero =
false, absl::string_view
name = {});
659 inline void Maximize(
double objective);
663 inline void Maximize(LinearTerm objective);
665 inline void Maximize(
const LinearExpression& objective);
667 inline void Maximize(
const QuadraticExpression& objective);
670 inline void Minimize(
double objective);
674 inline void Minimize(LinearTerm objective);
676 inline void Minimize(
const LinearExpression& objective);
678 inline void Minimize(
const QuadraticExpression& objective);
728 Variable second_variable,
double value);
769 absl::string_view
name = {});
773 absl::string_view
name = {});
777 absl::string_view
name = {});
781 absl::string_view
name = {});
830 inline void Maximize(Objective objective,
const LinearExpression& expression);
832 inline void Minimize(Objective objective,
const LinearExpression& expression);
834 void SetObjective(Objective objective,
const LinearExpression& expression,
838 void AddToObjective(Objective objective,
const LinearExpression& expression);
860 Objective objective)
const;
865 ModelProto
ExportModel(
bool remove_names =
false)
const;
908 friend std::ostream&
operator<<(std::ostream& ostr,
const Model& model);
928 const absl::Nonnull<std::shared_ptr<ModelStorage>> storage_;
943 const bool is_integer,
944 const absl::string_view name) {
954 const double upper_bound,
955 const absl::string_view name) {
961 const absl::string_view
name) {
973 return storage()->next_variable_id().value();
989 CHECK(
has_variable(
id)) <<
"No variable with id: " <<
id.value();
1041 if (storage_.get() !=
variable.storage()) {
1043 <<
"variable with id " <<
variable.id()
1044 <<
" is from a different model";
1049 <<
" is not found in this model (it was probably deleted)";
1051 return absl::OkStatus();
1058 const double upper_bound,
1059 const absl::string_view name) {
1065 CheckModel(constraint.
storage());
1070 return storage()->num_linear_constraints();
1074 return storage()->next_linear_constraint_id().value();
1082 return storage()->has_linear_constraint(
id);
1086 CHECK_GE(
id, 0) <<
"negative linear constraint id: " << id;
1092 <<
"No linear constraint with id: " <<
id.value();
1097 CheckModel(constraint.
storage());
1102 double lower_bound) {
1109 CheckModel(constraint.
storage());
1110 return storage()->linear_constraint_lower_bound(constraint.
typed_id());
1115 CheckModel(constraint.
storage());
1121 CheckModel(constraint.
storage());
1122 return storage()->linear_constraint_upper_bound(constraint.
typed_id());
1131 <<
" is from a different model";
1136 <<
" is not found in this model (it was probably deleted)";
1138 return absl::OkStatus();
1143 CheckModel(constraint.storage());
1145 storage()->set_linear_constraint_coefficient(constraint.typed_id(),
1151 CheckModel(constraint.
storage());
1153 return storage()->linear_constraint_coefficient(constraint.
typed_id(),
1159 CheckModel(constraint.storage());
1161 return storage()->is_linear_constraint_coefficient_nonzero(
1168 CheckModel(constraint.
storage());
1185 return storage()->has_constraint(
id);
1189 CHECK_GE(
id, 0) <<
"negative quadratic constraint id: " << id;
1196 <<
"No quadratic constraint with id: " <<
id.value();
1212 CheckModel(constraint.
storage());
1217 return storage()->num_constraints<SecondOrderConeConstraintId>();
1221 return storage()->next_constraint_id<SecondOrderConeConstraintId>().value();
1225 return id < 0 ?
false
1227 SecondOrderConeConstraintId(
id));
1231 const SecondOrderConeConstraintId
id)
const {
1236 const int64_t
id)
const {
1241 const SecondOrderConeConstraintId
id)
const {
1243 <<
"No second-order cone constraint with id: " <<
id.value();
1260 CheckModel(constraint.
storage());
1265 return storage()->num_constraints<Sos1ConstraintId>();
1269 return storage()->next_constraint_id<Sos1ConstraintId>().value();
1277 return storage()->has_constraint(
id);
1286 <<
"No SOS1 constraint with id: " <<
id.value();
1301 CheckModel(constraint.
storage());
1306 return storage()->num_constraints<Sos2ConstraintId>();
1310 return storage()->next_constraint_id<Sos2ConstraintId>().value();
1318 return storage()->has_constraint(
id);
1327 <<
"No SOS2 constraint with id: " <<
id.value();
1342 CheckModel(constraint.
storage());
1359 return storage()->has_constraint(
id);
1369 <<
"No indicator constraint with id: " <<
id.value();
1446 const Variable second_variable)
const {
1448 CheckModel(second_variable.
storage());
1455 const double value) {
1463 const double value) {
1464 CheckModel(first_variable.
storage());
1465 CheckModel(second_variable.
storage());
1466 storage()->set_quadratic_objective_coefficient(
1468 second_variable.
typed_id(), value);
1477 return storage()->is_linear_objective_coefficient_nonzero(
1484 CheckModel(second_variable.
storage());
1485 return storage()->is_quadratic_objective_coefficient_nonzero(
1517 const absl::string_view
name) {
1523 const bool is_maximize,
1524 const int64_t priority,
1525 const absl::string_view
name) {
1532 const int64_t priority,
1533 const absl::string_view
name) {
1539 const int64_t priority,
1540 const absl::string_view
name) {
1546 CheckModel(objective.
storage());
1547 CHECK(!objective.
is_primary()) <<
"cannot delete primary objective";
1550 <<
"cannot delete unrecognized auxiliary objective id: " << id;
1551 storage()->DeleteAuxiliaryObjective(
id);
1555 return storage()->num_auxiliary_objectives();
1559 return storage()->next_auxiliary_objective_id().
value();
1571 CHECK_GE(
id, 0) <<
"negative auxiliary objective id: " << id;
1577 <<
"unrecognized auxiliary objective id: " << id;
1594 const int64_t priority) {
1595 CheckModel(objective.
storage());
1601 const double value) {
1602 CheckModel(objective.
storage());
1609 const double value) {
1623 CheckModel(objective.
storage());
1627void Model::CheckOptionalModel(
1629 if (other_storage !=
nullptr) {
int64_t value() const
Returns the raw id value.
ModelStorageCPtr storage() const
Sos1Constraint AddSos1Constraint(const std::vector< LinearExpression > &expressions, std::vector< double > weights={}, absl::string_view name="")
void set_lower_bound(Variable variable, double lower_bound)
Sets a variable lower bound.
std::vector< IndicatorConstraint > SortedIndicatorConstraints() const
Objective auxiliary_objective(int64_t id) const
Will CHECK if has_auxiliary_objective(id) is false.
bool has_auxiliary_objective(int64_t id) const
Returns true if this id has been created and not yet deleted.
int64_t num_sos2_constraints() const
QuadraticConstraint AddQuadraticConstraint(const BoundedQuadraticExpression &bounded_expr, absl::string_view name="")
----------------------— Quadratic constraints --------------------------—
Sos2Constraint AddSos2Constraint(const std::vector< LinearExpression > &expressions, std::vector< double > weights={}, absl::string_view name="")
------------------------— SOS2 constraints -----------------------------—
ModelProto ExportModel(bool remove_names=false) const
bool has_variable(int64_t id) const
Returns true if this id has been created and not yet deleted.
std::vector< SecondOrderConeConstraint > SecondOrderConeConstraints() const
int64_t next_quadratic_constraint_id() const
The returned id of the next call to AddQuadraticConstraint.
friend std::ostream & operator<<(std::ostream &ostr, const Model &model)
int64_t next_auxiliary_objective_id() const
The returned id of the next call to AddAuxiliaryObjectve.
int64_t next_linear_constraint_id() const
void DeleteSecondOrderConeConstraint(SecondOrderConeConstraint constraint)
------------------— Second-order cone constraints ----------------------—
static absl::StatusOr< std::unique_ptr< Model > > FromModelProto(const ModelProto &model_proto)
std::vector< Sos2Constraint > SortedSos2Constraints() const
bool is_integer(Variable variable) const
Returns the integrality of a variable.
void set_continuous(Variable variable)
Makes the input variable continuous.
bool has_sos1_constraint(int64_t id) const
Returns true if this id has been created and not yet deleted.
std::vector< Sos1Constraint > SortedSos1Constraints() const
absl::string_view name() const
----------------------------— Variables --------------------------------—
int num_linear_constraints() const
void DeleteQuadraticConstraint(QuadraticConstraint constraint)
----------------------— Quadratic constraints --------------------------—
std::vector< QuadraticConstraint > SortedQuadraticConstraints() const
void set_coefficient(LinearConstraint constraint, Variable variable, double value)
double lower_bound(Variable variable) const
Returns a variable lower bound.
SecondOrderConeConstraint AddSecondOrderConeConstraint(absl::Span< const LinearExpression > arguments_to_norm, const LinearExpression &upper_bound, absl::string_view name="")
------------------— Second-order cone constraints ----------------------—
std::vector< Variable > NonzeroVariablesInLinearObjective() const
std::vector< SecondOrderConeConstraint > SortedSecondOrderConeConstraints() const
SecondOrderConeConstraint second_order_cone_constraint(int64_t id) const
Will CHECK if has_second_order_cone_constraint(id) is false.
int64_t next_indicator_constraint_id() const
The returned id of the next call to AddIndicatorConstraint.
std::vector< Objective > AuxiliaryObjectives() const
int64_t next_second_order_cone_constraint_id() const
The returned id of the next call to AddSecondOrderConeConstraint.
int64_t num_second_order_cone_constraints() const
std::unique_ptr< UpdateTracker > NewUpdateTracker()
std::vector< LinearConstraint > SortedLinearConstraints() const
int64_t next_variable_id() const
Objective AddMinimizationObjective(const LinearExpression &expression, int64_t priority, absl::string_view name={})
Adds expression as an auxiliary minimization objective to the model.
bool has_sos2_constraint(int64_t id) const
Returns true if this id has been created and not yet deleted.
void Maximize(double objective)
Sets the objective to maximize the provided expression.
std::vector< Sos1Constraint > Sos1Constraints() const
int num_variables() const
absl::Status ValidateExistingLinearConstraintOfThisModel(LinearConstraint linear_constraint) const
void set_is_maximize(bool is_maximize)
Prefer set_maximize() and set_minimize() above for more readable code.
int64_t num_sos1_constraints() const
bool has_indicator_constraint(int64_t id) const
Returns true if this id has been created and not yet deleted.
bool has_quadratic_constraint(int64_t id) const
Returns true if this id has been created and not yet deleted.
Variable variable(int64_t id) const
Will CHECK if has_variable(id) is false.
QuadraticExpression ObjectiveAsQuadraticExpression() const
Model & operator=(const Model &)=delete
void AddToObjective(double objective)
Adds the provided expression terms to the objective.
Variable AddVariable(double lower_bound, double upper_bound, bool is_integer, absl::string_view name="")
Adds a variable to the model and returns a reference to it.
Objective AddAuxiliaryObjective(int64_t priority, absl::string_view name={})
Adds an empty (== 0) auxiliary minimization objective to the model.
bool has_linear_constraint(int64_t id) const
Returns true if this id has been created and not yet deleted.
void DeleteAuxiliaryObjective(Objective objective)
ModelStorageCPtr storage() const
void set_objective_priority(Objective objective, int64_t priority)
void set_objective_offset(double value)
absl::Status ApplyUpdateProto(const ModelUpdateProto &update_proto)
int64_t next_sos2_constraint_id() const
The returned id of the next call to AddSos2Constraint.
std::vector< LinearConstraint > ColumnNonzeros(Variable variable) const
void DeleteLinearConstraint(LinearConstraint constraint)
int64_t num_indicator_constraints() const
std::vector< QuadraticConstraint > QuadraticConstraints() const
Model(const Model &)=delete
void DeleteSos1Constraint(Sos1Constraint constraint)
------------------------— SOS1 constraints -----------------------------—
bool has_second_order_cone_constraint(int64_t id) const
Returns true if this id has been created and not yet deleted.
void Minimize(double objective)
Sets the objective to minimize the provided expression.
std::vector< LinearConstraint > LinearConstraints() const
std::vector< Objective > SortedAuxiliaryObjectives() const
int64_t num_auxiliary_objectives() const
void DeleteIndicatorConstraint(IndicatorConstraint constraint)
------------------------— Indicator constraints ------------------------—
Variable AddIntegerVariable(double lower_bound, double upper_bound, absl::string_view name="")
LinearConstraint linear_constraint(int64_t id) const
Will CHECK if has_linear_constraint(id) is false.
void set_objective_coefficient(Variable variable, double value)
Variable AddBinaryVariable(absl::string_view name="")
Adds a variable to the model with domain {0, 1}.
double upper_bound(Variable variable) const
Returns a variable upper bound.
std::vector< Variable > Variables() const
void DeleteSos2Constraint(Sos2Constraint constraint)
------------------------— SOS2 constraints -----------------------------—
std::vector< Variable > NonzeroVariablesInQuadraticObjective() const
absl::Nonnull< std::unique_ptr< Model > > Clone(std::optional< absl::string_view > new_name=std::nullopt) const
bool is_objective_coefficient_nonzero(Variable variable) const
double coefficient(LinearConstraint constraint, Variable variable) const
Returns 0.0 if the variable is not used in the constraint.
void set_upper_bound(Variable variable, double upper_bound)
Sets a variable upper bound.
std::vector< Sos2Constraint > Sos2Constraints() const
int64_t next_sos1_constraint_id() const
The returned id of the next call to AddSos1Constraint.
std::vector< Variable > SortedVariables() const
void set_integer(Variable variable)
Makes the input variable integer.
Model(absl::string_view name="")
Creates an empty minimization problem.
Variable AddContinuousVariable(double lower_bound, double upper_bound, absl::string_view name="")
Adds a variable to the model with domain [lower_bound, upper_bound].
std::vector< IndicatorConstraint > IndicatorConstraints() const
double objective_coefficient(Variable variable) const
Returns 0.0 if this variable has no linear objective coefficient.
void DeleteVariable(Variable variable)
absl::Status ValidateExistingVariableOfThisModel(Variable variable) const
-----------------------— Linear constraints ----------------------------—
Objective AddMaximizationObjective(const LinearExpression &expression, int64_t priority, absl::string_view name={})
Adds expression as an auxiliary maximization objective to the model.
Objective primary_objective() const
IndicatorConstraint indicator_constraint(int64_t id) const
Will CHECK if has_indicator_constraint(id) is false.
LinearExpression ObjectiveAsLinearExpression() const
Sos2Constraint sos2_constraint(int64_t id) const
Will CHECK if has_sos2_constraint(id) is false.
int64_t num_quadratic_constraints() const
bool is_coefficient_nonzero(LinearConstraint constraint, Variable variable) const
QuadraticConstraint quadratic_constraint(int64_t id) const
Will CHECK if has_quadratic_constraint(id) is false.
double objective_offset() const
void SetObjective(double objective, bool is_maximize)
Sets the objective to optimize the provided expression.
IndicatorConstraint AddIndicatorConstraint(Variable indicator_variable, const BoundedLinearExpression &implied_constraint, bool activate_on_zero=false, absl::string_view name={})
------------------------— Indicator constraints ------------------------—
void set_is_integer(Variable variable, bool is_integer)
Sets the integrality of a variable.
LinearConstraint AddLinearConstraint(absl::string_view name="")
Adds a linear constraint to the model with bounds [-inf, +inf].
std::vector< Variable > RowNonzeros(LinearConstraint constraint) const
Sos1Constraint sos1_constraint(int64_t id) const
Will CHECK if has_sos1_constraint(id) is false.
ObjectiveId typed_id() const
static Objective Primary(ModelStorageCPtr storage)
Returns an object that refers to the primary objective of the model.
static Objective Auxiliary(ModelStorageCPtr storage, AuxiliaryObjectiveId id)
Returns an object that refers to an auxiliary objective of the model.
SecondOrderConeConstraintId typed_id() const
Sos1ConstraintId typed_id() const
Sos2ConstraintId typed_id() const
constexpr absl::string_view kObjectsFromOtherModelStorage
An object oriented wrapper for quadratic constraints in ModelStorage.
absl::Nonnull< const ModelStorage * > ModelStorageCPtr
std::vector< ConstraintType > AtomicConstraints(const ModelStorage &storage)
For infeasible and unbounded see Not checked if options check_solutions_if_inf_or_unbounded and the If options first_solution_only is false
problem is infeasible or unbounded (default).
ElementId< ElementType::kAuxiliaryObjective > AuxiliaryObjectiveId
std::vector< ConstraintType > SortedAtomicConstraints(const ModelStorage &storage)
ElementId< ElementType::kVariable > VariableId
constexpr ObjectiveId kPrimaryObjectiveId
absl::Nonnull< ModelStorage * > ModelStoragePtr
ElementId< ElementType::kQuadraticConstraint > QuadraticConstraintId
ElementId< ElementType::kLinearConstraint > LinearConstraintId
ElementId< ElementType::kIndicatorConstraint > IndicatorConstraintId
absl::Nullable< const ModelStorage * > NullableModelStorageCPtr
In SWIG mode, we don't want anything besides these top-level includes.
StatusBuilder InvalidArgumentErrorBuilder()
A LinearExpression with upper and lower bounds.
A QuadraticExpression with upper and lower bounds.
A term in an sum of variables multiplied by coefficients.