Jenetics HelloWorld in Scala

350 Views Asked by At

I'm trying to run a Jenetics HelloWorld example from Scala.

import org.jenetics.{BitChromosome, BitGene, Genotype}
import org.jenetics.engine.{Engine, EvolutionResult}

object BitCounting extends App {

  val genotypeFactory = Genotype.of(BitChromosome.of(128, 0.5))

  val fitness: java.util.function.Function[Genotype[BitGene], Int] = new java.util.function.Function[Genotype[BitGene], Int] {
    override def apply(genotype: Genotype[BitGene]): Int = genotype.asInstanceOf[BitChromosome].bitCount()
  }

  val engine = Engine.builder(fitness, genotypeFactory).build()    

  val result = engine
    .stream()
    .limit(100)
    .collect(EvolutionResult.toBestGenotype[BitGene, Int])

  println(s"Hello world:\n$result")

}

I'm getting a compilation error on the line where engine is initialized. Compiler complains that no Engine.Builder of conforming types exists. Can anyone explain why?

1

There are 1 best solutions below

0
On

Ok, the problem was that Engine.builder expects its second type parameter to have an upper bound to Comparable, since Scalas Int does not implement this interface, no surprise above code does not compile.

One of the solutions possible is to use java.lang.Integer instead of scala.Int