-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathshortest_path_heuristic.cpp
More file actions
156 lines (132 loc) · 4.44 KB
/
shortest_path_heuristic.cpp
File metadata and controls
156 lines (132 loc) · 4.44 KB
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
147
148
149
150
151
152
153
154
155
156
#include <fstream>
#include <iostream>
#include <unordered_set>
#include <boost/functional/hash.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/exterior_property.hpp>
#include <boost/graph/floyd_warshall_shortest.hpp>
#include <boost/program_options.hpp>
#include <yaml-cpp/yaml.h>
#include "timer.hpp"
struct Location {
Location(int x, int y) : x(x), y(y) {}
int x;
int y;
bool operator<(const Location& other) const {
return std::tie(x, y) < std::tie(other.x, other.y);
}
bool operator==(const Location& other) const {
return std::tie(x, y) == std::tie(other.x, other.y);
}
friend std::ostream& operator<<(std::ostream& os, const Location& c) {
return os << "(" << c.x << "," << c.y << ")";
}
};
namespace std {
template <>
struct hash<Location> {
size_t operator()(const Location& s) const {
size_t seed = 0;
boost::hash_combine(seed, s.x);
boost::hash_combine(seed, s.y);
return seed;
}
};
}
#include "shortest_path_heuristic.hpp"
int main(int argc, char* argv[]) {
namespace po = boost::program_options;
// Declare the supported options.
po::options_description desc("Allowed options");
std::string inputFile;
std::string outputFile;
desc.add_options()("help", "produce help message")(
"input,i", po::value<std::string>(&inputFile)->required(),
"input file (YAML)")("output,o",
po::value<std::string>(&outputFile)->required(),
"output file (CSV)");
try {
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << desc << "\n";
return 0;
}
} catch (po::error& e) {
std::cerr << e.what() << std::endl << std::endl;
std::cerr << desc << std::endl;
return 1;
}
YAML::Node config = YAML::LoadFile(inputFile);
std::unordered_set<Location> obstacles;
#if 0
typedef boost::adjacency_list_traits<boost::vecS, boost::vecS, boost::undirectedS > searchGraphTraits_t;
typedef searchGraphTraits_t::vertex_descriptor vertex_t;
typedef searchGraphTraits_t::edge_descriptor edge_t;
struct Vertex
{
};
struct Edge
{
float weight;
};
typedef boost::adjacency_list<
boost::vecS, boost::vecS, boost::undirectedS,
Vertex, Edge>
searchGraph_t;
typedef boost::exterior_vertex_property<searchGraph_t, float> distanceProperty_t;
typedef distanceProperty_t::matrix_type distanceMatrix_t;
typedef distanceProperty_t::matrix_map_type distanceMatrixMap_t;
searchGraph_t searchGraph;
std::map<Location, vertex_t> mapLocToVertex;
#endif
const auto& dim = config["map"]["dimensions"];
int dimx = dim[0].as<int>();
int dimy = dim[1].as<int>();
for (const auto& node : config["map"]["obstacles"]) {
obstacles.insert(Location(node[0].as<int>(), node[1].as<int>()));
}
#if 0
// add vertices
for (int x = 0; x < dimx; ++x) {
for (int y = 0; y < dimy; ++y) {
Location l(x, y);
auto v = boost::add_vertex(searchGraph);
mapLocToVertex[l] = v;
}
}
// add edges
for (int x = 0; x < dimx; ++x) {
for (int y = 0; y < dimy; ++y) {
Location l(x, y);
if (obstacles.find(l) == obstacles.end()) {
Location right(x+1, y);
if (x < dimx - 1 && obstacles.find(right) == obstacles.end()) {
auto e = boost::add_edge(mapLocToVertex[l], mapLocToVertex[right], searchGraph);
searchGraph[e.first].weight = 1;
}
Location below(x, y+1);
if (y < dimy - 1 && obstacles.find(below) == obstacles.end()) {
auto e = boost::add_edge(mapLocToVertex[l], mapLocToVertex[below], searchGraph);
searchGraph[e.first].weight = 1;
}
}
}
}
distanceMatrix_t m_shortestDistance(boost::num_vertices(searchGraph));
distanceMatrixMap_t distanceMap(m_shortestDistance, searchGraph);
boost::floyd_warshall_all_pairs_shortest_paths(searchGraph, distanceMap, boost::weight_map(boost::get(&Edge::weight, searchGraph)));
std::ofstream fileStream(outputFile.c_str());
fileStream << dimx << "," << dimy << std::endl;
for (size_t row = 0; row < dimx * dimy; ++row) {
for (size_t column = 0; column < m_shortestDistance[row].size(); ++column) {
fileStream << m_shortestDistance[row][column] << ",";
}
fileStream << std::endl;
}
#endif
ShortestPathHeuristic h(dimx, dimy, obstacles);
std::cout << h.getValue(Location(0, 0), Location(3, 0)) << std::endl;
return 0;
}