Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ortools.math_opt.python.model_storage.ModelStorage Class Reference
Inheritance diagram for ortools.math_opt.python.model_storage.ModelStorage:
ortools.math_opt.python.hash_model_storage.HashModelStorage

Public Member Functions

str name (self)
 
int add_variable (self, float lb, float ub, bool is_integer, str name)
 
None delete_variable (self, int variable_id)
 
bool variable_exists (self, int variable_id)
 
int next_variable_id (self)
 
None set_variable_lb (self, int variable_id, float lb)
 
None set_variable_ub (self, int variable_id, float ub)
 
None set_variable_is_integer (self, int variable_id, bool is_integer)
 
float get_variable_lb (self, int variable_id)
 
float get_variable_ub (self, int variable_id)
 
bool get_variable_is_integer (self, int variable_id)
 
str get_variable_name (self, int variable_id)
 
Iterator[int] get_variables (self)
 
int add_linear_constraint (self, float lb, float ub, str name)
 
None delete_linear_constraint (self, int linear_constraint_id)
 
bool linear_constraint_exists (self, int linear_constraint_id)
 
int next_linear_constraint_id (self)
 
None set_linear_constraint_lb (self, int linear_constraint_id, float lb)
 
None set_linear_constraint_ub (self, int linear_constraint_id, float ub)
 
float get_linear_constraint_lb (self, int linear_constraint_id)
 
float get_linear_constraint_ub (self, int linear_constraint_id)
 
str get_linear_constraint_name (self, int linear_constraint_id)
 
Iterator[int] get_linear_constraints (self)
 
None set_linear_constraint_coefficient (self, int linear_constraint_id, int variable_id, float lb)
 
float get_linear_constraint_coefficient (self, int linear_constraint_id, int variable_id)
 
Iterator[int] get_linear_constraints_with_variable (self, int variable_id)
 
Iterator[int] get_variables_for_linear_constraint (self, int linear_constraint_id)
 
Iterator[LinearConstraintMatrixIdEntryget_linear_constraint_matrix_entries (self)
 
None clear_objective (self)
 
None set_linear_objective_coefficient (self, int variable_id, float value)
 
float get_linear_objective_coefficient (self, int variable_id)
 
Iterator[LinearObjectiveEntryget_linear_objective_coefficients (self)
 
None set_quadratic_objective_coefficient (self, int first_variable_id, int second_variable_id, float value)
 
float get_quadratic_objective_coefficient (self, int first_variable_id, int second_variable_id)
 
Iterator[QuadraticEntryget_quadratic_objective_coefficients (self)
 
Iterator[int] get_quadratic_objective_adjacent_variables (self, int variable_id)
 
None set_is_maximize (self, bool is_maximize)
 
bool get_is_maximize (self)
 
None set_objective_offset (self, float offset)
 
float get_objective_offset (self)
 
model_pb2.ModelProto export_model (self)
 
StorageUpdateTracker add_update_tracker (self)
 
 remove_update_tracker (self, StorageUpdateTracker tracker)
 

Detailed Description

An interface for in memory storage of an optimization model.

Most users should not use this class directly and use Model defined in
model.py.

Stores an mixed integer programming problem of the form:

{max/min} c*x + d
s.t.  lb_c <= A * x <= ub_c
      lb_v <=     x <= ub_v
                  x_i integer for i in I

where x is a vector of n decision variables, d is a number, lb_v, ub_v, and c
are vectors of n numbers, lb_c and ub_c are vectors of m numbers, A is a
m by n matrix, and I is a subset of {1,..., n}.

Each of the n variables and m constraints have an integer id that you use to
get/set the problem data (c, A, lb_c etc.). Ids begin at zero and increase
sequentially. They are not reused after deletion. Note that if a variable is
deleted, your model has nonconsecutive variable ids.

For all methods taking an id (e.g. set_variable_lb), providing a bad id
(including the id of a deleted variable) will raise a BadVariableIdError or
BadLinearConstraintIdError. Further, the ModelStorage instance is assumed to
be in a bad state after any such error and there are no guarantees on further
interactions.

All implementations must have a constructor taking a str argument for the
model name with a default value of the empty string.

Any ModelStorage can be exported to model_pb2.ModelProto, the format consumed
by MathOpt solvers. Changes to a model can be exported to a
model_update_pb2.ModelUpdateProto with an UpdateTracker, see the UpdateTracker
documentation for details.

When solving this optimization problem we will additionally require that:
  * No numbers are NaN,
  * c, d, and A are all finite,
  * lb_c and lb_v are not +inf,
  * ub_c and ub_v are not -inf,
but those assumptions are not checked or enforced here (NaNs and infinite
values can be used anywhere).

Definition at line 143 of file model_storage.py.

Member Function Documentation

◆ add_linear_constraint()

int ortools.math_opt.python.model_storage.ModelStorage.add_linear_constraint ( self,
float lb,
float ub,
str name )

◆ add_update_tracker()

StorageUpdateTracker ortools.math_opt.python.model_storage.ModelStorage.add_update_tracker ( self)
Creates a StorageUpdateTracker registered with self to view model changes.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 417 of file model_storage.py.

◆ add_variable()

int ortools.math_opt.python.model_storage.ModelStorage.add_variable ( self,
float lb,
float ub,
bool is_integer,
str name )

◆ clear_objective()

None ortools.math_opt.python.model_storage.ModelStorage.clear_objective ( self)
Clears objective coefficients and offset. Does not change direction.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 315 of file model_storage.py.

◆ delete_linear_constraint()

None ortools.math_opt.python.model_storage.ModelStorage.delete_linear_constraint ( self,
int linear_constraint_id )

◆ delete_variable()

None ortools.math_opt.python.model_storage.ModelStorage.delete_variable ( self,
int variable_id )

◆ export_model()

model_pb2.ModelProto ortools.math_opt.python.model_storage.ModelStorage.export_model ( self)

◆ get_is_maximize()

bool ortools.math_opt.python.model_storage.ModelStorage.get_is_maximize ( self)

◆ get_linear_constraint_coefficient()

float ortools.math_opt.python.model_storage.ModelStorage.get_linear_constraint_coefficient ( self,
int linear_constraint_id,
int variable_id )

◆ get_linear_constraint_lb()

float ortools.math_opt.python.model_storage.ModelStorage.get_linear_constraint_lb ( self,
int linear_constraint_id )

◆ get_linear_constraint_matrix_entries()

Iterator[LinearConstraintMatrixIdEntry] ortools.math_opt.python.model_storage.ModelStorage.get_linear_constraint_matrix_entries ( self)
Yields the nonzero elements of the linear constraint matrix in undefined order.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 308 of file model_storage.py.

◆ get_linear_constraint_name()

str ortools.math_opt.python.model_storage.ModelStorage.get_linear_constraint_name ( self,
int linear_constraint_id )

◆ get_linear_constraint_ub()

float ortools.math_opt.python.model_storage.ModelStorage.get_linear_constraint_ub ( self,
int linear_constraint_id )

◆ get_linear_constraints()

Iterator[int] ortools.math_opt.python.model_storage.ModelStorage.get_linear_constraints ( self)
Yields the linear constraint ids in order of creation.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 279 of file model_storage.py.

◆ get_linear_constraints_with_variable()

Iterator[int] ortools.math_opt.python.model_storage.ModelStorage.get_linear_constraints_with_variable ( self,
int variable_id )
Yields the linear constraints with nonzero coefficient for a variable in undefined order.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 296 of file model_storage.py.

◆ get_linear_objective_coefficient()

float ortools.math_opt.python.model_storage.ModelStorage.get_linear_objective_coefficient ( self,
int variable_id )

◆ get_linear_objective_coefficients()

Iterator[LinearObjectiveEntry] ortools.math_opt.python.model_storage.ModelStorage.get_linear_objective_coefficients ( self)
Yields the nonzero linear objective terms in undefined order.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 327 of file model_storage.py.

◆ get_objective_offset()

float ortools.math_opt.python.model_storage.ModelStorage.get_objective_offset ( self)

◆ get_quadratic_objective_adjacent_variables()

Iterator[int] ortools.math_opt.python.model_storage.ModelStorage.get_quadratic_objective_adjacent_variables ( self,
int variable_id )
Yields the variables multiplying a variable in the objective function.

Variables are returned in an unspecified order.

For example, if variables x and y have ids 0 and 1 respectively, and the
quadratic portion of the objective is x^2 + 2 x*y, then
get_quadratic_objective_adjacent_variables(0) = (0, 1).

Args:
  variable_id: Function yields the variables multiplying variable_id in the
    objective function.

Yields:
  The variables multiplying variable_id in the objective function.

Raises:
  BadVariableIdError if variable_id is not in the model.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 374 of file model_storage.py.

◆ get_quadratic_objective_coefficient()

float ortools.math_opt.python.model_storage.ModelStorage.get_quadratic_objective_coefficient ( self,
int first_variable_id,
int second_variable_id )
Gets the objective coefficient for the product of two variables.

The ordering of the input variables does not matter.

Args:
  first_variable_id: The first variable in the product.
  second_variable_id: The second variable in the product.

Raises:
  BadVariableIdError if first_variable_id or second_variable_id are not in
  the model.

Returns:
  The value of the coefficient.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 350 of file model_storage.py.

◆ get_quadratic_objective_coefficients()

Iterator[QuadraticEntry] ortools.math_opt.python.model_storage.ModelStorage.get_quadratic_objective_coefficients ( self)
Yields the nonzero quadratic objective terms in undefined order.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 370 of file model_storage.py.

◆ get_variable_is_integer()

bool ortools.math_opt.python.model_storage.ModelStorage.get_variable_is_integer ( self,
int variable_id )

◆ get_variable_lb()

float ortools.math_opt.python.model_storage.ModelStorage.get_variable_lb ( self,
int variable_id )

◆ get_variable_name()

str ortools.math_opt.python.model_storage.ModelStorage.get_variable_name ( self,
int variable_id )

◆ get_variable_ub()

float ortools.math_opt.python.model_storage.ModelStorage.get_variable_ub ( self,
int variable_id )

◆ get_variables()

Iterator[int] ortools.math_opt.python.model_storage.ModelStorage.get_variables ( self)
Yields the variable ids in order of creation.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 238 of file model_storage.py.

◆ get_variables_for_linear_constraint()

Iterator[int] ortools.math_opt.python.model_storage.ModelStorage.get_variables_for_linear_constraint ( self,
int linear_constraint_id )
Yields the variables with nonzero coefficient in a linear constraint in undefined order.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 301 of file model_storage.py.

◆ linear_constraint_exists()

bool ortools.math_opt.python.model_storage.ModelStorage.linear_constraint_exists ( self,
int linear_constraint_id )

◆ name()

str ortools.math_opt.python.model_storage.ModelStorage.name ( self)

◆ next_linear_constraint_id()

int ortools.math_opt.python.model_storage.ModelStorage.next_linear_constraint_id ( self)

◆ next_variable_id()

int ortools.math_opt.python.model_storage.ModelStorage.next_variable_id ( self)

◆ remove_update_tracker()

ortools.math_opt.python.model_storage.ModelStorage.remove_update_tracker ( self,
StorageUpdateTracker tracker )
Stops tracker from getting updates on model changes in self.

An error will be raised if tracker is not a StorageUpdateTracker created by
this Model that has not previously been removed.

Using an UpdateTracker (via checkpoint or export_update) after it has been
removed will result in an error.

Args:
  tracker: The StorageUpdateTracker to unregister.

Raises:
  KeyError: The tracker was created by another model or was already removed.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 422 of file model_storage.py.

◆ set_is_maximize()

None ortools.math_opt.python.model_storage.ModelStorage.set_is_maximize ( self,
bool is_maximize )

◆ set_linear_constraint_coefficient()

None ortools.math_opt.python.model_storage.ModelStorage.set_linear_constraint_coefficient ( self,
int linear_constraint_id,
int variable_id,
float lb )

◆ set_linear_constraint_lb()

None ortools.math_opt.python.model_storage.ModelStorage.set_linear_constraint_lb ( self,
int linear_constraint_id,
float lb )

◆ set_linear_constraint_ub()

None ortools.math_opt.python.model_storage.ModelStorage.set_linear_constraint_ub ( self,
int linear_constraint_id,
float ub )

◆ set_linear_objective_coefficient()

None ortools.math_opt.python.model_storage.ModelStorage.set_linear_objective_coefficient ( self,
int variable_id,
float value )

◆ set_objective_offset()

None ortools.math_opt.python.model_storage.ModelStorage.set_objective_offset ( self,
float offset )

◆ set_quadratic_objective_coefficient()

None ortools.math_opt.python.model_storage.ModelStorage.set_quadratic_objective_coefficient ( self,
int first_variable_id,
int second_variable_id,
float value )
Sets the objective coefficient for the product of two variables.

The ordering of the input variables does not matter.

Args:
  first_variable_id: The first variable in the product.
  second_variable_id: The second variable in the product.
  value: The value of the coefficient.

Raises:
  BadVariableIdError if first_variable_id or second_variable_id are not in
  the model.

Reimplemented in ortools.math_opt.python.hash_model_storage.HashModelStorage.

Definition at line 332 of file model_storage.py.

◆ set_variable_is_integer()

None ortools.math_opt.python.model_storage.ModelStorage.set_variable_is_integer ( self,
int variable_id,
bool is_integer )

◆ set_variable_lb()

None ortools.math_opt.python.model_storage.ModelStorage.set_variable_lb ( self,
int variable_id,
float lb )

◆ set_variable_ub()

None ortools.math_opt.python.model_storage.ModelStorage.set_variable_ub ( self,
int variable_id,
float ub )

◆ variable_exists()

bool ortools.math_opt.python.model_storage.ModelStorage.variable_exists ( self,
int variable_id )

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