1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 15:32:29 +00:00
soil/examples/mesa/server.py

115 lines
2.8 KiB
Python
Raw Normal View History

2021-10-14 15:37:06 +00:00
from mesa.visualization.ModularVisualization import ModularServer
from soil.visualization import UserSettableParameter
from mesa.visualization.modules import ChartModule, NetworkModule, CanvasGrid
from social_wealth import MoneyEnv, graph_generator, SocialMoneyAgent
import networkx as nx
2021-10-14 15:37:06 +00:00
class MyNetwork(NetworkModule):
def render(self, model):
return self.portrayal_method(model)
def network_portrayal(env):
# The model ensures there is 0 or 1 agent per node
portrayal = dict()
2022-10-17 18:23:57 +00:00
wealths = {
node_id: data["agent"].wealth for (node_id, data) in env.G.nodes(data=True)
}
2021-10-14 15:37:06 +00:00
portrayal["nodes"] = [
{
"id": node_id,
2022-10-17 18:23:57 +00:00
"size": 2 * (wealth + 1),
"color": "#CC0000" if wealth == 0 else "#007959",
# "color": "#CC0000",
"label": f"{node_id}: {wealth}",
2022-10-17 18:23:57 +00:00
}
for (node_id, wealth) in wealths.items()
2021-10-14 15:37:06 +00:00
]
portrayal["edges"] = [
{"id": edge_id, "source": source, "target": target, "color": "#000000"}
for edge_id, (source, target) in enumerate(env.G.edges)
]
return portrayal
def gridPortrayal(agent):
"""
This function is registered with the visualization server to be called
each tick to indicate how to draw the agent in its current state.
:param agent: the agent in the simulation
:return: the portrayal dictionary
"""
2022-10-17 18:23:57 +00:00
color = max(10, min(agent.wealth * 10, 100))
2021-10-14 15:37:06 +00:00
return {
"Shape": "rect",
"w": 1,
"h": 1,
"Filled": "true",
"Layer": 0,
"Label": agent.unique_id,
"Text": agent.unique_id,
"x": agent.pos[0],
"y": agent.pos[1],
2022-10-17 18:23:57 +00:00
"Color": f"rgba(31, 10, 255, 0.{color})",
2021-10-14 15:37:06 +00:00
}
grid = MyNetwork(network_portrayal, 500, 500)
2021-10-14 15:37:06 +00:00
chart = ChartModule(
[{"Label": "Gini", "Color": "Black"}], data_collector_name="datacollector"
)
model_params = {
"N": UserSettableParameter(
"slider",
"N",
5,
2021-10-14 15:37:06 +00:00
1,
10,
1,
description="Choose how many agents to include in the model",
),
"height": UserSettableParameter(
"slider",
"height",
5,
5,
10,
1,
description="Grid height",
2022-10-17 18:23:57 +00:00
),
2021-10-14 15:37:06 +00:00
"width": UserSettableParameter(
"slider",
"width",
5,
5,
10,
1,
description="Grid width",
2022-10-17 18:23:57 +00:00
),
"agent_class": UserSettableParameter(
"choice",
"Agent class",
value="MoneyAgent",
choices=["MoneyAgent", "SocialMoneyAgent"],
),
"generator": graph_generator,
2021-10-14 15:37:06 +00:00
}
2022-10-17 18:23:57 +00:00
canvas_element = CanvasGrid(
gridPortrayal, model_params["width"].value, model_params["height"].value, 500, 500
)
2021-10-14 15:37:06 +00:00
server = ModularServer(
MoneyEnv, [grid, chart, canvas_element], "Money Model", model_params
)
server.port = 8521
server.launch(open_browser=False)