Commit afdaf062 authored by Romain Reuillon's avatar Romain Reuillon
Browse files

[Plugin] enh: mege exact an noisy objectives

parent 4c729009
......@@ -164,6 +164,7 @@ object Val {
def array(level: Int) = toArray(level)
def array = toArray
def unsecureFromArray = fromArray(prototype.asInstanceOf[Val[Array[T]]])
def unsecureType = prototype.`type`.asInstanceOf[Manifest[Any]]
}
......
......@@ -41,15 +41,11 @@ object EvolutionMetadata {
sealed trait GenomeBoundData
case class ExactObjective(
case class Objective(
name: String,
delta: Option[Double],
negative: Boolean)
case class NoisyObjective(
name: String,
delta: Option[Double],
negative: Boolean)
negative: Boolean,
noisy: Boolean)
def method = "evolution"
case object none extends EvolutionMetadata
......@@ -61,14 +57,14 @@ object EvolutionMetadata {
case class PSE(
genome: Seq[GenomeBoundData],
objective: Seq[ExactObjective],
objective: Seq[Objective],
grid: PSE.Grid,
generation: Long,
saveOption: SaveOption) extends EvolutionMetadata
case class StochasticPSE(
genome: Seq[GenomeBoundData],
objective: Seq[NoisyObjective],
objective: Seq[Objective],
grid: PSE.Grid,
sample: Int,
generation: Long,
......@@ -76,14 +72,14 @@ object EvolutionMetadata {
case class NSGA2(
genome: Seq[GenomeBoundData],
objective: Seq[ExactObjective],
objective: Seq[Objective],
populationSize: Int,
generation: Long,
saveOption: SaveOption) extends EvolutionMetadata
case class StochasticNSGA2(
genome: Seq[GenomeBoundData],
objective: Seq[NoisyObjective],
objective: Seq[Objective],
populationSize: Int,
sample: Int,
generation: Long,
......
......@@ -410,7 +410,7 @@ object GAIntegration {
genome.zipWithIndex.map { case (g, i) Genome.toArrayVariable(g, variables.map(_(i).value)) }.toVector
}
def objectivesOfPopulationToVariables[I](objectives: Seq[Objective], phenotypeValues: Vector[Vector[Double]]): Vector[Variable[_]] =
def objectivesOfPopulationToVariables[I](objectives: Seq[Objective[_]], phenotypeValues: Vector[Vector[Double]]): Vector[Variable[_]] =
Objectives.resultPrototypes(objectives).toVector.zipWithIndex.map {
case (objective, i)
Variable(
......
......@@ -32,11 +32,8 @@ object MetadataGeneration {
}
}
def exactObjectiveData(o: ExactObjective[_]) =
EvolutionMetadata.ExactObjective(o.as.getOrElse(o.prototype.name), o.delta, o.negative)
def noisyObjectiveData(o: NoisyObjective[_]) =
EvolutionMetadata.NoisyObjective(o.as.getOrElse(o.prototype.name), o.delta, o.negative)
def objectiveData(o: Objective[_]) =
EvolutionMetadata.Objective(o.as.getOrElse(o.prototype.name), o.delta, o.negative, o.noisy)
def fromString(s: String): EvolutionMetadata =
decode[EvolutionMetadata](s) match {
......
......@@ -57,14 +57,15 @@ object NSGA2 {
override def metadata(generation: Long, saveOption: SaveOption) =
EvolutionMetadata.NSGA2(
genome = MetadataGeneration.genomeData(om.genome),
objective = om.objectives.map(MetadataGeneration.exactObjectiveData),
objective = om.objectives.map(MetadataGeneration.objectiveData),
populationSize = om.mu,
generation = generation,
saveOption = saveOption
)
def result(population: Vector[I], state: S, keepAll: Boolean, includeOutputs: Boolean) = FromContext.value {
val res = MGONSGA2.result[Phenotype](population, Genome.continuous(om.genome), ExactObjective.toFitnessFunction(om.phenotypeContent, om.objectives), keepAll = keepAll)
def result(population: Vector[I], state: S, keepAll: Boolean, includeOutputs: Boolean) = FromContext { p
import p._
val res = MGONSGA2.result[Phenotype](population, Genome.continuous(om.genome), Objective.toFitnessFunction(om.phenotypeContent, om.objectives).from(context), keepAll = keepAll)
val genomes = GAIntegration.genomesOfPopulationToVariables(om.genome, res.map(_.continuous) zip res.map(_.discrete), scale = false)
val fitness = GAIntegration.objectivesOfPopulationToVariables(om.objectives, res.map(_.fitness))
......@@ -85,11 +86,12 @@ object NSGA2 {
import p._
val discrete = Genome.discrete(om.genome)
val rejectValue = om.reject.map(f GAIntegration.rejectValue[G](f, om.genome, _.continuousValues.toVector, _.discreteValues.toVector).from(context))
MGONSGA2.adaptiveBreeding[S, Phenotype](n, om.operatorExploration, discrete, ExactObjective.toFitnessFunction(om.phenotypeContent, om.objectives), rejectValue)(s, individuals, rng)
MGONSGA2.adaptiveBreeding[S, Phenotype](n, om.operatorExploration, discrete, Objective.toFitnessFunction(om.phenotypeContent, om.objectives).from(context), rejectValue)(s, individuals, rng)
}
def elitism(population: Vector[I], candidates: Vector[I], s: S, evaluated: Long, rng: scala.util.Random) = FromContext { p
val (s2, elited) = MGONSGA2.elitism[S, Phenotype](om.mu, Genome.continuous(om.genome), ExactObjective.toFitnessFunction(om.phenotypeContent, om.objectives))(s, population, candidates, rng)
import p._
val (s2, elited) = MGONSGA2.elitism[S, Phenotype](om.mu, Genome.continuous(om.genome), Objective.toFitnessFunction(om.phenotypeContent, om.objectives).from(context))(s, population, candidates, rng)
val s3 = Focus[S](_.generation).modify(_ + 1)(s2)
val s4 = Focus[S](_.evaluated).modify(_ + evaluated)(s3)
(s4, elited)
......@@ -111,7 +113,7 @@ object NSGA2 {
mu: Int,
genome: Genome,
phenotypeContent: PhenotypeContent,
objectives: Seq[ExactObjective[_]],
objectives: Seq[Objective[_]],
operatorExploration: Double,
reject: Option[Condition])
......@@ -132,7 +134,7 @@ object NSGA2 {
override def metadata(generation: Long, saveOption: SaveOption) =
EvolutionMetadata.StochasticNSGA2(
genome = MetadataGeneration.genomeData(om.genome),
objective = om.objectives.map(MetadataGeneration.noisyObjectiveData),
objective = om.objectives.map(MetadataGeneration.objectiveData),
sample = om.historySize,
populationSize = om.mu,
generation = generation,
......@@ -150,7 +152,7 @@ object NSGA2 {
def buildIndividual(genome: G, phenotype: Phenotype, context: Context) = CDGenome.NoisyIndividual.buildIndividual(genome, phenotype)
def initialState = EvolutionState[Unit](s = ())
def aggregate = NoisyObjective.aggregate(om.phenotypeContent, om.objectives)
def aggregate = Objective.aggregate(om.phenotypeContent, om.objectives)
def result(population: Vector[I], state: S, keepAll: Boolean, includeOutputs: Boolean) = FromContext { p
import p._
......@@ -204,7 +206,7 @@ object NSGA2 {
operatorExploration: Double,
genome: Genome,
phenotypeContent: PhenotypeContent,
objectives: Seq[NoisyObjective[_]],
objectives: Seq[Objective[_]],
historySize: Int,
cloneProbability: Double,
reject: Option[Condition]
......
......@@ -52,8 +52,9 @@ object NSGA3 {
def initialState = EvolutionState[Unit](s = ())
def result(population: Vector[I], state: S, keepAll: Boolean, includeOutputs: Boolean) = FromContext.value {
val res = MGONSGA3.result[Phenotype](population, Genome.continuous(om.genome), ExactObjective.toFitnessFunction(om.phenotypeContent, om.objectives), keepAll = keepAll)
def result(population: Vector[I], state: S, keepAll: Boolean, includeOutputs: Boolean) = FromContext { p
import p._
val res = MGONSGA3.result[Phenotype](population, Genome.continuous(om.genome), Objective.toFitnessFunction(om.phenotypeContent, om.objectives).from(context), keepAll = keepAll)
val genomes = GAIntegration.genomesOfPopulationToVariables(om.genome, res.map(_.continuous) zip res.map(_.discrete), scale = false)
val fitness = GAIntegration.objectivesOfPopulationToVariables(om.objectives, res.map(_.fitness))
......@@ -74,12 +75,12 @@ object NSGA3 {
import p._
val discrete = Genome.discrete(om.genome)
val rejectValue = om.reject.map(f GAIntegration.rejectValue[G](f, om.genome, _.continuousValues.toVector, _.discreteValues.toVector).from(context))
MGONSGA3.adaptiveBreeding[S, Phenotype](om.operatorExploration, discrete, ExactObjective.toFitnessFunction(om.phenotypeContent, om.objectives), rejectValue, lambda = n)(s, individuals, rng)
MGONSGA3.adaptiveBreeding[S, Phenotype](om.operatorExploration, discrete, Objective.toFitnessFunction(om.phenotypeContent, om.objectives).from(context), rejectValue, lambda = n)(s, individuals, rng)
}
def elitism(population: Vector[I], candidates: Vector[I], s: S, evaluated: Long, rng: scala.util.Random) = FromContext { p
import p._
val (s2, elited) = MGONSGA3.elitism[S, Phenotype](om.mu, om.references, Genome.continuous(om.genome), ExactObjective.toFitnessFunction(om.phenotypeContent, om.objectives))(s, population, candidates, rng)
val (s2, elited) = MGONSGA3.elitism[S, Phenotype](om.mu, om.references, Genome.continuous(om.genome), Objective.toFitnessFunction(om.phenotypeContent, om.objectives).from(context))(s, population, candidates, rng)
val s3 = Focus[S](_.generation).modify(_ + 1)(s2)
val s4 = Focus[S](_.evaluated).modify(_ + evaluated)(s3)
(s4, elited)
......@@ -102,7 +103,7 @@ object NSGA3 {
references: ReferencePoints,
genome: Genome,
phenotypeContent: PhenotypeContent,
objectives: Seq[ExactObjective[_]],
objectives: Seq[Objective[_]],
operatorExploration: Double,
reject: Option[Condition])
......@@ -130,7 +131,7 @@ object NSGA3 {
def buildIndividual(genome: G, phenotype: Phenotype, context: Context) = CDGenome.NoisyIndividual.buildIndividual(genome, phenotype)
def initialState = EvolutionState[Unit](s = ())
def aggregate = NoisyObjective.aggregate(om.phenotypeContent, om.objectives)
def aggregate = Objective.aggregate(om.phenotypeContent, om.objectives)
def result(population: Vector[I], state: S, keepAll: Boolean, includeOutputs: Boolean) = FromContext { p
import p._
......@@ -189,7 +190,7 @@ object NSGA3 {
operatorExploration: Double,
genome: Genome,
phenotypeContent: PhenotypeContent,
objectives: Seq[NoisyObjective[_]],
objectives: Seq[Objective[_]],
historySize: Int,
cloneProbability: Double,
reject: Option[Condition]
......@@ -246,8 +247,8 @@ object NSGA3Evolution {
p {
val refPoints =
p.references match {
case NSGA3.References.None mgo.evolution.algorithm.NSGA3Operations.ReferencePoints(50, Objectives.value(p.objective).size)
case NSGA3.References.Division(i) mgo.evolution.algorithm.NSGA3Operations.ReferencePoints(i, Objectives.value(p.objective).size)
case NSGA3.References.None mgo.evolution.algorithm.NSGA3Operations.ReferencePoints(50, p.objective.size)
case NSGA3.References.Division(i) mgo.evolution.algorithm.NSGA3Operations.ReferencePoints(i, p.objective.size)
case NSGA3.References.List(p) mgo.evolution.algorithm.NSGA3Operations.ReferencePoints(p)
}
......
......@@ -273,7 +273,7 @@ object NichedNSGA2 {
def result(population: Vector[I], state: S, keepAll: Boolean, includeOutputs: Boolean) = FromContext { p
import p._
val res = NichedNSGA2Algorithm.result(population, om.niche.from(context), Genome.continuous(om.genome), ExactObjective.toFitnessFunction(om.phenotypeContent, om.objectives), keepAll = keepAll)
val res = NichedNSGA2Algorithm.result(population, om.niche.from(context), Genome.continuous(om.genome), Objective.toFitnessFunction(om.phenotypeContent, om.objectives).from(context), keepAll = keepAll)
val genomes = GAIntegration.genomesOfPopulationToVariables(om.genome, res.map(_.continuous) zip res.map(_.discrete), scale = false)
val fitness = GAIntegration.objectivesOfPopulationToVariables(om.objectives, res.map(_.fitness))
......@@ -294,12 +294,12 @@ object NichedNSGA2 {
import p._
val discrete = Genome.discrete(om.genome)
val rejectValue = om.reject.map(f GAIntegration.rejectValue[G](f, om.genome, _.continuousValues.toVector, _.discreteValues.toVector).from(context))
mgo.evolution.algorithm.Profile.adaptiveBreeding[Phenotype](n, om.operatorExploration, discrete, ExactObjective.toFitnessFunction(om.phenotypeContent, om.objectives), rejectValue) apply (s, population, rng)
mgo.evolution.algorithm.Profile.adaptiveBreeding[Phenotype](n, om.operatorExploration, discrete, Objective.toFitnessFunction(om.phenotypeContent, om.objectives).from(context), rejectValue) apply (s, population, rng)
}
def elitism(population: Vector[I], candidates: Vector[I], s: S, evaluated: Long, rng: scala.util.Random) = FromContext { p
import p._
val (s2, elited) = NichedNSGA2Algorithm.elitism[S, Vector[Int], Phenotype](om.niche.from(context), om.nicheSize, Genome.continuous(om.genome), ExactObjective.toFitnessFunction(om.phenotypeContent, om.objectives)) apply (s, population, candidates, rng)
val (s2, elited) = NichedNSGA2Algorithm.elitism[S, Vector[Int], Phenotype](om.niche.from(context), om.nicheSize, Genome.continuous(om.genome), Objective.toFitnessFunction(om.phenotypeContent, om.objectives).from(context)) apply (s, population, candidates, rng)
val s3 = Focus[S](_.generation).modify(_ + 1)(s2)
val s4 = Focus[S](_.evaluated).modify(_ + evaluated)(s3)
(s4, elited)
......@@ -321,7 +321,7 @@ object NichedNSGA2 {
niche: FromContext[Niche[CDGenome.DeterministicIndividual.Individual[Phenotype], Vector[Int]]],
genome: Genome,
phenotypeContent: PhenotypeContent,
objectives: Seq[ExactObjective[_]],
objectives: Seq[Objective[_]],
operatorExploration: Double,
reject: Option[Condition])
......@@ -368,7 +368,7 @@ object NichedNSGA2 {
def result(population: Vector[I], state: S, keepAll: Boolean, includeOutputs: Boolean) = FromContext { p
import p._
val res = NoisyNichedNSGA2Algorithm.result(population, NoisyObjective.aggregate(om.phenotypeContent, om.objectives).from(context), om.niche.from(context), Genome.continuous(om.genome), onlyOldest = true, keepAll = keepAll)
val res = NoisyNichedNSGA2Algorithm.result(population, Objective.aggregate(om.phenotypeContent, om.objectives).from(context), om.niche.from(context), Genome.continuous(om.genome), onlyOldest = true, keepAll = keepAll)
val genomes = GAIntegration.genomesOfPopulationToVariables(om.genome, res.map(_.continuous) zip res.map(_.discrete), scale = false)
val fitness = GAIntegration.objectivesOfPopulationToVariables(om.objectives, res.map(_.fitness))
val samples = Variable(GAIntegration.samplesVal.array, res.map(_.replications).toArray)
......@@ -390,7 +390,7 @@ object NichedNSGA2 {
import p._
val discrete = Genome.discrete(om.genome)
val rejectValue = om.reject.map(f GAIntegration.rejectValue[G](f, om.genome, _.continuousValues.toVector, _.discreteValues.toVector).from(context))
NoisyNichedNSGA2Algorithm.adaptiveBreeding[S, Phenotype](n, rejectValue, om.operatorExploration, om.cloneProbability, NoisyObjective.aggregate(om.phenotypeContent, om.objectives).from(context), discrete) apply (s, individuals, rng)
NoisyNichedNSGA2Algorithm.adaptiveBreeding[S, Phenotype](n, rejectValue, om.operatorExploration, om.cloneProbability, Objective.aggregate(om.phenotypeContent, om.objectives).from(context), discrete) apply (s, individuals, rng)
}
def elitism(population: Vector[I], candidates: Vector[I], s: S, evaluated: Long, rng: scala.util.Random) =
......@@ -400,7 +400,7 @@ object NichedNSGA2 {
om.niche.from(context),
om.nicheSize,
om.historySize,
NoisyObjective.aggregate(om.phenotypeContent, om.objectives).from(context),
Objective.aggregate(om.phenotypeContent, om.objectives).from(context),
Genome.continuous(om.genome)) apply (s, population, candidates, rng)
val s3 = Focus[S](_.generation).modify(_ + 1)(s2)
......@@ -425,7 +425,7 @@ object NichedNSGA2 {
operatorExploration: Double,
genome: Genome,
phenotypeContent: PhenotypeContent,
objectives: Seq[NoisyObjective[_]],
objectives: Seq[Objective[_]],
historySize: Int,
cloneProbability: Double,
reject: Option[Condition])
......
......@@ -7,7 +7,7 @@ import monocle.macros.GenLens
import org.openmole.core.context.{ Context, Variable }
import org.openmole.core.workflow.builder.{ DefinitionScope, ValueAssignment }
import org.openmole.plugin.method.evolution.Genome.{ GenomeBound, Suggestion }
import org.openmole.plugin.method.evolution.Objective.{ ToExactObjective, ToNoisyObjective }
import org.openmole.plugin.method.evolution.Objective.{ ToObjective }
import org.openmole.tool.types.ToDouble
import squants.time.Time
......@@ -25,7 +25,7 @@ object OSE {
limit: Vector[Double],
genome: Genome,
phenotypeContent: PhenotypeContent,
objectives: Seq[ExactObjective[_]],
objectives: Seq[Objective[_]],
operatorExploration: Double,
reject: Option[Condition])
......@@ -61,8 +61,9 @@ object OSE {
def afterGeneration(g: Long, s: S, population: Vector[I]): Boolean = mgo.evolution.stop.afterGeneration[S, I](g, Focus[S](_.generation))(s, population)
def afterDuration(d: Time, s: S, population: Vector[I]): Boolean = mgo.evolution.stop.afterDuration[S, I](d, Focus[S](_.startTime))(s, population)
def result(population: Vector[I], state: S, keepAll: Boolean, includeOutputs: Boolean) = FromContext.value {
val res = MGOOSE.result[Phenotype](state, population, Genome.continuous(om.genome), ExactObjective.toFitnessFunction(om.phenotypeContent, om.objectives), keepAll = keepAll)
def result(population: Vector[I], state: S, keepAll: Boolean, includeOutputs: Boolean) = FromContext { p
import p._
val res = MGOOSE.result[Phenotype](state, population, Genome.continuous(om.genome), Objective.toFitnessFunction(om.phenotypeContent, om.objectives).from(context), keepAll = keepAll)
val genomes = GAIntegration.genomesOfPopulationToVariables(om.genome, res.map(_.continuous) zip res.map(_.discrete), scale = false)
val fitness = GAIntegration.objectivesOfPopulationToVariables(om.objectives, res.map(_.fitness))
......@@ -88,12 +89,13 @@ object OSE {
om.operatorExploration,
discrete,
om.origin,
ExactObjective.toFitnessFunction(om.phenotypeContent, om.objectives),
Objective.toFitnessFunction(om.phenotypeContent, om.objectives).from(context),
rejectValue) apply (s, individuals, rng)
}
def elitism(population: Vector[I], candidates: Vector[I], s: S, evaluated: Long, rng: scala.util.Random) = {
val (s2, elited) = MGOOSE.elitism[Phenotype](om.mu, om.limit, om.origin, Genome.continuous(om.genome), ExactObjective.toFitnessFunction(om.phenotypeContent, om.objectives)) apply (s, population, candidates, rng)
def elitism(population: Vector[I], candidates: Vector[I], s: S, evaluated: Long, rng: scala.util.Random) = FromContext { p
import p._
val (s2, elited) = MGOOSE.elitism[Phenotype](om.mu, om.limit, om.origin, Genome.continuous(om.genome), Objective.toFitnessFunction(om.phenotypeContent, om.objectives).from(context)) apply (s, population, candidates, rng)
val s3 = Focus[S](_.generation).modify(_ + 1)(s2)
val s4 = Focus[S](_.evaluated).modify(_ + evaluated)(s3)
(s4, elited)
......@@ -112,7 +114,7 @@ object OSE {
limit: Vector[Double],
genome: Genome,
phenotypeContent: PhenotypeContent,
objectives: Seq[NoisyObjective[_]],
objectives: Seq[Objective[_]],
historySize: Int,
cloneProbability: Double,
operatorExploration: Double,
......@@ -151,7 +153,7 @@ object OSE {
def result(population: Vector[I], state: S, keepAll: Boolean, includeOutputs: Boolean) = FromContext { p
import p._
val res = MGONoisyOSE.result(state, population, NoisyObjective.aggregate(om.phenotypeContent, om.objectives).from(context), Genome.continuous(om.genome), om.limit, keepAll = keepAll)
val res = MGONoisyOSE.result(state, population, Objective.aggregate(om.phenotypeContent, om.objectives).from(context), Genome.continuous(om.genome), om.limit, keepAll = keepAll)
val genomes = GAIntegration.genomesOfPopulationToVariables(om.genome, res.map(_.continuous) zip res.map(_.discrete), scale = false)
val fitness = GAIntegration.objectivesOfPopulationToVariables(om.objectives, res.map(_.fitness))
val samples = Variable(GAIntegration.samplesVal.array, res.map(_.replications).toArray)
......@@ -179,7 +181,7 @@ object OSE {
n,
om.operatorExploration,
om.cloneProbability,
NoisyObjective.aggregate(om.phenotypeContent, om.objectives).from(context),
Objective.aggregate(om.phenotypeContent, om.objectives).from(context),
discrete,
om.origin,
om.limit,
......@@ -193,7 +195,7 @@ object OSE {
MGONoisyOSE.elitism[Phenotype](
om.mu,
om.historySize,
NoisyObjective.aggregate(om.phenotypeContent, om.objectives).from(context),
Objective.aggregate(om.phenotypeContent, om.objectives).from(context),
Genome.continuous(om.genome),
om.origin,
om.limit) apply (s, population, candidates, rng)
......@@ -270,8 +272,8 @@ object OSE {
case class DiscreteSequenceOriginAxe(p: Genome.GenomeBound.SequenceOfInt, scale: Vector[Vector[Int]]) extends OriginAxe
object FitnessPattern {
implicit def fromUnderExactToPattern[T, V](v: Under[T, V])(implicit td: ToDouble[V], te: ToExactObjective[T]) = FitnessPattern(te.apply(v.value), td(v.under))
implicit def fromUnderNoisyToPattern[T, V](v: Under[T, V])(implicit td: ToDouble[V], te: ToNoisyObjective[T]) = FitnessPattern(te.apply(v.value), td(v.under))
implicit def fromUnderExactToPattern[T, V](v: Under[T, V])(implicit td: ToDouble[V], te: ToObjective[T]) = FitnessPattern(te.apply(v.value), td(v.under))
// implicit def fromUnderNoisyToPattern[T, V](v: Under[T, V])(implicit td: ToDouble[V], te: ToNoisyObjective[T]) = FitnessPattern(te.apply(v.value), td(v.under))
// implicit def fromUnderToObjective[T](v: Under[Val[T], T])(implicit td: ToDouble[T]) = FitnessPattern(v.value, td(v.under))
// implicit def fromNegativeUnderToObjective[T](v: Under[Negative[Val[T]], T])(implicit td: ToDouble[T]) = FitnessPattern(v.value, td(v.under))
......@@ -283,7 +285,7 @@ object OSE {
def toObjectives(f: Seq[FitnessPattern]) = f.map(_.objective)
}
case class FitnessPattern(objective: Objective, limit: Double)
case class FitnessPattern(objective: Objective[_], limit: Double)
def apply(
origin: Seq[OriginAxe],
......
......@@ -11,20 +11,14 @@ import scala.reflect.ClassTag
object Objective {
object ToExactObjective {
implicit def valIsToExact[T](implicit td: ToDouble[T]): ToExactObjective[Val[T]] = (v: Val[T]) ExactObjective[T](v, td.apply, negative = false, delta = None, as = None)
implicit def negativeToExact[T](implicit exact: ToExactObjective[T]): ToExactObjective[Negative[T]] = (t: Negative[T]) exact.apply(t.value).copy(negative = true)
implicit def deltaIsToExact[T, V](implicit exact: ToExactObjective[T], td: ToDouble[V]): ToExactObjective[Delta[T, V]] = (t: Delta[T, V]) exact.apply(t.value).copy(delta = Some(td.apply(t.delta)))
implicit def asIsToExact[T](implicit exact: ToExactObjective[T]): ToExactObjective[As[T, String]] = (t: As[T, String]) exact.apply(t.value).copy(as = Some(t.as))
implicit def asValIsToExact[T, P](implicit exact: ToExactObjective[T]): ToExactObjective[As[T, Val[P]]] = (t: As[T, Val[P]]) exact.apply(t.value).copy(as = Some(t.as.name))
}
trait ToExactObjective[T] {
def apply(t: T): ExactObjective[_]
}
object ToObjective {
implicit def valIsToExact[T](implicit td: ToDouble[T]): ToObjective[Val[T]] = v Objective[T](v, td.apply _, negative = false, delta = None, as = None)
implicit def negativeToExact[T](implicit exact: ToObjective[T]): ToObjective[Negative[T]] = t exact.apply(t.value).copy(negative = true)
implicit def deltaIsToExact[T, V](implicit exact: ToObjective[T], td: ToDouble[V]): ToObjective[Delta[T, V]] = t exact.apply(t.value).copy(delta = Some(td.apply(t.delta)))
implicit def asIsToExact[T](implicit exact: ToObjective[T]): ToObjective[As[T, String]] = t exact.apply(t.value).copy(as = Some(t.as))
implicit def asValIsToExact[T, P](implicit exact: ToObjective[T]): ToObjective[As[T, Val[P]]] = t exact.apply(t.value).copy(as = Some(t.as.name))
object ToNoisyObjective {
implicit def aggregateStringIsNoisy[T: ClassTag]: ToNoisyObjective[Aggregate[Val[T], String]] =
implicit def aggregateStringIsNoisy[T: ClassTag]: ToObjective[Aggregate[Val[T], String]] =
(t: Aggregate[Val[T], String]) {
val fromContext: FromContext[Double] = t.aggregate
......@@ -33,122 +27,72 @@ object Objective {
(v: Array[T]) fromContext.from(Context(t.value.toArray -> v))
}
NoisyObjective(t.value, aggregate, negative = false, delta = None, as = None, fromContext.validate)
Objective(t.value.array, aggregate, negative = false, delta = None, as = None, validate = fromContext.validate, noisy = true)
}
implicit def aggregateArrayIsToNoisy[T: ClassTag]: ToNoisyObjective[Aggregate[Val[T], Array[T] Double]] = (a: Aggregate[Val[T], Array[T] Double]) NoisyObjective(a.value, a.aggregate, negative = false, delta = None, as = None)
implicit def aggregateSeqIsToNoisy[T: ClassTag]: ToNoisyObjective[Aggregate[Val[T], Seq[T] Double]] = (a: Aggregate[Val[T], Seq[T] Double]) NoisyObjective(a.value, (v: Array[T]) a.aggregate(v.toVector), negative = false, delta = None, as = None)
implicit def aggregateVectorIsToNoisy[T: ClassTag]: ToNoisyObjective[Aggregate[Val[T], Vector[T] Double]] = (a: Aggregate[Val[T], Vector[T] Double]) NoisyObjective(a.value, (v: Array[T]) a.aggregate(v.toVector), negative = false, delta = None, as = None)
implicit def negativeIsToNoisy[T](implicit noisy: ToNoisyObjective[T]): ToNoisyObjective[Negative[T]] = (t: Negative[T]) noisy.apply(t.value).copy(negative = true)
implicit def deltaIsToNoisy[T, V](implicit noisy: ToNoisyObjective[T], td: ToDouble[V]): ToNoisyObjective[Delta[T, V]] = (t: Delta[T, V]) noisy.apply(t.value).copy(delta = Some(td.apply(t.delta)))
implicit def asStringIsToNoisy[T](implicit noisy: ToNoisyObjective[T]): ToNoisyObjective[As[T, String]] = (t: As[T, String]) noisy.apply(t.value).copy(as = Some(t.as))
implicit def asValIsToNoisy[T, P](implicit noisy: ToNoisyObjective[T]): ToNoisyObjective[As[T, Val[P]]] = (t: As[T, Val[P]]) noisy.apply(t.value).copy(as = Some(t.as.name))
}
implicit def aggregateArrayIsToNoisy[T: ClassTag]: ToObjective[Aggregate[Val[T], Array[T] Double]] = a Objective(a.value.array, a.aggregate, negative = false, delta = None, as = None, noisy = true)
implicit def aggregateSeqIsToNoisy[T: ClassTag]: ToObjective[Aggregate[Val[T], Seq[T] Double]] = a Objective(a.value.array, (v: Array[T]) a.aggregate(v.toVector), negative = false, delta = None, as = None, noisy = true)
implicit def aggregateVectorIsToNoisy[T: ClassTag]: ToObjective[Aggregate[Val[T], Vector[T] Double]] = a Objective(a.value.array, (v: Array[T]) a.aggregate(v.toVector), negative = false, delta = None, as = None, noisy = true)
}
trait ToNoisyObjective[T] {
def apply(t: T): NoisyObjective[_]
trait ToObjective[T] {
def apply(t: T): Objective[_]
}
object ToObjective {
implicit def toNoisyObjective[T: ToNoisyObjective]: ToObjective[T] = (t: T) implicitly[ToNoisyObjective[T]].apply(t)
implicit def toExactObjective[T: ToExactObjective]: ToObjective[T] = (t: T) implicitly[ToExactObjective[T]].apply(t)
implicit def objectiveToObjective: ToObjective[Objective] = (t: Objective) t
}
implicit def toObjective[T: ToObjective](t: T): Objective[_] = implicitly[ToObjective[T]].apply(t)
trait ToObjective[-T] {
def apply(t: T): Objective
}
implicit def toObjective[T: ToObjective](t: T): Objective = implicitly[ToObjective[T]].apply(t)
def toExact(o: Objective) =
o match {
case e: ExactObjective[_] e
case n: NoisyObjective[_] throw new UserBadDataError(s"Objective $n cannot be aggregated it should be exact.")
def toExact(o: Objective[_]) =
o.noisy match {
case false o
case true throw new UserBadDataError(s"Objective $o cannot be aggregated it should be exact.")
}
def toNoisy(o: Objective) = {
o match {
case n: NoisyObjective[_] n
case e: ExactObjective[_]
def medianAggregation[T](e: ExactObjective[T]) = {
def toNoisy[T](o: Objective[T]) = {
o.noisy match {
case true o
case false
def medianAggregation(e: Objective[T]) = FromContext { p
import p._
import org.openmole.tool.statistics._
(p: Array[T]) p.map(e.toDouble).median
(p: Array[T]) p.map(e.toDouble.from(context)).median
}
NoisyObjective(e.prototype, medianAggregation(e), e.negative, e.delta, e.as)
}
}
}
sealed trait Objective
object Objectives {
def onlyExact(o: Objectives) = Objectives.value(o).collect { case x: ExactObjective[_] x }.size == Objectives.value(o).size
def toExact(o: Objectives) = Objectives.value(o).map(o Objective.toExact(o))
def toNoisy(o: Objectives) = Objectives.value(o).map(o Objective.toNoisy(o))