private bool search(int source, int target) { Stack <GraphEdge> stack = new Stack <GraphEdge>(); foreach (var e in graph.GetAdjoinEdges(source)) { stack.Push(e); } hasVisited[source] = true; while (stack.Count != 0) { var edge = stack.Pop(); DebugController.instance.AddPathLine(graph.GetNode(edge.From).Pos, graph.GetNode(edge.To).Pos); hasVisited[edge.From] = true; pathIndex[edge.To] = edge.From; if (edge.To == target) { return(true); } foreach (var e in graph.GetAdjoinEdges(edge.To)) { if (!hasVisited[e.To]) { stack.Push(e); } } } return(false); }
private bool search(int source, int target) { if (source == target) { pathIndex[target] = source; return(true); } Queue <GraphEdge> queue = new Queue <GraphEdge>(); hasVisited[source] = true; pathIndex[source] = source; foreach (var e in graph.GetAdjoinEdges(source)) { queue.Enqueue(e); hasVisited[e.To] = true; pathIndex[e.To] = e.From; if (e.To == target) { return(true); } } while (queue.Count != 0) { var edge = queue.Dequeue(); DebugController.instance.AddPathLine(graph.GetNode(edge.From).Pos, graph.GetNode(edge.To).Pos); foreach (var e in graph.GetAdjoinEdges(edge.To)) { if (!hasVisited[e.To]) { queue.Enqueue(e); hasVisited[e.To] = true; pathIndex[e.To] = e.From; if (e.To == target) { return(true); } } } } return(false); }
private bool search(int source, int target) { //int num = 0; if (source == target) { //当起点和终点相同时 Debug.Log("目标和起点节点相同" + target); return(true); } //Debug.Log("起始点是"+source+"终点是"+target); costToNode[source] = 0; costToEnd[source] = 0; IndexPriorityQueue <double> pq = new IndexPriorityQueue <double>(costToEnd, (i, j) => (float)(i - j), PriorityOrder.MinFirst); searchFrontier[source] = new GraphEdge(source, source); while (!pq.isEmpty()) { //Debug.Log("A*"); int index = pq.DequeueIndex(); spt[index] = searchFrontier[index]; //Debug.Log("最小花费的点是"+index+"花费是"+costToEnd[index]); DebugController.instance.AddPathLine(graph.GetNode(spt[index].From).Pos, graph.GetNode(spt[index].To).Pos); if (index == target) { //Debug.Log("A*总的搜索次数是"+num); return(true); } //Debug.Log("" + graph.GetAdjoinEdges(index).Count); foreach (var e in graph.GetAdjoinEdges(index)) { //num++; double costNode = costToNode[e.From] + e.Cost; double costEnd = costNode + heuristic(e.To, target); //Debug.Log("边的总花费是" + costEnd+"边的花费是"+e.Cost+"上一个节点的花费是"+costToNode[e.From]+"启发因子花费是"+ heuristic(e.To, target)); if (searchFrontier[e.To] == null) { costToNode[e.To] = costNode; costToEnd[e.To] = costEnd; searchFrontier[e.To] = e; pq.ChangePriority(e.To, costEnd); } else { //Debug.Log("哈哈哈哈"+costNode+",,"+costToNode[e.To]); if (costNode < costToNode[e.To] && spt[e.To] == null) { //否则只有当spt上没有这个节点 //Debug.Log("啦啦啦啦"); costToEnd[e.To] = costEnd; costToNode[e.To] = costNode; searchFrontier[e.To] = e; pq.ChangePriority(e.To, costEnd); } } } } return(false); }
private bool search(int source, int target) { int num = 0; if (source == target) { spt[target] = new GraphEdge(target, target); Debug.Log("一样"); return(true); } IndexPriorityQueue <float> queue = new IndexPriorityQueue <float>(costToNode, (i, j) => (i - j), PriorityOrder.MinFirst); searchFrontier[source] = new GraphEdge(source, source); //searchFrontier[] //int t = queue.TopIndex(); //Debug.Log("起点处理完后最小的点是" + t+"花费是"+costToNode[t]); while (!queue.isEmpty()) { var index = queue.DequeueIndex(); spt[index] = searchFrontier[index]; DebugController.instance.AddPathLine(graph.GetNode(spt[index].From).Pos, graph.GetNode(spt[index].To).Pos); if (index == target) { //Debug.Log("DJ总的搜索次数是"+num); return(true); } //queue.ChangePriority(index,float.PositiveInfinity); #region 邻接边的寻找 foreach (var e in graph.GetAdjoinEdges(index)) { num++; float costNode = costToNode[e.From] + e.Cost; //float costEnd = costNode + heuristic(e.To, target); if (searchFrontier[e.To] == null) { costToNode[e.To] = costNode; searchFrontier[e.To] = e; queue.ChangePriority(e.To, costNode); } else if (costNode < costToNode[e.To] && spt[e.To] == null) { //否则只有当spt上没有这个节点 Debug.Log("lalalal"); costToNode[e.To] = costNode; searchFrontier[e.To] = e; queue.ChangePriority(e.To, costNode); } } #endregion } return(false); }