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
Esempio n. 2
0
def main() -> None:
    """
    Entry point of the program.
    """

    # Parse command line arguments
    args = parse_args()

    # Instantiate the data problem
    data = load_data_model(args.path)

    # Create the Routing Index Manager and Routing Model
    manager = RoutingIndexManager(data["num_locations"], data["num_vehicles"],
                                  data["depot"])
    routing = RoutingModel(manager)

    # Define weights of edges
    weight_callback_index = routing.RegisterTransitCallback(
        create_weight_callback(manager, data))
    routing.SetArcCostEvaluatorOfAllVehicles(weight_callback_index)

    # Add capacity constraints
    demand_callback = create_demand_callback(manager, data)
    demand_callback_index = routing.RegisterUnaryTransitCallback(
        demand_callback)
    add_capacity_constraints(routing, manager, data, demand_callback_index)

    # Add time window constraints
    time_callback_index = routing.RegisterTransitCallback(
        create_time_callback(manager, data))
    add_time_window_constraints(routing, manager, data, time_callback_index)

    # Set first solution heuristic (cheapest addition)
    search_params = DefaultRoutingSearchParameters()
    search_params.first_solution_strategy = FirstSolutionStrategy.PATH_CHEAPEST_ARC
    if args.gls:
        search_params.local_search_metaheuristic = (
            LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
        # NOTE: Since Guided Local Search could take a very long time, we set a
        # reasonable time limit
        search_params.time_limit.seconds = 30
    if args.verbose:
        search_params.log_search = True

    # Solve the problem
    assignment = routing.SolveWithParameters(search_params)
    if not assignment:
        print("No solution found.")
        return

    # Print the solution
    print_solution(data, routing, manager, assignment)

    # Export network and route graphs
    if args.export_network_graph:
        draw_network_graph(args.export_network_graph, data)
    if args.export_route_graph:
        draw_route_graph(args.export_route_graph, data, routing, manager,
                         assignment)
Esempio n. 3
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)
Esempio n. 4
0
    def solve(self, indices_to_visit: List[int] = None) -> Dict[str, Any]:
        """Finds the optimal order of facilities to minimize distance.

        Parameters
        ----------
        indices_to_visit : List[int]
            The list of indices corresponding to the desired facilities to visit

        Returns
        -------
        Dict[str, Any]
            Soltution dictionary with keys:
            - 'objective', set to objective value (minified distance)
            - 'order', instructions for the order of facilities to visit
        """
        if indices_to_visit is None:
            indices_to_visit = list(range(len(self.matrix)))

        # make sure home location is in the listed, and that the list is sorted
        if self.home_index not in indices_to_visit:
            indices_to_visit.append(self.home_index)
        indices_to_visit.sort()

        data = self._create_data_model(indices_to_visit)

        # create routing index manager
        manager = RoutingIndexManager(len(data['distance_matrix']),
                                      data['num_vehicles'], data['home'])

        # create routing model
        routing = RoutingModel(manager)

        def distance_callback(from_index, to_index):
            # returns distance between two nodes
            from_node = manager.IndexToNode(from_index)
            to_node = manager.IndexToNode(to_index)
            dist = data['distance_matrix'][from_node][to_node]

            return dist

        transit_callback_index = routing.RegisterTransitCallback(
            distance_callback)

        # define cost of each arc
        routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

        # set first solution heuristic
        search_params = pywrapcp.DefaultRoutingSearchParameters()
        search_params.first_solution_strategy = (
            routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)

        # solve problem
        assignment = routing.SolveWithParameters(search_params)

        return self._extract_solution(manager, routing, assignment,
                                      indices_to_visit)
Esempio n. 5
0
    def _set_objective_function(problem: Problem, manager: RoutingIndexManager,
                                solver: RoutingModel):
        """Method to set the objective function of the Optimization Model"""
        def _time_callback(from_index: int, to_index: int):
            """Callback to obtain the complete time between Stops"""

            origin = manager.IndexToNode(from_index)
            destination = manager.IndexToNode(to_index)
            travelling_time = problem.estimations[(origin, destination)]
            service_time = problem.stops[destination].service_time

            return travelling_time + service_time

        callback_index = solver.RegisterTransitCallback(_time_callback)
        solver.SetArcCostEvaluatorOfAllVehicles(callback_index)
        logging.info('Set the objective function to the OptimizationModel.')
def main():
    """
    Entry point of the program.
    """

    # Parse command line arguments
    args = parse_args()
    #Crafted data :
    coordinates = [[0.848307, 0.32357132], [0.53917086, 0.862489],
                   [0.28499496, 0.7470896], [0.66891015, 0.07388902],
                   [0.6514969, 0.4077109], [0.782356, 0.28167558],
                   [0.2601446, 0.9196638], [0.3402847, 0.62080956],
                   [0.3920853, 0.8653455], [0.79114413, 0.09456837],
                   [0.11343169, 0.73570406]]
    Distance_matrix = list()
    for j in range(len(coordinates)):
        l = list()
        for i in range(len(coordinates)):
            l.append(distance(coordinates[j], coordinates[i]))
        Distance_matrix.append(l)
    data = dict()
    data['weights'] = Distance_matrix
    data['service_times'] = [0, 1, 0.5, 1, 1, 0.5, 2, 2, 2, 0.5, 2]
    data['demands'] = [
        0, 0.25, 0.2, 0.1, 0.2, 0.35, 0.15, 0.3, 0.15, 0.15, 0.3
    ]
    data['time_windows'] = [[0, 9], [0, 5], [5, 9], [0, 9], [0, 9], [0, 9],
                            [0, 5], [5, 9], [5, 9], [0, 5], [0, 9]]
    data['vehicle_capacities'] = [1, 1, 1]
    data['depot'] = 0
    data['num_locations'] = 11
    data['num_vehicles'] = 3

    # Instantiate the data problem
    #data = load_data_model(args.path)

    # Create the Routing Index Manager and Routing Model
    manager = RoutingIndexManager(data['num_locations'], data['num_vehicles'],
                                  data['depot'])
    routing = RoutingModel(manager)

    # Define weight of each edge
    weight_callback_index = routing.RegisterTransitCallback(
        create_weight_callback(manager, data))
    routing.SetArcCostEvaluatorOfAllVehicles(weight_callback_index)

    # Add capacity constraints
    demand_callback = create_demand_callback(manager, data)
    demand_callback_index = routing.RegisterUnaryTransitCallback(
        demand_callback)
    add_capacity_constraints(routing, manager, data, demand_callback_index)

    # Add time window constraints
    time_callback_index = routing.RegisterTransitCallback(
        create_time_callback(manager, data))
    add_time_window_constraints(routing, manager, data, time_callback_index)

    # Set first solution heuristic (cheapest addition)
    search_params = DefaultRoutingSearchParameters()
    # pylint: disable=no-member
    search_params.first_solution_strategy = FirstSolutionStrategy.PATH_CHEAPEST_ARC
    if args.gls:
        # pylint: disable=no-member
        search_params.local_search_metaheuristic = LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH
        # NOTE: Since Guided Local Search could take a very long time, we set a reasonable time
        # limit
        search_params.time_limit.seconds = 30
    if args.verbose:
        search_params.log_search = True

    # Solve the problem
    assignment = routing.SolveWithParameters(search_params)
    if not assignment:
        print('No solution found.')
        return

    # Print the solution
    print_solution(Distance_matrix, data, routing, manager, assignment)

    # Draw network and route graphs
    """if args.graph:
Esempio n. 7
0
        hoge = OrtoolsJson(args.path)
        data = hoge.load_json()
    else:
        raise FileNotFoundError(f'json file: {args.path}')
    # Create Routing Index Manager
    manager = RoutingIndexManager(data['num_locations'], data['num_vehicles'],
                                  data['starts'], data['ends'])
    # Create Routing Model
    routing = RoutingModel(manager)
    """
	Define weight of each edge
	https://developers.google.com/optimization/routing/vrp
	"""
    distance_callback = create_distance_callback(manager, data)
    transit_callback_index = routing.RegisterTransitCallback(distance_callback)
    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
    """
	Add capacity constraints
	https://developers.google.com/optimization/routing/cvrp
	"""
    demand_callback = create_demand_callback(manager, data)
    demand_callback_index = routing.RegisterUnaryTransitCallback(
        demand_callback)
    add_capacity_constraints(routing, manager, data, demand_callback_index)
    """
	Add time window constraints
	https://developers.google.com/optimization/routing/vrptw
	"""
    if 'time_windows' in data.keys():
        time_callback = create_time_callback(manager, data)
        time_callback_index = routing.RegisterTransitCallback(time_callback)