-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwildfire.cpp
More file actions
329 lines (283 loc) · 9.66 KB
/
wildfire.cpp
File metadata and controls
329 lines (283 loc) · 9.66 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
File: wildfire.cpp
Initial start with modeling wildfire spread!
*/
#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
#include <random>
using namespace std;
random_device rd;
mt19937 gen(rd());
/*
Struct to represent a land cell. It currently contains...
-A boolean to represent if there is a fire
-A double to represent the amount of fuel left in the land cell
*/
struct LandCell{
bool fire = false;
double fuel;
bool populated = false;
};
/*
Struct to represent a populated area. It currently contains...
-ints i and j to represent the location of the populated area [TO-DO: make this cleaner?]
-A boolean to indicate if the populated area is evacuating or not
-An int, where if set to 0, means there are no longer people there (either evacuated or died)
*/
struct populatedArea{
int i;
int j;
bool evacuating = false;
int remainingTime;
};
/*
Function to help draw grid on the screen
*/
void drawGridworld(int gridDim) {
cout << "Picture of Grid World" << endl;
// Simple CLI grid
for (int i = 0; i < 1 + gridDim; i++) {
for (int j = 0; j < gridDim; j++) {
if (i == 0) {
cout << "__";
} else {
cout << "|_";
}
}
if (i > 0) {
cout << "|" << endl;
} else {
cout << "_" << endl;
}
}
}
/*
Function to calculte Euclidean distance between two points
*/
double calculateDistance(int xOne, int yOne, int xTwo, int yTwo) {
return sqrt(pow(xOne - xTwo, 2) + pow((yOne - yTwo), 2));
}
/*
Currently a replacement for `drawGridworld` -- helps to visualize the look of the canvas
*/
void printData(vector<vector<LandCell> >& state){
for(int i = 0; i < state.size(); i++){
for(int j = 0; j < state[0].size(); j++){
if (state[i][j].fire) {
cout << "F" << " ";
//Check check up
if(state[i][j].populated)
cout << "This code is broken :/" << endl;
} else if (state[i][j].populated) {
cout << "P" << " ";
} else {
cout << "_" << " ";
}
}
cout << endl;
}
}
/*
Helper function that updates the states through all deterministic changes in the time step.
Check through all states and deplete fire by 1.
If there is no more fire, then we set the boolean equal to 0.
*/
void runDetForward(vector<vector<LandCell> >& state, vector<populatedArea>& actionSpace){
// Deplete fire and check if there is still a fire
for (int i = 0; i < state.size(); i++) {
for (int j = 0; j < state[0].size(); j++)
if (state[i][j].fire) {
state[i][j].fuel = max(double(0), state[i][j].fuel - 1);
if (!state[i][j].fuel) {
state[i][j].fire = false;
}
}
}
// Decrease amount of time remaining for populated areas if already evacuating
for (int i = 0; i < actionSpace.size(); i++){
if (actionSpace[i].evacuating && actionSpace[i].remainingTime) {
actionSpace[i].remainingTime--;
if (!actionSpace[i].remainingTime) {
state[actionSpace[i].i][actionSpace[i].j].populated = false;
}
}
}
}
/*
At each point in the grid world, determine if there will be a fire by looking at the amount of fuel and the surrounding states.
*/
vector<vector<LandCell> > sampleNextState(vector<vector<LandCell> >& state, double distanceConstant){
// Create new state and set observation distance
vector<vector<LandCell> > newState = state;
int observationDistance = 2;
// Iterate through each point
for (int i = 0; i < state.size(); i++) {
for (int j = 0; j < state[0].size(); j++) {
// Check whether a certain state doesn't have a fire, but DOES have fuel
if (!state[i][j].fire && state[i][j].fuel > 0) {
// Utilize equation from SISL paper to calculate probability of there being a fire
double prob = 1;
for (int nI = max(0, i - observationDistance); nI < min(int(state.size()), i + observationDistance); nI++) {
for (int nJ = max(0, j - observationDistance); nJ < min(int(state.size()), j + observationDistance); nJ++) {
if (i != nI && j != nJ && state[nI][nJ].fire) {
prob *= 1 - (distanceConstant * pow(1.0 / calculateDistance(i, j, nI, nJ), 2));
//prob *= 1 - (distanceConstant * pow(1.0 / calculateDistance(i, j, nI, nJ), 2) * (state[nI][nJ].fire ? 1 : 0));
}
}
}
// Calculate final probability, and then use a Bernoulli distribution to determine if there will be a fire
prob = 1 - prob;
bernoulli_distribution b(prob);
newState[i][j].fire = b(gen);
}
}
}
return newState;
}
/*
Function to calculate the total utility of all of the states
TO-DO: should the first check see whether or not the action space is evacuating?
TO-DO: should the second case consider whether or not the spot is on fire but they are not evacuated?
*/
int getStateUtility(vector<vector<LandCell> >& state, vector<populatedArea>& actionSpace) {
int reward = 0;
for (int i = 0; i < actionSpace.size(); i++) {
// If the populated area still has remaining time left, but the area is already on fire, incur -100 reward.
if (actionSpace[i].remainingTime && state[actionSpace[i].i][actionSpace[i].j].fire) {
//cout << "IT'S SO OVER" << endl;
reward -= 100;
actionSpace[i].remainingTime = 0;
state[actionSpace[i].i][actionSpace[i].j].populated = false;
}
// If the current populated area is not evacuating, add 1 reward
else if (!actionSpace[i].evacuating && actionSpace[i].remainingTime) {
reward += 1;
}
}
return reward;
}
/*
Function to actually take an action after sampling a state
*/
vector<populatedArea> takeAction(int action, vector<populatedArea>& actionSpace) {
vector<populatedArea> newActionSpace = actionSpace;
// Only take an action if the action is not to do nothing
if (action != -1) {
actionSpace[action].evacuating = true;
}
return newActionSpace;
}
/*
Implements sparse sampling to approximate the value function. Note that we have the following action space:
-"-1" means to do nothing
-"i" means to evacuate the "i'th" area in the actionSpace vector of populated areas.
*/
pair<int,int> sparseSampling(vector<vector<LandCell> > state, vector<populatedArea> actionSpace, int depth, int samples, double distanceConstant) {
// Standalone scenario -- doing nothing
int stateReward = getStateUtility(state, actionSpace);
pair<int, int> best = {-1, INT_MIN};
// Base case: depth = 0, so we return doing nothing and the utility of being in the state
if (depth == 0) {
return {-1, stateReward};
}
// Recursive case: iterate through each action (each i'th area of evacuation)
for (int i = -1; i < (int) actionSpace.size(); i++) {
//Ignore already evacuating areas
if(i >= 0 && actionSpace[i].evacuating)
continue;
// Generate a sample for each and update the utility
int utility = 0;
vector<populatedArea> newActionSpace = takeAction(i, actionSpace);
vector<vector<LandCell> > resultantState = state;
runDetForward(resultantState, newActionSpace);
for (int j = 0; j < samples; j++) {
// Sample a new state and reward
vector<vector<LandCell> > sampledState = sampleNextState(resultantState, distanceConstant);
// Call sparse sampling recursively and update utility (assume undiscounted)
pair<int, int> returnPair = sparseSampling(sampledState, newActionSpace, depth - 1, samples, distanceConstant);
utility += returnPair.second;
}
//the discount factor
utility /= samples;
utility *= .99;
// Check if utility is better than current best
if (utility + stateReward > best.second) {
best = {i, utility + stateReward};
}
}
// Return best
return best;
}
/*
Main function that controls the simulation
*/
int runSimulation(int gridDim, double distanceConstant, int burnRate) {
// Set up randomness
random_device rd;
mt19937 gen(rd());
// Various hyperparameters
int timeToEvacuate = 3;
// Create the state of the simulation
vector<vector<LandCell> > state(gridDim, vector<LandCell>(gridDim, LandCell()));
// Create an action space of populated areas
vector<populatedArea> actionSpace;
actionSpace.push_back({11, 4, false, timeToEvacuate});
actionSpace.push_back({3, 14, false, timeToEvacuate});
// Indicate populated areas also on game state
state[11][4].populated = true;
state[3][14].populated = true;
// Places initial fire seeds
int burnCount = 2;
for(int i = 0; i < burnCount; i++){
state[rand() % gridDim][rand() % gridDim].fire = true;
}
// Sets fuel levels
normal_distribution<double> normal(8, 3);
for(int i = 0; i < state.size(); i++){
for(int j = 0; j < state[0].size(); j++){
state[i][j].fuel = max(double(0), normal(gen));
}
}
// Run simulation for x timesteps
int actualReward = 0;
for (int i = 0; i < 100; i++) {
// Run the next state forward and sample the next state
int temp = getStateUtility(state, actionSpace);
//printData(state);
actualReward += temp;
if(temp < 0)
cout << "We lost them" << endl;
runDetForward(state, actionSpace);
state = sampleNextState(state, distanceConstant);
// Run sparse sampling and take the next action
pair<int, int> best = sparseSampling(state, actionSpace, 5, 3, distanceConstant);
actionSpace = takeAction(best.first, actionSpace);
cout << i << endl;
}
cout << "HEAR YE HEAR YE. THE KING PROCLAIMS OUR FINAL REWARD IS " << actualReward << endl;
return actualReward;
}
/*
Calls main functions!
TO-DO: make the hyperparameters global?
*/
int main()
{
// Initialize random seed, grid dimension, and hyperparameters
srand (time(NULL));
int gridDim = 20;
double distanceConstant = 0.124;
int burnRate = 5;
// Running simulation
cout << "Initially running the simulation" << endl;
double total = 0;
for(int i = 0; i < 100; i++)
total += runSimulation(gridDim, distanceConstant, burnRate);
cout << "Average across all simulations is " << (total / 100) << endl;
return 0;
}