Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .gitignore
Binary file not shown.
60 changes: 60 additions & 0 deletions examples/charts/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import solara
from charts.agents import Person
from charts.model import Charts
from mesa.visualization import SolaraViz, make_plot_component, make_space_component
from mesa.visualization.components import AgentPortrayalStyle
from mesa.visualization.user_param import Slider

# Colors - same palette as the original server.py (Matplotlib tab10)
RICH_COLOR = "#2ca02c" # green
POOR_COLOR = "#d62728" # red
MID_COLOR = "#1f77b4" # blue


def agent_portrayal(agent):
"""
Returns AgentPortrayalStyle (Mesa 3.x) instead of a dict.
Color encodes wealth class:
green -> savings > rich_threshold (rich)
red -> loans > 10 (poor)
blue -> everything else (middle class)
"""
if not isinstance(agent, Person):
return AgentPortrayalStyle()

color = MID_COLOR

if agent.savings > agent.model.rich_threshold:
color = RICH_COLOR
if agent.loans > 10:
color = POOR_COLOR

return AgentPortrayalStyle(color=color, size=20, marker="o")


model_params = {
"init_people": Slider(label="People", value=25, min=1, max=200, step=1),
"rich_threshold": Slider(label="Rich Threshold", value=10, min=1, max=20, step=1),
"reserve_percent": Slider(label="Reserves (%)", value=50, min=1, max=100, step=1),
}

SpaceGraph = make_space_component(agent_portrayal)

WealthClassChart = make_plot_component(
{"Rich": RICH_COLOR, "Poor": POOR_COLOR, "Middle Class": MID_COLOR}
)

MoneySupplyChart = make_plot_component(
{"Savings": "#9467bd", "Wallets": "#8c564b", "Money": "#17becf", "Loans": "#e377c2"}
)

# Wrap the instance in solara.Reactive to prevent render loops.
# SolaraViz expects Model | solara.Reactive[Model].
model = solara.Reactive(Charts())

page = SolaraViz(
model,
components=[SpaceGraph, WealthClassChart, MoneySupplyChart],
model_params=model_params,
name="Mesa Charts - Bank Reserves",
)
112 changes: 0 additions & 112 deletions examples/charts/charts/server.py

This file was deleted.

7 changes: 5 additions & 2 deletions examples/charts/run.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from charts.server import server
import subprocess
import sys
from pathlib import Path

server.launch(open_browser=True)
app_path = Path(__file__).parent / "app.py"
subprocess.run([sys.executable, "-m", "solara", "run", str(app_path)], check=False)
5 changes: 2 additions & 3 deletions examples/hex_ant/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import matplotlib.pyplot as plt
from agent import AntState
from matplotlib.colors import LinearSegmentedColormap, ListedColormap
from mesa.visualization import SolaraViz, make_space_component
from mesa.visualization.components import PropertyLayerStyle

from .agent import AntState
from .model import AntForaging
from model import AntForaging

plt.rcParams["figure.figsize"] = (10, 10)

Expand Down
3 changes: 1 addition & 2 deletions examples/hex_ant/model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import mesa
from agent import Ant
from mesa.discrete_space import HexGrid

from .agent import Ant


class AntForaging(mesa.Model):
"""
Expand Down
21 changes: 21 additions & 0 deletions examples/hex_snowflake/hex_snowflake/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from hex_snowflake.cell import Cell
from hex_snowflake.model import HexSnowflake
from mesa.visualization import SolaraViz, make_space_component


def agent_portrayal(agent):
if agent is None:
return

if isinstance(agent, Cell):
color = "tab:green" if agent.state == Cell.ALIVE else "tab:grey"
return {"color": color, "size": 20}


model = HexSnowflake()

page = SolaraViz(
model,
components=[make_space_component(agent_portrayal=agent_portrayal)],
name="Hex Snowflake",
)
17 changes: 0 additions & 17 deletions examples/hex_snowflake/hex_snowflake/portrayal.py

This file was deleted.

12 changes: 0 additions & 12 deletions examples/hex_snowflake/hex_snowflake/server.py

This file was deleted.

3 changes: 0 additions & 3 deletions examples/hex_snowflake/run.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from hex_snowflake.server import server

server.launch(open_browser=True)
3 changes: 0 additions & 3 deletions examples/shape_example/run.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from shape_example.server import server

server.launch(open_browser=True)
20 changes: 20 additions & 0 deletions examples/shape_example/shape_example/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from mesa.visualization import SolaraViz, make_space_component
from shape_example.model import ShapeExample, Walker


def agent_portrayal(agent):
if isinstance(agent, Walker):
return {
"color": "tab:green",
"size": 50,
"marker": "^",
}


model = ShapeExample()

page = SolaraViz(
model,
components=[make_space_component(agent_portrayal=agent_portrayal)],
name="Shape Model Example",
)
31 changes: 16 additions & 15 deletions examples/shape_example/shape_example/model.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import mesa
from mesa.discrete_space import OrthogonalMooreGrid
from mesa.discrete_space import CellAgent, OrthogonalMooreGrid


class Walker(mesa.Agent):
class Walker(CellAgent): # ← CellAgent not mesa.Agent
def __init__(self, model, heading=(1, 0)):
super().__init__(model)
self.heading = heading
self.headings = {(1, 0), (0, 1), (-1, 0), (0, -1)}
self.headings = ((1, 0), (0, 1), (-1, 0), (0, -1))

def step(self):
new_cell = self.random.choice(list(self.cell.neighborhood))
if new_cell.is_empty:
self.cell = new_cell
self.heading = self.random.choice(self.headings)


class ShapeExample(mesa.Model):
def __init__(self, num_agents=2, width=20, height=10):
def __init__(self, num_agents=5, width=20, height=10):
super().__init__()
self.num_agents = num_agents # num of agents
self.headings = ((1, 0), (0, 1), (-1, 0), (0, -1)) # tuples are fast
self.num_agents = num_agents
self.headings = ((1, 0), (0, 1), (-1, 0), (0, -1))
self.grid = OrthogonalMooreGrid((width, height), torus=True, random=self.random)

self.make_walker_agents()
self.running = True

def make_walker_agents(self):
for _ in range(self.num_agents):
x = self.random.randrange(self.grid.dimensions[0])
y = self.random.randrange(self.grid.dimensions[1])
cell = self.grid[(x, y)]
cells = self.random.sample(list(self.grid.all_cells), self.num_agents)
for cell in cells:
heading = self.random.choice(self.headings)
# heading = (1, 0)
if cell.is_empty:
a = Walker(self, heading)
a.cell = cell
a = Walker(self, heading)
a.cell = cell

def step(self):
self.agents.shuffle_do("step")
43 changes: 0 additions & 43 deletions examples/shape_example/shape_example/server.py

This file was deleted.