diff --git a/src/Wima/OptimisationTools.cc b/src/Wima/OptimisationTools.cc index 65c3a0ebdf84af8e00735413c704a989287bb5d6..1716582e7f9d903317f9def2af62a24ddd52c676 100644 --- a/src/Wima/OptimisationTools.cc +++ b/src/Wima/OptimisationTools.cc @@ -11,7 +11,8 @@ namespace OptimisationTools { || startIndex < 0 || endIndex < 0 || startIndex >= numElements - || endIndex >= numElements) { + || endIndex >= numElements + || endIndex == startIndex) { return false; } // Node struct @@ -107,6 +108,4 @@ namespace OptimisationTools { return true; } - // don't seperate parameters with new lines or documentation will break - } // end OptimisationTools namespace diff --git a/src/Wima/OptimisationTools_delLater.cc b/src/Wima/OptimisationTools_delLater.cc new file mode 100644 index 0000000000000000000000000000000000000000..385fd7ac59ebbefc6d90dc5316928204d949fd5d --- /dev/null +++ b/src/Wima/OptimisationTools_delLater.cc @@ -0,0 +1,90 @@ +#include "OptimisationTools.h" +#include + +namespace OptimisationTools { + namespace { + + } + + bool dijkstraAlgorithm(const int numNodes,const int startIndex,const int endIndex, QVector &elementPath, std::function distance) + { + if ( numNodes < 0 + || startIndex < 0 + || endIndex < 0 + || endIndex >= numNodes + || startIndex >= numNodes + || endIndex == startIndex) { + return false; + } + // distanceToStart[i] contains the distance of element[i] to element[startIndex] (after successful algorithm termination) + QVector distanceToStart(numNodes, std::numeric_limits::infinity()); + // elementPredecessor[i] contains the predecessor of element[i] + QVector elementPredecessor(numNodes, -1); + + // Elements will be successively removed from this list during the execution of the Dijkstra Algorithm. + QVector workingSet; + workingSet.reserve(numNodes); + + //fill workingSet with 0, 1, ..., numNodes-1 + for (int i = 0; i < numNodes; i++) workingSet.append(i); + + distanceToStart[startIndex] = 0; + + // Dijkstra Algorithm + // https://de.wikipedia.org/wiki/Dijkstra-Algorithmus + while (workingSet.size() > 0) { + // serach Node with minimal distance + double minDist = std::numeric_limits::infinity(); + int minIndex = -1; // index of element with least distance to element[startIndex] + for (int i = 0; i < workingSet.size(); i++) { + int e = workingSet[i]; + double dist = distanceToStart[e]; + if (dist < minDist) { + minDist = dist; + minIndex = i; + } + } + if (minIndex == -1) + return false; + + int u = workingSet.takeAt(minIndex); + if (u == endIndex) // shortest path found + break; + + //update distance + double distanceU = distanceToStart[u]; + for (int i = 0; i < workingSet.size(); i++) { + int v = workingSet[i]; + double dist = distance(u, v); + // is ther a alternative path which is shorter? + double alternative = distanceU + dist; + if (alternative < distanceToStart[v]) { + distanceToStart[v] = alternative; + elementPredecessor[v] = u; + } + } + + } + // end Djikstra Algorithm + + + // reverse assemble elementPath + int e = endIndex; + while (1) { + if (e == -1) { + if (elementPath[0] == startIndex)// check if starting point was reached + break; + return false; + } + elementPath.prepend(e); + + //Update Node + e = elementPredecessor[e]; + } + return true; + } + + // end anonymous namespace + + +} // end OptimisationTools namespace