/// <summary> /// implmentation of the Search method. /// </summary> /// <param name="searchable"> the searching problem to search in. </param> /// <returns> Solution object that includes the nodes of the route from the initial node to the goal.</returns> public override Solution <T> Search(ISearchable <T> searchable) { ConcurrentPriorityQueue <State <T>, double> open = new ConcurrentPriorityQueue <State <T>, double>(); closed = new HashSet <State <T> >(); State <T> initialState = searchable.GetInitialState(); open.Enqueue(initialState, 0); while (open.Count != 0) { State <T> node = open.Dequeue(); evaluatedNodes++; if (node.Equals(searchable.GetGoalState())) { return(new Solution <T>(BackTrace(searchable), GetNumberOfNodesEvaluated())); } closed.Add(node); foreach (State <T> adjacent in searchable.GetAllPossibleStates(node)) { if (!closed.Contains(adjacent) && !open.Contains(adjacent)) { adjacent.CameFrom = node; adjacent.TotalCost = node.TotalCost + adjacent.Cost; open.Enqueue(adjacent, adjacent.TotalCost); } else { if ((node.TotalCost + adjacent.Cost) < adjacent.TotalCost) { adjacent.CameFrom = node; adjacent.TotalCost = node.TotalCost + adjacent.Cost; if (open.Contains(adjacent)) { open.UpdatePriority(adjacent, adjacent.TotalCost); } else { closed.Remove(adjacent); open.Enqueue(adjacent, adjacent.TotalCost); } } } } } return(null); }
public void Contains() { // Create a new priority queue. ConcurrentPriorityQueue <int> queue = new ConcurrentPriorityQueue <int>(); // Create and store a new element. PriorityValuePair <int> elem = new PriorityValuePair <int>(1.0, 2); // Ensure the queue contains the element. Assert.That(queue.Contains(elem), Is.False); // Enqueue it in the queue. queue.Enqueue(elem); // Ensure the queue now contains the element. Assert.That(queue.Contains(elem), Is.True); }
public void SamplesItemsWhenSizeLimitReached() { const int numberOfItemsToAddInitially = 100; const int numberOfItemsToAddAfterReservoirLimitReached = 100; //Concurrent Priority Queue will only hold NumberOfItemsToAddInitially items. ConcurrentPriorityQueue.Resize(numberOfItemsToAddInitially); var itemsToAddInitially = new PrioritizedNode <TestModel> [numberOfItemsToAddInitially]; for (var i = 0; i < numberOfItemsToAddInitially; ++i) { itemsToAddInitially[i] = Create(i * 0.001f); } //fill the CPQ with values 100-199 foreach (var itemToAdd in itemsToAddInitially) { Assert.IsTrue(ConcurrentPriorityQueue.Add(itemToAdd), "failed to add initial value"); } //make sure they are all accounted for Assert.AreEqual(ConcurrentPriorityQueue.Count, numberOfItemsToAddInitially); //now add more items that will cause the items from above to get removed. these will be valued 0-99 (precisely 100 less than those above) for (var i = 0; i < numberOfItemsToAddAfterReservoirLimitReached; ++i) { var itemToAdd = Create((i + 100) * 0.001f); var itemThatWillGetRemoved = itemsToAddInitially[i]; //each one we add will cause the corresponding smaller item to get removed. Assert.IsTrue(ConcurrentPriorityQueue.Add(itemToAdd), "failed to add subsequent value"); Assert.IsTrue(ConcurrentPriorityQueue.Contains(itemToAdd), "added value not found"); Assert.IsFalse(ConcurrentPriorityQueue.Contains(itemThatWillGetRemoved), "initial value did not get removed on addition of subsequent value"); Assert.AreEqual(ConcurrentPriorityQueue.Count, numberOfItemsToAddInitially); } }
public Stack <Tile> FindPath(bool includeLast) { if (_startTile == null || _endTile == null) { return(null); } if (_startTile.Region != _endTile.Region && !RegionTileNextTo(_startTile, _endTile)) { return(null); } if (_startTile == _endTile) { var newStack = new Stack <Tile>(); newStack.Push(_endTile); return(newStack); } _openList.Enqueue(_startTile, 0); _fScores[_startTile] = 0; do { var bestFScoreTile = _openList.Dequeue(); _closedList.Add(bestFScoreTile); var neighbors = bestFScoreTile.GetNeighbors(); if (bestFScoreTile == _endTile) { return(ConstructPath(includeLast)); } foreach (var neighbor in neighbors) { if (neighbor == null) { continue; } if (neighbor.MovementCost == 0 && neighbor != _endTile) { continue; } if (_closedList.Contains(neighbor)) { continue; } if (CanMoveToNeighbour(neighbor, neighbors) == false) { continue; } int fScore = _fScores[bestFScoreTile] + Cost(bestFScoreTile, neighbor); if (!_openList.Contains(neighbor) || fScore < _fScores[neighbor]) { _fScores[neighbor] = fScore; _cameFrom[neighbor] = bestFScoreTile; var priority = (int)(fScore + Heuristic(neighbor, _endTile)); _openList.Enqueue(neighbor, -priority); } } } while (_openList.Count > 0); return(null); }