simulation icon indicating copy to clipboard operation
simulation copied to clipboard

setValue does not work for Variable Primitive

Open herzogrh opened this issue 1 year ago • 5 comments

Currently, it does not seem possible to set Values for Agent Variables programmatically as detailed here. It seems to work well for eg. Stocks inside the Agent, but for Variables the following error occurs ([Height] being a variable primitive):

grafik

After looking into the code, it seems that there is no specific setValue function specified for variable primitives. Because of that, the default error is thrown.

herzogrh avatar Nov 28 '24 23:11 herzogrh

Hi Rico,

setValue() only works with primitives that are stateful, which are the Stock and State primitives. You could trying using a Stock instead of a Variable for your Height.

scottfr avatar Nov 30 '24 09:11 scottfr

Hi Scott, thanks for the quick reply! Both work, but in case I want to let's say store a piece of text in an agent's attribute, calling setValue on a Stock primitive with a String (which works just fine) seems a little counterintuitive.

herzogrh avatar Dec 02 '24 03:12 herzogrh

After experimenting more with ABMs and the simulation engine, I think that it would be useful to have a stateful primitive which stores attributes for each agent without being a state or a stock. For instance, if I want to store and update a target for each agent where it should move towards (instead of calculating this every timestep in an action). Then I encounter the following issue:

  • I cannot update the target vector in an agent variable due to the issue described above
  • I cannot call SetValue({x: 34, x: 34}) for a Stock variable, because I get the error SetValue([Primitive], Value) does not accept vectors for the parameter 'Value'.
  • I cannot set a State Variable, because it only accepts true/false values

herzogrh avatar Mar 18 '25 17:03 herzogrh

Hi Rico,

I think it makes sense to allow setting vectors on stocks using SetValue. I'll try to support that in the next version.

You can also look into using a global variable to store a map of agent index -> value for all your agents. That should work too.

scottfr avatar Mar 23 '25 10:03 scottfr

Hi Scott, thanks for the workaround with the global variable! Will look into that!

herzogrh avatar Apr 10 '25 15:04 herzogrh