Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
3.4 kB
23
Indexable
Never
data = {'distance_matrix': [
 [0.0, 4.0, 2.0, 27.0, 10.0, 32.0, 13.0, 0.0],
 [30.0, 0.0, 2.0, 27.0, 10.0, 32.0, 13.0, 0.0], 
 [30.0, 4.0, 0.0, 27.0, 10.0, 32.0, 13.0, 0.0],
 [30.0, 4.0, 2.0, 0.0, 10.0, 32.0, 13.0, 0.0],
 [30.0, 31.0, 31.0, 27.0, 0.0, 32.0, 13.0, 0.0],
 [30.0, 4.0, 2.0, 27.0, 10.0, 0.0, 13.0, 0.0],
 [30.0, 4.0, 2.0, 25.0, 10.0, 21.0, 0.0, 0.0],
 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
], 
'mapping': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 4, 10: 4, 11: 6},
'pickups': [[4, 0], [9, 1], [10, 2], [6, 5], [11, 3]],
'num_vehicles': 2,
 'depot': 8}
def main():
    data = {}
    data["distance_matrix"] = clean.create_distance_matrix("Data set - Sheet1.csv")
    data["mapping"], data["pickups"] = clean.extract_pick_up("Data set - Sheet1.csv", data["distance_matrix"])
    data["distance_matrix"] = clean.create_distance_matrix("Data set - Sheet1.csv").to_numpy().tolist()
    data["num_vehicles"] = 2
    data["depot"] = len(data["distance_matrix"])
    dup = max([i[0] for i in data["pickups"]]) + 1 # number of duplciated node
    print(dup, data)
    manager = pywrapcp.RoutingIndexManager(dup,
                                           data['num_vehicles'], data['depot'])
    routing = pywrapcp.RoutingModel(manager)

    # Define cost of each arc.
    def distance_callback(from_index, to_index):
        """Returns the manhattan 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)
        from_map = data["mapping"][from_node]
        to_map = data['mapping'][to_node]
        return int(data['distance_matrix'][from_map][to_map])

    transit_callback_index = routing.RegisterTransitCallback(distance_callback)
    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

    dimension_name = 'Distance'
    routing.AddDimension(
        transit_callback_index,
        0,  # no slack
        30000,  # vehicle maximum travel distance
        True,  # start cumul to zero
        dimension_name)
    distance_dimension = routing.GetDimensionOrDie(dimension_name)
    distance_dimension.SetGlobalSpanCostCoefficient(100)

    # Define Transportation Requests.
    for request in data['pickups']:
        pickup_index = manager.NodeToIndex(request[0])
        delivery_index = manager.NodeToIndex(request[1])
        routing.AddPickupAndDelivery(pickup_index, delivery_index)
        routing.solver().Add(
            routing.VehicleVar(pickup_index) == routing.VehicleVar(
                delivery_index))
        routing.solver().Add(
            distance_dimension.CumulVar(pickup_index) <=
            distance_dimension.CumulVar(delivery_index))

    search_parameters = pywrapcp.DefaultRoutingSearchParameters()
    search_parameters.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.PARALLEL_CHEAPEST_INSERTION)
    search_parameters.local_search_metaheuristic = (routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)

    search_parameters.time_limit.FromSeconds(1)

    # Solve the problem.
    solution = routing.SolveWithParameters(search_parameters)

    # Print solution on console.
    if solution:
        print_solution(data, manager, routing, solution)


if __name__ == '__main__':

    main()