PageRenderTime 29ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/GeoRouting/branches/GeoHyperstar/Algorithms.cs

http://georouting-hyperpath.googlecode.com/
C# | 1517 lines | 1225 code | 175 blank | 117 comment | 228 complexity | 168e090418dfb64e08525cc4ec8bff6f MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Diagnostics;
  7. using NetworkLib.Element;
  8. using NetworkLib.FibHeap;
  9. namespace GeoHyperstar.Forms
  10. {
  11. public partial class GeoHyperStar_MainForm
  12. {
  13. #region Dijkstra?????
  14. /// <summary>
  15. /// Dijkstra?????
  16. /// </summary>
  17. /// <param name="o">??</param>
  18. /// <param name="d"></param>
  19. /// <param name="TimeSpan"></param>
  20. /// <returns></returns>
  21. bool Dijkstra(Network WorkingNet, int o, out long TimeSpan)//??directed??????????????????
  22. {
  23. bool success = false;
  24. Stopwatch sw = new Stopwatch();
  25. sw.Start();
  26. try
  27. {
  28. List<Node> Updated = new List<Node>(); //Updated node collection, add the new updated nodes and delete the permanent nodes dynamically
  29. List<Node> ProcessFinished = new List<Node>();//Permanent collection
  30. WorkingNet.AllNodes[o - 1].OptHeuristic = 0;
  31. WorkingNet.AllNodes[o - 1].HasProcessed = true;
  32. Updated.Add(WorkingNet.AllNodes[o - 1]);
  33. double temp;
  34. int flag = 0;
  35. //bool Status = true;
  36. while (true)//go on the loop before every node gets into the permanent collection
  37. {
  38. temp = double.PositiveInfinity;
  39. //??P???
  40. for (int i = 0; i < Updated.Count; i++)
  41. {
  42. if (Updated[i].HasProcessed == false)
  43. {
  44. if (Updated[i].OptHeuristic <= temp)
  45. {
  46. temp = Updated[i].OptHeuristic;
  47. flag = i;
  48. }
  49. }
  50. }
  51. //??updated??????????P??????????????????????????????????????????????????Heuristic?????????????
  52. if (flag == Updated.Count)
  53. {
  54. break;
  55. }
  56. Node tempnode, tempnextnode;
  57. tempnode = Updated[flag];
  58. tempnode.HasProcessed = true;
  59. ProcessFinished.Add(Updated[flag]);//Push into the collection with permanent label
  60. Updated.Remove(Updated[flag]);
  61. for (int j = 0; j < tempnode.OutLinks.Count; j++)
  62. {
  63. //******************************************************************????????????ToID???????FromID
  64. //tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].ToID - 1];
  65. //????????????????????ToID???????????????????????????????tonode????outlink?????????????????tonode?????fromnode
  66. if (tempnode.GID == tempnode.OutLinks[j].ToGID)
  67. tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].From.ID - 1];
  68. else tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].To.ID - 1];
  69. // ********************************************************************
  70. if (tempnextnode.OptHeuristic > tempnode.OptHeuristic + tempnode.OutLinks[j].TravelTime_variable)
  71. {
  72. tempnextnode.OptHeuristic = tempnode.OptHeuristic + tempnode.OutLinks[j].TravelTime_variable;
  73. tempnextnode.NextNodeID = tempnode.GID;
  74. if (tempnextnode.HasProcessed == false)
  75. {
  76. Updated.Add(tempnextnode);
  77. }
  78. }
  79. }
  80. }
  81. success = true;
  82. }
  83. catch { success = false; }
  84. sw.Stop();
  85. TimeSpan = sw.ElapsedMilliseconds;
  86. return success;
  87. }
  88. /// <summary>
  89. /// ??????????????
  90. /// </summary>
  91. /// <param name="o">??</param>
  92. /// <param name="d">??</param>
  93. /// <param name="pathtype">true: link-represented path/ false: node-represented path</param>
  94. /// <param name="Accessible">????????</param>
  95. /// <returns></returns>
  96. public List<int> GetShortestPath(Network WorkingNet, int o, int d, bool pathtype, out bool Accessible)
  97. {
  98. List<int> path_node = new List<int>();
  99. List<int> path_link = new List<int>();
  100. int Next = d;
  101. path_node.Add(Next);
  102. Accessible = true;
  103. while (Next != o)
  104. {
  105. if (WorkingNet.AllNodes[Next - 1].NextNodeID == -1)
  106. {
  107. Accessible = false;
  108. break;
  109. }
  110. else
  111. {
  112. path_link.Add(GetLinkID(WorkingNet, WorkingNet.AllNodes[Next - 1].NextNodeID, Next));
  113. Next = WorkingNet.AllNodes[Next - 1].NextNodeID;
  114. path_node.Add(Next);
  115. }
  116. }
  117. if (!pathtype) return path_node;
  118. else return path_link;
  119. }
  120. public List<int> GetShortestPathForSubNet(Network WorkingNet, int o, int d, bool pathtype, out bool Accessible)
  121. {
  122. List<int> path_node = new List<int>();
  123. List<int> path_link = new List<int>();
  124. int Next = d;
  125. path_node.Add(Next);
  126. Accessible = true;
  127. while (Next != o)
  128. {
  129. if (WorkingNet.AllNodes[Next - 1].NextNodeID == -1)
  130. {
  131. Accessible = false;
  132. break;
  133. }
  134. else
  135. {
  136. path_link.Add(GetLinkIDForSubNet(WorkingNet, WorkingNet.AllNodes[Next - 1].NextNodeID, Next));
  137. Next = WorkingNet.AllNodes[Next - 1].NextNodeID;
  138. path_node.Add(Next);
  139. }
  140. }
  141. if (!pathtype) return path_node;
  142. else return path_link;
  143. }
  144. /// <summary>
  145. /// ????id????id
  146. /// </summary>
  147. /// <param name="fromid">ID of from node</param>
  148. /// <param name="toid">ID of to node</param>
  149. /// <returns></returns>
  150. private int GetLinkID(Network WorkingNet, int fromid, int toid)
  151. {
  152. int linkid = -1;
  153. foreach (Link i in WorkingNet.AllNodes[fromid - 1].OutLinks)
  154. {
  155. foreach (Link j in WorkingNet.AllNodes[toid - 1].InLinks)
  156. {
  157. if (i.ID == j.ID) linkid = i.ID;
  158. }
  159. }
  160. return linkid;
  161. }
  162. private int GetLinkIDForSubNet(Network WorkingNet, int fromid, int toid)
  163. {
  164. int linkid = -1;
  165. foreach (Link i in WorkingNet.AllNodes[fromid - 1].OutLinks)
  166. {
  167. foreach (Link j in WorkingNet.AllNodes[toid - 1].InLinks)
  168. {
  169. if (i.ID == j.ID) linkid = i.SubID;
  170. }
  171. }
  172. return linkid;
  173. }
  174. /// <summary>
  175. /// ???????????????????
  176. /// </summary>
  177. public void Dijkstra_Recover(Network WorkingNet)
  178. {
  179. //???????HasProcessed??
  180. foreach (Node i in WorkingNet.AllNodes)
  181. {
  182. i.HasProcessed = false;
  183. i.OptHeuristic = double.PositiveInfinity;
  184. i.PessHeuristic = double.PositiveInfinity;
  185. i.RegretModifiedHeuristic = double.PositiveInfinity;
  186. i.NextNodeID = -1;
  187. }
  188. }
  189. /// <summary>
  190. /// ??Dijkstra????????????????heuristics?
  191. /// </summary>
  192. #endregion
  193. #region Fibonacci????Dijkstra?????
  194. /// <summary>
  195. /// ??Fibonacci??Dijkstra?????,?????????????????????Heuristics
  196. /// </summary>
  197. /// <param name="o">??</param>
  198. /// <param name="d">??</param>
  199. /// <param name="prior">true??Optimistic Heuristics? false??Pessimistic Heuristics</param>
  200. /// <param name="TimeSpan">??????</param>
  201. /// <returns></returns>
  202. bool GoalDirectedFibDijkstra(Network WorkingNet, int o, int d, bool prior, out long TimeSpan)//??directed??????????????????,piror?ture??optimistic?false?pessimistic
  203. {
  204. bool success = false;
  205. Stopwatch sw = new Stopwatch();
  206. sw.Start();
  207. try
  208. {
  209. FibonacciHeap<Node> Updated = new FibonacciHeap<Node>();//Updated node collection, add the new updated nodes and delete the permanent nodes dynamically
  210. List<Node> ProcessFinished = new List<Node>();//Permanent collection
  211. WorkingNet.AllNodes[o - 1].OptHeuristic = 0;
  212. WorkingNet.AllNodes[o - 1].HasProcessed = true;
  213. Dictionary<Node, FibonacciHeapNode<Node>> FibNodeDict = new Dictionary<Node, FibonacciHeapNode<Node>>();
  214. Node tempnode = WorkingNet.AllNodes[o - 1];
  215. Node tempnextnode;
  216. Updated.insert(new FibonacciHeapNode<Node>(tempnode), 0);
  217. while (tempnode != WorkingNet.AllNodes[d - 1])//go on the loop before every node gets into the permanent collection
  218. {
  219. if (Updated.isEmpty()) break;//???????????P??????????P????????????????????????????
  220. tempnode = Updated.removeMin().getData();
  221. for (int j = 0; j < tempnode.OutLinks.Count; j++)
  222. {
  223. //******************************************************************????????????ToID???????FromID
  224. //tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].ToID - 1];
  225. //????????????????????ToID???????????????????????????????tonode????outlink?????????????????tonode?????fromnode
  226. if (tempnode.GID == tempnode.OutLinks[j].ToGID)
  227. tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].From.ID - 1];
  228. else tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].To.ID - 1];
  229. // ********************************************************************
  230. if (prior == true)
  231. {
  232. if (tempnextnode.OptHeuristic > tempnode.OptHeuristic + tempnode.OutLinks[j].TravelTime_variable) //??????????????
  233. {
  234. tempnextnode.OptHeuristic = tempnode.OptHeuristic + tempnode.OutLinks[j].TravelTime_variable;
  235. tempnextnode.NextNodeID = tempnode.GID;
  236. if (tempnextnode.HasProcessed == false)
  237. {
  238. FibonacciHeapNode<Node> FibNode;
  239. if (!FibNodeDict.ContainsKey(tempnextnode))
  240. {
  241. FibNode = new FibonacciHeapNode<Node>(tempnextnode);
  242. FibNodeDict.Add(tempnextnode, FibNode);
  243. Updated.insert(FibNode, tempnextnode.OptHeuristic);
  244. }
  245. else
  246. {
  247. FibNodeDict.TryGetValue(tempnextnode, out FibNode);
  248. Updated.decreaseKey(FibNode, tempnextnode.OptHeuristic);
  249. }
  250. }
  251. }
  252. }
  253. else
  254. {
  255. if (tempnextnode.PessHeuristic > tempnode.PessHeuristic + tempnode.OutLinks[j].TravelTime_variable + 1 / tempnode.OutLinks[j].Fa) //??????????????
  256. {
  257. tempnextnode.PessHeuristic = tempnode.PessHeuristic + tempnode.OutLinks[j].TravelTime_variable + 1 / tempnode.OutLinks[j].Fa;
  258. tempnextnode.NextNodeID = tempnode.GID;
  259. if (tempnextnode.HasProcessed == false)
  260. {
  261. FibonacciHeapNode<Node> FibNode;
  262. if (!FibNodeDict.ContainsKey(tempnextnode))
  263. {
  264. FibNode = new FibonacciHeapNode<Node>(tempnextnode);
  265. FibNodeDict.Add(tempnextnode, FibNode);
  266. Updated.insert(FibNode, tempnextnode.PessHeuristic);
  267. }
  268. else
  269. {
  270. FibNodeDict.TryGetValue(tempnextnode, out FibNode);
  271. Updated.decreaseKey(FibNode, tempnextnode.PessHeuristic);
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. success = true;
  279. }
  280. catch { success = false; }
  281. sw.Stop();
  282. TimeSpan = sw.ElapsedMilliseconds;
  283. return success;
  284. }
  285. /// <summary>
  286. /// ????Fibonacci??Dijkstra?????????????Heuristics
  287. /// </summary>
  288. /// <param name="o">??</param>
  289. /// <param name="d">??</param>
  290. /// <param name="prior">true if Optimistic heuristic is used, false if Pessimistic heuristic is used</param>
  291. /// <param name="TimeSpan">??????????</param>
  292. /// <returns></returns>
  293. bool FibDijkstra(Network WorkingNet, int o, int d, bool prior, out long TimeSpan)//??directed??????????????????,piror?ture??optimistic?false?pessimistic
  294. {
  295. TimeSpan = -1;
  296. Stopwatch sw = new Stopwatch();
  297. sw.Start();
  298. FibonacciHeap<Node> Updated = new FibonacciHeap<Node>();//Updated node collection, add the new updated nodes and delete the permanent nodes dynamically
  299. List<Node> ProcessFinished = new List<Node>();//Permanent collection
  300. if (prior == true)
  301. WorkingNet.AllNodes[o - 1].OptHeuristic = 0;
  302. else WorkingNet.AllNodes[o - 1].PessHeuristic = 0;
  303. WorkingNet.AllNodes[o - 1].HasProcessed = true;
  304. Dictionary<Node, FibonacciHeapNode<Node>> FibNodeDict = new Dictionary<Node, FibonacciHeapNode<Node>>();
  305. Node tempnode = WorkingNet.AllNodes[o - 1];
  306. Node tempnextnode;
  307. Updated.insert(new FibonacciHeapNode<Node>(tempnode), 0);
  308. int ClosedNodes = 1;
  309. while (ClosedNodes != WorkingNet.AllNodes.Count)//go on the loop before every node gets into the permanent collection
  310. {
  311. if (Updated.isEmpty()) break;//???????????P??????????P????????????????????????????
  312. tempnode = Updated.removeMin().getData();
  313. tempnode.HasProcessed = true;
  314. ClosedNodes++;
  315. for (int j = 0; j < tempnode.OutLinks.Count; j++)
  316. {
  317. //******************************************************************????????????ToID???????FromID
  318. //tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].ToID - 1];
  319. //????????????????????ToID???????????????????????????????tonode????outlink?????????????????tonode?????fromnode
  320. if (tempnode.GID == tempnode.OutLinks[j].ToGID)
  321. tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].From.ID - 1];
  322. else tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].To.ID - 1];
  323. // ********************************************************************
  324. if (prior == true)
  325. {
  326. if (tempnextnode.OptHeuristic > tempnode.OptHeuristic + tempnode.OutLinks[j].TravelTime_variable) //??????????????
  327. {
  328. tempnextnode.OptHeuristic = tempnode.OptHeuristic + tempnode.OutLinks[j].TravelTime_variable;
  329. tempnextnode.NextNodeID = tempnode.GID;
  330. if (tempnextnode.HasProcessed == false)
  331. {
  332. FibonacciHeapNode<Node> FibNode;
  333. if (!FibNodeDict.ContainsKey(tempnextnode))
  334. {
  335. FibNode = new FibonacciHeapNode<Node>(tempnextnode);
  336. FibNodeDict.Add(tempnextnode, FibNode);
  337. Updated.insert(FibNode, tempnextnode.OptHeuristic);
  338. }
  339. else
  340. {
  341. FibNodeDict.TryGetValue(tempnextnode, out FibNode);
  342. Updated.decreaseKey(FibNode, tempnextnode.OptHeuristic);
  343. }
  344. }
  345. }
  346. }
  347. else
  348. {
  349. if (tempnextnode.PessHeuristic > tempnode.PessHeuristic + tempnode.OutLinks[j].TravelTime_variable + 1 / tempnode.OutLinks[j].Fa) //??????????????
  350. {
  351. tempnextnode.PessHeuristic = tempnode.PessHeuristic + tempnode.OutLinks[j].TravelTime_variable + 1 / tempnode.OutLinks[j].Fa;
  352. tempnextnode.NextNodeID = tempnode.GID;
  353. if (tempnextnode.HasProcessed == false)
  354. {
  355. FibonacciHeapNode<Node> FibNode;
  356. if (!FibNodeDict.ContainsKey(tempnextnode))
  357. {
  358. FibNode = new FibonacciHeapNode<Node>(tempnextnode);
  359. FibNodeDict.Add(tempnextnode, FibNode);
  360. Updated.insert(FibNode, tempnextnode.PessHeuristic);
  361. }
  362. else
  363. {
  364. FibNodeDict.TryGetValue(tempnextnode, out FibNode);
  365. Updated.decreaseKey(FibNode, tempnextnode.PessHeuristic);
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. sw.Stop();
  373. TimeSpan = sw.ElapsedMilliseconds;
  374. return true;
  375. }
  376. bool FibDijkstraBackward(Network WorkingNet, int o, int d, bool prior, out long TimeSpan)//??directed??????????????????,piror?ture??optimistic?false?pessimistic
  377. {
  378. TimeSpan = -1;
  379. Stopwatch sw = new Stopwatch();
  380. sw.Start();
  381. FibonacciHeap<Node> Updated = new FibonacciHeap<Node>();//Updated node collection, add the new updated nodes and delete the permanent nodes dynamically
  382. List<Node> ProcessFinished = new List<Node>();//Permanent collection
  383. if (prior == true)
  384. WorkingNet.AllNodes[o - 1].OptHeuristic = 0;
  385. else WorkingNet.AllNodes[o - 1].PessHeuristic = 0;
  386. WorkingNet.AllNodes[o - 1].HasProcessed = true;
  387. Dictionary<Node, FibonacciHeapNode<Node>> FibNodeDict = new Dictionary<Node, FibonacciHeapNode<Node>>();
  388. Node tempnode = WorkingNet.AllNodes[o - 1];
  389. Node tempnextnode;
  390. Updated.insert(new FibonacciHeapNode<Node>(tempnode), 0);
  391. int ClosedNodes = 1;
  392. while (ClosedNodes != WorkingNet.AllNodes.Count)//go on the loop before every node gets into the permanent collection
  393. {
  394. if (Updated.isEmpty()) break;//???????????P??????????P????????????????????????????
  395. tempnode = Updated.removeMin().getData();
  396. tempnode.HasProcessed = true;
  397. ClosedNodes++;
  398. for (int j = 0; j < tempnode.InLinks.Count; j++)
  399. {
  400. //******************************************************************????????????ToID???????FromID
  401. //tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].ToID - 1];
  402. //????????????????????ToID???????????????????????????????tonode????outlink?????????????????tonode?????fromnode
  403. if (tempnode.GID == tempnode.InLinks[j].ToGID)
  404. tempnextnode = WorkingNet.AllNodes[tempnode.InLinks[j].From.ID - 1];
  405. else tempnextnode = WorkingNet.AllNodes[tempnode.InLinks[j].To.ID - 1];
  406. // ********************************************************************
  407. if (prior == true)
  408. {
  409. if (tempnextnode.OptHeuristic > tempnode.OptHeuristic + tempnode.InLinks[j].TravelTime_variable) //??????????????
  410. {
  411. tempnextnode.OptHeuristic = tempnode.OptHeuristic + tempnode.InLinks[j].TravelTime_variable;
  412. tempnextnode.NextNodeID = tempnode.GID;
  413. if (tempnextnode.HasProcessed == false)
  414. {
  415. FibonacciHeapNode<Node> FibNode;
  416. if (!FibNodeDict.ContainsKey(tempnextnode))
  417. {
  418. FibNode = new FibonacciHeapNode<Node>(tempnextnode);
  419. FibNodeDict.Add(tempnextnode, FibNode);
  420. Updated.insert(FibNode, tempnextnode.OptHeuristic);
  421. }
  422. else
  423. {
  424. FibNodeDict.TryGetValue(tempnextnode, out FibNode);
  425. Updated.decreaseKey(FibNode, tempnextnode.OptHeuristic);
  426. }
  427. }
  428. }
  429. }
  430. else
  431. {
  432. if (tempnextnode.PessHeuristic > tempnode.PessHeuristic + tempnode.InLinks[j].TravelTime_variable + 1 / tempnode.InLinks[j].Fa) //??????????????
  433. {
  434. tempnextnode.PessHeuristic = tempnode.PessHeuristic + tempnode.InLinks[j].TravelTime_variable + 1 / tempnode.InLinks[j].Fa;
  435. tempnextnode.NextNodeID = tempnode.GID;
  436. if (tempnextnode.HasProcessed == false)
  437. {
  438. FibonacciHeapNode<Node> FibNode;
  439. if (!FibNodeDict.ContainsKey(tempnextnode))
  440. {
  441. FibNode = new FibonacciHeapNode<Node>(tempnextnode);
  442. FibNodeDict.Add(tempnextnode, FibNode);
  443. Updated.insert(FibNode, tempnextnode.PessHeuristic);
  444. }
  445. else
  446. {
  447. FibNodeDict.TryGetValue(tempnextnode, out FibNode);
  448. Updated.decreaseKey(FibNode, tempnextnode.PessHeuristic);
  449. }
  450. }
  451. }
  452. }
  453. }
  454. }
  455. sw.Stop();
  456. TimeSpan = sw.ElapsedMilliseconds;
  457. return true;
  458. }
  459. bool FibDijkstraBackwardForSubNet(Network WorkingNet, int o, int d, bool prior, out long TimeSpan)//??directed??????????????????,piror?ture??optimistic?false?pessimistic
  460. {
  461. TimeSpan = -1;
  462. Stopwatch sw = new Stopwatch();
  463. sw.Start();
  464. FibonacciHeap<Node> Updated = new FibonacciHeap<Node>();//Updated node collection, add the new updated nodes and delete the permanent nodes dynamically
  465. List<Node> ProcessFinished = new List<Node>();//Permanent collection
  466. if (prior == true)
  467. WorkingNet.AllNodes[o - 1].OptHeuristic = 0;
  468. else WorkingNet.AllNodes[o - 1].PessHeuristic = 0;
  469. WorkingNet.AllNodes[o - 1].HasProcessed = true;
  470. Dictionary<Node, FibonacciHeapNode<Node>> FibNodeDict = new Dictionary<Node, FibonacciHeapNode<Node>>();
  471. Node tempnode = WorkingNet.AllNodes[o - 1];
  472. Node tempnextnode;
  473. Updated.insert(new FibonacciHeapNode<Node>(tempnode), 0);
  474. int ClosedNodes = 1;
  475. while (ClosedNodes != WorkingNet.AllNodes.Count)//go on the loop before every node gets into the permanent collection
  476. {
  477. if (Updated.isEmpty()) break;//???????????P??????????P????????????????????????????
  478. tempnode = Updated.removeMin().getData();
  479. tempnode.HasProcessed = true;
  480. ClosedNodes++;
  481. for (int j = 0; j < tempnode.InLinks.Count; j++)
  482. {
  483. //******************************************************************????????????ToID???????FromID
  484. //tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].ToID - 1];
  485. //????????????????????ToID???????????????????????????????tonode????outlink?????????????????tonode?????fromnode
  486. if (tempnode.GID == tempnode.InLinks[j].ToGID)
  487. tempnextnode = WorkingNet.AllNodes[tempnode.InLinks[j].From.SubID - 1];
  488. else tempnextnode = WorkingNet.AllNodes[tempnode.InLinks[j].To.SubID - 1];
  489. // ********************************************************************
  490. if (prior == true)
  491. {
  492. if (tempnextnode.OptHeuristic > tempnode.OptHeuristic + tempnode.InLinks[j].TravelTime_variable) //??????????????
  493. {
  494. tempnextnode.OptHeuristic = tempnode.OptHeuristic + tempnode.InLinks[j].TravelTime_variable;
  495. tempnextnode.NextNodeID = tempnode.SubID;
  496. if (tempnextnode.HasProcessed == false)
  497. {
  498. FibonacciHeapNode<Node> FibNode;
  499. if (!FibNodeDict.ContainsKey(tempnextnode))
  500. {
  501. FibNode = new FibonacciHeapNode<Node>(tempnextnode);
  502. FibNodeDict.Add(tempnextnode, FibNode);
  503. Updated.insert(FibNode, tempnextnode.OptHeuristic);
  504. }
  505. else
  506. {
  507. FibNodeDict.TryGetValue(tempnextnode, out FibNode);
  508. Updated.decreaseKey(FibNode, tempnextnode.OptHeuristic);
  509. }
  510. }
  511. }
  512. }
  513. else
  514. {
  515. if (tempnextnode.PessHeuristic > tempnode.PessHeuristic + tempnode.InLinks[j].TravelTime_variable + 1 / tempnode.InLinks[j].Fa) //??????????????
  516. {
  517. tempnextnode.PessHeuristic = tempnode.PessHeuristic + tempnode.InLinks[j].TravelTime_variable + 1 / tempnode.InLinks[j].Fa;
  518. tempnextnode.NextNodeID = tempnode.SubID;
  519. if (tempnextnode.HasProcessed == false)
  520. {
  521. FibonacciHeapNode<Node> FibNode;
  522. if (!FibNodeDict.ContainsKey(tempnextnode))
  523. {
  524. FibNode = new FibonacciHeapNode<Node>(tempnextnode);
  525. FibNodeDict.Add(tempnextnode, FibNode);
  526. Updated.insert(FibNode, tempnextnode.PessHeuristic);
  527. }
  528. else
  529. {
  530. FibNodeDict.TryGetValue(tempnextnode, out FibNode);
  531. Updated.decreaseKey(FibNode, tempnextnode.PessHeuristic);
  532. }
  533. }
  534. }
  535. }
  536. }
  537. }
  538. sw.Stop();
  539. TimeSpan = sw.ElapsedMilliseconds;
  540. return true;
  541. }
  542. private void ApplyBackwardPenalty(Network WorkingNet, int o, int d, double BackwardPenalty)
  543. {
  544. foreach (Link i in WorkingNet.AllLinks)
  545. {
  546. //???Backward Link????????BackwardPenalty
  547. if (JudgeBackward(WorkingNet.AllNodes[d - 1], i))
  548. {
  549. i.TravelTime_variable += BackwardPenalty * i.TravelTime_Fixed;
  550. }
  551. }
  552. }
  553. private bool JudgeBackward(Node D, Link a)
  554. {
  555. //?????????????????
  556. if (GetDistance(a.From, D) < GetDistance(a.To, D))
  557. { return true; }
  558. else return false;
  559. }
  560. private double GetDistance(Node A, Node B)
  561. {
  562. return Math.Sqrt(Math.Pow(A.X - B.X, 2) + Math.Pow(A.Y - B.Y, 2));
  563. }
  564. //????Heuristics?????????????Heuristics?????RSA????optimistic?pessimistic heuristics
  565. public void Dijkstra_RecoverForRSA(Network WorkingNet)
  566. {
  567. //???????HasProcessed??
  568. foreach (Node i in WorkingNet.AllNodes)
  569. {
  570. i.HasProcessed = false;
  571. i.NextNodeID = -1;
  572. }
  573. }
  574. #endregion
  575. #region ?????
  576. #region DHS??
  577. /// <summary>
  578. /// DHS ?????????Dijkstra??????????DHS???????????
  579. /// </summary>
  580. /// <param name="o"></param>
  581. /// <param name="d"></param>
  582. /// <param name="TimeSpan_SP"></param>
  583. /// <param name="TimeSpan_DHS"></param>
  584. /// <returns></returns>
  585. bool DHS(Network WorkingNet, int o, int d, ref List<Link> _RawHyperpath, out long TimeSpan_SP, out long TimeSpan_DHS)
  586. {
  587. //??????UiaddCa????????Heap???max???UiaddCa?????
  588. DefinedComparison Compare = new DefinedComparison();
  589. Stopwatch stw = new Stopwatch();
  590. bool success = false;
  591. long timeSP = 0;
  592. stw.Start();
  593. try
  594. {
  595. //??Dijkstra?????????Heuristic
  596. //Dijkstra(WorkingNet, o, out timeSP);
  597. FibDijkstra(WorkingNet, o, d, true, out timeSP);
  598. #region Initialization
  599. double beta = 0.0;
  600. Node tempi = WorkingNet.AllNodes[d - 1], tempj;
  601. Link tempa = new Link();
  602. bool status = true;
  603. List<Node> updatednodes = new List<Node>();
  604. //???????????????????????UiaddCa???????????link?????????????????????????????
  605. List<Link> updatedlinks = new List<Link>();
  606. updatednodes.Add(WorkingNet.AllNodes[d - 1]);
  607. WorkingNet.AllNodes[d - 1].Ui = 0;
  608. WorkingNet.AllNodes[o - 1].Yi = 1.0;
  609. #endregion
  610. #region Updating
  611. while (status)
  612. {
  613. foreach (Link j in tempi.InLinks)
  614. {
  615. Node tempnodej = null;
  616. tempnodej = WorkingNet.AllNodes[j.From.ID - 1];
  617. if (j.UiAddCa > tempi.Ui + j.TravelTime_variable + tempnodej.OptHeuristic)
  618. {
  619. j.UiAddCa = tempi.Ui + j.TravelTime_variable + tempnodej.OptHeuristic;
  620. if (j.Hasbeenremoved != true && updatedlinks.Contains(j) == false)
  621. if (j.InPathsCollection == false && j.HasUpdated == false)
  622. {
  623. updatedlinks.Add(j);
  624. j.HasUpdated = true;
  625. }
  626. }
  627. }
  628. double temp = double.PositiveInfinity;
  629. foreach (Link i in updatedlinks)
  630. {
  631. if (i.UiAddCa < temp)
  632. {
  633. temp = i.UiAddCa;
  634. tempa = i;
  635. }
  636. }
  637. tempi = WorkingNet.AllNodes[tempa.From.ID - 1];
  638. tempj = WorkingNet.AllNodes[tempa.To.ID - 1];
  639. updatedlinks.Remove(tempa);
  640. tempa.Hasbeenremoved = true;
  641. //update node i
  642. if (tempi.Ui >= tempj.Ui + tempa.TravelTime_variable)
  643. {
  644. if ((tempi.Ui >= double.PositiveInfinity) && tempi.Fi == 0)
  645. {
  646. beta = 1.0;
  647. }
  648. else
  649. {
  650. beta = tempi.Ui * tempi.Fi;
  651. }
  652. tempi.Ui = (beta + tempa.Fa * (tempj.Ui + tempa.TravelTime_variable)) / (tempi.Fi + tempa.Fa);
  653. updatednodes.Add(tempi);
  654. tempi.Fi += tempa.Fa;
  655. _RawHyperpath.Add(tempa);
  656. tempa.InPathsCollection = true;
  657. }
  658. if (tempj.Ui + tempa.TravelTime_variable + tempi.OptHeuristic > WorkingNet.AllNodes[o - 1].Ui || updatednodes.Count == 0)
  659. {
  660. status = false;
  661. }
  662. }
  663. #endregion
  664. #region Loading
  665. //Loading step
  666. WorkingNet.AllLinks.Sort(Compare);
  667. foreach (Link i in WorkingNet.AllLinks)
  668. {
  669. if (i.InPathsCollection)
  670. {
  671. i.Pa = (i.Fa / WorkingNet.AllNodes[i.From.ID - 1].Fi) * WorkingNet.AllNodes[i.From.ID - 1].Yi;
  672. WorkingNet.AllNodes[i.To.ID - 1].Yi += i.Pa;
  673. }
  674. }
  675. #endregion
  676. success = true;
  677. }
  678. catch { success = false; }
  679. stw.Stop();
  680. TimeSpan_DHS = (int)stw.ElapsedMilliseconds;
  681. TimeSpan_SP = timeSP;
  682. return success;
  683. }
  684. //????????
  685. #endregion
  686. #region HP Seies????
  687. bool NDHP(Network WorkingNet, int o, int d, ref List<Link> _RawHyperpath, out long TimeSpan_HP)
  688. {
  689. Stopwatch stw = new Stopwatch();
  690. bool success = false;
  691. stw.Start();
  692. try
  693. {
  694. #region Initialization
  695. double beta = 0.0;
  696. Node tempi = WorkingNet.AllNodes[d - 1], tempj;
  697. Link tempa = new Link();
  698. bool status = true;
  699. List<Node> updatednodes = new List<Node>();
  700. List<Link> updatedlinks = new List<Link>();
  701. updatednodes.Add(WorkingNet.AllNodes[d - 1]);
  702. WorkingNet.AllNodes[d - 1].Ui = 0;
  703. WorkingNet.AllNodes[o - 1].Yi = 1.0;
  704. #endregion
  705. #region Updating
  706. while (status)
  707. {
  708. foreach (Link j in tempi.InLinks)
  709. {
  710. Node tempnodej = WorkingNet.AllNodes[j.From.ID - 1];
  711. if (j.UiAddCa > tempi.Ui + j.TravelTime_variable)
  712. {
  713. j.UiAddCa = tempi.Ui + j.TravelTime_variable;
  714. if (j.Hasbeenremoved != true && j.HasUpdated == false)
  715. {
  716. updatedlinks.Add(j);
  717. j.HasUpdated = true;
  718. }
  719. }
  720. }
  721. double temp = double.PositiveInfinity;
  722. Link templink = new Link();
  723. foreach (Link i in updatedlinks)
  724. {
  725. if (i.UiAddCa < temp)
  726. {
  727. temp = i.UiAddCa;
  728. templink = i;
  729. }
  730. }
  731. tempa = templink;
  732. tempi = WorkingNet.AllNodes[tempa.From.ID - 1];
  733. tempj = WorkingNet.AllNodes[tempa.To.ID - 1];
  734. updatedlinks.Remove(templink);
  735. templink.Hasbeenremoved = true;
  736. if (tempi.Ui >= tempj.Ui + tempa.TravelTime_variable)
  737. {
  738. if ((tempi.Ui >= double.PositiveInfinity) && tempi.Fi == 0)
  739. {
  740. beta = 1.0;
  741. }
  742. else
  743. {
  744. beta = tempi.Ui * tempi.Fi;
  745. }
  746. tempi.Ui = (beta + tempa.Fa * (tempj.Ui + tempa.TravelTime_variable)) / (tempi.Fi + tempa.Fa);
  747. tempi.Fi += tempa.Fa;
  748. _RawHyperpath.Add(tempa);
  749. tempa.InPathsCollection = true;
  750. }
  751. if (tempj.Ui + tempa.TravelTime_variable > WorkingNet.AllNodes[o - 1].Ui)
  752. {
  753. status = false;
  754. }
  755. }
  756. #endregion
  757. #region Loading
  758. //Loading step
  759. DefinedComparison Compare = new DefinedComparison();
  760. WorkingNet.AllLinks.Sort(Compare);
  761. foreach (Link i in WorkingNet.AllLinks)
  762. {
  763. if (i.InPathsCollection)
  764. {
  765. i.Pa = (i.Fa / WorkingNet.AllNodes[i.From.ID - 1].Fi) * WorkingNet.AllNodes[i.From.ID - 1].Yi;
  766. WorkingNet.AllNodes[i.To.ID - 1].Yi += i.Pa;
  767. }
  768. }
  769. #endregion
  770. stw.Stop();
  771. success = true;
  772. }
  773. catch { success = false; }
  774. TimeSpan_HP = stw.ElapsedMilliseconds;
  775. return success;
  776. }
  777. bool HP(Network WorkingNet, int o, int d, ref List<Link> _RawHyperpath, out long TimeSpan_HP)
  778. {
  779. Stopwatch stw = new Stopwatch();
  780. bool success = false;
  781. _RawHyperpath = new List<Link>();
  782. stw.Start();
  783. try
  784. {
  785. #region Initialization
  786. double beta = 0.0;
  787. Node tempi = WorkingNet.AllNodes[d - 1], tempj;
  788. Link tempa = new Link();
  789. bool status = true;
  790. List<Node> updatednodes = new List<Node>();
  791. List<Link> updatedlinks = new List<Link>(WorkingNet.AllLinks.ToArray());
  792. updatednodes.Add(WorkingNet.AllNodes[d - 1]);
  793. WorkingNet.AllNodes[d - 1].Ui = 0;
  794. WorkingNet.AllNodes[o - 1].Yi = 1.0;
  795. #endregion
  796. #if RecordScanned
  797. StreamWriter tmpsw = new StreamWriter("..\\..\\ScannedLinks.txt");
  798. #endif
  799. #region Updating
  800. while (status)
  801. {
  802. tempa = new Link();
  803. double Minui = double.PositiveInfinity;
  804. //select link a
  805. foreach (Link i in updatedlinks)
  806. {
  807. if (i.UiAddCa > WorkingNet.AllNodes[i.To.ID - 1].Ui + i.TravelTime_variable)
  808. i.UiAddCa = WorkingNet.AllNodes[i.To.ID - 1].Ui + i.TravelTime_variable;
  809. if (Minui >= i.UiAddCa)
  810. {
  811. Minui = i.UiAddCa;
  812. tempa = i;
  813. }
  814. }
  815. #if RecordScanned
  816. tmpsw.WriteLine(tempa.GID);
  817. #endif
  818. updatedlinks.Remove(tempa);
  819. Minui = double.PositiveInfinity;
  820. tempi = WorkingNet.AllNodes[tempa.From.ID - 1];
  821. tempj = WorkingNet.AllNodes[tempa.To.ID - 1];
  822. YPoints_Ui.Add(tempj.Ui);
  823. YPoints_UiAddCa.Add(tempa.UiAddCa);
  824. //update node i
  825. if (tempi.Ui >= tempj.Ui + tempa.TravelTime_variable)
  826. {
  827. if ((tempi.Ui >= double.PositiveInfinity) && tempi.Fi == 0)
  828. {
  829. beta = 1.0;
  830. }
  831. else
  832. {
  833. beta = tempi.Ui * tempi.Fi;
  834. }
  835. tempi.Ui = (beta + tempa.Fa * (tempj.Ui + tempa.TravelTime_variable)) / (tempi.Fi + tempa.Fa);
  836. tempi.Fi += tempa.Fa;
  837. if (!tempa.InPathsCollection)
  838. { _RawHyperpath.Add(tempa); tempa.InPathsCollection = true; }
  839. }
  840. if (tempj.Ui + tempa.TravelTime_variable >= WorkingNet.AllNodes[o - 1].Ui) status = false;
  841. }
  842. #endregion
  843. #region Loading
  844. //Loading step
  845. DefinedComparison Compare = new DefinedComparison();
  846. WorkingNet.AllLinks.Sort(Compare);
  847. foreach (Link i in WorkingNet.AllLinks)
  848. {
  849. if (i.InPathsCollection)
  850. {
  851. i.Pa = (i.Fa / i.From.Fi) * i.From.Yi;
  852. WorkingNet.AllNodes[i.To.ID - 1].Yi += i.Pa;
  853. }
  854. }
  855. #endregion
  856. #if RecordScanned
  857. tmpsw.Close();
  858. #endif
  859. stw.Stop();
  860. success = true;
  861. }
  862. catch { success = false; TimeSpan_HP = -1; }
  863. TimeSpan_HP = stw.ElapsedMilliseconds;
  864. return success;
  865. }
  866. bool HP_heu(Network WorkingNet, int o, int d, ref List<Link> _RawHyperpath, out long TimeSpan_SP, out long TimeSpan_HP)
  867. {
  868. Stopwatch stw = new Stopwatch();
  869. bool success = false;
  870. _RawHyperpath = new List<Link>();
  871. #if RecordScanned
  872. StreamWriter tmpsw = new StreamWriter("..\\..\\ScannedLinks.txt");
  873. #endif
  874. stw.Start();
  875. TimeSpan_SP = -1;
  876. try
  877. {
  878. #region Initialization
  879. double beta = 0.0;
  880. Node tempi = WorkingNet.AllNodes[d - 1], tempj;
  881. Link tempa = new Link();
  882. bool status = true;
  883. List<Link> updatedlinks = new List<Link>(WorkingNet.AllLinks.ToArray());
  884. WorkingNet.AllNodes[d - 1].Ui = 0;
  885. WorkingNet.AllNodes[o - 1].Yi = 1.0;
  886. FibDijkstra(WorkingNet, o, d, true, out TimeSpan_SP);
  887. #endregion
  888. #region Updating
  889. while (status)
  890. {
  891. tempa = new Link();
  892. double Minui = double.PositiveInfinity;
  893. //select link a
  894. foreach (Link i in updatedlinks)
  895. {
  896. if (i.UiAddCa > WorkingNet.AllNodes[i.To.ID - 1].Ui + i.TravelTime_variable + WorkingNet.AllNodes[i.From.ID - 1].OptHeuristic)
  897. i.UiAddCa = WorkingNet.AllNodes[i.To.ID - 1].Ui + i.TravelTime_variable + WorkingNet.AllNodes[i.From.ID - 1].OptHeuristic;
  898. if (Minui >= i.UiAddCa)
  899. {
  900. Minui = i.UiAddCa;
  901. tempa = i;
  902. }
  903. }
  904. #if RecordScanned
  905. tmpsw.WriteLine(tempa.GID);
  906. #endif
  907. updatedlinks.Remove(tempa);
  908. Minui = double.PositiveInfinity;
  909. tempi = WorkingNet.AllNodes[tempa.From.ID - 1];
  910. tempj = WorkingNet.AllNodes[tempa.To.ID - 1];
  911. YPoints_Ui.Add(tempj.Ui);
  912. YPoints_UiAddCa.Add(tempa.UiAddCa);
  913. //update node i
  914. if (tempi.Ui >= tempj.Ui + tempa.TravelTime_variable)
  915. {
  916. if ((tempi.Ui >= double.PositiveInfinity) && tempi.Fi == 0)
  917. {
  918. beta = 1.0;
  919. }
  920. else
  921. {
  922. beta = tempi.Ui * tempi.Fi;
  923. }
  924. tempi.Ui = (beta + tempa.Fa * (tempj.Ui + tempa.TravelTime_variable)) / (tempi.Fi + tempa.Fa);
  925. tempi.Fi += tempa.Fa;
  926. if (!tempa.InPathsCollection)
  927. { _RawHyperpath.Add(tempa); tempa.InPathsCollection = true; }
  928. }
  929. if (tempj.Ui + tempa.TravelTime_variable + tempi.OptHeuristic >= WorkingNet.AllNodes[o - 1].Ui) status = false;
  930. }
  931. #endregion
  932. #region Loading
  933. //Loading step
  934. DefinedComparison Compare = new DefinedComparison();
  935. WorkingNet.AllLinks.Sort(Compare);
  936. foreach (Link i in WorkingNet.AllLinks)
  937. {
  938. if (i.InPathsCollection)
  939. {
  940. i.Pa = (i.Fa / i.From.Fi) * i.From.Yi;
  941. WorkingNet.AllNodes[i.To.ID - 1].Yi += i.Pa;
  942. }
  943. }
  944. #endregion
  945. #if RecordScanned
  946. tmpsw.Close();
  947. #endif
  948. stw.Stop();
  949. success = true;
  950. }
  951. catch { success = false; TimeSpan_HP = -1; }
  952. TimeSpan_HP = stw.ElapsedMilliseconds;
  953. return success;
  954. }
  955. bool HP_twist(Network WorkingNet, int o, int d, ref List<Link> _RawHyperpath, out long TimeSpan_DHS)
  956. {
  957. //??????UiaddCa????????Heap???max???UiaddCa?????
  958. DefinedComparison Compare = new DefinedComparison();
  959. Stopwatch stw = new Stopwatch();
  960. bool success = false;
  961. #if RecordScanned
  962. StreamWriter tmpsw = new StreamWriter("..\\..\\ScannedLinks.txt");
  963. #endif
  964. stw.Start();
  965. try
  966. {
  967. //??Dijkstra?????????Heuristic
  968. //Dijkstra(WorkingNet, o, out timeSP);
  969. //FibDijkstra(WorkingNet, o, d, true, out timeSP);
  970. #region Initialization
  971. double beta = 0.0;
  972. Node tempi = WorkingNet.AllNodes[d - 1], tempj;
  973. Link tempa = new Link();
  974. bool status = true;
  975. List<Node> updatednodes = new List<Node>();
  976. //???????????????????????UiaddCa???????????link?????????????????????????????
  977. List<Link> updatedlinks = new List<Link>();
  978. updatednodes.Add(WorkingNet.AllNodes[d - 1]);
  979. WorkingNet.AllNodes[d - 1].Ui = 0;
  980. WorkingNet.AllNodes[o - 1].Yi = 1.0;
  981. #endregion
  982. #region Updating
  983. while (status)
  984. {
  985. foreach (Link j in tempi.InLinks)
  986. {
  987. Node tempnodej = null;
  988. tempnodej = WorkingNet.AllNodes[j.From.ID - 1];
  989. if (j.UiAddCa > tempi.Ui + j.TravelTime_variable)
  990. {
  991. j.UiAddCa = tempi.Ui + j.TravelTime_variable;
  992. if (j.Hasbeenremoved != true && updatedlinks.Contains(j) == false)
  993. if (j.InPathsCollection == false && j.HasUpdated == false)
  994. {

Large files files are truncated, but you can click here to view the full file