Ephemere
Ephemere

Reputation: 53

How to remove all the agents in a flow chart during the simulation in Anylogic?

I would like to remove all the remaining agents in all the blocks in a flowchart when needed, however, I didn't find any funtion or method from Anylogic to do this. Do you have any idea of how to achieve this?

I tried to write a function which iterates all the blocks, gets the agents in them and removes the agents one by one. But this isn't effective and always throws errors, since I have several different types like Service, Queue, Convey, ConveyorPath, Batch, Wait and so on.

Edit: the code I used. The lists in the for each loops are my collection of different blocks. Kerze is the agent type in these blocks. The errors were like some agents might be at two blocks simultaneously and then taken by enter4 twice, the loops for Wait and Batch also don't work, where I guess contents() isn't the correct function to get the agents in the block.

for (ConveyorPath conveyor : ersterTeilConveyor) {
    List list = conveyor.getAgents();
    for (Object a : list) {
        Kerze k = (Kerze) a;
        traceln(k.currentBlock().getName() + " " + k.getIndex());
        conveyor.removeAgent(k);
        enter4.take(k);
    }
}
for (Agent block : ersterTeilBlock) {
    if (block instanceof Queue) {
        Queue queue = (Queue) block;
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            queue.removeFirst();
        }
    } else if (block instanceof Batch) {
        Batch batch = (Batch) block;
        for (Agent k : batch.contents()) {
            traceln(k.currentBlock().getName() + " " + k.getIndex());
            batch.remove(k);
            enter4.take(k);
        }
    } else if (block instanceof Delay) {
        Delay delay = (Delay) block;
        if (delay.size() > 0) {
            Kerze k = (Kerze) delay.get(0);
            traceln(k.currentBlock().getName() + " " + k.getIndex());
            delay.remove(k);
            enter4.take(k);
        }
    }
}   
for (ConveyorPath conveyor : zweiterTeilConveyor) {
    List list = conveyor.getAgents();
    for (Object a : list) {
        Kerze k = (Kerze) a;
        traceln(k.currentBlock().getName() + " " + k.getIndex());
        conveyor.removeAgent(k);
        enter4.take(k);
    }
}
for (Agent block : zweiterTeilBlock) {
    if (block instanceof Queue) {
        Queue queue = (Queue) block;
        for (Agent k : queue.contents()) {
            traceln(k.currentBlock().getName() + " " + k.getIndex());
            queue.remove(k);
            enter4.take(k);
        }
    } else if (block instanceof Batch) {
        Batch batch = (Batch) block;
        for (Agent k : batch.contents()) {
            traceln(k.currentBlock().getName() + " " + k.getIndex());
            batch.remove(k);
            enter4.take(k);
        }
    } else if (block instanceof Service) {
        Service service = (Service) block;
        int size = service.queueSize();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                Kerze k = (Kerze) service.queueGet(i);
                traceln(k.currentBlock().getName() + " " + k.getIndex());
                service.queueRemove(k);
                enter4.take(k);
            }
        }
        if (service.delaySize() > 0) {
            Kerze k2 = (Kerze) service.delayGet(0);
            traceln(k2.currentBlock().getName() + " " + k2.getIndex());
            service.delayRemove(k2);
            enter4.take(k2);
        }
    } else if (block instanceof Wait) {
        Wait wait = (Wait) block;
        for (Agent k : wait.contents()) {
            traceln(k.currentBlock().getName() + " " + k.getIndex());
            wait.remove(k);
            enter4.take(k);
        }
    }
}

An error was :

Exception during discrete event execution:

root.enter4.output.output:

Internal error: agent 34291 is already pending as 'ready' on some other port: root.enter4.output.output.out

java.lang.RuntimeException: root.enter4.output.output:

Internal error: agent 34291 is already pending as 'ready' on some other port: root.enter4.output.output.out

    at com.anylogic.engine.Engine.error(Unknown Source)

    at com.anylogic.engine.Agent.error(Unknown Source)

    at com.anylogic.engine.Utilities.error(Unknown Source)

    at com.anylogic.engine.Utilities.error(Unknown Source)

    at com.anylogic.libraries.processmodeling.ExtEntityImpl.c(Unknown Source)

    at com.anylogic.libraries.processmodeling.OutputBlock.notifyReady(Unknown Source)

    at com.anylogic.libraries.processmodeling.OutputBuffer.a(Unknown Source)

    at com.anylogic.libraries.processmodeling.OutputBuffer.take(Unknown Source)

    at com.anylogic.libraries.processmodeling.Enter.take(Unknown Source)

    at prozess.Main.removeKerzen2(Main.java:7449)

    at prozess.Main._sink2_onEnter_xjal(Main.java:6164)

    at prozess.Main$14.onEnter(Main.java:1895)

    at prozess.Main$14.onEnter(Main.java:1)

    at com.anylogic.libraries.processmodeling.Sink.b(Unknown Source)

    at com.anylogic.libraries.processmodeling.Sink$1.onEnter(Unknown Source)

    at com.anylogic.libraries.processmodeling.InputBlock$1.b(Unknown Source)

    at com.anylogic.libraries.processmodeling.InPort.a(Unknown Source)

    at com.anylogic.libraries.processmodeling.InPort.receiveImmediately(Unknown Source)

    at com.anylogic.libraries.processmodeling.InputBlock$1.a(Unknown Source)

    at com.anylogic.libraries.processmodeling.OutPort.a(Unknown Source)

    at com.anylogic.libraries.processmodeling.OutPort.b(Unknown Source)

    at com.anylogic.libraries.processmodeling.PlainTransfer$1.a(Unknown Source)

    at com.anylogic.libraries.processmodeling.OutPort.a(Unknown Source)

    at com.anylogic.libraries.processmodeling.OutPort.b(Unknown Source)

    at com.anylogic.libraries.processmodeling.OutPort.a(Unknown Source)

    at com.anylogic.libraries.processmodeling.OutputBlock.a(Unknown Source)

    at com.anylogic.libraries.processmodeling.OutputBlock$2.a(Unknown Source)

    at com.anylogic.libraries.processmodeling.OutputBlock$2.action(Unknown Source)

    at com.anylogic.libraries.processmodeling.AsynchronousExecutor_xjal$a.execute(Unknown Source)

    at com.anylogic.engine.LibraryEventHandler$i.execute(Unknown Source)

    at com.anylogic.engine.Engine.f(Unknown Source)

    at com.anylogic.engine.Engine.fn(Unknown Source)

    at com.anylogic.engine.Engine$i.run(Unknown Source)

Upvotes: 0

Views: 210

Answers (1)

Felipe
Felipe

Reputation: 9421

I'm not going to totally answer your question maybe but this is something you can't do:

for (Agent k : batch.contents()) {
            batch.remove(k);
            enter4.take(k);
        }

Instead you need to create a list of agents in the batch first

List <Agent> as=new ArrayList();
for (Agent k : batch.contents()) {
   as.add(k);
}
for (Agent k : as) {
            batch.remove(k);
            enter4.take(k);
        }

Upvotes: 1

Related Questions