0

In AnyLogic, I am trying to make a little service loop based on a variable inside an agent. For some reason, my variables are not changing as I expect and I can't make it work.

In my block selectHowMany i have the following code, that is supposed to modify the variable inside customer population:

ped.howMany = ped.howMany + 1.0
ped.howMany = ped.howMany + 2.0
ped.howMany = ped.howMany + 3.0
ped.howMany = ped.howMany + 4.0
ped.howMany = ped.howMany + 5.0

my model with selectHowMany block open

Then, I want the output to determine wether the ped should go to service or to sink, depending if the howMany variable is greater than 1 or not. For that I have another boolean variable isItEnough inside customer population and 'if' statement in output block.

  if(agent.howMany < 1.0){
    agent.isItEnough = false;
}

Condition:

agent.isItEnough = true

output properties

Lastly, at the exit of each of the service blocks I have the following code, so that the variable is actually modifying based on pedestrian's actions:

ped.howMany = ped.howMany - 1.0

My intention was, that pedestrian entering the selectHowMany block will define the variable inside the customer population determining how many times will this pedestrian use one of the service blocks.

No matter how I try the pedestrian is never seizing using blocks (no matter which selectHowMany output it exits), so something is not correct with modifying the variables. The ped is in continuous loop and it never reaches sink.

I hope someone will be able to help me.

Many thanks, Peter

Peter B-S
  • 27
  • 6
  • 1
    Hi @Peter, great effort on the question, happy to help. However, can I suggest you shorten this question and simplify it so it can be grasped in a minute or so? You should be able to condense the problem down to the core issue first, maybe abstract away from your specific model. Make life simple for us, see https://stackoverflow.com/help/how-to-ask and also https://www.benjamin-schumann.com/blog/2021/4/1/how-to-win-at-anylogic-on-stackoverflow – Benjamin Jun 21 '21 at 17:11
  • Many thanks Benjamin, feedback well received. I will see if I can solve it with some hints from Emile and rephrase the question to be more direct. I apologise, It's not that I have not read the how-to-ask, but to be honest I hoped to get the entire idea validated, as I am a rookie with any use of Java. – Peter B-S Jun 22 '21 at 07:29
  • no worries. But you are much more likely to get good answers for short&sweet questions. Best ask several to get your entire idea validated :) – Benjamin Jun 23 '21 at 04:57

1 Answers1

0

For the condition you need to use two equal signs, not one.

agent.isItEnough == true

That is one mistake I was able to identify... if this doesn't solve it, let me know.

Another problem is AnyLogic evaluates the select output condition before it enters the block. Refer to this other question: Unexpected behavior SelectOutput block in AnyLogic

So when you add an action in the On Enter of the select output, it will not impact the decision of that select output. One way to solve it, as mentioned in the link I shared, is to add a dummy delay of 0.01 millisecond for example before the select output element in which you place the following code:

 if(agent.howMany < 1.0){
    agent.isItEnough = false;
}
Emile Zankoul
  • 2,161
  • 2
  • 6
  • 18
  • 1
    Many thanks Emile, this helps a lot. I made the dummy delay, but It didn't solve the problem so far. I actually used the traceln to print out my variables in certain locations and interestingly - the if statement doesn't work at all. Before if statement: ped.howMany - 5.0; ped.isItEnouhg - false; After the if statement: ped.howMany - 5.0; ped.isItEnouhg - false; I will try to dig in this direction more, unless you spot some terrible mistakes here as well? :-) – Peter B-S Jun 22 '21 at 07:31
  • 1
    Solved it just now ! Many thanks ! It was actually quite simple. My if statement never turned the variable to false... Silly mistake, but I am happy to catch it. The final code required also the "else": 'if(agent.howMany >= 1.0){ agent.isItEnough = true; } else{ agent.isItEnough = false; }' Once again, many thanks for your valuable help! All the best, Peter – Peter B-S Jun 22 '21 at 07:38