void Individual::update(Individual& source) { if (!this->isLocked()) return; if (Util::isBetterSolution(source.getTotalCost(), this->getTotalCost())) { this->setGene(source.getGene()); this->setRoutes(source.getRoutesConst()); this->updateParameters(true); } }
Individual Individual::evolve() { // //If it is locked // if (this->isLocked()) // return Individual(); this->setLocked(true); Individual offspring = this->copy(); if (this->getGene().size() == 0) { printf("Subpop = %d => Ind vazio!\n", this->getDepot()); Individual offspring = Individual(this->getProblem(), this->getConfig(), this->getDepot(), this->getId()); offspring.create(); } //cout << "Offspring = " << offspring.getTotalCost() << endl; bool mutate = false; //printf("Offspring = D: %d - Id: %d => PM: %.2f / PLS: %.2f / RS: %d\n", this->getDepot(), // this->getId(), this->getMutationRatePM(), this->getMutationRatePLS(), this->isRestartMoves()); if (Random::randFloat() <= this->getMutationRatePM()) { offspring.mutate(); offspring.evaluate(true); mutate = true; } else { if (offspring.getRoutes().size() == 0) offspring.evaluate(true); } //offspring.printSolution(); if (Random::randFloat() <= this->getMutationRatePLS()) { //Individual orig = offspring; //offspring.printSolution(true); offspring.localSearch(); offspring.routesToGenes(); //offspring.printSolution(true); if (offspring.getGene().size() == 0) { cout << "Mutate = " << mutate << endl; this->print(); this->print(true); this->printSolution(); offspring.print(); offspring.print(true); offspring.printSolution(); // orig.print(); // orig.print(true); // orig.printSolution(); } } if (this->getRoutes().size() == 0) { //cout << "Wait\n\n"; //cout << "Old: " << this->getTotalCost() << endl; this->evaluate(true); //cout << "New: " << this->getTotalCost() << endl; } if (Util::isBetterSolution(offspring.getTotalCost(), this->getTotalCost())) { //this->setGene(offspring.getGene()); //this->setRoutes(offspring.getRoutesConst()); //this->evaluate(true); this->updateParameters(true); } else { this->updateParameters(false); } //this->print(true); //this->setLocked(false); return offspring; }