Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cmath>
  5. #include <algorithm>
  6.  
  7. struct Location {
  8. double x, y;
  9. double operator[](const Location &other) const {
  10. return sqrt((this->x - other.x) * (this->x - other.x) + (this->y - other.y) * (this->y - other.y));
  11. }
  12. Location operator=(const Location &other) {
  13. this->x = other.x;
  14. this->y = other.y;
  15. return *this;
  16. }
  17. friend std::istream& operator>>(std::istream &is, Location &location) {
  18. is >> location.x >> location.y;
  19. return is;
  20. }
  21. Location(const double &c_x = 0, const double &c_y = 0) {
  22. x = c_x;
  23. y = c_y;
  24. }
  25. };
  26.  
  27. struct Node {
  28. int id;
  29. Location location;
  30. std::vector<int> id_of_neighbours;
  31. std::vector<double> cost_of_neighbours;
  32. Node operator=(const Node &other) {
  33. this->id = other.id;
  34. this->location = other.location;
  35. this->id_of_neighbours = other.id_of_neighbours;
  36. this->cost_of_neighbours = other.cost_of_neighbours;
  37. return *this;
  38. }
  39. friend std::istream& operator>>(std::istream &is, Node &node) {
  40. is >> node.id >> node.location;
  41. int numberOfNeighbours;
  42. is >> numberOfNeighbours;
  43. for (int i = 0; i < numberOfNeighbours; i++) {
  44. int buffer;
  45. is >> buffer;
  46. node.id_of_neighbours.push_back(buffer);
  47. }
  48. for (int i = 0; i < numberOfNeighbours; i++) {
  49. double buffer;
  50. is >> buffer;
  51. node.cost_of_neighbours.push_back(buffer);
  52. }
  53. return is;
  54. }
  55. Node(const int &c_id = 0, const Location &c_location = { 0, 0 }) {
  56. id = c_id;
  57. location = c_location;
  58. }
  59. };
  60.  
  61. struct Pair {
  62. int id;
  63. double cost;
  64. bool operator<(const Pair &other) const {
  65. return this->cost < other.cost;
  66. }
  67. bool operator>(const Pair &other) const {
  68. return this->cost > other.cost;
  69. }
  70. Pair operator=(const Pair &other) {
  71. this->id = other.id;
  72. this->cost = other.cost;
  73. return *this;
  74. }
  75. Pair(const int &c_id, const double &c_cost) {
  76. id = c_id;
  77. cost = c_cost;
  78. }
  79. };
  80.  
  81. template<typename T>
  82. class MinHeap {
  83. private:
  84. std::vector<T> elements;
  85. void siftUp(const int &currentPosition) {
  86. if (currentPosition != 0) {
  87. int father = (currentPosition - 1) / 2;
  88. if (elements[father] > elements[currentPosition]) {
  89. std::swap(elements[father], elements[currentPosition]);
  90. this->siftUp(father);
  91. }
  92. }
  93. }
  94. void siftDown(int &currentPosition) {
  95. int leftSon = currentPosition * 2 + 1, rightSon = currentPosition * 2 + 2;
  96. while ((elements[currentPosition] > elements[leftSon]) || (elements[currentPosition] > elements[rightSon])) {
  97. if (elements[leftSon] < elements[rightSon]) {
  98. std::swap(elements[leftSon], elements[currentPosition]);
  99. currentPosition = leftSon;
  100. leftSon = currentPosition * 2 + 1;
  101. rightSon = currentPosition * 2 + 2;
  102. }
  103. else {
  104. std::swap(elements[rightSon], elements[currentPosition]);
  105. currentPosition = rightSon;
  106. leftSon = currentPosition * 2 + 1;
  107. rightSon = currentPosition * 2 + 2;
  108. }
  109. }
  110. }
  111. public:
  112. void insert(const T &toInsert) {
  113. elements.push_back(toInsert);
  114. this->siftUp(elements.size() - 1);
  115. }
  116. T extractMax() {
  117. T toReturn = elements[0];
  118. std::swap(elements[0], elements[elements.size() - 1]);
  119. elements.erase(elements.begin() + elements.size() - 1);
  120. this->siftDown();
  121. return toReturn;
  122. }
  123. T maxElement() const {
  124. return elements[0];
  125. }
  126. bool empty() const {
  127. return elements.empty();
  128. }
  129. void constructHeap(const std::vector<T> &buildFrom) {
  130. for (auto val : buildFrom) {
  131. this->insert(val);
  132. }
  133. }
  134. void printVector() const {
  135. for (auto val : elements) {
  136. std::cout << val << " ";
  137. }
  138. }
  139. };
  140.  
  141. double heu(const Node &first, const Node &second, const double minCost) {
  142. return first.location[second.location] * minCost;
  143. }
  144.  
  145. void readGraph(std::vector<Node> &graph) {
  146. std::ifstream fin("graph.in");
  147. int numberOfNodes;
  148. fin >> numberOfNodes;
  149. for (int i = 0; i < numberOfNodes; i++){
  150. Node toAdd;
  151. fin >> toAdd;
  152. graph.push_back(toAdd);
  153. }
  154. }
  155.  
  156. double getMinCost(const std::vector<Node> &graph) {
  157. double minimum = INT32_MAX;
  158. for (auto node : graph) {
  159. for (auto val : node.cost_of_neighbours) {
  160. minimum = std::min(minimum, val);
  161. }
  162. }
  163. return minimum;
  164. }
  165.  
  166. bool AStar(const std::vector<Node> graph, const int &start, const int &finish) {
  167. double minCost = getMinCost(graph);
  168. bool *visited = new bool[graph.size()];
  169. for (int i = 0; i < graph.size(); ++i)
  170. visited[i] = false;
  171. visited[start] = true;
  172. int *prev = new int[graph.size()];
  173. for (int i = 0; i < graph.size(); ++i)
  174. prev[i] = i;
  175. double *costUpUntil = new double[graph.size()];
  176. for (int i = 0; i < graph.size(); ++i)
  177. costUpUntil[i] = INT32_MAX;
  178. costUpUntil[start] = 0;
  179. Pair auxilliary(start, heu(graph[start], graph[finish], minCost));
  180. MinHeap<Pair> frontier;
  181. for (frontier.insert(auxilliary); frontier.empty() == false; ) {
  182.  
  183. }
  184. }
  185.  
  186. int main() {
  187. std::vector<Node> graph;
  188. return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement