Commit 6b7eb10e authored by Juste Raimbault's avatar Juste Raimbault
Browse files

[Plugin] First version OneFactorSampling

parent 7c77acc3
......@@ -468,7 +468,7 @@ lazy val sensitivity = OsgiProject(pluginDir, "org.openmole.plugin.method.sensit
/* Sampling */
def allSampling = Seq(combineSampling, csvSampling, lhsSampling, quasirandomSampling)
def allSampling = Seq(combineSampling, csvSampling,oneFactorSampling, lhsSampling, quasirandomSampling)
lazy val combineSampling = OsgiProject(pluginDir, "org.openmole.plugin.sampling.combine", imports = Seq("*")) dependsOn(exception, modifierDomain, collectionDomain, workflow) settings (pluginSettings: _*)
......@@ -476,6 +476,8 @@ lazy val csvSampling = OsgiProject(pluginDir, "org.openmole.plugin.sampling.csv"
libraryDependencies += Libraries.scalatest
) settings (pluginSettings: _*)
lazy val oneFactorSampling = OsgiProject(pluginDir, "org.openmole.plugin.sampling.onefactor", imports = Seq("*")) dependsOn(exception, workflow, openmoleDSL) settings (pluginSettings: _*)
lazy val lhsSampling = OsgiProject(pluginDir, "org.openmole.plugin.sampling.lhs", imports = Seq("*")) dependsOn(exception, workflow, workspace, openmoleDSL) settings (pluginSettings: _*)
lazy val quasirandomSampling = OsgiProject(pluginDir, "org.openmole.plugin.sampling.quasirandom", imports = Seq("*")) dependsOn(exception, workflow, workspace) settings (
......
package org.openmole.plugin.sampling.onefactor
import org.openmole.core.pluginmanager._
import org.openmole.core.preference.ConfigurationInfo
import org.osgi.framework.BundleContext
class Activator extends PluginInfoActivator {
override def stop(context: BundleContext): Unit = {
PluginInfo.unregister(this)
ConfigurationInfo.unregister(this)
}
override def start(context: BundleContext): Unit = {
import org.openmole.core.pluginmanager.KeyWord._
val keyWords: Vector[KeyWord] =
Vector(
Sampling(classOf[OneFactorSampling])
)
PluginInfo.register(this, Vector(this.getClass.getPackage), keyWords = keyWords)
ConfigurationInfo.register(
this,
ConfigurationInfo.list()
)
}
}
\ No newline at end of file
package org.openmole.plugin.sampling.onefactor
import org.openmole.core.context.{ PrototypeSet, Val, Variable }
import org.openmole.core.expansion.FromContext
import org.openmole.core.workflow.domain.Finite
import org.openmole.core.workflow.sampling.{ ExplicitSampling, Factor, FactorSampling, Sampling }
case class NominalFactor[D, T](factor: Factor[D, T], nominalValue: T, values: Finite[D, T]) {
def inputs: PrototypeSet = Seq(factor.value)
def prototype: Val[T] = factor.value
def toValuesSampling: Sampling = FactorSampling[D, T](factor)(values)
def toNominalSampling: Sampling = ExplicitSampling[T](prototype, Seq(nominalValue))
}
object OneFactorSampling {
def apply(factors: NominalFactor[_, _]*) = new OneFactorSampling(factors: _*)
}
case class OneFactorSampling(factors: NominalFactor[_, _]*) extends Sampling {
override def inputs: PrototypeSet = PrototypeSet.empty ++ factors.toSeq.flatMap { _.inputs }
override def prototypes: Iterable[Val[_]] = factors.map { _.prototype }
override def apply(): FromContext[Iterator[Iterable[Variable[_]]]] = FromContext { p
import p._
if (factors.isEmpty) Iterator.empty
else
factors.toIterator.flatMap(oneFactorSampling(_).from(context))
}
/**
* Sampling along one of parameters
* (the FromContext is needed as NominalFactor function return Sampling, since they use core sampling primitive)
* @param n
* @return
*/
def oneFactorSampling(n: NominalFactor[_, _]): FromContext[Iterator[Iterable[Variable[_]]]] = FromContext {
p =>
import p._
complete(Seq(n.toValuesSampling().from(context)) ++ factors.filter(!_.equals(n)).map(_.toNominalSampling().from(context)))
}
/**
* complete sampling
* @param samplings
* @return
*/
def complete(samplings: Seq[Iterator[Iterable[Variable[_]]]]): Iterator[Iterable[Variable[_]]] = samplings.reduceLeft(combine)
/**
* combination for the complete sampling
* @param s1
* @param s2
* @return
*/
def combine(s1: Iterator[Iterable[Variable[_]]], s2: Iterator[Iterable[Variable[_]]]): Iterator[Iterable[Variable[_]]] = for (x s1; y s2) yield x ++ y
}
package org.openmole.plugin.sampling
package onefactor {
import org.openmole.core.workflow.domain.Finite
import org.openmole.core.workflow.sampling.Factor
trait OneFactorDSL {
implicit class SamplingIsNominalFactor[D, _](f: Factor[D, Any])(implicit domain: Finite[D,Any]) {
def nominal[T](t: T) = NominalFactor(f, t, domain)
}
}
}
package object onefactor extends OneFactorDSL
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment