-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-Project_visualization.py
More file actions
284 lines (237 loc) · 9.34 KB
/
3-Project_visualization.py
File metadata and controls
284 lines (237 loc) · 9.34 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
##### Markov simulation project: simulation of customers walking around in a supermarket
##### Part 3: Visualization (under construction)
##### 13th of June 2022
import numpy as np
import pandas as pd
from faker import Faker
import random
import time
import cv2
TILE_SIZE = 32
MARKET = """
##################
##..............##
##..QA..WP..DS..##
##..QA..WP..DS..##
##..QA..WP..DS..##
##..QA..WP..DS..##
##..QA..WP..DS..##
##...............#
##..C#..C#..C#...#
##..##..##..##...#
##...............#
##############GG##
""".strip()
fake = Faker()
### Create class supermarket
class Supermarket:
"""Manages multiple Customer instances (adds new ones, moves them) over a time period of 07:00 to 22:00. Output in customers_overview.csv
"""
def __init__(self):
"""all initial attributes of the supermarket """
self.customers = [] # List of customers at the current time in the market
self.minutes = 1318 # Shop opens at 07:00 (420 minutes after 00:00)
self.closing = 1320 # SHop closes at 22:00 (1320 minutes after 00:00)
self.df_customers = pd.DataFrame(columns = ["minutes", "name", "location"])
def __repr__(self):
return ''
def get_time(self):
""" returns the current time in HH:MM format.
"""
self.time = time.strftime("%H:%M", time.gmtime(self.minutes * 60))
return self.time
def print_customers(self):
"""print all customers with the current time, name and location in CSV format (Final output)
"""
self.df_customers = self.df_customers.set_index("minutes")
#self.df_customers.to_csv("customers_overview.csv")
def next_minute(self):
"""propagates all customers to the next state as long as the supermarket is open
"""
self.minutes = self.minutes + 1
churned =[] # Collects all customers who were at the checkout at the last time step
for i in self.customers:
if i.is_active() == True: # Is the customer not in checkout?
if self.minutes == self.closing: # Is the market closing at this time step?
i.state = "checkout"
else:
i.change_state() # If the customer was not in checkout and the market is not closing, move according to prob matrix
update = {"minutes": self.get_time(), "name": i.name, "location": i.state}
self.df_customers = self.df_customers.append(update, ignore_index=True)
else:
churned.append(i)
for i in churned:
self.remove_exitsting_customers(i) # remove all customers who were at checkout last time step
if self.minutes != self.closing:
self.add_new_customers()
self.next_minute()
else:
self.print_customers() # If the market is closing at this time step, don't add customers but save the data into an csv
def add_new_customers(self):
"""randomly creates new customers with the start location entrance
"""
for i in range(0, random.randint(0,2)):
new = {"minutes": self.get_time(), "name": fake.name(), "location": "entrance"}
self.customers.append(Customer(new["name"], new["location"], marketmap=market))
self.df_customers = self.df_customers.append(new, ignore_index=True)
return self.df_customers
def remove_exitsting_customers(self, customer):
"""removes every customer that is not active any more (who was at the checkout last time step)
"""
self.customers.remove(customer)
def open(self):
""" Opens the supermarket at 07:00 and starts with initial number of customers
"""
self.add_new_customers()
self.next_minute()
### Create Class customer
class Customer:
"""Class of customers that have an id and can move to other sections with a probability according to the transition matrix
"""
def __init__(self, name, state, marketmap):
self.marketmap = marketmap
self.name = name
self.state = state
self.transition_probs = pd.read_csv("transition_matrix_ilona_pascal.csv", index_col="location")
self.tiles = tiles
self.avatar = self.extract_tile(random.randint(4,8),15)
self.row = 11
self.col = 15
def __repr__(self):
return f"The customer {self.name} is currently at the location {self.state}"
def change_state(self):
"""Customer moves with a certain probability to the next location
"""
if self.is_active() == True:
self.state = np.random.choice(a=["checkout", "dairy", "drinks", "fruit", "spices", "entrance"], p=self.transition_probs.loc[self.state])
return f"The customer {self.name} is currently at the location {self.state}"
else:
return f"The customer {self.name} has already left"
def is_active(self):
"""Returns True if the customer has not reached the checkout yet
"""
if self.state == "checkout":
return False
else:
return True
def draw(self, frame):
x = self.col * TILE_SIZE
y = self.row * TILE_SIZE
frame[y:y+TILE_SIZE, x:x+TILE_SIZE] = self.avatar
def extract_tile(self, row, col):
"""extract a tile array from the tiles image"""
y = row*TILE_SIZE
x = col*TILE_SIZE
return self.tiles[y:y+TILE_SIZE, x:x+TILE_SIZE]
def move(self):
if self.state == "entrance":
new_row = 5
new_col = 8
elif self.state == "fruit":
new_row = random.randint(2,6)
new_col = 11
elif self.state == "dairy":
new_row = random.randint(2,6)
new_col = 14
elif self.state == "spices":
new_row = random.randint(2,6)
new_col = 5
elif self.state == "drinks":
new_row = random.randint(2,6)
new_col = 3
else:
new_row = 8
new_col = random.choice([3, 7, 11])
if self.marketmap.contents[new_row][new_col] == '.':
self.col = new_col
self.row = new_row
else:
self.col = self.col
self.row = self.row
class SupermarketMap:
"""Visualizes the supermarket background"""
def __init__(self, layout, tiles):
"""
layout : a string with each character representing a tile
tiles : a numpy array containing all the tile images
"""
self.tiles = tiles
# split the layout string into a two dimensional matrix
self.contents = [list(row) for row in layout.split("\n")]
self.ncols = len(self.contents[0])
self.nrows = len(self.contents)
self.image = np.zeros(
(self.nrows*TILE_SIZE, self.ncols*TILE_SIZE, 3), dtype=np.uint8
)
self.prepare_map()
def extract_tile(self, row, col):
"""extract a tile array from the tiles image"""
y = row*TILE_SIZE
x = col*TILE_SIZE
return self.tiles[y:y+TILE_SIZE, x:x+TILE_SIZE]
def get_tile(self, char):
"""returns the array for a given tile character"""
if char == "#":
return self.extract_tile(0, 0)
elif char == "G":
return self.extract_tile(7, 3)
elif char == "C":
return self.extract_tile(2, 8)
elif char == "S":
return self.extract_tile(1, 5)
elif char == "D":
return self.extract_tile(1, 6)
elif char == "P":
return self.extract_tile(1, 7)
elif char == "W":
return self.extract_tile(1, 8)
elif char == "A":
return self.extract_tile(1, 9)
elif char == "Q":
return self.extract_tile(1,10)
else:
return self.extract_tile(1, 2)
def prepare_map(self):
"""prepares the entire image as a big numpy array"""
for row, line in enumerate(self.contents):
for col, char in enumerate(line):
bm = self.get_tile(char)
y = row*TILE_SIZE
x = col*TILE_SIZE
self.image[y:y+TILE_SIZE, x:x+TILE_SIZE] = bm
def draw(self, frame):
"""
draws the image into a frame
"""
frame[0:self.image.shape[0], 0:self.image.shape[1]] = self.image
def write_image(self, filename):
"""writes the image into a file"""
cv2.imwrite(filename, self.image)
if __name__ == "__main__":
background = np.zeros((500, 700, 3), np.uint8)
tiles = cv2.imread("tiles.png")
market = SupermarketMap(MARKET, tiles)
lidl = Supermarket()
lidl.add_new_customers()
while True:
time.sleep(1)
churned = []
frame = background.copy()
market.draw(frame)
for i in lidl.customers:
i.draw(frame)
i.change_state()
print(i.state)
i.move()
if i.is_active() == False:
print("churned")
churned.append(i)
lidl.minutes +=1
lidl.add_new_customers()
# https://www.ascii-code.com/
key = cv2.waitKey(1)
if key == 113: # 'q' key
break
cv2.imshow("frame", frame)
cv2.destroyAllWindows()
market.write_image("supermarket.png")