added new data-structures
added new test-cases added flexible dijkstra calculation added debugging log modified: plotting, grid-generation, grid-importance, refactoring
This commit is contained in:
129
nav/dijkstra/Dijkstra.h
Normal file
129
nav/dijkstra/Dijkstra.h
Normal file
@@ -0,0 +1,129 @@
|
||||
#ifndef DIJKSTRA_H
|
||||
#define DIJKSTRA_H
|
||||
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "DijkstraStructs.h"
|
||||
#include "../../misc/Debug.h"
|
||||
|
||||
#include <KLib/Assertions.h>
|
||||
|
||||
template <typename T> class Dijkstra {
|
||||
|
||||
/** all allocated nodes for the user-data inputs */
|
||||
std::unordered_map<const T*, DijkstraNode<T>*> nodes;
|
||||
|
||||
/** all already processed edges */
|
||||
std::unordered_set<DijkstraEdge<T>> usedEdges;
|
||||
|
||||
/** to-be-processed nodes (USE LINKED LIST INSTEAD?!) */
|
||||
std::vector<DijkstraNode<T>*> toBeProcessedNodes;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
/** get (or create) a new node for the given user-node */
|
||||
DijkstraNode<T>* getNode(const T* userNode) {
|
||||
if (nodes.find(userNode) == nodes.end()) {
|
||||
DijkstraNode<T>* dn = new DijkstraNode<T>(userNode);
|
||||
nodes[userNode] = dn;
|
||||
}
|
||||
return nodes[userNode];
|
||||
}
|
||||
|
||||
/** get the edge (bi-directional) between the two given nodes */
|
||||
DijkstraEdge<T> getEdge(const DijkstraNode<T>* n1, const DijkstraNode<T>* n2) {
|
||||
return DijkstraEdge<T>(n1, n2);
|
||||
}
|
||||
|
||||
/** get the dijkstra-pendant for the given user-node */
|
||||
DijkstraNode<T>* getNode(const T& userNode) {
|
||||
return nodes[&userNode];
|
||||
}
|
||||
|
||||
/** build shortest path from start to end using the provided wrapper-class */
|
||||
template <typename Access> void build(const T& start, const T& end, const Access& acc) {
|
||||
|
||||
// NOTE: end is currently ignored!
|
||||
// runs until all nodes were evaluated
|
||||
|
||||
Log::add("Dijkstra", "calculating dijkstra from " + (std::string)start);
|
||||
|
||||
// cleanup
|
||||
toBeProcessedNodes.clear();
|
||||
usedEdges.clear();
|
||||
nodes.clear();
|
||||
|
||||
// run from start
|
||||
const T* cur = &start;
|
||||
|
||||
// create a node for the start element
|
||||
DijkstraNode<T>* dnStart = getNode(cur);
|
||||
dnStart->cumWeight = 0;
|
||||
|
||||
// add this node to the processing list
|
||||
toBeProcessedNodes.push_back(dnStart);
|
||||
|
||||
// until we are done
|
||||
while(!toBeProcessedNodes.empty()) {
|
||||
|
||||
// get the next to-be-processed node
|
||||
DijkstraNode<T>* dnSrc = toBeProcessedNodes[0];
|
||||
|
||||
// and remove him from the list
|
||||
toBeProcessedNodes.erase(toBeProcessedNodes.begin());
|
||||
|
||||
// process each neighbor of the current element
|
||||
for (int i = 0; i < acc.getNumNeighbors(*dnSrc->element); ++i) {
|
||||
|
||||
// get the neighbor itself
|
||||
const T* dst = acc.getNeighbor(*dnSrc->element, i);
|
||||
|
||||
// get the distance-weight to the neighbor
|
||||
const float weight = acc.getWeightBetween(*dnSrc->element, *dst);
|
||||
_assertTrue(weight >= 0, "edge-weight must not be negative!");
|
||||
|
||||
// get-or-create a node for the neighbor
|
||||
DijkstraNode<T>* dnDst = getNode(dst);
|
||||
|
||||
// get-or-create the edge describing the connection
|
||||
DijkstraEdge<T> edge = getEdge(dnSrc, dnDst);
|
||||
|
||||
// was this edge already processed? -> skip it
|
||||
if (usedEdges.find(edge) != usedEdges.end()) {continue;}
|
||||
|
||||
// otherwise: remember it
|
||||
usedEdges.insert(edge);
|
||||
|
||||
// and add the node for later processing
|
||||
toBeProcessedNodes.push_back(dnDst);
|
||||
|
||||
// update the weight to the destination?
|
||||
const float potentialWeight = dnSrc->cumWeight + weight;
|
||||
if (potentialWeight < dnDst->cumWeight) {
|
||||
dnDst->cumWeight = potentialWeight;
|
||||
dnDst->previous = dnSrc;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// sort the nodes by distance-from-start (shortest first)
|
||||
auto comp = [] (const DijkstraNode<T>* n1, const DijkstraNode<T>* n2) {return n1->cumWeight < n2->cumWeight;};
|
||||
std::sort(toBeProcessedNodes.begin(), toBeProcessedNodes.end(), comp);
|
||||
|
||||
}
|
||||
|
||||
Log::add("Dijkstra", "processed " + std::to_string(nodes.size()) + " nodes");
|
||||
|
||||
// cleanup
|
||||
toBeProcessedNodes.clear();
|
||||
usedEdges.clear();
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // DIJKSTRA_H
|
||||
77
nav/dijkstra/DijkstraStructs.h
Normal file
77
nav/dijkstra/DijkstraStructs.h
Normal file
@@ -0,0 +1,77 @@
|
||||
#ifndef DIJKSTRANODE_H
|
||||
#define DIJKSTRANODE_H
|
||||
|
||||
/**
|
||||
* wrapper around a user data structure
|
||||
* adds additional fields needed for dijkstra calculation
|
||||
*/
|
||||
template <typename T> struct DijkstraNode {
|
||||
|
||||
/** pos infinity */
|
||||
static constexpr float INF = +99999999;
|
||||
|
||||
/** the user-element this node describes */
|
||||
const T* element;
|
||||
|
||||
/** the previous dijkstra node (navigation path) */
|
||||
DijkstraNode<T>* previous;
|
||||
|
||||
/** the weight from the start up to this element */
|
||||
float cumWeight;
|
||||
|
||||
|
||||
// /** ctor */
|
||||
// DijkstraNode() : element(nullptr), previous(), cumWeight(INF) {;}
|
||||
|
||||
/** ctor */
|
||||
DijkstraNode(const T* element) : element(element), previous(), cumWeight(INF) {;}
|
||||
|
||||
|
||||
/** equal? (bi-dir) */
|
||||
bool operator == (const DijkstraNode<T>& other) {
|
||||
return element == other.element;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* data structure describing the connection between two nodes
|
||||
* only used to track already processed connections!
|
||||
*/
|
||||
template <typename T> struct DijkstraEdge {
|
||||
|
||||
/** the edge's source */
|
||||
const DijkstraNode<T>* src;
|
||||
|
||||
/** the edge's destination */
|
||||
const DijkstraNode<T>* dst;
|
||||
|
||||
/** ctor */
|
||||
DijkstraEdge(const DijkstraNode<T>* src, const DijkstraNode<T>* dst) : src(src), dst(dst) {;}
|
||||
|
||||
/** equal? (bi-dir) */
|
||||
bool operator == (const DijkstraEdge& other) const {
|
||||
return ((dst == other.dst) && (src == other.src)) ||
|
||||
((src == other.dst) && (dst == other.src));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//template <typename T> struct DijkstraEdgeWeighted : public DijkstraEdge<T> {
|
||||
|
||||
// /** the edge's weight */
|
||||
// float weight;
|
||||
|
||||
// DijkstraEdgeWeighted(const DijkstraNode<T>* src, const DijkstraNode<T>* dst, const float weight) : DijkstraEdge<T>(src,dst), weight(weight) {;}
|
||||
|
||||
//};
|
||||
|
||||
namespace std {
|
||||
template <typename T> struct hash<DijkstraEdge<T>>{
|
||||
size_t operator()(const DijkstraEdge<T>& e) const {
|
||||
return hash<size_t>()( (size_t)e.src^(size_t)e.dst);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // DIJKSTRANODE_H
|
||||
Reference in New Issue
Block a user