I’m using AnyLogic PLE v8.9.5 for a digital twin project where my current Simulation
experiment integrates a Genetic Algorithm (GA) loop:
Python GA writes parameters to an input text file. AnyLogic reads them, runs the Simulation
experiment, and writes the results to an output text file. Python updates the parameters, and the process repeats (loop continues) until the GA stops.
What I’m trying to do now:
I created a second simulation experiment called Simulation_GA_Optimization
.
My goal is:
Simulation
→ runs continuously- acts as a production plantSimulation_GA_Optimization
→ started on demand (e.g., when production plan changes), runs its optimization loop (multiple simulation runs) at high speed in parallel, finds the best parameters, sends them back to Simulation
, then stops — all without pausing the main Simulation
.What I tried:
In the Main
, I created an Event
that triggers to start Simulation_GA_Optimization
by using the following code:
((Simulation_GA_Optimization) getExperiment()).button.action();
start();
But I got the following error message:
ServerException during discrete event execution:
class aluminum_die_casting_simulation.Simulation cannot be cast to class aluminum_die_casting_simulation.Simulation_GA_Optimization (aluminum_die_casting_simulation.Simulation and aluminum_die_casting_simulation.Simulation_GA_Optimization are in unnamed module of loader 'app')
java.lang.ClassCastException: class aluminum_die_casting_simulation.Simulation cannot be cast to class aluminum_die_casting_simulation.Simulation_GA_Optimization (aluminum_die_casting_simulation.Simulation and aluminum_die_casting_simulation.Simulation_GA_Optimization are in unnamed module of loader 'app')
at aluminum_die_casting_simulation.Main.executeActionOf(Main.java:4637)
at com.anylogic.engine.EventTimeout.execute(Unknown Source)
at com.anylogic.engine.Engine.j(Unknown Source)
at com.anylogic.engine.Engine.mf(Unknown Source)
at com.anylogic.engine.Engine$k.run(Unknown Source)
java.lang.IndexOutOfBoundsException: Trying to remove more items than the buffer contains
at com.anylogic.engine.gui.k.n(Unknown Source)
at com.anylogic.engine.gui.n.a(Unknown Source)
at com.anylogic.engine.gui.n.g(Unknown Source)
at com.anylogic.engine.gui.n.write(Unknown Source)
at java.base/java.io.PrintStream.write(PrintStream.java:568)
at java.base/sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:234)
at java.base/sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:313)
at java.base/sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:111)
at java.base/java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:178)
at java.base/java.io.PrintStream.writeln(PrintStream.java:723)
at java.base/java.io.PrintStream.println(PrintStream.java:1051)
at com.anylogic.engine.gui.b.println(Unknown Source)
at com.anylogic.engine.gui.ConsoleItemPrintStream.println(Unknown Source)
at com.anylogic.engine.Utilities.traceln(Unknown Source)
at com.anylogic.engine.Engine.a(Unknown Source)
at com.anylogic.engine.Engine.j(Unknown Source)
at com.anylogic.engine.Engine.mf(Unknown Source)
at com.anylogic.engine.Engine$k.run(Unknown Source)
Is it possible in AnyLogic PLE to run two Simulation experiments in parallel (one continuous, one doing multiple simulation loops)?
If not, what’s the best way to design this architecture so the continuous simulation and optimization loop can work together?
The getExperiment()
function returns the current experiment - in this case, it is not the Simulation_GA_Optimization
experiment, but the Simulation
experiment. Thus, since there is no equivalence between those two classes, they cannot be cast one to another.
As far as I know, there is no way to do it. I am unsure if PLE can do it. In order to do so, I'd suggest maybe exporting to Java or using Custom Experiments, but both are only available in pro versions. You could maybe also save a snapshot, then run the optimization experiment based on that snapshot, then load the snapshot and use the results - but yet, again, snapshots are only available on the paid version.
I don't know exactly what you need on the GA (or even how the GA experiment would interact time-wise with the main simulation which keeps running in parallel), but I'd suggest you try to step out of it. You are able to call external solvers with java interfaces if you need optimization during the simulation, but it just makes the simulation WAY slower depending on the frequency you need the optimization.