@@ -12,7 +12,7 @@ def __init__(self, pos, model, agent_type):
12
12
13
13
Args:
14
14
unique_id: Unique identifier for the agent.
15
- x, y : Agent initial location.
15
+ pos : Agent initial location.
16
16
agent_type: Indicator for the agent's type (minority=1, majority=0)
17
17
"""
18
18
super ().__init__ (pos , model )
@@ -38,48 +38,38 @@ class Schelling(mesa.Model):
38
38
"""
39
39
40
40
def __init__ (self , width = 20 , height = 20 , density = 0.8 , minority_pc = 0.2 , homophily = 3 ):
41
- """ """
42
-
41
+ super ().__init__ ()
43
42
self .width = width
44
43
self .height = height
45
- self .density = density
46
- self .minority_pc = minority_pc
47
44
self .homophily = homophily
48
45
49
- self .schedule = mesa .time .RandomActivation (self )
50
46
self .grid = mesa .space .SingleGrid (width , height , torus = True )
51
47
52
48
self .happy = 0
53
49
self .datacollector = mesa .DataCollector (
54
50
{"happy" : "happy" }, # Model-level count of happy agents
55
- # For testing purposes, agent's individual x and y
56
- {"x" : lambda a : a .pos [0 ], "y" : lambda a : a .pos [1 ]},
57
51
)
58
52
59
53
# Set up agents
60
54
# We use a grid iterator that returns
61
55
# the coordinates of a cell as well as
62
56
# its contents. (coord_iter)
63
- for cell in self .grid .coord_iter ():
64
- x , y = cell [1 ]
65
- if self .random .random () < self .density :
66
- agent_type = 1 if self .random .random () < self .minority_pc else 0
67
-
68
- agent = SchellingAgent ((x , y ), self , agent_type )
69
- self .grid .place_agent (agent , (x , y ))
70
- self .schedule .add (agent )
57
+ for _ , pos in self .grid .coord_iter ():
58
+ if self .random .random () < density :
59
+ agent_type = 1 if self .random .random () < minority_pc else 0
60
+ agent = SchellingAgent (pos , self , agent_type )
61
+ self .grid .place_agent (agent , pos )
71
62
72
- self .running = True
73
63
self .datacollector .collect (self )
74
64
75
65
def step (self ):
76
66
"""
77
67
Run one step of the model. If All agents are happy, halt the model.
78
68
"""
79
69
self .happy = 0 # Reset counter of happy agents
80
- self .schedule . step ( )
70
+ self .agents . shuffle (). do ( "step" )
81
71
# collect data
82
72
self .datacollector .collect (self )
83
73
84
- if self .happy == self .schedule . get_agent_count ( ):
74
+ if self .happy == len ( self .agents ):
85
75
self .running = False
0 commit comments