I'm working on a framework for a EA (evolutionary alg) project in Scala. In this i have a trait that implements common EA-code and leaves problem spesific code like genotype convertion and fitness testing to classes that implement this trait. However, I don't want to fully implement the trait before it is actually run because of testing different population selection protocols/strategies. This gives the code
trait EAProblem{
// common code ...
def fitness(ind:Individual):Double
def selectionStrategy(p: Population): List[(Individual, Double)]
def nextGeneration(p: Population): Population
}
/* Silly test problem */
abstract class OneMax(logPath: String) extends EAProblem {
def phenotype(ind:Individual) = {
ind.genotype
}
def fitness(ind: Individual): Double = {
ind.genotype.size.toFloat / ind.genotype.capacity
}
}
At runtime the protocol/strategy is choosen:
object EASelectionStrategyProtocolDemo {
def main(args: Array[String]) {
val problem_impl = List[EAProblem](
// Full replacement
new OneMax("sigma_strat_full-rep_prot_onemax.log.dat") {
def selectionStrategy(p: Population): List[(Individual, Double)] =
SelectionStrategies.sigmaScalingMatingSelection(p)
def nextGeneration(p: Population): Population = SelectionProtocols.fullReplacement(p)
},
new OneMax("boltz_strat_full-rep_prot_onemax.log.dat") {
def selectionStrategy(p: Population): List[(Individual, Double)] =
SelectionStrategies.boltzmannSelection(p)
def nextGeneration(p: Population): Population = SelectionProtocols.fullReplacement(p)
})
for(problem <- problem_impl)
new Simulator(problem)
}
The SelectionStrategies/SelectionProtocols objects contains clusures with references to other code in EAProblem.
What I want now is some way to instantiate other abstract classes like OneMax (I have many of them) using reflection (or some other mechanism). Pseudocode:
val listOfClassNames = List("OneMax", "classA", "classB", ...)
for(className <- listOfClassNames){
class_sigma = Class.forname(className)
/*
Implement class_class with this code and instantiate it
def selectionStrategy(p: Population): List[(Individual, Double)] =
SelectionStrategies.sigmaScalingMatingSelection(p)
def nextGeneration(p: Population): Population = SelectionProtocols.fullReplacement(p)
*/
class_boltz = Class.forname(className)
/*
Implement class_boltz with this code and instantiate it
def selectionStrategy(p: Population): List[(Individual, Double)] =
SelectionStrategies.boltzmannSelection(p)
def nextGeneration(p: Population): Population = SelectionProtocols.fullReplacement(p)
*/
}