public void CountEmpty() { var heap = new Heap <int, string>(); Assert.AreEqual(0, heap.Count); Assert.IsFalse(heap.Any()); }
public void CountAfterPush() { var heap = new Heap <int, string>(); heap.Push(1, "A"); heap.Push(2, "B"); Assert.AreEqual(2, heap.Count); Assert.IsTrue(heap.Any()); }
public void CountEmptyAfterPushPull() { var heap = new Heap <int, string>(); heap.Push(1, "A"); heap.Pull(); Assert.AreEqual(0, heap.Count); Assert.IsFalse(heap.Any()); }
private List <TValue> PullAllValue <TKey, TValue>(Heap <TKey, TValue> heap) where TKey : IComparable <TKey> { var allValue = new List <TValue>(); while (heap.Any()) { allValue.Add(heap.Pull().Value); } return(allValue); }
public static IEnumerable <MultiMazeRunner> BreadthFirstSearchMulti(MultiMazeRunner startTrack) { var tracks = new Heap <MultiMazeRunner>(); tracks.Add(startTrack, startTrack.MoveCount); var visited = new HashSet <MultiMazeRunner>(); while (tracks.Any()) { var track = tracks.Pop(); if (!visited.Add(track)) { continue; } foreach (var nextTrack in track.Children()) { yield return(nextTrack); tracks.Add(nextTrack, nextTrack.MoveCount); } } }
public IEnumerable <AStarNode <State> > Search(AStarNode <State> initialNode) { var closedNodes = new HashSet <State>(); var fringesPriorityQueue = new Heap <AStarNode <State> >(new[] { initialNode }); while (fringesPriorityQueue.Any()) { var currentNode = fringesPriorityQueue.ExtractMin(); if (Mode == SearchMode.GraphSearch) { if (closedNodes.Contains(currentNode.State)) { continue; } else { closedNodes.Add(currentNode.State); } } MaxDepthStat = Math.Max(MaxDepthStat, currentNode.Depth); if (currentNode.IsGoalState()) { yield return(currentNode); } else { foreach (var node in Expander.ExpandNode(currentNode)) { fringesPriorityQueue.Add(node); ExpandedNodesStat++; } } } }
/// <summary>Returns the node that need to be followed in order to reach the destination. Currently only works /// on a Grid, without costs associated with the movements.</summary> // This version with the Heap is actually slower than the one with list. public List <GridNodeAStar> GetAStarNodesWithHeap(string originId, string targetId) { var origin = GetNode(originId); var target = GetNode(targetId); var openSet = new Heap <GridNodeAStar>(_maxWidth * _maxHeight); var closedSet = new Heap <GridNodeAStar>(_maxWidth * _maxHeight); var aStarOrigin = new GridNodeAStar(origin, origin, target); openSet.Add(aStarOrigin); while (openSet.Any()) { var currentNode = openSet.Pop(); closedSet.Add(currentNode); if (currentNode.Id == target.Id) { // Found the target node. Retracing steps. var path = new List <GridNodeAStar>(); var pathPosition = currentNode; while (pathPosition.Id != origin.Id) { path.Add(pathPosition); pathPosition = pathPosition.Parent; } path.Reverse(); return(path); } foreach (var neighbour in GetNeighbours(currentNode).Select(node => new GridNodeAStar(node, origin, target))) { if (closedSet.Items.FirstOrDefault(node => node != null && node.Id == neighbour.Id) != null) { continue; } var newMovementCostToNeighbour = currentNode.GCost + Trigonometry.GetGridDistance( new Point(currentNode.X, currentNode.Y), new Point(neighbour.X, neighbour.Y)); var neighbourInOpenSet = openSet.Items.FirstOrDefault(node => node != null && node.Id == neighbour.Id); if (neighbourInOpenSet == null) { // Neighbour has not been analyzed yet, so we are generating the costs and adding to open set. neighbour.GCost = newMovementCostToNeighbour; neighbour.HCost = Trigonometry.GetGridDistance( new Point(neighbour.X, neighbour.Y), new Point(target.X, target.Y)); neighbour.Parent = currentNode; openSet.Add(neighbour); } if (neighbourInOpenSet != null && newMovementCostToNeighbour > neighbourInOpenSet.GCost) { // Neighbour already exists in open set, but the new movement cost is cheaper, so we're updating it. neighbourInOpenSet.GCost = newMovementCostToNeighbour; neighbourInOpenSet.HCost = Trigonometry.GetDistance( new Point(neighbourInOpenSet.X, neighbourInOpenSet.Y), new Point(target.X, target.Y)); neighbourInOpenSet.Parent = currentNode; } } } return(new List <GridNodeAStar>()); }