maybe a trivial question but i am really struggling. (Update, fixed to provide what i wanted, but very open to more efficient formulations)
i have this DF:
Base_Node Turbine_1 Turbine_2 Charging_Station_1
0 Base_Node 0.0 1.1 5.3 23.5
1 Turbine_1 1.1 0.0 4.2 23.4
2 Turbine_2 5.3 4.2 0.0 22.8
3 Charging_Station_1 23.5 23.4 22.8 0.0
which describes the cost of going from node i to j: I have writen a function that transforms this into a dict successfully. However since im working on a delivery and pick up variant where each (node) should be 2 nodes, one delivery and one pick-up. And for the model to work easier i need the base node (delivery-orgin / pickup-destination) to be in the index 0 and 2n+1, while turbines and chargers have delivery 1...n and pick-up n...2n, where the relation between pick-up and delivery is i and i+n.
def matrix_to_dict(cost_df):
cost_of_route = {}
for row in range(cost_df.shape[0]): # Loop through rows
for col in range(1, cost_df.shape[1]): # Start from 1 to exclude first column
cost_of_route[(row, col-1)] = cost_df.iloc[row, col]
return cost_of_route
this makes good dicts but dont take into account that its double up with nodes,
Have fix the problem with this code, it but seems inefficient, but i am not so experienced, so very open for suggestions:
def matrix_to_dict(cost_df,n):
cost_of_route = {}
for row in range(0, 1): # Loop through rows
for col in range(1, cost_df.shape[1]): # Start from 1 to exclude first column
cost_of_route[(row, col-1)] = cost_df.iloc[row, col]
for row in range(0, 1): # Loop through rows
for col in range(1, cost_df.shape[1]-1): # Start from 1 to exclude first column
cost_of_route[(row, col+n)] = cost_df.iloc[row, col+1]
for row in range(0, cost_df.shape[0]): # Loop through rows
for col in range(1, 2): # Start from 1 to exclude first column
cost_of_route[(row, col-1)] = cost_df.iloc[row, col]
for row in range(0, cost_df.shape[0]): # Loop through rows
for col in range(1, 2): # Start from 1 to exclude first column
cost_of_route[(row+n, col-1)] = cost_df.iloc[row, col]
for row in range(1, cost_df.shape[0]): # Loop through rows
for col in range(2, cost_df.shape[1]): # Start from 1 to exclude first column
cost_of_route[(row, col-1)] = cost_df.iloc[row, col]
for row in range(1, cost_df.shape[0]): # Loop through rows
for col in range(2, cost_df.shape[1]): # Start from 1 to exclude first column
cost_of_route[(row+n, col-1)] = cost_df.iloc[row, col]
for row in range(1, cost_df.shape[0]): # Loop through rows
for col in range(2, cost_df.shape[1]): # Start from 1 to exclude first column
cost_of_route[(row, col-1+n)] = cost_df.iloc[row, col]
for row in range(1, cost_df.shape[0]): # Loop through rows
for col in range(2, cost_df.shape[1]): # Start from 2 to exclude first column
cost_of_route[(row+n, col-1+n)] = cost_df.iloc[row, col]
for row in range(0, 1): # Loop through rows
for col in range(2, cost_df.shape[1]): # Start from 1 to exclude first column
cost_of_route[(n+n+1, col-1+n)] = cost_df.iloc[row, col]
# print(n+n+1, col-1+n)
for row in range(0, 1): # Loop through rows
for col in range(2, cost_df.shape[1]): # Start from 1 to exclude first column
cost_of_route[(col-1+n, n+n+1)] = cost_df.iloc[row, col]
# print(col-1+n, n+n+1)
for row in range(0, cost_df.shape[0]): # Loop through rows
for col in range(1, 2): # Start from 1 to exclude first column
cost_of_route[(row+n, col-1+n)] = cost_df.iloc[row, col]
# print(row+n, col-1+n)
for row in range(0, cost_df.shape[0]): # Loop through rows
for col in range(1, 2): # Start from 1 to exclude first column
cost_of_route[(col-1+n, row+n)] = cost_df.iloc[row, col]
return cost_of_route
output as wanted:
{(0, 0): '0.0', (0, 1): '21.4', (0, 2): '26.4', (0, 3): '21.6', (0, 4): '21.4', (0, 5): '26.4', (0, 6): '21.6', (1, 0): '21.4', (2, 0): '26.4', (3, 0): '0.0', (4, 0): '21.4', (5, 0): '26.4', (6, 0): '21.6', (1, 1): '0.0', (1, 2): '5.0', (1, 3): '1.3', (2, 1): '5.0', (2, 2): '0.0', (2, 3): '5.0', (3, 1): '1.3', (3, 2): '5.0', (3, 3): '0.0', (4, 1): '0.0', (4, 2): '5.0', (4, 3): '21.4', (5, 1): '5.0', (5, 2): '0.0', (5, 3): '26.4', (6, 1): '1.3', (6, 2): '5.0', (6, 3): '21.6', (1, 4): '0.0', (1, 5): '5.0', (1, 6): '1.3', (2, 4): '5.0', (2, 5): '0.0', (2, 6): '5.0', (3, 4): '21.4', (3, 5): '26.4', (3, 6): '21.6', (4, 4): '0.0', (4, 5): '5.0', (4, 6): '1.3', (5, 4): '5.0', (5, 5): '0.0', (5, 6): '5.0', (6, 4): '1.3', (6, 5): '5.0', (6, 6): '0.0', (7, 4): '21.4', (7, 5): '26.4', (7, 6): '21.6', (4, 7): '21.4', (5, 7): '26.4', (6, 7): '21.6'}