From 27c0ea4b35bcd108b0977d4a9da716bd75170ee1 Mon Sep 17 00:00:00 2001 From: Pascal Date: Wed, 29 Jan 2020 01:19:11 -0600 Subject: [PATCH] [fix] fixed simulation to pass proper results --- .../scala/scim/components/Simulation.scala | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/scala/scim/components/Simulation.scala b/src/main/scala/scim/components/Simulation.scala index 56f1a5f..78bf136 100644 --- a/src/main/scala/scim/components/Simulation.scala +++ b/src/main/scala/scim/components/Simulation.scala @@ -1,7 +1,9 @@ package scim.components import scim.datastruct.{Behaviour, BehaviourVector, Cell, Configuration, FunctionArgs, GroundTruth, Group, Sensor, SensorCount, SensorResult, Time, Truth} +import scim.lib.ConcurrentBroadcast.BroadcastObject import scim.lib.simulation.SimulationBehaviour +import scim.lib.debugUtil.{DebugType, debug, debugf} class Simulation(config: Configuration) { private val sharedMemory = config.sharedMemory @@ -14,9 +16,10 @@ class Simulation(config: Configuration) { val duration = sharedConfig.duration val timeVector = Vector.tabulate(duration)(t => Time(t, duration)) def simTime(time: Time): Unit = { - val prevTruths = if(time.start) sharedMemory.groundTruth(time) else sharedMemory.groundTruth(time.prev) - val resultTruths = sharedMemory.groundTruth(time) - val sensorResults = sharedMemory.sensor(time) + // FIXME: really shouldn't have to .map(kv => kv._1 -> kv._2) to get an immutable map, in fact it shouldn't even be mutable in the first place... + val prevTruths: scala.collection.immutable.Map[Cell, Map[Group, Truth]] = if(time.start) sharedMemory.groundTruth(time).map(kv => kv._1 -> kv._2) else sharedMemory.groundTruth(time.prev).map(kv => kv._1 -> kv._2) + val resultTruths = sharedMemory.groundTruth(time).map(kv => kv._1 -> kv._2) + val sensorResults = sharedMemory.sensor(time).map(kv => kv._1 -> kv._2) simCells(time, prevTruths, resultTruths, sensorResults) } timeVector.foreach(simTime) @@ -54,13 +57,22 @@ class Simulation(config: Configuration) { // this would require sharedConfig.cellThreadPool * 2 additional threads // : // another option would be to calculate all ground truths within a list first and than parellize the sensor list + def simGroup(input: (Group, List[(Group, Sensor, Option[GroundTruth])])): Unit = { val group = input._1 val truthGroup = group.getParentOrGroup // make sure the correct ground truth gets passed val groupList = input._2 // overwrite truth with value from configuration if set for this time and group val prevTruth = if(!configTruths.isEmpty && configTruths.get.contains(truthGroup)) configTruths.get(truthGroup) else PrevTruths(truthGroup) - groupList.foreach(element => simulate(time, cell, prevTruth, resultTruths(group), resultSensors(group), element)) + groupList.foreach(element => { + val subGroup = element._1 + debugf(DebugType.DEBUG, "simGroups: Time: %s | Cell: %s | Group: %s | Groupelement: %s", time.toString, cell.toString, group.toString, element._1.name) + // pass either subGroup truth, if truth is available for subGroup, or the sensor parents truth + val resTruth = if(subGroup.sensorParent.isEmpty) resultTruths(subGroup) else resultTruths(sharedMemory.groups(subGroup.parent.getOrElse(subGroup.name))) + // construct dummy result for sensors with count 0 + val restSens = if(resultSensors.contains(subGroup)) resultSensors(subGroup) else Vector(SensorResult(new BroadcastObject())) + simulate(time, cell, prevTruth, resTruth, restSens, element) + }) } if(sharedConfig.parGroup != 0 && cells.size >= sharedConfig.parGroup ) { val groupList = groups.par @@ -84,6 +96,7 @@ class Simulation(config: Configuration) { val v: Vector[Option[(SimulationBehaviour, Option[FunctionArgs])]] = Vector.fill(time.duration)(None) GroundTruth(group, Some(Behaviour(BehaviourVector(v)))).simulate(cell, time, prevTruth, resultTruth) } + debugf(DebugType.DEBUG, "simulate: Time: %s | Cell: %s | Group: %s | Truth: %s", time.toString, cell.toString, group.toString, resultTruth.value.peak()) // simulate the sensor sensor.simulate(time, resultTruth, resultSensors) }