2
2
3
3
4
4
def compute_gini (model ):
5
- agent_wealths = [ agent . wealth for agent in model .schedule . agents ]
5
+ agent_wealths = model .agents . get ( "wealth" )
6
6
x = sorted (agent_wealths )
7
7
N = model .num_agents
8
8
B = sum (xi * (N - i ) for i , xi in enumerate (x )) / (N * sum (x ))
@@ -19,27 +19,26 @@ class BoltzmannWealthModel(mesa.Model):
19
19
20
20
def __init__ (self , N = 100 , width = 10 , height = 10 ):
21
21
super ().__init__ ()
22
+ self .running = True # TODO remove this line when at Mesa 3.0
22
23
self .num_agents = N
23
24
self .grid = mesa .space .MultiGrid (width , height , True )
24
- self .schedule = mesa .time .RandomActivation (self )
25
25
self .datacollector = mesa .DataCollector (
26
26
model_reporters = {"Gini" : compute_gini }, agent_reporters = {"Wealth" : "wealth" }
27
27
)
28
28
# Create agents
29
29
for i in range (self .num_agents ):
30
30
a = MoneyAgent (i , self )
31
- self .schedule .add (a )
32
31
# Add the agent to a random grid cell
33
32
x = self .random .randrange (self .grid .width )
34
33
y = self .random .randrange (self .grid .height )
35
34
self .grid .place_agent (a , (x , y ))
36
35
37
- self .running = True
38
36
self .datacollector .collect (self )
39
37
40
38
def step (self ):
41
- self .schedule .step ()
42
- # collect data
39
+ self .agents .shuffle ().do ("step" )
40
+ # Must be before data collection.
41
+ self ._advance_time () # Temporary API; will be finalized by Mesa 3.0 release
43
42
self .datacollector .collect (self )
44
43
45
44
def run_model (self , n ):
@@ -55,17 +54,18 @@ def __init__(self, unique_id, model):
55
54
self .wealth = 1
56
55
57
56
def move (self ):
58
- possible_steps = self .model .grid .get_neighborhood (
57
+ possible_positions = self .model .grid .get_neighborhood (
59
58
self .pos , moore = True , include_center = False
60
59
)
61
- new_position = self .random .choice (possible_steps )
62
- self .model .grid .move_agent (self , new_position )
60
+ self .model .grid .move_agent_to_one_of (self , possible_positions )
63
61
64
62
def give_money (self ):
65
- cellmates = self .model .grid .get_cell_list_contents ([self .pos ])
66
- cellmates .pop (
67
- cellmates .index (self )
68
- ) # Ensure agent is not giving money to itself
63
+ cellmates = [
64
+ c
65
+ for c in self .model .grid .get_cell_list_contents ([self .pos ])
66
+ # Ensure agent is not giving money to itself
67
+ if c is not self
68
+ ]
69
69
if len (cellmates ) > 0 :
70
70
other = self .random .choice (cellmates )
71
71
other .wealth += 1
0 commit comments