google / or-tools

Google's Operations Research tools:
https://developers.google.com/optimization/
Apache License 2.0
11.25k stars 2.13k forks source link

[Python] Setting routing model parameters via constructor hangs SolveWithParameters #3680

Closed paul-hd closed 9 months ago

paul-hd commented 1 year ago

What version of OR-Tools and what language are you using? Version: 9.3.10497, Language: Python

Which solver are you using (e.g. CP-SAT, Routing Solver, GLOP, BOP, Gurobi) Routing Solver

What operating system (Linux, Windows, ...) and version? Macos 13.2

What did you do? Toy example

from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import routing_enums_pb2
import time

n_vehicles = 2
starts = [0,1]
ends = [5,6]
transit_matrix = [
    [0, 100, 776, 696, 582, 0, 100],
    [100, 0, 776, 696, 582, 100, 100],
    [776, 684, 0, 992, 878, 0, 500],
    [776, 684, 992, 0, 878, 0, 100],
    [776, 684, 992, 992, 0, 0, 100],
    [776, 684, 992, 992, 684, 0, 100],
    [776, 684, 992, 992, 684, 100, 0],

]
manager = pywrapcp.RoutingIndexManager(
            len(transit_matrix),
            n_vehicles,
            starts, 
            ends,  
        )
parameters = pywrapcp.DefaultRoutingModelParameters()
parameters.solver_parameters.trace_propagation = True
parameters.solver_parameters.trace_search = True
routing = pywrapcp.RoutingModel(manager, parameters)
# routing = pywrapcp.RoutingModel(manager)

transit_callback_index = routing.RegisterTransitMatrix(transit_matrix)
# Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

# Add Distance constraint.
dimension_name = 'time'
routing.AddDimension(
    transit_callback_index,
    0,  # no slack
    50000,  # vehicle maximum travel distance
    True,  # start cumul to zero
    dimension_name)   

search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC
search_parameters.local_search_metaheuristic = routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH
search_parameters.time_limit.seconds = 1
# search_parameters.log_search = True

ts = time.monotonic()
routing.CloseModelWithParameters(search_parameters)
print(f'Close model {time.monotonic() - ts:.2f}s')

assignment = routing.SolveWithParameters(search_parameters)

def print_solution(manager, routing, solution):
    """Prints solution on console."""
    print(f'Objective: {solution.ObjectiveValue()}')
    max_route_distance = 0
    for vehicle_id in range(n_vehicles):
        index = routing.Start(vehicle_id)
        plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
        route_distance = 0
        while not routing.IsEnd(index):
            plan_output += ' {} -> '.format(manager.IndexToNode(index))
            previous_index = index
            index = solution.Value(routing.NextVar(index))
            route_distance += routing.GetArcCostForVehicle(
                previous_index, index, vehicle_id)
        plan_output += '{}\n'.format(manager.IndexToNode(index))
        plan_output += 'Distance of the route: {}m\n'.format(route_distance)
        print(plan_output)
        max_route_distance = max(route_distance, max_route_distance)
    print('Maximum of the route distances: {}m'.format(max_route_distance))

print_solution(manager, routing, assignment)

What did you expect to see Using the default RoutingModel(manager) constructor I see the below route in 1 sec (respecting time_limit)

Objective: 2370 Route for vehicle 0: 0 -> 4 -> 2 -> 5 Distance of the route: 1574m

Route for vehicle 1: 1 -> 3 -> 6 Distance of the route: 796m

Maximum of the route distances: 1574m

What did you see instead? Instead the script just doesn't terminate (it's been an hour so far)

Mizux commented 1 year ago

working on v9.5 on Linux, will test on Mac ASAP...

Mizux commented 9 months ago

can't reproduce the error on macOS arm64 (Sonoma 14.3) using the pypi package ortools v9.8 -> fixed