merged
This commit is contained in:
@@ -69,6 +69,7 @@ ADD_DEFINITIONS(
|
|||||||
|
|
||||||
-DWITH_TESTS
|
-DWITH_TESTS
|
||||||
-DWITH_ASSERTIONS
|
-DWITH_ASSERTIONS
|
||||||
|
-DWITH_DEBUG_LOG
|
||||||
-march=native
|
-march=native
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ public:
|
|||||||
gen.seed(1234);
|
gen.seed(1234);
|
||||||
|
|
||||||
// build all shortest path to reach th target
|
// build all shortest path to reach th target
|
||||||
dijkstra.build(target, target, acc);
|
dijkstra.build(&target, acc);
|
||||||
|
|
||||||
// attach a corresponding weight-information to each user-grid-node
|
// attach a corresponding weight-information to each user-grid-node
|
||||||
for (T& node : grid) {
|
for (T& node : grid) {
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public:
|
|||||||
gen.seed(1234);
|
gen.seed(1234);
|
||||||
|
|
||||||
// build all shortest path to reach th target
|
// build all shortest path to reach th target
|
||||||
dijkstra.build(target, target, acc);
|
dijkstra.build(&target, acc);
|
||||||
|
|
||||||
// attach a corresponding weight-information to each user-grid-node
|
// attach a corresponding weight-information to each user-grid-node
|
||||||
for (T& node : grid) {
|
for (T& node : grid) {
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ public:
|
|||||||
gen.seed(1234);
|
gen.seed(1234);
|
||||||
|
|
||||||
// build all shortest path to reach th target
|
// build all shortest path to reach th target
|
||||||
dijkstra.build(target, target, acc);
|
dijkstra.build(&target, acc);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
16
misc/Debug.h
16
misc/Debug.h
@@ -9,12 +9,12 @@
|
|||||||
/** quick and dirty workaround */
|
/** quick and dirty workaround */
|
||||||
static decltype(Time::tick()) LogLastTick;
|
static decltype(Time::tick()) LogLastTick;
|
||||||
|
|
||||||
|
#ifdef WITH_DEBUG_LOG
|
||||||
|
|
||||||
class Log {
|
class Log {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void add(const char* comp, const std::string what, const bool nl = true) {
|
static void add(const char* comp, const std::string what, const bool nl = true) {
|
||||||
addComp(comp);
|
addComp(comp);
|
||||||
std::cout << what;
|
std::cout << what;
|
||||||
@@ -50,4 +50,16 @@ private:
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
class Log {
|
||||||
|
public:
|
||||||
|
static void add(const char* comp, const std::string what, const bool nl = true) { (void)comp; (void) what; (void) nl; }
|
||||||
|
static void add(const std::string& component, const std::string what, const bool nl = true) { (void)component; (void) what; (void) nl; }
|
||||||
|
static void tick() {;}
|
||||||
|
static void tock() {;}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // DEBUG_H
|
#endif // DEBUG_H
|
||||||
|
|||||||
@@ -22,20 +22,34 @@ template <typename T> class Dijkstra {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
/** dtor: cleanup */
|
||||||
|
~Dijkstra() {
|
||||||
|
for (auto it : nodes) {delete it.second;}
|
||||||
|
}
|
||||||
|
|
||||||
/** get the dijkstra-pendant for the given user-node. null if none matches */
|
/** get the dijkstra-pendant for the given user-node. null if none matches */
|
||||||
DijkstraNode<T>* getNode(const T& userNode) const {
|
DijkstraNode<T>* getNode(const T& userNode) const {
|
||||||
auto it = nodes.find(&userNode);
|
auto it = nodes.find(&userNode);
|
||||||
return (unlikely(it == nodes.end())) ? (nullptr) : (it->second);
|
return (unlikely(it == nodes.end())) ? (nullptr) : (it->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** build shortest path from start to end using the provided wrapper-class */
|
/** calculate all shortest paths from ANY node to the given destination */
|
||||||
template <typename Access> void build(const T& start, const T& end, const Access& acc) {
|
template <typename Access> void build(const T* end, const Access& acc) {
|
||||||
|
build(end, nullptr, acc, NAN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* build the shortest path from start to end using the provided access-wrapper-class.
|
||||||
|
* if end is null, the algorithm will terminate only if every possible node was checked.
|
||||||
|
* if given, the algorithm will also terminate if the current distance is already > the given maximum
|
||||||
|
*/
|
||||||
|
template <typename Access> void build(const T* start, const T* end, const Access& acc, const float maxWeight = 0) {
|
||||||
|
|
||||||
// NOTE: end is currently ignored!
|
// NOTE: end is currently ignored!
|
||||||
// runs until all nodes were evaluated
|
// runs until all nodes were evaluated
|
||||||
(void) end;
|
(void) end;
|
||||||
|
|
||||||
Log::add("Dijkstra", "calculating dijkstra from " + (std::string)start + " to ALL OTHER nodes", false);
|
Log::add("Dijkstra", "calculating dijkstra from " + (std::string)*start + " to ALL OTHER nodes", false);
|
||||||
Log::tick();
|
Log::tick();
|
||||||
|
|
||||||
// cleanup previous runs
|
// cleanup previous runs
|
||||||
@@ -45,7 +59,7 @@ public:
|
|||||||
ToProcess toBeProcessedNodes;
|
ToProcess toBeProcessedNodes;
|
||||||
|
|
||||||
// run from start
|
// run from start
|
||||||
const T* cur = &start;
|
const T* cur = start;
|
||||||
|
|
||||||
// create a node for the start element
|
// create a node for the start element
|
||||||
DijkstraNode<T>* dnStart = getOrCreateNode(cur);
|
DijkstraNode<T>* dnStart = getOrCreateNode(cur);
|
||||||
@@ -60,8 +74,11 @@ public:
|
|||||||
// get the next to-be-processed node
|
// get the next to-be-processed node
|
||||||
DijkstraNode<T>* dnSrc = toBeProcessedNodes.pop();
|
DijkstraNode<T>* dnSrc = toBeProcessedNodes.pop();
|
||||||
|
|
||||||
// stop when end was reached??
|
// when an end is given, stop when end was reached
|
||||||
//if (dnSrc->element == &end) {break;}
|
if (end != nullptr && dnSrc->element == end) {break;}
|
||||||
|
|
||||||
|
// when a maximum weight is given, stop when current cum-dist > maxWeight
|
||||||
|
if (maxWeight != 0 && dnSrc->cumWeight > maxWeight) {break;}
|
||||||
|
|
||||||
// visit (and maybe update) each neighbor of the current element
|
// visit (and maybe update) each neighbor of the current element
|
||||||
for (int i = 0; i < acc.getNumNeighbors(*dnSrc->element); ++i) {
|
for (int i = 0; i < acc.getNumNeighbors(*dnSrc->element); ++i) {
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ TEST(TestAll, Nav) {
|
|||||||
const GP& start = g.getNodeFor(GridPoint(500,200,20));
|
const GP& start = g.getNodeFor(GridPoint(500,200,20));
|
||||||
const GP& end = g.getNodeFor(GridPoint(1200,200,340));
|
const GP& end = g.getNodeFor(GridPoint(1200,200,340));
|
||||||
//const GP& end = g.getNodeFor(GridPoint(1300,1300,20));
|
//const GP& end = g.getNodeFor(GridPoint(1300,1300,20));
|
||||||
d.build(start, end, tmp);
|
d.build(&start, &end, tmp);
|
||||||
|
|
||||||
// add the path's importance to the grid
|
// add the path's importance to the grid
|
||||||
gi.addImportance(g, d.getNode(start), d.getNode(end));
|
gi.addImportance(g, d.getNode(start), d.getNode(end));
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ TEST(Dijkstra, build) {
|
|||||||
} tmp(grid);
|
} tmp(grid);
|
||||||
|
|
||||||
Dijkstra<GP> d;
|
Dijkstra<GP> d;
|
||||||
d.build(grid[idx5], grid[idx3], tmp);
|
d.build(&grid[idx5], &grid[idx3], tmp, 99999);
|
||||||
|
|
||||||
// start node must be "idx5"
|
// start node must be "idx5"
|
||||||
DijkstraNode<GP>* n = d.getNode(grid[idx5]);
|
DijkstraNode<GP>* n = d.getNode(grid[idx5]);
|
||||||
@@ -98,7 +98,7 @@ void dijkstra(Grid<GP>& grid) {
|
|||||||
} tmp(grid);
|
} tmp(grid);
|
||||||
|
|
||||||
Dijkstra<GP> d;
|
Dijkstra<GP> d;
|
||||||
d.build(grid[0], grid[0], tmp);
|
d.build(&grid[0], tmp);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user