OptimisationTools.cc 3.68 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7
#include "OptimisationTools.h"

namespace OptimisationTools {
    namespace  {

    } // end anonymous namespace

Valentin Platzgummer's avatar
Valentin Platzgummer committed
8 9 10 11 12 13
    bool dijkstraAlgorithm(const int numElements, int startIndex, int endIndex, QVector<int> &elementPath, std::function<double (const int, const int)> distanceDij)
    {
        if (    numElements < 0
             || startIndex < 0
             || endIndex < 0
             || startIndex >= numElements
Valentin Platzgummer's avatar
Valentin Platzgummer committed
14 15
             || endIndex >= numElements
             || endIndex == startIndex) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
16 17 18 19 20 21 22 23 24 25 26 27 28
            return false;
        }
        // Node struct
        // predecessorIndex is the index of the predecessor node (nodeList[predecessorIndex])
        // distance is the distance between the node and the start node
        // node number is stored by the position in nodeList
        struct Node{
            int predecessorIndex = -1;
            double distance = std::numeric_limits<qreal>::infinity();
        };

        // The list with all Nodes (elements)
        QVector<Node> nodeList(numElements);
29
        // This list will be initalized with indices referring to the elements of nodeList.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        // Elements will be successively remove during the execution of the Dijkstra Algorithm.
        QVector<int> workingSet(numElements);

        //append elements to node list
        for (int i = 0; i < numElements; ++i) workingSet[i] = i;


        nodeList[startIndex].distance = 0;

//        qDebug() << "nodeList" ;
//        for (auto node : nodeList) {
//            qDebug() << "predecessor: " << node.predecessorIndex;
//            qDebug() << "distance: " << node.distance;
//        }
//        qDebug() << "workingSet";
//        for (auto node : workingSet) {
//            qDebug() << "index: " << node;
//        }

        // Dijkstra Algorithm
        // https://de.wikipedia.org/wiki/Dijkstra-Algorithmus
        while (workingSet.size() > 0) {
            // serach Node with minimal distance
            double minDist = std::numeric_limits<qreal>::infinity();
            int minDistIndex_WS = -1; // WS = workinSet
            for (int i = 0; i < workingSet.size(); ++i) {
                const int nodeIndex = workingSet.at(i);
                const double dist = nodeList.at(nodeIndex).distance;
                if (dist < minDist) {
                    minDist = dist;
                    minDistIndex_WS = i;
                }
            }
            if (minDistIndex_WS == -1)
                    return false;

            int indexU_NL = workingSet.takeAt(minDistIndex_WS); // NL = nodeList
            if (indexU_NL == endIndex) // shortest path found
                break;

            const double distanceU = nodeList.at(indexU_NL).distance;
            //update distance
            for (int i = 0; i < workingSet.size(); ++i) {
                int indexV_NL = workingSet[i]; // NL = nodeList
                Node* v = &nodeList[indexV_NL];
                double dist = distanceDij(indexU_NL, indexV_NL);
                // is ther an alternative path which is shorter?
                double alternative = distanceU + dist;
                if (alternative < v->distance)  {
                    v->distance         = alternative;
                    v->predecessorIndex = indexU_NL;
                }
            }

        }
        // end Djikstra Algorithm


        // reverse assemble path
        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 = nodeList[e].predecessorIndex;

        }
        return true;
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
105
} // end OptimisationTools namespace