我正在尝试运行Scala中的Jenetics 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。有谁能解释一下原因吗?
发布于 2015-01-13 00:54:53
好的,问题是Engine.builder期望它的第二个类型参数的上限是可比较的,因为Scalas Int没有实现这个接口,所以上面的代码不能编译也就不足为奇了。
可能的解决方案之一是使用java.lang.Integer而不是scala.Int
https://stackoverflow.com/questions/27901296
复制相似问题