[fix] fixed simulation to pass proper results

This commit is contained in:
2020-01-29 01:19:11 -06:00
parent e84a253648
commit 27c0ea4b35

View File

@@ -1,7 +1,9 @@
package scim.components package scim.components
import scim.datastruct.{Behaviour, BehaviourVector, Cell, Configuration, FunctionArgs, GroundTruth, Group, Sensor, SensorCount, SensorResult, Time, Truth} 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.simulation.SimulationBehaviour
import scim.lib.debugUtil.{DebugType, debug, debugf}
class Simulation(config: Configuration) { class Simulation(config: Configuration) {
private val sharedMemory = config.sharedMemory private val sharedMemory = config.sharedMemory
@@ -14,9 +16,10 @@ class Simulation(config: Configuration) {
val duration = sharedConfig.duration val duration = sharedConfig.duration
val timeVector = Vector.tabulate(duration)(t => Time(t, duration)) val timeVector = Vector.tabulate(duration)(t => Time(t, duration))
def simTime(time: Time): Unit = { def simTime(time: Time): Unit = {
val prevTruths = if(time.start) sharedMemory.groundTruth(time) else sharedMemory.groundTruth(time.prev) // 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 resultTruths = sharedMemory.groundTruth(time) 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 sensorResults = sharedMemory.sensor(time) 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) simCells(time, prevTruths, resultTruths, sensorResults)
} }
timeVector.foreach(simTime) timeVector.foreach(simTime)
@@ -54,13 +57,22 @@ class Simulation(config: Configuration) {
// this would require sharedConfig.cellThreadPool * 2 additional threads // 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 // 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 = { def simGroup(input: (Group, List[(Group, Sensor, Option[GroundTruth])])): Unit = {
val group = input._1 val group = input._1
val truthGroup = group.getParentOrGroup // make sure the correct ground truth gets passed val truthGroup = group.getParentOrGroup // make sure the correct ground truth gets passed
val groupList = input._2 val groupList = input._2
// overwrite truth with value from configuration if set for this time and group // 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) 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 ) { if(sharedConfig.parGroup != 0 && cells.size >= sharedConfig.parGroup ) {
val groupList = groups.par val groupList = groups.par
@@ -84,6 +96,7 @@ class Simulation(config: Configuration) {
val v: Vector[Option[(SimulationBehaviour, Option[FunctionArgs])]] = Vector.fill(time.duration)(None) val v: Vector[Option[(SimulationBehaviour, Option[FunctionArgs])]] = Vector.fill(time.duration)(None)
GroundTruth(group, Some(Behaviour(BehaviourVector(v)))).simulate(cell, time, prevTruth, resultTruth) 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 // simulate the sensor
sensor.simulate(time, resultTruth, resultSensors) sensor.simulate(time, resultTruth, resultSensors)
} }