forked from myrlund/salabim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGas station.py
78 lines (59 loc) · 2.34 KB
/
Gas station.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
import salabim as sim
GAS_STATION_SIZE = 200. # liters
THRESHOLD = 25. # Threshold for calling the tank truck (in %)
FUEL_TANK_SIZE = 50. # liters
# Min/max levels of fuel tanks (in liters)
FUEL_TANK_LEVEL = sim.Uniform(5, 25)
REFUELING_SPEED = 2. # liters / second
TANK_TRUCK_TIME = 300. # Seconds it takes the tank truck to arrive
T_INTER = sim.Uniform(10, 100) # Create a car every [min, max] seconds
SIM_TIME = 200000 # Simulation time in seconds
class Car(sim.Component):
'''
A car arrives at the gas station for refueling.
It requests one of the gas station's fuel pumps and tries to get the
desired amount of gas from it. If the stations reservoir is
depleted, the car has to wait for the tank truck to arrive.
'''
def process(self):
fuel_tank_level = int(FUEL_TANK_LEVEL.sample())
yield self.request(gas_station)
liters_required = FUEL_TANK_SIZE - fuel_tank_level
if (fuel_pump.available_quantity() - liters_required) / fuel_pump.capacity() * 100 < THRESHOLD:
if tank_truck.ispassive():
tank_truck.activate()
yield self.request((fuel_pump, liters_required))
yield self.hold(liters_required / REFUELING_SPEED)
class TankTruck(sim.Component):
'''
Periodically check the level of the *fuel_pump* and call the tank
truck if the level falls below a threshold.
'''
def process(self):
while True:
yield self.passivate()
yield self.hold(TANK_TRUCK_TIME)
fuel_pump.release()
class CarGenerator(sim.Component):
'''
Generate new cars that arrive at the gas station.
'''
def process(self):
while True:
yield self.hold(T_INTER.sample())
Car()
# Setup and start the simulation
env = sim.Environment(trace=False)
print('Gas Station refuelling')
# Create environment and start processes
gas_station = sim.Resource('gas_station', 2)
fuel_pump = sim.Resource(
'fuel_pump', capacity=GAS_STATION_SIZE, anonymous=True)
tank_truck = TankTruck()
CarGenerator()
env.run(SIM_TIME)
fuel_pump.capacity.print_histogram()
fuel_pump.claimed_quantity.print_histogram()
fuel_pump.available_quantity.print_histogram()
gas_station.requesters().length.print_histogram()
gas_station.requesters().length_of_stay.print_histogram(30, 0, 10)