grid.SetVar

class grid.SetVar(id, initial_value)[source]

Bases: Expr

Set-valued decision variable, with elements from the node-id universe.

Returned by RoutingModel.add_nodes_set_var() and VehicleType.add_nodes_set_var(). Provides set-algebra methods (add(), remove(), contains(), is_empty(), len()) that build Expr instances usable in constraints and transitions.

Instances are typically obtained via RoutingModel.add_nodes_set_var() and VehicleType.add_nodes_set_var(), not constructed directly.

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

  • initial_value (set or list) – Initial contents of the set.

id

Variable identifier.

Type:

int

type

Always "setvar".

Type:

str

initial_value

Initial contents.

Type:

set or list

preference

Set variables do not carry a solver preference.

Type:

None

Examples

>>> import grid
>>> model = grid.RoutingModel()
>>> vehicle = model.add_vehicle_type(id=0, count=1, capacity=10)
>>> stations = vehicle.add_nodes_set_var(initial_value={3, 4})
>>> stations
SetVar_0
>>> stations.contains(3)
Expr(contains, SetVar_0, SetElem_3, boolean)
>>> stations.remove(3)
Expr(remove, SetVar_0, SetElem_3, set)
__init__(id, initial_value)[source]
Parameters:

id (int)

Methods

__init__(id, initial_value)

add(element)

Return an expression representing the set with element added.

contains(element)

Return a boolean expression testing membership of element.

is_empty()

Return a boolean expression testing whether the set is empty.

len()

Return an integer expression giving the cardinality of the set.

remove(element)

Return an expression representing the set with element removed.

same(other)

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

add(element)[source]

Return an expression representing the set with element added.

Parameters:

element (int or Expr) – Element to add.

Returns:

Expression of type "set".

Return type:

Expr

Examples

>>> import grid
>>> model = grid.RoutingModel()
>>> vehicle = model.add_vehicle_type(id=0, count=1, capacity=10)
>>> stations = vehicle.add_nodes_set_var(initial_value={3, 4})
>>> stations.add(5)
Expr(add, SetVar_0, SetElem_5, set)
contains(element)[source]

Return a boolean expression testing membership of element.

Parameters:

element (int or Expr) – Element to test.

Returns:

Boolean expression.

Return type:

Expr

Examples

>>> import grid
>>> model = grid.RoutingModel()
>>> vehicle = model.add_vehicle_type(id=0, count=1, capacity=10)
>>> stations = vehicle.add_nodes_set_var(initial_value={3, 4})
>>> stations.contains(3)
Expr(contains, SetVar_0, SetElem_3, boolean)
is_empty()[source]

Return a boolean expression testing whether the set is empty.

Returns:

Boolean expression.

Return type:

Expr

Examples

>>> import grid
>>> model = grid.RoutingModel()
>>> vehicle = model.add_vehicle_type(id=0, count=1, capacity=10)
>>> stations = vehicle.add_nodes_set_var(initial_value={3, 4})
>>> stations.is_empty()
Expr(empty, SetVar_0, None, boolean)
len()[source]

Return an integer expression giving the cardinality of the set.

Returns:

Integer expression.

Return type:

Expr

Examples

>>> import grid
>>> model = grid.RoutingModel()
>>> vehicle = model.add_vehicle_type(id=0, count=1, capacity=10)
>>> stations = vehicle.add_nodes_set_var(initial_value={3, 4})
>>> stations.len()
Expr(add, SetVar_0, None, integer)
remove(element)[source]

Return an expression representing the set with element removed.

Parameters:

element (int or Expr) – Element to remove.

Returns:

Expression of type "set".

Return type:

Expr

Examples

>>> import grid
>>> model = grid.RoutingModel()
>>> vehicle = model.add_vehicle_type(id=0, count=1, capacity=10)
>>> stations = vehicle.add_nodes_set_var(initial_value={3, 4})
>>> stations.remove(4)
Expr(remove, SetVar_0, SetElem_4, set)
same(other)[source]

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

Parameters:

other (SetVar)

Return type:

bool

Examples

>>> import grid
>>> model = grid.RoutingModel()
>>> vehicle = model.add_vehicle_type(id=0, count=1, capacity=10)
>>> s1 = vehicle.add_nodes_set_var(initial_value={1})
>>> s2 = vehicle.add_nodes_set_var(initial_value={2})
>>> s1.same(s1)
True
>>> s1.same(s2)
False