Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agent size change based on size of figure #1820

Closed
wants to merge 5 commits into from
Closed
Changes from 2 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
22 changes: 19 additions & 3 deletions mesa/experimental/jupyter_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ def make_model():
set_current_step(0)
return model

def calculate_space_size():
pass

def calculate_agent_size():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Matplotlib marker size shouldn't depend on the number of agents. It should depend only on the grid size and the computer's screen size.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be more abstract, like screen width, because in a Jupyter notebook, the width should be even smaller.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, let me think how this can be implemented. Currently the size of each agent is parsed via agent_portrayal and hence it's easier(also less compute expensive) to manipulate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would also be important to show all agents in a smaller window size, hence the implementation can work there. I try to come up with some scaling to fit the screen width.

lower_limit = 5
upper_limit = 50
num_agents = model_parameters["N"]
delta = 0
if model_parameters["N"] > 50:
new_agent_size = max(lower_limit, int(-0.9 * num_agents + 90))
print(new_agent_size)
delta = new_agent_size - upper_limit
return delta

reset_counter = solara.use_reactive(0)
model = solara.use_memo(
make_model, dependencies=[*list(model_parameters.values()), reset_counter.value]
Expand All @@ -61,14 +75,16 @@ def handle_change_model_params(name: str, value: any):

# 3. Set up UI
solara.Markdown(name)
# calculate agent size based on number of users
agent_size_delta = calculate_agent_size()
UserInputs(user_params, on_change=handle_change_model_params)
ModelController(model, play_interval, current_step, set_current_step, reset_counter)

with solara.GridFixed(columns=2):
# 4. Space
if space_drawer == "default":
# draw with the default implementation
make_space(model, agent_portrayal)
make_space(model, agent_portrayal, delta=agent_size_delta)
elif space_drawer:
# if specified, draw agent space with an alternate renderer
space_drawer(model, agent_portrayal)
Expand Down Expand Up @@ -234,7 +250,7 @@ def change_handler(value, name=name):
raise ValueError(f"{input_type} is not a supported input type")


def make_space(model, agent_portrayal):
def make_space(model, agent_portrayal, delta=0):
def portray(g):
x = []
y = []
Expand All @@ -253,7 +269,7 @@ def portray(g):
x.append(i)
y.append(j)
if "size" in data:
s.append(data["size"])
s.append(max(data["size"] + delta, data["min_size"]))
if "color" in data:
c.append(data["color"])
out = {"x": x, "y": y}
Expand Down
Loading