Exemplo n.º 1
0
    def _set_arc_distance(self, data: VehicleRoutingProblemInstance,
                          manager: RoutingIndexManager, routing: RoutingModel):
        # ========= DISTANCE CONSTRAIN =========
        cost_callback_indices = []
        # model "cost between nodes per vehicle"
        for vehicle_idx in range(data.num_plans_to_create):

            def vehicle_cost_callback(from_index, to_index):
                from_node = manager.IndexToNode(from_index)
                to_node = manager.IndexToNode(to_index)
                distance = data.car_distance_matrix[from_node][
                    to_node]  # in meters

                return distance

            cost_callback_index = routing.RegisterTransitCallback(
                vehicle_cost_callback)
            cost_callback_indices.append(cost_callback_index)
            routing.SetArcCostEvaluatorOfVehicle(cost_callback_index,
                                                 vehicle_idx)

        routing.AddDimensionWithVehicleTransits(
            cost_callback_indices,
            0,  # waiting time
            MAX_TIMESTAMP_VALUE,  # maximum distance per vehicle
            True,  # Force start cumul to zero.
            self.DISTANCE_DIMENSION_NAME)
Exemplo n.º 2
0
    def _set_arc_durations(self, data: VehicleRoutingProblemInstance,
                           manager: RoutingIndexManager, routing: RoutingModel,
                           dimension_name: str):
        # ========= DURATION CONSTRAIN =========
        cost_callback_indices = []
        # model "cost between nodes per vehicle"
        for vehicle_idx in range(data.num_plans_to_create):

            def vehicle_cost_callback(from_index, to_index):
                from_node = manager.IndexToNode(from_index)
                to_node = manager.IndexToNode(to_index)
                time = data.car_duration_matrix[from_node][
                    to_node]  # in seconds

                is_pickup = from_node in data.pickup_nodes
                is_drop = from_node in data.drop_nodes

                waiting_time = 0
                if is_pickup:
                    waiting_time = ConfigProvider.get_config(
                    ).pickup_waiting_time
                elif is_drop:
                    waiting_time = ConfigProvider.get_config(
                    ).drop_waiting_time

                return time + waiting_time

            cost_callback_index = routing.RegisterTransitCallback(
                vehicle_cost_callback)
            cost_callback_indices.append(cost_callback_index)

        routing.AddDimensionWithVehicleTransits(
            cost_callback_indices,
            MAX_TIMESTAMP_VALUE,  # waiting time
            MAX_TIMESTAMP_VALUE,  # maximum time per vehicle
            # since we are using timestamps as measuring unit we should not overflow
            False,  # Don't force start cumul to zero.
            dimension_name)