Oct-18-2018, 12:19 PM
I have two situations. In one situation the simulation is executed based on random variables and in the second situation the random variables are obtained from a txt document.
This is the first simulation:
this is the second simulation:
Thanks in advance,
This is the first simulation:
import random
import simpy
class TerminalSimulator:
container_information = []
# You must set the values of these variables during the execution of the simulation:
nr_ships = 0 # the number of ships that arrived
nr_containers = 0 # the number of containers that arrived
nr_containers_inbound = 0 # the number of containers that were touched by a crane for unloading from the ship
nr_containers_outbound = 0 # the number of containers that were touched by a crane for loading onto a truck
total_waiting_storage = 0 # the sum of the simlation times (durations) that containters spent in storage
total_waiting_inbound = 0 # the sum of the simulation times (durations) that containers spent waiting to be unloaded from the ship
total_waiting_outbound = 0 # the sum of the simulation times (durations) that containers spent waiting to be loaded onto a truck
def __init__(self):
self.container_information = []
def container_generator(self, env, inbound_crane, outbound_crane):
while True:
self.nr_ships += 1
Containers = int(random.uniform(1, 100))
a = 0
while a < Containers:
c = self.container(env, inbound_crane, outbound_crane)
env.process(c)
a += 1
interarrivalTime = random.expovariate(1/12)
yield env.timeout(interarrivalTime)
def container (self, env, inbound_crane, outbound_crane):
arrival_time = env.now
icr = inbound_crane.request()
yield icr
start_processing_time1 = env.now
service_time1 = random.uniform(0.18, 0.25)
yield env.timeout(service_time1)
complete_processing_time1 = env.now
inbound_crane.release(icr)
waiting_inbound = start_processing_time1 - arrival_time
self.total_waiting_inbound += waiting_inbound
self.total_waiting_inbound = round(self.total_waiting_inbound,2)
self.nr_containers += 1
self.nr_containers_inbound += 1
storage = random.uniform(1, 2)
yield env.timeout(storage)
complete_storage = env.now
storage_time = complete_storage - complete_processing_time1
self.total_waiting_storage += storage_time
self.total_waiting_storage = round(self.total_waiting_storage,2)
ocr = outbound_crane.request()
yield ocr
start_processing_time2 = env.now
service_time2 = random.uniform(0.16, 0.19)
yield env.timeout(service_time2)
outbound_crane.release(ocr)
waiting_outbound = start_processing_time2 - complete_storage
self.total_waiting_outbound += waiting_outbound
self.total_waiting_outbound = round(self.total_waiting_outbound,2)
self.nr_containers_outbound += 1
def simulate(self, seed=1):
random.seed()
env = simpy.Environment()
inbound_crane = simpy.Resource(env, capacity=2) #create the inbound crane
outbound_crane = simpy.Resource(env, capacity=2) #create the outbound crane
env.process(self.container_generator(env, inbound_crane, outbound_crane))
env.run(1500)
return env
ts = TerminalSimulator()
env = ts.simulate(2000)
(ts.nr_ships, ts.nr_containers, ts.total_waiting_storage, ts.total_waiting_inbound, ts.total_waiting_outbound)with the following output of the first simulation:(ts.nr_ships, ts.nr_containers, ts.total_waiting_storage, ts.total_waiting_inbound, ts.total_waiting_outbound) Out[103]: (134, 7001, 10532.03, 41694.63, 431.18)My second simulation based on variables obtained from a txt should give about the same output as the first simulation. However when computing the total_waiting_outbound and total_waiting_inbound something is going wrong.
this is the second simulation:
import simpy
class TerminalSimulator:
def __init__(self):
self.nr_containers = 0 # the number of containers that arrived
self.nr_containers_inbound = 0 # the number of containers that were touched by a crane for unloading from the ship
self.nr_containers_outbound = 0 # the number of containers that were touched by a crane for loading onto a truck
self.total_waiting_storage = 0 # the sum of the simlation times (durations) that containters spent in storage
self.total_waiting_inbound = 0 # the sum of the simulation times (durations) that containers spent waiting to be unloaded from the ship
self.total_waiting_outbound = 0 # the sum of the simulation times (durations) that containers spent waiting to be loaded onto a truck
# You do not need to set the value of this variable. It is just here as a recommendation.
self.containers = [] # the list of containers that is loaded from the file,
# note that there is no strict requirements on how the containers are loaded from the file,
# you can make your own choice.
# We used a list of dictionaries again, as shown in the example output in the assignment description.
# If you use it, you need to set it to an empty list in the constructor, to prevent unwanted side-effects, i.e.:
# self.containers = []
def container (self, env, inbound_crane, outbound_crane):
File = open('containers.txt', 'r')
next(File)
container = [line.split(',') for line in File.readlines()]
for i in container:
i[-1]=i[-1].strip()
self.containers.append({
'ship_id': i[0],
'container_id': i[1],
'arrival_time': i[2],
'unload_time': i[3],
'load_time': i[4],
'storage_time': i[5]})
self.nr_containers += 1
arrival_time = float(i[2])
icr = inbound_crane.request()
yield icr
start_processing_time1 = env.now
service_time1 = float(i[3])
yield env.timeout(service_time1)
complete_processing_time1 = env.now
inbound_crane.release(icr)
waiting_inbound = start_processing_time1 - arrival_time
self.total_waiting_inbound += waiting_inbound
self.nr_containers_inbound += 1
storage = float(i[5])
yield env.timeout(storage)
complete_storage = env.now
storage_time = complete_storage - complete_processing_time1
self.total_waiting_storage += storage_time
ocr = outbound_crane.request()
yield ocr
start_processing_time2 = env.now
service_time2 = float(i[4])
yield env.timeout(service_time2)
outbound_crane.release(ocr)
waiting_outbound = start_processing_time2 - complete_storage
self.total_waiting_outbound += waiting_outbound
self.nr_containers_outbound += 1
def simulate(self, seed=1):
env = simpy.Environment()
inbound_crane = simpy.Resource(env, capacity=2) #create the inbound crane
outbound_crane = simpy.Resource(env, capacity=2) #create the outbound crane
env.process(self.container(env, inbound_crane, outbound_crane))
env.run()
return env
ts = TerminalSimulator()
env = ts.simulate()
(ts.nr_containers, ts.total_waiting_storage, ts.total_waiting_inbound, ts.total_waiting_outbound)
ts.containers[0]the output of simulation 2:Out[104]:
{'ship_id': '1',
'container_id': '1',
'arrival_time': '0',
'unload_time': '0.23',
'load_time': '0.21',
'storage_time': '1.31'}
(ts.nr_containers, ts.total_waiting_storage, ts.total_waiting_inbound, ts.total_waiting_outbound)
Out[105]: (6425, 9668.259999999964, 34833051.33999741, 0.0)as you can see in the output of simulation 2 the total_waiting_inbound and total_waiting_outbound are not the same as the output of simulation 1. They don't need to be exact the same however now they are total different. I know simulation 1 is correct. So what am I doing wrong in simulation 2?Thanks in advance,
