-
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
Lakshayb30/issue2221 #2250
Lakshayb30/issue2221 #2250
Conversation
Performance benchmarks:
|
Thanks for the PR, could you add a proper title and description to it? See https://github.com/projectmesa/mesa/blob/main/.github/PULL_REQUEST_TEMPLATE/feature.md?plain=1 |
Sorry for the inconvenience @EwoutH , I have updated the description for my PR. |
Thanks! I have a few conceptual thoughts:
Wolf.create(n=5, model=self, energy=4)
Sheep.create(n=5, model=self, energy=2)
Then we need to make a call if the convenience of such a function is worth it against the implicitness. I think it might be (and I love beautiful code). Your PR is already useful, because now we're thinking and talking about these things! |
Hi @lakshayb30! I don't know if you're still interested in working on this, but if so, automatic |
Here's an implementation that might work: import inspect
class Agent:
def __init__(self, model: 'Model', **kwargs):
self.model = model
for key, value in kwargs.items():
setattr(self, key, value)
self.model.register_agent(self)
@classmethod
def create(cls: Type['Agent'], n: int = 1, **kwargs) -> list['Agent']:
frame = inspect.currentframe().f_back
model = frame.f_locals.get('self')
if not isinstance(model, Model):
raise ValueError("create() must be called from within a Model instance method")
return [cls(model=model, **kwargs) for _ in range(n)] The Then you should be able to do something like: class PredatorPreyModel(Model):
def __init__(self):
...
Wolf.create(n=5, energy=4)
Sheep.create(n=10, energy=2) |
What would be really powerful you could input weighted distributions (in dict form, with the keys as values and values as weights) and random distributions. class PredatorPreyModel(Model):
def __init__(self):
...
Wolf.create(n=5, energy=4)
Sheep.create(n=10, energy={3: 0.25, 4: 0.5, 5:0.25})
Grass.create(n=10, growth_rate=random.triangular(20, 60, 30)) Implementation wise you check for a function (if so evaluate it) and dicts (if so sample it). |
I would separate this out into several steps (which is also why this is not an a good first issue
The tricky thing now will be to handle the 3 options under 2 in an elegant way. And, to make it even a bit more complicated, hybrids of the three options. you probably get something roughly like @classmethod
def create(cls, n, model, **kwargs):
for i in range(n):
agent_kwargs = {}
for key, value in kwargs:
if isinstance(value, list):
value = value[i]
if isinstance(value, callable):
value = value() # not sure this works, should be tied to an rng etc.
agent_kwargs[key] value
agent = cls(model, **agent_kwargs)
|
This PR is stale and is picked up again in #2351. |
New function under class agentset to create multiple agents of a single class.
Added a new method create_agents() to class AgentSet, which is able to create as many agents of the same class without looping through each one.
The create_agents method has been implemented within the AgentSet class. It takes the class of agents, the number of agents to be created, and other arguments to be passed to the constructor of the agent as input. The use of list comprehension is used to create the agents and then returns the list of the created agents.
an example for its usage :
agent_set.create_agents(BarCustomer, self.num_agents, memory_size, crowd_threshold, num_strategies)