Home > Back-end >  Anylogic creating and calling function to sum multiple variables and create a cumulative variable
Anylogic creating and calling function to sum multiple variables and create a cumulative variable

Time:04-09

I am new to Java, programming, and Anylogic. I want to calculate “sowsInSystem” variable as a sum of all the weekOne, weekTwo, etc. variables. I know that the value in the Initial Value field of the individual variables is calculated at the start of the model only and won’t update automatically, so I am attempting to write and then call a function (getSowNumbers) that will give me the sum of all the individual variables in a new cumulative variable (sowsInSystem) that updates as the model progresses.

Thank you.

Screenshot of variables, function

Screenshot of variables, function

CodePudding user response:

An easy solution to this is to use an Event. You'll want to set the event's trigger type to "Timeout" and mode to "Cyclic". Then, in the event's action, simply write:

sowsInSystem = getSowNumbers();

With this method, "sowsInSystem" is updated every time the event triggers. You can set the cycle time to be 1 second, 1 day, 1 week, or whatever you need it to be. However, I would recommend against setting the cycle time to be unreasonably small (like 1 millisecond) as this will cause many unnecessary computations and slow down your model.

  • Related