@@ -18,7 +18,8 @@ def __init__(self, num_agents=7, num_nodes=10):
1818 self .num_agents = num_agents
1919 self .num_nodes = num_nodes if num_nodes >= self .num_agents else self .num_agents
2020 self .G = nx .erdos_renyi_graph (n = self .num_nodes , p = 0.5 )
21- self .grid = mesa .space .NetworkGrid (self .G )
21+ self .grid = mesa .spaces .Network (self .G , random = self .random , capacity = 1 )
22+
2223
2324 self .datacollector = mesa .DataCollector (
2425 model_reporters = {"Gini" : compute_gini },
@@ -28,11 +29,11 @@ def __init__(self, num_agents=7, num_nodes=10):
2829 list_of_random_nodes = self .random .sample (list (self .G ), self .num_agents )
2930
3031 # Create agents
31- for i in range ( self . num_agents ) :
32- a = MoneyAgent (self )
32+ for position in list_of_random_nodes :
33+ agent = MoneyAgent (self )
3334
3435 # Add the agent to a random node
35- self .grid . place_agent ( a , list_of_random_nodes [ i ])
36+ agent . move_to ( self .grid [ position ])
3637
3738 self .running = True
3839 self .datacollector .collect (self )
@@ -47,31 +48,25 @@ def run_model(self, n):
4748 self .step ()
4849
4950
50- class MoneyAgent (mesa .Agent ):
51+ class MoneyAgent (mesa .spaces . CellAgent ):
5152 """An agent with fixed initial wealth."""
5253
5354 def __init__ (self , model ):
5455 super ().__init__ (model )
5556 self .wealth = 1
5657
57- def move (self ):
58- possible_steps = [
59- node
60- for node in self .model .grid .get_neighborhood (self .pos , include_center = False )
61- if self .model .grid .is_cell_empty (node )
62- ]
63- if len (possible_steps ) > 0 :
64- new_position = self .random .choice (possible_steps )
65- self .model .grid .move_agent (self , new_position )
6658
6759 def give_money (self ):
68- neighbors = self .model . grid . get_neighbors ( self . pos , include_center = False )
60+ neighbors = [ agent for agent in self .cell . neighborhood (). agents if not self ]
6961 if len (neighbors ) > 0 :
7062 other = self .random .choice (neighbors )
7163 other .wealth += 1
7264 self .wealth -= 1
7365
7466 def step (self ):
75- self .move ()
67+ empty_neighbors = [cell for cell in self .cell .neighborhood () if cell .is_empty ]
68+ if empty_neighbors :
69+ self .move_to (self .random .choice (empty_neighbors ))
70+
7671 if self .wealth > 0 :
7772 self .give_money ()
0 commit comments