Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Simple CA simulator in Python
- #
- # *** Forest fire ***
- #
- # Copyright 2008-2012 Hiroki Sayama
- # sayama@binghamton.edu
- # Modified to run with Python 3
- import matplotlib
- matplotlib.use('TkAgg')
- import pylab as PL
- import random as RD
- import scipy as SP
- RD.seed()
- width = 100
- height = 100
- initProb = 0.4
- empty, tree, fire, char = range(4)
- ignitation = 0.5
- def init():
- global time, config, nextConfig
- time = 0
- config = SP.zeros([height, width])
- for x in range(width):
- for y in range(height):
- if RD.random() < initProb:
- state = tree
- else:
- state = empty
- config[y, x] = state
- config[height//2, width//2] = fire
- nextConfig = SP.zeros([height, width])
- def draw():
- PL.cla()
- PL.pcolor(config, vmin = 0, vmax = 3, cmap = PL.cm.binary)
- PL.axis('image')
- PL.title('t = ' + str(time))
- def step():
- global time, config, nextConfig
- time += 1
- for x in range(width):
- for y in range(height):
- state = config[y, x]
- if state == fire:
- state = char
- elif state == tree:
- for dx in range(-1, 2):
- for dy in range(-1, 2):
- if config[(y+dy)%height, (x+dx)%width] == fire:
- state = fire
- nextConfig[y, x] = state
- config, nextConfig = nextConfig, config
- # import pycxsimulator
- # pycxsimulator.GUI().start(func=[init,draw,step])
- import numpy as np
- fire_prob = np.linspace(0,1,num = 20)
- Time = []
- Charred = []
- for prob in fire_prob:
- Char = []
- times_list =[]
- initial_Prob = prob #checking different initial fire probility
- for j in range(10):
- init() #initiate
- for i in range(20):
- draw()
- step()
- if np.all(config == nextConfig):
- times_list.append(time) #append each
- break
- Char.append((config == 3).sum())
- Charred.append(np.mean(Char)/width/height)
- #calculate the mean density of the Charred cells
- #All fired tree will turn chars and stay chars, so
- #np.mean(Char)/width/height is to get the proportion of fires
- Time.append(np.mean(times_list)) #calculate the mean
- ignite_prob = np.linspace(0,1,num = 10)
- #generate different probabilities for ignitation fires
- def init():
- global time, config, nextConfig
- time = 0
- config = SP.zeros([height, width])
- for x in range(width):
- for y in range(height):
- if RD.random() < initProb:
- state = tree
- else:
- state = empty
- config[y, x] = state
- config[height//2, width//2] = fire
- nextConfig = SP.zeros([height, width])
- def draw():
- PL.cla()
- PL.pcolor(config, vmin = 0, vmax = 3, cmap = PL.cm.binary)
- PL.axis('image')
- PL.title('t = ' + str(time))
- def step():
- global time, config, nextConfig
- time += 1
- for x in range(width):
- for y in range(height):
- state = config[y, x]
- if state == fire:
- state = char
- elif state == tree:
- for dx in range(-1, 2):
- for dy in range(-1, 2):
- if config[(y+dy)%height, (x+dx)%width] == fire and RD.random < RD.random(ignite_prob):
- state = fire
- nextConfig[y, x] = state
- config, nextConfig = nextConfig, config
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement