我正在尝试运行 Jenetics来自 Scala 的 HelloWorld 示例。
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")
}
我在初始化引擎的那一行遇到编译错误。编译器提示说 不存在符合类型的 Engine.Builder。谁能解释为什么?
最佳答案
好的,问题是 Engine.builder 期望它的第二个类型参数有一个 Comparable 的上限,因为 Scalas Int 没有实现这个接口(interface),所以上面的代码不能编译也就不足为奇了。
可能的解决方案之一是使用 java.lang.Integer 而不是 scala.Int
关于java - Scala 中的 Jenetics HelloWorld,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27901296/