From 963a8d2b5f80a111514dc6eaa56d6b637ee783e1 Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Sat, 10 Dec 2016 00:02:43 +0000 Subject: starting astar algorithm --- bin/main | Bin 221936 -> 236616 bytes include/astar.hpp | 17 +++++++++++++++-- include/node.hpp | 14 ++++++++++---- src/astar.cpp | 25 ++++++++++++++++++++++++- src/main.cpp | 24 ------------------------ src/node.cpp | 20 ++++++++++++++++++++ 6 files changed, 69 insertions(+), 31 deletions(-) diff --git a/bin/main b/bin/main index dab6147..d49be03 100755 Binary files a/bin/main and b/bin/main differ diff --git a/include/astar.hpp b/include/astar.hpp index bcff452..a3d2178 100644 --- a/include/astar.hpp +++ b/include/astar.hpp @@ -5,6 +5,7 @@ #include "node.hpp" #include +#include // defines the number of nodes one can go to // we don't need this as we will have the queues @@ -13,10 +14,22 @@ // TODO add constructors and functions to calculate heuristics etc.. class AStar { public: - AStar(); + AStar(int *curr_graph, const unsigned int& width, const unsigned int& height); private: PriorityQueue open_set; - Node current; + std::vector closed_set; + + int *graph; + int graph_width; + int graph_height; + + Node current_node, start_node, end_node; + + void set_start_end(); + + Node get_neighbour(const Node& n_in, const int& neighbour_num); + + void calc_heuristic(Node& n); }; #endif // ASTAR_HPP diff --git a/include/node.hpp b/include/node.hpp index 314fc7c..3b81b1c 100644 --- a/include/node.hpp +++ b/include/node.hpp @@ -3,13 +3,19 @@ #include +class AStar; + class Node { + friend AStar; public: Node(); - void set_f(int i) { - f_score = i; - } + void set_x(int x_in); + void set_y(int y_in); + void set_h_score(int h); + void set_g_score(int g); + + void compute_f(); // overloading operators for ease of use. friend bool operator<(const Node& n1, const Node& n2); @@ -31,7 +37,7 @@ private: int f_score; // path length to get to that node. int g_score; -// heulistic length to destination. +// heuristic length to destination. int h_score; // the x and y coordinates of the node in the grid diff --git a/src/astar.cpp b/src/astar.cpp index b533130..b556d76 100644 --- a/src/astar.cpp +++ b/src/astar.cpp @@ -1,4 +1,27 @@ #include "astar.hpp" -AStar::AStar() { +AStar::AStar(int *curr_graph, const unsigned int& width, const unsigned int& height) : graph(curr_graph), graph_width(width), graph_height(height) { + set_start_end(); + open_set.push(start_node); +} + +void AStar::set_start_end() { + for(unsigned int i = 0; i < graph_width * graph_height; ++i) + if(graph[i] == 3) { + start_node.x = i % graph_width; + start_node.y = i / graph_width; + start_node.g_score = 0; + } else if(graph[i] == 2) { + end_node.x = i % graph_width; + end_node.y = i / graph_width; + end_node.h_score = 0; + } +} + +Node AStar::get_neighbour(const Node& n_in, const int& neighbour_num) { + if(neighbour_num == 0) { + } +} + +void AStar::calc_heuristic(Node& n) { } diff --git a/src/main.cpp b/src/main.cpp index 2e51f2f..494ff43 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,29 +16,5 @@ using namespace std; int main(int argc, char *argv[]) { - PriorityQueue pq; - - Node n, n2, n3, n4, n5, n6; - n.set_f(65); - n2.set_f(2); - n3.set_f(6); - n4.set_f(34); - n5.set_f(75); - n6.set_f(2); - - pq.push(n); - pq.push(n2); - pq.push(n3); - pq.push(n4); - pq.push(n5); - pq.push(n6); - - cout << "First node in queue: " << pq.pop() << endl; - cout << "Second node in queue: " << pq.pop() << endl; - cout << "Third node in queue: " << pq.pop() << endl; - cout << "Fourth node in queue: " << pq.pop() << endl; - cout << "Fifth node in queue: " << pq.pop() << endl; - cout << "Sixth node in queue: " << pq.pop() << endl; - return 0; } diff --git a/src/node.cpp b/src/node.cpp index c10aa1b..9273376 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -3,6 +3,26 @@ Node::Node() : previous_node(NULL), f_score(-1), g_score(0), h_score(-1), x(0), y(0) { } +void Node::set_x(int x_in) { + x = x_in; +} + +void Node::set_y(int y_in) { + y = y_in; +} + +void Node::set_h_score(int h) { + h_score = h; +} + +void Node::set_g_score(int g) { + g_score = g; +} + +void Node::compute_f() { + f_score = g_score + h_score; +} + bool operator<(const Node& n1, const Node& n2) { if(n1.f_score == n2.f_score) return n1.h_score < n2.h_score; -- cgit