I have stolen diamonds in a lot of different places. The places are on a coordinate system (x,y) where each place is named after a number and have an d-time for example:
Name X Y dT
1 283 248 0
2 100 118 184
3 211 269 993
4 200 200 948
5 137 152 0
6 297 263 513
7 345 256 481
8 265 212 0
9 185 222 840
10 214 180 1149
11 153 218 0
12 199 199 0
13 289 285 149
14 177 184 597
15 359 192 0
16 161 207 0
17 94 121 316
18 296 246 0
19 193 122 423
20 265 216 11
dT stand for due time and it's given for each place, which is the fixed time when we need to get the diamonds back before the thief move their hideout away.
Starting point is always 1.
I need to visit all places only once and get the diamonds back such that the total delay is minimized. Distance is calculated with Euclidean distance rounded to its closest integer. The arrival time for each places is calculated as in distance + previous distance. The delay for each place is arrival-due and the total delay is the sum of the delays between places.
If the police can get the diamonds before the due time of that place, then the delay is equal to 0; otherwise, the delay equals the difference between the time of arrival and due time of the place.
My mission is to find the right order in which the police can visit each place once that minimizes the delay for two larger instances.
I think I'm pretty much close to an answer myself but I would love to know how would you solve it and also to get a better understanding of the math behind it to be able to program it better.
Here are my codes that calculate everything, the only thing missing is the way to find the right order :
#------------------------------------------------------------------------
poss=[(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)] # the order
here=[]
for p in range(len(poss)):
tempos=[]
for o in range(len(index)):
point=poss[p][o]
valuez=order[point-1]
tempos.append(valuez)
here.append(tempos)
#//DUE//
due =[[item[b][3] for b in range(len(index))] for item in here]
#//DISTANCE//
x_ = [[item[b][1] for b in range(len(index))] for item in here]
y_ = [[item[b][2] for b in range(len(index))] for item in here]
z = [list(zip(x_[a],y_[a])) for a in range(len(x_))]
dis = []
for aa in range(len(poss)) :
tempor=[]
for i in range(len(index)-1):
firstpoint = z[aa][i]
secondpoint = z[aa][i+1]
distance = round(np.linalg.norm(np.array(secondpoint)-np.array(firstpoint)))
distance = int(distance)
tempor.append(distance)
dis.append(tempor)
#//ARRIVAL TIME//
#Arrival time is the sum of the pv distance.
arrival = []
for v in range(len(poss)):
forone = [0,dis[v][0],]
for r in range(len(index)-2):
sumz = dis[v][r+1] + forone[r+1]
sumz = int(sumz)
forone.append(sumz)
arrival.append(forone)
#//DELAY//
delay=[]
for d in range(len(poss)) :
tempo=[]
for q in range(len(index)):
v=arrival[d][q]-due[d][q]
if arrival[d][q] <= due[d][q]:
tempo.append(0)
else :
tempo.append(v)
delay.append(tempo)
#//ORDER//
#goal is to find the right order that minimizes the delay for two larger instances.
total = [sum(x) for x in delay]
small= min(total)
final=here[total.index(small)]
print(small)