Implement/instantiate abstract class via reflection in Scala?

I don't know if you can instantiate it by reflection while, at the same time, defining the abstract methods. Or, at least, not easily Why don't you make these methods into functions? The way I would go about it is something like this: private var _selectionStrategy: OptionPopulation => List(Individual, Double) = None def selectionStrategy(p: Population) = _selectionStrategy.

GetOrElse(error("Uninitialized selection strategy! "))(p) def setSelectionStrategy(f: Population => List(Individual, Double)) = if (_selectionStrategy. IsEmpty) _selectionStrategy = f else error("Selection strategy already initialized!") // Same thing for nextGeneration Of course OneMax wouldn't be abstract then.

Which, actually, is kind of the point. You then use reflection to create a new instance of OneMax which is reasonably straight-forward, and use setSelectionStrategy and setNextGeneration to set the functions.

I don't know if you can instantiate it by reflection while, at the same time, defining the abstract methods. Or, at least, not easily. Why don't you make these methods into functions?

The way I would go about it is something like this: private var _selectionStrategy: OptionPopulation => List(Individual, Double) = None def selectionStrategy(p: Population) = _selectionStrategy. GetOrElse(error("Uninitialized selection strategy! "))(p) def setSelectionStrategy(f: Population => List(Individual, Double)) = if (_selectionStrategy.

IsEmpty) _selectionStrategy = f else error("Selection strategy already initialized! ") // Same thing for nextGeneration Of course, OneMax wouldn't be abstract then. Which, actually, is kind of the point.

You then use reflection to create a new instance of OneMax, which is reasonably straight-forward, and use setSelectionStrategy and setNextGeneration to set the functions.

You cannot instantiate abstract class you don't need abstract class fitness, selectionStrategy, nextGeneration - are all "independent" variables. Thus tying them togather in one interface is going against the nature of the problem. Try this: type Fitness = Individual => Double type SelectionStrategy = Population = > List(Individual, Double) type NextGeneration = Population => Population case class EAProblem( fitness: Fitness, selectionStrategy: SelectionStrategy, nextGeneration: NextGeneration) { /* common code */ } val fitnesses = List(OneMax, classA, classB, ...) val strategies = List( SelectionStrategies.

SigmaScalingMatingSelection, SelectionStrategies. BoltzmannSelection) fitnesses. Map ( fitness => strategies.

Map ( strategy => EAProblem(fitness, strategy, SelectionProtocols. FullReplacement))) Edit: you can instantiate abstract class ... with CGLib or such.

If you really want to use reflection (with runtime code compilation) in this way, I think you're better off with a language like Python. But I don't think you really want to use reflection in this way. I think your best bet is to have a second class rather than a trait contain the routines that perform fitness measurement.

For example, abstract class Selector { def fitness(ind: Individual): Double def name: String } class Throughput extends Selector { def fitness(ind: Individual) = ind. FractionCorrect/ind. ComputeTime def name = "Throughput" } You then can val selectors = List(new Throughput, new ...) val userInputMap = List.

Map( t => (t. Name , t) ). ToMap and look up the right selector by name.

You then have OneMax (and the others) take the selector as a constructor argument, which you can provide from a string via the userInputMap.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions