Ejemplo n.º 1
0
def main():
    numLocations = 8
    depotIndex = 0
    numVehicles = 3
    vehicleCapacity = 3

    times = [[0, 957.7, 748.5, 570.8, 706.7, 574.2, 893.2, 187],
             [975.1, 0, 436.6, 894.6, 293, 446.4, 760.2, 901.2],
             [814.6, 399.6, 0, 458, 352.5, 231.5, 876.8, 675.8],
             [526.8, 841.3, 441.7, 0, 785.8, 598.5, 1266.5, 457.8],
             [733.3, 328.4, 351.6, 748.7, 0, 231.8, 625, 659.4],
             [713.7, 360.9, 174.3, 577.1, 270.3, 0, 845.2, 639.8],
             [917.6, 919.7, 1000.8, 1357.9, 733.1, 931, 0, 996.4],
             [246.7, 901.6, 626.4, 383.8, 673.9, 540.7, 986.4, 0]]

    def timeCallback(s, t):
        return times[s][t]

    #demands = [0, 0, 1, 1, 3, 0, 0, 1]
    demands = [0, 1, 1, 1, 1, 1, 1, 1]

    def demandsCallback(s, _):
        return demands[s]

    pickupDeliveries = [
        (6, 4), (5, 4)
    ]  #[ ( 6, 4 ), ( 5, 4 ), ( 5, 3 ), ( 1, 2 ), ( 5, 2 ), ( 6, 4 ), ( 5, 7 ) ];
    model = RoutingModel(numLocations, numVehicles, depotIndex)
    model.SetArcCostEvaluatorOfAllVehicles(timeCallback)

    model.AddDimension(timeCallback, 28800, 28800, True, 'Time')
    timeDimension = model.GetDimensionOrDie('Time')
    for i in range(numLocations):
        timeDimension.CumulVar(i).SetRange(0, 28800)

    model.AddDimension(demandsCallback, 0, vehicleCapacity, True, 'Capacity')
    capacityDimension = model.GetDimensionOrDie('Capacity')

    solver = model.solver()

    for pickup, delivery in pickupDeliveries:
        pickupIndex = model.NodeToIndex(pickup)
        deliveryIndex = model.NodeToIndex(delivery)

        solver.AddConstraint(
            model.VehicleVar(pickupIndex) == model.VehicleVar(deliveryIndex))
        solver.AddConstraint(
            timeDimension.CumulVar(pickupIndex) <= timeDimension.CumulVar(
                deliveryIndex))

        model.AddPickupAndDelivery(pickup, delivery)

    assignment = model.Solve()

    if not assignment:
        sys.exit('Error: No Assignment')

    printAssignment(assignment, model)
    def apply(self, manager:  pywrapcp.RoutingModel, routing: pywrapcp.RoutingModel, data_model: DataModel):
        def distance_callback(from_index, to_index):
            """Returns the distance between the two nodes."""
            # Convert from routing variable Index to distance matrix NodeIndex.
            from_node = manager.IndexToNode(from_index)
            to_node = manager.IndexToNode(to_index)
            return data_model.distance_matrix[from_node][to_node]

        transit_callback_index = routing.RegisterTransitCallback(distance_callback)
        routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

        dimension_name = 'Distance'
        routing.AddDimension(
            transit_callback_index,
            0,  # no slack
            3000,  # vehicle maximum travel distance
            True,  # start cumul to zero
            dimension_name)
        distance_dimension = routing.GetDimensionOrDie(dimension_name)
        distance_dimension.SetGlobalSpanCostCoefficient(100)

        for i in range(data_model.number_of_engineers):
            routing.AddVariableMinimizedByFinalizer(
                distance_dimension.CumulVar(routing.Start(i)))
            routing.AddVariableMinimizedByFinalizer(
                distance_dimension.CumulVar(routing.End(i)))

        return routing
Ejemplo n.º 3
0
def add_time_window_constraints(routing: pywrapcp.RoutingModel, data: dict,
                                time_callback):
    """
    Add time window constraints.
    """
    time = 'Time'
    horizon = 120
    routing.AddDimension(
        evaluator=time_callback,
        slack_max=horizon,  # allow waiting time
        capacity=horizon,  # maximum time per vehicle
        # Don't force start cumul to zero. This doesn't have any effect in this example,
        # since the depot has a start window of (0, 0).
        fix_start_cumul_to_zero=False,
        name=time,
    )
    time_dimension = routing.GetDimensionOrDie(time)
    for loc_node, (open_time, close_time) in enumerate(data['time_windows']):
        index = routing.NodeToIndex(loc_node)
        time_dimension.CumulVar(index).SetRange(open_time, close_time)
Ejemplo n.º 4
0
def add_time_window_constraints(
    routing: RoutingModel,
    manager: RoutingIndexManager,
    data: dict,
    time_callback_index: int,
) -> None:
    max_ = 120
    routing.AddDimension(
        time_callback_index,
        slack_max=
        max_,  # An upper bound for slack (the wait times at the locations)
        capacity=
        max_,  # An upper bound for the total time over each vehicle's route
        # Don't force start cumul to zero. This doesn't have any effect in this example,
        # since the depot has a start window of (0, 0).
        fix_start_cumul_to_zero=False,
        name='Time',
    )
    time_dimension = routing.GetDimensionOrDie('Time')
    for loc_idx, (open_time, close_time) in enumerate(data['time_windows']):
        index = manager.NodeToIndex(loc_idx)
        time_dimension.CumulVar(index).SetRange(open_time, close_time)
Ejemplo n.º 5
0
def add_time_window_constraints(
    routing: RoutingModel,
    manager: RoutingIndexManager,
    data: dict,
    time_callback_index: int,
) -> None:
    """
    Add time window constraints.
    """

    horizon = 120
    routing.AddDimension(
        time_callback_index,
        slack_max=horizon,  # allow waiting time
        capacity=horizon,  # maximum time per vehicle
        # Don't force start cumul to zero. This doesn't have any effect in this example,
        # since the depot has a start window of (0, 0).
        fix_start_cumul_to_zero=False,
        name='Time',
    )
    time_dimension = routing.GetDimensionOrDie('Time')
    for loc_idx, (open_time, close_time) in enumerate(data['time_windows']):
        index = manager.NodeToIndex(loc_idx)
        time_dimension.CumulVar(index).SetRange(open_time, close_time)