-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom_evacuation.py
227 lines (192 loc) · 8.22 KB
/
room_evacuation.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# -*- coding: utf-8 -*-
"""
Created on Tue May 29 13:36:57 2018
@author: azm
"""
import random
import matplotlib.pyplot as plt
import math
import numpy as np
class person():
def __init__(self,number,x,y):
self.number = number
self.x = x#現在の場所
self.y = y
self.next_x = None#次行きたい場所
self.next_y = None
def ikitai_basyo(self,L,xr,xl):#行きたい場所を決める
rnd = random.random()
R=0.3
a=3
xT = L/2#(L+1)/2
if (self.y < a*(self.x-xr) or self.y < -a*(self.x-xl)):
yT = -L/10 +2*L/5*(-self.y/math.sqrt((self.y-L/2+5)**2+self.y**2)+math.sin(math.atan(a)))
else:
yT = -L/10
#yT = -L/10
syahen = math.sqrt(abs(self.x-xT)**2+abs(self.y-yT)**2)
tate = -xT+self.x
yoko = -yT+self.y
cos = yoko/syahen
sin = tate/syahen
Z = abs(cos) + abs(sin)
Ru = 1/4*R + (1-R)*(-cos*(1-np.sign(cos)))/(2*Z)
Rd = 1/4*R + (1-R)*(cos*(1+np.sign(cos)))/(2*Z)
Rr = 1/4*R + (1-R)*(-sin*(1-np.sign(sin)))/(2*Z)
Rl = 1/4*R + (1-R)*(sin*(1+np.sign(sin)))/(2*Z)
#Ru,Rd,Rr,Rl = 0/4,2/4,1/4,1/4
#print(Rd)
#print(math.degrees(alpha),math.degrees(math.asin(tate/syahen)))
if 0 < rnd <= Ru:#up
self.next_x = self.x
self.next_y = self.y+1
elif Ru<rnd<=Ru+Rd:#down
self.next_x = self.x
self.next_y = self.y-1
elif Ru+Rd<rnd<=Ru+Rd+Rr:#right
self.next_x = self.x+1
self.next_y = self.y
elif Ru+Rd+Rr<rnd<=Ru+Rd+Rr+Rl:#left
self.next_x = self.x-1
self.next_y = self.y
def ikitai_basyo_iku(self):
self.x = self.next_x#現在の場所に次の場所にする
self.y = self.next_y
self.ikitai_basyo_kesu()#行きたい場所を消す
def ikitai_basyo_kesu(self):
self.next_x = None#行きたい場所を消す
self.next_y = None
def show_property(self):#プレイや1人の情報を表示
print(self.number,'('+str(self.x)+','+str(self.y)+')','('+str(self.next_x)+','+str(self.next_y)+')')
class room():
def __init__(self,L,W,rho):
self.W = W#yの長さ
self.L = L#xの長さ
self.rho = rho
self.Ld = self.L/5
self.xl= self.L/2 - self.Ld/2
self.xr =self.L/2 + self.Ld/2
self.M = int((self.L-2)*(self.W-2)*self.rho)#人数
self.person = []#プレイや一人一人を格納
self.hitogairu_basyo = None#プレイヤーがいる場所
xy_list = []#プレイヤーの座標の候補を格納
for i in range(1,L):#1<= x <= L-1
for j in range(1,W):#1 <= x <= W-1
xy_list.append([i,j])#プレイヤーの座標の候補を格納
for i in range(self.M):
tmp = xy_list.pop(int(random.random()*len(xy_list)))
x,y = tmp[0],tmp[1]
self.person.append(person(i,x,y))#M人分の人を作る
def hitogairu_basyo_kazoeru(self):
x_list,y_list=[],[]
for i in range(self.M):
x_list.append(self.person[i].x)
y_list.append(self.person[i].y)
self.hitogairu_basyo = x_list,y_list
def idou(self,nn):
#print('移動'+str(nn+1)+'回目')
for i in range(self.M):
self.person[i].ikitai_basyo(self.L,self.xr,self.xl)#行きたい場所を決める
#print('行きたい場所を選んだ後')
#self.show_property()
self.hitogairu_basyo_kazoeru()#人がいる場所を確認
for i in range(self.M):#行きたい場所がダメなところの場合
for x,y in zip(self.hitogairu_basyo[0],self.hitogairu_basyo[1]):
if self.person[i].next_x == x and self.person[i].next_y == y:#行きたい場所に人がいるとき
self.person[i].ikitai_basyo_kesu()
if self.person[i].next_x is None or self.person[i].next_y is None:#Noneとばす
pass
elif self.xl <= self.person[i].next_x <= self.xr and self.person[i].next_y <= 0:#
pass
elif not((0 < self.person[i].next_x < self.L) and (0 < self.person[i].next_y < self.W)):#境界条件,壁にはいけない
self.person[i].ikitai_basyo_kesu()
#print('いけない場所を除く')
#self.show_property()
for i in range(self.M):#移動
kabuttahito = [self.person[i].number]
if self.person[i].next_x == None:
continue
for j in range(self.M):
if self.person[j].next_x == None or i == j:
pass
elif self.person[i].next_x == self.person[j].next_x and self.person[i].next_y == self.person[j].next_y:
kabuttahito.append(self.person[j].number)
#print(kabuttahito)
if len(kabuttahito) == 1:
self.person[i].ikitai_basyo_iku()
elif len(kabuttahito) == 2:
player1 = kabuttahito[0]
player2 = kabuttahito[1]
if 0 <= random.random() < 1/2:#CC
self.person[player1].ikitai_basyo_iku()
self.person[player2].ikitai_basyo_kesu()
#print('win player',player1)
else:
self.person[player2].ikitai_basyo_iku()
self.person[player1].ikitai_basyo_kesu()
#print('win player',player2)
elif len(kabuttahito) >= 3:
winner = kabuttahito[int(random.random()*len(kabuttahito))]
#print('winner player',winner)
self.person[winner].ikitai_basyo_iku()
for k in kabuttahito:
self.person[k].ikitai_basyo_kesu()
#print('対戦終了')
#self.show_property()
for i in range(self.M):#逃げる(出口)
if self.xl <= self.person[i].x <= self.xr and self.person[i].y <= 0:#take out of the system
self.person[i].x = -100
self.person[i].y = -100
nokori = self.nokori_ninzu()
self.plot(nn,nokori)
return nokori
#print('------')
def nokori_ninzu(self):
n=0
for i in range(self.M):
if 0 <= self.person[i].x <= self.L and 0 <= self.person[i].y <= self.W:
n += 1
print(n)
return n
def plot(self,nn,nokori):#図を作成
x,y = [],[]#青点
xg,yg =[],[]#次逃げれそうな人(オレンジ)
linex = [self.xr+0.2,self.W,self.W,0,0,self.xl-0.2]#wall
liney = [0,0,self.L,self.L,0,0]#wall
for i in range(self.M):
if self.xl <= self.person[i].x <= self.xr and self.person[i].y == 1:#逃げれそうな人に色を付ける
xg.append(self.person[i].x)
yg.append(self.person[i].y)
elif self.person[i].x >=1 and self.person[i].y >=0:
x.append(self.person[i].x)
y.append(self.person[i].y)
plt.figure(figsize=(self.L/8,self.W/8))
plt.ylim([-0.5,self.L+0.5])
plt.xlim([-0.5,self.W+0.5])
plt.plot(linex,liney,'r-')
plt.scatter(x, y)
plt.scatter(xg, yg)
plt.text(1,1, r't='+str(nn))
plt.text(1,3, r'n='+str(nokori))
plt.savefig(r'./figure2/'+str(nn)+'.png')
plt.close()
def show_property(self):
for i in range(self.M):
self.person[i].show_property()
print('------')
# 散布図を描画
#plt.scatter(x, y)
def main():
L,W = 50,50
rho = 0.4
r = room(L,W,rho)
#print('最初の状態')
#r.show_property()
for i in range(501):
n = r.idou(i)
if n <= 0:
break
#print('最後の状態')
#r.show_property()
if __name__ == "__main__":
main()