gpt4 book ai didi

java - JGAP 不保留适者生存

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:31 25 4
gpt4 key购买 nike

我正在尝试使用 JGAP 来完成遗传算法任务。我用过他们的例子:

    // Start with a DefaultConfiguration, which comes setup with the
// most common settings.
// -------------------------------------------------------------
Configuration conf = new DefaultConfiguration();

// Set the fitness function we want to use, which is our
// MinimizingMakeChangeFitnessFunction that we created earlier.
// We construct it with the target amount of change provided
// by the user.
// ------------------------------------------------------------
int targetAmount = TARGET_AMOUNT_OF_CHANGE;
FitnessFunction myFunc = new MinimizingMakeChangeFitnessFunction(targetAmount);

conf.setFitnessFunction(myFunc);

// Now we need to tell the Configuration object how we want our
// Chromosomes to be setup. We do that by actually creating a
// sample Chromosome and then setting it on the Configuration
// object. As mentioned earlier, we want our Chromosomes to
// each have four genes, one for each of the coin types. We
// want the values of those genes to be integers, which represent
// how many coins of that type we have. We therefore use the
// IntegerGene class to represent each of the genes. That class
// also lets us specify a lower and upper bound, which we set
// to sensible values for each coin type.
// --------------------------------------------------------------
Gene[] sampleGenes = new Gene[4];

sampleGenes[0] = new IntegerGene(conf, 0, 3); // Quarters
sampleGenes[1] = new IntegerGene(conf, 0, 2); // Dimes
sampleGenes[2] = new IntegerGene(conf, 0, 1); // Nickels
sampleGenes[3] = new IntegerGene(conf, 0, 4); // Pennies

Chromosome sampleChromosome = new Chromosome(conf, sampleGenes);

conf.setSampleChromosome(sampleChromosome);

// Finally, we need to tell the Configuration object how many
// Chromosomes we want in our population. The more Chromosomes,
// the larger the number of potential solutions (which is good
// for finding the answer), but the longer it will take to evolve
// the population each round. We'll set the population size to
// 500 here.
// --------------------------------------------------------------
conf.setPopulationSize(30000);

Genotype population = Genotype.randomInitialGenotype(conf);

for (int i = 0; i < MAX_ALLOWED_EVOLUTIONS; i++) {
population.evolve();
}

IChromosome bestSolutionSoFar = population.getFittestChromosome();

当我打印它时:

System.out.println(population.getConfiguration().isPreserveFittestIndividual());

我发现它是错误。我错过了什么吗?

最佳答案

JGAP 默认使用 NaturalSelectors。这意味着每条染色体被选择进入下一步的概率与其染色体的适应度值成正比。被选中的可能性最高并不能保证被后代选中。

JGAP 允许您在配置设置中使用以下命令以确定性方式始终保留最佳染色体。一个通用的例子:

Configuration.setPreservFittestIndividual(boolean a_preserveFittest);

在你的情况下,你应该写这样的东西:

...
conf.setFitnessFunction(myFunc);
conf.setPreservFittestIndividual(true);
...

希望对你有帮助。有关更多信息,您可以查看 JGAP v3.6 API Here

关于java - JGAP 不保留适者生存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36379060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com