Search code examples
javasimulationanylogic

Anylogic model ceases execution at a specified time


I don't know why the models stop working at this time of 127.33 days. The model typically runs when I remove this code from the flowcharts, but when I added it, the model halted at the specified time.

Code:

while ( !collection.isEmpty() )
{
Order order = collection.getFirst();
if ( products > 0 )
{
if ( order.amount <= products )
{
order.retailer.products += order.amount;
traceln("order.amount fel backorder: " +order.amount + " for : " +order.retailer);
// remove the demand from the queue
collection.removeFirst();
}
}
}

Problem Screenshot

I've attempted to add code using an event that checks the inventory level is greater than 0 every day, but I've encountered the same issue.


Solution

  • use this code:

    LinkedList <Order> tempOrders=new LinkedList();
    for(Order o : collection){
        tempOrders.add(o);
    }
    
    for(Order o : tempOrders){
        if(products>0){
                 if(order.amounts<=products){
                      order.retailer.products+=order.amount;
                      collection.remove(o);
                 }else{
                traceln("nothing happens because order.amounts is bigger than products... if i had used a while loop it would be stuck");
                 }
          }else{
            traceln("another opportunity for my while loop to get stuck... nothing happens because products are equal to "+products);
          }
    }
    
    if(collection.size()>0){
        traceln("if i had used a while loop, it would be stuck right now");
    }