Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. //
  2. // Created by Damian on 16.01.2020.
  3. //
  4.  
  5. #include "CDiffEvol.h"
  6. #include <algorithm>
  7. #include <thread>
  8.  
  9. CDiffEvol::CDiffEvol(CProblem* pcProblem) : COptimizer(pcProblem) {
  10.     iGenotypeSize = pcProblem->iGetSolutionSize();
  11.     iQuantityOfIndividuals = iDefaultQuantityOfIndividuals;
  12.     pcIndividuals = new vector<CIndividual*>();
  13.     for (int i = 0; i < iQuantityOfIndividuals; i++) {
  14.         pcIndividuals->push_back(new CIndividual(pcProblem));
  15.     }
  16. }
  17.  
  18. CDiffEvol::CDiffEvol(int iQuantityOfIndividuals, CProblem* pcProblem) : COptimizer(pcProblem) {
  19.     iGenotypeSize = pcProblem->iGetSolutionSize();
  20.     this->iQuantityOfIndividuals = iQuantityOfIndividuals;
  21.     pcIndividuals = new vector<CIndividual*>();
  22.     for (int i = 0; i < iQuantityOfIndividuals; i++) {
  23.         pcIndividuals->push_back(new CIndividual(pcProblem));
  24.     }
  25. }
  26.  
  27. CDiffEvol::CDiffEvol(vector<double>** ppdInitSolutions, int iQuantityOfIndividuals, CProblem* pcProblem) : COptimizer(
  28.     pcProblem) {
  29.     iGenotypeSize = pcProblem->iGetSolutionSize();
  30.     this->iQuantityOfIndividuals = iQuantityOfIndividuals;
  31.     pcIndividuals = new vector<CIndividual*>();
  32.     for (int i = 0; i < iQuantityOfIndividuals; i++) {
  33.         pcIndividuals->push_back(new CIndividual(ppdInitSolutions[i], pcProblem));
  34.     }
  35. }
  36.  
  37. CDiffEvol::~CDiffEvol() {
  38.     for (int i = 0; i < iQuantityOfIndividuals; i++) {
  39.         delete pcIndividuals->at(i);
  40.     }
  41.     delete pcIndividuals;
  42. }
  43.  
  44. vector<double>* CDiffEvol::pdFindSolution(int iLimit) {
  45.     this->iLimit = iLimit;
  46.     vector<thread> threads;
  47.     while (bCheckStopCondition()) {
  48.         for (int i = 0; i < iQuantityOfIndividuals; i++) {
  49.             iIndex = i;
  50.             threads.push_back(thread(&CDiffEvol::vEvolveOne, this, i));
  51.         }
  52.         for (int i = 0; i < iQuantityOfIndividuals; i++) {
  53.             threads.at(i).join();
  54.         }
  55.         threads.clear();
  56.         // for (int i = 0; i < iQuantityOfIndividuals; i++) {
  57.         //  CIndividual* cBaseInd = cGetRandomInd();
  58.         //  CIndividual* cAddInd0 = cGetRandomInd();
  59.         //  CIndividual* cAddInd1 = cGetRandomInd();
  60.         //  if (bIndividualsAreDifferent(pcIndividuals->at(i), cBaseInd, cAddInd0, cAddInd1)) {
  61.         //      CIndividual* indNew = new CIndividual(pcProblem);
  62.         //      for (int iGeneOffset = 0; iGeneOffset < iGenotypeSize; iGeneOffset++) {
  63.         //          if (cRand.dGetRandomDouble(0, 1) < dCrossProb) {
  64.         //              indNew->at(iGeneOffset) =
  65.         //                  cBaseInd->at(iGeneOffset) +
  66.         //                  dDiffWeight * (cAddInd0->at(iGeneOffset) - cAddInd1->at(iGeneOffset));
  67.         //          }
  68.         //          else indNew->at(iGeneOffset) = pcIndividuals->at(i)->at(iGeneOffset);
  69.         //      }
  70.         //      string err;
  71.         //      if (pcProblem->bConstraintsSatisfied(indNew->pdGenotype, err) && dFitness(indNew) >=
  72.         //          dFitness(pcIndividuals->at(i))) {
  73.         //          delete pcIndividuals->at(i);
  74.         //          pcIndividuals->at(i) = indNew;
  75.         //      }
  76.         //      else {
  77.         //          delete indNew;
  78.         //      }
  79.         //  }
  80.         // }
  81.     }
  82.     return pdGetBestSolution();
  83. }
  84.  
  85.  
  86. void CDiffEvol::vEvolveOne(int iIndex) {
  87.     CRandom cRand;
  88.     CIndividual* cBaseInd = cGetRandomInd();
  89.     CIndividual* cAddInd0 = cGetRandomInd();
  90.     CIndividual* cAddInd1 = cGetRandomInd();
  91.     if (bIndividualsAreDifferent(pcIndividuals->at(iIndex), cBaseInd, cAddInd0, cAddInd1)) {
  92.         CIndividual* indNew = new CIndividual(pcProblem);
  93.         for (int iGeneOffset = 0; iGeneOffset < iGenotypeSize; iGeneOffset++) {
  94.             if (cRand.dGetRandomDouble(0, 1) < dCrossProb) {
  95.                 indNew->at(iGeneOffset) =
  96.                     cBaseInd->at(iGeneOffset) +
  97.                     dDiffWeight * (cAddInd0->at(iGeneOffset) - cAddInd1->at(iGeneOffset));
  98.  
  99.             }
  100.             else indNew->at(iGeneOffset) = pcIndividuals->at(iIndex)->at(iGeneOffset);
  101.  
  102.         }
  103.         string err;
  104.         mtx.lock();
  105.         if (pcProblem->bConstraintsSatisfied(indNew->pdGenotype, err) && dFitness(indNew) >=
  106.             dFitness(pcIndividuals->at(iIndex))) {
  107.             delete pcIndividuals->at(iIndex);
  108.             pcIndividuals->at(iIndex) = indNew;
  109.         }
  110.         else {
  111.             delete indNew;
  112.  
  113.         }
  114.         mtx.unlock();
  115.     }
  116. }
  117.  
  118. vector<double>* CDiffEvol::pdGetBestSolution() {
  119.     vector<double>* pdBestSolution;
  120.     pdBestSolution = pcIndividuals->at(0)->pdGenotype;
  121.     string err;
  122.     double dBestResult = pcProblem->dGetQuality(pdBestSolution, err);
  123.     for (int i = 1; i < iQuantityOfIndividuals; i++) {
  124.         vector<double>* pdCurrentSolution = pcIndividuals->at(i)->pdGenotype;
  125.         double dCurrentResult = pcProblem->dGetQuality(pdCurrentSolution, err);
  126.         if (dCurrentResult > dBestResult) {
  127.             pdBestSolution = pdCurrentSolution;
  128.             dBestResult = dCurrentResult;
  129.         }
  130.     }
  131.  
  132.     vector<double>* pdBestSolutionCopy = new vector<double>();
  133.     for (int i = 0; i < iGenotypeSize; i++) {
  134.         pdBestSolutionCopy->push_back(pdBestSolution->at(i));
  135.     }
  136.     return pdBestSolutionCopy;
  137. }
  138.  
  139. bool CDiffEvol::bCheckStopCondition() {
  140.     return iNumberOfRatings < iLimit;
  141. }
  142.  
  143. CIndividual* CDiffEvol::cGetRandomInd() {
  144.     CRandom cRand;
  145.     return pcIndividuals->at(cRand.iGetRandomInteger(0, iQuantityOfIndividuals - 1));
  146. }
  147.  
  148. bool CDiffEvol::bIndividualsAreDifferent(CIndividual* cInd, CIndividual* cBaseInd, CIndividual* cAddInd0,
  149.                                          CIndividual* cAddInd1) {
  150.     if (cInd->equals(cBaseInd)) return false;
  151.     if (cInd->equals(cAddInd0)) return false;
  152.     if (cInd->equals(cAddInd1)) return false;
  153.  
  154.     if (cBaseInd->equals(cAddInd0)) return false;
  155.     if (cBaseInd->equals(cAddInd1)) return false;
  156.  
  157.     return !cAddInd0->equals(cAddInd1);
  158.  
  159. }
  160.  
  161. double CDiffEvol::dFitness(CIndividual* cInd) {
  162.     iNumberOfRatings++;
  163.     string err;
  164.     return pcProblem->dGetQuality(cInd->pdGenotype, err);
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement