使用 OOP 对森林火灾建模

In [1]:

  1. %matplotlib inline
  2.  
  3. import matplotlib.pyplot as plt
  4. import numpy as np

对森林建模

In [2]:

  1. class Forest(object):
  2. def __init__(self, size=(150, 150), p_sapling=0.0025, p_lightning=5.e-6, name=None):
  3. self.size = size
  4. self.trees = np.zeros(self.size, dtype=bool)
  5. self.forest_fires = np.zeros(self.size, dtype=bool)
  6. self.p_sapling = p_sapling
  7. self.p_lightning = p_lightning
  8. if name is not None:
  9. self.name = name
  10. else:
  11. self.name = self.__class__.__name__
  12.  
  13. @property
  14. def num_cells(self):
  15. return self.size[0] * self.size[1]
  16.  
  17. @property
  18. def tree_fraction(self):
  19. return self.trees.sum() / float(self.num_cells)
  20.  
  21. @property
  22. def fire_fraction(self):
  23. return self.forest_fires.sum() / float(self.num_cells)
  24.  
  25. def advance_one_step(self):
  26. self.grow_trees()
  27. self.start_fires()
  28. self.burn_trees()
  29.  
  30. def grow_trees(self):
  31. growth_sites = self._rand_bool(self.p_sapling)
  32. self.trees[growth_sites] = True
  33.  
  34. def start_fires(self):
  35. lightning_strikes = (self._rand_bool(self.p_lightning) &
  36. self.trees)
  37. self.forest_fires[lightning_strikes] = True
  38.  
  39. def burn_trees(self):
  40. fires = np.zeros((self.size[0] + 2, self.size[1] + 2), dtype=bool)
  41. fires[1:-1, 1:-1] = self.forest_fires
  42. north = fires[:-2, 1:-1]
  43. south = fires[2:, 1:-1]
  44. east = fires[1:-1, :-2]
  45. west = fires[1:-1, 2:]
  46. new_fires = (north | south | east | west) & self.trees
  47. self.trees[self.forest_fires] = False
  48. self.forest_fires = new_fires
  49.  
  50. def _rand_bool(self, p):
  51. return np.random.uniform(size=self.trees.shape) < p

定义一个森林类之后,我们创建一个新的森林类对象:

In [3]:

  1. forest = Forest()

显示当前的状态:

In [4]:

  1. print forest.trees
  1. [[False False False ..., False False False]
  2. [False False False ..., False False False]
  3. [False False False ..., False False False]
  4. ...,
  5. [False False False ..., False False False]
  6. [False False False ..., False False False]
  7. [False False False ..., False False False]]

In [5]:

  1. print forest.forest_fires
  1. [[False False False ..., False False False]
  2. [False False False ..., False False False]
  3. [False False False ..., False False False]
  4. ...,
  5. [False False False ..., False False False]
  6. [False False False ..., False False False]
  7. [False False False ..., False False False]]

使用 matshow 进行可视化:

In [6]:

  1. plt.matshow(forest.trees, cmap=plt.cm.Greens)
  2.  
  3. plt.show()

08.02 使用 OOP 对森林火灾建模 - 图1

模拟森林生长和火灾的过程

经过一段时间:

In [7]:

  1. forest.advance_one_step()
  2. plt.matshow(forest.trees, cmap=plt.cm.Greens)
  3. plt.show()

08.02 使用 OOP 对森林火灾建模 - 图2

循环很长时间:

In [8]:

  1. for i in range(500):
  2. forest.advance_one_step()
  3. plt.matshow(forest.trees, cmap=plt.cm.Greens)
  4. print forest.tree_fraction
  1. 0.253111111111

08.02 使用 OOP 对森林火灾建模 - 图3

迭代更长时间:

In [9]:

  1. forest = Forest()
  2. tree_fractions = []
  3. for i in range(5000):
  4. forest.advance_one_step()
  5. tree_fractions.append(forest.tree_fraction)
  6. fig = plt.figure()
  7. ax0 = fig.add_subplot(1,2,1)
  8. ax0.matshow(forest.trees, cmap=plt.cm.Greens)
  9. ax1 = fig.add_subplot(1,2,2)
  10. ax1.plot(tree_fractions)
  11.  
  12. plt.show()

08.02 使用 OOP 对森林火灾建模 - 图4

原文: https://nbviewer.jupyter.org/github/lijin-THU/notes-python/blob/master/08-object-oriented-programming/08.02-using-oop-model-a-forest-fire.ipynb