17#ifndef OR_TOOLS_MATH_OPT_CPP_MODEL_H_
18#define OR_TOOLS_MATH_OPT_CPP_MODEL_H_
26#include "absl/log/check.h"
27#include "absl/status/status.h"
28#include "absl/status/statusor.h"
29#include "absl/strings/string_view.h"
30#include "absl/types/span.h"
44#include "ortools/math_opt/model.pb.h"
45#include "ortools/math_opt/model_update.pb.h"
129 const ModelProto& model_proto);
132 explicit Model(absl::string_view
name =
"");
161 std::unique_ptr<Model>
Clone(
162 std::optional<absl::string_view> new_name = std::nullopt)
const;
164 inline absl::string_view
name()
const;
182 absl::string_view
name =
"");
187 absl::string_view
name =
"");
270 absl::string_view
name =
"");
377 absl::string_view
name =
"");
405 QuadraticConstraintId
id)
const;
429 absl::Span<const LinearExpression> arguments_to_norm,
453 SecondOrderConeConstraintId
id)
const;
461 SecondOrderConeConstraintId
id)
const;
470 inline std::vector<SecondOrderConeConstraint>
494 const std::vector<LinearExpression>& expressions,
495 std::vector<double> weights = {}, absl::string_view
name =
"");
553 const std::vector<LinearExpression>& expressions,
554 std::vector<double> weights = {}, absl::string_view
name =
"");
614 const BoundedLinearExpression& implied_constraint,
615 bool activate_on_zero =
false, absl::string_view
name = {});
643 IndicatorConstraintId
id)
const;
658 inline void Maximize(
double objective);
662 inline void Maximize(LinearTerm objective);
664 inline void Maximize(
const LinearExpression& objective);
666 inline void Maximize(
const QuadraticExpression& objective);
669 inline void Minimize(
double objective);
673 inline void Minimize(LinearTerm objective);
675 inline void Minimize(
const LinearExpression& objective);
677 inline void Minimize(
const QuadraticExpression& objective);
727 Variable second_variable,
double value);
768 absl::string_view
name = {});
772 absl::string_view
name = {});
776 absl::string_view
name = {});
780 absl::string_view
name = {});
829 inline void Maximize(Objective objective,
const LinearExpression& expression);
831 inline void Minimize(Objective objective,
const LinearExpression& expression);
833 void SetObjective(Objective objective,
const LinearExpression& expression,
837 void AddToObjective(Objective objective,
const LinearExpression& expression);
859 Objective objective)
const;
864 ModelProto
ExportModel(
bool remove_names =
false)
const;
896 const ModelStorage*
storage()
const {
return storage_.get(); }
902 ModelStorage*
storage() {
return storage_.get(); }
907 friend std::ostream&
operator<<(std::ostream& ostr,
const Model& model);
914 inline void CheckOptionalModel(
const ModelStorage* other_storage)
const;
919 inline void CheckModel(
const ModelStorage* other_storage)
const;
927 const std::shared_ptr<ModelStorage> storage_;
942 const bool is_integer,
943 const absl::string_view name) {
953 const double upper_bound,
954 const absl::string_view name) {
960 const absl::string_view
name) {
972 return storage()->next_variable_id().value();
988 CHECK(
has_variable(
id)) <<
"No variable with id: " <<
id.value();
1040 if (storage_.get() !=
variable.storage()) {
1042 <<
"variable with id " <<
variable.id()
1043 <<
" is from a different model";
1048 <<
" is not found in this model (it was probably deleted)";
1050 return absl::OkStatus();
1057 const double upper_bound,
1058 const absl::string_view name) {
1064 CheckModel(constraint.
storage());
1069 return storage()->num_linear_constraints();
1073 return storage()->next_linear_constraint_id().value();
1081 return storage()->has_linear_constraint(
id);
1090 <<
"No linear constraint with id: " <<
id.value();
1095 CheckModel(constraint.
storage());
1101 CheckModel(constraint.
storage());
1107 CheckModel(constraint.
storage());
1108 return storage()->linear_constraint_lower_bound(constraint.
typed_id());
1113 CheckModel(constraint.
storage());
1119 CheckModel(constraint.
storage());
1120 return storage()->linear_constraint_upper_bound(constraint.
typed_id());
1129 <<
" is from a different model";
1134 <<
" is not found in this model (it was probably deleted)";
1136 return absl::OkStatus();
1141 CheckModel(constraint.storage());
1143 storage()->set_linear_constraint_coefficient(constraint.typed_id(),
1149 CheckModel(constraint.
storage());
1151 return storage()->linear_constraint_coefficient(constraint.
typed_id(),
1157 CheckModel(constraint.storage());
1159 return storage()->is_linear_constraint_coefficient_nonzero(
1166 CheckModel(constraint.
storage());
1171 return storage()->num_constraints<QuadraticConstraintId>();
1175 return storage()->next_constraint_id<QuadraticConstraintId>().value();
1183 return storage()->has_constraint(
id);
1191 const QuadraticConstraintId
id)
const {
1193 <<
"No quadratic constraint with id: " <<
id.value();
1209 CheckModel(constraint.
storage());
1214 return storage()->num_constraints<SecondOrderConeConstraintId>();
1218 return storage()->next_constraint_id<SecondOrderConeConstraintId>().value();
1226 const SecondOrderConeConstraintId
id)
const {
1227 return storage()->has_constraint(
id);
1231 const int64_t
id)
const {
1236 const SecondOrderConeConstraintId
id)
const {
1238 <<
"No second-order cone constraint with id: " <<
id.value();
1255 CheckModel(constraint.
storage());
1260 return storage()->num_constraints<Sos1ConstraintId>();
1264 return storage()->next_constraint_id<Sos1ConstraintId>().value();
1272 return storage()->has_constraint(
id);
1281 <<
"No SOS1 constraint with id: " <<
id.value();
1296 CheckModel(constraint.
storage());
1301 return storage()->num_constraints<Sos2ConstraintId>();
1305 return storage()->next_constraint_id<Sos2ConstraintId>().value();
1313 return storage()->has_constraint(
id);
1322 <<
"No SOS2 constraint with id: " <<
id.value();
1337 CheckModel(constraint.
storage());
1342 return storage()->num_constraints<IndicatorConstraintId>();
1346 return storage()->next_constraint_id<IndicatorConstraintId>().value();
1354 return storage()->has_constraint(
id);
1362 const IndicatorConstraintId
id)
const {
1364 <<
"No indicator constraint with id: " <<
id.value();
1441 const Variable second_variable)
const {
1443 CheckModel(second_variable.
storage());
1450 const double value) {
1458 const double value) {
1459 CheckModel(first_variable.
storage());
1460 CheckModel(second_variable.
storage());
1461 storage()->set_quadratic_objective_coefficient(
1463 second_variable.
typed_id(), value);
1472 return storage()->is_linear_objective_coefficient_nonzero(
1479 CheckModel(second_variable.
storage());
1480 return storage()->is_quadratic_objective_coefficient_nonzero(
1512 const absl::string_view
name) {
1518 const bool is_maximize,
1519 const int64_t priority,
1520 const absl::string_view
name) {
1527 const int64_t priority,
1528 const absl::string_view
name) {
1534 const int64_t priority,
1535 const absl::string_view
name) {
1541 CheckModel(objective.
storage());
1542 CHECK(!objective.
is_primary()) <<
"cannot delete primary objective";
1543 const AuxiliaryObjectiveId
id = *objective.
typed_id();
1545 <<
"cannot delete unrecognized auxiliary objective id: " << id;
1546 storage()->DeleteAuxiliaryObjective(
id);
1550 return storage()->num_auxiliary_objectives();
1554 return storage()->next_auxiliary_objective_id().value();
1571 <<
"unrecognized auxiliary objective id: " << id;
1588 const int64_t priority) {
1589 CheckModel(objective.
storage());
1595 const double value) {
1596 CheckModel(objective.
storage());
1603 const double value) {
1617 CheckModel(objective.
storage());
1621void Model::CheckOptionalModel(
const ModelStorage*
const other_storage)
const {
1622 if (other_storage !=
nullptr) {
1623 CHECK_EQ(other_storage,
storage())
1628void 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
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.
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 -----------------------------—
std::vector< Variable > NonzeroVariablesInQuadraticObjective() 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.
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
constexpr absl::string_view kObjectsFromOtherModelStorage
An object oriented wrapper for quadratic constraints in ModelStorage.
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.