Reputation: 55
I have an agent "TANKFARM" which is a collection of tanks with certain rules as to the order in which they fill up, or their product gets used, etc. The number of these tanks (visible or not) and initial contents - what type of fluid they contain and the amount of such fluid, etc. is all read from an Excel-sheet when the model starts up. The "PRODUCTS" themselves is defined by an agent that on startup reads the list of products, each with their properties, from another Excel-sheet.
Thus in one simulation run the products might be different grades of gasoline. In the next run it may be chemicals, etc. And the initial contents in the tankfarm (products and initial volumes) may also vary from one run to another (defined in spreadsheets for each simulation-run)
So now, the model is configured that the "On Startup" event of the "PRODUCTS" agent reads its content from Excel. It then has an embedded function that set the colors of the different products when the user passes the productID.
The "On startup" event of "TANKFARM" similarly reads the Excel-file with the tankfarm setup. It then runs a function to set the initial conditions of all tanks (also in "On Startup"). So it now sets all parameters, among them the PRODUCTID parameter, does tank.set_capacity() & tank.set_initialAmount()
etc. The Anylogic "tank" object has its InitialBatch set to the parameter "PRODUCTID" and InitialBatchColor to PRODUCTS_AGENT.setColor(PRODUCTID)
(the custom function that returns a Color when the user passes a ProductID). All fine.
The problem now comes in as follows: When Anylogic starts the model run, it creates the TANKFARM object BEFORE it creates the PRODUCTS object. And it then runs the TANKFRAM "On Startup" event BEFORE it runs the PRODUCTS "On Startup" event (and therefore the Excel files get read in the wrong order). Thus the list of products (with their colors) does not yet exist when the TANKFARM executes the code to set the initial amounts in the tanks and therefore the colors of initial product volume in the tanks does not get set correctly - at the point when the customInitialBatch get assigned, the products list is still empty.
I can't find a way to change the customInitialBatchColor AFTER the InitialAmount was set.
So... is there a way to change the creation order of agents, or the sequence in which different agent's "On Startup" events gets called? (OR.. is there a way to change the initial batch (the color of the fluid) AFTER everything was read from and setup in the "On startup" event?)
Upvotes: 1
Views: 633
Reputation: 12793
This is a murky area in AnyLogic as you cannot explicitly change the order of code execution:
Simplest solution: override such problems by not writing custom "OnStartup" code into your individual agents and other places. Put it all together in 1 controllable function on Main.
Alternatively, create your PRODUCTS manually after your TANKFARM using an event.
Or, if both PRODUCTS and TANKFARM live on Main, try deleting both and recreate their populations in the order you need. This will change the code execution...
Many more options, really depends on your specific setup
Upvotes: 2