Mesa tutorial: plot of agent wealth is deterministic instead of random

469 Views Asked by At

thanks for taking the time. (Python 3.7.0) I'm a beginner at python and doing the Mesa tutorial since I want to make an agent-based model for a research.

I have the following issue: when I run the following code, each time a randomized plot showing the wealth of 10 agents in the model should come up. The agents all start with wealth 1 and randomly trade (=give wealth) with each other. However, the plot is always the same and just shows a stack of value 10! I think there is something wrong in the definition of agent_wealth, but I took it straight from the tutorial.

from mesa_tutorial import * #import all definitions from mesa_tutorial
import matplotlib.pyplot as plt
model = MoneyModel(10)
for i in range(10):
  model.step()
agent_wealth = [a.wealth for a in model.schedule.agents]
plt.hist(agent_wealth)
plt.show()

Resulting in the following plot: non-random plot with stack 10

Here's the definition of the model

class MoneyModel(Model): # define MoneyModel as a Subclass of Model
'''A model with some number (N) of agents'''
  def __init__(self, N):
      #Create N agents
      self.num_agents = N
      self.schedule = RandomActivation(self) #Executes the step of all agents, one at a time, in random order.         
      for i in range(self.num_agents): #loop with a range of N = number of agents           
          a = MoneyAgent(i, self) # no idea what happens here, a = agent?            
          self.schedule.add(a) #adds a to the schedule of the model       

  def step(self):
      '''Advance the model by one step'''         
      self.schedule.step()
1

There are 1 best solutions below

1
On

Can you post your Moneyagent class in this class the agents should randomly exchange money. See the step function below.

# model.py
class MoneyAgent(Agent):
    """ An agent with fixed initial wealth."""
    def __init__(self, unique_id, model):
        super().__init__(unique_id, model)
        self.wealth = 1

    def step(self):
        if self.wealth == 0:
            return
        other_agent = random.choice(self.model.schedule.agents)
        other_agent.wealth += 1
        self.wealth -= 1

With this step function you should start getting a positively skewed distribution or the positive half of a normal distribution, if the agents can go negative.