ped.getGroup().size() weird behavior - anylogic

I encountered a weird behaviour of one of my groups, am I missing something?
The groups of 1 to 4 are approaching a pedOutput block where under first exit there is this condition:
ped.getGroup().size() <= 2 && v_table02_occupied < 20;
The condition works fine for some time, as I intended ( groups of <=2 exit with 1, otherwise exit 2), until the group of 4 approaches and comes through exit 1. I have no idea why.
It shouldn't be connected to v_table02_occupied variable, but so you know, at the time this group enters the pedOutput block it prints as 16.
Did someone see groups misbehaving like that before?
Thanks,
Peter

The problem is that your v_table02_occupied +1 code is executed after the condition check.
Best execute the code before the condition by using a simple delay of 0 duration in front of the pedSelectOutput. Call the +1 code there in its "on enter" code box.
And then do some reading on how code is executed sequentially, this is a typical pitfall for many as it is so subtle :)

Stupid mistake on my part.
Condition statements seemingly should not end with ;.
The statement that worked:
ped.getGroup().size() <= 2 && v_table02_occupied < 20
Instead of:
ped.getGroup().size() <= 2 && v_table02_occupied < 20;
Thanks Benjamin for taking time to help.
//Peter

Related

Visible candle counter on pinescript v.5

So I don't code at all but was looking for a simple script to tell me how many candles are currently on my chart. how many are visible. I used chatgtp to write me a script. No matter how many different ways i ask the question I always get the same error when compiling.
study("Visible Candle Counter")
count = 0
for i in range(bar_index - 300, bar_index):
if i < 0 or na(close[i]):
break
count := count + 1
plot(count, color=color.blue, linewidth=2, title="Visible Candles")
the error is:Script could not be translated from: |B|for i in range(0,
I've tried asking chatgtp different ways to write the script. i've gotten like 6 iterations of it but they all come up with that same error.

How to use nCars() in Anylogic Flowchart

I'm using a select output block to do something if one track has less railcars on it than another track. Right now I'm using the following code in the if condition is true area:
int x
int y
track1.nCars()=x
track2.nCars()=y
(x.intValue() >= y.intValue());
But this isn't working, and I'm wondering how to use the nCars() function in a flowchart block like this?
You can only write 1 line of code in such a condition code box.
As long as the track1 and track2 objects are in the same agent as the SelectOutput, simply turn your code into a 1-line condition:
track1.nCars() >= track2.nCars()
Agents will exit via the true branch if track1 has more/equal cars than track 2

Roblox Leaderstats not updating/only updating once

Roblox Leaderstats aren't updating correctly.
When told to update leaderstats, it sometimes doesn't update, and other times, it only updates once.
Code is below:
game.ReplicatedStorage.sleep.OnServerEvent:Connect(function(plr)
local en = plr.leaderstats.Energy
en.Value += 1
print(en.value)
wait()
local speed = 5.07 * ((en.Value / 10) + 1)
plr.Character:WaitForChild("Humanoid").WalkSpeed = speed
print(speed)
end)
On times it doesn't update, the print(en.value) says 1 while the leaderstat stays as 0.
When leaderstat only updates once, the print only updates once as well.
Edit: The leaderstat was defined/created in a previous script.
I completely forgot to answer this, but I am using +=, you would assume it would add to the current amount as the hint says, but it doesn't
Old Code: en.Value += 1
What I do now: en.Value = en.Value + 1
You are declaring the "en" variable right before you add one to the value in this function. This means that every time you run this code, you will reset the "en" variable. If you declare the variable outside of the scope of this function, it should get rid of your issues.

Bounded-Waiting Mutual Exclusion with Compare-and-Swap

I added comments to each line to the best of my understanding, but I still don't get why we set waiting[j] = false; at the end without running process j's critical section. In my opinion, waiting[j] = false; should be replaced with i = j; so when it loops again, we run process j's critical section. Or else we'll always be running process i's critical section!
For a process to enter its critical section, waiting[i] must be false, but waiting[i] can only be set to false if a process is leaving its critical section by calling waiting[j] = false, which I take to mean that now process j can enter its critical section prompting process i to wait. I'm still learning these concepts so I'm not 100% sure. Abraham and Silberschatz 9th edition does not do a very thorough job of explaining these algorithms.
Validity of the Algorithm
First, it is important to note that this algorithm solves the critical section problem only when there are two processes (here they are referred to as process 0 and process 1).
Next, as per the convention in Operating System Concepts by Abraham and Silberschatz . Peter B Galvin . Gerge Gagne, i refers to one of the processes amongst 0 and 1 and j refers to the other process.
Mapping of right code to right process
Having said that, it must be noted that this code is for the process i. The code for process j would be obtained by interchanging i and j in the given code. (In my opinion this is what caused confusion to you, since you said the following)
should be replaced with i = j; so when it loops again, we run process j's critical section. Or else we'll always be running process i's critical section!
Finally, consequences of waiting[j] = false (which happens in Process i)
Now, both these codes would execute as two different processes in a system. So as soon as you set waiting[j] = false in the last line of Process i following events occur:
The condition in while(waiting[j] && key == 1) for the code of Process j (Note that the code for Process j is obtained by replacing i with j as explained in the previous heading of Mapping of right code to right process) turns out to be false and therefore Process j resumes it's execution by entering the critical section.
Meanwhile, Process i loops back into the outermost while(true) loop, setting waiting[i] to true, key to 1 and waiting by looping over in the while(waiting[i] && key == 1) loop.

plc structured text loop delay

I am trying to have a loop where it will start at 100 and drop until it hits to a point where the while condition no longer holds true.
I started with
While Solar_Power_House_W_Solar_PER <= OneHundred AND BatChargePercent < OneHundred DO
State_Dis_Charge := false
FOR PLC_SetLoopChargeValue:= 100 TO 0 By -1 DO
ConvertoReal := INT_TO_LREAL(PLC_SetLoopChargeValue);
Divide := ConvertoReal DIV(100);
PLC_SetCharge := Divide;
PLC_Charge := 1500 * PLC_SetCharge;
RB_Charge := PLC_Charge;
Visual_RBPower := 1500 * PLC_SetCharge; (*Charge *)
END_FOR;
The problem I believe I have with this is that it cycles too fast so the condition never gets out of the while loop because it takes a while for the system to update so I thought of adding a delay portion:
While Solar_Power_House_W_Solar_PER <= OneHundred AND BatChargePercent < OneHundred DO
State_Dis_Charge := false;
wait(IN:=not wait.Q , PT:=T#50ms);
if Wait.Q Then
FOR PLC_SetLoopChargeValue:= 100 TO 0 By -1 DO
ConvertoReal := INT_TO_LREAL(PLC_SetLoopChargeValue);
Divide := ConvertoReal DIV(100);
PLC_SetCharge := Divide;
PLC_Charge := 1500 * PLC_SetCharge;
RB_Charge := PLC_Charge;
Visual_RBPower := 1500 * PLC_SetCharge; (*Charge *)
END_FOR;
END_IF;
END_WHILE;
How I think it should work is every 50ms 1 for loop should run. Currently nothing happens every 50ms.
You have to consider that WHILE and FOR are executed synchronously. It means blocking. It means that interpreter do not execute next line, until previous line is finished.
This means that "running to fast" cannot apply here. It does not matter how fast it runs, the execution of the lines will be always in order.
The only thing I would change and loop not from 100 to 0 but vice versa from 0 to 100, because I am not sure this backward will work fine. And then all you have to change:
ConvertoReal := INT_TO_LREAL(100 - PLC_SetLoopChargeValue);
You do now show all code it is VERY HARD to judge but if FOR loom is complete it totally make no sense. You calculate some variables but you do not use them there. You know that you cannot use them outside of your FOR loop, right? Because outside of your FOR loop those variable will be always same value of last loop.
In your second example your FOR loop, although it might work, you should not use timer to run the loop inside the loop. Because loops are synchronous and times async.
As I understand you task you do not need WHILE at all. With this approach your program execution of other parts will be blocked until 100%. That might take a while as I can see. So you have to use IF.
IF Solar_Power_House_W_Solar_PER <= OneHundred AND BatChargePercent < OneHundred DO
// ....
END_IF;
The difference is significant. With WHILE it will block your program till WHILE finish and other parts will not be executed for this long, in the same PLC cycle FOR might be executed so many times.
With IF if will run FOR one time per one PLC cycle and actualy doe snot change your logic.
If you would share your full code or at least parts where variables you have here are used so that the whole picture is visible, you might get a better help. Edit your post and I'll edit my comment.
With this answer im only adressing your issue with the for loop not being executed every 50ms.
The other answers why the while loop cant be exited are correct unless the variables Solar_Power_House_W_Solar_PER and BatChargePercent aren't changed in a parrellel thread.
I suggest wait is a TON function block. Please mind that names of FBs are case sensitive: wait.Q is possibly unequal Wait.Q. I think that is the main reason your for loop is not executed, because you check the output of another FB. Maybe check your declaration list for doubles with higher or lower cases.
Another possibility is, that your condition for the while loop isn't met at all and you didn't notice. In this case the for loop wouldn't be executed too of course.