private void WriteLogWithTwoWayTraveledData(TwoWayTraveledPathData data) { Application.Current.Dispatcher.Invoke(new Action(() => { SearchEventManager sem = data.SEM; Logger.ClearLog(); foreach (var searchEvent in sem.Events) { Logger.WriteLogInfo( searchEvent.EventMessage); } StringBuilder msg = new StringBuilder(); Logger.WriteLogInfo(string.Format("Total edges EXPLORED in PATH1: {0}", data.Path1.ExploredEdges.Count)); foreach (var edge in data.Path1.ExploredEdges) { msg.Append(edge + "=>"); } Logger.WriteLogInfo(msg.ToString()); msg.Clear(); Logger.WriteLogInfo(string.Format("Total edges EXPLORED in PATH2: {0}", data.Path2.ExploredEdges.Count)); foreach (var edge in data.Path2.ExploredEdges) { msg.Append(edge + "=>"); } Logger.WriteLogInfo(msg.ToString()); msg.Clear(); Logger.WriteLogInfo(string.Format("Total edges TRAVELED in PATH1: {0}", data.Path1.TraveledEdges.Count)); foreach (var vert in data.Path1.TraveledEdges) { msg.Append(vert + "=>"); } Logger.WriteLogInfo(msg.ToString()); msg.Clear(); Logger.WriteLogInfo(string.Format("Total edges TRAVELED in PATH2: {0}", data.Path2.TraveledEdges.Count)); foreach (var vert in data.Path2.TraveledEdges) { msg.Append(vert + "=>"); } Logger.WriteLogInfo(msg.ToString()); msg.Clear(); Logger.WriteLogInfo("SHORTEST PATH:"); var shortestPath = data.GetShortestPath(); foreach (var edge in shortestPath) { msg.Append(edge + "=>"); } Logger.WriteLogInfo(msg.ToString()); })); }
private TwoWayTraveledPathData Search() { pathData = new TwoWayTraveledPathData(root, goal); // enqueue root node in frontier pathData.Frontier1.Enqueue(root); pathData.Frontier2.Enqueue(goal); // if frontier is empty - we finished searching while (!pathData.Frontier1.Empty || !pathData.Frontier2.Empty) { // by priority at top I mean the smallest total cost path to vertex in frontier pathData.UpdateCost1(pathData.Frontier1.PriorityAtTop()); pathData.UpdateCost2(pathData.Frontier2.PriorityAtTop()); // dequeue vertex with the least total cost Vertex node1 = pathData.Frontier1.Dequeue(); Vertex node2 = pathData.Frontier2.Dequeue(); // search in explored for adjacent edge for added node foreach (var edge in pathData.Path1.ExploredEdges) { if (edge.VerticeTo == node1) { pathData.AddTraveledEdgeToPath1(new Edge(edge, pathData.Cost1)); } } foreach (var edge in pathData.Path2.ExploredEdges) { if (edge.VerticeTo == node2) { pathData.AddTraveledEdgeToPath2(new Edge(edge, pathData.Cost2)); } } // if our dequeued node is goal node, then we got path with shortest cost to it if (node1 == goal || node2 == root) { return(pathData); } // add dequeued node to explored list pathData.Explored1.Add(node1); pathData.Explored2.Add(node2); if (pathData.Path2.TraveledEdges.Any(e => e.VerticeTo == node1) || pathData.Path1.TraveledEdges.Any(e => e.VerticeTo == node2)) { return(pathData); } // foreach neighbour's node foreach (var adjacentVertex in graph.AdjacentVertices(node1)) { // if node is not in explored if (!pathData.Explored1.Contains(adjacentVertex)) { Edge edge = graph.GetEdge(node1, adjacentVertex); // and if neighbour node is not in frontier if (!pathData.Frontier1.Contains(adjacentVertex)) { // add edge NODE-ADJACENTVERTEX pathData.AddExploredEdgeToPath1(new Edge(edge, pathData.Cost1 + edge.Weight)); pathData.Frontier1.Enqueue(adjacentVertex, pathData.Cost1 + edge.Weight); } // else if neighbour node is in frontier and its cost is higher then current // -> replace existing node with node with lower path cost else if ((pathData.Cost1 + edge.Weight) < pathData.Frontier1.GetPriority(adjacentVertex)) { // remove edge PREVIOUSNODE-ADJACENTVERTEX // add edge NODE-ADJACENTVERTEX pathData.RemoveExploredFromPath1(adjacentVertex); pathData.AddExploredEdgeToPath1(new Edge(edge, pathData.Cost1 + edge.Weight)); // update vertex priority in frontier pathData.Frontier1.Remove(adjacentVertex); pathData.Frontier1.Enqueue(adjacentVertex, pathData.Cost1 + edge.Weight); } else { // add and delete it cause it is failure, longer path Edge tempEdge = new Edge(edge, pathData.Cost1 + edge.Weight); pathData.AddExploredEdgeToPath1(tempEdge); pathData.RemoveExploredFromPath1(tempEdge); } } } // foreach neighbour's node foreach (var adjacentVertex in graph.AdjacentVertices(node2)) { // if node is not in explored if (!pathData.Explored2.Contains(adjacentVertex)) { Edge edge = graph.GetEdge(node2, adjacentVertex); // and if neighbour node is not in frontier if (!pathData.Frontier2.Contains(adjacentVertex)) { // add edge NODE-ADJACENTVERTEX pathData.AddExploredEdgeToPath2(new Edge(edge, pathData.Cost2 + edge.Weight)); pathData.Frontier2.Enqueue(adjacentVertex, pathData.Cost2 + edge.Weight); } // else if neighbour node is in frontier and its cost is higher then current // -> replace existing node with node with lower path cost else if ((pathData.Cost2 + edge.Weight) < pathData.Frontier2.GetPriority(adjacentVertex)) { // remove edge PREVIOUSNODE-ADJACENTVERTEX // add edge NODE-ADJACENTVERTEX pathData.RemoveExploredFromPath2(adjacentVertex); pathData.AddExploredEdgeToPath2(new Edge(edge, pathData.Cost2 + edge.Weight)); // update vertex priority in frontier pathData.Frontier2.Remove(adjacentVertex); pathData.Frontier2.Enqueue(adjacentVertex, pathData.Cost2 + edge.Weight); } else { // add and delete it cause it is failure, longer path Edge tempEdge = new Edge(edge, pathData.Cost2 + edge.Weight); pathData.AddExploredEdgeToPath2(tempEdge); pathData.RemoveExploredFromPath2(tempEdge); } } } } return(null); }
private TwoWayTraveledPathData Search() { pathData = new TwoWayTraveledPathData(root, goal); // enqueue root node in frontier pathData.Frontier1.Enqueue(root); pathData.Frontier2.Enqueue(goal); // if frontier is empty - we finished searching while (!pathData.Frontier1.Empty || !pathData.Frontier2.Empty) { // by priority at top I mean the smallest total cost path to vertex in frontier pathData.UpdateCost1(pathData.Frontier1.PriorityAtTop()); pathData.UpdateCost2(pathData.Frontier2.PriorityAtTop()); // dequeue vertex with the least total cost Vertex node1 = pathData.Frontier1.Dequeue(); Vertex node2 = pathData.Frontier2.Dequeue(); // search in explored for adjacent edge for added node foreach (var edge in pathData.Path1.ExploredEdges) { if (edge.VerticeTo == node1) { pathData.AddTraveledEdgeToPath1(new Edge(edge, pathData.Cost1)); } } foreach (var edge in pathData.Path2.ExploredEdges) { if (edge.VerticeTo == node2) { pathData.AddTraveledEdgeToPath2(new Edge(edge, pathData.Cost2)); } } // if our dequeued node is goal node, then we got path with shortest cost to it if (node1 == goal || node2 == root) { return pathData; } // add dequeued node to explored list pathData.Explored1.Add(node1); pathData.Explored2.Add(node2); if (pathData.Path2.TraveledEdges.Any(e => e.VerticeTo == node1) || pathData.Path1.TraveledEdges.Any(e => e.VerticeTo == node2)) { return pathData; } // foreach neighbour's node foreach (var adjacentVertex in graph.AdjacentVertices(node1)) { // if node is not in explored if (!pathData.Explored1.Contains(adjacentVertex)) { Edge edge = graph.GetEdge(node1, adjacentVertex); // and if neighbour node is not in frontier if (!pathData.Frontier1.Contains(adjacentVertex)) { // add edge NODE-ADJACENTVERTEX pathData.AddExploredEdgeToPath1(new Edge(edge, pathData.Cost1 + edge.Weight)); pathData.Frontier1.Enqueue(adjacentVertex, pathData.Cost1 + edge.Weight); } // else if neighbour node is in frontier and its cost is higher then current // -> replace existing node with node with lower path cost else if ((pathData.Cost1 + edge.Weight) < pathData.Frontier1.GetPriority(adjacentVertex)) { // remove edge PREVIOUSNODE-ADJACENTVERTEX // add edge NODE-ADJACENTVERTEX pathData.RemoveExploredFromPath1(adjacentVertex); pathData.AddExploredEdgeToPath1(new Edge(edge, pathData.Cost1 + edge.Weight)); // update vertex priority in frontier pathData.Frontier1.Remove(adjacentVertex); pathData.Frontier1.Enqueue(adjacentVertex, pathData.Cost1 + edge.Weight); } else { // add and delete it cause it is failure, longer path Edge tempEdge = new Edge(edge, pathData.Cost1 + edge.Weight); pathData.AddExploredEdgeToPath1(tempEdge); pathData.RemoveExploredFromPath1(tempEdge); } } } // foreach neighbour's node foreach (var adjacentVertex in graph.AdjacentVertices(node2)) { // if node is not in explored if (!pathData.Explored2.Contains(adjacentVertex)) { Edge edge = graph.GetEdge(node2, adjacentVertex); // and if neighbour node is not in frontier if (!pathData.Frontier2.Contains(adjacentVertex)) { // add edge NODE-ADJACENTVERTEX pathData.AddExploredEdgeToPath2(new Edge(edge, pathData.Cost2 + edge.Weight)); pathData.Frontier2.Enqueue(adjacentVertex, pathData.Cost2 + edge.Weight); } // else if neighbour node is in frontier and its cost is higher then current // -> replace existing node with node with lower path cost else if ((pathData.Cost2 + edge.Weight) < pathData.Frontier2.GetPriority(adjacentVertex)) { // remove edge PREVIOUSNODE-ADJACENTVERTEX // add edge NODE-ADJACENTVERTEX pathData.RemoveExploredFromPath2(adjacentVertex); pathData.AddExploredEdgeToPath2(new Edge(edge, pathData.Cost2 + edge.Weight)); // update vertex priority in frontier pathData.Frontier2.Remove(adjacentVertex); pathData.Frontier2.Enqueue(adjacentVertex, pathData.Cost2 + edge.Weight); } else { // add and delete it cause it is failure, longer path Edge tempEdge = new Edge(edge, pathData.Cost2 + edge.Weight); pathData.AddExploredEdgeToPath2(tempEdge); pathData.RemoveExploredFromPath2(tempEdge); } } } } return null; }