From 13b5c8a08f6680a00ce59ada80a3eda6ed5725f3 Mon Sep 17 00:00:00 2001 From: Spartan-71 Date: Sun, 16 Mar 2025 15:32:58 +0530 Subject: [PATCH 01/11] refactor: adapt boltzmman example for mesa 3.1.4 --- .../boltzmann_wealth_model_network/agent.py | 21 +++++++ .../boltzmann_wealth_model_network/app.py | 52 ++++++++++++++++ .../boltzmann_wealth_model_network/model.py | 35 ++--------- .../boltzmann_wealth_model_network/server.py | 60 ------------------- .../boltzmann_wealth_model_network/run.py | 3 - 5 files changed, 77 insertions(+), 94 deletions(-) create mode 100644 examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py create mode 100644 examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py delete mode 100644 examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/server.py delete mode 100644 examples/boltzmann_wealth_model_network/run.py diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py new file mode 100644 index 00000000..dfab8f6c --- /dev/null +++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py @@ -0,0 +1,21 @@ +from mesa.experimental.cell_space import CellAgent + +class MoneyAgent(CellAgent): + """ An agent with fixed initial wealth""" + + def __init__(self,model): + super().__init__(model) + self.wealth = 1 + + def give_money(self): + neighbours = [agent for agent in self.cell.neighborhood.agents] + if len(neighbours) > 0: + other = self.random.choice(neighbours) + other.wealth += 1 + self.wealth -= 1 + + def step(self): + if self.wealth > 0: + self.give_money() + + diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py new file mode 100644 index 00000000..c58eeef0 --- /dev/null +++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py @@ -0,0 +1,52 @@ +from mesa.visualization import ( + Slider, + SolaraViz, + make_space_component, + make_plot_component, +) + +from mesa.space import NetworkGrid +from model import BoltzmannWealthModelNetwork + +def agent_portrayal(agent): + return { + "color": "red" if agent.wealth == 0 else "green", + "size" : 30, + } + +model_params = { + "num_agents": Slider( + label = "Number of agents", + value = 10, + min = 5, + max = 20, + step = 1, + ), + "num_nodes" : Slider( + label = "Number of nodes", + value = 10, + min = 5, + max = 20, + step = 1, + ), +} + +def post_process_lineplot(ax): + ax.set_ylim(ymin=0) + ax.set_xlim(xmin=0) + +SpacePlot = make_space_component(agent_portrayal) +GiniPlot = make_plot_component("Gini",post_process=post_process_lineplot) + +model = BoltzmannWealthModelNetwork() + +page = SolaraViz( + model, + components = [ + GiniPlot, + SpacePlot + ], + model_params = model_params, + name = "Boltzmann_wealth_model_network", +) +page \ No newline at end of file diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py index fa671ce3..6740af08 100644 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py +++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py @@ -1,6 +1,7 @@ import mesa import networkx as nx - +from mesa.experimental.cell_space import Network +from agent import MoneyAgent def compute_gini(model): agent_wealths = [agent.wealth for agent in model.agents] @@ -13,18 +14,17 @@ def compute_gini(model): class BoltzmannWealthModelNetwork(mesa.Model): """A model with some number of agents.""" - def __init__(self, num_agents=7, num_nodes=10): + def __init__(self, num_agents=10, num_nodes=10): super().__init__() self.num_agents = num_agents self.num_nodes = num_nodes if num_nodes >= self.num_agents else self.num_agents self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=0.5) - self.grid = mesa.experimental.cell_space.Network( + self.grid = Network( self.G, random=self.random, capacity=1 ) self.datacollector = mesa.DataCollector( model_reporters={"Gini": compute_gini}, - agent_reporters={"Wealth": lambda _: _.wealth}, ) list_of_random_nodes = self.random.sample(list(self.G), self.num_agents) @@ -32,7 +32,6 @@ def __init__(self, num_agents=7, num_nodes=10): # Create agents for position in list_of_random_nodes: agent = MoneyAgent(self) - # Add the agent to a random node agent.move_to(self.grid[position]) @@ -44,29 +43,3 @@ def step(self): # collect data self.datacollector.collect(self) - def run_model(self, n): - for i in range(n): - self.step() - - -class MoneyAgent(mesa.experimental.cell_space.CellAgent): - """An agent with fixed initial wealth.""" - - def __init__(self, model): - super().__init__(model) - self.wealth = 1 - - def give_money(self): - neighbors = [agent for agent in self.cell.neighborhood.agents if not self] - if len(neighbors) > 0: - other = self.random.choice(neighbors) - other.wealth += 1 - self.wealth -= 1 - - def step(self): - empty_neighbors = [cell for cell in self.cell.neighborhood if cell.is_empty] - if empty_neighbors: - self.cell = self.random.choice(empty_neighbors) - - if self.wealth > 0: - self.give_money() diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/server.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/server.py deleted file mode 100644 index 50a019ce..00000000 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/server.py +++ /dev/null @@ -1,60 +0,0 @@ -import mesa - -from .model import BoltzmannWealthModelNetwork - - -def network_portrayal(G): - # The model ensures there is 0 or 1 agent per node - - portrayal = {} - portrayal["nodes"] = [ - { - "id": node_id, - "size": 3 if agents else 1, - "color": "#CC0000" if not agents or agents[0].wealth == 0 else "#007959", - "label": ( - None - if not agents - else f"Agent:{agents[0].unique_id} Wealth:{agents[0].wealth}" - ), - } - for (node_id, agents) in G.nodes.data("agent") - ] - - portrayal["edges"] = [ - {"id": edge_id, "source": source, "target": target, "color": "#000000"} - for edge_id, (source, target) in enumerate(G.edges) - ] - - return portrayal - - -grid = mesa.visualization.NetworkModule(network_portrayal, 500, 500) -chart = mesa.visualization.ChartModule( - [{"Label": "Gini", "Color": "Black"}], data_collector_name="datacollector" -) - -model_params = { - "num_agents": mesa.visualization.Slider( - "Number of agents", - 7, - 2, - 10, - 1, - description="Choose how many agents to include in the model", - ), - "num_nodes": mesa.visualization.Slider( - "Number of nodes", - 10, - 3, - 12, - 1, - description="Choose how many nodes to include in the model, with at " - "least the same number of agents", - ), -} - -server = mesa.visualization.ModularServer( - BoltzmannWealthModelNetwork, [grid, chart], "Money Model", model_params -) -server.port = 8521 diff --git a/examples/boltzmann_wealth_model_network/run.py b/examples/boltzmann_wealth_model_network/run.py deleted file mode 100644 index eb60c904..00000000 --- a/examples/boltzmann_wealth_model_network/run.py +++ /dev/null @@ -1,3 +0,0 @@ -from boltzmann_wealth_model_network.server import server - -server.launch(open_browser=True) From 863cc10b6f9611e28b94b2859ba54c01d15f2345 Mon Sep 17 00:00:00 2001 From: Spartan-71 Date: Sun, 16 Mar 2025 15:35:24 +0530 Subject: [PATCH 02/11] chore: updated requirements.txt for Mesa 3.1.4 compatibility --- examples/boltzmann_wealth_model_network/requirements.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/boltzmann_wealth_model_network/requirements.txt b/examples/boltzmann_wealth_model_network/requirements.txt index e9403e6c..64d4aebd 100644 --- a/examples/boltzmann_wealth_model_network/requirements.txt +++ b/examples/boltzmann_wealth_model_network/requirements.txt @@ -1,5 +1,5 @@ -jupyter -matplotlib -mesa~=2.0 -numpy +mesa==3.1.4 +solara networkx +matplotlib +altair \ No newline at end of file From db3c30f70cb79705d48eaf1aa133ea5e740cbd99 Mon Sep 17 00:00:00 2001 From: Spartan-71 Date: Sun, 16 Mar 2025 15:46:25 +0530 Subject: [PATCH 03/11] chore: updated readme for new file structure --- examples/boltzmann_wealth_model_network/README.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/boltzmann_wealth_model_network/README.md b/examples/boltzmann_wealth_model_network/README.md index cd3bcd8d..577c3d5f 100644 --- a/examples/boltzmann_wealth_model_network/README.md +++ b/examples/boltzmann_wealth_model_network/README.md @@ -10,7 +10,6 @@ In this network implementation, agents must be located on a node, with a limit o As the model runs, the distribution of wealth among agents goes from being perfectly uniform (all agents have the same starting wealth), to highly skewed -- a small number have high wealth, more have none at all. -JavaScript library used in this example to render the network: [sigma.js](http://sigmajs.org/). ## Installation @@ -22,19 +21,19 @@ To install the dependencies use pip and the requirements.txt in this directory. ## How to Run -To run the model interactively, run ``mesa runserver`` in this directory. e.g. +To run the model interactively, run ``solara run app.py`` in this directory. ``` - $ mesa runserver + $ solara run app.py ``` -Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run. +Then open your browser to [http://127.0.0.1:8765/](http://127.0.0.1:8765/) and press Reset, then Run. ## Files -* ``run.py``: Launches a model visualization server. -* ``model.py``: Contains the agent class, and the overall model class. -* ``server.py``: Defines classes for visualizing the model (network layout) in the browser via Mesa's modular server, and instantiates a visualization server. +* ``agent.py``: Defines the agent class. +* ``model.py``: Defines the model class. +* ``app.py`` : Defines the visualization and spins up a solara server. ## Further Reading From fce868237faf91e4acf34b628f5c604ff5b2cb7f Mon Sep 17 00:00:00 2001 From: Spartan-71 Date: Sun, 16 Mar 2025 15:51:53 +0530 Subject: [PATCH 04/11] style: format code using Black --- .../boltzmann_wealth_model_network/agent.py | 7 ++- .../boltzmann_wealth_model_network/app.py | 43 ++++++++++--------- .../boltzmann_wealth_model_network/model.py | 6 +-- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py index dfab8f6c..11257e5d 100644 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py +++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py @@ -1,9 +1,10 @@ from mesa.experimental.cell_space import CellAgent + class MoneyAgent(CellAgent): - """ An agent with fixed initial wealth""" + """An agent with fixed initial wealth""" - def __init__(self,model): + def __init__(self, model): super().__init__(model) self.wealth = 1 @@ -17,5 +18,3 @@ def give_money(self): def step(self): if self.wealth > 0: self.give_money() - - diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py index c58eeef0..f179eec7 100644 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py +++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py @@ -8,45 +8,46 @@ from mesa.space import NetworkGrid from model import BoltzmannWealthModelNetwork + def agent_portrayal(agent): return { "color": "red" if agent.wealth == 0 else "green", - "size" : 30, - } + "size": 30, + } + model_params = { "num_agents": Slider( - label = "Number of agents", - value = 10, - min = 5, - max = 20, - step = 1, + label="Number of agents", + value=10, + min=5, + max=20, + step=1, ), - "num_nodes" : Slider( - label = "Number of nodes", - value = 10, - min = 5, - max = 20, - step = 1, + "num_nodes": Slider( + label="Number of nodes", + value=10, + min=5, + max=20, + step=1, ), } + def post_process_lineplot(ax): ax.set_ylim(ymin=0) ax.set_xlim(xmin=0) + SpacePlot = make_space_component(agent_portrayal) -GiniPlot = make_plot_component("Gini",post_process=post_process_lineplot) +GiniPlot = make_plot_component("Gini", post_process=post_process_lineplot) model = BoltzmannWealthModelNetwork() page = SolaraViz( model, - components = [ - GiniPlot, - SpacePlot - ], - model_params = model_params, - name = "Boltzmann_wealth_model_network", + components=[GiniPlot, SpacePlot], + model_params=model_params, + name="Boltzmann_wealth_model_network", ) -page \ No newline at end of file +page diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py index 6740af08..d5523349 100644 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py +++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py @@ -3,6 +3,7 @@ from mesa.experimental.cell_space import Network from agent import MoneyAgent + def compute_gini(model): agent_wealths = [agent.wealth for agent in model.agents] x = sorted(agent_wealths) @@ -19,9 +20,7 @@ def __init__(self, num_agents=10, num_nodes=10): self.num_agents = num_agents self.num_nodes = num_nodes if num_nodes >= self.num_agents else self.num_agents self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=0.5) - self.grid = Network( - self.G, random=self.random, capacity=1 - ) + self.grid = Network(self.G, random=self.random, capacity=1) self.datacollector = mesa.DataCollector( model_reporters={"Gini": compute_gini}, @@ -42,4 +41,3 @@ def step(self): self.agents.shuffle_do("step") # collect data self.datacollector.collect(self) - From 96edde418a7bf86d1025bbbf4b915fad8deba245 Mon Sep 17 00:00:00 2001 From: Spartan-71 Date: Sun, 16 Mar 2025 16:23:11 +0530 Subject: [PATCH 05/11] chore: fix pre-commit errors --- .../boltzmann_wealth_model_network/agent.py | 2 +- .../boltzmann_wealth_model_network/app.py | 5 +---- .../boltzmann_wealth_model_network/model.py | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py index 11257e5d..f44339df 100644 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py +++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py @@ -9,7 +9,7 @@ def __init__(self, model): self.wealth = 1 def give_money(self): - neighbours = [agent for agent in self.cell.neighborhood.agents] + neighbours = list(self.cell.neighborhood.agents) if len(neighbours) > 0: other = self.random.choice(neighbours) other.wealth += 1 diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py index f179eec7..fadefc1b 100644 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py +++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py @@ -1,11 +1,9 @@ from mesa.visualization import ( Slider, SolaraViz, - make_space_component, make_plot_component, + make_space_component, ) - -from mesa.space import NetworkGrid from model import BoltzmannWealthModelNetwork @@ -50,4 +48,3 @@ def post_process_lineplot(ax): model_params=model_params, name="Boltzmann_wealth_model_network", ) -page diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py index d5523349..b0551ab0 100644 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py +++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py @@ -1,7 +1,7 @@ import mesa import networkx as nx -from mesa.experimental.cell_space import Network from agent import MoneyAgent +from mesa.experimental.cell_space import Network def compute_gini(model): From 009a8cc73ad3a86cd817662ece4e5beae1f03060 Mon Sep 17 00:00:00 2001 From: Spartan-71 Date: Wed, 19 Mar 2025 19:54:27 +0530 Subject: [PATCH 06/11] chore: restructured file structure --- .../{boltzmann_wealth_model_network => }/__init__.py | 0 .../{boltzmann_wealth_model_network => }/agent.py | 1 - .../{boltzmann_wealth_model_network => }/app.py | 0 .../{boltzmann_wealth_model_network => }/model.py | 2 +- 4 files changed, 1 insertion(+), 2 deletions(-) rename examples/boltzmann_wealth_model_network/{boltzmann_wealth_model_network => }/__init__.py (100%) rename examples/boltzmann_wealth_model_network/{boltzmann_wealth_model_network => }/agent.py (99%) rename examples/boltzmann_wealth_model_network/{boltzmann_wealth_model_network => }/app.py (100%) rename examples/boltzmann_wealth_model_network/{boltzmann_wealth_model_network => }/model.py (97%) diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/__init__.py b/examples/boltzmann_wealth_model_network/__init__.py similarity index 100% rename from examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/__init__.py rename to examples/boltzmann_wealth_model_network/__init__.py diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py b/examples/boltzmann_wealth_model_network/agent.py similarity index 99% rename from examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py rename to examples/boltzmann_wealth_model_network/agent.py index f44339df..8e5ddb81 100644 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py +++ b/examples/boltzmann_wealth_model_network/agent.py @@ -1,6 +1,5 @@ from mesa.experimental.cell_space import CellAgent - class MoneyAgent(CellAgent): """An agent with fixed initial wealth""" diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py b/examples/boltzmann_wealth_model_network/app.py similarity index 100% rename from examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/app.py rename to examples/boltzmann_wealth_model_network/app.py diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py b/examples/boltzmann_wealth_model_network/model.py similarity index 97% rename from examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py rename to examples/boltzmann_wealth_model_network/model.py index b0551ab0..ea174624 100644 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py +++ b/examples/boltzmann_wealth_model_network/model.py @@ -1,6 +1,6 @@ import mesa import networkx as nx -from agent import MoneyAgent +from .agent import MoneyAgent from mesa.experimental.cell_space import Network From 9657b57282bf4b25ce32248249da5fdfbdcdbdd4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 19 Mar 2025 14:25:00 +0000 Subject: [PATCH 07/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/boltzmann_wealth_model_network/agent.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/boltzmann_wealth_model_network/agent.py b/examples/boltzmann_wealth_model_network/agent.py index 8e5ddb81..f44339df 100644 --- a/examples/boltzmann_wealth_model_network/agent.py +++ b/examples/boltzmann_wealth_model_network/agent.py @@ -1,5 +1,6 @@ from mesa.experimental.cell_space import CellAgent + class MoneyAgent(CellAgent): """An agent with fixed initial wealth""" From 4ae6426f66478623af1e42404f5ec0065be5ed86 Mon Sep 17 00:00:00 2001 From: Spartan-71 Date: Wed, 19 Mar 2025 19:59:09 +0530 Subject: [PATCH 08/11] style: formatted using ruff --- examples/boltzmann_wealth_model_network/model.py | 3 ++- examples/hotelling_law/tests.py | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/examples/boltzmann_wealth_model_network/model.py b/examples/boltzmann_wealth_model_network/model.py index ea174624..d26c45df 100644 --- a/examples/boltzmann_wealth_model_network/model.py +++ b/examples/boltzmann_wealth_model_network/model.py @@ -1,8 +1,9 @@ import mesa import networkx as nx -from .agent import MoneyAgent from mesa.experimental.cell_space import Network +from .agent import MoneyAgent + def compute_gini(model): agent_wealths = [agent.wealth for agent in model.agents] diff --git a/examples/hotelling_law/tests.py b/examples/hotelling_law/tests.py index 3d5a25fe..7787fa8a 100644 --- a/examples/hotelling_law/tests.py +++ b/examples/hotelling_law/tests.py @@ -32,9 +32,9 @@ def test_decreasing_price_variance(): df_model = model.datacollector.get_model_vars_dataframe() - assert check_slope( - df_model["Price Variance"], increasing=False - ), "The price variance should decrease over time." + assert check_slope(df_model["Price Variance"], increasing=False), ( + "The price variance should decrease over time." + ) def test_constant_price_variance(): @@ -53,6 +53,6 @@ def test_constant_price_variance(): df_model = model.datacollector.get_model_vars_dataframe() - assert ( - get_slope(df_model["Price Variance"]) == 0 - ), "The price variance constant over time." + assert get_slope(df_model["Price Variance"]) == 0, ( + "The price variance constant over time." + ) From 0c38d344bd6462f40c594765ccd5dfcf3696a3e5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 19 Mar 2025 14:30:29 +0000 Subject: [PATCH 09/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/hotelling_law/tests.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/hotelling_law/tests.py b/examples/hotelling_law/tests.py index 7787fa8a..3d5a25fe 100644 --- a/examples/hotelling_law/tests.py +++ b/examples/hotelling_law/tests.py @@ -32,9 +32,9 @@ def test_decreasing_price_variance(): df_model = model.datacollector.get_model_vars_dataframe() - assert check_slope(df_model["Price Variance"], increasing=False), ( - "The price variance should decrease over time." - ) + assert check_slope( + df_model["Price Variance"], increasing=False + ), "The price variance should decrease over time." def test_constant_price_variance(): @@ -53,6 +53,6 @@ def test_constant_price_variance(): df_model = model.datacollector.get_model_vars_dataframe() - assert get_slope(df_model["Price Variance"]) == 0, ( - "The price variance constant over time." - ) + assert ( + get_slope(df_model["Price Variance"]) == 0 + ), "The price variance constant over time." From c3d37540d74660ff49fdcc1cd2c10413f9713fd0 Mon Sep 17 00:00:00 2001 From: Spartan-71 Date: Mon, 14 Apr 2025 23:04:52 +0530 Subject: [PATCH 10/11] chore: resturcutred --- examples/boltzmann_wealth_model_network/README.md | 5 +---- examples/boltzmann_wealth_model_network/app.py | 7 +++++-- .../{ => boltzmann_wealth_model_network}/__init__.py | 0 .../{ => boltzmann_wealth_model_network}/agent.py | 0 .../{ => boltzmann_wealth_model_network}/model.py | 0 5 files changed, 6 insertions(+), 6 deletions(-) rename examples/boltzmann_wealth_model_network/{ => boltzmann_wealth_model_network}/__init__.py (100%) rename examples/boltzmann_wealth_model_network/{ => boltzmann_wealth_model_network}/agent.py (100%) rename examples/boltzmann_wealth_model_network/{ => boltzmann_wealth_model_network}/model.py (100%) diff --git a/examples/boltzmann_wealth_model_network/README.md b/examples/boltzmann_wealth_model_network/README.md index 577c3d5f..a5106471 100644 --- a/examples/boltzmann_wealth_model_network/README.md +++ b/examples/boltzmann_wealth_model_network/README.md @@ -4,7 +4,7 @@ This is the same Boltzmann Wealth Model, but with a network grid implementation. -A simple model of agents exchanging wealth. All agents start with the same amount of money. Every step, each agent with one unit of money or more gives one unit of wealth to another random agent. This is the model described in the [Intro Tutorial](https://mesa.readthedocs.io/en/latest/tutorials/intro_tutorial.html). +A simple model of agents exchanging wealth. All agents start with the same amount of money. Every step, each agent with one unit of money or more gives one unit of wealth to another random agent. This is the model described in the [Intro Tutorial](https://mesa.readthedocs.io/latest/tutorials/0_first_model.html). In this network implementation, agents must be located on a node, with a limit of one agent per node. In order to give or receive the unit of money, the agent must be directly connected to the other agent (there must be a direct link between the nodes). @@ -37,9 +37,6 @@ Then open your browser to [http://127.0.0.1:8765/](http://127.0.0.1:8765/) and p ## Further Reading -The full tutorial describing how the model is built can be found at: -https://mesa.readthedocs.io/en/latest/tutorials/intro_tutorial.html - This model is drawn from econophysics and presents a statistical mechanics approach to wealth distribution. Some examples of further reading on the topic can be found at: [Milakovic, M. A Statistical Equilibrium Model of Wealth Distribution. February, 2001.](https://editorialexpress.com/cgi-bin/conference/download.cgi?db_name=SCE2001&paper_id=214) diff --git a/examples/boltzmann_wealth_model_network/app.py b/examples/boltzmann_wealth_model_network/app.py index fadefc1b..9e569939 100644 --- a/examples/boltzmann_wealth_model_network/app.py +++ b/examples/boltzmann_wealth_model_network/app.py @@ -1,10 +1,10 @@ +from boltzmann_wealth_model_network.model import BoltzmannWealthModelNetwork from mesa.visualization import ( Slider, SolaraViz, make_plot_component, make_space_component, ) -from model import BoltzmannWealthModelNetwork def agent_portrayal(agent): @@ -44,7 +44,10 @@ def post_process_lineplot(ax): page = SolaraViz( model, - components=[GiniPlot, SpacePlot], + components=[ + SpacePlot, + GiniPlot, + ], model_params=model_params, name="Boltzmann_wealth_model_network", ) diff --git a/examples/boltzmann_wealth_model_network/__init__.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/__init__.py similarity index 100% rename from examples/boltzmann_wealth_model_network/__init__.py rename to examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/__init__.py diff --git a/examples/boltzmann_wealth_model_network/agent.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py similarity index 100% rename from examples/boltzmann_wealth_model_network/agent.py rename to examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py diff --git a/examples/boltzmann_wealth_model_network/model.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py similarity index 100% rename from examples/boltzmann_wealth_model_network/model.py rename to examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py From 2c9a720b87395af399513c7e1a7af446ee1864e5 Mon Sep 17 00:00:00 2001 From: Spartan-71 Date: Mon, 21 Apr 2025 22:16:53 +0530 Subject: [PATCH 11/11] chore: fix imports and add warning msg --- .../boltzmann_wealth_model_network/agent.py | 2 +- .../boltzmann_wealth_model_network/model.py | 13 +++++++++++-- .../boltzmann_wealth_model_network/requirements.txt | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py index f44339df..0278850b 100644 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py +++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/agent.py @@ -1,4 +1,4 @@ -from mesa.experimental.cell_space import CellAgent +from mesa.discrete_space import CellAgent class MoneyAgent(CellAgent): diff --git a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py index d26c45df..45c52227 100644 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py +++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py @@ -1,6 +1,6 @@ import mesa import networkx as nx -from mesa.experimental.cell_space import Network +from mesa.discrete_space import Network from .agent import MoneyAgent @@ -19,7 +19,16 @@ class BoltzmannWealthModelNetwork(mesa.Model): def __init__(self, num_agents=10, num_nodes=10): super().__init__() self.num_agents = num_agents - self.num_nodes = num_nodes if num_nodes >= self.num_agents else self.num_agents + if self.num_agents > num_nodes: + self.num_nodes = self.num_agents + print(""" + ╔═══════════════════════════════════ Warning ════════════════════════════════════════╗ + ║ Number of agents > Number of nodes. ║ + ║ Since each node can hold only one agent, so num_nodes has been set to num_agents. ║ + ╚════════════════════════════════════════════════════════════════════════════════════╝ + """) + else: + self.num_nodes = num_nodes self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=0.5) self.grid = Network(self.G, random=self.random, capacity=1) diff --git a/examples/boltzmann_wealth_model_network/requirements.txt b/examples/boltzmann_wealth_model_network/requirements.txt index 64d4aebd..2fa9e43f 100644 --- a/examples/boltzmann_wealth_model_network/requirements.txt +++ b/examples/boltzmann_wealth_model_network/requirements.txt @@ -1,4 +1,4 @@ -mesa==3.1.4 +mesa solara networkx matplotlib