Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ortools.math_opt.python.model Namespace Reference

Classes

class  _ProcessedElements
 
class  _QuadraticProcessedElements
 
class  _ToProcessElements
 
class  _ToProcessElementsImplementation
 
class  BoundedLinearExpression
 
class  LinearBase
 
class  LinearConstraint
 
class  LinearConstraintMatrixEntry
 
class  LinearExpression
 
class  LinearLinearProduct
 
class  LinearProduct
 
class  LinearSum
 
class  LinearTerm
 
class  LowerBoundedLinearExpression
 
class  Model
 
class  NormalizedLinearInequality
 
class  Objective
 
class  QuadraticBase
 
class  QuadraticExpression
 
class  QuadraticProduct
 
class  QuadraticSum
 
class  QuadraticTerm
 
class  QuadraticTermKey
 
class  UpdateTracker
 
class  UpperBoundedLinearExpression
 
class  VarEqVar
 
class  Variable
 

Functions

NoReturn _raise_binary_operator_type_error (str operator, Type[Any] lhs, Type[Any] rhs, Optional[str] extra_message=None)
 
NoReturn _raise_ne_not_supported ()
 
LinearExpression as_flat_linear_expression (LinearTypes value)
 
QuadraticExpression as_flat_quadratic_expression (QuadraticTypes value)
 
NormalizedLinearInequality as_normalized_linear_inequality (Optional[Union[bool, BoundedLinearTypes]] bounded_expr=None, *, Optional[float] lb=None, Optional[float] ub=None, Optional[LinearTypes] expr=None)
 

Variables

 Storage = model_storage.ModelStorage
 
 StorageClass = model_storage.ModelStorageImplClass
 
 LinearTypes = Union[int, float, "LinearBase"]
 
 QuadraticTypes = Union[int, float, "LinearBase", "QuadraticBase"]
 
 LinearTypesExceptVariable
 
tuple _CHAINED_COMPARISON_MESSAGE
 
tuple _EXPRESSION_COMP_EXPRESSION_MESSAGE
 
tuple BoundedLinearTypesList
 
 BoundedLinearTypes = Union[BoundedLinearTypesList]
 
 _T = TypeVar("_T", "LinearBase", Union["LinearBase", "QuadraticBase"])
 
 _LinearToProcessElements = _ToProcessElementsImplementation["LinearBase"]
 
 _QuadraticToProcessElements
 

Detailed Description

A solver independent library for modeling optimization problems.

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

  model = mathopt.Model(name='my_model')
  x = model.add_binary_variable(name='x')
  y = model.add_variable(lb=0.0, ub=2.5, name='y')
  # We can directly use linear combinations of variables ...
  model.add_linear_constraint(x + y <= 1.5, name='c')
  # ... or build them incrementally.
  objective_expression = 0
  objective_expression += 2 * x
  objective_expression += y
  model.maximize(objective_expression)

  # May raise a RuntimeError on invalid input or internal solver errors.
  result = mathopt.solve(model, mathopt.SolverType.GSCIP)

  if result.termination.reason not in (mathopt.TerminationReason.OPTIMAL,
                                       mathopt.TerminationReason.FEASIBLE):
    raise RuntimeError(f'model failed to solve: {result.termination}')

  print(f'Objective value: {result.objective_value()}')
  print(f'Value for variable x: {result.variable_values()[x]}')

Function Documentation

◆ _raise_binary_operator_type_error()

NoReturn ortools.math_opt.python.model._raise_binary_operator_type_error ( str operator,
Type[Any] lhs,
Type[Any] rhs,
Optional[str] extra_message = None )
protected
Raises TypeError on unsupported operators.

Definition at line 97 of file model.py.

◆ _raise_ne_not_supported()

NoReturn ortools.math_opt.python.model._raise_ne_not_supported ( )
protected

Definition at line 113 of file model.py.

◆ as_flat_linear_expression()

LinearExpression ortools.math_opt.python.model.as_flat_linear_expression ( LinearTypes value)
Converts floats, ints and Linear objects to a LinearExpression.

Definition at line 2145 of file model.py.

◆ as_flat_quadratic_expression()

QuadraticExpression ortools.math_opt.python.model.as_flat_quadratic_expression ( QuadraticTypes value)
Converts floats, ints, LinearBase and QuadraticBase objects to a QuadraticExpression.

Definition at line 2152 of file model.py.

◆ as_normalized_linear_inequality()

NormalizedLinearInequality ortools.math_opt.python.model.as_normalized_linear_inequality ( Optional[Union[bool, BoundedLinearTypes]] bounded_expr = None,
* ,
Optional[float] lb = None,
Optional[float] ub = None,
Optional[LinearTypes] expr = None )
Converts a linear constraint to a NormalizedLinearInequality.

The simplest way to specify the constraint is by passing a one-sided or
two-sided linear inequality as in:
  * as_normalized_linear_inequality(x + y + 1.0 <= 2.0),
  * as_normalized_linear_inequality(x + y >= 2.0), or
  * as_normalized_linear_inequality((1.0 <= x + y) <= 2.0).

Note the extra parenthesis for two-sided linear inequalities, which is
required due to some language limitations (see
https://peps.python.org/pep-0335/ and https://peps.python.org/pep-0535/).
If the parenthesis are omitted, a TypeError will be raised explaining the
issue (if this error was not raised the first inequality would have been
silently ignored because of the noted language limitations).

The second way to specify the constraint is by setting lb, ub, and/o expr as
in:
  * as_normalized_linear_inequality(expr=x + y + 1.0, ub=2.0),
  * as_normalized_linear_inequality(expr=x + y, lb=2.0),
  * as_normalized_linear_inequality(expr=x + y, lb=1.0, ub=2.0), or
  * as_normalized_linear_inequality(lb=1.0).
Omitting lb is equivalent to setting it to -math.inf and omiting ub is
equivalent to setting it to math.inf.

These two alternatives are exclusive and a combined call like:
  * as_normalized_linear_inequality(x + y <= 2.0, lb=1.0), or
  * as_normalized_linear_inequality(x + y <= 2.0, ub=math.inf)
will raise a ValueError. A ValueError is also raised if expr's offset is
infinite.

Args:
  bounded_expr: a linear inequality describing the constraint. Cannot be
    specified together with lb, ub, or expr.
  lb: The constraint's lower bound if bounded_expr is omitted (if both
    bounder_expr and lb are omitted, the lower bound is -math.inf).
  ub: The constraint's upper bound if bounded_expr is omitted (if both
    bounder_expr and ub are omitted, the upper bound is math.inf).
  expr: The constraint's linear expression if bounded_expr is omitted.

Returns:
  A NormalizedLinearInequality representing the linear constraint.

Definition at line 2188 of file model.py.

Variable Documentation

◆ _CHAINED_COMPARISON_MESSAGE

tuple ortools.math_opt.python.model._CHAINED_COMPARISON_MESSAGE
protected
Initial value:
1= (
2 "If you were trying to create a two-sided or "
3 "ranged linear inequality of the form `lb <= "
4 "expr <= ub`, try `(lb <= expr) <= ub` instead"
5)

Definition at line 84 of file model.py.

◆ _EXPRESSION_COMP_EXPRESSION_MESSAGE

tuple ortools.math_opt.python.model._EXPRESSION_COMP_EXPRESSION_MESSAGE
protected
Initial value:
1= (
2 "This error can occur when adding "
3 "inequalities of the form `(a <= b) <= "
4 "c` where (a, b, c) includes two or more"
5 " non-constant linear expressions"
6)

Definition at line 89 of file model.py.

◆ _LinearToProcessElements

ortools.math_opt.python.model._LinearToProcessElements = _ToProcessElementsImplementation["LinearBase"]
protected

Definition at line 391 of file model.py.

◆ _QuadraticToProcessElements

ortools.math_opt.python.model._QuadraticToProcessElements
protected
Initial value:
1= _ToProcessElementsImplementation[
2 Union["LinearBase", "QuadraticBase"]
3]

Definition at line 392 of file model.py.

◆ _T

ortools.math_opt.python.model._T = TypeVar("_T", "LinearBase", Union["LinearBase", "QuadraticBase"])
protected

Definition at line 370 of file model.py.

◆ BoundedLinearTypes

ortools.math_opt.python.model.BoundedLinearTypes = Union[BoundedLinearTypesList]

Definition at line 302 of file model.py.

◆ BoundedLinearTypesList

tuple ortools.math_opt.python.model.BoundedLinearTypesList
Initial value:
1= (
2 LowerBoundedLinearExpression,
3 UpperBoundedLinearExpression,
4 BoundedLinearExpression,
5 VarEqVar,
6)

Definition at line 296 of file model.py.

◆ LinearTypes

ortools.math_opt.python.model.LinearTypes = Union[int, float, "LinearBase"]

Definition at line 78 of file model.py.

◆ LinearTypesExceptVariable

ortools.math_opt.python.model.LinearTypesExceptVariable
Initial value:
1= Union[
2 float, int, "LinearTerm", "LinearExpression", "LinearSum", "LinearProduct"
3]

Definition at line 80 of file model.py.

◆ QuadraticTypes

ortools.math_opt.python.model.QuadraticTypes = Union[int, float, "LinearBase", "QuadraticBase"]

Definition at line 79 of file model.py.

◆ Storage

ortools.math_opt.python.model.Storage = model_storage.ModelStorage

Definition at line 75 of file model.py.

◆ StorageClass

ortools.math_opt.python.model.StorageClass = model_storage.ModelStorageImplClass

Definition at line 76 of file model.py.