-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_vehicle.py
57 lines (46 loc) · 1.72 KB
/
simple_vehicle.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
__author__ = 'nick'
# Program that simulates the movement of a simple vehicle around a 2d area.
import random
import simpy
class Vehicle:
env = 0
name = ""
timeout = 0
def __init__(self, env, name):
self.env = env
self.name = name
# self.timeout_ = random.randint(1,10)
print("Created instance of vehicle with name {0}".format(self.name))
self.action = env.process(self.run())
# yield self.env_.process(self.honk(self.timeout_))
def run(self):
# Main vehicle loop.
while True:
print('Start parking and charging at %d' % self.env.now)
charge_duration = 5
# We yield the process that process() returns
# to wait for it to finish
# We may get interrupted while charging the battery
try:
yield self.env.process(self.charge(charge_duration))
except simpy.Interrupt:
# When we received an interrupt, we stop charing and
# switch to the "driving" state
print('Was interrupted. Hope, the battery is full enough ...')
# The charge process has finished and
# we can start driving again.
print('Start driving at %d' % self.env.now)
trip_duration = 2
yield self.env.timeout(trip_duration)
def charge(self, duration):
yield self.env.timeout(duration)
def honk(self, time):
yield self.env_.timeout(time)
print("Vehicle {0} says HONK!!!".format(self.name))
def driver(env, car):
yield env.timeout(3)
car.action.interrupt()
env = simpy.Environment()
veh1 = Vehicle(env, "Vehicle1")
env.process(driver(env, veh1))
env.run(until=15)