Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::math_opt::Model Class Reference

#include <model.h>

Public Member Functions

 Model (absl::string_view name="")
 Creates an empty minimization problem.
 
 Model (std::unique_ptr< ModelStorage > storage)
 
 Model (const Model &)=delete
 
Modeloperator= (const Model &)=delete
 
std::unique_ptr< ModelClone (std::optional< absl::string_view > new_name=std::nullopt) const
 
const std::string & name () const
 ----------------------------— Variables --------------------------------—
 
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.
 
Variable AddVariable (absl::string_view name="")
 Adds a continuous unbounded variable to the model.
 
Variable AddBinaryVariable (absl::string_view name="")
 Adds a variable to the model with domain {0, 1}.
 
Variable AddContinuousVariable (double lower_bound, double upper_bound, absl::string_view name="")
 Adds a variable to the model with domain [lower_bound, upper_bound].
 
Variable AddIntegerVariable (double lower_bound, double upper_bound, absl::string_view name="")
 
void DeleteVariable (Variable variable)
 
int num_variables () const
 
int64_t next_variable_id () const
 
bool has_variable (int64_t id) const
 Returns true if this id has been created and not yet deleted.
 
bool has_variable (VariableId 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.
 
Variable variable (VariableId id) const
 Will CHECK if has_variable(id) is false.
 
const std::string & name (Variable variable) const
 Returns the variable name.
 
void set_lower_bound (Variable variable, double lower_bound)
 Sets a variable lower bound.
 
double lower_bound (Variable variable) const
 Returns a variable lower bound.
 
void set_upper_bound (Variable variable, double upper_bound)
 Sets a variable upper bound.
 
double upper_bound (Variable variable) const
 Returns a variable upper bound.
 
void set_is_integer (Variable variable, bool is_integer)
 Sets the integrality of a variable.
 
void set_integer (Variable variable)
 Makes the input variable integer.
 
void set_continuous (Variable variable)
 Makes the input variable continuous.
 
bool is_integer (Variable variable) const
 Returns the integrality of a variable.
 
std::vector< VariableVariables () const
 
std::vector< VariableSortedVariables () const
 
absl::Status ValidateExistingVariableOfThisModel (Variable variable) const
 -----------------------— Linear constraints ----------------------------—
 
std::vector< LinearConstraintColumnNonzeros (Variable variable) const
 
LinearConstraint AddLinearConstraint (absl::string_view name="")
 Adds a linear constraint to the model with bounds [-inf, +inf].
 
LinearConstraint AddLinearConstraint (double lower_bound, double upper_bound, absl::string_view name="")
 Adds a linear constraint with bounds [lower_bound, upper_bound].
 
LinearConstraint AddLinearConstraint (const BoundedLinearExpression &bounded_expr, absl::string_view name="")
 
void DeleteLinearConstraint (LinearConstraint constraint)
 
int num_linear_constraints () const
 
int64_t next_linear_constraint_id () const
 
bool has_linear_constraint (int64_t id) const
 Returns true if this id has been created and not yet deleted.
 
bool has_linear_constraint (LinearConstraintId id) const
 Returns true if this id has been created and not yet deleted.
 
LinearConstraint linear_constraint (int64_t id) const
 Will CHECK if has_linear_constraint(id) is false.
 
LinearConstraint linear_constraint (LinearConstraintId id) const
 Will CHECK if has_linear_constraint(id) is false.
 
const std::string & name (LinearConstraint constraint) const
 Returns the linear constraint name.
 
void set_lower_bound (LinearConstraint constraint, double lower_bound)
 Sets a linear constraint lower bound.
 
double lower_bound (LinearConstraint constraint) const
 Returns a linear constraint lower bound.
 
void set_upper_bound (LinearConstraint constraint, double upper_bound)
 Sets a linear constraint upper bound.
 
double upper_bound (LinearConstraint constraint) const
 Returns a linear constraint upper bound.
 
void set_coefficient (LinearConstraint constraint, Variable variable, double value)
 
double coefficient (LinearConstraint constraint, Variable variable) const
 Returns 0.0 if the variable is not used in the constraint.
 
bool is_coefficient_nonzero (LinearConstraint constraint, Variable variable) const
 
std::vector< VariableRowNonzeros (LinearConstraint constraint) const
 
std::vector< LinearConstraintLinearConstraints () const
 
std::vector< LinearConstraintSortedLinearConstraints () const
 
absl::Status ValidateExistingLinearConstraintOfThisModel (LinearConstraint linear_constraint) const
 
QuadraticConstraint AddQuadraticConstraint (const BoundedQuadraticExpression &bounded_expr, absl::string_view name="")
 ----------------------— Quadratic constraints --------------------------—
 
void DeleteQuadraticConstraint (QuadraticConstraint constraint)
 ----------------------— Quadratic constraints --------------------------—
 
int64_t num_quadratic_constraints () const
 
int64_t next_quadratic_constraint_id () const
 The returned id of the next call to AddQuadraticConstraint.
 
bool has_quadratic_constraint (int64_t id) const
 Returns true if this id has been created and not yet deleted.
 
bool has_quadratic_constraint (QuadraticConstraintId id) const
 Returns true if this id has been created and not yet deleted.
 
QuadraticConstraint quadratic_constraint (int64_t id) const
 Will CHECK if has_quadratic_constraint(id) is false.
 
QuadraticConstraint quadratic_constraint (QuadraticConstraintId id) const
 Will CHECK if has_quadratic_constraint(id) is false.
 
std::vector< QuadraticConstraintQuadraticConstraints () const
 
std::vector< QuadraticConstraintSortedQuadraticConstraints () const
 
SecondOrderConeConstraint AddSecondOrderConeConstraint (absl::Span< const LinearExpression > arguments_to_norm, const LinearExpression &upper_bound, absl::string_view name="")
 ------------------— Second-order cone constraints ----------------------—
 
void DeleteSecondOrderConeConstraint (SecondOrderConeConstraint constraint)
 ------------------— Second-order cone constraints ----------------------—
 
int64_t num_second_order_cone_constraints () const
 
int64_t next_second_order_cone_constraint_id () const
 The returned id of the next call to AddSecondOrderConeConstraint.
 
bool has_second_order_cone_constraint (int64_t id) const
 Returns true if this id has been created and not yet deleted.
 
bool has_second_order_cone_constraint (SecondOrderConeConstraintId id) const
 Returns true if this id has been created and not yet deleted.
 
SecondOrderConeConstraint second_order_cone_constraint (int64_t id) const
 Will CHECK if has_second_order_cone_constraint(id) is false.
 
SecondOrderConeConstraint second_order_cone_constraint (SecondOrderConeConstraintId id) const
 Will CHECK if has_second_order_cone_constraint(id) is false.
 
std::vector< SecondOrderConeConstraintSecondOrderConeConstraints () const
 
std::vector< SecondOrderConeConstraintSortedSecondOrderConeConstraints () const
 
Sos1Constraint AddSos1Constraint (const std::vector< LinearExpression > &expressions, std::vector< double > weights={}, absl::string_view name="")
 
void DeleteSos1Constraint (Sos1Constraint constraint)
 ------------------------— SOS1 constraints -----------------------------—
 
int64_t num_sos1_constraints () const
 
int64_t next_sos1_constraint_id () const
 The returned id of the next call to AddSos1Constraint.
 
bool has_sos1_constraint (int64_t id) const
 Returns true if this id has been created and not yet deleted.
 
bool has_sos1_constraint (Sos1ConstraintId id) const
 Returns true if this id has been created and not yet deleted.
 
Sos1Constraint sos1_constraint (int64_t id) const
 Will CHECK if has_sos1_constraint(id) is false.
 
Sos1Constraint sos1_constraint (Sos1ConstraintId id) const
 Will CHECK if has_sos1_constraint(id) is false.
 
std::vector< Sos1ConstraintSos1Constraints () const
 
std::vector< Sos1ConstraintSortedSos1Constraints () const
 
Sos2Constraint AddSos2Constraint (const std::vector< LinearExpression > &expressions, std::vector< double > weights={}, absl::string_view name="")
 ------------------------— SOS2 constraints -----------------------------—
 
void DeleteSos2Constraint (Sos2Constraint constraint)
 ------------------------— SOS2 constraints -----------------------------—
 
int64_t num_sos2_constraints () const
 
int64_t next_sos2_constraint_id () const
 The returned id of the next call to AddSos2Constraint.
 
bool has_sos2_constraint (int64_t id) const
 Returns true if this id has been created and not yet deleted.
 
bool has_sos2_constraint (Sos2ConstraintId id) const
 Returns true if this id has been created and not yet deleted.
 
Sos2Constraint sos2_constraint (int64_t id) const
 Will CHECK if has_sos2_constraint(id) is false.
 
Sos2Constraint sos2_constraint (Sos2ConstraintId id) const
 Will CHECK if has_sos2_constraint(id) is false.
 
std::vector< Sos2ConstraintSos2Constraints () const
 
std::vector< Sos2ConstraintSortedSos2Constraints () const
 
IndicatorConstraint AddIndicatorConstraint (Variable indicator_variable, const BoundedLinearExpression &implied_constraint, bool activate_on_zero=false, absl::string_view name={})
 ------------------------— Indicator constraints ------------------------—
 
void DeleteIndicatorConstraint (IndicatorConstraint constraint)
 ------------------------— Indicator constraints ------------------------—
 
int64_t num_indicator_constraints () const
 
int64_t next_indicator_constraint_id () const
 The returned id of the next call to AddIndicatorConstraint.
 
bool has_indicator_constraint (int64_t id) const
 Returns true if this id has been created and not yet deleted.
 
bool has_indicator_constraint (IndicatorConstraintId id) const
 Returns true if this id has been created and not yet deleted.
 
IndicatorConstraint indicator_constraint (int64_t id) const
 Will CHECK if has_indicator_constraint(id) is false.
 
IndicatorConstraint indicator_constraint (IndicatorConstraintId id) const
 Will CHECK if has_indicator_constraint(id) is false.
 
std::vector< IndicatorConstraintIndicatorConstraints () const
 
std::vector< IndicatorConstraintSortedIndicatorConstraints () const
 
void Maximize (double objective)
 Sets the objective to maximize the provided expression.
 
void Maximize (Variable objective)
 Sets the objective to maximize the provided expression.
 
void Maximize (LinearTerm objective)
 Sets the objective to maximize the provided expression.
 
void Maximize (const LinearExpression &objective)
 Sets the objective to maximize the provided expression.
 
void Maximize (const QuadraticExpression &objective)
 Sets the objective to maximize the provided expression.
 
void Minimize (double objective)
 Sets the objective to minimize the provided expression.
 
void Minimize (Variable objective)
 Sets the objective to minimize the provided expression.
 
void Minimize (LinearTerm objective)
 Sets the objective to minimize the provided expression.
 
void Minimize (const LinearExpression &objective)
 Sets the objective to minimize the provided expression.
 
void Minimize (const QuadraticExpression &objective)
 Sets the objective to minimize the provided expression.
 
void SetObjective (double objective, bool is_maximize)
 Sets the objective to optimize the provided expression.
 
void SetObjective (Variable objective, bool is_maximize)
 Sets the objective to optimize the provided expression.
 
void SetObjective (LinearTerm objective, bool is_maximize)
 Sets the objective to optimize the provided expression.
 
void SetObjective (const LinearExpression &objective, bool is_maximize)
 Sets the objective to optimize the provided expression.
 
void SetObjective (const QuadraticExpression &objective, bool is_maximize)
 Sets the objective to optimize the provided expression.
 
void AddToObjective (double objective)
 Adds the provided expression terms to the objective.
 
void AddToObjective (Variable objective)
 Adds the provided expression terms to the objective.
 
void AddToObjective (LinearTerm objective)
 Adds the provided expression terms to the objective.
 
void AddToObjective (const LinearExpression &objective)
 Adds the provided expression terms to the objective.
 
void AddToObjective (const QuadraticExpression &objective)
 Adds the provided expression terms to the objective.
 
LinearExpression ObjectiveAsLinearExpression () const
 
QuadraticExpression ObjectiveAsQuadraticExpression () const
 
Objective primary_objective () const
 
double objective_coefficient (Variable variable) const
 Returns 0.0 if this variable has no linear objective coefficient.
 
double objective_coefficient (Variable first_variable, Variable second_variable) const
 
void set_objective_coefficient (Variable variable, double value)
 
void set_objective_coefficient (Variable first_variable, Variable second_variable, double value)
 
void clear_objective ()
 
bool is_objective_coefficient_nonzero (Variable variable) const
 
bool is_objective_coefficient_nonzero (Variable first_variable, Variable second_variable) const
 
double objective_offset () const
 
void set_objective_offset (double value)
 
bool is_maximize () const
 
void set_maximize ()
 
void set_minimize ()
 
void set_is_maximize (bool is_maximize)
 Prefer set_maximize() and set_minimize() above for more readable code.
 
Objective AddAuxiliaryObjective (int64_t priority, absl::string_view name={})
 Adds an empty (== 0) auxiliary minimization objective to the model.
 
Objective AddAuxiliaryObjective (const LinearExpression &expression, bool is_maximize, int64_t priority, absl::string_view name={})
 Adds expression as an auxiliary objective to the model.
 
Objective AddMaximizationObjective (const LinearExpression &expression, int64_t priority, absl::string_view name={})
 Adds expression as an auxiliary maximization objective to the model.
 
Objective AddMinimizationObjective (const LinearExpression &expression, int64_t priority, absl::string_view name={})
 Adds expression as an auxiliary minimization objective to the model.
 
void DeleteAuxiliaryObjective (Objective objective)
 
int64_t num_auxiliary_objectives () const
 
int64_t next_auxiliary_objective_id () const
 The returned id of the next call to AddAuxiliaryObjectve.
 
bool has_auxiliary_objective (int64_t id) const
 Returns true if this id has been created and not yet deleted.
 
bool has_auxiliary_objective (AuxiliaryObjectiveId id) const
 Returns true if this id has been created and not yet deleted.
 
Objective auxiliary_objective (int64_t id) const
 Will CHECK if has_auxiliary_objective(id) is false.
 
Objective auxiliary_objective (AuxiliaryObjectiveId id) const
 Will CHECK if has_auxiliary_objective(id) is false.
 
std::vector< ObjectiveAuxiliaryObjectives () const
 
std::vector< ObjectiveSortedAuxiliaryObjectives () const
 
void Maximize (Objective objective, const LinearExpression &expression)
 Sets objective to be maximizing expression.
 
void Minimize (Objective objective, const LinearExpression &expression)
 Sets objective to be minimizing expression.
 
void SetObjective (Objective objective, const LinearExpression &expression, bool is_maximize)
 Sets the objective to optimize the provided expression.
 
void AddToObjective (Objective objective, const LinearExpression &expression)
 Adds the provided expression terms to the objective.
 
void set_objective_priority (Objective objective, int64_t priority)
 
void set_objective_coefficient (Objective objective, Variable variable, double value)
 
void set_objective_offset (Objective objective, double value)
 
void set_maximize (Objective objective)
 
void set_minimize (Objective objective)
 
void set_is_maximize (Objective objective, bool is_maximize)
 Prefer set_maximize() and set_minimize() above for more readable code.
 
ModelProto ExportModel (bool remove_names=false) const
 
std::unique_ptr< UpdateTrackerNewUpdateTracker ()
 
absl::Status ApplyUpdateProto (const ModelUpdateProto &update_proto)
 
const ModelStorage * storage () const
 
ModelStorage * storage ()
 

Static Public Member Functions

static absl::StatusOr< std::unique_ptr< Model > > FromModelProto (const ModelProto &model_proto)
 

Friends

std::ostream & operator<< (std::ostream &ostr, const Model &model)
 

Detailed Description

A C++ API for building optimization problems.

Warning
Variable and LinearConstraint (along with all other constraint objects) are value types, see "Memory Model" below.

A simple example:

Model the problem: max 2.0 * x + y s.t. x + y <= 1.5 x in {0.0, 1.0} y in [0.0, 2.5]

math_opt::Model model("my_model"); const math_opt::Variable x = model.AddBinaryVariable("x"); const math_opt::Variable y = model.AddContinuousVariable(0.0, 2.5, "y"); ///< We can directly use linear combinations of variables ... model.AddLinearConstraint(x + y <= 1.5, "c"); ///< ... or build them incrementally. math_opt::LinearExpression objective_expression; objective_expression += 2 * x; objective_expression += y; model.Maximize(objective_expression); ASSIGN_OR_RETURN(const math_opt::SolveResult result, Solve(model, math_opt::SolverType::kGscip)); switch (result.termination.reason) { case math_opt::TerminationReason::kOptimal: case math_opt::TerminationReason::kFeasible: std::cout << "objective value: " << result.objective_value() << std::endl << "value for variable x: " << result.variable_values().at(x) << std::endl; return absl::OkStatus(); default: return util::InternalErrorBuilder() << "model failed to solve: " << result.termination; }

Memory model:

Variable, LinearConstraint, QuadraticConstraint, etc. are value types that represent references to the underlying Model object. They don't hold any of the actual model data, they can be copied, and they should be passed by value. They can be regenerated arbitrarily from Model. Model holds all the data.

As a consequence of Variable and LinearConstraint holding back pointers, Model is not copyable or movable. Users needing to copy a Model can call Model::Clone() (this will create a new Model with no update trackers), and users needing to move a Model should wrap it in a std::unique_ptr.

Performance:

This class is a thin wrapper around ModelStorage (for incrementally building the model and reading it back, and producing the Model proto). Operations for building/reading/modifying the problem typically run in O(read/write size) and rely on hashing, see the ModelStorage documentation for details. At solve time (if you are solving locally) beware that there will be (at least) three copies of the model in memory, ModelStorage, the Model proto, and the underlying solver's copy(/ies). Note that the Model proto is reclaimed before the underlying solver begins solving.

Definition at line 113 of file model.h.

Constructor & Destructor Documentation

◆ Model() [1/3]

operations_research::math_opt::Model::Model ( absl::string_view name = "")
explicit

Creates an empty minimization problem.

Definition at line 60 of file model.cc.

◆ Model() [2/3]

operations_research::math_opt::Model::Model ( std::unique_ptr< ModelStorage > storage)
explicit

Creates a model from the existing model storage.

This constructor is used when loading a model, for example from a ModelProto or an MPS file. Note that in those cases the FromModelProto() should be used.

Definition at line 63 of file model.cc.

◆ Model() [3/3]

operations_research::math_opt::Model::Model ( const Model & )
delete

Member Function Documentation

◆ AddAuxiliaryObjective() [1/2]

Objective operations_research::math_opt::Model::AddAuxiliaryObjective ( const LinearExpression & expression,
bool is_maximize,
int64_t priority,
absl::string_view name = {} )
inline

Adds expression as an auxiliary objective to the model.

Definition at line 1524 of file model.h.

◆ AddAuxiliaryObjective() [2/2]

Objective operations_research::math_opt::Model::AddAuxiliaryObjective ( int64_t priority,
absl::string_view name = {} )
inline

Adds an empty (== 0) auxiliary minimization objective to the model.

-----------------------— Auxiliary objectives --------------------------—

Auxiliary objective methods

This is an API for creating and deleting auxiliary objectives. To modify them, use the multi-objective API below.

Definition at line 1518 of file model.h.

◆ AddBinaryVariable()

Variable operations_research::math_opt::Model::AddBinaryVariable ( absl::string_view name = "")
inline

Adds a variable to the model with domain {0, 1}.

Definition at line 959 of file model.h.

◆ AddContinuousVariable()

Variable operations_research::math_opt::Model::AddContinuousVariable ( double lower_bound,
double upper_bound,
absl::string_view name = "" )
inline

Adds a variable to the model with domain [lower_bound, upper_bound].

Definition at line 963 of file model.h.

◆ AddIndicatorConstraint()

IndicatorConstraint operations_research::math_opt::Model::AddIndicatorConstraint ( Variable indicator_variable,
const BoundedLinearExpression & implied_constraint,
bool activate_on_zero = false,
absl::string_view name = {} )

------------------------— Indicator constraints ------------------------—

IndicatorConstraint methods Adds an indicator constraint to the model.

Assume for the moment that activate_on_zero == false (the default value).

  • If indicator_variable == 1, then implied_constraint must hold.
  • If indicator_variable == 0, then implied_constraint need not hold. Alternatively, if activate_on_zero = true, flip the 1 and 0 above.

The indicator_variable is expected to be a binary variable in the model. If this is not the case, the solver may elect to either implicitly add the binary constraint, or reject the model.

Usage: Model model = ...; const Variable x = model.AddBinaryVariable("x"); const Variable y = model.AddBinaryVariable("y"); model.AddIndicatorConstraint(x, y <= 0); model.AddIndicatorConstraint(y, x >= 2, true, "c");

We ignore the offset while unpacking here; instead, we account for it below by using the {lower,upper}_bound_minus_offset member functions.

Definition at line 477 of file model.cc.

◆ AddIntegerVariable()

Variable operations_research::math_opt::Model::AddIntegerVariable ( double lower_bound,
double upper_bound,
absl::string_view name = "" )
inline

Adds a variable to the model that can take integer values between lower_bound and upper_bound (inclusive).

Definition at line 969 of file model.h.

◆ AddLinearConstraint() [1/3]

LinearConstraint operations_research::math_opt::Model::AddLinearConstraint ( absl::string_view name = "")
inline

Adds a linear constraint to the model with bounds [-inf, +inf].

LinearConstraint methods

Definition at line 1064 of file model.h.

◆ AddLinearConstraint() [2/3]

LinearConstraint operations_research::math_opt::Model::AddLinearConstraint ( const BoundedLinearExpression & bounded_expr,
absl::string_view name = "" )

Adds a linear constraint from the given bounded linear expression.

Usage: Model model = ...; const Variable x = ...; const Variable y = ...; model.AddLinearConstraint(3 <= 2 * x + y + 1 <= 5, "c"); ///< The new constraint formula is: ///< 3 - 1 <= 2 * x + y <= 5 - 1 ///< Which is: ///< 2 <= 2 * x + y <= 4 ///< since the offset has been removed from bounds.

model.AddLinearConstraint(2 * x + y == x + 5 * z + 3); model.AddLinearConstraint(x >= 5);

Definition at line 71 of file model.cc.

◆ AddLinearConstraint() [3/3]

LinearConstraint operations_research::math_opt::Model::AddLinearConstraint ( double lower_bound,
double upper_bound,
absl::string_view name = "" )
inline

Adds a linear constraint with bounds [lower_bound, upper_bound].

Definition at line 1067 of file model.h.

◆ AddMaximizationObjective()

Objective operations_research::math_opt::Model::AddMaximizationObjective ( const LinearExpression & expression,
int64_t priority,
absl::string_view name = {} )
inline

Adds expression as an auxiliary maximization objective to the model.

Definition at line 1533 of file model.h.

◆ AddMinimizationObjective()

Objective operations_research::math_opt::Model::AddMinimizationObjective ( const LinearExpression & expression,
int64_t priority,
absl::string_view name = {} )
inline

Adds expression as an auxiliary minimization objective to the model.

Definition at line 1540 of file model.h.

◆ AddQuadraticConstraint()

QuadraticConstraint operations_research::math_opt::Model::AddQuadraticConstraint ( const BoundedQuadraticExpression & bounded_expr,
absl::string_view name = "" )

----------------------— Quadratic constraints --------------------------—

QuadraticConstraint methods Adds a quadratic constraint from the given bounded quadratic expression.

Usage: Model model = ...; const Variable x = ...; const Variable y = ...; model.AddQuadraticConstraint(2 * x * x + y + 1 <= 5, "q"); model.AddQuadraticConstraint(2 * x * x + y * y == x + 5 * z + 3); model.AddQuadraticConstraint(x * y >= 5);

Definition at line 383 of file model.cc.

◆ AddSecondOrderConeConstraint()

SecondOrderConeConstraint operations_research::math_opt::Model::AddSecondOrderConeConstraint ( absl::Span< const LinearExpression > arguments_to_norm,
const LinearExpression & upper_bound,
absl::string_view name = "" )

------------------— Second-order cone constraints ----------------------—

SecondOrderConeConstraint methods Adds a second-order cone constraint to the model of the form |arguments_to_norm|₂ ≤ upper_bound.

Usage: Model model = ...; const Variable x = ...; const Variable y = ...; model.AddSecondOrderConeConstraint({x, y}, 1.0, "soc"); model.AddSecondOrderConeConstraint({1.0, 3 * y - x}, 2 * x);

Definition at line 410 of file model.cc.

◆ AddSos1Constraint()

Sos1Constraint operations_research::math_opt::Model::AddSos1Constraint ( const std::vector< LinearExpression > & expressions,
std::vector< double > weights = {},
absl::string_view name = "" )

Sos1Constraint methods Adds an SOS1 constraint to the model: at most one of the expressions may take a nonzero value.

The weights are an implementation detail in the solver used to order the expressions; see the Gurobi documentation for more detail: https://www.gurobi.com/documentation/9.5/refman/constraints.html#subsubsection:SOSConstraints

These weights must either be empty or the same length as expressions. If it is empty, default weights of 1, 2, ... will be used.

Usage: Model model = ...; const Variable x = ...; const Variable y = ...; model.AddSos1Constraint({x, y}, {}, "c"); model.AddSos1Constraint({1 - 2 * x, y}, {3, 2});

Definition at line 451 of file model.cc.

◆ AddSos2Constraint()

Sos2Constraint operations_research::math_opt::Model::AddSos2Constraint ( const std::vector< LinearExpression > & expressions,
std::vector< double > weights = {},
absl::string_view name = "" )

------------------------— SOS2 constraints -----------------------------—

Sos2Constraint methods Adds an SOS2 constraint to the model: at most two of the expressions may take a nonzero value, and they must be adjacent in their ordering.

The weights are an implementation detail in the solver used to order the expressions; see the Gurobi documentation for more detail: https://www.gurobi.com/documentation/9.5/refman/constraints.html#subsubsection:SOSConstraints

These weights must either be empty or the same length as expressions. If it is empty, default weights of 1, 2, ... will be used.

Usage: Model model = ...; const Variable x = ...; const Variable y = ...; model.AddSos2Constraint({x, y}, {}, "c"); model.AddSos2Constraint({1 - 2 * x, y}, {3, 2});

Definition at line 464 of file model.cc.

◆ AddToObjective() [1/6]

void operations_research::math_opt::Model::AddToObjective ( const LinearExpression & objective)

Adds the provided expression terms to the objective.

Definition at line 172 of file model.cc.

◆ AddToObjective() [2/6]

void operations_research::math_opt::Model::AddToObjective ( const QuadraticExpression & objective)

Adds the provided expression terms to the objective.

Definition at line 186 of file model.cc.

◆ AddToObjective() [3/6]

void operations_research::math_opt::Model::AddToObjective ( double objective)
inline

Adds the provided expression terms to the objective.

Definition at line 1431 of file model.h.

◆ AddToObjective() [4/6]

void operations_research::math_opt::Model::AddToObjective ( LinearTerm objective)
inline

Adds the provided expression terms to the objective.

Definition at line 1437 of file model.h.

◆ AddToObjective() [5/6]

void operations_research::math_opt::Model::AddToObjective ( Objective objective,
const LinearExpression & expression )

Adds the provided expression terms to the objective.

Definition at line 264 of file model.cc.

◆ AddToObjective() [6/6]

void operations_research::math_opt::Model::AddToObjective ( Variable objective)
inline

Adds the provided expression terms to the objective.

Definition at line 1434 of file model.h.

◆ AddVariable() [1/2]

Variable operations_research::math_opt::Model::AddVariable ( absl::string_view name = "")
inline

Adds a continuous unbounded variable to the model.

Definition at line 949 of file model.h.

◆ AddVariable() [2/2]

Variable operations_research::math_opt::Model::AddVariable ( double lower_bound,
double upper_bound,
bool is_integer,
absl::string_view name = "" )
inline

Adds a variable to the model and returns a reference to it.

Variable methods

Definition at line 952 of file model.h.

◆ ApplyUpdateProto()

absl::Status operations_research::math_opt::Model::ApplyUpdateProto ( const ModelUpdateProto & update_proto)

Apply the provided update to this model. Returns a failure if the update is not valid.

As with FromModelProto(), duplicated names are ignored.

Note
it takes O(num_variables + num_constraints) extra memory and execution to apply the update (due to the need to build a ModelSummary). So even a small update will have some cost.

Definition at line 283 of file model.cc.

◆ auxiliary_objective() [1/2]

Objective operations_research::math_opt::Model::auxiliary_objective ( AuxiliaryObjectiveId id) const
inline

Will CHECK if has_auxiliary_objective(id) is false.

Definition at line 1576 of file model.h.

◆ auxiliary_objective() [2/2]

Objective operations_research::math_opt::Model::auxiliary_objective ( int64_t id) const
inline

Will CHECK if has_auxiliary_objective(id) is false.

Definition at line 1572 of file model.h.

◆ AuxiliaryObjectives()

std::vector< Objective > operations_research::math_opt::Model::AuxiliaryObjectives ( ) const

Returns all the existing (created and not deleted) auxiliary objectives in the model in an arbitrary order.

Definition at line 233 of file model.cc.

◆ clear_objective()

void operations_research::math_opt::Model::clear_objective ( )
inline

Sets the objective offset, linear terms, and quadratic terms of the objective to zero. The name, direction, and priority are unchanged. Equivalent to SetObjective(0.0, is_maximize()).

Runs in O(#linear and quadratic objective terms with nonzero coefficient).

Definition at line 1477 of file model.h.

◆ Clone()

std::unique_ptr< Model > operations_research::math_opt::Model::Clone ( std::optional< absl::string_view > new_name = std::nullopt) const

Returns a clone of this model, optionally changing the model's name.

The variables and constraints have the same integer ids. The clone will also not reused any id of variable/constraint that was deleted in the original.

That said, the Variable and LinearConstraint reference objects are model specific. Hence the ones linked to the original model must NOT be used with the clone. The Variable and LinearConstraint reference objects for the clone can be obtained using:

Note
the returned model does not have any update tracker.

Definition at line 66 of file model.cc.

◆ coefficient()

double operations_research::math_opt::Model::coefficient ( LinearConstraint constraint,
Variable variable ) const
inline

Returns 0.0 if the variable is not used in the constraint.

Definition at line 1158 of file model.h.

◆ ColumnNonzeros()

std::vector< LinearConstraint > operations_research::math_opt::Model::ColumnNonzeros ( Variable variable) const

Definition at line 103 of file model.cc.

◆ DeleteAuxiliaryObjective()

void operations_research::math_opt::Model::DeleteAuxiliaryObjective ( Objective objective)
inline

Removes an auxiliary objective from the model.

It is an error to use any reference to this auxiliary objective after this operation. Runs in O(1) time.

Will CHECK-fail if objective is from another model, has already been deleted, or is a primary objective.

Definition at line 1547 of file model.h.

◆ DeleteIndicatorConstraint()

void operations_research::math_opt::Model::DeleteIndicatorConstraint ( IndicatorConstraint constraint)
inline

------------------------— Indicator constraints ------------------------—

Removes an indicator constraint from the model.

It is an error to use any reference to this indicator constraint after this operation. Runs in O(#terms in implied constraint).

Definition at line 1347 of file model.h.

◆ DeleteLinearConstraint()

void operations_research::math_opt::Model::DeleteLinearConstraint ( LinearConstraint constraint)
inline

Removes a linear constraint from the model.

It is an error to use any reference to this linear constraint after this operation. Runs in O(#variables in the linear constraint).

Definition at line 1074 of file model.h.

◆ DeleteQuadraticConstraint()

void operations_research::math_opt::Model::DeleteQuadraticConstraint ( QuadraticConstraint constraint)
inline

----------------------— Quadratic constraints --------------------------—

Removes a quadratic constraint from the model.

It is an error to use any reference to this quadratic constraint after this operation. Runs in O(#linear or quadratic terms appearing in constraint).

Definition at line 1176 of file model.h.

◆ DeleteSecondOrderConeConstraint()

void operations_research::math_opt::Model::DeleteSecondOrderConeConstraint ( SecondOrderConeConstraint constraint)
inline

------------------— Second-order cone constraints ----------------------—

Removes a second-order cone constraint from the model.

It is an error to use any reference to this second-order cone constraint after this operation. Runs in O(#linear terms appearing in constraint).

Definition at line 1218 of file model.h.

◆ DeleteSos1Constraint()

void operations_research::math_opt::Model::DeleteSos1Constraint ( Sos1Constraint constraint)
inline

------------------------— SOS1 constraints -----------------------------—

Removes an SOS1 constraint from the model.

It is an error to use any reference to this SOS1 constraint after this operation. Runs in O(#terms in all expressions).

Definition at line 1265 of file model.h.

◆ DeleteSos2Constraint()

void operations_research::math_opt::Model::DeleteSos2Constraint ( Sos2Constraint constraint)
inline

------------------------— SOS2 constraints -----------------------------—

Removes an SOS2 constraint from the model.

It is an error to use any reference to this SOS2 constraint after this operation. Runs in O(#terms in all expressions).

Definition at line 1306 of file model.h.

◆ DeleteVariable()

void operations_research::math_opt::Model::DeleteVariable ( Variable variable)
inline

Removes a variable from the model.

It is an error to use any reference to this variable after this operation. Runs in O(#constraints containing the variable).

Definition at line 975 of file model.h.

◆ ExportModel()

ModelProto operations_research::math_opt::Model::ExportModel ( bool remove_names = false) const

Returns a proto representation of the optimization model.

See FromModelProto() to build a Model from a proto.

Definition at line 275 of file model.cc.

◆ FromModelProto()

absl::StatusOr< std::unique_ptr< Model > > operations_research::math_opt::Model::FromModelProto ( const ModelProto & model_proto)
static

Returns a model from the input proto. Returns a failure status if the input proto is invalid.

On top of loading a model from a MathOpt ModelProto, this function can also be used to load a model from other formats using the functions in math_opt/io/ like ReadMpsFile().

See ExportModel() to get the proto of a Model. See ApplyUpdateProto() to apply an update to the model.

Usage example reading an MPS file: ASSIGN_OR_RETURN(const ModelProto model_proto, ReadMpsFile(path)); ASSIGN_OR_RETURN(const std::unique_ptr<Model> model, Model::FromModelProto(model_proto));

Definition at line 53 of file model.cc.

◆ has_auxiliary_objective() [1/2]

bool operations_research::math_opt::Model::has_auxiliary_objective ( AuxiliaryObjectiveId id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1568 of file model.h.

◆ has_auxiliary_objective() [2/2]

bool operations_research::math_opt::Model::has_auxiliary_objective ( int64_t id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1564 of file model.h.

◆ has_indicator_constraint() [1/2]

bool operations_research::math_opt::Model::has_indicator_constraint ( IndicatorConstraintId id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1364 of file model.h.

◆ has_indicator_constraint() [2/2]

bool operations_research::math_opt::Model::has_indicator_constraint ( int64_t id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1360 of file model.h.

◆ has_linear_constraint() [1/2]

bool operations_research::math_opt::Model::has_linear_constraint ( int64_t id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1087 of file model.h.

◆ has_linear_constraint() [2/2]

bool operations_research::math_opt::Model::has_linear_constraint ( LinearConstraintId id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1091 of file model.h.

◆ has_quadratic_constraint() [1/2]

bool operations_research::math_opt::Model::has_quadratic_constraint ( int64_t id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1189 of file model.h.

◆ has_quadratic_constraint() [2/2]

bool operations_research::math_opt::Model::has_quadratic_constraint ( QuadraticConstraintId id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1193 of file model.h.

◆ has_second_order_cone_constraint() [1/2]

bool operations_research::math_opt::Model::has_second_order_cone_constraint ( int64_t id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1232 of file model.h.

◆ has_second_order_cone_constraint() [2/2]

bool operations_research::math_opt::Model::has_second_order_cone_constraint ( SecondOrderConeConstraintId id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1236 of file model.h.

◆ has_sos1_constraint() [1/2]

bool operations_research::math_opt::Model::has_sos1_constraint ( int64_t id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1278 of file model.h.

◆ has_sos1_constraint() [2/2]

bool operations_research::math_opt::Model::has_sos1_constraint ( Sos1ConstraintId id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1282 of file model.h.

◆ has_sos2_constraint() [1/2]

bool operations_research::math_opt::Model::has_sos2_constraint ( int64_t id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1319 of file model.h.

◆ has_sos2_constraint() [2/2]

bool operations_research::math_opt::Model::has_sos2_constraint ( Sos2ConstraintId id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 1323 of file model.h.

◆ has_variable() [1/2]

bool operations_research::math_opt::Model::has_variable ( int64_t id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 986 of file model.h.

◆ has_variable() [2/2]

bool operations_research::math_opt::Model::has_variable ( VariableId id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 990 of file model.h.

◆ indicator_constraint() [1/2]

IndicatorConstraint operations_research::math_opt::Model::indicator_constraint ( IndicatorConstraintId id) const
inline

Will CHECK if has_indicator_constraint(id) is false.

Definition at line 1372 of file model.h.

◆ indicator_constraint() [2/2]

IndicatorConstraint operations_research::math_opt::Model::indicator_constraint ( int64_t id) const
inline

Will CHECK if has_indicator_constraint(id) is false.

Definition at line 1368 of file model.h.

◆ IndicatorConstraints()

std::vector< IndicatorConstraint > operations_research::math_opt::Model::IndicatorConstraints ( ) const
inline

Returns all the existing (created and not deleted) indicator constraints in the model in an arbitrary order.

Definition at line 1379 of file model.h.

◆ is_coefficient_nonzero()

bool operations_research::math_opt::Model::is_coefficient_nonzero ( LinearConstraint constraint,
Variable variable ) const
inline

Definition at line 1166 of file model.h.

◆ is_integer()

bool operations_research::math_opt::Model::is_integer ( Variable variable) const
inline

Returns the integrality of a variable.

Definition at line 1041 of file model.h.

◆ is_maximize()

bool operations_research::math_opt::Model::is_maximize ( ) const
inline

Definition at line 1504 of file model.h.

◆ is_objective_coefficient_nonzero() [1/2]

bool operations_research::math_opt::Model::is_objective_coefficient_nonzero ( Variable first_variable,
Variable second_variable ) const
inline

Definition at line 1487 of file model.h.

◆ is_objective_coefficient_nonzero() [2/2]

bool operations_research::math_opt::Model::is_objective_coefficient_nonzero ( Variable variable) const
inline

Definition at line 1481 of file model.h.

◆ linear_constraint() [1/2]

LinearConstraint operations_research::math_opt::Model::linear_constraint ( int64_t id) const
inline

Will CHECK if has_linear_constraint(id) is false.

Definition at line 1095 of file model.h.

◆ linear_constraint() [2/2]

LinearConstraint operations_research::math_opt::Model::linear_constraint ( LinearConstraintId id) const
inline

Will CHECK if has_linear_constraint(id) is false.

Definition at line 1099 of file model.h.

◆ LinearConstraints()

std::vector< LinearConstraint > operations_research::math_opt::Model::LinearConstraints ( ) const

Returns all the existing (created and not deleted) linear constraints in the model in an arbitrary order.

Definition at line 125 of file model.cc.

◆ lower_bound() [1/2]

double operations_research::math_opt::Model::lower_bound ( LinearConstraint constraint) const
inline

Returns a linear constraint lower bound.

Definition at line 1117 of file model.h.

◆ lower_bound() [2/2]

double operations_research::math_opt::Model::lower_bound ( Variable variable) const
inline

Returns a variable lower bound.

Definition at line 1013 of file model.h.

◆ Maximize() [1/6]

void operations_research::math_opt::Model::Maximize ( const LinearExpression & objective)
inline

Sets the objective to maximize the provided expression.

Definition at line 1398 of file model.h.

◆ Maximize() [2/6]

void operations_research::math_opt::Model::Maximize ( const QuadraticExpression & objective)
inline

Sets the objective to maximize the provided expression.

Definition at line 1401 of file model.h.

◆ Maximize() [3/6]

void operations_research::math_opt::Model::Maximize ( double objective)
inline

Sets the objective to maximize the provided expression.

----------------------------— Objective --------------------------------—

Objective methods

Definition at line 1389 of file model.h.

◆ Maximize() [4/6]

void operations_research::math_opt::Model::Maximize ( LinearTerm objective)
inline

Sets the objective to maximize the provided expression.

Definition at line 1395 of file model.h.

◆ Maximize() [5/6]

void operations_research::math_opt::Model::Maximize ( Objective objective,
const LinearExpression & expression )
inline

Sets objective to be maximizing expression.

-------------------------— Multi-objective -----------------------------—

Multi-objective methods

This is an API for setting objective properties (for either primary or auxiliary objectives). Only linear objectives are supported through this API. To query objective properties, use the methods on Objective.

Definition at line 1584 of file model.h.

◆ Maximize() [6/6]

void operations_research::math_opt::Model::Maximize ( Variable objective)
inline

Sets the objective to maximize the provided expression.

Definition at line 1392 of file model.h.

◆ Minimize() [1/6]

void operations_research::math_opt::Model::Minimize ( const LinearExpression & objective)
inline

Sets the objective to minimize the provided expression.

Definition at line 1414 of file model.h.

◆ Minimize() [2/6]

void operations_research::math_opt::Model::Minimize ( const QuadraticExpression & objective)
inline

Sets the objective to minimize the provided expression.

Definition at line 1417 of file model.h.

◆ Minimize() [3/6]

void operations_research::math_opt::Model::Minimize ( double objective)
inline

Sets the objective to minimize the provided expression.

Definition at line 1405 of file model.h.

◆ Minimize() [4/6]

void operations_research::math_opt::Model::Minimize ( LinearTerm objective)
inline

Sets the objective to minimize the provided expression.

Definition at line 1411 of file model.h.

◆ Minimize() [5/6]

void operations_research::math_opt::Model::Minimize ( Objective objective,
const LinearExpression & expression )
inline

Sets objective to be minimizing expression.

Definition at line 1589 of file model.h.

◆ Minimize() [6/6]

void operations_research::math_opt::Model::Minimize ( Variable objective)
inline

Sets the objective to minimize the provided expression.

Definition at line 1408 of file model.h.

◆ name() [1/3]

const std::string & operations_research::math_opt::Model::name ( ) const
inline

----------------------------— Variables --------------------------------—

Inline function implementations

Definition at line 947 of file model.h.

◆ name() [2/3]

const std::string & operations_research::math_opt::Model::name ( LinearConstraint constraint) const
inline

Returns the linear constraint name.

Definition at line 1105 of file model.h.

◆ name() [3/3]

const std::string & operations_research::math_opt::Model::name ( Variable variable) const
inline

Returns the variable name.

Definition at line 1003 of file model.h.

◆ NewUpdateTracker()

std::unique_ptr< UpdateTracker > operations_research::math_opt::Model::NewUpdateTracker ( )

Returns a tracker that can be used to generate a ModelUpdateProto with the updates that happened since the last checkpoint. The tracker initial checkpoint corresponds to the current state of the model.

The returned UpdateTracker keeps a reference to this model. See the implications in the documentation of the UpdateTracker class.

Thread-safety: this method must not be used while modifying the model (variables, constraints, ...). The user is expected to use proper synchronization primitive to serialize changes to the model and the use of this method.

Definition at line 279 of file model.cc.

◆ next_auxiliary_objective_id()

int64_t operations_research::math_opt::Model::next_auxiliary_objective_id ( ) const
inline

The returned id of the next call to AddAuxiliaryObjectve.

Definition at line 1560 of file model.h.

◆ next_indicator_constraint_id()

int64_t operations_research::math_opt::Model::next_indicator_constraint_id ( ) const
inline

The returned id of the next call to AddIndicatorConstraint.

Definition at line 1356 of file model.h.

◆ next_linear_constraint_id()

int64_t operations_research::math_opt::Model::next_linear_constraint_id ( ) const
inline

The returned id of the next call to AddLinearConstraint.

Equal to the number of linear constraints created.

Definition at line 1083 of file model.h.

◆ next_quadratic_constraint_id()

int64_t operations_research::math_opt::Model::next_quadratic_constraint_id ( ) const
inline

The returned id of the next call to AddQuadraticConstraint.

Definition at line 1185 of file model.h.

◆ next_second_order_cone_constraint_id()

int64_t operations_research::math_opt::Model::next_second_order_cone_constraint_id ( ) const
inline

The returned id of the next call to AddSecondOrderConeConstraint.

Definition at line 1228 of file model.h.

◆ next_sos1_constraint_id()

int64_t operations_research::math_opt::Model::next_sos1_constraint_id ( ) const
inline

The returned id of the next call to AddSos1Constraint.

Definition at line 1274 of file model.h.

◆ next_sos2_constraint_id()

int64_t operations_research::math_opt::Model::next_sos2_constraint_id ( ) const
inline

The returned id of the next call to AddSos2Constraint.

Definition at line 1315 of file model.h.

◆ next_variable_id()

int64_t operations_research::math_opt::Model::next_variable_id ( ) const
inline

The returned id of the next call to AddVariable.

Equal to the number of variables created.

Definition at line 982 of file model.h.

◆ num_auxiliary_objectives()

int64_t operations_research::math_opt::Model::num_auxiliary_objectives ( ) const
inline

The number of auxiliary objectives in the model.

Equal to the number of auxiliary objectives created minus the number of auxiliary objectives deleted.

Definition at line 1556 of file model.h.

◆ num_indicator_constraints()

int64_t operations_research::math_opt::Model::num_indicator_constraints ( ) const
inline

The number of indicator constraints in the model.

Equal to the number of indicator constraints created minus the number of indicator constraints deleted.

Definition at line 1352 of file model.h.

◆ num_linear_constraints()

int operations_research::math_opt::Model::num_linear_constraints ( ) const
inline

The number of linear constraints in the model.

Equal to the number of linear constraints created minus the number of linear constraints deleted.

Definition at line 1079 of file model.h.

◆ num_quadratic_constraints()

int64_t operations_research::math_opt::Model::num_quadratic_constraints ( ) const
inline

The number of quadratic constraints in the model.

Equal to the number of quadratic constraints created minus the number of quadratic constraints deleted.

Definition at line 1181 of file model.h.

◆ num_second_order_cone_constraints()

int64_t operations_research::math_opt::Model::num_second_order_cone_constraints ( ) const
inline

The number of second-order cone constraints in the model.

Equal to the number of second-order cone constraints created minus the number of second-order cone constraints deleted.

Definition at line 1224 of file model.h.

◆ num_sos1_constraints()

int64_t operations_research::math_opt::Model::num_sos1_constraints ( ) const
inline

The number of SOS1 constraints in the model.

Equal to the number of SOS1 constraints created minus the number of SOS1 constraints deleted.

Definition at line 1270 of file model.h.

◆ num_sos2_constraints()

int64_t operations_research::math_opt::Model::num_sos2_constraints ( ) const
inline

The number of SOS2 constraints in the model.

Equal to the number of SOS2 constraints created minus the number of SOS2 constraints deleted.

Definition at line 1311 of file model.h.

◆ num_variables()

int operations_research::math_opt::Model::num_variables ( ) const
inline

The number of variables in the model.

Equal to the number of variables created minus the number of variables deleted.

Definition at line 980 of file model.h.

◆ objective_coefficient() [1/2]

double operations_research::math_opt::Model::objective_coefficient ( Variable first_variable,
Variable second_variable ) const
inline

Returns 0.0 if this variable pair has no quadratic objective coefficient. The order of the variables does not matter.

Definition at line 1451 of file model.h.

◆ objective_coefficient() [2/2]

double operations_research::math_opt::Model::objective_coefficient ( Variable variable) const
inline

Returns 0.0 if this variable has no linear objective coefficient.

Definition at line 1445 of file model.h.

◆ objective_offset()

double operations_research::math_opt::Model::objective_offset ( ) const
inline

Definition at line 1496 of file model.h.

◆ ObjectiveAsLinearExpression()

LinearExpression operations_research::math_opt::Model::ObjectiveAsLinearExpression ( ) const
Note
This will CHECK fail if the objective has quadratic terms.

Definition at line 207 of file model.cc.

◆ ObjectiveAsQuadraticExpression()

QuadraticExpression operations_research::math_opt::Model::ObjectiveAsQuadraticExpression ( ) const

Definition at line 219 of file model.cc.

◆ operator=()

Model & operations_research::math_opt::Model::operator= ( const Model & )
delete

◆ primary_objective()

Objective operations_research::math_opt::Model::primary_objective ( ) const
inline

Returns an object referring to the primary objective in the model. Can be used with the multi-objective API in the same way that an auxiliary objective can be.

Definition at line 1441 of file model.h.

◆ quadratic_constraint() [1/2]

QuadraticConstraint operations_research::math_opt::Model::quadratic_constraint ( int64_t id) const
inline

Will CHECK if has_quadratic_constraint(id) is false.

Definition at line 1197 of file model.h.

◆ quadratic_constraint() [2/2]

QuadraticConstraint operations_research::math_opt::Model::quadratic_constraint ( QuadraticConstraintId id) const
inline

Will CHECK if has_quadratic_constraint(id) is false.

Definition at line 1201 of file model.h.

◆ QuadraticConstraints()

std::vector< QuadraticConstraint > operations_research::math_opt::Model::QuadraticConstraints ( ) const
inline

Returns all the existing (created and not deleted) quadratic constraints in the model in an arbitrary order.

Definition at line 1208 of file model.h.

◆ RowNonzeros()

std::vector< Variable > operations_research::math_opt::Model::RowNonzeros ( LinearConstraint constraint) const

Definition at line 114 of file model.cc.

◆ second_order_cone_constraint() [1/2]

SecondOrderConeConstraint operations_research::math_opt::Model::second_order_cone_constraint ( int64_t id) const
inline

Will CHECK if has_second_order_cone_constraint(id) is false.

Definition at line 1241 of file model.h.

◆ second_order_cone_constraint() [2/2]

SecondOrderConeConstraint operations_research::math_opt::Model::second_order_cone_constraint ( SecondOrderConeConstraintId id) const
inline

Will CHECK if has_second_order_cone_constraint(id) is false.

Definition at line 1246 of file model.h.

◆ SecondOrderConeConstraints()

std::vector< SecondOrderConeConstraint > operations_research::math_opt::Model::SecondOrderConeConstraints ( ) const
inline

Returns all the existing (created and not deleted) second-order cone constraints in the model in an arbitrary order.

Definition at line 1253 of file model.h.

◆ set_coefficient()

void operations_research::math_opt::Model::set_coefficient ( LinearConstraint constraint,
Variable variable,
double value )
inline

Setting a value to 0.0 will delete the {constraint, variable} pair from the underlying sparse matrix representation (and has no effect if the pair is not present).

Definition at line 1150 of file model.h.

◆ set_continuous()

void operations_research::math_opt::Model::set_continuous ( Variable variable)
inline

Makes the input variable continuous.

Definition at line 1037 of file model.h.

◆ set_integer()

void operations_research::math_opt::Model::set_integer ( Variable variable)
inline

Makes the input variable integer.

Definition at line 1033 of file model.h.

◆ set_is_integer()

void operations_research::math_opt::Model::set_is_integer ( Variable variable,
bool is_integer )
inline

Sets the integrality of a variable.

Definition at line 1028 of file model.h.

◆ set_is_maximize() [1/2]

void operations_research::math_opt::Model::set_is_maximize ( bool is_maximize)
inline

Prefer set_maximize() and set_minimize() above for more readable code.

Definition at line 1512 of file model.h.

◆ set_is_maximize() [2/2]

void operations_research::math_opt::Model::set_is_maximize ( Objective objective,
bool is_maximize )
inline

Prefer set_maximize() and set_minimize() above for more readable code.

Definition at line 1623 of file model.h.

◆ set_lower_bound() [1/2]

void operations_research::math_opt::Model::set_lower_bound ( LinearConstraint constraint,
double lower_bound )
inline

Sets a linear constraint lower bound.

Definition at line 1110 of file model.h.

◆ set_lower_bound() [2/2]

void operations_research::math_opt::Model::set_lower_bound ( Variable variable,
double lower_bound )
inline

Sets a variable lower bound.

Definition at line 1008 of file model.h.

◆ set_maximize() [1/2]

void operations_research::math_opt::Model::set_maximize ( )
inline

Definition at line 1508 of file model.h.

◆ set_maximize() [2/2]

void operations_research::math_opt::Model::set_maximize ( Objective objective)
inline

Definition at line 1615 of file model.h.

◆ set_minimize() [1/2]

void operations_research::math_opt::Model::set_minimize ( )
inline

Definition at line 1510 of file model.h.

◆ set_minimize() [2/2]

void operations_research::math_opt::Model::set_minimize ( Objective objective)
inline

Definition at line 1619 of file model.h.

◆ set_objective_coefficient() [1/3]

void operations_research::math_opt::Model::set_objective_coefficient ( Objective objective,
Variable variable,
double value )
inline

Setting a value to 0.0 will delete the variable from the underlying sparse representation (and has no effect if the variable is not present).

Definition at line 1600 of file model.h.

◆ set_objective_coefficient() [2/3]

void operations_research::math_opt::Model::set_objective_coefficient ( Variable first_variable,
Variable second_variable,
double value )
inline

Set quadratic objective terms for the product of two variables. Setting a value to 0.0 will delete the variable pair from the underlying sparse representation (and has no effect if the pair is not present). The order of the variables does not matter.

Definition at line 1467 of file model.h.

◆ set_objective_coefficient() [3/3]

void operations_research::math_opt::Model::set_objective_coefficient ( Variable variable,
double value )
inline

Setting a value to 0.0 will delete the variable from the underlying sparse representation (and has no effect if the variable is not present).

Definition at line 1460 of file model.h.

◆ set_objective_offset() [1/2]

void operations_research::math_opt::Model::set_objective_offset ( double value)
inline

Definition at line 1500 of file model.h.

◆ set_objective_offset() [2/2]

void operations_research::math_opt::Model::set_objective_offset ( Objective objective,
double value )
inline

Definition at line 1609 of file model.h.

◆ set_objective_priority()

void operations_research::math_opt::Model::set_objective_priority ( Objective objective,
int64_t priority )
inline

Sets the priority for an objective (lower is more important). priority must be nonnegative.

Definition at line 1594 of file model.h.

◆ set_upper_bound() [1/2]

void operations_research::math_opt::Model::set_upper_bound ( LinearConstraint constraint,
double upper_bound )
inline

Sets a linear constraint upper bound.

Definition at line 1122 of file model.h.

◆ set_upper_bound() [2/2]

void operations_research::math_opt::Model::set_upper_bound ( Variable variable,
double upper_bound )
inline

Sets a variable upper bound.

Definition at line 1018 of file model.h.

◆ SetObjective() [1/6]

void operations_research::math_opt::Model::SetObjective ( const LinearExpression & objective,
bool is_maximize )

Sets the objective to optimize the provided expression.

Definition at line 143 of file model.cc.

◆ SetObjective() [2/6]

void operations_research::math_opt::Model::SetObjective ( const QuadraticExpression & objective,
bool is_maximize )

Sets the objective to optimize the provided expression.

Definition at line 155 of file model.cc.

◆ SetObjective() [3/6]

void operations_research::math_opt::Model::SetObjective ( double objective,
bool is_maximize )
inline

Sets the objective to optimize the provided expression.

Definition at line 1421 of file model.h.

◆ SetObjective() [4/6]

void operations_research::math_opt::Model::SetObjective ( LinearTerm objective,
bool is_maximize )
inline

Sets the objective to optimize the provided expression.

Definition at line 1427 of file model.h.

◆ SetObjective() [5/6]

void operations_research::math_opt::Model::SetObjective ( Objective objective,
const LinearExpression & expression,
bool is_maximize )

Sets the objective to optimize the provided expression.

Definition at line 251 of file model.cc.

◆ SetObjective() [6/6]

void operations_research::math_opt::Model::SetObjective ( Variable objective,
bool is_maximize )
inline

Sets the objective to optimize the provided expression.

Definition at line 1424 of file model.h.

◆ SortedAuxiliaryObjectives()

std::vector< Objective > operations_research::math_opt::Model::SortedAuxiliaryObjectives ( ) const

Returns all the existing (created and not deleted) auxiliary objectives in the model sorted by id.

Definition at line 242 of file model.cc.

◆ SortedIndicatorConstraints()

std::vector< IndicatorConstraint > operations_research::math_opt::Model::SortedIndicatorConstraints ( ) const
inline

Returns all the existing (created and not deleted) indicator constraints in the model sorted by id.

Definition at line 1383 of file model.h.

◆ SortedLinearConstraints()

std::vector< LinearConstraint > operations_research::math_opt::Model::SortedLinearConstraints ( ) const

Returns all the existing (created and not deleted) linear constraints in the model sorted by id.

Definition at line 134 of file model.cc.

◆ SortedQuadraticConstraints()

std::vector< QuadraticConstraint > operations_research::math_opt::Model::SortedQuadraticConstraints ( ) const
inline

Returns all the existing (created and not deleted) quadratic constraints in the model sorted by id.

Definition at line 1212 of file model.h.

◆ SortedSecondOrderConeConstraints()

std::vector< SecondOrderConeConstraint > operations_research::math_opt::Model::SortedSecondOrderConeConstraints ( ) const
inline

Returns all the existing (created and not deleted) second-order cone constraints in the model sorted by id.

Definition at line 1258 of file model.h.

◆ SortedSos1Constraints()

std::vector< Sos1Constraint > operations_research::math_opt::Model::SortedSos1Constraints ( ) const
inline

Returns all the existing (created and not deleted) SOS1 constraints in the model sorted by id.

Definition at line 1300 of file model.h.

◆ SortedSos2Constraints()

std::vector< Sos2Constraint > operations_research::math_opt::Model::SortedSos2Constraints ( ) const
inline

Returns all the existing (created and not deleted) SOS2 constraints in the model sorted by id.

Definition at line 1341 of file model.h.

◆ SortedVariables()

std::vector< Variable > operations_research::math_opt::Model::SortedVariables ( ) const

Returns all the existing (created and not deleted) variables in the model, sorted by id.

Definition at line 94 of file model.cc.

◆ sos1_constraint() [1/2]

Sos1Constraint operations_research::math_opt::Model::sos1_constraint ( int64_t id) const
inline

Will CHECK if has_sos1_constraint(id) is false.

Definition at line 1286 of file model.h.

◆ sos1_constraint() [2/2]

Sos1Constraint operations_research::math_opt::Model::sos1_constraint ( Sos1ConstraintId id) const
inline

Will CHECK if has_sos1_constraint(id) is false.

Definition at line 1290 of file model.h.

◆ Sos1Constraints()

std::vector< Sos1Constraint > operations_research::math_opt::Model::Sos1Constraints ( ) const
inline

Returns all the existing (created and not deleted) SOS1 constraints in the model in an arbitrary order.

Definition at line 1296 of file model.h.

◆ sos2_constraint() [1/2]

Sos2Constraint operations_research::math_opt::Model::sos2_constraint ( int64_t id) const
inline

Will CHECK if has_sos2_constraint(id) is false.

Definition at line 1327 of file model.h.

◆ sos2_constraint() [2/2]

Sos2Constraint operations_research::math_opt::Model::sos2_constraint ( Sos2ConstraintId id) const
inline

Will CHECK if has_sos2_constraint(id) is false.

Definition at line 1331 of file model.h.

◆ Sos2Constraints()

std::vector< Sos2Constraint > operations_research::math_opt::Model::Sos2Constraints ( ) const
inline

Returns all the existing (created and not deleted) SOS2 constraints in the model in an arbitrary order.

Definition at line 1337 of file model.h.

◆ storage() [1/2]

ModelStorage * operations_research::math_opt::Model::storage ( )
inline

Returns a pointer to the underlying model storage.

This API is for internal use only and regular users should have no need for it.

Definition at line 911 of file model.h.

◆ storage() [2/2]

const ModelStorage * operations_research::math_opt::Model::storage ( ) const
inline
Todo
(user): expose a way to efficiently iterate through the nonzeros of the linear constraint matrix.

Returns a pointer to the underlying model storage.

This API is for internal use only and regular users should have no need for it.

Definition at line 905 of file model.h.

◆ upper_bound() [1/2]

double operations_research::math_opt::Model::upper_bound ( LinearConstraint constraint) const
inline

Returns a linear constraint upper bound.

Definition at line 1129 of file model.h.

◆ upper_bound() [2/2]

double operations_research::math_opt::Model::upper_bound ( Variable variable) const
inline

Returns a variable upper bound.

Definition at line 1023 of file model.h.

◆ ValidateExistingLinearConstraintOfThisModel()

absl::Status operations_research::math_opt::Model::ValidateExistingLinearConstraintOfThisModel ( LinearConstraint linear_constraint) const
inline

Returns an error if linear_constraint is from another model or the id is not in this model (typically, if it was deleted).

Todo
(b/239810718): use << for LinearConstraint once it does not CHECK.

Definition at line 1134 of file model.h.

◆ ValidateExistingVariableOfThisModel()

absl::Status operations_research::math_opt::Model::ValidateExistingVariableOfThisModel ( Variable variable) const
inline

-----------------------— Linear constraints ----------------------------—

Returns an error if variable is from another model or the id is not in this model (typically, if it was deleted).

Todo
(b/239810718): use << for Variable once it does not CHECK.

Definition at line 1048 of file model.h.

◆ variable() [1/2]

Variable operations_research::math_opt::Model::variable ( int64_t id) const
inline

Will CHECK if has_variable(id) is false.

Definition at line 994 of file model.h.

◆ variable() [2/2]

Variable operations_research::math_opt::Model::variable ( VariableId id) const
inline

Will CHECK if has_variable(id) is false.

Definition at line 998 of file model.h.

◆ Variables()

std::vector< Variable > operations_research::math_opt::Model::Variables ( ) const

Returns all the existing (created and not deleted) variables in the model in an arbitrary order.

Definition at line 85 of file model.cc.

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & ostr,
const Model & model )
friend

Prints the objective, the constraints and the variables of the model over several lines in a human-readable way. Includes a new line at the end of the model.

Definition at line 287 of file model.cc.


The documentation for this class was generated from the following files: