Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. //
  2. // Created by Jan Woźniak on 24.10.2018.
  3. //
  4.  
  5. #include "HeldKarp.h"
  6. #include <limits>
  7. #include <algorithm>
  8. #include <numeric>
  9. #include <list>
  10. #include "Timer.h"
  11.  
  12. HeldKarp::HeldKarp(std::string fileName) : AlgorithmTSP(std::move(fileName)) {
  13.     CreatePermutations(0);
  14.     PrintPermutations();
  15. }
  16.  
  17. void HeldKarp::PrintPermutations() {
  18.     for (const auto &innerVector : permutations_) {
  19.         for (const auto &vector_el : innerVector.middleValues_) {
  20.             std::cout << vector_el << "; ";
  21.         }
  22.         std::cout << std::endl;
  23.     }
  24. }
  25.  
  26. double HeldKarp::CalculatePath(unsigned startVertex) {
  27.     if (startVertex >= 0 && startVertex < graphSize_) {
  28.         CalculatedPath calculatedPath(0, graphSize_);
  29.         calculatedPath.visitedVertices_[startVertex] = true;
  30.         calculatedPath.path_.push_back(startVertex);
  31.  
  32.         Timer timer;
  33.         timer.StartCounter();
  34.         auto optimalPath = CalculatePath(startVertex, std::move(calculatedPath));
  35.         optimalPath.price_ += graph_[optimalPath.path_[graphSize_ - 1]][startVertex];
  36.         optimalPath.path_.push_back(startVertex);
  37.         double measured_time = timer.GetCounter();
  38.         std::cout << "Min price is equal to: " << optimalPath.price_ << std::endl;
  39.         std::cout << "Measured time is equal to: " << measured_time << "s." << std::endl;
  40.         for (const auto node : optimalPath.path_) {
  41.             std::cout << node << "; ";
  42.         }
  43.         std::cout << std::endl;
  44.         return measured_time;
  45.     } else {
  46.         std::cout << "Vertex is not a part of the graph" << std::endl;
  47.         return 0;
  48.     }
  49. }
  50.  
  51. CalculatedPath
  52. HeldKarp::CalculatePath(unsigned startVertex, CalculatedPath &&calculatedPath) {
  53.     if (CheckIfAllVerticesAreVisited(calculatedPath.visitedVertices_)) {
  54.         return calculatedPath;
  55.     }
  56.     std::vector<CalculatedPath> pathsFound;
  57.     for (unsigned i = 0; i < graphSize_; ++i) {
  58.         if (!calculatedPath.visitedVertices_.at(i)) {
  59.             CalculatedPath currentCalculatedPath(calculatedPath);
  60.             currentCalculatedPath.visitedVertices_.at(i) = true;
  61.             currentCalculatedPath.path_.push_back(i);
  62.             CalculatedPath calculatedPathToPushBack = CalculatePath(i, std::move(currentCalculatedPath));
  63.             calculatedPathToPushBack.price_ += graph_[startVertex][i];
  64.             pathsFound.push_back(std::move(calculatedPathToPushBack));
  65.         }
  66.     }
  67.     return pathsFound[FindIndexOfOptimalPath(pathsFound)];
  68. }
  69.  
  70. unsigned HeldKarp::FindIndexOfOptimalPath(const std::vector<CalculatedPath> &paths) {
  71.     unsigned indexOfOptimalPath = 0;
  72.     unsigned minPrice = std::numeric_limits<unsigned>::max();
  73.     for (unsigned i = 0; i < paths.size(); ++i) {
  74.         if (paths[i].price_ < minPrice) {
  75.             minPrice = paths[i].price_;
  76.             indexOfOptimalPath = i;
  77.         }
  78.     }
  79.     return indexOfOptimalPath;
  80. }
  81.  
  82. bool HeldKarp::CheckIfAllVerticesAreVisited(const std::vector<bool> &visitedVertices) {
  83.     for (const auto isVisited : visitedVertices) {
  84.         if (!isVisited) {
  85.             return false;
  86.         }
  87.     }
  88.     return true;
  89. }
  90.  
  91. void HeldKarp::CreatePermutations(unsigned startVertex) {
  92.     if (graphSize_ > 0) {
  93.         unsigned numberOfElements = 1;
  94.         Permutation firstPermutation(startVertex, 0);
  95.         permutations_[0].push_back(std::move(firstPermutation));
  96.         for (unsigned missingIndex = 0; missingIndex < graphSize_; ++missingIndex) {
  97.             if (missingIndex == startVertex) {
  98.                 continue;
  99.             }
  100.             unsigned numberOfNewElements = 0;
  101.             for (unsigned oldElementsIndex = 0; oldElementsIndex < numberOfElements; ++oldElementsIndex) {
  102.                 Permutation permutation = permutations_[oldElementsIndex];
  103.                 permutation.middleValues_.push_back(missingIndex);
  104.                 permutations_.push_back(std::move(permutation));
  105.                 ++numberOfNewElements;
  106.             }
  107.             numberOfElements += numberOfNewElements;
  108.         }
  109.     }
  110. }
  111.  
  112. double HeldKarp::CalculatePathCorrectly(unsigned startVertex) {
  113.     CreatePermutations(startVertex);
  114.     std::list<unsigned> listOfEndNodes;
  115.     for (unsigned i = 0; i < graphSize_; ++i) {
  116.         if (i != startVertex) {
  117.             listOfEndNodes.push_back(i);
  118.         }
  119.     }
  120.     /*
  121.      * startowy
  122.      * vector pośrednich
  123.      * koszt (nieobliczony)
  124.      */
  125.     for (const auto &permutation : permutations_) {
  126.         for (const auto &node : listOfEndNodes) {
  127.  
  128.         }
  129.     }
  130. }
  131.  
  132. /*
  133.  *
  134.  */
  135. Permutation HeldKarp::FindMinCostOfPermutation(const Permutation &permutation) {
  136.     for(const auto& node: permutation.middleValues_) {
  137.  
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement