-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPC_ANN_combined_large.py
237 lines (211 loc) · 6.72 KB
/
MPC_ANN_combined_large.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 30 09:25:57 2017
@author: juno
"""
import os
import random
import time
import pandas as pd
import math
import numpy as np
import matplotlib.pyplot as plt
from itertools import permutations
from itertools import product
from itertools import chain
from copy import deepcopy
from Ship import Ship
from ContainerTerminal import ContainerTerminal
import pickle
import keras
time_horizon = 1
max_time_horizon = 6
operations_cost_hour = 200
scenario = 'equal'
ships_data_folder = '/Users/Juno/Desktop/Scriptie/Python/Ship configurations/{0}/'.format(scenario)
standardscalerpath = '/Users/Juno/Desktop/Scriptie/Python/ANN/standard_scaler_{0}.p'.format(scenario)
ANNpath = '/Users/Juno/Desktop/Scriptie/Python/ANN/{0}'.format(scenario)
shipsFilename = 'set_of_ships_1.csv'
#==============================================================================
# Don't change!
#==============================================================================
daily_operations_cost = 24*operations_cost_hour
costList = list()
findInputTime = 0
inputTime = 0
timeSequences = 0
ANN = keras.models.load_model(ANNpath)
dataset = pd.read_csv(ships_data_folder+shipsFilename)
def makeList(number):
listname = list()
for i in range(number):
listname.append(i)
return listname
def createJKUVXYZ(berths):
j = [0]
k = [0]
u = [Ship(0, "starting ship", 0, 0)]
v = [dict(zip(berths, [1]*len(berths)))]
x = [dict(zip(berths, [0]*len(berths)))]
y = [dict(zip(berths, [0]*len(berths)))]
z = [dict(zip(berths, [0]*len(berths)))]
return j,k,u,v,x,y,z
def createShips(filename):
total_path = ships_data_folder+filename
shipsDF = pd.read_csv(total_path)
ships = list()
for i in range(len(shipsDF)):
name = shipsDF['Name'][i]
arrival_time = shipsDF['Arrival Time'][i]
teu = shipsDF['TEU'][i]
waiting_cost = shipsDF['Waiting Cost'][i]
ship = Ship(i, name, arrival_time, teu, waiting_cost)
ships.append(ship)
return ships
def createTerminals(filename = 0):
terminals = list()
ship = Ship(0,'dummy', 0,0,0)
if filename == 0:
terminal = ContainerTerminal('Groningen', 'small', 2, 7, ship)
terminals.append(terminal)
return terminals
ships = createShips(shipsFilename)
terminals = createTerminals()
terminal = terminals[-1]
berths = makeList(terminal.berth_positions)
j_k, k_k, u_k, v_k, x_k, y_k, z_k = createJKUVXYZ(berths)
berthDict = {berth:[] for berth in berths}
def updateParameters(u,v):
"""
j: earliest available berth
x: finishing times of berths
y: remaining operation times of berths
z: starting times of berths
u: input ship
v: input QC sequence
"""
x = {}
y = {}
z = {}
lastX = x_k[-1]
lastY = y_k[-1]
lastZ = z_k[-1]
lastV = v_k[-1]
j = min(lastX, key = lastX.get)
k = min(lastX.values())
u.starting_time = lastX[j]
#terminal.berths[j].finishing_time = lastX[j]
for b in berths:
if b==j:
z[b]=max(lastX[b], u.arrival_time)
y[b] = u.operating_time
x[b] = z[b]+y[b]/v[b]
else:
if lastX[j]>lastZ[b]:
z[b] = lastX[j]
else:
z[b] = lastZ[b]
y[b] = lastY[b]-(z[b]-lastZ[b])*lastV[b]
x[b] = z[b]+y[b]/v[b]
u.assigned = True
u.allocated_berth = j
terminal.berths[j] = u
for berth in berths:
terminal.berths[berth].finishing_time = x[berth]
berthDict[j].append(u)
return k,j,x,y,z
def realCost():
for ship in u_k:
waiting_time = ship.finishing_time - ship.arrival_time
waiting_cost = waiting_time*ship.waiting_cost
ship.total_waiting_cost = waiting_cost
operating_cost = ship.operating_time*daily_operations_cost
ship.total_operating_cost = operating_cost
total_operating_cost = sum([ship.total_operating_cost for ship in u_k])
total_waiting_cost = sum([ship.total_waiting_cost for ship in u_k])
total_cost = total_operating_cost + total_waiting_cost
return total_cost
def findSetOfShipsToBeBerthed(time_horizon = time_horizon):
ships_unassigned = [ship for ship in ships if ship.assigned == False]
if len(ships_unassigned)<time_horizon:
time_horizon = len(ships_unassigned)
S = ships_unassigned[0:time_horizon]
return S, time_horizon
def findUandV():
S, time_horizon = findSetOfShipsToBeBerthed()
inputArray = []
currentX = deepcopy(x_k[-1])
currentV = deepcopy(v_k[-1])
V0 = [currentV[0]]
inputArray.append(V0)
for ship in S:
inputArray.append(ship.training_values)
for extra_ship in range(max_time_horizon-len(S)):
inputArray.append([0,0,0])
inputArray = list(chain(*inputArray))
currentTime = min(currentX.values())
j = min(currentX, key = currentX.get)
currentY = {}
for berth in berths:
currentY[berth]=(currentX[berth]-currentTime)*currentV[berth]
for berth in berths:
inputArray.append(currentY[berth])
standardscaler = pickle.load(open(standardscalerpath, "rb"))
X = standardscaler.transform(inputArray)
X = np.array([X])
y = ANN.predict(X)
allU = y[:,:max_time_horizon]
allU = allU[:,:time_horizon]
uIndex = allU.argmax()
u = S[uIndex]
allV = y[:,max_time_horizon:]
vIndex = allV.argmax()
v = {}
totalQC = 0
for berth in berths:
if berth!=berths[-1]:
v[berth]=vIndex+1
totalQC+=vIndex+1
else:
v[berth]=terminal.QC_number-totalQC
return u,v
def main():
starttime = time.time()
condition = True
while condition == True:
u,v = findUandV()
u_k.append(u)
v_k.append(v)
k,j,x,y,z = updateParameters(u,v)
global x_k
x_k.append(x)
global y_k
y_k.append(y)
global z_k
z_k.append(z)
global j_k
j_k.append(j)
global k_k
k_k.append(k)
if sum(ship.assigned == True for ship in ships)==len(ships):
condition=False
total_cost = realCost()
totalTime = time.time()-starttime
return total_cost, totalTime
th = 6
#totalCostDictANN = {}
#totalTimeDictANN = {}
#totalCostArray = list()
#totalTimeArray = list()
total_cost, total_time = main()
totalCostArray.append(total_cost)
totalTimeArray.append(total_time)
totalCost = np.mean(totalCostArray)
totalTime = np.mean(totalTimeArray)
totalCostDictANN[th]=totalCost
totalTimeDictANN[th]=totalTime
totalCostArray.clear()
totalTimeArray.clear()
#pickle.dump(totalCostDictANN, open('ANNcost-{0}.p'.format(scenario),"wb"))
#pickle.dump(totalTimeDictANN, open('ANNtime-{0}.p'.format(scenario),"wb"))