-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShip.py
executable file
·176 lines (155 loc) · 5.49 KB
/
Ship.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 20 22:01:39 2016
@author: Juno
"""
import numpy as np
import pandas as pd
import random
import math
#==============================================================================
# PARAMETERS
# load_type: load_scenario
# number_of_ships: number of ships to create
# path of save file
# name of save file
# m = mean of ship interarrival time
# v = standard deviation of ship interarrival time
# TEU_UB: upper bound of number of containers on ship in scenario
# TEU_LB: lower bound of number of containers on ship in scenario
#==============================================================================
load_type = 1 #reduced = 1, normal = 2, heavy = 3
number_of_ships = 50
nameOfFile = 'test.csv'
path = '/Users/Juno/Desktop/Scriptie/Python/Ship configurations/'
mapName = 'equal/'
pathOfNames = '/Users/Juno/Desktop/Scriptie/Python/Ship configurations/Ship_names.csv'
waiting_cost_mean = 3600
waiting_cost_stdev = 300
#==============================================================================
# CODE
#==============================================================================
if load_type == 1:
m = 6
v = 0.6
TEU_UB=3000
TEU_LB=1000
elif load_type == 2:
m = 5.5
v = 0.5
TEU_UB = 5000
TEU_LB = 3000
else:
m = 5.0
v = 0.4
TEU_UB = 10000
TEU_LB = 5000
m = m/24
v = v/24
names = pd.read_csv(pathOfNames)
pathOfShipFile = '{0}{1}'.format(path, nameOfFile)
class Ship(object):
""" Class used to generate a ship with attributes:
name: name of the ship
load type: type of load
arrival_time: arrival time of the ship in days
TEU: number of TEUs the ship is transporting
operation time: the number of QC minutes it takes to unload the ship"""
def __init__(self, ship_number, name, arrival_time, TEU, waiting_cost = 3600):
self.ship_number = ship_number
self.name = name
self.arrival_time = arrival_time
self.TEU = TEU
operation = TEU*3/60/24
self.operating_time = round(operation,2)
self.waiting_cost = waiting_cost
self.allocated_berth = "-1"
self.assigned = False
self.starting_time = 0
self.finishing_time = 0
self.training_values = [self.arrival_time, self.TEU, self.waiting_cost]
self.total_operating_cost = 0
self.total_waiting_cost = 0
def __str__(self):
return "Name:" +self.name
def berth_number(self, berth_number):
""" The berth number the vessel is assigned to """
self.allocated_berth = berth_number
def show(self):
print('Ship number: ', self.ship_number)
print('Name: ', self.name)
print('Arrival time: ', self.arrival_time)
print('TEU: ', self.TEU)
print('Operations time (QC days): ', self.operation_time)
if self.allocated_berth == "-1":
print("Not assigned to a berth yet")
else:
print("Berth Number: ", self.allocated_berth)
def arrivalTime(oldTime):
mu = math.log(m**2/math.sqrt(v+m**2))
sigma = math.sqrt(math.log(v/(m**2)+1))
interArrivalTime = np.random.lognormal(mu,sigma)
arrivalTime = oldTime+interArrivalTime
arrivalTime = round(arrivalTime,2)
return arrivalTime
def waitingCost(scenario):
if scenario == 1:
cost = 3600
return cost
elif scenario == 2:
waiting_cost_stdev = 360
elif scenario == 3:
waiting_cost_stdev = 900
cost = int(random.gauss(waiting_cost_mean, waiting_cost_stdev))
return cost
def numberOfTEUs():
TEU = random.randint(TEU_LB, TEU_UB)
return TEU
def pickName():
name = names['Names'][random.randint(0,len(names)-1)]
return name
def createShips():
ships1 = list()
ships2 = list()
ships3 = list()
oldArrivalTime = 0
for i in range(number_of_ships):
name = pickName()
TEUs = numberOfTEUs()
arrival_time = arrivalTime(oldArrivalTime)
oldArrivalTime = arrival_time
waiting_cost_1 = waitingCost(1)
waiting_cost_2 = waitingCost(2)
waiting_cost_3 = waitingCost(3)
ships1.append(Ship(i, name, arrival_time, TEUs, waiting_cost_1))
ships2.append(Ship(i, name, arrival_time, TEUs, waiting_cost_2))
ships3.append(Ship(i, name, arrival_time, TEUs, waiting_cost_3))
return ships1, ships2, ships3
def saveShipsToCSV(ships, mapName,filename):
shipNames = list()
shipsArrivalTime = list()
shipsTEU = list()
shipsWaitingcost = list()
for ship in ships:
shipNames.append(ship.name)
shipsArrivalTime.append(ship.arrival_time)
shipsTEU.append(ship.TEU)
shipsWaitingcost.append(ship.waiting_cost)
dataframe = pd.DataFrame(
{'Name': shipNames,
'Arrival Time': shipsArrivalTime,
'TEU': shipsTEU,
'Waiting Cost': shipsWaitingcost
})
dataframe = dataframe[['Name', 'Arrival Time', 'TEU', 'Waiting Cost']]
dataframe.to_csv(path+mapName+filename , index = False)
def play(set_number):
filename = 'set_of_ships_{0}.csv'.format(set_number)
ships1, ships2, ships3 = createShips()
saveShipsToCSV(ships1, 'test_equal/' ,filename)
saveShipsToCSV(ships2, 'test_gaussian/',filename)
saveShipsToCSV(ships3, 'test_random/',filename)
return ships1, ships2, ships3
if __name__ == "__main__":
ships1, ships2, ships3 = play(1)