-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.h
146 lines (113 loc) · 4.46 KB
/
solver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef MAZES_FOR_PROGRAMMERS_SOLVER_H
#define MAZES_FOR_PROGRAMMERS_SOLVER_H
#include "dijkstra.h"
#include <chrono>
#include <thread>
#include <future>
#include <iostream>
class Solver {
private:
Maze *maze;
bool mazeSolved = false;
std::vector<Node *> solvedPath;
static std::vector<Node *> getLongestPath(unsigned long numberOfNodes, Node *start, std::vector<Node *> endPoints) {
Dijkstra dijkstraSolver;
dijkstraSolver.setNumberOfNodes(numberOfNodes);
std::vector<Node *> longestPath;
std::unordered_map<Node *, std::vector<Node *>> potentialSolvedPaths =
dijkstraSolver.getPath(start, endPoints);
for (auto potentialSolvedPath : potentialSolvedPaths) {
std::vector<Node *> potentialPath = potentialSolvedPath.second;
if (potentialPath.size() > longestPath.size()) {
longestPath = potentialPath;
}
}
return longestPath;
}
std::vector<Node *> getLongestPathFromBatches(std::unordered_map<Node *, std::vector<Node *>> batches) {
std::vector<Node *> longestPath;
int batchSize = 100;
while (!batches.empty()) {
std::vector<std::future<std::vector<Node *>>> futuresSolutions;
int counter = 0;
std::vector<Node *> batchesToRemove;
for (auto batch : batches) {
Node *start = batch.first;
std::vector<Node *> endPoints = batch.second;
batchesToRemove.push_back(start);
futuresSolutions.push_back(
std::async(std::launch::async, &getLongestPath, maze->getMap().size(), start, endPoints));
if (counter++ == batchSize) {
break;
}
}
for (std::size_t i = 0; i != futuresSolutions.size(); ++i) {
std::vector<Node *> potentialLongestPath = futuresSolutions[i].get();
if (potentialLongestPath.size() > longestPath.size()) {
longestPath = potentialLongestPath;
}
}
for (auto batchToRemove : batchesToRemove) {
batches.erase(batchToRemove);
}
}
return longestPath;
}
public:
Solver(Maze *maze) {
this->maze = maze;
}
std::vector<Node *> solve() {
std::vector<Node *> longestPath;
if (this->mazeSolved) {
return this->solvedPath;
}
std::vector<Node *> deadEnds = this->maze->getPotentialEntraceExitNodes();
if (deadEnds.size() < 2) {
//todo make throw exceptions, no dependency on cout
std::cout << "Er....not enough dead ends to solve" << std::endl;
return longestPath;
}
std::cout << "Ensuring solution" << std::endl;
auto start_time = std::chrono::high_resolution_clock::now();
std::unordered_map<Node *, std::vector<Node *>> batches;
/**
* Not quite All Pairs All Paths, but since A->B is the same path as B->A for shortest paths
* This is a good approach to half the amount of paths to be calculated.
*
* Split them into batches of start node -> many end nodes
*/
for (std::size_t i = 0; i < deadEnds.size(); i++) {
Node *start = deadEnds.at(i);
std::vector<Node *> endPoints;
for (std::size_t j = i + 1; j < deadEnds.size(); j++) {
Node *end = deadEnds.at(j);
endPoints.push_back(end);
}
batches.insert({start, endPoints});
}
auto current_time = std::chrono::high_resolution_clock::now();
longestPath = getLongestPathFromBatches(batches);
batches.clear();
this->mazeSolved = true;
current_time = std::chrono::high_resolution_clock::now();
std::cout << "Solution took "
<< std::chrono::duration_cast<std::chrono::duration<float>>(current_time - start_time).count()
<< " seconds and has a distance of " << longestPath.size() << std::endl;
this->solvedPath = longestPath;
return this->solvedPath;
}
Node *getStartNode() {
return this->solve().front();
}
Node *getEndNode() {
return this->solve().back();
}
void setMazeUnsolved() {
this->mazeSolved = false;
}
bool getMazeSolved() {
return this->mazeSolved;
}
};
#endif //MAZES_FOR_PROGRAMMERS_SOLVER_H