/
main.py
executable file
·149 lines (121 loc) · 3.82 KB
/
main.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
#!/usr/bin/python
'''
main.py
author: Daniel Henderson, Jier Chen
ALL RIGHT RESERVED
'''
#System Support
import threading
import Queue
import grid_info
#Engine Part
from engine import Engine
from terrain import Terrain, Traffic_light
from generator import Generator, Human_source
#random number generator
import rand
import time
#for gaussian cdf
#from scipy.stats import norm
#UI module
# macro to switch whether to use user interface or not
USE_UI = True
if USE_UI :
import ui_module
end_condition = False
terminate = False
result = 0
class Ui(threading.Thread):
UI = None
def __init__(self, grid_size, tile_size, queue, door_coords):
self.grid_size = grid_size
self.tile_size = tile_size
self.agent_queue = queue
self.door_coords = door_coords
self.UI = ui_module.UserInterface(self.grid_size, self.tile_size, 'tech_square.jpg', self.door_coords)
super(Ui, self).__init__()
#threading.Thread.__init__(self)
def run(self):
global end_condition
agents = []
while not self.UI.checkEvents():
if agents != -1:
agents = self.agent_queue.get()
#if end_condition : break
#print "Queue size: %d"%self.agent_queue.qsize()
#print "End Condition: %s"%end_condition
#print "Agent List:"
#print agents
if agents == -1:
self.UI.drawScreen([])
else:
self.UI.drawScreen(agents)
else:
self.UI.drawScreen([])
end_condition = True
try : rubbish = self.agent_queue.get_nowait()
except : pass
self.UI.quit()
class Simulator(threading.Thread):
engine = None
agent_queue = None
def __init__(self, queue, door_coords):
self.agent_queue = queue
source = Human_source(1500)
event_list = [
Generator('0_0', door_coords[0][0], source, 3),
Generator('0_1', door_coords[0][1], source, 3),
Generator('0_2', door_coords[0][2], source, 3),
Generator('0_3', door_coords[0][3], source, 3),
Generator('1_0', door_coords[1][0], source, 3),
Generator('1_1', door_coords[1][1], source, 3),
Generator('2_0', door_coords[2][0], source, 3),
Generator('3_0', door_coords[3][0], source, 3),
Generator('4_0', door_coords[4][0], source, 3),
Generator('5_0', door_coords[5][0], source, 3)
]
for area, timming, initial_light in grid_info.traffic_lights :
green,red = timming
green,red = green*grid_info.FPS, red*grid_info.FPS
light = Traffic_light(area, (green,red), initial_light)
event_list.append(light)
self.engine = Engine(terrain = Terrain("grid_bits.txt"), initial_event_list = event_list)
super(Simulator, self).__init__()
#threading.Thread.__init__(self)
def run(self):
global end_condition
global result
print 'Starts to run simulation!'
while not self.engine.end_condition :
self.engine.step_simulate()
if end_condition:
break
if USE_UI:
self.agent_queue.put(self.engine.flush_agent())
result = self.engine.time_elapse
end_condition = True
try : self.agent_queue.put_nowait(-1)
except : pass
#To enable engine pump agent information into agent_queue, uncomment line above
#Data structure that engine pumped to agent is
#((coordinate X, coordinate Y), agent ID, agent status)
# agent status can be two "waiting" and "blocking"
# "waiting" means agent is waiting the CD for next move
# "blocking" means agent is able to move but blocked at current position, needs some one beside him move to release available grid to him
def run_simulation(door_coords=grid_info.door_coords, seed=int(time.time())):
global result
agent_queue = Queue.Queue()
rand.srand(seed)
#gen_list = [ Generator(0, (201, 295, 1, 0), 5, 3) ]
simulator = Simulator(agent_queue, door_coords)
simulator.start()
if USE_UI:
ui = Ui(grid_info.grid_size, grid_info.tile_size, agent_queue, door_coords)
ui.start()
ui.join()
global terminate
terminate = True
simulator.join()
return result
if __name__ == '__main__':
print 'total time',run_simulation()