-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfiretests.py
259 lines (215 loc) · 8.17 KB
/
firetests.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import firesim
# DEFINE FUNCTIONS
# Helper dist function
def sqDistFromCenter(point, center):
pX, pY = point
cX, cY = center
return float((pX-cX)**2 + (pY-cY)**2)
# Point Firefighters
# point = top, right, bottom, left
def generatePointFFs(center, radius, point = 'top', numFFs = 8):
# How many FFs to left of center
leftFFs = numFFs/2
rightFFs = numFFs - leftFFs
ffs = []
if point == 'top':
# Lineup numFFs across top of fire
for ffPosX in range(center[0] - numFFs/2, center[0] + rightFFs):
ffPosY = center[1] + radius + 1
ffs.append((ffPosX, ffPosY))
elif point == 'right':
# Lineup numFFs along right side of fire
for ffPosY in range(center[1] - numFFs/2, center[1] + rightFFs):
ffPosX = center[0] + radius + 1
ffs.append((ffPosX, ffPosY))
elif point == 'bottom':
# Lineup numFFs across bottom of fire
for ffPosX in range(center[0] - numFFs/2, center[0] + rightFFs):
ffPosY = center[1] - radius - 1
ffs.append((ffPosX, ffPosY))
elif point == 'left':
# Lineup numFFs along left side of fire
for ffPosY in range(center[1] - numFFs/2, center[1] + rightFFs):
ffPosX = center[0] - radius - 1
ffs.append((ffPosX, ffPosY))
return ffs
# Surround Firefighters
def generateSurroundFFs(center, radius, numFFs = 8):
ffs = []
# indices: top - 0, right - 1, bottom - 2, left - 3
ind = ['top', 'right', 'bottom', 'left']
# Distribution of how many FFs per side
ffDist = [0 for i in range(4)]
for i in range(numFFs):
ffDist[i%4] += 1
# Iterate through the 4 locations and generate a point allocation
# based on num FFs assigned to each location
for i in range(len(ffDist)):
ffs += generatePointFFs(center, radius, point = ind[i], numFFs = ffDist[i])
return ffs
# Optimal Firefighters
def generateOptimalFFs(center, radius, numFFs = 8):
# TODO using Anastasiya's code
return False
def generateRoundFire(center, radius, minInten = 0.1):
fire = [(center[0], center[1], 1)]
for dx in range(-radius, radius+1):
for dy in range(-radius, radius+1):
# Generate new point
newFirePt = (center[0] + dx, center[1] + dy)
# Get intensity proportional to sq distance from center (1 at center, minInten on outside)
# ie on the outside: sqDist = 16, radius = 16, minInten = .25
# We then have 1-((16*(1-.25)))/16) = 1 - 12/16 = 1-.75 = .25, which is the desired result
fireInten = 1. - (sqDistFromCenter(newFirePt, center) * (1-minInten))/(2*(radius**2))
newFireCell = (newFirePt[0], newFirePt[1], fireInten)
if not (newFireCell in fire):
fire.append(newFireCell)
return fire
def generateEllipseFire(center, xRadius, yRadius, minInten = 0.1):
fire = [(center[0], center[1], 1)]
for dx in range(-xRadius, xRadius+1):
for dy in range(-yRadius, yRadius+1):
# Generate new point
newFirePt = (center[0] + dx, center[1] + dy)
# Get intensity proportional to sq distance from center (1 at center, minInten on outside)
# ie on the outside: sqDist = 16, radius = 16, minInten = .25
# We then have 1-((16*(1-.25)))/16) = 1 - 12/16 = 1-.75 = .25, which is the desired result
fireInten = 1. - (sqDistFromCenter(newFirePt, center) * (1-minInten))/(2*(xRadius**2))
newFireCell = (newFirePt[0], newFirePt[1], fireInten)
if not (newFireCell in fire):
fire.append(newFireCell)
return fire
def generateOddFire(center, radius, minInten = 0.1):
fire = [(center[0], center[1], 1)]
for dx in range(-radius, radius+1):
for dy in range(-radius, radius+1):
# Generate new point
newFirePt = (center[0] + dx, center[1] + dy)
# Get intensity proportional to sq distance from center (1 at center, minInten on outside)
# ie on the outside: sqDist = 16, radius = 16, minInten = .25
# We then have 1-((16*(1-.25)))/16) = 1 - 12/16 = 1-.75 = .25, which is the desired result
fireInten = 1. - (sqDistFromCenter(newFirePt, center) * (1-minInten))/(2*(radius**2))
newFireCell = (newFirePt[0], newFirePt[1], fireInten)
if abs(dx-dy) < radius/2 and not (newFireCell in fire):
fire.append(newFireCell)
return fire
################################################################
# BUILD FIRES
testSize = 10
totalNumFFs = 8
fires = []
# # # Round fire
# center = (testSize/2, testSize/2)
# ## Small
# smallRadius = 1
# smallFireR = generateRoundFire(center, smallRadius)
# fires.append(("Small Round Fire", center, smallRadius, smallFireR))
# ## Medium
# medRadius = 2
# medFireR = generateRoundFire(center, medRadius)
# fires.append(("Med Round Fire", center, medRadius, medFireR))
# ## Large
# largeRadius = 3
# largeFireR = generateRoundFire(center, largeRadius)
# fires.append(("Med Round Fire", center, largeRadius, largeFireR))
# # Elliptical fire - right facing
# center = (testSize/2, testSize/2)
# ## Small
# smallRadius = 1
# smallFireE = generateEllipseFire(center, smallRadius*2, smallRadius)
# fires.append(("Small Ellipse Fire", center, smallRadius, smallFireE))
# ## Medium
# medRadius = 2
# medFireE = generateEllipseFire(center, medRadius*2, medRadius)
# fires.append(("Med Ellipse Fire", center, medRadius, medFireE))
# ## Large
# largeRadius = 3
# largeFireE = generateEllipseFire(center, largeRadius*2, largeRadius)
# fires.append(("Large Ellipse Fire", center, largeRadius, largeFireE))
# Odd-shaped fire
center = (testSize/2, testSize/2)
# ## Small
# smallRadius = 1
# smallFireO = generateRoundFire(center, smallRadius)
# fires.append(("Small Odd Fire", center, smallRadius, smallFireO))
# ## Medium
# medRadius = 3
# medFireO = generateRoundFire(center, medRadius)
# fires.append(("Med Odd Fire", center, medRadius, medFireO))
## Large
largeRadius = 3
largeFireO = generateRoundFire(center, largeRadius)
fires.append(("Large Odd Fire", center, largeRadius, largeFireO))
###################################################################
## RUN TESTS
# Init
def runTests():
import numpy as np
import matplotlib.pyplot as plt
figIdx = 1
# name, center, radius, cells = ("Small Round Fire", center, smallRadius, smallFireR)
styles = ["Point Configuration - ", "Surround Configuration - ", "Optimal Configuration - "]
strats = ["random", "greedy"]
strats2 = ["random", "greedy", "optimal", "teamOptimal"]
# print name, center, radius, cells
# print "###"
for name, center, radius, cells in fires:
for configStyle in range(2): # 0 - point, 1 - surround, 2 - optimal
avgs = []
for strat in strats2:
trials = 10
stepsToExtinguish = []
# Initialize figure
fig = plt.figure(figIdx)
fig.suptitle(styles[configStyle] + name)
plt.xlabel('Iterations')
plt.ylabel('Turns to Extinguish')
print "Working on", styles[configStyle], name, strat
for t in range(trials):
sim = firesim.AreaSimulation(testSize)
sim.initialize()
# Light the fire and count the num fires lit
c = 0
for x, y, inten in cells:
sim.grid[(x, y)].fire_inten = inten
c += 1
# sim.num_fires = c
if configStyle == 0:
ff_config = generatePointFFs(center, radius, point = 'top', numFFs = totalNumFFs)
elif configStyle == 1:
ff_config = generateSurroundFFs(center, radius, numFFs = totalNumFFs)
else:
ff_config = sim.best_ff_config(totalNumFFs)
for ff in ff_config:
ff2 = firesim.FireFighter(ff[0], ff[1], sim, style = strat, efficacy = 1)
sim.fight_fire(ff2)
# sim.gprint()
# Run simulation
steps = 0
iters = 50
for itr in range(iters):
sim.gnew()
steps += 1
#print sim.num_fires
if sim.num_fires == 0:
break
stepsToExtinguish.append(steps)
print "Done Trial with", steps, "steps"
print "Done. Plotting", styles[configStyle], name, strat
plt.plot(stepsToExtinguish, label = strat)
avgs.append(sum(stepsToExtinguish)/len(stepsToExtinguish))
plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),
ncol=2, fancybox=True, shadow=True)
fig.savefig(styles[configStyle] + name + '.jpg')
fig = plt.figure(figIdx+1)
fig.suptitle(styles[configStyle] + name)
plt.xlabel('Approach')
plt.ylabel('Average Steps to Extinguish')
ind = np.arange(4)
plt.bar(ind, avgs, width=.95, color='blue')
plt.ylim([0, max(avgs)+1])
plt.xticks(ind+.475, tuple(strats2))
fig.savefig(styles[configStyle] + name + ' Bar.jpg')
print "PLOT GENERATED!", styles[configStyle], name
figIdx += 2
# runTests()