-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunMeForTest.py
executable file
·78 lines (69 loc) · 2.35 KB
/
RunMeForTest.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
from pycx_gui import GUI
from myModel import CAmodel
import matplotlib.pyplot as plt
import numpy as np
import random
def initial_row(initRowLength, density):
initRow = [0] * initRowLength
for i in range(initRowLength):
if (random.random() > density):
initRow[i] = 0
else:
initRow[i] = 1
return initRow
def count_1(list, initRowLength):
x = 0
for i in range(initRowLength):
if (list[i] == 1):
x += 1
return x
def get_critical_density_flow(densityList, flowVehicleList):
#resultDict = dict()
maxFlowVehicle = max(flowVehicleList)
for i in range(len(densityList)):
#resultDict[densityList[i]]=flowVehicleList[i]
if (maxFlowVehicle == flowVehicleList[i]):
break
return densityList[i], flowVehicleList[i]
r = 1 #range
k = 2 #base
systemSize = r * 2 + 1
initRowLength = 100
densityListXaxis = list()
flowListYaxis = list()
densityRange = np.linspace(0, 1, 51) #set intervel of densityList
rule = 184
criticalDenList = list()
TList = [1000] #set TList as T
#For Q6
#TList = range(10, 60, 1)
for T in TList: # T
initStateTimes = 10 # R
#densityRange = [0.4,[0.9]] # for Qestion 2
initRow = [0] * initStateTimes
flowVehicleList = [0] * initStateTimes
for density in densityRange:
for R in range(initStateTimes):
initRow[R] = initial_row(initRowLength, density)
model = CAmodel(r, k, rule, initRow[R], systemSize)
runTime = 0 #run times
flowVehicle = 0
while runTime < T: #T
currentRow = tuple(model.currentRow)
runTime += 1
currentRow = model.step()
if (currentRow[0] == 1):
if (currentRow[1] == 0):
flowVehicle += 1
flowVehicleList[R] = flowVehicle
densityListXaxis.append(density)
flowListYaxis.append(np.mean(flowVehicle))
#For Q6 get critical density and flow figure
# criticalDen, criticalFlow = get_critical_density_flow(
# densityListXaxis, flowListYaxis)
# print("critical density is:", criticalDen, "critical flow is:",
# criticalFlow)
# criticalDenList.append(criticalDen)
#model.draw_hw4_criticalDensity_T(criticalDenList, TList)
#draw normal density-flow figure
model.draw_hw4(densityListXaxis, flowListYaxis)