Skip to content

Commit

Permalink
Update tutorials to use create_agents and rng.integers (projectme…
Browse files Browse the repository at this point in the history
…sa#2541)

Updates the tutorials to align with Mesa 3.1's recommended practices for agent creation and random number generation.
  • Loading branch information
DarshPareek authored Dec 14, 2024
1 parent f72a268 commit 366b482
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 48 deletions.
11 changes: 6 additions & 5 deletions docs/tutorials/MoneyModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ def __init__(self, n=10, width=10, height=10, seed=None):
self.grid = mesa.space.MultiGrid(width, height, True)

# Create agents
for _ in range(self.num_agents):
a = MoneyAgent(self)
agents = MoneyAgent.create_agents(model=self, n=n)
# Create x and y positions for agents
x = self.rng.integers(0, self.grid.width, size=(n,))
y = self.rng.integers(0, self.grid.height, size=(n,))
for a, i, j in zip(agents, x, y):
# Add the agent to a random grid cell
x = self.random.randrange(self.grid.width)
y = self.random.randrange(self.grid.height)
self.grid.place_agent(a, (x, y))
self.grid.place_agent(a, (i, j))

self.datacollector = mesa.DataCollector(
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
Expand Down
81 changes: 38 additions & 43 deletions docs/tutorials/intro_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@
" super().__init__(seed=seed)\n",
" self.num_agents = n\n",
" # Create agents\n",
" for _ in range(self.num_agents):\n",
" a = MoneyAgent(self)"
" MoneyAgent.create_agents(model=self, n=n)"
]
},
{
Expand Down Expand Up @@ -295,11 +294,8 @@
" super().__init__(seed=seed)\n",
" self.num_agents = n\n",
"\n",
" # Create agents\n",
" for _ in range(self.num_agents):\n",
" a = MoneyAgent(\n",
" self\n",
" ) # This calls the agent class parameter n number of times\n",
" # Create n agents\n",
" MoneyAgent.create_agents(model=self, n=n)\n",
"\n",
" def step(self):\n",
" \"\"\"Advance the model by one step.\"\"\"\n",
Expand Down Expand Up @@ -478,10 +474,7 @@
" self.num_agents = n\n",
"\n",
" # Create agents\n",
" for _ in range(self.num_agents):\n",
" a = MoneyAgent(\n",
" self\n",
" ) # This calls the agent class parameter n number of times\n",
" MoneyAgent.create_agents(model=self, n=n)\n",
"\n",
" def step(self):\n",
" \"\"\"Advance the model by one step.\"\"\"\n",
Expand Down Expand Up @@ -620,13 +613,13 @@
" self.grid = mesa.space.MultiGrid(width, height, True)\n",
"\n",
" # Create agents\n",
" for _ in range(self.num_agents):\n",
" a = MoneyAgent(self)\n",
"\n",
" agents = MoneyAgent.create_agents(model=self, n=n)\n",
" # Create x and y positions for agents\n",
" x = self.rng.randint(0, self.grid.width, size=(n,))\n",
" y = self.rng.randint(0, self.grid.height, size=(n,))\n",
" for a, i, j in zip(agents, x, y):\n",
" # Add the agent to a random grid cell\n",
" x = self.random.randrange(self.grid.width)\n",
" y = self.random.randrange(self.grid.height)\n",
" self.grid.place_agent(a, (x, y))"
" self.grid.place_agent(a, (i, j))"
]
},
{
Expand Down Expand Up @@ -724,12 +717,13 @@
" self.grid = mesa.space.MultiGrid(width, height, True)\n",
"\n",
" # Create agents\n",
" for _ in range(self.num_agents):\n",
" a = MoneyAgent(self)\n",
" agents = MoneyAgent.create_agents(model=self, n=n)\n",
" # Create x and y coordinates for agents\n",
" x = self.rng.integers(0, self.grid.width, size=(n,))\n",
" y = self.rng.integers(0, self.grid.height, size=(n,))\n",
" for a, i, j in zip(agents, x, y):\n",
" # Add the agent to a random grid cell\n",
" x = self.random.randrange(self.grid.width)\n",
" y = self.random.randrange(self.grid.height)\n",
" self.grid.place_agent(a, (x, y))\n",
" self.grid.place_agent(a, (i, j))\n",
"\n",
" def step(self):\n",
" self.agents.shuffle_do(\"move\")\n",
Expand Down Expand Up @@ -852,7 +846,7 @@
" other = self.random.choice(cellmates)\n",
" other.wealth += 1\n",
" self.wealth -= 1\n",
" \n",
"\n",
"\n",
"class MoneyModel(mesa.Model):\n",
" \"\"\"A model with some number of agents.\"\"\"\n",
Expand All @@ -868,12 +862,13 @@
" )\n",
"\n",
" # Create agents\n",
" for _ in range(self.num_agents):\n",
" a = MoneyAgent(self)\n",
" agents = MoneyAgent.create_agents(model=self, n=n)\n",
" # Create x and y positions for agents\n",
" x = self.rng.integers(0, self.grid.width, size=(n,))\n",
" y = self.rng.integers(0, self.grid.height, size=(n,))\n",
" for a, i, j in zip(agents, x, y):\n",
" # Add the agent to a random grid cell\n",
" x = self.random.randrange(self.grid.width)\n",
" y = self.random.randrange(self.grid.height)\n",
" self.grid.place_agent(a, (x, y))\n",
" self.grid.place_agent(a, (i, j))\n",
"\n",
" def step(self):\n",
" self.datacollector.collect(self)\n",
Expand Down Expand Up @@ -1137,12 +1132,13 @@
" self.grid = mesa.space.MultiGrid(width, height, True)\n",
"\n",
" # Create agents\n",
" for _ in range(self.num_agents):\n",
" a = MoneyAgent(self)\n",
" agents = MoneyAgent.create_agents(model=self, n=n)\n",
" # Create x and y positions for agents\n",
" x = self.rng.integers(0, self.grid.width, size=(n,))\n",
" y = self.rng.integers(0, self.grid.height, size=(n,))\n",
" for a, i, j in zip(agents, x, y):\n",
" # Add the agent to a random grid cell\n",
" x = self.random.randrange(self.grid.width)\n",
" y = self.random.randrange(self.grid.height)\n",
" self.grid.place_agent(a, (x, y))\n",
" self.grid.place_agent(a, (i, j))\n",
"\n",
" self.datacollector = mesa.DataCollector(\n",
" model_reporters={\"Gini\": compute_gini}, agent_reporters={\"Wealth\": \"wealth\"}\n",
Expand Down Expand Up @@ -1234,8 +1230,7 @@
" self.num_agents = n\n",
"\n",
" # Create agents\n",
" for _ in range(self.num_agents):\n",
" a = MoneyAgent(self)\n",
" MoneyAgent.create_agents(model=self, n=n)\n",
"\n",
" self.datacollector = mesa.DataCollector(\n",
" model_reporters={\"Gini\": compute_gini}, agent_reporters={\"Wealth\": \"wealth\"}\n",
Expand Down Expand Up @@ -1320,8 +1315,7 @@
" ethnicities = [\"Green\", \"Blue\", \"Mixed\"]\n",
"\n",
" # Create agents\n",
" for _ in range(self.num_agents):\n",
" a = MoneyAgent(self, self.random.choice(ethnicities))\n",
" MoneyAgent.create_agents(model=self, n=n, ethnicity=self.random.choice(ethnicities))\n",
"\n",
" self.datacollector = mesa.DataCollector(\n",
" model_reporters={\"Gini\": compute_gini},\n",
Expand Down Expand Up @@ -1406,12 +1400,13 @@
" self.running = True\n",
"\n",
" # Create agents\n",
" for _ in range(self.num_agents):\n",
" a = MoneyAgent(self)\n",
" agents = MoneyAgent.create_agents(model=self, n=n)\n",
" # Create x and y positions for agents\n",
" x = self.rng.integers(0, self.grid.width, size=(n,))\n",
" y = self.rng.integers(0, self.grid.height, size=(n,))\n",
" for a, i, j in zip(agents, x, y):\n",
" # Add the agent to a random grid cell\n",
" x = self.random.randrange(self.grid.width)\n",
" y = self.random.randrange(self.grid.height)\n",
" self.grid.place_agent(a, (x, y))\n",
" self.grid.place_agent(a, (i, j))\n",
"\n",
" self.datacollector = mesa.DataCollector(\n",
" model_reporters={\"Gini\": compute_gini},\n",
Expand Down Expand Up @@ -1902,7 +1897,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
"version": "3.12.3"
},
"widgets": {
"state": {},
Expand Down

0 comments on commit 366b482

Please sign in to comment.