diff --git a/src/GeneticAlgorithm/MainProcess.cpp b/src/GeneticAlgorithm/MainProcess.cpp index 991374ffe6c84bfc421bf3d6ddb8bbd01c820e79..25f9897e5cf17ddc59192c16b59c4b38e373ad47 100644 --- a/src/GeneticAlgorithm/MainProcess.cpp +++ b/src/GeneticAlgorithm/MainProcess.cpp @@ -70,20 +70,24 @@ namespace GeneticAlgorithm { if (nullptr == this->population || nullptr == this->selectedChromosome || nullptr == this->newChromosome) { return; } - if (1 == this->keep && keep > 1) { // 之前是keep=1的话,会因为优化而不会排序 - this->keep = keep; - this->sort(); // 先排序避免后满淘汰掉较优解 - } else { - this->keep = keep; + if (this->keep != keep) { // 之前是keep=1的话,会因为优化而不会排序 + if (1 == this->keep && keep > 1) { // 之前是keep=1的话,会因为优化而不会排序 + this->keep = keep; + this->kill = this->numberOfChromosome - keep; + this->sort(); // 先排序避免后满淘汰掉较优解 + } else { + this->keep = keep; + this->kill = this->numberOfChromosome - keep; + } + // 尺寸发生变化,删除旧的再申请新空间 + delete[] this->selectedChromosome; + delete[] this->newChromosome; + this->selectedChromosome = new Chromosome*[2 * this->kill]; + this->newChromosome = new Chromosome*[this->kill]; } - this->kill = this->numberOfChromosome - keep; - // 尺寸发生变化,删除旧的再申请新空间 - delete[] this->selectedChromosome; - delete[] this->newChromosome; - this->selectedChromosome = new Chromosome*[2 * this->kill]; - this->newChromosome = new Chromosome*[this->kill]; this->r = r; unsigned long i = 0; + this->maxFitness = this->population->getMaxFitnessChromosome()->getFitness(); while (i < maxLoop && this->maxFitness < stopFitness) { this->select(); this->crossover(); @@ -120,9 +124,9 @@ namespace GeneticAlgorithm { } void MainProcess::replaceChromosome(Chromosome* chromosome) { - Chromosome* max = this->population->getMaxFitnessChromosome(); + Chromosome* maxChromosome = this->population->getMaxFitnessChromosome(); for (unsigned long offset = this->numberOfChromosome - 1; offset + 2 > 1; offset--) { - if ((void*)this->population->getChromosome(offset) != (void*)max) { + if ((void*)this->population->getChromosome(offset) != (void*)maxChromosome) { this->population->replaceChromosome(offset, chromosome); return; } diff --git a/src/GeneticAlgorithm/Population.cpp b/src/GeneticAlgorithm/Population.cpp index 82af57504db56ead910130f65163712f41d99281..f4673f87197f62ee72d19a460ca2e6868f68e44b 100644 --- a/src/GeneticAlgorithm/Population.cpp +++ b/src/GeneticAlgorithm/Population.cpp @@ -22,50 +22,35 @@ namespace GeneticAlgorithm { } bool Population::setChromosome(unsigned long offset, Chromosome *chromosome) { - if (offset < this->numberOfChromosome && !this->chromosomeArray[offset]) { - if (this->isMaxFitnessChromosomeCache) { - if (this->maxFitnessChromosomeCache->getFitness() > chromosome->getFitness()) { - if (this->maxFitnessChromosomeOffset != offset) { - this->chromosomeArray[offset] = chromosome; - } else { - this->isMaxFitnessChromosomeCache = false; - this->chromosomeArray[offset] = chromosome; - } - } else { - this->maxFitnessChromosomeCache = chromosome; - this->maxFitnessChromosomeOffset = offset; - this->chromosomeArray[offset] = chromosome; - } + if (offset >= this->numberOfChromosome) { + return false; + } + if (nullptr == this->chromosomeArray[offset]) { + this->chromosomeArray[offset] = chromosome; + this->isMaxFitnessChromosomeCache = false; + return true; + } + Chromosome* origin = this->chromosomeArray[offset]; + if ((void*)origin == (void*)chromosome) { + return true; + } + if (this->isMaxFitnessChromosomeCache) { + if ((void*)(this->maxFitnessChromosomeCache) == (void*)chromosome) { + return false; + } + if (this->maxFitnessChromosomeCache->getFitness() > chromosome->getFitness()) { + this->isMaxFitnessChromosomeCache = false; } else { - this->chromosomeArray[offset] = chromosome; + this->maxFitnessChromosomeCache = chromosome; } - return true; } - return false; + this->chromosomeArray[offset] = chromosome; + delete origin; + return true; } bool Population::replaceChromosome(unsigned long offset, Chromosome *chromosome) { - if (offset < this->numberOfChromosome && this->chromosomeArray[offset]) { - delete this->chromosomeArray[offset]; - if (this->isMaxFitnessChromosomeCache) { // 这段逻辑与setChromosome里面的相同 - if (this->maxFitnessChromosomeCache->getFitness() > chromosome->getFitness()) { - if (this->maxFitnessChromosomeOffset != offset) { - this->chromosomeArray[offset] = chromosome; - } else { - this->isMaxFitnessChromosomeCache = false; - this->chromosomeArray[offset] = chromosome; - } - } else { - this->maxFitnessChromosomeCache = chromosome; - this->maxFitnessChromosomeOffset = offset; - this->chromosomeArray[offset] = chromosome; - } - } else { - this->chromosomeArray[offset] = chromosome; - } - return true; - } - return false; + return this->setChromosome(offset, chromosome); } Chromosome* Population::getChromosome(unsigned long offset) {