Conditional Event Triggering Anylogic

926 Views Asked by At

Is there another way to use the conditional event to monitor parking spots? The end goal is to see on the events output log, when a car has parked and where. I made an event, that calls a function, that runs a loop through each parking space and returns true on a spot that is taken. I wanted to trigger this event whenever a car has parked but the looping is causing the simulation to freeze.

Function(){
for(int i = 0; i<29; i++) //29 = number of parking spaces
    {
        if(parkingLot2.getCarOnSpace(i) != null) //if spot i taken
        {
            return true; 
            //true sent back to event, is then triggered
        }       
    }
return false;
}

Event
condition: Function();
Action: event.restart();
1

There are 1 best solutions below

2
On

So first the event.restart() function only applies if the event has trigger type: timeout and mode: user control, otherwise your event.restart() function does nothing...

Second, you need to call your function not on a conditional event, but on the moment a car parks... You can do this on the "on exit" action of the carMoveTo block.

And your function can be done better using nSpaces instead of 29:

for(int i = 0; i<parkingLot2.nSpaces(); i++)
    {
        if(parkingLot2.getCarOnSpace(i) != null)
        {
            return true; 
        }       
    }
return false;

You can use a similar function to know in what space the car parked, but you need to have a separate array collecting info on which spaces are free or not since the parkingLot object doesn't have that function. Imagine you have an array with size parkingLot2.nSpaces() and boolean elements all starting with false since all the parking spaces are free. Whenever your car enters the parking space, you use the same function but instead of "return true" you set the array to be true in that particular index. And you have to set the array to false when the car exits.