-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_mc.py
executable file
·169 lines (137 loc) · 6.52 KB
/
main_mc.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
167
168
169
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from mc import MonteCarlo
import numpy as np
import os
import time
def run_mc(area="kochi",simtime=30, meandeparture=15, numSim0=0, numBlocks= 5, simPerBlock= 1000,name='r'):
t0 = time.time()
agentsProfileName= os.path.join(area,"data","agentsdb.csv")
nodesdbFile= os.path.join(area,"data","nodesdb.csv")
linksdbFile= os.path.join(area,"data", "linksdb.csv")
transLinkdbFile= os.path.join(area,"data", "actionsdb.csv")
transNodedbFile= os.path.join(area,"data", "transitionsdb.csv")
folderStateNames = os.path.join(area,f"state_{name}")
if not os.path.exists(folderStateNames):
os.mkdir(folderStateNames)
meanRayleighTest = meandeparture*60
simulTime = simtime*60
survivorsPerSim= []
if numSim0 == 0:
randomChoiceRate = 0.99
optimalChoiceRate = 1.0 - randomChoiceRate
case = MonteCarlo(agentsProfileName = agentsProfileName ,
nodesdbFile= nodesdbFile,
linksdbFile= linksdbFile,
transLinkdbFile= transLinkdbFile,
transNodedbFile= transNodedbFile,
meanRayleigh = meanRayleighTest,
folderStateNames= folderStateNames)
totalagents = np.sum(case.pedDB.shape[0])
for t in range( int(min(case.pedDB[:,9])) , simulTime ):
case.initEvacuationAtTime()
case.stepForward()
optimalChoice = bool(np.random.choice(2, 1, p=[randomChoiceRate , optimalChoiceRate]))
case.checkTarget(ifOptChoice = optimalChoice)
if not t % 10:
case.computePedHistDenVelAtLinks()
case.updateVelocityAllPedestrians()
case.updateValueFunctionDB()
outfile = os.path.join(folderStateNames , "sim_%09d.csv" % numSim0)
outfilepedDB = os.path.join(folderStateNames , "ped_%09d.csv" % numSim0)
case.exportStateMatrix(outnamefile = outfile)
case.exportAgentDBatTimet(outnamefile = outfilepedDB)
print("\n\n ***** Simu %d (t= %.2f)*****" % ( numSim0, (time.time()-t0)/60. ))
print("epsilon greedy - exploration: %f" % randomChoiceRate)
print(f"survived: {np.sum(case.pedDB[:,10] == 1)} / total: {totalagents}")
survivorsPerSim.append([numSim0, np.sum(case.pedDB[:,10] == 1)])
fname = f"survivorsPerSim_{numBlocks}x{simPerBlock}.csv"
outSurvivors= os.path.join(folderStateNames, fname)
np.savetxt(outSurvivors, np.array(survivorsPerSim), delimiter= ",", fmt= "%d" )
if survivorsPerSim[-1] == case.pedDB.shape[0]:
return
case= None
numSim= numSim0 +1
for b in range(numBlocks):
for s in range(simPerBlock):
eoe = int(0.8*simPerBlock) #end of exploration
if s < eoe:
# randomChoiceRate = -1/(eoe)**2*s**2+1
randomChoiceRate = 0.9
else:
# randomChoiceRate = 0.
randomChoiceRate = 0.1
# randomChoiceRate = (simPerBlock - s - 1.0)/(simPerBlock - s + 1.0) #1.0/(0.015*s + 1.0)
optimalChoiceRate = 1.0 - randomChoiceRate
case = MonteCarlo(agentsProfileName = agentsProfileName ,
nodesdbFile= nodesdbFile,
linksdbFile= linksdbFile,
transLinkdbFile= transLinkdbFile,
transNodedbFile= transNodedbFile,
meanRayleigh = meanRayleighTest,
folderStateNames = folderStateNames)
namefile = os.path.join(folderStateNames , "sim_%09d.csv" % (numSim-1) )
case.loadStateMatrixFromFile(namefile = namefile)
totalagents = np.sum(case.pedDB.shape[0])
for t in range( int(min(case.pedDB[:,9])) , simulTime ):
case.initEvacuationAtTime()
case.stepForward()
optimalChoice = bool(np.random.choice(2, 1, p=[randomChoiceRate , optimalChoiceRate]))
case.checkTarget(ifOptChoice = optimalChoice)
if not t % 10:
case.computePedHistDenVelAtLinks()
case.updateVelocityAllPedestrians()
case.updateValueFunctionDB()
outfile = os.path.join(folderStateNames , "sim_%09d.csv" % numSim)
outfilepedDB = os.path.join(folderStateNames , "ped_%09d.csv" % numSim)
case.exportStateMatrix(outnamefile = outfile)
case.exportAgentDBatTimet(outnamefile = outfilepedDB)
print("\n\n ***** Simu %d (t= %.2f)*****" % ( numSim , (time.time()-t0)/60. ))
print("epsilon greedy - exploration: %f" % randomChoiceRate)
print(f"survived: {np.sum(case.pedDB[:,10] == 1)} / total: {totalagents}")
#evaluate survivors in simulation
survivorsPerSim.append([numSim, np.sum(case.pedDB[:,10] == 1)])
fname = f"survivorsPerSim_{numBlocks}x{simPerBlock}.csv"
outSurvivors= os.path.join(folderStateNames, fname)
np.savetxt(outSurvivors, np.array(survivorsPerSim), delimiter= ",", fmt= "%d" )
if survivorsPerSim[-1] == case.pedDB.shape[0]:
return
case= None
numSim += 1
#QFun, VFun, policy = case.computeAction_Value_Policy() #computeAction_Value_Policy
return
def kochi_mc():
simtime=30 #min
meandeparture=15 #min
numSim0= 1950
numBlocks= 1
simPerBlock= 8050
name=f"mc_{simtime}_{meandeparture}"
area="kochi"
run_mc(area=area,simtime=simtime, meandeparture=meandeparture,
numSim0=numSim0, numBlocks=numBlocks, simPerBlock=simPerBlock, name=name)
def arahama_mc():
simtime=67 #min
meandeparture=15 #min
numSim0= 0
numBlocks= 1
simPerBlock= 100
name=f"mc_{simtime}_{meandeparture}"
area="arahama"
run_mc(area=area,simtime=simtime, meandeparture=meandeparture,
numSim0=numSim0, numBlocks=numBlocks, simPerBlock=simPerBlock, name=name)
def new_kochi_mc():
simtime=30 #min
meandeparture=15 #min
numSim0= 0
numBlocks= 1
simPerBlock= 100
name=f"sarsa_{simtime}_{meandeparture}"
area="new_kochi"
run_mc(area=area,simtime=simtime, meandeparture=meandeparture,
numSim0=numSim0, numBlocks=numBlocks, simPerBlock=simPerBlock, name=name)
return
if __name__ == "__main__":
kochi_mc()
# arahama_mc()
# new_kochi_mc()