/// <summary> /// 从PlanCourse取出一个当前累积权值最小,并且没有被处理过的节点 /// </summary> /// <returns></returns> private Node GetMinWeightRudeNode(PlanCourse planCourse, List <Node> nodeList, string originID) { double weight = double.MaxValue; Node destNode = null; foreach (Node node in nodeList) { if (node.ID == originID) { continue; } PassedPath pPath = planCourse[node.ID]; if (pPath.BeProcessed) { continue; } if (pPath.Weight < weight) { weight = pPath.Weight; destNode = node; } } return(destNode); }
public PlanCourse(List <UnitStar> nodeList, int originID) { this.htPassedPath = new Hashtable(); UnitStar originNode = null; foreach (UnitStar node in nodeList) { if (node.UnitId == originID) { originNode = node; } else { PassedPath pPath = new PassedPath(node.UnitId); this.htPassedPath.Add(node.UnitId, pPath);//保存源节点跟其他节点的路径 } } if (originNode == null) { throw new Exception("The origin node is not exist !"); } this.InitializeWeight(originNode); }
//从PlanCourse取出一个当前累积权值最小,并且没有被处理过的节点 private UnitStar GetMinWeightRudeNode(PlanCourse planCourse, List <UnitStar> nodeList, int originID) { double weight = double.MaxValue; UnitStar destNode = null; foreach (UnitStar node in nodeList) { if (node.UnitId == originID) { continue; } PassedPath pPath = planCourse[node.UnitId]; if (pPath.BeProcessed) { continue; } if (pPath.Weight < weight) { weight = pPath.Weight; destNode = node; } } return(destNode); }
//获取权值最小的路径 public RoutePlanResult Paln(List <UnitStar> nodeList, int originID, int destID) { PlanCourse planCourse = new PlanCourse(nodeList, originID); UnitStar curNode = this.GetMinWeightRudeNode(planCourse, nodeList, originID); #region 计算过程 while (curNode != null) { PassedPath curPath = planCourse[curNode.UnitId];//获取当前计算节点的路径 foreach (Edge edge in curNode.EdgeList) { //Debug.Log("curPath:" + curPath.CurNodeID + " targetPath:" + edge.EndNodeID); PassedPath targetPath = planCourse[edge.EndNodeID]; //获取目标计算节点的路径 if (targetPath == null) //回溯到原点,没有意义 { continue; } double tempWeight = curPath.Weight + edge.Weight; //算出当前计算节点的权重加上边的权重 if (tempWeight < targetPath.Weight) //如果小于直接到目标节点的权重,则更新 { targetPath.Weight = tempWeight; // targetPath.PassedIDList.Clear(); for (int i = 0; i < curPath.PassedIDList.Count; i++) { targetPath.PassedIDList.Add(curPath.PassedIDList[i]); } targetPath.PassedIDList.Add(curNode.UnitId); } } //标志为已处理 planCourse[curNode.UnitId].BeProcessed = true; //获取下一个未处理节点 curNode = this.GetMinWeightRudeNode(planCourse, nodeList, originID); } #endregion 计算过程 //表示规划结束 return(this.GetResult(planCourse, destID)); }
/// <summary> /// 获取权值最小的路径 /// </summary> /// <param name="nodeList"></param> /// <param name="originID"></param> /// <param name="destID"></param> /// <returns></returns> public RoutePlanResult Paln(List <Node> nodeList, string originID, string destID) { //初始化起始节点到其他节点的路径表(权值,经过的节点,是否被处理) //同时初始化其他节点的路径表 PlanCourse planCourse = new PlanCourse(nodeList, originID); Node curNode = this.GetMinWeightRudeNode(planCourse, nodeList, originID); #region 计算过程 while (curNode != null) { PassedPath curPath = planCourse[curNode.ID]; foreach (Edge edge in curNode.EdgeList) { PassedPath targetPath = planCourse[edge.EndNodeID]; double tempWeight = curPath.Weight + edge.Weight; if (tempWeight < targetPath.Weight) { targetPath.Weight = tempWeight; targetPath.PassedNodeList.Clear(); for (int i = 0; i < curPath.PassedNodeList.Count; i++) { targetPath.PassedNodeList.Add(curPath.PassedNodeList[i]); } targetPath.PassedNodeList.Add(curNode); } } //标志为已处理 planCourse[curNode.ID].BeProcessed = true; //获取下一个未处理节点 curNode = this.GetMinWeightRudeNode(planCourse, nodeList, originID); } #endregion //表示规划结束 return(this.GetResult(planCourse, destID)); }
private void InitializeWeight(UnitStar originNode) { if ((originNode.EdgeList == null) || (originNode.EdgeList.Count == 0)) { return; } foreach (Edge edge in originNode.EdgeList) { PassedPath pPath = this[edge.EndNodeID]; if (pPath == null) { continue; } pPath.PassedIDList.Add(originNode.UnitId);//找出跟源节点联通的点 pPath.Weight = edge.Weight; } }
/// <summary> /// 从PlanCourse表中取出目标节点的PassedPath,这个PassedPath即是规划结果 /// </summary> /// <returns></returns> private RoutePlanResult GetResult(PlanCourse planCourse, string destID) { PassedPath pPath = planCourse[destID]; if (pPath.Weight == int.MaxValue) { RoutePlanResult result1 = new RoutePlanResult(null, int.MaxValue); return(result1); } Node[] passedNodeIDs = new Node[pPath.PassedNodeList.Count]; for (int i = 0; i < passedNodeIDs.Length; i++) { passedNodeIDs[i] = pPath.PassedNodeList[i]; } RoutePlanResult result = new RoutePlanResult(passedNodeIDs, pPath.Weight); return(result); }
//从PlanCourse表中取出目标节点的PassedPath,这个PassedPath即是规划结果 private RoutePlanResult GetResult(PlanCourse planCourse, int destID) { PassedPath pPath = planCourse[destID]; if (pPath.Weight == int.MaxValue) { RoutePlanResult result1 = new RoutePlanResult(); result1.Weight = int.MaxValue; return(result1); } List <UnitStar> passedStars = new List <UnitStar>(); for (int i = 0; i < pPath.PassedIDList.Count; i++) { passedStars.Add(MogoWorld.m_dataMapManager.GetUnitStarById(pPath.PassedIDList[i])); } RoutePlanResult result = new RoutePlanResult(); result.PassedStars = passedStars; result.Weight = pPath.Weight; return(result); }