'In Anylogic, how to randomly select agents from a collection of collections
I am new to anylogic and am trying to build an agent-based model which randomly directs agents (vehicles) from a starting point (a Home location point on GIS map) to a selection of locations. I have 6 different sets of collections which have varying respective locations stored within them (SchoolsCollection, SupermarketsCollection, ShoppingCentresCollection, HospitalsCollection, TourismCollection and WorkOfficeCollection).
Within my statechart for my vehicles, I instruct it to visit 4 locations. So far I have only been able to make the agent move to a random location within one of the collections in each transition using: moveTo(randomFrom(main.HospitalsCollection)) for example.
But as I want my agent to randomly pick a location from the six collections above and not revisit the same class of collection from its previous journey, I am unsure on how to:
move my agent to randomly select a location from the collection of collections I have above
when moving on to the next location via the transition in my statechart, to not revsit the previous type of location it was just at (ie not go from one school in one trip and the following trip go to another school). From my limited experience, I would imagine this make use an if loop?
If you could help me with the above two queries that would be much appreciated!
Thanks in advance.
Solution 1:[1]
Either do the approach by Felipe or you can also group your elements in 1 large LinkedHashMap collection where the keys are a String (or better an OptionList with the types "Hospital", "School"...) and the values are ArrayList<GISPoint> with all the actual points.
(LinkedHashMaps are a standard Java data type, check the web to learn about them).
Re avoiding revisiting: Best have a collection in each Vehicle placesVisted (type ArrayList<GISPoint> or similar) and store each place you already visited. Then before moving somewhere else, first check if you have been there previously
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Benjamin |
