-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
166 lines (146 loc) · 6.87 KB
/
model.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
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
import numpy as np
from queue_models import *
import sys
import progressbar
SECOND_PER_PERSON = 5
NUM_OF_WINDOW = 2
NUM_OF_WORKER = 5
item_list = []
with open('Data.csv') as fin:
for l in fin:
arr = l.split(',')
item_list.append(Item(arr[0], int(arr[1]), float(arr[2])))
pops = np.array([item.popularity for item in item_list])
probs = pops / np.sum(pops)
item_num_distribution = np.array([73, 19, 5, 3])
lambdas = []
def generator(model):
alpha = 1 / SECOND_PER_PERSON
beta = np.log(2) / 15
items_per_person = np.sum(item_num_distribution *
np.arange(1, (item_num_distribution.shape[0] + 1))) / np.sum(item_num_distribution)
accumulate = 0
person_num = 0
while True:
# lam = alpha * np.exp(-beta * (model.people_count - model.people_served))
lam = alpha
lambdas.append(lam)
accumulate += np.random.poisson(lam)
l = []
while accumulate >= 1:
accumulate -= 1
person_num += 1
p = Person(person_num, [])
for _ in range(np.random.poisson(items_per_person - 1) + 1):
p.items.append(np.random.choice(item_list, p=probs))
l.append(p)
yield l
order_queue_length = []
total_population_vs_time = []
wait_time_per_item = []
# people_served_total = [], []
def run_model(model, simulation_time):
global total_population_vs_time, wait_time_per_item
print(type(model).__name__)
total_population_vs_time.append([])
order_queue_length.append([])
wait_time_per_item += [[], []]
wait_time = [0 for _ in range(int(simulation_time / 60))]
item_num = [0 for _ in range(int(simulation_time / 60))]
# total_time = []
for t in range(simulation_time):
# print(model.print_stat())
# if time_scheduler.time % 60 == 0:
# input()
total_population_vs_time[-1].append(model.people_count - model.people_served)
order_queue_length[-1].append(model.queue_length())
time_scheduler.time_pass()
condition_scheduler.time_pass()
total_population_vs_time[-1].append(model.people_count - model.people_served)
order_queue_length[-1].append(model.queue_length())
print(model.print_stat())
for p in Person.people_list:
# if p.served:
# total_time.append(p.total_wait_time.time)
if p.served and len(p.items) == 1:
index = int((p.enqueued_time - 1) / 60)
wait_time[index] += p.total_wait_time.time
item_num[index] += 1
for i in range(int(simulation_time / 60)):
t, n = wait_time[i], item_num[i]
if n != 0:
wait_time_per_item[-2].append(t / n)
wait_time_per_item[-1].append(i + 1)
# if len(people_served_total[0]) == len(people_served_total[1]):
# people_served_total[0].append(sum(total_time) / len(total_time))
# else:
# people_served_total[1].append(sum(total_time) / len(total_time))
print('total_people_arrived:\t{0}'.format(model.people_count))
print('total_people_served:\t{0}'.format(model.people_served))
print('total_order_placed:\t{0}'.format(model.workshop.order_count))
print('total_order_finished:\t{0}'.format(model.workshop.order_finished))
total_wait_time = np.array([p.total_wait_time.time for p in Person.people_list if p.served])
print('total_wait_time_avg:\t{0}\ttotal_wait_time_std:\t{1}'.format(round(np.average(total_wait_time), 2),
round(np.std(total_wait_time), 2)))
drink_wait_time = np.array([p.drink_wait_time.time for p in Person.people_list if p.served])
print('drink_wait_time_avg:\t{0}\tdrink_wait_time_std:\t{1}'.format(round(np.average(drink_wait_time), 2),
round(np.std(drink_wait_time), 2)))
order_wait_time = np.array([p.order_wait_time.time for p in Person.people_list if p.served])
print('order_wait_time_avg:\t{0}\torder_wait_time_std:\t{1}'.format(round(np.average(order_wait_time), 2),
round(np.std(order_wait_time), 2)))
order_cost_time = np.array([o.wait_time.time for o in Order.order_list if o.ready])
print('order_cost_time_avg:\t{0}\torder_cost_time_std:\t{1}'.format(round(np.average(order_cost_time), 2),
round(np.std(order_cost_time), 2)))
total_item_num = np.sum(np.array([len(p.items) for p in Person.people_list if p.served]))
print('average_time_per_item_ordered:\t{0}'.format(round(np.sum(total_wait_time) / total_item_num, 2)))
assert total_item_num <= np.sum(np.array([len(o.items) for o in Order.order_list if o.ready]))
print('average_lambda:\t{0}'.format(round(np.average(lambdas), 2)))
print()
if __name__ == '__main__':
assert len(sys.argv) == 2, 'illegal arguments'
simulation_time = int(sys.argv[1])
models = [OneLineModel,
OneLinePickupModel,
MultiLineModel,
MultiLinePickupModel]
for m in models:
time_scheduler.reset()
condition_scheduler.reset()
Person.reset()
Order.reset()
lambdas.clear()
run_model(m(window_num=NUM_OF_WINDOW, worker_num=NUM_OF_WORKER, person_generator=generator),
simulation_time)
f_name = 'Plots/total population vs time.txt'
with open(f_name, 'w') as f:
f.write('\n'.join([' '.join(map(str, s)) for s in total_population_vs_time]))
print('{0} saved'.format(f_name))
f_name = 'Plots/wait time per item.txt'
with open(f_name, 'w') as f:
f.write('\n'.join([' '.join(map(str, s)) for s in wait_time_per_item]))
print('{0} saved'.format(f_name))
f_name = 'Plots/queue length vs time.txt'
with open(f_name, 'w') as f:
f.write('\n'.join([' '.join(map(str, s)) for s in order_queue_length]))
print('{0} saved'.format(f_name))
# with progressbar.ProgressBar(max_value=50) as bar:
# for w in range(1, 51, 1):
# time_scheduler.reset()
# condition_scheduler.reset()
# Person.reset()
# Order.reset()
# lambdas.clear()
# run_model(OneLinePickupModel(window_num=w, worker_num=100, person_generator=generator),
# simulation_time)
# time_scheduler.reset()
# condition_scheduler.reset()
# Person.reset()
# Order.reset()
# lambdas.clear()
# run_model(MultiLinePickupModel(window_num=w, worker_num=100, person_generator=generator),
# simulation_time)
# bar.update(w - 1)
# f_name = 'Plots/avg time vs window 10 second.txt'
# with open(f_name, 'w') as f:
# f.write('\n'.join([' '.join(map(str, s)) for s in people_served_total]))
# print('{0} saved'.format(f_name))