I am currently working on a vehicle routing problem in Python but I have to set up a dictionary with ('zone x', 'zone y'): numeric distance. I have a lot of zones and for all individual combinations of zones I need a combination for the distance: distance [zone i, zone j]: "numeric value".
I tried to fix it in the following manner:
> dfdistances = pd.read_excel("Distances.xlsx", index_col=0)
> distances = dfdistances.to_dict('index')
I am also aware of the other options for 'index', but they also seem to not fulfill my goal of multi-key indexation. When I try to solve my linear programming I get the following error:
KeyError: ('2', '2')
Of course I could write all the possible zone combinations out by hand, but this is far from ideal and requires a lot of time if I would do tests on multiple sources of data. In the ideal situation I would get a final dictionary of all combinations (dictionary of 51x51), I have illustrated it below for zone 0 and zone 1.
Can someone help we with the implementation?
Distances between zones [i,j] in kilometres
> distances= {('0','0'):0.000, ('0','1'):4.525, ('0','2'):4.990, ('0','3'):2.053, ('0','4'):3.848, ('0','5'):3.743, ('0','6'):5.734, ('0','7'):3.975, ('0','8'):3.761, ('0','9'):2.601, ('0','10'):7.320, ('0','11'):3.836, ('0','12'):2.970, ('0','13'):3.134, ('0','14'):4.332, ('0','15'):3.500, ('0','16'):3.732, ('0','17'):3.990, ('0','18'):6.676, ('0','19'):2.703, ('0','20'):5.197,
('1','0'):4.525, ('1','1'):0.000, ('1','2'):1.572, ('1','3'):2.561, ('1','4'):1.150, ('1','5'):0.942, ('1','6'):1.540, ('1','7'):0.863, ('1','8'):0.776, ('1','9'):1.937, ('1','10'):2.825, ('1','11'):0.694, ('1','12'):2.018, ('1','13'):1.748, ('1','14'):3.441, ('1','15'):1.083, ('1','16'):0.812, ('1','17'):0.828, ('1','18'):2.274, ('1','19'):2.080, ('1','20'):1.116,
Eventually, I will use the distance matrix ('distances') in my objective function for vehicle routing, but then I encounter the problem of non-existing keys for the distances between zone i and zone j.
Set up the objective function
> objExpr = sum(0.1*distances[i,j]*model.x[i,j] for i in model.N for j in model.N) + sum(fc[j]*(1-CAP[j,w])*model.y[j,w] for j in model.C for w in model.W)
> model.obj = pe.Objective(expr = objExpr, sense = pe.minimize)