@@ -18,6 +18,7 @@ class BoltzmannWealthModel(mesa.Model):
18
18
"""
19
19
20
20
def __init__ (self , N = 100 , width = 10 , height = 10 ):
21
+ super ().__init__ ()
21
22
self .num_agents = N
22
23
self .grid = mesa .space .MultiGrid (width , height , True )
23
24
self .datacollector = mesa .DataCollector (
@@ -31,12 +32,10 @@ def __init__(self, N=100, width=10, height=10):
31
32
y = self .random .randrange (self .grid .height )
32
33
self .grid .place_agent (a , (x , y ))
33
34
34
- self .running = True
35
35
self .datacollector .collect (self )
36
36
37
37
def step (self ):
38
38
self .agents .shuffle ().do ("step" )
39
- # collect data
40
39
self .datacollector .collect (self )
41
40
42
41
def run_model (self , n ):
@@ -52,23 +51,25 @@ def __init__(self, unique_id, model):
52
51
self .wealth = 1
53
52
54
53
def move (self ):
55
- possible_steps = self .model .grid .get_neighborhood (
54
+ possible_positions = self .model .grid .get_neighborhood (
56
55
self .pos , moore = True , include_center = False
57
56
)
58
- new_position = self .random .choice (possible_steps )
59
- self .model .grid .move_agent (self , new_position )
57
+ self .model .grid .move_agent_to_one_of (self , possible_positions )
60
58
61
59
def give_money (self ):
62
- cellmates = self .model .grid .get_cell_list_contents ([self .pos ])
63
- cellmates .pop (
64
- cellmates .index (self )
65
- ) # Ensure agent is not giving money to itself
60
+ if self .wealth <= 0 :
61
+ return
62
+ cellmates = [
63
+ c
64
+ for c in self .model .grid .get_cell_list_contents ([self .pos ])
65
+ # Ensure agent is not giving money to itself
66
+ if c is not self
67
+ ]
66
68
if len (cellmates ) > 0 :
67
69
other = self .random .choice (cellmates )
68
70
other .wealth += 1
69
71
self .wealth -= 1
70
72
71
73
def step (self ):
72
74
self .move ()
73
- if self .wealth > 0 :
74
- self .give_money ()
75
+ self .give_money ()
0 commit comments