Skip to content

Commit

Permalink
Update Base to use new API
Browse files Browse the repository at this point in the history
  • Loading branch information
ZiluTian committed Sep 3, 2024
1 parent acf410a commit 28aafb5
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions Base/src/main/scala/Simulate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,43 @@ package simulation.base.API

import scala.collection.mutable.{Buffer, Map => MutMap}
import meta.runtime.{SimRuntime, Actor, Message}
import meta.API.{SimulationSnapshot, util}
import meta.API._
import scala.util.Random

object Simulate {
def apply(agents: Traversable[Actor], totalRound: Long): SimulationSnapshot = {
val r_seed = new Random(1000)

// Default: anonymous simulations that returns snapshot
def apply(agents: IndexedSeq[Actor], totalRound: Long)(implicit strategy: DeforestationStrategy): SimulationData = {
new Simulate(agents, totalRound, Map("name" -> f"Simulation_${r_seed.nextInt()}", "data" -> "snapshot"), None).run(strategy)
}

// SimulateUntil
def withUntilCondition(agents: IndexedSeq[Actor], totalRound: Long, cond: Iterable[Iterable[Serializable]] => Boolean)(implicit strategy: DeforestationStrategy): SimulationData = {
new Simulate(agents, totalRound, Map("name" -> f"Simulation_${r_seed.nextInt()}", "data" -> "timeseries"), Some(cond)).run(strategy)
}
}

class Simulate(agents: IndexedSeq[Actor], totalRound: Long, conf: Map[String, Any], cond: Option[Iterable[Iterable[Serializable]] => Boolean] = None) {
def run(strategy: DeforestationStrategy): SimulationData = {

require(conf.isDefinedAt("name")) // name of the actor system, to allow concurrent simulations
require(conf.isDefinedAt("data")) // timeseries or snapshot

val name: String = conf("name").asInstanceOf[String]
val dataConf: String = conf("data").asInstanceOf[String]

val builder: SimulationDataBuilder = if (dataConf == "timeseries") {
new TimeseriesBuilder(strategy)
} else {
new SnapshotBuilder()
}

var currentRound: Long = 0
var elapsedRound: Int = 0
var collectedMessages: MutMap[Long, Buffer[Message]] = MutMap[Long, Buffer[Message]]()
var collectedSerializedMessages: MutMap[Long, Buffer[Array[Byte]]] = MutMap[Long, Buffer[Array[Byte]]]()
var actors: Traversable[Actor] = agents
var actors: IndexedSeq[Actor] = agents

val initial: Long = System.currentTimeMillis()
var end: Long = initial
Expand All @@ -19,7 +47,7 @@ object Simulate {
val start: Long = end
// Add newly generated agents
if (!SimRuntime.newActors.isEmpty) {
actors = SimRuntime.newActors ++ actors
actors = actors ++ SimRuntime.newActors
SimRuntime.newActors.clear()
}

Expand Down Expand Up @@ -53,6 +81,9 @@ object Simulate {
} else {
println(f"Average ${end - initial} ms")
}
SimulationSnapshot(actors, collectedMessages.flatMap(i => i._2).toList)
builder.addAgents(actors)
builder.addMessages(collectedMessages.flatMap(i => i._2).toVector)
// SimulationSnapshot(actors, collectedMessages.flatMap(i => i._2).toList)
builder.build()
}
}

0 comments on commit 28aafb5

Please sign in to comment.