/// Constructs new random solution; used by PSA, MOSA, SMOSA, NSGA and SPEA TDSESolution::TDSESolution() : TMOMHSolution() { this->WeightVector = GetRandomWeightVector(); //A solution is formed by filling the map //param2Value. createRandom(*this); // Now I have to set the objective value for the execution time and the power consumption; // in order to do this I have to resort to simulation this->updateObjectives(); }
template < class TProblemSolution > void TPMA < TProblemSolution >::Run() { // Callback function this->Start(); this->MainPopulation.DeleteAll(); this->pNondominatedSet->DeleteAll(); this->FindInitialPopulation(); // Callback function this->InitialPopulationFound(); MainPopulationSize = this->MainPopulation.size() * this->TempPopulationSize; // Main loop unsigned int Iteration = 0; do { // Draw at random new weight vector this->WeightVector = GetRandomWeightVector(); // Rescale the weight vector this->WeightVector.Rescale(this->pNondominatedSet->ApproximateIdealPoint, this->pNondominatedSet->ApproximateNadirPoint); // Calculate reference point TPoint TempReferencePoint = this->pNondominatedSet->ApproximateIdealPoint; TempReferencePoint.Augment(this->pNondominatedSet->ApproximateIdealPoint, this->pNondominatedSet->ApproximateNadirPoint); // Find two different solutions for recombination int TournamentSize = (int) floor(3.0 * this->MainPopulation.size() / (this->TempPopulationSize + 1) + 0.5); // Parent 1 - the best solution in tournament // Parent 2 - the second best solution in tournament TProblemSolution *Parent1 = (TProblemSolution *) this->MainPopulation[rand() % this->MainPopulation.size()]; TProblemSolution *Parent2; do { Parent2 = (TProblemSolution *) this->MainPopulation[rand() % this->MainPopulation.size()]; } while (Parent1 == Parent2); Parent1->SetScalarizingFunctionType(this->ScalarizingFunctionType); Parent1->ScalarizingFunctionValue = Parent1->ScalarizingFunction(TempReferencePoint, this->WeightVector); Parent2->SetScalarizingFunctionType(this->ScalarizingFunctionType); Parent2->ScalarizingFunctionValue = Parent2->ScalarizingFunction(TempReferencePoint, this->WeightVector); // Perform tournament int i; for (i = 2; i < TournamentSize; i++) { TProblemSolution *Challenger = (TProblemSolution *) this->MainPopulation[rand() % this->MainPopulation.size()]; Challenger->SetScalarizingFunctionType(this->ScalarizingFunctionType); Challenger->ScalarizingFunctionValue = Challenger->ScalarizingFunction(TempReferencePoint, this->WeightVector); if (Challenger->ScalarizingFunctionValue < Parent1->ScalarizingFunctionValue) { Parent2 = Parent1; Parent1 = Challenger; } else if ((Challenger->ScalarizingFunctionValue < Parent2->ScalarizingFunctionValue) && (Parent1 != Challenger)) { Parent2 = Challenger; } } // Generate new solution by recombination TListSet < TProblemSolution > TempNondominatedSet; TProblemSolution *Solution = new TProblemSolution(*Parent1, *Parent2, this->ScalarizingFunctionType, TempReferencePoint, this->WeightVector, TempNondominatedSet); // Update the nondominated set bool bAdded = this->pNondominatedSet->Update(*Solution); bool bAddedSet = this->pNondominatedSet->Update(TempNondominatedSet); bAdded = bAdded || bAddedSet; Solution->ScalarizingFunctionValue = Solution->ScalarizingFunction(TempReferencePoint, this->WeightVector); bool bBetter = Solution->ScalarizingFunctionValue < Parent2->ScalarizingFunctionValue; if (bAdded) { // Callback function this->NewNondominatedSolutionFound(); } // If the new solution is nondominated or better than // the worst solution in the current population // Add it to the main population if (bAdded || bBetter) { this->MainPopulation.push_back(Solution); if (this->MainPopulation.size() > MainPopulationSize) { delete((TProblemSolution *) this->MainPopulation[0]); this->MainPopulation.erase(this->MainPopulation.begin()); } } else delete Solution; // Callback function NewSolutionGenerated(*Solution); Iteration++; } while (!this->bStop && ((this->bOnlineOfflineEvaluating) || (Iteration < NumberOfIterations * this->InitialPopulationSize))); this->MainPopulation.DeleteAll(); // Callback function this->End(); }
/// Recombination constructor used by NSGA and SPEA; there is not local heuristic to /// be called at the end of the recombination operator TDSESolution::TDSESolution(TDSESolution & Parent1, TDSESolution & Parent2) : TMOMHSolution(Parent1, Parent2){ this->WeightVector = GetRandomWeightVector(); this->crossSolution(*this, Parent1, Parent2); this->updateObjectives(); }