grid.Var

class grid.Var(id, var_type, initial_value, preference=None)[source]

Bases: Expr

Decision variable of the model.

Returned by the variable-creation methods of RoutingModel and VehicleType (add_integer_var, add_float_var, add_nodes_set_var). Subclass of Expr so that variables can be combined with other expressions through operator overloading.

Instances are typically obtained via RoutingModel.add_integer_var(), RoutingModel.add_float_var(), or the equivalent methods on VehicleType, not constructed directly..

Parameters:
  • id (int) – Unique identifier of the variable within the model.

  • var_type (Literal['integer', 'float', 'boolean']) – Numeric type of the variable’s value.

  • initial_value (int, float, or bool) – Value of the variable at the start of the route.

  • preference (Literal['low', 'high', None]) – Solver hint indicating preferred direction during search.

id

Variable identifier.

Type:

int

type

Numeric type (same as var_type).

Type:

str

initial_value

initial value.

Type:

int, float, or bool

preference

Solver preference hint.

Type:

str or None

Examples

>>> import grid
>>> model = grid.RoutingModel()
>>> vehicle = model.add_vehicle_type(id=0, count=1, capacity=10)
>>> load = vehicle.add_integer_var(initial_value=10)
>>> load
Var_0
>>> load - 3
Expr(-, Var_0, Const_3, integer)
>>> load >= 0
Expr(>=, Var_0, Const_0, boolean)
__init__(id, var_type, initial_value, preference=None)[source]
Parameters:
  • id (int)

  • var_type (Literal['integer', 'float', 'boolean'])

  • preference (Literal['low', 'high', None])

Methods

__init__(id, var_type, initial_value[, ...])

same(other)

Return whether this variable refers to the same id as other.

same(other)[source]

Return whether this variable refers to the same id as other.

Distinct from __eq__, which is overloaded to build a boolean Expr for use in constraints.

Parameters:

other (Var)

Return type:

bool

Examples

>>> import grid
>>> model = grid.RoutingModel()
>>> vehicle = model.add_vehicle_type(id=0, count=1, capacity=10)
>>> load = vehicle.add_integer_var(initial_value=10)
>>> battery = vehicle.add_float_var(initial_value=100.0)
>>> load.same(load)
True
>>> load.same(battery)
False