-
Notifications
You must be signed in to change notification settings - Fork 883
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
A system for managing Model/Agent default values and ranges #2268
Comments
My initial idea playing with this is something like: # Define dataclass somewhere (called ParameterSpec in this case)
from dataclasses import dataclass
@dataclass
class ParameterSpec:
default: any
min_val: any = None
max_val: any = None
step: any = None # Modify the Mesa Model to initialize this ParameterSpec's default value,
# if no other value is passed
class Model():
def __init__(self, **kwargs):
....
# Iterate through each attribute defined in the class
for key, value in self.__class__.__dict__.items():
if isinstance(value, ParameterSpec):
# Set the value from kwargs or use the default from ParameterSpec
setattr(self, key, kwargs.get(key, value.default)) # Allow the users to define class-level variables in ParameterSpec form
class MyModel(BaseModel):
wealth_spec = ParameterSpec(100, min_val=50, max_val=150)
area_spec = ParameterSpec(10, min_val=5, max_val=20)
def __init__(self, **kwargs):
super().__init__(**kwargs) # Calls BaseModel.__init__
# Additional initialization code here # The model can now be initialized without any input values
model_default = MyModel()
print(model_default.wealth) # Output will be 100
print(model_default.area) # Output will be 10
# But they can also be easily overwritten
model_custom = MyModel(wealth=120, area=15)
print(model_custom.wealth) # Output will be 120
print(model_custom.area) # Output will be 15 The only thing we now required is the Now:
Edit: |
I really like the basic idea! I think one of the challenges is to catch all basic parameter types. You outlined numeric values, but we should also handle strings (maybe default and list of options) and Boolean values. Of course theoretical any python can be an input parameter. Maybe there is an elegant way of handling this? Also for numerical values, not the highest priority, but maybe already think about how we could handle non-linear scaling (maybe you have a range between 1e3 and 1e6 or the like) But will be super useful |
Thanks for you insights! I was also churning om some similar things
We could add a parameter Maybe an explicit
Also thinking about this. Ideally, Finally, how do you see this integrating with visualisation? It might remove a lot of the boilerplate you have to write in For non-numeric, it might just be able to be a drop-down menu or something like that. |
I like the idea. Drawing on my experience with the workbench, you would need something like the following:
Booleans can be useful, but in the workbench they are are subclassed from Subclassing from ParameterSpec might be the best idea. It forces the user to be explicit about the nature of each parameter and how it can be handled. In particular, ordered vs. unordered sets is an important distinction. I would be hesitant to include the |
https://scientific-python.org/specs/ might be the place if we want to take the high-effort route. |
Let's not wait for that if we think this is a useful idea of MESA. If a default SPEC emerges for this, we might choose to start following that. I doubt, however, that it will come because the nature of ABMs is quite different from many other simulation problems in that for those you typically only need real parameters. |
We could start making sure it’s compatible with the workbench.
Decoupling sampling strategies from the parameter ranges seems to be a good idea indeed. |
This is a great idea! |
Okay, I thought about this a bit more. Basically we have the four main for data types:
You could add more details with Stevens's typology of level of measurement, but (for now) I don't think that's necessary. Boolean is a bit weird, because it can basically be a special case of either ordered or categorial data. So what's the bare minimum you need to sample each? Categorical (unordered) sets
Ordered sets
Taking these two, we can already observe they align quite well. They can probably be one class, with an boolean attribute for Discrete (interval) values
Continuous values (return:
|
Some resources linked from @tupui and @ConnectedSystems over in SALib/SALib#634:
Might be interesting if we can learn something from them! |
Okay, to move forward:
What does everybody think? What am I missing or should be different? CC @tupui and @ConnectedSystems |
Let's centralize the discussion to Scientific Python, so we can get all ideas in one place: I will make a little introduction there with my thoughts on the problem from the perspective of Mesa. I would love the same from other maintainers from other libraries! |
@EwoutH Since I saw you are working on a new batch-runner, let me just very quickly outline some of the possibilities with class MyModel:
n = param.Integer(10, bounds=(0, None), soft_bounds=(0, 100), step=5, help="Number of agents") That is we define the number of agents with a default value (10), some hard bounds (must be positive, raises an exception if not), some soft bounds (should be betwenn 0 and 100, e.g. can be picked up by a slider), some step size (not enforced, but can be used for parameter sweeps or again a GUI), and a short help text. Now the interesting part relating to batch running is that besides setting this value directly ( https://param.holoviz.org/user_guide/Dynamic_Parameters.html |
Thanks, I think I’m going to try to integrate it and see how it works. |
Python has a |
Currently, I need to specify default values and sometimes ranges on multiple places:
min
,max
,step
and a defaultvalue
batch_run
I'm defining discrete ranges of variablesIf seems like Mesa could benefit from some way to define default values and/or ranges in a better way that can be used throughout different components.
The text was updated successfully, but these errors were encountered: