-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathThursday Apr 16 2015 Forest Fire.py
115 lines (83 loc) · 2.55 KB
/
Thursday Apr 16 2015 Forest Fire.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
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <codecell>
from matplotlib.figure import Figure
from matplotlib.axes import Subplot
import matplotlib
# for convolve2d
import scipy.signal as sig
# <codecell>
from IPython.display import display, clear_output
# <codecell>
def paramtext(x,y,*args,**kwargs):
paramstr='\n'.join(args)
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
T=text(x,y,paramstr,
ha='center',
va='top',
bbox=props,
transform=gca().transAxes,
multialignment='left',
**kwargs)
# <codecell>
class Forest(object):
def __init__(self,N=100,p=.01,f=.0001,g=0.0):
self.neighborhood=ones((3,3))
self.neighborhood[1,1]=0
self.name="Forest Fire Simulation"
self.p=p
self.f=f
self.N=N
self.g=g
self.field=zeros((self.N,self.N))
self.image=[self.field]
def reset(self):
self.field=zeros((self.N,self.N))
self.image=[self.field]
def update(self):
r=random.rand(self.N,self.N)
r2=random.rand(self.N,self.N)
empty=(self.field==0)
trees=(self.field==1)
burning=(self.field==2)
s=sig.convolve2d(burning,self.neighborhood,mode='same')
# grow a tree
self.field[(r<self.p) & empty]=1
# burning lasts only 1 cycle
self.field[burning]=0
# random extra burning
r=random.rand(self.N,self.N)
self.field[(r<self.f) & trees]=2
# burn the neighborhood
self.field[(s>0) & trees & (r2<(1.0-self.g))]=2
def run(self):
_fig=figure(figsize=(6,6))
cmap=[ [0,0,0.0] , [0,1.0,0.0],[1.0,0,0]]
my_cmap=matplotlib.colors.ListedColormap(cmap)
ax=subplot(1,1,1)
h=pcolor(f.field,cmap=my_cmap,vmin=0,vmax=2)
paramtext(1.2,.9,
'p=%.4f' % f.p,
'f=%.4f' % f.f,
'g=%.4f' % f.g,
'N=%d' % f.N,
)
axis('equal')
i=0
try:
while True:
clear_output(wait=True)
h.set_array(f.field.ravel())
title(str(i))
i+=1
f.update()
display(_fig)
except KeyboardInterrupt:
pass
# <codecell>
f=Forest(N=100,
p=.01, # tree growing
f=.00001, # fire starting
g=0.0) # 1-p(neighbor burning) g=0 ==> all neighbors
f.run()
# <codecell>