Exemplo n.º 1
0
    def apply(self, manager: pywrapcp.RoutingIndexManager,
              routing: pywrapcp.RoutingModel, data_model: DataModel):
        nodes = data_model.nodes
        engineers = data_model.engineers_to_plan

        for index in range(0, data_model.number_of_nodes):
            if index < data_model.number_of_engineers:
                routing.SetAllowedVehiclesForIndex([index], index)
            else:
                allowed_vehicles = [
                    ind for ind, node in enumerate(engineers)
                    if self.can_visit(data_model, nodes[index], engineers[ind])
                ]
                routing.SetAllowedVehiclesForIndex(allowed_vehicles, index)

        return routing
Exemplo n.º 2
0
    def _set_pickup_delivery_constraints(self,
                                         data: VehicleRoutingProblemInstance,
                                         routing: RoutingModel,
                                         manager: RoutingIndexManager):
        time_dimension = routing.GetDimensionOrDie(
            self.DURATION_DIMENSION_NAME)

        # ========= PICKUP/DELIVERY CONSTRAIN =========
        # Define Transportation Requests.
        solver: Solver = routing.solver()
        for pickup_node, delivery_node in data.deliveries_not_started:
            pickup_index = manager.NodeToIndex(pickup_node)
            delivery_index = manager.NodeToIndex(delivery_node)
            routing.AddPickupAndDelivery(pickup_index, delivery_index)
            solver.Add(
                routing.VehicleVar(pickup_index) == routing.VehicleVar(
                    delivery_index))
            solver.Add(
                time_dimension.CumulVar(pickup_index) <=
                time_dimension.CumulVar(delivery_index))

        # Define constraint of deliveries in progress - only vehicle that picked the order is able to finish it
        for courier_idx, node in data.deliveries_in_progress:
            index = manager.NodeToIndex(node)
            routing.SetAllowedVehiclesForIndex([courier_idx], index)