Guest User

Untitled

a guest
Apr 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. //
  2. // Created by brand on 4/11/2018.
  3. //
  4.  
  5. #include "Grid.h"
  6. #include<iostream>
  7. #include<queue>
  8. #include "Grid.h"
  9. #include <iostream>
  10. #include <vector>
  11. #include <string>
  12. #include <list>
  13.  
  14. #include <limits> // for numeric_limits
  15.  
  16. #include <set>
  17. #include <utility> // for pair
  18. #include <algorithm>
  19. #include <iterator>
  20.  
  21. using namespace std;
  22.  
  23. typedef int vertex_t;
  24. typedef double weight_t;
  25. struct neighbor {
  26. vertex_t target;
  27. weight_t weight;
  28. neighbor(vertex_t arg_target, weight_t arg_weight)
  29. : target(arg_target), weight(arg_weight) { }
  30. };
  31.  
  32. Grid::Grid() {
  33.  
  34. const weight_t max_weight = std::numeric_limits<double>::infinity();
  35. this->generate_grid();
  36.  
  37. }
  38.  
  39.  
  40. Grid::Grid(uint8_t width, uint8_t length) {
  41. this->width = width;
  42. this->length = length;
  43. this->generate_grid();
  44. }
  45. void Grid::DijkstraComputePaths(vertex_t source,
  46. const adjacency_list_t &adjacency_list,
  47. std::vector<weight_t> &min_distance,
  48. std::vector<vertex_t> &previous)
  49. {
  50. int n = adjacency_list.size();
  51. min_distance.clear();
  52. min_distance.resize(n, max_weight);
  53. min_distance[source] = 0;
  54. previous.clear();
  55. previous.resize(n, -1);
  56. std::set<std::pair<weight_t, vertex_t> > vertex_queue;
  57. vertex_queue.insert(std::make_pair(min_distance[source], source));
  58.  
  59. while (!vertex_queue.empty())
  60. {
  61. weight_t dist = vertex_queue.begin()->first;
  62. vertex_t u = vertex_queue.begin()->second;
  63. vertex_queue.erase(vertex_queue.begin());
  64.  
  65. // Visit each edge exiting u
  66. const std::vector<neighbor> &neighbors = adjacency_list[u];
  67. for (std::vector<neighbor>::const_iterator neighbor_iter = neighbors.begin();
  68. neighbor_iter != neighbors.end();
  69. neighbor_iter++)
  70. {
  71. vertex_t v = neighbor_iter->target;
  72. weight_t weight = neighbor_iter->weight;
  73. weight_t distance_through_u = dist + weight;
  74. if (distance_through_u < min_distance[v]) {
  75. vertex_queue.erase(std::make_pair(min_distance[v], v));
  76.  
  77. min_distance[v] = distance_through_u;
  78. previous[v] = u;
  79. vertex_queue.insert(std::make_pair(min_distance[v], v));
  80.  
  81. }
  82.  
  83. }
  84. }
  85. }
  86.  
  87. /*
  88. *
  89. * Caller
  90. std::vector<weight_t> min_distance;
  91. std::vector<vertex_t> previous;
  92. DijkstraComputePaths(0, adjacency_list, min_distance, previous);
  93. std::cout << "Distance from 0 to 4: " << min_distance[4] << std::endl;
  94. std::list<vertex_t> path = DijkstraGetShortestPathTo(4, previous);
  95. std::cout << "Path : ";
  96. std::copy(path.begin(), path.end(), std::ostream_iterator<vertex_t>(std::cout, " "));
  97. std::cout << std::endl;
  98.  
  99. return 0;
  100.  
  101. */
  102.  
  103.  
  104.  
  105. void Grid::generate_grid() {
  106. // Define grid and storing in member variable
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. typedef std::vector<std::vector<neighbor> > adjacency_list_t;
  114.  
  115.  
  116.  
  117. std::list<vertex_t> DijkstraGetShortestPathTo(
  118. vertex_t vertex, const std::vector<vertex_t> &previous)
  119. {
  120. std::list<vertex_t> path;
  121. for ( ; vertex != -1; vertex = previous[vertex])
  122. path.push_front(vertex);
  123. return path;
  124. }
  125.  
  126.  
  127.  
  128. // remember to insert edges both ways for an undirected graph
  129. adjacency_list_t adjacency_list(25);
  130. // 0 = a
  131. adjacency_list[0].push_back(neighbor(1, 7));
  132. adjacency_list[0].push_back(neighbor(2, 9));
  133. adjacency_list[0].push_back(neighbor(5, 14));
  134. // 1 = b
  135. adjacency_list[1].push_back(neighbor(0, 7));
  136. adjacency_list[1].push_back(neighbor(2, 10));
  137. adjacency_list[1].push_back(neighbor(3, 15));
  138. // 2 = c
  139. adjacency_list[2].push_back(neighbor(0, 9));
  140. adjacency_list[2].push_back(neighbor(1, 10));
  141. adjacency_list[2].push_back(neighbor(3, 11));
  142. adjacency_list[2].push_back(neighbor(5, 2));
  143. // 3 = d
  144. adjacency_list[3].push_back(neighbor(1, 15));
  145. adjacency_list[3].push_back(neighbor(2, 11));
  146. adjacency_list[3].push_back(neighbor(4, 6));
  147. // 4 = e
  148. adjacency_list[4].push_back(neighbor(3, 6));
  149. adjacency_list[4].push_back(neighbor(5, 9));
  150. // 5 = f
  151. adjacency_list[5].push_back(neighbor(0, 14));
  152. adjacency_list[5].push_back(neighbor(2, 2));
  153. adjacency_list[5].push_back(neighbor(4, 9));
  154.  
  155.  
  156. 1, 2 , 1);
  157. 1, 8 , 1);
  158.  
  159. 2, 3 , 1);
  160. 2, 9 , 1);
  161.  
  162. 3, 4 , 1);
  163. 3, 10 , 1);
  164.  
  165. 4, 5 , 1);
  166. 4, 11 , 1);
  167.  
  168. 5, 6 , 1);
  169. 5, 12 , 1);
  170.  
  171. 6, 7 , 1);
  172. 6, 13 , 1);
  173.  
  174. 7, 14 , 1);
  175.  
  176. 8, 9 , 1);
  177. 8, 15 , 1);
  178. 8, 1 , 1);
  179. 9, 10 , 1);
  180. 9, 16 , 1);
  181. 9, 2 , 1);
  182. 10, 11 , 1);
  183. 10, 17 , 1);
  184. 10, 3 , 1);
  185. 11, 12 , 1);
  186. 11, 18 , 1);
  187. 11, 4 , 1);
  188. 12, 13 , 1);
  189. 12, 19 , 1);
  190. 12, 5 , 1);
  191. 13, 14 , 1);
  192. 13, 20 , 1);
  193. 13, 6 , 1);
  194. 14, 21 , 1);
  195. 14, 7 , 1);
  196. 15, 16 , 1);
  197. 15, 22 , 1);
  198. 15, 8 , 1);
  199. 16, 17 , 1);
  200. 16, 23 , 1);
  201. 16, 9 , 1);
  202. 17, 18 , 1);
  203. 17, 24 , 1);
  204. 17, 10 , 1);
  205. 18, 19 , 1);
  206. 18, 25 , 1);
  207. 18, 11 , 1);
  208. 19, 20 , 1);
  209. 19, 26 , 1);
  210. 19, 12 , 1);
  211. 20, 21 , 1);
  212. 20, 27 , 1);
  213. 20, 13 , 1);
  214. 21, 14 , 1);
  215. 22, 23 , 1);
  216. 22, 29 , 1);
  217. 22, 15 , 1);
  218. 23, 24 , 1);
  219. 23, 30 , 1);
  220. 23, 16 , 1);
  221. 24, 25 , 1);
  222. 24, 31 , 1);
  223. 24, 17 , 1);
  224. 25, 26 , 1);
  225. 25, 32 , 1);
  226. 25, 18 , 1);
  227.  
  228.  
  229. 2, 1 , 1);
  230. 3, 2 , 1);
  231. 4, 3 , 1);
  232. 5, 4 , 1);
  233. 6, 5 , 1);
  234. 9, 8 , 1);
  235. 10, 9 , 1);
  236. 11, 10 , 1);
  237. 12, 11 , 1);
  238. 13, 12 , 1);
  239. 16, 15 , 1);
  240. 17, 16 , 1);
  241. 18, 17 , 1);
  242. 19, 18 , 1);
  243. 20, 19 , 1);
  244. 23, 22 , 1);
  245. 24, 23 , 1);
  246. 25, 24 , 1);
  247.  
  248.  
  249.  
  250. }
  251.  
  252.  
  253.  
  254. }
  255.  
  256.  
  257. /**
  258. *
  259. * @param width used to know which width coordinate
  260. * @param length used to know which length coordinate
  261. */
  262. void Grid::addObstacle(unsigned int width, unsigned int length) {
  263. this->grid[]
  264.  
  265. }
  266.  
  267. vector<int> Grid::pathFinding() {
  268. // generating a path which is returned in a series of ints inside a vector
  269. // pathfinder will avoid obstacles
  270.  
  271. }
Add Comment
Please, Sign In to add comment