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)
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)
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
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)
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 _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)
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:
if args.path is not None: 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)