/GeoRouting/branches/GeoHyperstar/Algorithms.cs
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Forms;
- using System.IO;
- using System.Diagnostics;
-
-
- using NetworkLib.Element;
- using NetworkLib.FibHeap;
-
- namespace GeoHyperstar.Forms
- {
- public partial class GeoHyperStar_MainForm
- {
- #region Dijkstra?????
-
- /// <summary>
- /// Dijkstra?????
- /// </summary>
- /// <param name="o">??</param>
- /// <param name="d"></param>
- /// <param name="TimeSpan"></param>
- /// <returns></returns>
-
- bool Dijkstra(Network WorkingNet, int o, out long TimeSpan)//??directed??????????????????
- {
- bool success = false;
- Stopwatch sw = new Stopwatch();
- sw.Start();
- try
- {
- List<Node> Updated = new List<Node>(); //Updated node collection, add the new updated nodes and delete the permanent nodes dynamically
- List<Node> ProcessFinished = new List<Node>();//Permanent collection
- WorkingNet.AllNodes[o - 1].OptHeuristic = 0;
- WorkingNet.AllNodes[o - 1].HasProcessed = true;
- Updated.Add(WorkingNet.AllNodes[o - 1]);
- double temp;
- int flag = 0;
- //bool Status = true;
- while (true)//go on the loop before every node gets into the permanent collection
- {
- temp = double.PositiveInfinity;
- //??P???
-
- for (int i = 0; i < Updated.Count; i++)
- {
- if (Updated[i].HasProcessed == false)
- {
- if (Updated[i].OptHeuristic <= temp)
- {
- temp = Updated[i].OptHeuristic;
- flag = i;
- }
- }
- }
- //??updated??????????P??????????????????????????????????????????????????Heuristic?????????????
- if (flag == Updated.Count)
- {
- break;
- }
- Node tempnode, tempnextnode;
- tempnode = Updated[flag];
- tempnode.HasProcessed = true;
- ProcessFinished.Add(Updated[flag]);//Push into the collection with permanent label
- Updated.Remove(Updated[flag]);
- for (int j = 0; j < tempnode.OutLinks.Count; j++)
- {
- //******************************************************************????????????ToID???????FromID
- //tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].ToID - 1];
- //????????????????????ToID???????????????????????????????tonode????outlink?????????????????tonode?????fromnode
- if (tempnode.GID == tempnode.OutLinks[j].ToGID)
- tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].From.ID - 1];
- else tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].To.ID - 1];
- // ********************************************************************
- if (tempnextnode.OptHeuristic > tempnode.OptHeuristic + tempnode.OutLinks[j].TravelTime_variable)
- {
- tempnextnode.OptHeuristic = tempnode.OptHeuristic + tempnode.OutLinks[j].TravelTime_variable;
- tempnextnode.NextNodeID = tempnode.GID;
- if (tempnextnode.HasProcessed == false)
- {
- Updated.Add(tempnextnode);
- }
- }
- }
- }
- success = true;
- }
- catch { success = false; }
- sw.Stop();
- TimeSpan = sw.ElapsedMilliseconds;
- return success;
- }
-
- /// <summary>
- /// ??????????????
- /// </summary>
- /// <param name="o">??</param>
- /// <param name="d">??</param>
- /// <param name="pathtype">true: link-represented path/ false: node-represented path</param>
- /// <param name="Accessible">????????</param>
- /// <returns></returns>
- public List<int> GetShortestPath(Network WorkingNet, int o, int d, bool pathtype, out bool Accessible)
- {
- List<int> path_node = new List<int>();
- List<int> path_link = new List<int>();
- int Next = d;
- path_node.Add(Next);
- Accessible = true;
- while (Next != o)
- {
- if (WorkingNet.AllNodes[Next - 1].NextNodeID == -1)
- {
- Accessible = false;
- break;
- }
- else
- {
- path_link.Add(GetLinkID(WorkingNet, WorkingNet.AllNodes[Next - 1].NextNodeID, Next));
- Next = WorkingNet.AllNodes[Next - 1].NextNodeID;
- path_node.Add(Next);
- }
- }
- if (!pathtype) return path_node;
- else return path_link;
- }
- public List<int> GetShortestPathForSubNet(Network WorkingNet, int o, int d, bool pathtype, out bool Accessible)
- {
- List<int> path_node = new List<int>();
- List<int> path_link = new List<int>();
- int Next = d;
- path_node.Add(Next);
- Accessible = true;
- while (Next != o)
- {
- if (WorkingNet.AllNodes[Next - 1].NextNodeID == -1)
- {
- Accessible = false;
- break;
- }
- else
- {
- path_link.Add(GetLinkIDForSubNet(WorkingNet, WorkingNet.AllNodes[Next - 1].NextNodeID, Next));
- Next = WorkingNet.AllNodes[Next - 1].NextNodeID;
- path_node.Add(Next);
- }
- }
- if (!pathtype) return path_node;
- else return path_link;
- }
-
- /// <summary>
- /// ????id????id
- /// </summary>
- /// <param name="fromid">ID of from node</param>
- /// <param name="toid">ID of to node</param>
- /// <returns></returns>
-
- private int GetLinkID(Network WorkingNet, int fromid, int toid)
- {
- int linkid = -1;
- foreach (Link i in WorkingNet.AllNodes[fromid - 1].OutLinks)
- {
- foreach (Link j in WorkingNet.AllNodes[toid - 1].InLinks)
- {
- if (i.ID == j.ID) linkid = i.ID;
- }
- }
- return linkid;
- }
-
- private int GetLinkIDForSubNet(Network WorkingNet, int fromid, int toid)
- {
- int linkid = -1;
- foreach (Link i in WorkingNet.AllNodes[fromid - 1].OutLinks)
- {
- foreach (Link j in WorkingNet.AllNodes[toid - 1].InLinks)
- {
- if (i.ID == j.ID) linkid = i.SubID;
- }
- }
- return linkid;
- }
-
- /// <summary>
- /// ???????????????????
- /// </summary>
-
- public void Dijkstra_Recover(Network WorkingNet)
- {
- //???????HasProcessed??
- foreach (Node i in WorkingNet.AllNodes)
- {
- i.HasProcessed = false;
- i.OptHeuristic = double.PositiveInfinity;
- i.PessHeuristic = double.PositiveInfinity;
- i.RegretModifiedHeuristic = double.PositiveInfinity;
- i.NextNodeID = -1;
- }
- }
-
-
- /// <summary>
- /// ??Dijkstra????????????????heuristics?
- /// </summary>
-
-
- #endregion
-
- #region Fibonacci????Dijkstra?????
- /// <summary>
- /// ??Fibonacci??Dijkstra?????,?????????????????????Heuristics
- /// </summary>
- /// <param name="o">??</param>
- /// <param name="d">??</param>
- /// <param name="prior">true??Optimistic Heuristics? false??Pessimistic Heuristics</param>
- /// <param name="TimeSpan">??????</param>
- /// <returns></returns>
- bool GoalDirectedFibDijkstra(Network WorkingNet, int o, int d, bool prior, out long TimeSpan)//??directed??????????????????,piror?ture??optimistic?false?pessimistic
- {
- bool success = false;
- Stopwatch sw = new Stopwatch();
- sw.Start();
- try
- {
- FibonacciHeap<Node> Updated = new FibonacciHeap<Node>();//Updated node collection, add the new updated nodes and delete the permanent nodes dynamically
- List<Node> ProcessFinished = new List<Node>();//Permanent collection
- WorkingNet.AllNodes[o - 1].OptHeuristic = 0;
- WorkingNet.AllNodes[o - 1].HasProcessed = true;
- Dictionary<Node, FibonacciHeapNode<Node>> FibNodeDict = new Dictionary<Node, FibonacciHeapNode<Node>>();
- Node tempnode = WorkingNet.AllNodes[o - 1];
- Node tempnextnode;
- Updated.insert(new FibonacciHeapNode<Node>(tempnode), 0);
- while (tempnode != WorkingNet.AllNodes[d - 1])//go on the loop before every node gets into the permanent collection
- {
- if (Updated.isEmpty()) break;//???????????P??????????P????????????????????????????
- tempnode = Updated.removeMin().getData();
- for (int j = 0; j < tempnode.OutLinks.Count; j++)
- {
- //******************************************************************????????????ToID???????FromID
- //tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].ToID - 1];
- //????????????????????ToID???????????????????????????????tonode????outlink?????????????????tonode?????fromnode
- if (tempnode.GID == tempnode.OutLinks[j].ToGID)
- tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].From.ID - 1];
- else tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].To.ID - 1];
- // ********************************************************************
- if (prior == true)
- {
- if (tempnextnode.OptHeuristic > tempnode.OptHeuristic + tempnode.OutLinks[j].TravelTime_variable) //??????????????
- {
- tempnextnode.OptHeuristic = tempnode.OptHeuristic + tempnode.OutLinks[j].TravelTime_variable;
- tempnextnode.NextNodeID = tempnode.GID;
- if (tempnextnode.HasProcessed == false)
- {
- FibonacciHeapNode<Node> FibNode;
- if (!FibNodeDict.ContainsKey(tempnextnode))
- {
- FibNode = new FibonacciHeapNode<Node>(tempnextnode);
- FibNodeDict.Add(tempnextnode, FibNode);
- Updated.insert(FibNode, tempnextnode.OptHeuristic);
- }
- else
- {
- FibNodeDict.TryGetValue(tempnextnode, out FibNode);
- Updated.decreaseKey(FibNode, tempnextnode.OptHeuristic);
- }
- }
- }
- }
- else
- {
- if (tempnextnode.PessHeuristic > tempnode.PessHeuristic + tempnode.OutLinks[j].TravelTime_variable + 1 / tempnode.OutLinks[j].Fa) //??????????????
- {
- tempnextnode.PessHeuristic = tempnode.PessHeuristic + tempnode.OutLinks[j].TravelTime_variable + 1 / tempnode.OutLinks[j].Fa;
- tempnextnode.NextNodeID = tempnode.GID;
- if (tempnextnode.HasProcessed == false)
- {
- FibonacciHeapNode<Node> FibNode;
- if (!FibNodeDict.ContainsKey(tempnextnode))
- {
- FibNode = new FibonacciHeapNode<Node>(tempnextnode);
- FibNodeDict.Add(tempnextnode, FibNode);
- Updated.insert(FibNode, tempnextnode.PessHeuristic);
- }
- else
- {
- FibNodeDict.TryGetValue(tempnextnode, out FibNode);
- Updated.decreaseKey(FibNode, tempnextnode.PessHeuristic);
- }
- }
- }
- }
- }
- }
- success = true;
- }
- catch { success = false; }
- sw.Stop();
- TimeSpan = sw.ElapsedMilliseconds;
-
- return success;
-
- }
-
-
- /// <summary>
- /// ????Fibonacci??Dijkstra?????????????Heuristics
- /// </summary>
- /// <param name="o">??</param>
- /// <param name="d">??</param>
- /// <param name="prior">true if Optimistic heuristic is used, false if Pessimistic heuristic is used</param>
- /// <param name="TimeSpan">??????????</param>
- /// <returns></returns>
-
- bool FibDijkstra(Network WorkingNet, int o, int d, bool prior, out long TimeSpan)//??directed??????????????????,piror?ture??optimistic?false?pessimistic
- {
- TimeSpan = -1;
-
- Stopwatch sw = new Stopwatch();
- sw.Start();
-
- FibonacciHeap<Node> Updated = new FibonacciHeap<Node>();//Updated node collection, add the new updated nodes and delete the permanent nodes dynamically
- List<Node> ProcessFinished = new List<Node>();//Permanent collection
- if (prior == true)
- WorkingNet.AllNodes[o - 1].OptHeuristic = 0;
- else WorkingNet.AllNodes[o - 1].PessHeuristic = 0;
- WorkingNet.AllNodes[o - 1].HasProcessed = true;
- Dictionary<Node, FibonacciHeapNode<Node>> FibNodeDict = new Dictionary<Node, FibonacciHeapNode<Node>>();
- Node tempnode = WorkingNet.AllNodes[o - 1];
- Node tempnextnode;
- Updated.insert(new FibonacciHeapNode<Node>(tempnode), 0);
- int ClosedNodes = 1;
- while (ClosedNodes != WorkingNet.AllNodes.Count)//go on the loop before every node gets into the permanent collection
- {
- if (Updated.isEmpty()) break;//???????????P??????????P????????????????????????????
- tempnode = Updated.removeMin().getData();
- tempnode.HasProcessed = true;
- ClosedNodes++;
- for (int j = 0; j < tempnode.OutLinks.Count; j++)
- {
- //******************************************************************????????????ToID???????FromID
- //tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].ToID - 1];
- //????????????????????ToID???????????????????????????????tonode????outlink?????????????????tonode?????fromnode
- if (tempnode.GID == tempnode.OutLinks[j].ToGID)
- tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].From.ID - 1];
- else tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].To.ID - 1];
- // ********************************************************************
- if (prior == true)
- {
- if (tempnextnode.OptHeuristic > tempnode.OptHeuristic + tempnode.OutLinks[j].TravelTime_variable) //??????????????
- {
- tempnextnode.OptHeuristic = tempnode.OptHeuristic + tempnode.OutLinks[j].TravelTime_variable;
- tempnextnode.NextNodeID = tempnode.GID;
- if (tempnextnode.HasProcessed == false)
- {
- FibonacciHeapNode<Node> FibNode;
- if (!FibNodeDict.ContainsKey(tempnextnode))
- {
- FibNode = new FibonacciHeapNode<Node>(tempnextnode);
- FibNodeDict.Add(tempnextnode, FibNode);
- Updated.insert(FibNode, tempnextnode.OptHeuristic);
- }
- else
- {
- FibNodeDict.TryGetValue(tempnextnode, out FibNode);
- Updated.decreaseKey(FibNode, tempnextnode.OptHeuristic);
- }
- }
- }
- }
- else
- {
- if (tempnextnode.PessHeuristic > tempnode.PessHeuristic + tempnode.OutLinks[j].TravelTime_variable + 1 / tempnode.OutLinks[j].Fa) //??????????????
- {
- tempnextnode.PessHeuristic = tempnode.PessHeuristic + tempnode.OutLinks[j].TravelTime_variable + 1 / tempnode.OutLinks[j].Fa;
- tempnextnode.NextNodeID = tempnode.GID;
- if (tempnextnode.HasProcessed == false)
- {
- FibonacciHeapNode<Node> FibNode;
- if (!FibNodeDict.ContainsKey(tempnextnode))
- {
- FibNode = new FibonacciHeapNode<Node>(tempnextnode);
- FibNodeDict.Add(tempnextnode, FibNode);
- Updated.insert(FibNode, tempnextnode.PessHeuristic);
- }
- else
- {
- FibNodeDict.TryGetValue(tempnextnode, out FibNode);
- Updated.decreaseKey(FibNode, tempnextnode.PessHeuristic);
- }
- }
- }
- }
- }
- }
-
-
-
- sw.Stop();
- TimeSpan = sw.ElapsedMilliseconds;
-
- return true;
- }
-
-
-
- bool FibDijkstraBackward(Network WorkingNet, int o, int d, bool prior, out long TimeSpan)//??directed??????????????????,piror?ture??optimistic?false?pessimistic
- {
- TimeSpan = -1;
-
-
- Stopwatch sw = new Stopwatch();
- sw.Start();
-
- FibonacciHeap<Node> Updated = new FibonacciHeap<Node>();//Updated node collection, add the new updated nodes and delete the permanent nodes dynamically
- List<Node> ProcessFinished = new List<Node>();//Permanent collection
- if (prior == true)
- WorkingNet.AllNodes[o - 1].OptHeuristic = 0;
- else WorkingNet.AllNodes[o - 1].PessHeuristic = 0;
- WorkingNet.AllNodes[o - 1].HasProcessed = true;
- Dictionary<Node, FibonacciHeapNode<Node>> FibNodeDict = new Dictionary<Node, FibonacciHeapNode<Node>>();
- Node tempnode = WorkingNet.AllNodes[o - 1];
- Node tempnextnode;
- Updated.insert(new FibonacciHeapNode<Node>(tempnode), 0);
- int ClosedNodes = 1;
- while (ClosedNodes != WorkingNet.AllNodes.Count)//go on the loop before every node gets into the permanent collection
- {
- if (Updated.isEmpty()) break;//???????????P??????????P????????????????????????????
- tempnode = Updated.removeMin().getData();
- tempnode.HasProcessed = true;
- ClosedNodes++;
- for (int j = 0; j < tempnode.InLinks.Count; j++)
- {
- //******************************************************************????????????ToID???????FromID
- //tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].ToID - 1];
- //????????????????????ToID???????????????????????????????tonode????outlink?????????????????tonode?????fromnode
- if (tempnode.GID == tempnode.InLinks[j].ToGID)
- tempnextnode = WorkingNet.AllNodes[tempnode.InLinks[j].From.ID - 1];
- else tempnextnode = WorkingNet.AllNodes[tempnode.InLinks[j].To.ID - 1];
- // ********************************************************************
- if (prior == true)
- {
- if (tempnextnode.OptHeuristic > tempnode.OptHeuristic + tempnode.InLinks[j].TravelTime_variable) //??????????????
- {
- tempnextnode.OptHeuristic = tempnode.OptHeuristic + tempnode.InLinks[j].TravelTime_variable;
- tempnextnode.NextNodeID = tempnode.GID;
- if (tempnextnode.HasProcessed == false)
- {
- FibonacciHeapNode<Node> FibNode;
- if (!FibNodeDict.ContainsKey(tempnextnode))
- {
- FibNode = new FibonacciHeapNode<Node>(tempnextnode);
- FibNodeDict.Add(tempnextnode, FibNode);
- Updated.insert(FibNode, tempnextnode.OptHeuristic);
- }
- else
- {
- FibNodeDict.TryGetValue(tempnextnode, out FibNode);
- Updated.decreaseKey(FibNode, tempnextnode.OptHeuristic);
- }
- }
- }
- }
- else
- {
- if (tempnextnode.PessHeuristic > tempnode.PessHeuristic + tempnode.InLinks[j].TravelTime_variable + 1 / tempnode.InLinks[j].Fa) //??????????????
- {
- tempnextnode.PessHeuristic = tempnode.PessHeuristic + tempnode.InLinks[j].TravelTime_variable + 1 / tempnode.InLinks[j].Fa;
- tempnextnode.NextNodeID = tempnode.GID;
- if (tempnextnode.HasProcessed == false)
- {
- FibonacciHeapNode<Node> FibNode;
- if (!FibNodeDict.ContainsKey(tempnextnode))
- {
- FibNode = new FibonacciHeapNode<Node>(tempnextnode);
- FibNodeDict.Add(tempnextnode, FibNode);
- Updated.insert(FibNode, tempnextnode.PessHeuristic);
- }
- else
- {
- FibNodeDict.TryGetValue(tempnextnode, out FibNode);
- Updated.decreaseKey(FibNode, tempnextnode.PessHeuristic);
- }
- }
- }
- }
- }
- }
-
-
-
- sw.Stop();
- TimeSpan = sw.ElapsedMilliseconds;
-
- return true;
- }
-
- bool FibDijkstraBackwardForSubNet(Network WorkingNet, int o, int d, bool prior, out long TimeSpan)//??directed??????????????????,piror?ture??optimistic?false?pessimistic
- {
- TimeSpan = -1;
-
-
- Stopwatch sw = new Stopwatch();
- sw.Start();
-
- FibonacciHeap<Node> Updated = new FibonacciHeap<Node>();//Updated node collection, add the new updated nodes and delete the permanent nodes dynamically
- List<Node> ProcessFinished = new List<Node>();//Permanent collection
- if (prior == true)
- WorkingNet.AllNodes[o - 1].OptHeuristic = 0;
- else WorkingNet.AllNodes[o - 1].PessHeuristic = 0;
- WorkingNet.AllNodes[o - 1].HasProcessed = true;
- Dictionary<Node, FibonacciHeapNode<Node>> FibNodeDict = new Dictionary<Node, FibonacciHeapNode<Node>>();
- Node tempnode = WorkingNet.AllNodes[o - 1];
- Node tempnextnode;
- Updated.insert(new FibonacciHeapNode<Node>(tempnode), 0);
- int ClosedNodes = 1;
- while (ClosedNodes != WorkingNet.AllNodes.Count)//go on the loop before every node gets into the permanent collection
- {
- if (Updated.isEmpty()) break;//???????????P??????????P????????????????????????????
- tempnode = Updated.removeMin().getData();
- tempnode.HasProcessed = true;
- ClosedNodes++;
- for (int j = 0; j < tempnode.InLinks.Count; j++)
- {
- //******************************************************************????????????ToID???????FromID
- //tempnextnode = WorkingNet.AllNodes[tempnode.OutLinks[j].ToID - 1];
- //????????????????????ToID???????????????????????????????tonode????outlink?????????????????tonode?????fromnode
- if (tempnode.GID == tempnode.InLinks[j].ToGID)
- tempnextnode = WorkingNet.AllNodes[tempnode.InLinks[j].From.SubID - 1];
- else tempnextnode = WorkingNet.AllNodes[tempnode.InLinks[j].To.SubID - 1];
- // ********************************************************************
- if (prior == true)
- {
- if (tempnextnode.OptHeuristic > tempnode.OptHeuristic + tempnode.InLinks[j].TravelTime_variable) //??????????????
- {
- tempnextnode.OptHeuristic = tempnode.OptHeuristic + tempnode.InLinks[j].TravelTime_variable;
- tempnextnode.NextNodeID = tempnode.SubID;
- if (tempnextnode.HasProcessed == false)
- {
- FibonacciHeapNode<Node> FibNode;
- if (!FibNodeDict.ContainsKey(tempnextnode))
- {
- FibNode = new FibonacciHeapNode<Node>(tempnextnode);
- FibNodeDict.Add(tempnextnode, FibNode);
- Updated.insert(FibNode, tempnextnode.OptHeuristic);
- }
- else
- {
- FibNodeDict.TryGetValue(tempnextnode, out FibNode);
- Updated.decreaseKey(FibNode, tempnextnode.OptHeuristic);
- }
- }
- }
- }
- else
- {
- if (tempnextnode.PessHeuristic > tempnode.PessHeuristic + tempnode.InLinks[j].TravelTime_variable + 1 / tempnode.InLinks[j].Fa) //??????????????
- {
- tempnextnode.PessHeuristic = tempnode.PessHeuristic + tempnode.InLinks[j].TravelTime_variable + 1 / tempnode.InLinks[j].Fa;
- tempnextnode.NextNodeID = tempnode.SubID;
- if (tempnextnode.HasProcessed == false)
- {
- FibonacciHeapNode<Node> FibNode;
- if (!FibNodeDict.ContainsKey(tempnextnode))
- {
- FibNode = new FibonacciHeapNode<Node>(tempnextnode);
- FibNodeDict.Add(tempnextnode, FibNode);
- Updated.insert(FibNode, tempnextnode.PessHeuristic);
- }
- else
- {
- FibNodeDict.TryGetValue(tempnextnode, out FibNode);
- Updated.decreaseKey(FibNode, tempnextnode.PessHeuristic);
- }
- }
- }
- }
- }
- }
-
-
-
- sw.Stop();
- TimeSpan = sw.ElapsedMilliseconds;
-
- return true;
- }
-
- private void ApplyBackwardPenalty(Network WorkingNet, int o, int d, double BackwardPenalty)
- {
- foreach (Link i in WorkingNet.AllLinks)
- {
- //???Backward Link????????BackwardPenalty
- if (JudgeBackward(WorkingNet.AllNodes[d - 1], i))
- {
- i.TravelTime_variable += BackwardPenalty * i.TravelTime_Fixed;
- }
- }
- }
- private bool JudgeBackward(Node D, Link a)
- {
- //?????????????????
- if (GetDistance(a.From, D) < GetDistance(a.To, D))
- { return true; }
- else return false;
- }
- private double GetDistance(Node A, Node B)
- {
- return Math.Sqrt(Math.Pow(A.X - B.X, 2) + Math.Pow(A.Y - B.Y, 2));
- }
-
- //????Heuristics?????????????Heuristics?????RSA????optimistic?pessimistic heuristics
- public void Dijkstra_RecoverForRSA(Network WorkingNet)
- {
- //???????HasProcessed??
- foreach (Node i in WorkingNet.AllNodes)
- {
- i.HasProcessed = false;
- i.NextNodeID = -1;
- }
- }
- #endregion
-
- #region ?????
-
- #region DHS??
- /// <summary>
- /// DHS ?????????Dijkstra??????????DHS???????????
- /// </summary>
- /// <param name="o"></param>
- /// <param name="d"></param>
- /// <param name="TimeSpan_SP"></param>
- /// <param name="TimeSpan_DHS"></param>
- /// <returns></returns>
- bool DHS(Network WorkingNet, int o, int d, ref List<Link> _RawHyperpath, out long TimeSpan_SP, out long TimeSpan_DHS)
- {
- //??????UiaddCa????????Heap???max???UiaddCa?????
- DefinedComparison Compare = new DefinedComparison();
-
- Stopwatch stw = new Stopwatch();
- bool success = false;
- long timeSP = 0;
- stw.Start();
- try
- {
- //??Dijkstra?????????Heuristic
- //Dijkstra(WorkingNet, o, out timeSP);
- FibDijkstra(WorkingNet, o, d, true, out timeSP);
- #region Initialization
- double beta = 0.0;
- Node tempi = WorkingNet.AllNodes[d - 1], tempj;
- Link tempa = new Link();
- bool status = true;
- List<Node> updatednodes = new List<Node>();
- //???????????????????????UiaddCa???????????link?????????????????????????????
- List<Link> updatedlinks = new List<Link>();
- updatednodes.Add(WorkingNet.AllNodes[d - 1]);
- WorkingNet.AllNodes[d - 1].Ui = 0;
- WorkingNet.AllNodes[o - 1].Yi = 1.0;
- #endregion
-
- #region Updating
- while (status)
- {
-
- foreach (Link j in tempi.InLinks)
- {
- Node tempnodej = null;
- tempnodej = WorkingNet.AllNodes[j.From.ID - 1];
- if (j.UiAddCa > tempi.Ui + j.TravelTime_variable + tempnodej.OptHeuristic)
- {
- j.UiAddCa = tempi.Ui + j.TravelTime_variable + tempnodej.OptHeuristic;
- if (j.Hasbeenremoved != true && updatedlinks.Contains(j) == false)
- if (j.InPathsCollection == false && j.HasUpdated == false)
- {
- updatedlinks.Add(j);
- j.HasUpdated = true;
- }
- }
- }
- double temp = double.PositiveInfinity;
- foreach (Link i in updatedlinks)
- {
- if (i.UiAddCa < temp)
- {
- temp = i.UiAddCa;
- tempa = i;
- }
- }
-
- tempi = WorkingNet.AllNodes[tempa.From.ID - 1];
- tempj = WorkingNet.AllNodes[tempa.To.ID - 1];
- updatedlinks.Remove(tempa);
- tempa.Hasbeenremoved = true;
-
-
- //update node i
-
- if (tempi.Ui >= tempj.Ui + tempa.TravelTime_variable)
- {
- if ((tempi.Ui >= double.PositiveInfinity) && tempi.Fi == 0)
- {
- beta = 1.0;
- }
- else
- {
- beta = tempi.Ui * tempi.Fi;
- }
- tempi.Ui = (beta + tempa.Fa * (tempj.Ui + tempa.TravelTime_variable)) / (tempi.Fi + tempa.Fa);
-
- updatednodes.Add(tempi);
-
- tempi.Fi += tempa.Fa;
-
- _RawHyperpath.Add(tempa);
-
- tempa.InPathsCollection = true;
- }
-
- if (tempj.Ui + tempa.TravelTime_variable + tempi.OptHeuristic > WorkingNet.AllNodes[o - 1].Ui || updatednodes.Count == 0)
- {
- status = false;
- }
- }
-
- #endregion
-
- #region Loading
- //Loading step
-
- WorkingNet.AllLinks.Sort(Compare);
-
- foreach (Link i in WorkingNet.AllLinks)
- {
- if (i.InPathsCollection)
- {
- i.Pa = (i.Fa / WorkingNet.AllNodes[i.From.ID - 1].Fi) * WorkingNet.AllNodes[i.From.ID - 1].Yi;
-
- WorkingNet.AllNodes[i.To.ID - 1].Yi += i.Pa;
- }
-
- }
- #endregion
-
- success = true;
- }
- catch { success = false; }
- stw.Stop();
- TimeSpan_DHS = (int)stw.ElapsedMilliseconds;
- TimeSpan_SP = timeSP;
- return success;
- }
- //????????
- #endregion
-
- #region HP Seies????
-
- bool NDHP(Network WorkingNet, int o, int d, ref List<Link> _RawHyperpath, out long TimeSpan_HP)
- {
- Stopwatch stw = new Stopwatch();
- bool success = false;
-
- stw.Start();
- try
- {
- #region Initialization
- double beta = 0.0;
- Node tempi = WorkingNet.AllNodes[d - 1], tempj;
- Link tempa = new Link();
- bool status = true;
- List<Node> updatednodes = new List<Node>();
- List<Link> updatedlinks = new List<Link>();
- updatednodes.Add(WorkingNet.AllNodes[d - 1]);
- WorkingNet.AllNodes[d - 1].Ui = 0;
- WorkingNet.AllNodes[o - 1].Yi = 1.0;
-
- #endregion
-
- #region Updating
- while (status)
- {
- foreach (Link j in tempi.InLinks)
- {
- Node tempnodej = WorkingNet.AllNodes[j.From.ID - 1];
- if (j.UiAddCa > tempi.Ui + j.TravelTime_variable)
- {
- j.UiAddCa = tempi.Ui + j.TravelTime_variable;
- if (j.Hasbeenremoved != true && j.HasUpdated == false)
- {
- updatedlinks.Add(j);
- j.HasUpdated = true;
- }
- }
- }
- double temp = double.PositiveInfinity;
- Link templink = new Link();
-
- foreach (Link i in updatedlinks)
- {
- if (i.UiAddCa < temp)
- {
- temp = i.UiAddCa;
- templink = i;
- }
- }
- tempa = templink;
- tempi = WorkingNet.AllNodes[tempa.From.ID - 1];
- tempj = WorkingNet.AllNodes[tempa.To.ID - 1];
- updatedlinks.Remove(templink);
- templink.Hasbeenremoved = true;
-
-
-
- if (tempi.Ui >= tempj.Ui + tempa.TravelTime_variable)
- {
- if ((tempi.Ui >= double.PositiveInfinity) && tempi.Fi == 0)
- {
- beta = 1.0;
- }
- else
- {
- beta = tempi.Ui * tempi.Fi;
- }
-
- tempi.Ui = (beta + tempa.Fa * (tempj.Ui + tempa.TravelTime_variable)) / (tempi.Fi + tempa.Fa);
- tempi.Fi += tempa.Fa;
- _RawHyperpath.Add(tempa);
- tempa.InPathsCollection = true;
- }
- if (tempj.Ui + tempa.TravelTime_variable > WorkingNet.AllNodes[o - 1].Ui)
- {
- status = false;
- }
- }
- #endregion
-
- #region Loading
- //Loading step
- DefinedComparison Compare = new DefinedComparison();
- WorkingNet.AllLinks.Sort(Compare);
-
- foreach (Link i in WorkingNet.AllLinks)
- {
- if (i.InPathsCollection)
- {
- i.Pa = (i.Fa / WorkingNet.AllNodes[i.From.ID - 1].Fi) * WorkingNet.AllNodes[i.From.ID - 1].Yi;
-
- WorkingNet.AllNodes[i.To.ID - 1].Yi += i.Pa;
- }
-
- }
-
- #endregion
-
- stw.Stop();
- success = true;
- }
- catch { success = false; }
- TimeSpan_HP = stw.ElapsedMilliseconds;
- return success;
- }
-
- bool HP(Network WorkingNet, int o, int d, ref List<Link> _RawHyperpath, out long TimeSpan_HP)
- {
- Stopwatch stw = new Stopwatch();
- bool success = false;
- _RawHyperpath = new List<Link>();
- stw.Start();
- try
- {
- #region Initialization
- double beta = 0.0;
- Node tempi = WorkingNet.AllNodes[d - 1], tempj;
- Link tempa = new Link();
- bool status = true;
- List<Node> updatednodes = new List<Node>();
- List<Link> updatedlinks = new List<Link>(WorkingNet.AllLinks.ToArray());
- updatednodes.Add(WorkingNet.AllNodes[d - 1]);
- WorkingNet.AllNodes[d - 1].Ui = 0;
- WorkingNet.AllNodes[o - 1].Yi = 1.0;
- #endregion
- #if RecordScanned
- StreamWriter tmpsw = new StreamWriter("..\\..\\ScannedLinks.txt");
- #endif
- #region Updating
- while (status)
- {
- tempa = new Link();
- double Minui = double.PositiveInfinity;
- //select link a
- foreach (Link i in updatedlinks)
- {
- if (i.UiAddCa > WorkingNet.AllNodes[i.To.ID - 1].Ui + i.TravelTime_variable)
- i.UiAddCa = WorkingNet.AllNodes[i.To.ID - 1].Ui + i.TravelTime_variable;
-
- if (Minui >= i.UiAddCa)
- {
- Minui = i.UiAddCa;
- tempa = i;
- }
- }
- #if RecordScanned
- tmpsw.WriteLine(tempa.GID);
- #endif
- updatedlinks.Remove(tempa);
- Minui = double.PositiveInfinity;
- tempi = WorkingNet.AllNodes[tempa.From.ID - 1];
- tempj = WorkingNet.AllNodes[tempa.To.ID - 1];
- YPoints_Ui.Add(tempj.Ui);
- YPoints_UiAddCa.Add(tempa.UiAddCa);
-
- //update node i
- if (tempi.Ui >= tempj.Ui + tempa.TravelTime_variable)
- {
- if ((tempi.Ui >= double.PositiveInfinity) && tempi.Fi == 0)
- {
- beta = 1.0;
- }
- else
- {
- beta = tempi.Ui * tempi.Fi;
- }
- tempi.Ui = (beta + tempa.Fa * (tempj.Ui + tempa.TravelTime_variable)) / (tempi.Fi + tempa.Fa);
- tempi.Fi += tempa.Fa;
-
- if (!tempa.InPathsCollection)
-
- { _RawHyperpath.Add(tempa); tempa.InPathsCollection = true; }
- }
- if (tempj.Ui + tempa.TravelTime_variable >= WorkingNet.AllNodes[o - 1].Ui) status = false;
- }
- #endregion
-
- #region Loading
- //Loading step
-
- DefinedComparison Compare = new DefinedComparison();
- WorkingNet.AllLinks.Sort(Compare);
-
- foreach (Link i in WorkingNet.AllLinks)
- {
- if (i.InPathsCollection)
- {
- i.Pa = (i.Fa / i.From.Fi) * i.From.Yi;
-
- WorkingNet.AllNodes[i.To.ID - 1].Yi += i.Pa;
- }
-
- }
-
- #endregion
- #if RecordScanned
- tmpsw.Close();
- #endif
- stw.Stop();
- success = true;
- }
- catch { success = false; TimeSpan_HP = -1; }
- TimeSpan_HP = stw.ElapsedMilliseconds;
- return success;
- }
-
- bool HP_heu(Network WorkingNet, int o, int d, ref List<Link> _RawHyperpath, out long TimeSpan_SP, out long TimeSpan_HP)
- {
- Stopwatch stw = new Stopwatch();
- bool success = false;
- _RawHyperpath = new List<Link>();
- #if RecordScanned
- StreamWriter tmpsw = new StreamWriter("..\\..\\ScannedLinks.txt");
- #endif
- stw.Start();
- TimeSpan_SP = -1;
- try
- {
- #region Initialization
- double beta = 0.0;
- Node tempi = WorkingNet.AllNodes[d - 1], tempj;
- Link tempa = new Link();
- bool status = true;
-
- List<Link> updatedlinks = new List<Link>(WorkingNet.AllLinks.ToArray());
-
- WorkingNet.AllNodes[d - 1].Ui = 0;
- WorkingNet.AllNodes[o - 1].Yi = 1.0;
- FibDijkstra(WorkingNet, o, d, true, out TimeSpan_SP);
- #endregion
-
- #region Updating
- while (status)
- {
- tempa = new Link();
- double Minui = double.PositiveInfinity;
- //select link a
- foreach (Link i in updatedlinks)
- {
- if (i.UiAddCa > WorkingNet.AllNodes[i.To.ID - 1].Ui + i.TravelTime_variable + WorkingNet.AllNodes[i.From.ID - 1].OptHeuristic)
- i.UiAddCa = WorkingNet.AllNodes[i.To.ID - 1].Ui + i.TravelTime_variable + WorkingNet.AllNodes[i.From.ID - 1].OptHeuristic;
-
- if (Minui >= i.UiAddCa)
- {
- Minui = i.UiAddCa;
- tempa = i;
- }
- }
- #if RecordScanned
- tmpsw.WriteLine(tempa.GID);
- #endif
- updatedlinks.Remove(tempa);
- Minui = double.PositiveInfinity;
- tempi = WorkingNet.AllNodes[tempa.From.ID - 1];
- tempj = WorkingNet.AllNodes[tempa.To.ID - 1];
- YPoints_Ui.Add(tempj.Ui);
- YPoints_UiAddCa.Add(tempa.UiAddCa);
-
- //update node i
- if (tempi.Ui >= tempj.Ui + tempa.TravelTime_variable)
- {
- if ((tempi.Ui >= double.PositiveInfinity) && tempi.Fi == 0)
- {
- beta = 1.0;
- }
- else
- {
- beta = tempi.Ui * tempi.Fi;
- }
- tempi.Ui = (beta + tempa.Fa * (tempj.Ui + tempa.TravelTime_variable)) / (tempi.Fi + tempa.Fa);
- tempi.Fi += tempa.Fa;
-
- if (!tempa.InPathsCollection)
-
- { _RawHyperpath.Add(tempa); tempa.InPathsCollection = true; }
- }
- if (tempj.Ui + tempa.TravelTime_variable + tempi.OptHeuristic >= WorkingNet.AllNodes[o - 1].Ui) status = false;
- }
- #endregion
-
- #region Loading
- //Loading step
-
- DefinedComparison Compare = new DefinedComparison();
- WorkingNet.AllLinks.Sort(Compare);
-
- foreach (Link i in WorkingNet.AllLinks)
- {
- if (i.InPathsCollection)
- {
- i.Pa = (i.Fa / i.From.Fi) * i.From.Yi;
-
- WorkingNet.AllNodes[i.To.ID - 1].Yi += i.Pa;
- }
-
- }
-
- #endregion
- #if RecordScanned
- tmpsw.Close();
- #endif
- stw.Stop();
- success = true;
- }
- catch { success = false; TimeSpan_HP = -1; }
- TimeSpan_HP = stw.ElapsedMilliseconds;
- return success;
- }
-
- bool HP_twist(Network WorkingNet, int o, int d, ref List<Link> _RawHyperpath, out long TimeSpan_DHS)
- {
- //??????UiaddCa????????Heap???max???UiaddCa?????
- DefinedComparison Compare = new DefinedComparison();
-
- Stopwatch stw = new Stopwatch();
- bool success = false;
- #if RecordScanned
- StreamWriter tmpsw = new StreamWriter("..\\..\\ScannedLinks.txt");
- #endif
- stw.Start();
- try
- {
- //??Dijkstra?????????Heuristic
- //Dijkstra(WorkingNet, o, out timeSP);
- //FibDijkstra(WorkingNet, o, d, true, out timeSP);
- #region Initialization
- double beta = 0.0;
- Node tempi = WorkingNet.AllNodes[d - 1], tempj;
- Link tempa = new Link();
- bool status = true;
- List<Node> updatednodes = new List<Node>();
- //???????????????????????UiaddCa???????????link?????????????????????????????
- List<Link> updatedlinks = new List<Link>();
- updatednodes.Add(WorkingNet.AllNodes[d - 1]);
- WorkingNet.AllNodes[d - 1].Ui = 0;
- WorkingNet.AllNodes[o - 1].Yi = 1.0;
- #endregion
-
- #region Updating
- while (status)
- {
-
- foreach (Link j in tempi.InLinks)
- {
- Node tempnodej = null;
- tempnodej = WorkingNet.AllNodes[j.From.ID - 1];
- if (j.UiAddCa > tempi.Ui + j.TravelTime_variable)
- {
- j.UiAddCa = tempi.Ui + j.TravelTime_variable;
- if (j.Hasbeenremoved != true && updatedlinks.Contains(j) == false)
- if (j.InPathsCollection == false && j.HasUpdated == false)
- {
- …
Large files files are truncated, but you can click here to view the full file