Skip to content

Commit

Permalink
changed the matrix the right way
Browse files Browse the repository at this point in the history
  • Loading branch information
seppeV2 committed May 2, 2022
1 parent afc9f00 commit 6a8edec
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 65 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Service time = time spend at the costumer.

after depot: do whatever you want.

at wpf: we unload without taking the containr off, no need to calculate additional 12 min.
at wpf: we unload without taking the container off, no need to calculate additional 12 min.

if you can go direct, don't think about depot in the distance matrix!

Expand Down Expand Up @@ -167,7 +167,4 @@ createDistanceMatrix {
# the values, are already taking into consideration all possible intermediate steps, and the shortest one is already selected.
}

```

distance matrix element M[i,j] = time spend for service at costumer i + time need to go to time j
including wpf and depot visit
```
4 changes: 2 additions & 2 deletions clientsTest.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
1,bookstore,20,paper,Antwerpen,3,
13,vegetables,20,general,Veurne,4,15
18,fish shop,20,general,Herentals,4,
8,labo,18,dangerous,Hasselt,6,
8,labo,18,dangerous,Leuven,6,
12,electronics,20,general,Duffel,1,
24,vegetables,20,general,Diest,2,
25,construction,20,construction,Zelzate,5,
69,shit,15,dangerous,Kortrijk,6
69,shit,15,dangerous,Antwerpen,6
17 changes: 9 additions & 8 deletions distanceMatrix.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
,0,1,2,3,4,5,6,7,8
0,40.0,0.0,303.0,183.0,186.0,165.0,161.0,216.0,207.0
1,160.0,274.0,0.0,245.0,300.0,218.0,279.0,159.0,345.0
2,40.0,142.0,233.0,0.0,167.0,91.0,146.0,142.0,212.0
3,66.0,310.0,453.0,333.0,0.0,315.0,245.0,365.0,397.0
4,22.0,31.0,200.0,80.0,86.0,0.0,62.0,112.0,123.0
5,45.0,144.0,235.0,119.0,169.0,93.0,0.0,143.0,214.0
6,72.0,187.0,158.0,163.0,213.0,134.0,192.0,0.0,258.0
7,111.0,374.0,541.0,421.0,440.0,403.0,420.0,453.0,0.0
0,0.0,137.0,206.0,71.0,160.0,40.0,57.0,108.0,84.0
1,40.0,0.0,303.0,183.0,149.0,165.0,161.0,216.0,97.0
2,194.0,274.0,0.0,245.0,247.0,218.0,279.0,159.0,274.0
3,76.0,142.0,233.0,0.0,115.0,91.0,146.0,142.0,142.0
4,13.0,199.0,326.0,206.0,0.0,188.0,183.0,238.0,199.0
5,22.0,31.0,200.0,80.0,45.0,0.0,62.0,112.0,31.0
6,91.0,144.0,235.0,119.0,117.0,93.0,0.0,143.0,144.0
7,117.0,187.0,158.0,163.0,160.0,134.0,192.0,0.0,187.0
8,40.0,44.0,250.0,130.0,96.0,112.0,108.0,163.0,0.0
165 changes: 115 additions & 50 deletions distanceMatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,80 +16,52 @@ def createDistanceMatrix(csvClientFile, cityNames, wpf):
#line form one client
clienti = clients.iloc[i]
if clienti['ActionType'] == 1:
service = 12
if not pd.isnull(clienti['Additional']):
service += clienti['Additional']

for j in range(clientsAmount):
if i != j:
clientj = clients.iloc[j]
distanceMatrix[i,j] = round(newContractTo(clienti, clientj,cityNames)) + service
distanceMatrix[i,j] = round(newContractTo(clienti, clientj,cityNames)) + getServiceTime(clienti,cityNames,wpf)

elif clienti['ActionType'] == 2:
service = 12
if not pd.isnull(clienti['Additional']):
service += clienti['Additional']
for j in range(clientsAmount):
if i != j:
clientj = clients.iloc[j]
distanceMatrix[i,j] = round(endContractTo(clienti, clientj,cityNames, wpf)) + service
distanceMatrix[i,j] = round(endContractTo(clienti, clientj,cityNames, wpf)) + getServiceTime(clienti,cityNames,wpf)

elif clienti['ActionType'] == 3:
service = 12
service += timeBetweenPlaces(clienti['Place'], closestPathWpf(clienti,clienti['Place'] ,wpf, cityNames)['Place'],cityNames)
service += 20
service += timeBetweenPlaces(closestPathWpf(clienti,clienti['Place'] ,wpf, cityNames)['Place'],clienti['Place'],cityNames)
service += 12
if not pd.isnull(clienti['Additional']):
service += clienti['Additional']
for j in range(clientsAmount):
if i != j:
clientj = clients.iloc[j]
distanceMatrix[i,j] = round(sameContainerTo(clienti, clientj,cityNames)) + round(service)
distanceMatrix[i,j] = round(sameContainerTo(clienti, clientj,cityNames)) + getServiceTime(clienti,cityNames,wpf)

elif clienti['ActionType'] == 4:
service = 25
if not pd.isnull(clienti['Additional']):
service += clienti['Additional']
for j in range(clientsAmount):
if i != j:
clientj = clients.iloc[j]
distanceMatrix[i,j] = round(switchTo(clienti, clientj,cityNames, wpf)) + service
distanceMatrix[i,j] = round(switchTo(clienti, clientj,cityNames, wpf)) + getServiceTime(clienti,cityNames,wpf)
elif clienti['ActionType'] == 5:
service = 30
if not pd.isnull(clienti['Additional']):
service += clienti['Additional']
for j in range(clientsAmount):
if i != j:
clientj = clients.iloc[j]
distanceMatrix[i,j] = round(fillingUpTo(clienti, clientj,cityNames, wpf)) + service
distanceMatrix[i,j] = round(fillingUpTo(clienti, clientj,cityNames, wpf)) + getServiceTime(clienti,cityNames,wpf)
elif clienti['ActionType'] == 6:
service = 12
service += timeBetweenPlaces(clienti['Place'], closestPathWpf(clienti,clienti['Place'], wpf, cityNames)['Place'],cityNames)
service += 20
service += timeBetweenPlaces(closestPathWpf(clienti, clienti['Place'] ,wpf, cityNames)['Place'],clienti['Place'],cityNames)
service += 12
if not pd.isnull(clienti['Additional']):
service += clienti['Additional']
for j in range(clientsAmount):
if i != j:
clientj = clients.iloc[j]
distanceMatrix[i,j] = round(dangerousTo(clienti, clientj,cityNames)) + round(service)
distanceMatrix[i,j] = round(dangerousTo(clienti, clientj,cityNames)) + getServiceTime(clienti,cityNames,wpf)

# extending the matrix for depot
# row 0 is start: depot to customers
startMatrix = depotToClient(csvClientFile, cityNames)
startMatrix = depotToClient(csvClientFile, cityNames, wpf)
distanceMatrix=np.vstack([startMatrix,distanceMatrix])

# column 0 is finish: customers to depot
endMatrix = np.c_[np.append(0, np.ones(clientsAmount))]
endMatrix = np.c_[np.append(0, lastToDepot(csvClientFile, wpf, cityNames))]
distanceMatrix=np.append(endMatrix,distanceMatrix,axis=1)

print(distanceMatrix)

return distanceMatrix


#calculate distance in km between two coordinates
def distanceKmFromCoord(lat1, lon1, lat2, lon2):
# approximate radius of earth in km
Expand Down Expand Up @@ -614,27 +586,120 @@ def dangerousTo(clientNow, clientNext,cityNames):
return time

#makes an array with the time between depot and the first client
def depotToClient(csvClient, cityNames):
def depotToClient(csvClient, cityNames, wpf):
clients = pd.read_csv(csvClient)
depotToStart = np.zeros([len(clients.index)])
for i in range(len(clients.index)):
depotToStart[i] = round(timeBetweenPlaces('Kampenhout' , clients.iloc[i]['Place'], cityNames))
clienti = clients.iloc[i]
if clienti['ActionType'] == 1:
#load the container
timeFromDepot = 6
#drive to the first client
timeFromDepot += round(timeBetweenPlaces('Kampenhout' , clienti['Place'], cityNames))
#allocate
depotToStart[i] = timeFromDepot + getServiceTime(clienti,cityNames,wpf)
elif clienti['ActionType'] == 4:
#load the container
timeFromDepot = 6
#drive to the first client
timeFromDepot += round(timeBetweenPlaces('Kampenhout' , clienti['Place'], cityNames))
#allocate
depotToStart[i] = timeFromDepot + getServiceTime(clienti,cityNames,wpf)
elif clienti['ActionType'] == 5:
#load the container
timeFromDepot = 6
#drive to the first client
timeFromDepot += round(timeBetweenPlaces('Kampenhout' , clienti['Place'], cityNames))
#allocate
depotToStart[i] = timeFromDepot + getServiceTime(clienti,cityNames,wpf)
elif clienti['ActionType'] == 2:
#go with empty truck to first client
timeFromDepot = round(timeBetweenPlaces('Kampenhout' , clienti['Place'], cityNames))
#allocate
depotToStart[i] = timeFromDepot + getServiceTime(clienti,cityNames,wpf)
elif clienti['ActionType'] == 3 or clienti['ActionType'] == 6:
#go with empty truck to first client
timeFromDepot = round(timeBetweenPlaces('Kampenhout' , clienti['Place'], cityNames))
#allocate
depotToStart[i] = timeFromDepot + getServiceTime(clienti,cityNames,wpf)
return depotToStart

#makes an array with the time between last client and depot
# def lastToDepot(csvClient, WPF , cityNames):
# clients = pd.read_csv(csvClient)
# wpf = pd.read_csv(WPF)
# lastToDepot = np.zeros([(len(clients.index)+len(wpf.index)),2])
# for i in range((len(clients.index)+len(wpf.index))):
# if i < len(clients.index):
# lastToDepot[i,0] = clients.iloc[i]['ClientID']
# lastToDepot[i,1] = round(timeBetweenPlaces( clients.iloc[i]['Place'], 'Kampenhout' ,cityNames))
# else:
# lastToDepot[i,0] = wpf.iloc[(i-len(clients.index))]['WPFid']
# lastToDepot[i,1] = round(timeBetweenPlaces( wpf.iloc[(i-len(clients.index))]['Place'], 'Kampenhout' ,cityNames))
# return lastToDepot
def lastToDepot(csvClient, WPF , cityNames):
clients = pd.read_csv(csvClient)
lastToDepot = np.zeros([(len(clients.index))])
for i in range(len(clients.index)):
clienti = clients.iloc[i]
if clienti['ActionType'] == 2 or clienti['ActionType'] == 4 or clienti['ActionType'] == 5:
#first go to best wpf than to depot
bestWpf = closestPathWpf(clienti,'Kampenhout', WPF, cityNames)
timeToDepot = round(timeBetweenPlaces( clienti['Place'], bestWpf['Place'] ,cityNames))
#empty container
timeToDepot += 20
#go to depot
timeToDepot += round(timeBetweenPlaces(bestWpf['Place'], 'Kampenhout', cityNames))
#unload container
timeToDepot += 6
#allocate this to the matrix
lastToDepot[i] = timeToDepot
else:
#go with empty truck to depot
timeToDepot = round(timeBetweenPlaces(clienti['Place'], 'Kampenhout', cityNames))
lastToDepot[i] = timeToDepot
return lastToDepot

def getServiceTime(client, cityNames, wpf):
state = client['ActionType']
place = client['Place']
if state == 1:
#do the action at the fist client
#unloading the container
serviceTime = 12
#extras
if not pd.isnull(client['Additional']):
serviceTime += client['Additional']
return round(serviceTime)
elif state == 4:
#do the action at the fist client
#swapping the container
serviceTime = 25
#extras
if not pd.isnull(client['Additional']):
serviceTime += client['Additional']
return round(serviceTime)
elif state == 5:
#do the action at the fist client
#filling the container
serviceTime = 30
#extras
if not pd.isnull(client['Additional']):
serviceTime += client['Additional']
return round(serviceTime)
elif state == 2:
#do the action at the fist client
#loading the container
serviceTime = 12
#extras
if not pd.isnull(client['Additional']):
serviceTime += client['Additional']
return round(serviceTime)
elif state == 3 or state == 6:
#do the action at the fist client
#loading the container
serviceTime = 12
#go to wpf
bestWpf = closestPathWpf(client, place, wpf, cityNames)
serviceTime += timeBetweenPlaces(place , bestWpf['Place'], cityNames)
#empty the truck at wpf
serviceTime += 20
#go back to client
serviceTime += timeBetweenPlaces(bestWpf['Place'], place, cityNames)
#unload the container
serviceTime += 12
#extras
if not pd.isnull(client['Additional']):
serviceTime += client['Additional']
return round(serviceTime)

matrix = createDistanceMatrix('clientsTest.csv', 'belgian-cities-geocoded.csv', 'WPF.csv')
pd.DataFrame(matrix).to_csv('distanceMatrix.csv')
Expand Down

0 comments on commit 6a8edec

Please sign in to comment.