Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. #include "CRandomSearch.h"
  2. #include <thread>
  3.  
  4. CRandomSearch::CRandomSearch(CProblem* pcProblem) : COptimizer(pcProblem) {
  5.     bHasProblem = true;
  6. }
  7.  
  8. void CRandomSearch::vSetProblem(CProblem* pcProblem) {
  9.     this->pcProblem = pcProblem;
  10.     bHasProblem = true;
  11. }
  12.  
  13. vector<double>* CRandomSearch::pdFindSolution(int iIterationLimit) {
  14.     pdBestSolution = pdFindByThread(1);
  15.     vector<thread> threads;
  16.  
  17.     while (iIterationLimit > 0) {
  18.         if (iIterationLimit > 100) {
  19.             threads.push_back(thread(&CRandomSearch::pdFindByThread, this, 100));
  20.         }
  21.         else {
  22.             threads.push_back(thread(&CRandomSearch::pdFindByThread, this, iIterationLimit));
  23.         }
  24.         iIterationLimit -= 100;
  25.     }
  26.  
  27.  
  28.     for (int i = 0; i < threads.size(); i++) {
  29.         threads[i].join();
  30.     }
  31.  
  32.     return pdBestSolution;
  33. }
  34.  
  35.  
  36. vector<double>* CRandomSearch::pdFindByThread(int iIterationLimit) {
  37.     vector<double>* pdSolution = new vector<double>();
  38.     vector<double>* pdBestSolution = new vector<double>();
  39.     CRandom cRandom;
  40.     string err = "";
  41.  
  42.     for (int iIter = 0; iIter < iIterationLimit; iIter++) {
  43.         pdSolution->clear();
  44.         vector<int>* piParamIntegers = pcProblem->piGetParamIntegers();
  45.         int iNumberOfSuppliers = piParamIntegers->at(0);
  46.         int iNumberOfFactories = piParamIntegers->at(1);
  47.         int iNumberOfMagazines = piParamIntegers->at(2);
  48.         int iNumberOfShops = piParamIntegers->at(3);
  49.         delete piParamIntegers;
  50.  
  51.         vector<double*>* ppdParamDoubleArrays = pcProblem->ppdGetParamDoubleArrays();
  52.         double* pdArrayOfProductionCapacityOfSuppliers = ppdParamDoubleArrays->at(0);
  53.         double* pdArrayOfProductionCapacityOfFactories = ppdParamDoubleArrays->at(1);
  54.         double* pdArrayOfProductionCapacityOfMagazines = ppdParamDoubleArrays->at(2);
  55.         double* pdArrayOfMarketDemandOfShops = ppdParamDoubleArrays->at(3);
  56.  
  57.         double* pdProductsFromSuppliers = new double[iNumberOfSuppliers];
  58.         double* pdProductsFromFactories = new double[iNumberOfFactories];
  59.         double* pdProductsFromMagazines = new double[iNumberOfMagazines];
  60.  
  61.         double* pdProductsToFactories = new double[iNumberOfFactories];
  62.         double* pdProductsToMagazines = new double[iNumberOfMagazines];
  63.         double* pdProductsToShops = new double[iNumberOfShops];
  64.  
  65.         for (int i = 0; i < iNumberOfSuppliers; i++) {
  66.             pdProductsFromSuppliers[i] = 0;
  67.         }
  68.  
  69.         for (int i = 0; i < iNumberOfFactories; i++) {
  70.             pdProductsFromFactories[i] = 0;
  71.             pdProductsToFactories[i] = 0;
  72.         }
  73.  
  74.         for (int i = 0; i < iNumberOfMagazines; i++) {
  75.             pdProductsFromMagazines[i] = 0;
  76.             pdProductsToMagazines[i] = 0;
  77.         }
  78.  
  79.         for (int i = 0; i < iNumberOfShops; i++) {
  80.             pdProductsToShops[i] = 0;
  81.         }
  82.  
  83.         double* pdSolutionMinimalValues = ppdParamDoubleArrays->at(4);
  84.  
  85.         double* pdSolutionMaximalValues = ppdParamDoubleArrays->at(5);
  86.  
  87.         delete ppdParamDoubleArrays;
  88.  
  89.         int iCurrentCell = 0;
  90.  
  91.         for (int i = 0; i < iNumberOfSuppliers; i++) {
  92.             for (int j = 0; j < iNumberOfFactories; j++) {
  93.                 double limits[] = {
  94.                     pdSolutionMaximalValues[iCurrentCell],
  95.                     pdArrayOfProductionCapacityOfSuppliers[i] - pdProductsFromSuppliers[i],
  96.                     pdArrayOfProductionCapacityOfFactories[j] - pdProductsToFactories[j]
  97.                 };
  98.                 double value = cRandom.dGetRandomDouble(pdSolutionMinimalValues[iCurrentCell], dFindMinimal(limits, 3));
  99.                 pdProductsFromSuppliers[i] += value;
  100.                 pdProductsToFactories[j] += value;
  101.                 pdSolution->push_back(value);
  102.                 iCurrentCell++;
  103.             }
  104.         }
  105.  
  106.         for (int i = 0; i < iNumberOfFactories; i++) {
  107.             for (int j = 0; j < iNumberOfMagazines; j++) {
  108.                 double limits[] = {
  109.                     pdSolutionMaximalValues[iCurrentCell],
  110.                     pdProductsToFactories[i] - pdProductsFromFactories[i],
  111.                     pdArrayOfProductionCapacityOfFactories[i] - pdProductsFromFactories[i],
  112.                     pdArrayOfProductionCapacityOfMagazines[j] - pdProductsToMagazines[j]
  113.                 };
  114.                 double value = cRandom.dGetRandomDouble(pdSolutionMinimalValues[iCurrentCell], dFindMinimal(limits, 4));
  115.                 pdProductsFromFactories[i] += value;
  116.                 pdProductsToMagazines[j] += value;
  117.                 pdSolution->push_back(value);
  118.                 iCurrentCell++;
  119.             }
  120.         }
  121.  
  122.         for (int i = 0; i < iNumberOfMagazines; i++) {
  123.             for (int j = 0; j < iNumberOfShops; j++) {
  124.                 double limits[] = {
  125.                     pdSolutionMaximalValues[iCurrentCell],
  126.                     pdProductsToMagazines[i] - pdProductsFromMagazines[i],
  127.                     pdArrayOfProductionCapacityOfMagazines[i] - pdProductsFromMagazines[i],
  128.                     pdArrayOfMarketDemandOfShops[j] - pdProductsToShops[j]
  129.                 };
  130.                 double value = cRandom.dGetRandomDouble(pdSolutionMinimalValues[iCurrentCell], dFindMinimal(limits, 4));
  131.                 pdProductsFromMagazines[i] += value;
  132.                 pdProductsToShops[j] += value;
  133.                 pdSolution->push_back(value);
  134.                 iCurrentCell++;
  135.             }
  136.         }
  137.  
  138.         delete[] pdProductsFromSuppliers;
  139.         delete[] pdProductsFromFactories;
  140.         delete[] pdProductsFromMagazines;
  141.  
  142.         delete[] pdProductsToFactories;
  143.         delete[] pdProductsToMagazines;
  144.         delete[] pdProductsToShops;
  145.         if (pcProblem->bConstraintsSatisfied(pdSolution, err)) {
  146.             if (iIter == 0) {
  147.                 for (int i = 0; i < pdSolution->size(); i++) {
  148.                     pdBestSolution->push_back(pdSolution->at(i));
  149.                 }
  150.             }
  151.             else if (pcProblem->dGetQuality(pdSolution, err) > pcProblem->dGetQuality(pdBestSolution, err)) {
  152.                 for (int i = 0; i < pdSolution->size(); i++) {
  153.                     pdBestSolution->at(i) = pdSolution->at(i);
  154.                 }
  155.             }
  156.         }
  157.         else {
  158.             iIter--;
  159.         }
  160.  
  161.     }
  162.  
  163.     delete pdSolution;
  164.  
  165.     m.lock();
  166.     if (this->pdBestSolution->empty() || pcProblem->dGetQuality(pdBestSolution, err) > pcProblem->dGetQuality(
  167.         this->pdBestSolution, err)) {
  168.         delete this->pdBestSolution;
  169.         this->pdBestSolution = pdBestSolution;
  170.     }
  171.     else {
  172.         delete pdBestSolution;
  173.     }
  174.     m.unlock();
  175.  
  176.     return pdBestSolution;
  177. }
  178.  
  179. double CRandomSearch::dFindMinimal(double* pdNumbers, int iLength) {
  180.     double min = pdNumbers[0];
  181.     for (int i = 0; i < iLength; i++) {
  182.         if (pdNumbers[i] < min) {
  183.             min = pdNumbers[i];
  184.         }
  185.     }
  186.  
  187.     return min;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement