mesa icon indicating copy to clipboard operation
mesa copied to clipboard

AgentSet.get is hardcoded to raise AttributeError for agents with missing attribute

Open rht opened this issue 1 year ago • 3 comments

What's the problem this feature will solve?

This is https://github.com/projectmesa/mesa/pull/2044#discussion_r1495594493 turned into an issue. Currently, AgentSet.get requires member agents to possess the attribute(s) it specifies. This may not be convenient if user wants to quickly get the result and intentionally want to specify undefined attribute(s) as None.

Describe the solution you'd like

What I have in mind is to add an extra optional argument called handle_undefined, which defaults to "raise_exception" (raise AttributeError). The other option would be "fallback_none" to fallback to None without raising an exception. Though user might want to fallback to np.nan instead of None, so this idea still needs further iteration.

rht avatar Feb 21 '24 07:02 rht

@EwoutH Here's a brief overview of the proposed change:

Add handle_undefined parameter with default "raise_exception". Introduce fallback_value parameter to specify the value to return when handle_undefined is set to "fallback_value". This modification aims to offer more flexibility and convenience for users dealing with diverse agent attributes. I believe this could be a valuable addition to our project, and I'm eager to hear your thoughts, suggestions, or any concerns you might have regarding this proposal.

ai-naymul avatar Mar 01 '24 13:03 ai-naymul

Generally sounds good. Maybe handle_undefined could just be a boolean, I don't know how many options we are going to add.

fallback_value might be interesting to allow getting a median, or average.

Feel free to start implementing this and open a (draft) PR.

EwoutH avatar Mar 01 '24 14:03 EwoutH

Hi @rht, Could you please have a look at the pr and let me know if I am on the right path.. Thanks :)

ai-naymul avatar Mar 02 '24 04:03 ai-naymul

@quaquel what's your view on this?

EwoutH avatar Sep 03 '24 15:09 EwoutH

It probably would be better to work from #2067, where there is code.

I am fine with the basic idea of adding some fallback value that is user-specifiable. This is analogous to python's dict.get. Code wise, I need to take a look. At a minimum, tests are missing atm.

quaquel avatar Sep 03 '24 15:09 quaquel