Untitled

mail@pastecode.io avatarunknown
python
3 days ago
981 B
2
Indexable
Never
def my_heuristic(scip, heur, solution, complete):
    # Create tours for two vehicles using a simple greedy heuristic (nearest neighbor)
    unvisited_cities = set(range(num_cities))
    tours = [[], []]
    
    # Start from the first city for both vehicles
    current_cities = [0, 0]
    
    for vehicle in range(2):
        while unvisited_cities:
            current_city = current_cities[vehicle]
            nearest_city = min(unvisited_cities, key=lambda city: scip.distance.euclidean(city_coords[current_city], city_coords[city]))
            tours[vehicle].append(nearest_city)
            current_cities[vehicle] = nearest_city
            unvisited_cities.remove(nearest_city)
    
    # Set the variables in the solution to represent the tours for both vehicles
    for vehicle in range(2):
        for i in range(num_cities):
            for j in range(num_cities):
                if i != j:
                    solution[x[tours[vehicle][i], tours[vehicle][j]]] = 1