|
| Model (absl::string_view name="") |
| Creates an empty minimization problem.
|
|
| Model (std::unique_ptr< ModelStorage > storage) |
|
| Model (const Model &)=delete |
|
Model & | operator= (const Model &)=delete |
|
std::unique_ptr< Model > | Clone (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< Variable > | Variables () const |
|
std::vector< Variable > | SortedVariables () const |
|
absl::Status | ValidateExistingVariableOfThisModel (Variable variable) const |
| -----------------------— Linear constraints ----------------------------—
|
|
std::vector< LinearConstraint > | ColumnNonzeros (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< Variable > | RowNonzeros (LinearConstraint constraint) const |
|
std::vector< LinearConstraint > | LinearConstraints () const |
|
std::vector< LinearConstraint > | SortedLinearConstraints () 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< QuadraticConstraint > | QuadraticConstraints () const |
|
std::vector< QuadraticConstraint > | SortedQuadraticConstraints () 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< SecondOrderConeConstraint > | SecondOrderConeConstraints () const |
|
std::vector< SecondOrderConeConstraint > | SortedSecondOrderConeConstraints () 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< Sos1Constraint > | Sos1Constraints () const |
|
std::vector< Sos1Constraint > | SortedSos1Constraints () 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< Sos2Constraint > | Sos2Constraints () const |
|
std::vector< Sos2Constraint > | SortedSos2Constraints () 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< IndicatorConstraint > | IndicatorConstraints () const |
|
std::vector< IndicatorConstraint > | SortedIndicatorConstraints () 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< Objective > | AuxiliaryObjectives () const |
|
std::vector< Objective > | SortedAuxiliaryObjectives () 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< UpdateTracker > | NewUpdateTracker () |
|
absl::Status | ApplyUpdateProto (const ModelUpdateProto &update_proto) |
|
const ModelStorage * | storage () const |
|
ModelStorage * | storage () |
|
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.