1

I am trying to seize a given number of resources dynamically, but I can't figure out the syntax. In the Resource Sets Dynamic Assignment, each unit is represented by the name of the resource set it belongs to. In the picture, the seize block will seize 3 resources of the set "resourcePool".

I need to seize a specific number of resources for each individual agent. Then I tried creating ArrayList of Resource Pool objects and passing it in the dynamic assignment but it doesn't work as the type doesn't match.

For example, let's say I have an agent which requires 4 resources, so the expression needed is: { resourcePool, resourcePool, resourcePool, resourcePool }. How can I assign this expression in a variable or collection of the agent such that it can be used in the Resource Sets Dynamic Assignment box? I think I should finally get something like:

{{agent.resourceSetsToSeize}}

So how to define "resourceSetsToSeize"?

enter image description here

1 Answers1

1

You were so close. The only issue is that the parameters inside the agent must be of type ResourcePool[][], an array of arrays. To convert an array list, in your case resourceSetsToSeize to array you need to call toArray() but with the argument of the specific array you want to convert it to. So your code should have looked like

{agent.resourceSetsToSeize.toArray(new ResourcePool[resourceSetsToSeize.size()]}

(Assuming that resourceSetsToSeize is a List object

The code can be a bit confusing, see another example below of how to rather use an array as the parameter and then use that directly without converting.

Here is an agent with the parameter of type ResourcePool[][]

enter image description here

When you create the agent you then create this array and put it in the constructor. As you can see you don't need to use the empty constructor and then assign it, you can make use of your parameterized constructor.

enter image description here

And then in the seize object you can simply access the parameter.

enter image description here

Jaco-Ben Vosloo
  • 3,770
  • 2
  • 16
  • 33
  • 1
    In general, don't encourage instantiating agents using the Java `new` syntax because that has subtle side-effects (e.g., things other than 'plain' parameters and variables inside the agent aren't initialised properly unless the undocumented `createAndStart` is called) and, in the Source block case, there is no need to anyway --- just set up the agent's parameters in the "On at exit" action. – Stuart Rossiter Aug 11 '21 at 11:32
  • Excellent comment, thank you @StuartRossiter. I double-checked with support. "The method `createAndStart()` is called by default when an agent exits Source/PedSource blocks. `new resource unit/agent/pedestrian` are safe to use them for creating an agent programmatically, this is what they were developed for." However they confirmed that use `new Agent()` outside of these will have unwanted side effects. – Jaco-Ben Vosloo Aug 19 '21 at 10:26