-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
110 lines (107 loc) · 3.1 KB
/
main.cpp
File metadata and controls
110 lines (107 loc) · 3.1 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
#include <iostream>
#include <list>
#include <set>
#include "structs.h"
#include "utils.h"
#include <chrono>
class BFS //breadth-first-search
{
public:
Result find_path(Node start, Node goal, Map grid)
{
auto time_now = std::chrono::high_resolution_clock::now();
Result result;
int steps = 0;
start.g = 0;
std::list<Node> OPEN;
OPEN.push_back(start);
std::set<Node> CLOSED;
CLOSED.insert(start);
bool pathfound = false;
while(!OPEN.empty() && !pathfound)
{
Node current = OPEN.front();
OPEN.pop_front();
steps++;
auto neighbors = grid.get_neighbors(current);
for(auto n:neighbors) {
if (CLOSED.find(n) == CLOSED.end())
{
n.g = current.g + 1;
n.parent = &(*CLOSED.find(current));
OPEN.push_back(n);
CLOSED.insert(n);
if(n == goal) {
result.path = reconstruct_path(n);
result.cost = n.g;
pathfound = true;
break;
}
}
}
}
result.steps = steps;
result.nodes_created = CLOSED.size();
result.runtime = (std::chrono::high_resolution_clock::now() - time_now).count()/1e+9;
return result;
}
std::list<Node> reconstruct_path(Node n)
{
std::list<Node> path;
while(n.parent != nullptr)
{
path.push_front(n);
n = *n.parent;
}
path.push_front(n);
return path;
}
};
class AStar
{
public:
Result find_path(Node start, Node goal, Map grid, std::string metrictype="Octile", int connections=8, double hweight=1)
{
//TODO - implement the main cycle of AStar algorithm
return Result();
}
double count_h_value(Node current, Node goal, std::string metrictype="Octile")
{
//TODO - add support of all three metrics
return 0;
}
std::list<Node> reconstruct_path(Node)
{
//TODO - reconstruct path using back pointers
return {};
}
};
int main(int argc, char* argv[]) //argc - argumnet counter, argv - argument values
{
for(int i=0; i<argc; i++)
std::cout<<argv[i]<<"\n";
if(argc<2)
{
std::cout << "Name of the input XML file is not specified."<<std::endl;
return 1;
}
Loader loader;
loader.load_instance(argv[1]);
Result result;
if(loader.algorithm == "BFS")
{
BFS bfs;
result = bfs.find_path(loader.start, loader.goal, loader.grid);
}
else
{
if(loader.algorithm == "Dijkstra")
loader.hweight = 0;
AStar astar;
result = astar.find_path(loader.start, loader.goal, loader.grid, loader.metrictype, loader.connections, loader.hweight);
}
loader.grid.print(result.path);
std::cout<<"Cost: "<<result.cost<<"\nRuntime: "<<result.runtime
<<"\nSteps: "<<result.steps<<"\nNodes created: "<<result.nodes_created<<std::endl;
return 0;
}