This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,73 @@
link graphics,printf
$define EDGE 0
$define EMPTY 1
$define TREE 2
$define FIRE 3
global Colours,Width,Height,ProbTree,ProbFire,ProbInitialTree,Forest,oldForest
procedure main() # forest fire
Height := 400 # Window height
Width := 400 # Window width
ProbInitialTree := .10 # intial probability of trees
ProbTree := .01 # ongoing probability of trees
ProbFire := ProbTree/50. # probability of fire
Rounds := 500 # rounds to evolve
setup_forest()
every 1 to Rounds do {
show_forest()
evolve_forest()
}
printf("Forest fire %d x %d rounds=%d p.initial=%r p/f=%r/%r fps=%r\n",
Width,Height,Rounds,ProbInitialTree,ProbTree,ProbFire,
Rounds/(&time/1000.)) # stats
WDone()
end
procedure setup_forest() #: setup the forest
Colours := table() # define colours
Colours[EDGE] := "black"
Colours[EMPTY] := "grey"
Colours[TREE] := "green"
Colours[FIRE] := "red"
WOpen("label=Forest Fire", "bg=black",
"size=" || Width+2 || "," || Height+2) | # add for border
stop("Unable to open Window")
every !(Forest := list(Height)) := list(Width,EMPTY) # default
every ( Forest[1,1 to Width] | Forest[Height,1 to Width] |
Forest[1 to Height,1] | Forest[1 to Height,Width] ) := EDGE
every r := 2 to Height-1 & c := 2 to Width-1 do
if probability(ProbInitialTree) then Forest[r,c] := TREE
end
procedure show_forest() #: show Forest - drawn changes only
every r := 2 to *Forest-1 & c := 2 to *Forest[r]-1 do
if /oldForest | oldForest[r,c] ~= Forest[r,c] then {
WAttrib("fg=" || Colours[Forest[r,c]])
DrawPoint(r,c)
}
end
procedure evolve_forest() #: evolve forest
old := oldForest := list(*Forest) # freeze copy
every old[i := 1 to *Forest] := copy(Forest[i]) # deep copy
every r := 2 to *Forest-1 & c := 2 to *Forest[r]-1 do
Forest[r,c] := case old[r,c] of { # apply rules
FIRE : EMPTY
TREE : if probability(ProbFire) |
( old[r-1, c-1 to c+1] |
old[r,c-1|c+1] |
old[r+1,c-1 to c+1] ) = FIRE then FIRE
EMPTY: if probability(ProbTree) then TREE
}
end
procedure probability(P) #: succeed with probability P
if ?0 <= P then return
end

View file

@ -0,0 +1,21 @@
NB. states: 0 empty, 1 tree, _1 fire
dims =:10 10
tessellate=: 0,0,~0,.0,.~ 3 3 >./@,;._3 ]
mask=: tessellate dims$1
chance=: 1 :'(> ? bind (dims$0)) bind (mask*m)'
start=: 0.5 chance
grow =: 0.01 chance
fire =: 0.001 chance
spread=: [: tessellate 0&>
step=: grow [`]@.(|@])"0 >.&0 * _1 ^ fire +. spread
run=:3 :0
forest=. start''
for.i.y do.
smoutput ' #o' {~ forest=. step forest
end.
)

View file

@ -0,0 +1,20 @@
run 2
##### #
# #
### ####
# # # #
##### #
## # #
# #
o## #
##### #
# #
### ####
# # # #
##### #
## # #
o #
o# #

View file

@ -0,0 +1,17 @@
@caversion 1;
dimensions 2;
state EMPTY, TREE, BURNING;
// an empty cell grows a tree with a chance of p = 5 %
rule{EMPTY} [0.05] : -> TREE;
// a burning cell turns to a burned cell
rule{BURNING}: -> EMPTY;
// a tree starts burning if there is at least one neighbor burning
rule{TREE} : BURNING{1,} -> BURNING;
// a tree is hit by lightning with a change of f = 0.006 %
rule{TREE} [0.00006] : -> BURNING;

View file

@ -0,0 +1,41 @@
'[RC] Forest Fire
dim oldgen(200,200), newgen(200,200)
p =0.99
f =0.9999
nomainwin
WindowWidth = 200
WindowHeight = 200
open "Forest Fire" for graphics_nsb_nf as #1
#1 "trapclose [quit]"
#1 "down ; fill brown ; flush"
p =0.99
f =0.9999
for generation = 1 to 200
for x = 1 to 199
for y = 1 to 199
scan 'we can break early
select case oldgen(x,y)
case 0
if rnd(0) > p then newgen(x,y) = 1 : #1 "color green ; set "; x; " "; y
case 2
newgen(x,y) = 0 : #1 "color brown ; set "; x; " "; y
case 1
if oldgen(x-1,y-1) = 2 or oldgen(x-1,y) = 2 or oldgen(x-1,y+1) = 2_
or oldgen(x,y-1) = 2 or oldgen(x,y+1) = 2 or oldgen(x+1,y-1) = 2_
or oldgen(x+1,y) = 2 or oldgen(x+1,y+1) = 2 or rnd(0) > f then
#1 "color red ; set "; x; " "; y
newgen(x,y) = 2
end if
end select
oldgen(x-1,y-1)=newgen(x-1,y-1)
next y
next x
next generation
[quit]
close #1
end

View file

@ -0,0 +1,8 @@
evolve[nbhd_List, k_] := 0 /; nbhd[[2, 2]] == 2 (*burning->empty*)
evolve[nbhd_List, k_] := 2 /; nbhd[[2, 2]] == 1 && Max@nbhd == 2 (*near_burning&nonempty->burning*)
evolve[nbhd_List, k_] := RandomChoice[{f, 1 - f} -> {2, nbhd[[2, 2]]}] /; nbhd[[2, 2]] == 1 && Max@nbhd < 2 (*spontaneously combusting tree*)
evolve[nbhd_List, k_] := RandomChoice[{p, 1 - p} -> {1, nbhd[[2, 2]]}] /; nbhd[[2, 2]] == 0 (*random tree growth*)
r = 100; c = 100; p = 10^-2; f = 10^-4;
init = RandomInteger[BernoulliDistribution[0.05], {r, c}];
MatrixPlot[CellularAutomaton[{evolve, {}, {1, 1}}, {init, 0}, {{{300}}}], ColorRules -> {0 -> White, 1 -> Green, 2 -> Red}, Frame -> False]