简体   繁体   中英

Q: [Anylogic] How to calculate the sum of variables of all agents that is waiting in the queue of seize block?

I am still new to Anylogic. I need a suggestion to solve the problem. For example, right now there are 3 agents waiting in the queue of the seize block. first agent contains variable 6 Second agent contains variable 5 Third agent contains variable 6 Thus, sum of variable from 3 agents is 17. NOTE: these three variables are the same type(integer).

Question : I would like to know is there any way to calculate this value?

Explanation of the model.

There is a single product which will be processed by two workers. Basically, a product has to be processed by 3 tasks, which are task A, task B and task C. The task A and C are called “Fixed-task”. Task A will be done by worker 1 at work station 1. In the same way, Task C will be done by worker 2 at station 2. However, the task B, which is the “Shared-task”, can be done by either worker 1 or 2 at their own station. Every time the worker 1 finished doing task A, he has to decide whether he will place the product at the buffer for worker 2 to do the shared task or he will be doing the shared task by himself.

First of all, the product flows from the source block, then it will be processed by the first worker at the first workstation, after worker 1 finished his fixed task, he will be making a decision whether he will do or pass the shared task to worker2. The decision based on the total number of subtasks that are in the buffer in front of station 2 (Let's assign this value = T). Value T will be compared to value R, which obtained by calculation. If T is more than R , then the worker 1 will do the shared task, otherwise he will let worker 2 do it.

My idea is, I have created a variable in the agent (product) that is the number of remaining subtasks to be done. This value will be embedded when the agent exited the selectoutput block true or false. The problem is, at the buffer, I do not know how to calculate the T value which is the value of all remaining subtasks that worker 2 has to do.

Regarding to my question, how to obtain the total number of subtasks (T) in the buffer?

请点击查看模型

Thank you in advance.

AnyLogic allows to collect such statistics with Stream API in the following manner: seize.queue.waitingEntities.stream().mapToInt( a -> a.variable ).sum();'

Usually, such manner is more readable than for loop.

This is how I understand your question:

  1. You have a number of agents in the seize queue
  2. All agents are the same, meaning that they all contain a variable, called "variable" that can take any integer value
  3. You want to know what is the sum of the variable of all the agents that are in the seize queue block

If I understand your question correctly, you do it like this:

int var=0;
for(int i=0;i<seize.size();i++){
    var+=seize.get(i).variable;
}

"var" gives you the number you want to calculate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM