-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexperiment.py
53 lines (51 loc) · 1.72 KB
/
experiment.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
import csv
import sys
import os
from model.world import World
from settings import setDict
path = os.path.join(os.getcwd(), "data")
START_DENSITY = 0.005
TERMINATE_DENSITY = 0.9
STEP = 0.005
ITERATION = 10
CAR_NUM = 15
START_COLLECT = 10000
checkLockTime = 10000
zero = [0 for i in range(10)]
for car in range(15, 220, 5):
print('Car number is {}'.format(car))
i = 0
while i < ITERATION:
print('exp{}'.format(i + 1))
world = World(exp=True)
world.load()
world.carsNumber = car
expList = []
time = 0
zeroList = []
while time < setDict["collecting_time"] + 1:
world.onTick(0.2)
if len(world.cars) == 0:
print()
break
avgSpeed, avgDensity, flow = world.systemInfo()
zeroList.append(0 if avgSpeed == 0.0 else 1)
if time > 100 and zeroList[-10:] == zero:
print()
break
if time > START_COLLECT:
expList.append({'time': time, 'avgSpeed': avgSpeed,
'flow': flow, 'avgDensity': avgDensity})
sys.stdout.write("\r%d%%" % ((time - START_COLLECT) / 100))
sys.stdout.flush()
time += 1
if time > 20000:
with open(os.path.join(path, 'car_{0}_id_{1}.csv'.format(car, i)), 'w') as f:
colName = ['time', 'avgSpeed', 'flow', 'avgDensity']
fwriter = csv.DictWriter(f, fieldnames=colName)
fwriter.writeheader()
fwriter.writerows(expList)
f.close()
i += 1
print()
print('--------------------------------------------')