-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.py
90 lines (63 loc) · 2.23 KB
/
env.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
from product import SugarDonut
from machine import Machine
import simpy
# SimPy goes here
class DonutShop:
def __init__(self, name, creation_interval):
self.env = simpy.Environment()
self.stop = False
self.name = name
self.machines = self.initialize_machines()
self.profit = 0
self.donuts_created = 0
self.creation_interval = creation_interval
self.observation_space = []
self.actions_space = []
def initialize_machines(self):
machines = {}
machines[0] = Machine(self.env, 'Mixer', 3)
machines[1] = Machine(self.env, 'Donut Cutter', 1)
machines[2] = Machine(self.env, 'Deep Fryer', 5)
machines[3] = Machine(self.env, 'Glazing Station', 2)
return machines
# Initialize environment
def create(self):
# Start working process for all machines
for machine in self.machines.values():
self.env.process(machine.run())
# Run process for a specified time
self.env.process(self.create_donuts())
# Start new episode of environment
def reset(self):
self.profit = 0
self.donuts_created = 0
self.env = simpy.Environment()
# Reset Queues of functions
for machine in self.machines.values():
machines.reset(self.env)
self.create()
# Run environment until next event
def step(self, action):
if self.env.peek() < float('inf'):
self.env.step()
# Start creation process of donuts and add profit for finished donut
def run_donut(self, donut):
self.donuts_created += 1
yield self.env.process(donut.startCreation())
self.profit += donut.cost
print('Time: ' + str(self.env.now) + '; Donut done; Profit: ' + str(self.profit))
# Unlimited loop to create donuts after a specified wait time
def create_donuts(self):
while True:
#create instance of activity generator
donut = SugarDonut(self.env, self.machines)
#run the activity generator for this donut
self.env.process(self.run_donut(donut))
#freeze till time next donut enters queue has passed
yield self.env.timeout(self.creation_interval)
# Start an environment run with a fixed creation interval
def start_shift(self, length):
self.create()
self.env.run(until=length)
print('Donuts Created: ' + str(self.donuts_created))
print('End Profit: ' + str(self.profit))