Import distance matrix from CSV

1,074 views
Skip to first unread message

Bryan Salazar López

unread,
Mar 2, 2021, 8:48:29 PM3/2/21
to or-tools-discuss
Hi guys, I'm new to this group and I really don't know how I've progressed in building my model, since I'm an Industrial Engineer and I'm not proficient in Python.

I have managed to build the script that imports the distance matrix from "Distance Matrix API" and then operates them by multiplying matrices and scalars, transforming a matrix of distances and a matrix of times, into a matrix resulting in costs.

This cost matrix I can export in multiple formats: txt / csv, and of course it is a variable from my script.

I have not succeeded in getting the script that contains the Or-tools model to import the matrix from the script that generates the cost matrix. However, as I have mentioned, I have the csv and txt files.

Can you help me in the structure of the model in Python that imports a csv file as the distance matrix of the Or-tools model?

I would greatly appreciate your collaboration, this optimization module is part of a project to analyze the process of generating biodiesel from used cooking oil.
matriz_costos.csv

Madushan Fernando

unread,
Mar 2, 2021, 8:58:05 PM3/2/21
to or-tools...@googlegroups.com
Hi Bryan, 
I used the following python code to import data from CSV and create the nested matrix. Then input that matrix as distance matrix.
import pandas
##Import CSV##
pandas.set_option('expand_frame_repr'False)
df = pandas.read_csv(r'C:\Users\User\Desktop\My working\Distance.csv')
print(df)
##Dataframe to list##
Matrix= df["Distance"].tolist() 
print(Matrix)
##Convert into nested List##
i=0
new_list=[]
while i<len(Matrix):
  new_list.append(Matrix[i:i+12])
  i+=12
print(new_list)


--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/78eaae49-fac1-49cc-b3c9-2446eaa568den%40googlegroups.com.

Bryan Salazar López

unread,
Mar 2, 2021, 9:00:15 PM3/2/21
to or-tools-discuss
I attach the scripts that I use to call and operate the distances (D_matrix) and the optimization script (prueba_penalties)
prueba_penalties.py
D_matrix.py

Bryan Salazar López

unread,
Mar 2, 2021, 9:18:56 PM3/2/21
to or-tools-discuss
Hello, I appreciate your answer, I do not know if you could observe my .csv

I don't know if your code applies to my format, since the matrix is practically built

In fact, I'm going to show you the output I have from my D_matrix.py file:

output_D_Matrix.png

This output is named as variable F in my script.

How can I invoke it from the optimizer script? I see that it is in the precise format that Or-tools requires

Or, How can I import my .csv in the format I attached above?

Madushan Fernando

unread,
Mar 2, 2021, 9:34:59 PM3/2/21
to or-tools...@googlegroups.com
I imported data from the following CSV and it contains a distances data frame.

Distance.csv

Bryan Salazar López

unread,
Mar 2, 2021, 9:46:24 PM3/2/21
to or-tools-discuss
Now I understand your code a bit. No, I already have my csv in matrix form

output_D_Matrix.png

An n x m matrix, where n = m

sirinebe...@gmail.com

unread,
Oct 5, 2021, 7:18:04 AM10/5/21
to or-tools-discuss
Which is the feasible Dataframe or dictionary? I import data from csv file after  i convert to dictionary data. it  read correctly
readCSV.png
 But,  the code of TSP   displays the distance = 0

Bryan Salazar López

unread,
Oct 5, 2021, 12:40:15 PM10/5/21
to or-tools-discuss
It has worked like this for me (VRP):

def matrix_distance():

  matrix = np.genfromtxt('distance_matrix.csv', delimiter=';')
    
  return matrix

def create_data_model():
    """Stores the data for the problem."""
    data['distance_matrix'] = matrix_distance()
    data['vehicle_capacities'] = vehiculos_capacidad
    vehiculos_numero = len(data['vehicle_capacities'])
    data['num_vehicles'] = vehiculos_numero
    data['penalties'] = penalty
    data['depot'] = 0
    return data

    def distance_callback(from_index, to_index):
        # Convert from routing variable Index to distance matrix NodeIndex.
        from_node = manager.IndexToNode(from_index)
        to_node = manager.IndexToNode(to_index)
        return data['distance_matrix'][from_node][to_node]

    distance_callback_index = routing.RegisterTransitCallback(distance_callback)

Sirine Belguith

unread,
Oct 5, 2021, 3:22:00 PM10/5/21
to or-tools...@googlegroups.com
Thanks Bryan.
it returns the distance of the route  and objectives =0.
resultsVRP.png
Below is my code:


def matrix_distance():

    matrix = np.genfromtxt('vehicle (2).csv', delimiter=';')
    
    return matrix
def create_data_model():
    """Stores the data for the problem."""
    data = {}
    data['distance_matrix'] = matrix_distance()
    data['num_vehicles'] = 1
    data['depot'] = 0
    return data


def print_solution(datamanagerroutingsolution):
    """Prints solution on console."""
    print(f'Objective: {solution.ObjectiveValue()}')
    
    max_route_distance = 0
    print(len(data['distance_matrix']))
    for vehicle_id in range(data['num_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)
        #{print(index)
        max_route_distance = max(route_distance, max_route_distance)
    print('Maximum of the route distances: {}m'.format(max_route_distance))
def main():
    """Entry point of the program."""
    # Instantiate the data problem.
    data = create_data_model()

    # Create the routing index manager.
    manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),
                                           data['num_vehicles'], data['depot'])

    # Create Routing Model.
    routing = pywrapcp.RoutingModel(manager)


    # Create and register a transit callback.
    def distance_callback(from_indexto_index):
        """Returns the 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)
        return data['distance_matrix'][from_node][to_node]

    transit_callback_index = routing.RegisterTransitCallback(distance_callback)

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

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

    # Setting first solution heuristic.
    search_parameters = pywrapcp.DefaultRoutingSearchParameters()
    search_parameters.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)

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

    # Print solution on console.
    if solution:
        print_solution(data, manager, routing, solution)
    else:
        print('No solution found !')


if __name__ == '__main__':
    main()


Garanti sans virus. www.avast.com

Reply all
Reply to author
Forward
0 new messages