3

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?

4

1 回答 1

2

好的,问题是 Engine.builder 期望它的第二个类型参数具有 Comparable 的上限,因为 Scalas Int 没有实现这个接口,所以上面的代码不能编译也就不足为奇了。

可能的解决方案之一是使用 java.lang.Integer 而不是 scala.Int

于 2015-01-12T16:54:52.573 回答