Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import time
  4.  
  5.  
  6. def generate_forest(density, grid_size):
  7. lattice = np.zeros((grid_size))
  8. number_of_trees = int(np.around(density*grid_size))
  9. #print("number of trees in artificial forest: ", number_of_trees)
  10. tree_sites = np.random.randint(0, grid_size, (number_of_trees))
  11. lattice[tree_sites] = 1
  12. return lattice
  13.  
  14.  
  15. def induce_artificial_fire(lattice):
  16. tree_sites = np.where(lattice == 1)[0]
  17. ignition_location_index = np.random.randint(0, len(tree_sites))
  18. ignition_location = tree_sites[ignition_location_index]
  19. lattice, size_of_fire = burn_down_trees(lattice, ignition_location)
  20. return lattice, size_of_fire
  21.  
  22.  
  23. def burn_down_trees(lattice, starting_point):
  24. size_of_fire = 0
  25. lattice[starting_point] = 2
  26. is_fire_active = True
  27. while (is_fire_active):
  28.  
  29. ignited_sites = np.where(lattice == 2)[0]
  30. for site in ignited_sites:
  31. if (site < GRID_SIDE_LENGTH):
  32. site_above = (GRID_SIZE - GRID_SIDE_LENGTH) + site%GRID_SIDE_LENGTH
  33. else:
  34. site_above = site - GRID_SIDE_LENGTH
  35. if (site >= (GRID_SIZE - GRID_SIDE_LENGTH)):
  36. site_below = site%GRID_SIDE_LENGTH
  37. else:
  38. site_below = site + GRID_SIDE_LENGTH
  39.  
  40. if (site%GRID_SIDE_LENGTH == 0):
  41. site_left = site + (GRID_SIDE_LENGTH - 1)
  42. else:
  43. site_left = site - 1
  44. if (site%GRID_SIDE_LENGTH == (GRID_SIDE_LENGTH-1) ):
  45. site_right = site - (GRID_SIDE_LENGTH - 1)
  46. else:
  47. site_right = site + 1
  48.  
  49. if (lattice[site_above] == 1):
  50. lattice[site_above] = 2
  51.  
  52. if (lattice[site_below] == 1):
  53. lattice[site_below] = 2
  54.  
  55. if (lattice[site_left] == 1):
  56. lattice[site_left] = 2
  57.  
  58. if (lattice[site_right] == 1):
  59. lattice[site_right] = 2
  60.  
  61. lattice[site] = 3
  62. size_of_fire = size_of_fire + 1
  63. ignited_sites = (np.where(lattice == 2))[0]
  64.  
  65. is_fire_active = (len(ignited_sites) > 0)
  66. return lattice, size_of_fire
  67.  
  68.  
  69.  
  70. GRID_SIDE_LENGTH = 128
  71. GRID_SIZE = GRID_SIDE_LENGTH*GRID_SIDE_LENGTH
  72. SIMULATION_LIMIT = 1000
  73.  
  74. pop_up_probability = 0.01
  75. lightning_probability = 0.1
  76.  
  77. lattice = np.zeros((GRID_SIZE))
  78.  
  79.  
  80. #Set up plot environment
  81.  
  82. """
  83. plt.ion()
  84. fig = plt.figure()
  85. ax1 = fig.add_subplot(111)
  86.  
  87. fig3 = plt.figure(3)
  88. ax3 = fig3.add_subplot(111)
  89.  
  90. """
  91. total_number_of_fires = 0
  92. data1 = np.zeros((SIMULATION_LIMIT))
  93. data2 = np.zeros((SIMULATION_LIMIT))
  94.  
  95.  
  96. while (total_number_of_fires < SIMULATION_LIMIT):
  97. #Creation of new trees
  98. for iLattice in range(len(lattice)):
  99. if (lattice[iLattice] != 1):
  100. r = np.random.rand()
  101. if (r < pop_up_probability):
  102. lattice[iLattice] = 1
  103.  
  104.  
  105. tree_locations = np.where(lattice == 1)[0]
  106. tree_density = len(tree_locations)/GRID_SIZE
  107.  
  108. #Lightning strikes
  109. lightning_strike_occured = (np.random.rand() < lightning_probability)
  110. size_of_fire = 0
  111.  
  112. if (lightning_strike_occured):
  113. random_site = np.random.randint(0, GRID_SIZE)
  114. has_ignited_tree = (lattice[random_site] == 1)
  115. if (has_ignited_tree):
  116. #print("Number of trees in original forest: ", len(tree_locations))
  117. lattice, size_of_fire = burn_down_trees(lattice, random_site)
  118. comp_forest = generate_forest(tree_density, GRID_SIZE)
  119. comp_lattice, comp_size_of_fire = induce_artificial_fire(comp_forest)
  120.  
  121. #print("Size of original fire: ", size_of_fire)
  122. #print("Size of artificial fire: ", comp_size_of_fire)
  123. data1[total_number_of_fires] = size_of_fire/GRID_SIZE
  124. data2[total_number_of_fires] = comp_size_of_fire/GRID_SIZE
  125. total_number_of_fires = total_number_of_fires + 1
  126.  
  127. print("Total number of fires ", total_number_of_fires)
  128. artificial_tree_locations = np.where(comp_lattice == 1)[0]
  129. artificial_fire_locations = np.where(comp_lattice == 3)[0]
  130.  
  131. y_coordinates = artificial_tree_locations%GRID_SIDE_LENGTH
  132. x_coordinates = (artificial_tree_locations - y_coordinates)/GRID_SIDE_LENGTH
  133.  
  134. y_coordinates_fire = artificial_fire_locations%GRID_SIDE_LENGTH
  135. x_coordinates_fire = (artificial_fire_locations - y_coordinates_fire)/GRID_SIDE_LENGTH
  136.  
  137. """
  138. ax3.clear()
  139. ax3.scatter(x_coordinates, y_coordinates, s = 10, c = 'g', marker = 's')
  140. ax3.scatter(x_coordinates_fire, y_coordinates_fire, s = 10, c = '#FFA500', marker = 's')
  141. plt.xlim(-1, GRID_SIDE_LENGTH+1)
  142. plt.ylim(-1, GRID_SIDE_LENGTH+1)
  143.  
  144. fig3.suptitle('Density: ' + str(tree_density))
  145.  
  146. fig3.canvas.draw()
  147. """
  148.  
  149.  
  150. tree_locations = np.where(lattice == 1)[0]
  151.  
  152.  
  153.  
  154.  
  155.  
  156. y_coordinates = tree_locations%GRID_SIDE_LENGTH
  157. x_coordinates = (tree_locations - y_coordinates)/GRID_SIDE_LENGTH
  158.  
  159. fire_locations = np.where(lattice == 3)[0]
  160. y_coordinates_fire = fire_locations%GRID_SIDE_LENGTH
  161. x_coordinates_fire = (fire_locations - y_coordinates_fire)/GRID_SIDE_LENGTH
  162.  
  163. #ax1.clear()
  164. #ax1.scatter(x_coordinates, y_coordinates, s = 10, c = 'g', marker = 's')
  165. #c = ax1.scatter(x_coordinates_fire, y_coordinates_fire, s = 10, c = '#FFA500', marker = 's')
  166. #plt.xlim(-1, GRID_SIDE_LENGTH+1)
  167. #plt.ylim(-1, GRID_SIDE_LENGTH+1)
  168. #fig.canvas.draw()
  169. #ax1.scatter(x_coordinates_fire, y_coordinates_fire, s = 10, c = '#ffffff')
  170. #c.remove()
  171. lattice[fire_locations] = 0
  172.  
  173. #fig.canvas.draw()
  174.  
  175.  
  176.  
  177.  
  178.  
  179. #plt.pause(0.001)
  180.  
  181.  
  182. #plt.waitforbuttonpress()
  183.  
  184. original_data = np.sort(data1)[::-1]
  185. artificial_data = np.sort(data2)[::-1]
  186.  
  187. ranking = np.zeros((SIMULATION_LIMIT))
  188.  
  189. for i in range(SIMULATION_LIMIT):
  190. ranking[i] = (i+1)/SIMULATION_LIMIT
  191.  
  192.  
  193. np.save('fire_data.npy', original_data)
  194. np.save('ranking.npy', ranking)
  195. np.save('artificial_data.npy', artificial_data)
  196. #print("original: ", original_data)
  197. #print("artificial: ", artificial_data)
  198. #print("ranking: ", ranking)
  199.  
  200. fig2 = plt.figure(2)
  201. ax2 = fig2.add_subplot(111)
  202. axes = fig2.gca()
  203. ax2.scatter(original_data, ranking, s = 8, c = 'b')
  204. ax2.scatter(artificial_data, ranking, s= 8, c = 'r')
  205. axes.set_xscale('log')
  206. axes.set_yscale('log')
  207. plt.xlim(0.00001, 2)
  208. plt.ylim(0.0000001, 2)
  209.  
  210. fig2.canvas.draw()
  211.  
  212. plt.waitforbuttonpress()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement