11# https://github.com/projectmesa/mesa-examples/blob/main/examples/boltzmann_wealth_model_experimental/model.py
22import mesa
3-
3+ import mesa . spaces as spaces
44
55def compute_gini (model ):
66 agent_wealths = [agent .wealth for agent in model .agents ]
@@ -21,18 +21,18 @@ class BoltzmannWealth(mesa.Model):
2121 def __init__ (self , seed = None , n = 100 , width = 10 , height = 10 ):
2222 super ().__init__ ()
2323 self .num_agents = n
24- self .grid = mesa . space . MultiGrid ( width , height , True )
24+ self .grid = spaces . OrthogonalMooreGrid (( width , height ) )
2525 self .schedule = mesa .time .RandomActivation (self )
2626 self .datacollector = mesa .DataCollector (
2727 model_reporters = {"Gini" : compute_gini }, agent_reporters = {"Wealth" : "wealth" }
2828 )
2929 # Create agents
3030 for _ in range (self .num_agents ):
31- a = MoneyAgent (self )
31+ agent = MoneyAgent (self )
3232 # Add the agent to a random grid cell
33- x = self .random .randrange (self . grid . width )
34- y = self .random .randrange (self . grid . height )
35- self .grid . place_agent ( a , ( x , y ))
33+ x = self .random .randrange (width )
34+ y = self .random .randrange (height )
35+ agent . move_to ( self .grid [( x , y )] )
3636
3737 self .running = True
3838 self .datacollector .collect (self )
@@ -47,31 +47,34 @@ def run_model(self, n):
4747 self .step ()
4848
4949
50- class MoneyAgent (mesa .Agent ):
50+ class MoneyAgent (mesa .spaces . CellAgent ):
5151 """An agent with fixed initial wealth."""
5252
5353 def __init__ (self , model ):
5454 super ().__init__ (model )
5555 self .wealth = 1
5656
57- def move (self ):
58- possible_steps = self .model .grid .get_neighborhood (
59- self .pos , moore = True , include_center = False
60- )
61- new_position = self .random .choice (possible_steps )
62- self .model .grid .move_agent (self , new_position )
57+ # def move(self):
58+ # possible_steps = self.model.grid.get_neighborhood(
59+ # self.pos, moore=True, include_center=False
60+ # )
61+ # new_position = self.random.choice(possible_steps)
62+ # self.model.grid.move_agent(self, new_position)
6363
6464 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
65+ cellmates = [agent for agent in self .cell .agents if not agent is self ]
6966 if len (cellmates ) > 0 :
7067 other = self .random .choice (cellmates )
7168 other .wealth += 1
7269 self .wealth -= 1
7370
7471 def step (self ):
75- self .move ( )
72+ self .move_to ( self . cell . neighborhood (). select_random_cell () )
7673 if self .wealth > 0 :
7774 self .give_money ()
75+
76+
77+ if __name__ == '__main__' :
78+ model = BoltzmannWealth ()
79+ for _ in range (10 ):
80+ model .step ()
0 commit comments