Search code examples
anylogic

How to access variables in a resource in Anylogic?


I am attempting to simulate the transport of goods via ships. The way the diagram is supposed to work is that a ship agent is supposed to generate from the source at partially random intervals with an amount of supplies, stored in a variable attached to the agent. Then, after transiting to the dock, it begins loading its supplies onto the dock.

Image of Anylogic Diagram

I modeled the docks with the resource pool, because there are two docks, so they two ships can be serviced at a time. However, I also want these docks to have a capacity. Whenever a ship loads at the dock, I need to subtract the ship's supplies from the dock's capacity, to keep track of how much more supplies it can take. I tried to do this in the service, but it only recognizes the agent's variables, not the resource's.

Finally, I want the deliverer source underneath the ships source to create a deliverer every time the dock capacity reaches a certain value, not just at random time intervals. Online searching has told me to use the inject() method, but I don't know how to call it anywhere outside of itself.

Any help would be appreciated, and I am still new at this. Thank you!


Solution

  • Is it a total capacity or individual capacities per dock? If its just total capacity, I would just create a variable capacity inside main and subtract the amount inside the ship agent from it. You could do this for instance at the "on exit" field of the serviceblock, code would be something like

    dockCapacity=dockCapacity-agent.amount

    where agent is the ship agent flowing through the process blocks

    If you want to create individual capacities per dock , you could create a custom agenttype for the resourcePool and inside this type add the variables you need. You then can access these variables inside the actionfields of the resourcePool and several actionfields of the serviceblock(lightbulbs before fields tell you which types you can use) using unit instead of agent. Note that this can be a bit complex as a beginner so you may need to follow some of the AnyLogic tutorials first before figuring this out.

    Another way to work with individual capacities is to use the serviceblock method getResourceUnits() and compare the unit in usage with capacityvariables for each of the docks (which you can put inside main). But I've never used this so far.

    For your last point you could use this code:

    if(dockCapacity<10)Deliver.inject(1);
    

    In this case, if cap. falls below 10, 1 agent is injected trough the Deliver block each time you call this function.

    Good luck!