From 8000dfb460cde273338c3b02b448acb9b1ab3365 Mon Sep 17 00:00:00 2001 From: rht Date: Tue, 24 May 2022 01:26:56 -0400 Subject: [PATCH] examples: Convert to using Slider UserParam class --- examples/bank_reserves/Readme.md | 2 +- .../bank_reserves/bank_reserves/agents.py | 4 ++-- examples/bank_reserves/bank_reserves/model.py | 2 +- .../bank_reserves/bank_reserves/server.py | 10 ++++----- examples/bank_reserves/batch_run.py | 2 +- .../boltzmann_wealth_model/server.py | 3 +-- .../boltzmann_wealth_model_network/server.py | 6 ++--- examples/charts/charts/agents.py | 4 ++-- examples/charts/charts/model.py | 2 +- examples/charts/charts/server.py | 10 ++++----- examples/forest_fire/forest_fire/server.py | 4 +--- examples/schelling/server.py | 12 +++------- .../virus_on_network/server.py | 22 +++++++------------ 13 files changed, 31 insertions(+), 52 deletions(-) diff --git a/examples/bank_reserves/Readme.md b/examples/bank_reserves/Readme.md index 07210604ac2..27570d209b0 100644 --- a/examples/bank_reserves/Readme.md +++ b/examples/bank_reserves/Readme.md @@ -7,7 +7,7 @@ A highly abstracted, simplified model of an economy, with only one type of agent The model demonstrates the following Mesa features: - MultiGrid for creating shareable space for agents - DataCollector for collecting data on individual model runs - - UserSettableParameters for adjusting initial model parameters + - Slider for adjusting initial model parameters - ModularServer for visualization of agent interaction - Agent object inheritance - Using a BatchRunner to collect data on multiple combinations of model parameters diff --git a/examples/bank_reserves/bank_reserves/agents.py b/examples/bank_reserves/bank_reserves/agents.py index b8b22b72386..d59390df084 100644 --- a/examples/bank_reserves/bank_reserves/agents.py +++ b/examples/bank_reserves/bank_reserves/agents.py @@ -20,8 +20,8 @@ def __init__(self, unique_id, model, reserve_percent=50): super().__init__(unique_id, model) # for tracking total value of loans outstanding self.bank_loans = 0 - """percent of deposits the bank must keep in reserves - this is a - UserSettableParameter in server.py""" + """percent of deposits the bank must keep in reserves - this is set via + Slider in server.py""" self.reserve_percent = reserve_percent # for tracking total value of deposits self.deposits = 0 diff --git a/examples/bank_reserves/bank_reserves/model.py b/examples/bank_reserves/bank_reserves/model.py index 2696f0cd418..a27b1bdace6 100644 --- a/examples/bank_reserves/bank_reserves/model.py +++ b/examples/bank_reserves/bank_reserves/model.py @@ -103,7 +103,7 @@ class BankReserves(mesa.Model): grid_w = 20 """init parameters "init_people", "rich_threshold", and "reserve_percent" - are all UserSettableParameters""" + are all set via Slider""" def __init__( self, diff --git a/examples/bank_reserves/bank_reserves/server.py b/examples/bank_reserves/bank_reserves/server.py index 927d080856c..c66cd920b9c 100644 --- a/examples/bank_reserves/bank_reserves/server.py +++ b/examples/bank_reserves/bank_reserves/server.py @@ -50,19 +50,17 @@ def person_portrayal(agent): # dictionary of user settable parameters - these map to the model __init__ parameters model_params = { - "init_people": mesa.visualization.UserSettableParameter( - "slider", "People", 25, 1, 200, description="Initial Number of People" + "init_people": mesa.visualization.Slider( + "People", 25, 1, 200, description="Initial Number of People" ), - "rich_threshold": mesa.visualization.UserSettableParameter( - "slider", + "rich_threshold": mesa.visualization.Slider( "Rich Threshold", 10, 1, 20, description="Upper End of Random Initial Wallet Amount", ), - "reserve_percent": mesa.visualization.UserSettableParameter( - "slider", + "reserve_percent": mesa.visualization.Slider( "Reserves", 50, 1, diff --git a/examples/bank_reserves/batch_run.py b/examples/bank_reserves/batch_run.py index 9f5de64ef86..768fe1876b8 100644 --- a/examples/bank_reserves/batch_run.py +++ b/examples/bank_reserves/batch_run.py @@ -114,7 +114,7 @@ class BankReservesModel(mesa.Model): grid_w = 20 """init parameters "init_people", "rich_threshold", and "reserve_percent" - are all UserSettableParameters""" + are all set via Slider""" def __init__( self, diff --git a/examples/boltzmann_wealth_model/boltzmann_wealth_model/server.py b/examples/boltzmann_wealth_model/boltzmann_wealth_model/server.py index b3a1e22b83c..a49546ce741 100644 --- a/examples/boltzmann_wealth_model/boltzmann_wealth_model/server.py +++ b/examples/boltzmann_wealth_model/boltzmann_wealth_model/server.py @@ -22,8 +22,7 @@ def agent_portrayal(agent): ) model_params = { - "N": mesa.visualization.UserSettableParameter( - "slider", + "N": mesa.visualization.Slider( "Number of agents", 100, 2, 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 index a8e48cce51c..abc493a3e5b 100644 --- a/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/server.py +++ b/examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/server.py @@ -33,8 +33,7 @@ def network_portrayal(G): ) model_params = { - "num_agents": mesa.visualization.UserSettableParameter( - "slider", + "num_agents": mesa.visualization.Slider( "Number of agents", 7, 2, @@ -42,8 +41,7 @@ def network_portrayal(G): 1, description="Choose how many agents to include in the model", ), - "num_nodes": mesa.visualization.UserSettableParameter( - "slider", + "num_nodes": mesa.visualization.Slider( "Number of nodes", 10, 3, diff --git a/examples/charts/charts/agents.py b/examples/charts/charts/agents.py index 67a0956bb03..0d18c453e5f 100644 --- a/examples/charts/charts/agents.py +++ b/examples/charts/charts/agents.py @@ -20,8 +20,8 @@ def __init__(self, unique_id, model, reserve_percent=50): super().__init__(unique_id, model) # for tracking total value of loans outstanding self.bank_loans = 0 - """percent of deposits the bank must keep in reserves - this is a - UserSettableParameter in server.py""" + """percent of deposits the bank must keep in reserves - this is set via + Slider in server.py""" self.reserve_percent = reserve_percent # for tracking total value of deposits self.deposits = 0 diff --git a/examples/charts/charts/model.py b/examples/charts/charts/model.py index 0f26e9b54a8..295dfc27665 100644 --- a/examples/charts/charts/model.py +++ b/examples/charts/charts/model.py @@ -87,7 +87,7 @@ class Charts(mesa.Model): grid_w = 20 """init parameters "init_people", "rich_threshold", and "reserve_percent" - are all UserSettableParameters""" + are all set via Slider""" def __init__( self, diff --git a/examples/charts/charts/server.py b/examples/charts/charts/server.py index 195a3b5a035..ba7bfbdfd51 100644 --- a/examples/charts/charts/server.py +++ b/examples/charts/charts/server.py @@ -50,19 +50,17 @@ def person_portrayal(agent): # dictionary of user settable parameters - these map to the model __init__ parameters model_params = { - "init_people": mesa.visualization.UserSettableParameter( - "slider", "People", 25, 1, 200, description="Initial Number of People" + "init_people": mesa.visualization.Slider( + "People", 25, 1, 200, description="Initial Number of People" ), - "rich_threshold": mesa.visualization.UserSettableParameter( - "slider", + "rich_threshold": mesa.visualization.Slider( "Rich Threshold", 10, 1, 20, description="Upper End of Random Initial Wallet Amount", ), - "reserve_percent": mesa.visualization.UserSettableParameter( - "slider", + "reserve_percent": mesa.visualization.Slider( "Reserves", 50, 1, diff --git a/examples/forest_fire/forest_fire/server.py b/examples/forest_fire/forest_fire/server.py index 253aea0a03a..6d8f9fd31cf 100644 --- a/examples/forest_fire/forest_fire/server.py +++ b/examples/forest_fire/forest_fire/server.py @@ -29,9 +29,7 @@ def forest_fire_portrayal(tree): model_params = { "height": 100, "width": 100, - "density": mesa.visualization.UserSettableParameter( - "slider", "Tree density", 0.65, 0.01, 1.0, 0.01 - ), + "density": mesa.visualization.Slider("Tree density", 0.65, 0.01, 1.0, 0.01), } server = mesa.visualization.ModularServer( ForestFire, [canvas_element, tree_chart, pie_chart], "Forest Fire", model_params diff --git a/examples/schelling/server.py b/examples/schelling/server.py index 3f4d09dbf90..12377491729 100644 --- a/examples/schelling/server.py +++ b/examples/schelling/server.py @@ -39,15 +39,9 @@ def schelling_draw(agent): model_params = { "height": 20, "width": 20, - "density": mesa.visualization.UserSettableParameter( - "slider", "Agent density", 0.8, 0.1, 1.0, 0.1 - ), - "minority_pc": mesa.visualization.UserSettableParameter( - "slider", "Fraction minority", 0.2, 0.00, 1.0, 0.05 - ), - "homophily": mesa.visualization.UserSettableParameter( - "slider", "Homophily", 3, 0, 8, 1 - ), + "density": mesa.visualization.Slider("Agent density", 0.8, 0.1, 1.0, 0.1), + "minority_pc": mesa.visualization.Slider("Fraction minority", 0.2, 0.00, 1.0, 0.05), + "homophily": mesa.visualization.Slider("Homophily", 3, 0, 8, 1), } server = mesa.visualization.ModularServer( diff --git a/examples/virus_on_network/virus_on_network/server.py b/examples/virus_on_network/virus_on_network/server.py index fe7f6028f57..3aa6e4f97b6 100644 --- a/examples/virus_on_network/virus_on_network/server.py +++ b/examples/virus_on_network/virus_on_network/server.py @@ -71,8 +71,7 @@ def render(self, model): model_params = { - "num_nodes": mesa.visualization.UserSettableParameter( - "slider", + "num_nodes": mesa.visualization.Slider( "Number of agents", 10, 10, @@ -80,11 +79,10 @@ def render(self, model): 1, description="Choose how many agents to include in the model", ), - "avg_node_degree": mesa.visualization.UserSettableParameter( - "slider", "Avg Node Degree", 3, 3, 8, 1, description="Avg Node Degree" + "avg_node_degree": mesa.visualization.Slider( + "Avg Node Degree", 3, 3, 8, 1, description="Avg Node Degree" ), - "initial_outbreak_size": mesa.visualization.UserSettableParameter( - "slider", + "initial_outbreak_size": mesa.visualization.Slider( "Initial Outbreak Size", 1, 1, @@ -92,8 +90,7 @@ def render(self, model): 1, description="Initial Outbreak Size", ), - "virus_spread_chance": mesa.visualization.UserSettableParameter( - "slider", + "virus_spread_chance": mesa.visualization.Slider( "Virus Spread Chance", 0.4, 0.0, @@ -101,8 +98,7 @@ def render(self, model): 0.1, description="Probability that susceptible neighbor will be infected", ), - "virus_check_frequency": mesa.visualization.UserSettableParameter( - "slider", + "virus_check_frequency": mesa.visualization.Slider( "Virus Check Frequency", 0.4, 0.0, @@ -110,8 +106,7 @@ def render(self, model): 0.1, description="Frequency the nodes check whether they are infected by " "a virus", ), - "recovery_chance": mesa.visualization.UserSettableParameter( - "slider", + "recovery_chance": mesa.visualization.Slider( "Recovery Chance", 0.3, 0.0, @@ -119,8 +114,7 @@ def render(self, model): 0.1, description="Probability that the virus will be removed", ), - "gain_resistance_chance": mesa.visualization.UserSettableParameter( - "slider", + "gain_resistance_chance": mesa.visualization.Slider( "Gain Resistance Chance", 0.5, 0.0,