11import mesa
22
3+ from mesa .experimental .cell_space import OrthogonalMooreGrid
4+
35from .agent import TreeCell
46
57
@@ -8,19 +10,19 @@ class ForestFire(mesa.Model):
810 Simple Forest Fire model.
911 """
1012
11- def __init__ (self , width = 100 , height = 100 , density = 0.65 ):
13+ def __init__ (self , width = 100 , height = 100 , density = 0.65 , seed = None ):
1214 """
1315 Create a new forest fire model.
1416
1517 Args:
1618 width, height: The size of the grid to model
1719 density: What fraction of grid cells have a tree in them.
1820 """
19- super ().__init__ ()
20- # Set up model objects
21+ super ().__init__ (seed = seed )
2122
22- self . grid = mesa . space . SingleGrid ( width , height , torus = False )
23+ # Set up model objects
2324
25+ self .grid = OrthogonalMooreGrid ((width , height ), capacity = 1 )
2426 self .datacollector = mesa .DataCollector (
2527 {
2628 "Fine" : lambda m : self .count_type (m , "Fine" ),
@@ -30,14 +32,13 @@ def __init__(self, width=100, height=100, density=0.65):
3032 )
3133
3234 # Place a tree in each cell with Prob = density
33- for contents , ( x , y ) in self .grid .coord_iter () :
35+ for cell in self .grid .all_cells :
3436 if self .random .random () < density :
3537 # Create a tree
36- new_tree = TreeCell (self )
38+ new_tree = TreeCell (self , cell )
3739 # Set all trees in the first column on fire.
38- if x == 0 :
40+ if cell . coordinate [ 0 ] == 0 :
3941 new_tree .condition = "On Fire"
40- self .grid .place_agent (new_tree , (x , y ))
4142
4243 self .running = True
4344 self .datacollector .collect (self )
@@ -59,8 +60,4 @@ def count_type(model, tree_condition):
5960 """
6061 Helper method to count trees in a given condition in a given model.
6162 """
62- count = 0
63- for tree in model .agents :
64- if tree .condition == tree_condition :
65- count += 1
66- return count
63+ return len (model .agents .select (lambda x : x .condition == tree_condition ))
0 commit comments