Esempio n. 1
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. 2
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. 3
0
    def _solve(self, routing: RoutingModel,
               search_parameters: RoutingSearchParameters,
               initial_routes) -> Assignment:

        if initial_routes is None:
            solution = routing.SolveWithParameters(search_parameters)
        else:
            routing.CloseModelWithParameters(search_parameters)
            initial_solution = routing.ReadAssignmentFromRoutes(
                initial_routes, True)
            solution = routing.SolveFromAssignmentWithParameters(
                initial_solution, search_parameters)

        return solution
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. 5
0
	For other options, see the link below
	https://developers.google.com/optimization/routing/routing_options#first_sol_options
	"""
    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 routing.status() == 1 and assignment:
        t2 = time()
        # Print the solution
        best_score = None
        best_score = print_solution(data, routing, manager, assignment)
        print(f'Measured Execute time: {round(t2-t1, 6)}sec')
        if args.write_csv is not None:
            with open(args.write_csv, 'a') as f:
                f.write(f'{t2-t1},{best_score/1e4}\n')

        # Draw network and route graphs
        if args.graph:
            from visualize import draw_network_graph, draw_route_graph
            # draw_network_graph(data)