holgerbrandl / kalasim

Discrete Event Simulator
https://www.kalasim.org/
MIT License
68 stars 10 forks source link

Allow branching of simulations #19

Open holgerbrandl opened 2 years ago

holgerbrandl commented 2 years ago

To enable different trajectories (with controlled randomization), the user may want to branch simulations at a specific time.

Example

// current API 
val sim  : Environment = createSimulation{ 
// define simulation entities here
}

// run it for 100 ticks
sim.run(100)

// inspect some metric (see https://www.kalasim.org/analysis/)
sim.gatherStatistic()

// Pseudocode/Desired API from here on 
val branches : List<Environment> = sim.branch(100)

val branchStats = branches.forEach { it.run(100) }.map{ it.gatherStatistic() }

To do so we must persist the entire graph including koin, monitors, the environment (including its queue) and process states (which are modelled as kotlin.sequences.Sequence iterators). Naturally - by design of kalasim - user-specific code will also leak into the graph to be persisted.

Current state :heavy_check_mark: Framework evaluation -> kryo it is, see evaluation branch https://github.com/holgerbrandl/kalasim/tree/kryoeval :heavy_check_mark: Persist koin :heavy_check_mark: Persist main entities and environment :heavy_check_mark: Deserialize environment including sequence iterators of process definitions (graph looks good and runs up to https://github.com/holgerbrandl/kalasim/blob/9d383e07d7dd7e59488a3f7a1c4a03105b6efc62/modules/persistence/src/main/kotlin/Kryo.kt#L77) :x: Run deserialized environment--> Fails to run, which boils to sequence iterators being not serializable as of now in https://github.com/holgerbrandl/kryo-kotlin-sam/blob/master/src/main/kotlin/simpleproc/SimpleProc.kt (see https://github.com/Kotlin/kotlinx.coroutines/issues/76 and https://github.com/EsotericSoftware/kryo/issues/867)

holgerbrandl commented 2 years ago

FYI ^^ @arnaudgiuliani and @theigl to give you some context why I had bothered you earlier this week. Thanks again for your great support.

holgerbrandl commented 2 years ago

Also see https://anylogic.help/anylogic/running/snapshots.html