@@ -103,7 +103,8 @@ int main(int argc, char* argv[]) {
103103"""Vehicle Routing example."""
104104
105105from ortools.routing import enums_pb2
106- from ortools.routing import pywraprouting
106+ from ortools.routing import parameters_pb2
107+ from ortools.routing.python import routing
107108
108109
109110def main():
@@ -114,46 +115,50 @@ def main():
114115 depot = 0
115116
116117 # Create the routing index manager.
117- manager = pywraprouting.IndexManager(
118- num_locations, num_vehicles, depot
119- )
118+ manager = routing.IndexManager(num_locations, num_vehicles, depot)
120119
121- # Create Routing Model .
122- routing = pywraprouting .Model(manager)
120+ # Create Routing routing .
121+ routing_model = routing .Model(manager)
123122
124123 # Create and register a transit callback.
125124 def distance_callback(from_index, to_index):
126125 """Returns the absolute difference between the two nodes."""
127126 # Convert from routing variable Index to user NodeIndex.
128- from_node = int(manager.IndexToNode (from_index))
129- to_node = int(manager.IndexToNode (to_index))
127+ from_node = int(manager.index_to_node (from_index))
128+ to_node = int(manager.index_to_node (to_index))
130129 return abs(to_node - from_node)
131130
132- transit_callback_index = routing.RegisterTransitCallback(distance_callback)
131+ transit_callback_index = routing_model.register_transit_callback(
132+ distance_callback
133+ )
133134
134135 # Define cost of each arc.
135- routing.SetArcCostEvaluatorOfAllVehicles (transit_callback_index)
136+ routing_model.set_arc_cost_evaluator_of_all_vehicles (transit_callback_index)
136137
137138 # Setting first solution heuristic.
138- search_parameters = pywraprouting.DefaultRoutingSearchParameters()
139+ search_parameters: parameters_pb2.RoutingSearchParameters = (
140+ routing.default_routing_search_parameters()
141+ )
139142 search_parameters.first_solution_strategy = (
140143 enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC
141144 ) # pylint: disable=no-member
142145
143146 # Solve the problem.
144- assignment = routing.SolveWithParameters (search_parameters)
147+ assignment = routing_model.solve_with_parameters (search_parameters)
145148
146149 # Print solution on console.
147- print(f"Objective: {assignment.ObjectiveValue ()}")
148- index = routing.Start (0)
150+ print(f"Objective: {assignment.objective_value ()}")
151+ index = routing_model.start (0)
149152 plan_output = "Route for vehicle 0:\n"
150153 route_distance = 0
151- while not routing.IsEnd (index):
152- plan_output += f"{manager.IndexToNode (index)} -> "
154+ while not routing_model.is_end (index):
155+ plan_output += f"{manager.index_to_node (index)} -> "
153156 previous_index = index
154- index = assignment.Value(routing.NextVar(index))
155- route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
156- plan_output += f"{manager.IndexToNode(index)}\n"
157+ index = assignment.value(routing_model.next_var(index))
158+ route_distance += routing_model.get_arc_cost_for_vehicle(
159+ previous_index, index, 0
160+ )
161+ plan_output += f"{manager.index_to_node(index)}\n"
157162 plan_output += f"Distance of the route: {route_distance}m\n"
158163 print(plan_output)
159164
0 commit comments