Home > Enterprise >  Anylogic- Function not changing boolean variable
Anylogic- Function not changing boolean variable

Time:08-09

In Anylogic I have a variable that increases and decreases when agents pass through certain blocks.

The first variable 'robot4count' is a double, when the double reaches a value above 5 I want another variable 'robot4free' to change to false, and return to true when below 5. I would also like the variable to constantly update

The function I have written so far :

    if(robot4count<5){
    robot4free=true;
    }
    else if(robot4count>5){
    robot4free=false;
    };

Unfortunately the variable robot4free does not change. The functions access is public, set to 'Just action (returns nothing), and I do not have any arguments set. The double 'robot4count' changes but the boolean 'robot4free' doesn't.

Is there a problem with my code, or have the properties been set incorrectly? Any help would be great, thanks. (NOTE: I am new to Anylogic and have little experience in java)

CodePudding user response:

First of all I have to point out that your code must first consider whether there is a 5, which is missing from your code. The second point, I hope you can show your calling process, if the counter does not increment or decrement, do you need to consider whether your method is the same call? Or maybe your code has some threading issues causing some count errors? Here is my test, I think it should be fine:

Method

class Method{
public boolean countInt(double robot4count){
    if (robot4count>=0&&robot4count<=5){
        return true;
    }else {
        return false;
    }
}
}

TestCount

@Slf4j
public class TestCount {
public static void main(String[] args) {
    AtomicInteger countInt=new AtomicInteger(0);
    Method method = new Method();
    for (int i = 0; i <10 ; i  ) {
        new Thread(()->{
            int now = countInt.incrementAndGet();
            if (method.countInt(now)) {
                log.debug("true:{}",now);
            }else {
                log.debug("false:{}",now);
            }
        },"" i).start();
    }


}
}

<br/>
<div align=center><img src="https://i.stack.imgur.com/QnI0a.png"></div>

Hope my test can help you!

the test result

CodePudding user response:

For the function to be able to detect if conveyor1 has 5 agents on it I added

robot4count  

to the 'On enter' Action of convey1, and

robot4count-- 

to the 'On exit' Action of queue1.

The function

if(robot4count<5){
robot4free=true;
}
else if(robot4count==5){
robot4free=false;
}
else if(robot4count>5){
robot4free=false;
};

then detects if the variable robot4count is five or over, if so it changes the boolean variable robot4free to false.

selectOutput has the condition robot4free and this means that it allows items through only when there are less than five agents on conveyor1

function4 is called On enter to delay (which is set to 0.1 seconds) through

function4()

The top conveyor only allows 5 agents at a time.

I hope this helps anyone who had the same questions as me.

  • Related