Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "CRandomSearch.h"
- #include <thread>
- CRandomSearch::CRandomSearch(CProblem* pcProblem) : COptimizer(pcProblem) {
- bHasProblem = true;
- }
- void CRandomSearch::vSetProblem(CProblem* pcProblem) {
- this->pcProblem = pcProblem;
- bHasProblem = true;
- }
- vector<double>* CRandomSearch::pdFindSolution(int iIterationLimit) {
- pdBestSolution = pdFindByThread(1);
- vector<thread> threads;
- while (iIterationLimit > 0) {
- if (iIterationLimit > 100) {
- threads.push_back(thread(&CRandomSearch::pdFindByThread, this, 100));
- }
- else {
- threads.push_back(thread(&CRandomSearch::pdFindByThread, this, iIterationLimit));
- }
- iIterationLimit -= 100;
- }
- for (int i = 0; i < threads.size(); i++) {
- threads[i].join();
- }
- return pdBestSolution;
- }
- vector<double>* CRandomSearch::pdFindByThread(int iIterationLimit) {
- vector<double>* pdSolution = new vector<double>();
- vector<double>* pdBestSolution = new vector<double>();
- CRandom cRandom;
- string err = "";
- for (int iIter = 0; iIter < iIterationLimit; iIter++) {
- pdSolution->clear();
- vector<int>* piParamIntegers = pcProblem->piGetParamIntegers();
- int iNumberOfSuppliers = piParamIntegers->at(0);
- int iNumberOfFactories = piParamIntegers->at(1);
- int iNumberOfMagazines = piParamIntegers->at(2);
- int iNumberOfShops = piParamIntegers->at(3);
- delete piParamIntegers;
- vector<double*>* ppdParamDoubleArrays = pcProblem->ppdGetParamDoubleArrays();
- double* pdArrayOfProductionCapacityOfSuppliers = ppdParamDoubleArrays->at(0);
- double* pdArrayOfProductionCapacityOfFactories = ppdParamDoubleArrays->at(1);
- double* pdArrayOfProductionCapacityOfMagazines = ppdParamDoubleArrays->at(2);
- double* pdArrayOfMarketDemandOfShops = ppdParamDoubleArrays->at(3);
- double* pdProductsFromSuppliers = new double[iNumberOfSuppliers];
- double* pdProductsFromFactories = new double[iNumberOfFactories];
- double* pdProductsFromMagazines = new double[iNumberOfMagazines];
- double* pdProductsToFactories = new double[iNumberOfFactories];
- double* pdProductsToMagazines = new double[iNumberOfMagazines];
- double* pdProductsToShops = new double[iNumberOfShops];
- for (int i = 0; i < iNumberOfSuppliers; i++) {
- pdProductsFromSuppliers[i] = 0;
- }
- for (int i = 0; i < iNumberOfFactories; i++) {
- pdProductsFromFactories[i] = 0;
- pdProductsToFactories[i] = 0;
- }
- for (int i = 0; i < iNumberOfMagazines; i++) {
- pdProductsFromMagazines[i] = 0;
- pdProductsToMagazines[i] = 0;
- }
- for (int i = 0; i < iNumberOfShops; i++) {
- pdProductsToShops[i] = 0;
- }
- double* pdSolutionMinimalValues = ppdParamDoubleArrays->at(4);
- double* pdSolutionMaximalValues = ppdParamDoubleArrays->at(5);
- delete ppdParamDoubleArrays;
- int iCurrentCell = 0;
- for (int i = 0; i < iNumberOfSuppliers; i++) {
- for (int j = 0; j < iNumberOfFactories; j++) {
- double limits[] = {
- pdSolutionMaximalValues[iCurrentCell],
- pdArrayOfProductionCapacityOfSuppliers[i] - pdProductsFromSuppliers[i],
- pdArrayOfProductionCapacityOfFactories[j] - pdProductsToFactories[j]
- };
- double value = cRandom.dGetRandomDouble(pdSolutionMinimalValues[iCurrentCell], dFindMinimal(limits, 3));
- pdProductsFromSuppliers[i] += value;
- pdProductsToFactories[j] += value;
- pdSolution->push_back(value);
- iCurrentCell++;
- }
- }
- for (int i = 0; i < iNumberOfFactories; i++) {
- for (int j = 0; j < iNumberOfMagazines; j++) {
- double limits[] = {
- pdSolutionMaximalValues[iCurrentCell],
- pdProductsToFactories[i] - pdProductsFromFactories[i],
- pdArrayOfProductionCapacityOfFactories[i] - pdProductsFromFactories[i],
- pdArrayOfProductionCapacityOfMagazines[j] - pdProductsToMagazines[j]
- };
- double value = cRandom.dGetRandomDouble(pdSolutionMinimalValues[iCurrentCell], dFindMinimal(limits, 4));
- pdProductsFromFactories[i] += value;
- pdProductsToMagazines[j] += value;
- pdSolution->push_back(value);
- iCurrentCell++;
- }
- }
- for (int i = 0; i < iNumberOfMagazines; i++) {
- for (int j = 0; j < iNumberOfShops; j++) {
- double limits[] = {
- pdSolutionMaximalValues[iCurrentCell],
- pdProductsToMagazines[i] - pdProductsFromMagazines[i],
- pdArrayOfProductionCapacityOfMagazines[i] - pdProductsFromMagazines[i],
- pdArrayOfMarketDemandOfShops[j] - pdProductsToShops[j]
- };
- double value = cRandom.dGetRandomDouble(pdSolutionMinimalValues[iCurrentCell], dFindMinimal(limits, 4));
- pdProductsFromMagazines[i] += value;
- pdProductsToShops[j] += value;
- pdSolution->push_back(value);
- iCurrentCell++;
- }
- }
- delete[] pdProductsFromSuppliers;
- delete[] pdProductsFromFactories;
- delete[] pdProductsFromMagazines;
- delete[] pdProductsToFactories;
- delete[] pdProductsToMagazines;
- delete[] pdProductsToShops;
- if (pcProblem->bConstraintsSatisfied(pdSolution, err)) {
- if (iIter == 0) {
- for (int i = 0; i < pdSolution->size(); i++) {
- pdBestSolution->push_back(pdSolution->at(i));
- }
- }
- else if (pcProblem->dGetQuality(pdSolution, err) > pcProblem->dGetQuality(pdBestSolution, err)) {
- for (int i = 0; i < pdSolution->size(); i++) {
- pdBestSolution->at(i) = pdSolution->at(i);
- }
- }
- }
- else {
- iIter--;
- }
- }
- delete pdSolution;
- m.lock();
- if (this->pdBestSolution->empty() || pcProblem->dGetQuality(pdBestSolution, err) > pcProblem->dGetQuality(
- this->pdBestSolution, err)) {
- delete this->pdBestSolution;
- this->pdBestSolution = pdBestSolution;
- }
- else {
- delete pdBestSolution;
- }
- m.unlock();
- return pdBestSolution;
- }
- double CRandomSearch::dFindMinimal(double* pdNumbers, int iLength) {
- double min = pdNumbers[0];
- for (int i = 0; i < iLength; i++) {
- if (pdNumbers[i] < min) {
- min = pdNumbers[i];
- }
- }
- return min;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement