Google OR-Tools的TSP教程示例在Ubuntu 16.04上崩溃求助
Hey there, let's work through this crash issue you're hitting with your TSP program using OR-Tools on Ubuntu 16.04. That Check failed: step > 0 error from search.cc usually points to a problem with either your model setup or the search parameters you're using—let's break down the fixes step by step:
1. First, Verify Your TSP Model Has Valid Nodes
The most common cause of this error is having too few nodes in your problem. TSP requires at least 2 distinct nodes to calculate a valid route. Double-check:
- Your distance matrix isn't empty or only has a single entry
- You're passing the correct number of nodes to the
RoutingIndexManager - All node indices in your constraints are valid (no out-of-bounds values)
2. Tweak Your Search Parameters Configuration
You've already updated the default parameters call, but let's make sure you're setting explicit values that avoid the step-size issue. Try replacing your parameters setup with this:
# Initialize search parameters with explicit settings search_parameters = pywrapcp.RoutingModel.DefaultModelParameters() # Set a heuristic to guide the search search_parameters.local_search_metaheuristic = pywrapcp.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH # Add a time limit to prevent infinite loops search_parameters.time_limit.seconds = 10 # Enable verbose logging to see what's happening during search search_parameters.log_search = True
Explicitly defining these parameters gives the solver clear instructions, which often resolves the "step > 0" check failure.
3. Fix OR-Tools Version Compatibility
Ubuntu 16.04 ships with Python 3.5, which is no longer supported by newer OR-Tools releases. The binary you installed via pip3 install ortools might be incompatible. Try installing a version that works with Python 3.5:
pip3 install ortools==8.2.8710
This is the last stable release that officially supports older Python versions like 3.5, which should play nicely with Ubuntu 16.04.
4. Double-Check Your Routing Model Setup
Make sure you're initializing the RoutingIndexManager and RoutingModel correctly. Here's a quick sanity check snippet for a basic TSP:
# Example: 4-node TSP distance matrix distance_matrix = [ [0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0] ] # Initialize manager with correct node count, vehicle count, and start node manager = pywrapcp.RoutingIndexManager(len(distance_matrix), 1, 0) routing = pywrapcp.RoutingModel(manager) # Make sure your distance callback is properly registered def distance_callback(from_index, to_index): from_node = manager.IndexToNode(from_index) to_node = manager.IndexToNode(to_index) return distance_matrix[from_node][to_node] transit_callback_index = routing.RegisterTransitCallback(distance_callback) routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
Ensure your code follows this pattern—any mistake in registering callbacks or setting up the manager can lead to the solver hitting invalid state checks.
5. Enable Detailed Logging to Debug Further
If the above steps don't fix it, enable proper Google Logging to get more context before the crash. Add this at the very start of your code:
from ortools.init import pywrapinit # Initialize logging to a file for detailed debugging pywrapinit.CppBridge.InitLogging('tsp_debug.log')
The log file will capture solver activity leading up to the crash, which can help pinpoint exactly where the step-size issue is occurring.
内容的提问来源于stack exchange,提问作者Leevi L

