grid.Node

class grid.Node(id, demand=None, tw_start=None, tw_end=None, service_t=None, waiting_t=None, pickup=None, delivery=None, optional=False, max_visits=1, depot=False)[source]

Bases: object

Vertex of the routing graph, representing a customer, depot, or stop.

Holds attributes used to model many VRP variants: demand (CVRP), time windows (VRPTW), pickup/delivery quantities (PDPTW), precedence, optional visits and profits (orienteering / TOP), and depot flags.

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

  • demand (dict[str, float] | int | float) – Demand consumed when visiting the node. A scalar value is treated as single-commodity demand; a dict maps commodity name to per-commodity demand for multi-commodity problems.

  • tw_start (float) – Earliest time at which service can begin at the node.

  • tw_end (float) – Latest time at which service can begin at the node.

  • service_t (float) – Service time required at the node.

  • waiting_t (float) – Maximum waiting time allowed at the node before its time window opens. Caps how long a vehicle may wait when it arrives before tw_start.

  • pickup (dict[str, float]) – Quantities picked up at the node, mapped by commodity name (PDPTW).

  • delivery (dict[str, float]) – Quantities delivered at the node, mapped by commodity name (PDPTW).

  • optional (bool) – Whether the node may be skipped.

  • max_visits (int) – Maximum number of times the node may be visited.

  • depot (bool) – Whether the node acts as a depot.

transitions

Transitions registered on this node via add_transition().

Type:

list of Transition

constraints

Constraints registered on this node via add_constraint().

Type:

list of Constraint

Examples

>>> import grid
>>> model = grid.RoutingModel()
>>> depot = model.add_node(id=0, depot=True)
>>> customer = model.add_node(id=1, demand=3, service_t=2)
>>> customer.demand
3
>>> depot.depot
True
__init__(id, demand=None, tw_start=None, tw_end=None, service_t=None, waiting_t=None, pickup=None, delivery=None, optional=False, max_visits=1, depot=False)[source]
Parameters:

Methods

__init__(id[, demand, tw_start, tw_end, ...])

add_constraint(constraint)

Register a boolean precondition required to visit this node.

add_transition(var, expression)

Register a variable update applied when the node is visited.

add_constraint(constraint)[source]

Register a boolean precondition required to visit this node.

Parameters:

constraint (Expr) – Boolean expression that must evaluate to true for the node to be visited.

Examples

>>> import grid
>>> model = grid.RoutingModel()
>>> customer = model.add_node(id=1, demand=3)
>>> vehicle = model.add_vehicle_type(id=0, count=1, capacity=10)
>>> load = vehicle.add_integer_var(initial_value=10)
>>> customer.add_constraint(load - 3 >= 0)
>>> len(customer.constraints)
1
add_transition(var, expression)[source]

Register a variable update applied when the node is visited.

Parameters:
  • var (Var) – The decision variable to update.

  • expression (Expr or int or float or bool) – Expression assigned to var. Python literals are automatically wrapped into a Const.

Examples

>>> import grid
>>> model = grid.RoutingModel()
>>> customer = model.add_node(id=1, demand=3)
>>> vehicle = model.add_vehicle_type(id=0, count=1, capacity=10)
>>> load = vehicle.add_integer_var(initial_value=10)
>>> customer.add_transition(var=load, expression=load - 3)
>>> len(customer.transitions)
1