我使用Jenetics库来解决ga的一个问题。我将官方的例子扩展到使用以下几条染色体:
List<BitChromosome> arr = new ArrayList<>();
arr.add(BitChromosome.of(16, 0.5));
arr.add(BitChromosome.of(16, 0.5));
arr.add(BitChromosome.of(16, 0.5));
Factory<Genotype<BitGene>> gtf = Genotype.of(arr);并将eval函数更改为正好有81s和80s:
private static int eval(Genotype<BitGene> gt) {
return 10 - Math.abs(gt.getChromosome()
.as(BitChromosome.class)
.bitCount()-8);其余部分保持不变:
// 3.) Create the execution environment.
Engine<BitGene, Integer> engine = Engine
.builder(Test1::eval, gtf)
.build();
// 4.) Start the execution (evolution) and
// collect the result.
Genotype<BitGene> result = engine.stream()
.limit(100)
.collect(EvolutionResult.toBestGenotype());我原以为ga go会产生3条染色体,最大限度地发挥这一作用,但我得到了:
[01110010|00010111,01000000|00000100,10011101|01110110]如您所见,只有第一个结果满足条件。我怎样才能扩展这个例子,使所有染色体都能最大限度地发挥评估功能?
发布于 2017-11-09 11:28:46
这正是我在看了健身功能之后所期望的。你只使用第一条染色体来计算适合度。Genotype.getChromosome()方法返回第一染色体。这是Genotype.getChromosome(0)的一条捷径。另外两条染色体在你的健身功能中不被考虑。
https://stackoverflow.com/questions/47196922
复制相似问题