17#ifndef OR_TOOLS_MATH_OPT_CPP_MODEL_H_
18#define OR_TOOLS_MATH_OPT_CPP_MODEL_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"
45#include "ortools/math_opt/model.pb.h"
46#include "ortools/math_opt/model_update.pb.h"
130 const ModelProto& model_proto);
133 explicit Model(absl::string_view
name =
"");
162 std::unique_ptr<Model>
Clone(
163 std::optional<absl::string_view> new_name = std::nullopt)
const;
165 inline const std::string&
name()
const;
183 absl::string_view
name =
"");
188 absl::string_view
name =
"");
271 absl::string_view
name =
"");
378 absl::string_view
name =
"");
406 QuadraticConstraintId
id)
const;
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 =
"");
614 Variable indicator_variable,
615 const BoundedLinearExpression& implied_constraint,
616 bool activate_on_zero =
false, absl::string_view
name = {});
644 IndicatorConstraintId
id)
const;
659 inline void Maximize(
double objective);
661 inline void Maximize(Variable 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);
672 inline void Minimize(Variable objective);
674 inline void Minimize(LinearTerm objective);
676 inline void Minimize(
const LinearExpression& objective);
678 inline void Minimize(
const QuadraticExpression& objective);
717 Variable second_variable)
const;
728 Variable second_variable,
double value);
739 Variable second_variable)
const;
762 absl::string_view
name = {});
766 absl::string_view
name = {});
770 absl::string_view
name = {});
774 absl::string_view
name = {});
823 inline void Maximize(Objective objective,
const LinearExpression& expression);
825 inline void Minimize(Objective objective,
const LinearExpression& expression);
827 void SetObjective(Objective objective,
const LinearExpression& expression,
831 void AddToObjective(Objective objective,
const LinearExpression& expression);
853 ModelProto
ExportModel(
bool remove_names =
false)
const;
885 const ModelStorage*
storage()
const {
return storage_.get(); }
891 ModelStorage*
storage() {
return storage_.get(); }
903 inline void CheckOptionalModel(
const ModelStorage* other_storage)
const;
908 inline void CheckModel(
const ModelStorage* other_storage)
const;
916 const std::shared_ptr<ModelStorage> storage_;
931 const bool is_integer,
932 const absl::string_view
name) {
943 const absl::string_view
name) {
949 const absl::string_view
name) {
961 return storage()->next_variable_id().value();
977 CHECK(
has_variable(
id)) <<
"No variable with id: " <<
id.value();
1032 <<
" is from a different model";
1037 <<
" is not found in this model (it was probably deleted)";
1039 return absl::OkStatus();
1047 const absl::string_view
name) {
1053 CheckModel(constraint.
storage());
1058 return storage()->num_linear_constraints();
1062 return storage()->next_linear_constraint_id().value();
1070 return storage()->has_linear_constraint(
id);
1079 <<
"No linear constraint with id: " <<
id.value();
1084 CheckModel(constraint.
storage());
1090 CheckModel(constraint.
storage());
1096 CheckModel(constraint.
storage());
1097 return storage()->linear_constraint_lower_bound(constraint.
typed_id());
1102 CheckModel(constraint.
storage());
1108 CheckModel(constraint.
storage());
1109 return storage()->linear_constraint_upper_bound(constraint.
typed_id());
1118 <<
" is from a different model";
1123 <<
" is not found in this model (it was probably deleted)";
1125 return absl::OkStatus();
1130 CheckModel(constraint.storage());
1132 storage()->set_linear_constraint_coefficient(constraint.typed_id(),
1138 CheckModel(constraint.
storage());
1140 return storage()->linear_constraint_coefficient(constraint.
typed_id(),
1145 const Variable variable)
const {
1146 CheckModel(constraint.storage());
1148 return storage()->is_linear_constraint_coefficient_nonzero(
1155 CheckModel(constraint.
storage());
1160 return storage()->num_constraints<QuadraticConstraintId>();
1164 return storage()->next_constraint_id<QuadraticConstraintId>().
value();
1172 return storage()->has_constraint(
id);
1180 const QuadraticConstraintId
id)
const {
1182 <<
"No quadratic constraint with id: " <<
id.value();
1198 CheckModel(constraint.
storage());
1203 return storage()->num_constraints<SecondOrderConeConstraintId>();
1207 return storage()->next_constraint_id<SecondOrderConeConstraintId>().
value();
1215 const SecondOrderConeConstraintId
id)
const {
1216 return storage()->has_constraint(
id);
1220 const int64_t
id)
const {
1225 const SecondOrderConeConstraintId
id)
const {
1227 <<
"No second-order cone constraint with id: " <<
id.value();
1244 CheckModel(constraint.
storage());
1249 return storage()->num_constraints<Sos1ConstraintId>();
1261 return storage()->has_constraint(
id);
1270 <<
"No SOS1 constraint with id: " <<
id.value();
1285 CheckModel(constraint.
storage());
1290 return storage()->num_constraints<Sos2ConstraintId>();
1294 return storage()->next_constraint_id<Sos2ConstraintId>().
value();
1302 return storage()->has_constraint(
id);
1311 <<
"No SOS2 constraint with id: " <<
id.value();
1326 CheckModel(constraint.
storage());
1331 return storage()->num_constraints<IndicatorConstraintId>();
1335 return storage()->next_constraint_id<IndicatorConstraintId>().
value();
1343 return storage()->has_constraint(
id);
1351 const IndicatorConstraintId
id)
const {
1353 <<
"No indicator constraint with id: " <<
id.value();
1430 const Variable second_variable)
const {
1432 CheckModel(second_variable.
storage());
1439 const double value) {
1447 const double value) {
1448 CheckModel(first_variable.
storage());
1449 CheckModel(second_variable.
storage());
1450 storage()->set_quadratic_objective_coefficient(
1461 return storage()->is_linear_objective_coefficient_nonzero(
1468 CheckModel(second_variable.
storage());
1469 return storage()->is_quadratic_objective_coefficient_nonzero(
1497 const absl::string_view
name) {
1503 const bool is_maximize,
1504 const int64_t priority,
1505 const absl::string_view
name) {
1512 const int64_t priority,
1513 const absl::string_view
name) {
1519 const int64_t priority,
1520 const absl::string_view
name) {
1526 CheckModel(objective.
storage());
1527 CHECK(!objective.
is_primary()) <<
"cannot delete primary objective";
1528 const AuxiliaryObjectiveId
id = *objective.
typed_id();
1530 <<
"cannot delete unrecognized auxiliary objective id: " << id;
1531 storage()->DeleteAuxiliaryObjective(
id);
1535 return storage()->num_auxiliary_objectives();
1539 return storage()->next_auxiliary_objective_id().value();
1556 <<
"unrecognized auxiliary objective id: " << id;
1573 const int64_t priority) {
1574 CheckModel(objective.
storage());
1580 const double value) {
1581 CheckModel(objective.
storage());
1588 const double value) {
1602 CheckModel(objective.
storage());
1606void Model::CheckOptionalModel(
const ModelStorage*
const other_storage)
const {
1607 if (other_storage !=
nullptr) {
1608 CHECK_EQ(other_storage,
storage())
1613void Model::CheckModel(
const ModelStorage*
const other_storage)
const {
IndicatorConstraintId typed_id() const
const ModelStorage * storage() const
const ModelStorage * storage() const
LinearConstraintId typed_id() 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
const ModelStorage * storage() 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
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< 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.
std::unique_ptr< Model > Clone(std::optional< absl::string_view > new_name=std::nullopt) const
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)
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 -----------------------------—
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.
const std::string & name() const
----------------------------— Variables --------------------------------—
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.
static Objective Primary(const ModelStorage *storage)
Returns an object that refers to the primary objective of the model.
static Objective Auxiliary(const ModelStorage *storage, AuxiliaryObjectiveId id)
Returns an object that refers to an auxiliary objective of the model.
ObjectiveId typed_id() const
const ModelStorage * storage() const
Returns a const-pointer to the underlying storage object for the model.
QuadraticConstraintId typed_id() const
const ModelStorage * storage() const
const ModelStorage * storage() const
SecondOrderConeConstraintId typed_id() const
const ModelStorage * storage() const
Sos1ConstraintId typed_id() const
const ModelStorage * storage() const
Sos2ConstraintId typed_id() const
VariableId typed_id() const
const ModelStorage * storage() const
const std::string name
A name for logging purposes.
constexpr absl::string_view kObjectsFromOtherModelStorage
std::vector< ConstraintType > AtomicConstraints(const ModelStorage &storage)
std::vector< ConstraintType > SortedAtomicConstraints(const ModelStorage &storage)
constexpr ObjectiveId kPrimaryObjectiveId
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.