-
Notifications
You must be signed in to change notification settings - Fork 928
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
AgentSet: Add set
method
#2254
AgentSet: Add set
method
#2254
Conversation
Added a `set` method to the `AgentSet` class that allows setting a specified attribute of all agents within the set to a given value. This method provides a streamlined way to update agent attributes in bulk.
An open question is if this needs |
Performance benchmarks:
|
good idea, and probably an inplace keyword should be included preferably in this PR. It keeps the API clear and consistent across methods. |
Can we think of cases where you want to continue the chain and return an AgentSet? In that case, should a default Maybe I'm now mixing things up with Edit: Changing set seems useful for multiple variables, so that should be possible. agentset.set('has_license', True).set('has_car', True) Or even allow a dict? agentset.set({'has_license': True, 'has_car': True}) |
I see three scenarios here:
Of course you can just ignore the return for 1. So return is not really needed, just return it and the user can decide if they use it or not. Okay, I get it. Now we're in a bit of a pickle, because with I think the most similar function is To summarize:
|
good analysis. There is no point to return a new agent set, just as with do. |
Return the AgentSet itself after performing the `set()` method, to be able to either directly assign the new AgentSet or keep chaining. This is consistent with how `do()`.
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice and simple 🔝
Thanks, I will add a few unittest tonight and merge. |
What I just now realized and what's quite powerful is that you also can set completely new variables this way. Punching above it's weight! |
Adds a `set` method to the `AgentSet` class that allows setting a specified attribute of all agents within the set to a given value. This method provides a streamlined way to update agent attributes in bulk, without having to resort to `lambda` or other callable functions. The motivation behind this feature is to simplify and expedite the process of modifying attributes across all agents in an `AgentSet`. Previously, this required iterating manually over the agents or using a `lambda` or other callable function. This new method makes the operation more elegant. The `set` method was added to the `AgentSet` class. It iterates through all agents in the set and applies the `setattr` function to assign the specified value to the desired attribute. The method operates in place, modifying the existing agents directly. The (now updated) AgentSet itself is returned, which allows for chaining. You can both set existing Agent variables or set new ones.
Adds a `set` method to the `AgentSet` class that allows setting a specified attribute of all agents within the set to a given value. This method provides a streamlined way to update agent attributes in bulk, without having to resort to `lambda` or other callable functions. The motivation behind this feature is to simplify and expedite the process of modifying attributes across all agents in an `AgentSet`. Previously, this required iterating manually over the agents or using a `lambda` or other callable function. This new method makes the operation more elegant. The `set` method was added to the `AgentSet` class. It iterates through all agents in the set and applies the `setattr` function to assign the specified value to the desired attribute. The method operates in place, modifying the existing agents directly. The (now updated) AgentSet itself is returned, which allows for chaining. You can both set existing Agent variables or set new ones.
Interesting case where I couldn't use # Update currently available modes after having assigned cars
self.agents.do(lambda a: setattr(a, 'currently_available_modes', a.available_modes)) Maybe there might be a way to also do this nicely. Or just accept that you have to use an do-lambda-setattr. |
let me check if I understand this correctly, you want to set an agent attribute on the basis of another agent attribute? |
No on the basis of it's own agent attribute. I updated the It would be nice to be able to refer to the agent itself without the whole lambda thing. Not only in (not high priority) |
Sorry, that is what I was trying to say, and, indeed, I agree. Would be a good issue to open and a feature to add at some point. |
This PR adds a
set
method to theAgentSet
class that allows setting a specified attribute of all agents within the set to a given value. This method provides a streamlined way to update agent attributes in bulk, without having to resort tolambda
or other callable functions.Motive
The motivation behind this feature is to simplify and expedite the process of modifying attributes across all agents in an
AgentSet
. Previously, this required iterating manually over the agents or using alambda
or other callable function. This new method makes the operation more elegant.Implementation
The
set
method was added to theAgentSet
class. It iterates through all agents in the set and applies thesetattr
function to assign the specified value to the desired attribute. The method operates in place, modifying the existing agents directly.The (now updated) AgentSet itself is returned, which allows for chaining.
You can both set existing Agent variables or set new ones.
Usage Examples
Note that it doesn't matter if
has_license
already exists in the Agent, both work.Together with #2253, you can now set a value for a fraction of your AgentSet:
It's also possible to set multiple things by chaining the
set()
commands:Additional Notes
It feels logical that if you have a
get
method, you also should have aset
method.Edit:
do()
, so inplace isn't needed.