Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. # Simple CA simulator in Python
  2. #
  3. # *** Forest fire ***
  4. #
  5. # Copyright 2008-2012 Hiroki Sayama
  6. # sayama@binghamton.edu
  7.  
  8. # Modified to run with Python 3
  9.  
  10. import matplotlib
  11. matplotlib.use('TkAgg')
  12.  
  13. import pylab as PL
  14. import random as RD
  15. import scipy as SP
  16.  
  17. RD.seed()
  18.  
  19. width = 100
  20. height = 100
  21. initProb = 0.4
  22. empty, tree, fire, char = range(4)
  23. ignitation = 0.5
  24.  
  25. def init():
  26. global time, config, nextConfig
  27.  
  28. time = 0
  29.  
  30. config = SP.zeros([height, width])
  31. for x in range(width):
  32. for y in range(height):
  33. if RD.random() < initProb:
  34. state = tree
  35. else:
  36. state = empty
  37. config[y, x] = state
  38. config[height//2, width//2] = fire
  39.  
  40. nextConfig = SP.zeros([height, width])
  41.  
  42. def draw():
  43. PL.cla()
  44. PL.pcolor(config, vmin = 0, vmax = 3, cmap = PL.cm.binary)
  45. PL.axis('image')
  46. PL.title('t = ' + str(time))
  47.  
  48. def step():
  49. global time, config, nextConfig
  50.  
  51. time += 1
  52.  
  53. for x in range(width):
  54. for y in range(height):
  55. state = config[y, x]
  56. if state == fire:
  57. state = char
  58. elif state == tree:
  59. for dx in range(-1, 2):
  60. for dy in range(-1, 2):
  61. if config[(y+dy)%height, (x+dx)%width] == fire:
  62. state = fire
  63. nextConfig[y, x] = state
  64.  
  65. config, nextConfig = nextConfig, config
  66.  
  67. # import pycxsimulator
  68. # pycxsimulator.GUI().start(func=[init,draw,step])
  69.  
  70.  
  71. import numpy as np
  72.  
  73. fire_prob = np.linspace(0,1,num = 20)
  74.  
  75. Time = []
  76. Charred = []
  77.  
  78. for prob in fire_prob:
  79. Char = []
  80. times_list =[]
  81. initial_Prob = prob #checking different initial fire probility
  82. for j in range(10):
  83. init() #initiate
  84. for i in range(20):
  85. draw()
  86. step()
  87. if np.all(config == nextConfig):
  88. times_list.append(time) #append each
  89. break
  90. Char.append((config == 3).sum())
  91. Charred.append(np.mean(Char)/width/height)
  92. #calculate the mean density of the Charred cells
  93. #All fired tree will turn chars and stay chars, so
  94. #np.mean(Char)/width/height is to get the proportion of fires
  95. Time.append(np.mean(times_list)) #calculate the mean
  96.  
  97. ignite_prob = np.linspace(0,1,num = 10)
  98. #generate different probabilities for ignitation fires
  99.  
  100. def init():
  101. global time, config, nextConfig
  102.  
  103. time = 0
  104.  
  105. config = SP.zeros([height, width])
  106. for x in range(width):
  107. for y in range(height):
  108. if RD.random() < initProb:
  109. state = tree
  110. else:
  111. state = empty
  112. config[y, x] = state
  113. config[height//2, width//2] = fire
  114.  
  115. nextConfig = SP.zeros([height, width])
  116.  
  117. def draw():
  118. PL.cla()
  119. PL.pcolor(config, vmin = 0, vmax = 3, cmap = PL.cm.binary)
  120. PL.axis('image')
  121. PL.title('t = ' + str(time))
  122.  
  123. def step():
  124. global time, config, nextConfig
  125.  
  126. time += 1
  127.  
  128. for x in range(width):
  129. for y in range(height):
  130. state = config[y, x]
  131. if state == fire:
  132. state = char
  133. elif state == tree:
  134. for dx in range(-1, 2):
  135. for dy in range(-1, 2):
  136. if config[(y+dy)%height, (x+dx)%width] == fire and RD.random < RD.random(ignite_prob):
  137. state = fire
  138. nextConfig[y, x] = state
  139.  
  140. config, nextConfig = nextConfig, config
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement