protected override void PostStop() { this._cancellable.CancelIfNotNull(); this._cancellable = null; this._queue = null; this._map = null; }
public OldEventsourcedScheduler(int maxQueuedMessages, int operationCountPerSnapshot) { this._maxQueuedMessages = maxQueuedMessages; this._operationCountPerSnapshot = operationCountPerSnapshot; this._queue = new GenericPriorityQueue <FutureMessageNode, DateTimeOffset>(maxQueuedMessages); this._map = new Dictionary <string, FutureMessageNode>(maxQueuedMessages); }
private void Update() { var adjacencyRule = (AdjacencyRule)_baseMap.DistanceMeasurement; var openSet = new GenericPriorityQueue <PositionNode, double>(Width * Height); foreach (var point in _baseMap.Walkable) { var newPoint = _baseMap[point] * -Magnitude; _goalMap[point] = newPoint; openSet.Enqueue(_nodes[point], newPoint.Value); } var edgeSet = new HashSet <Coord>(); var closedSet = new HashSet <Coord>(); while (openSet.Count > 0) //multiple runs are needed to deal with islands { var minNode = openSet.Dequeue(); closedSet.Add(minNode.Position); foreach (var openPoint in adjacencyRule.Neighbors(minNode.Position)) { if ((!closedSet.Contains(openPoint)) && _baseMap.BaseMap[openPoint] != GoalState.Obstacle) { edgeSet.Add(openPoint); } } while (edgeSet.Count > 0) { foreach (var coord in edgeSet.ToArray()) { var current = _goalMap[coord].Value; foreach (var openPoint in adjacencyRule.Neighbors(coord)) { if (closedSet.Contains(openPoint) || _baseMap.BaseMap[openPoint] == GoalState.Obstacle) { continue; } var neighborValue = _goalMap[openPoint].Value; var newValue = current + _baseMap.DistanceMeasurement.Calculate(coord, openPoint); if (newValue < neighborValue) { _goalMap[openPoint] = newValue; openSet.UpdatePriority(_nodes[openPoint], newValue); edgeSet.Add(openPoint); } } edgeSet.Remove(coord); closedSet.Add(coord); openSet.Remove(_nodes[coord]); } } } }
/// <summary> /// Constructor. Takes a goal map where in all goals are treated as threats to be avoided, /// and a magnitude to use (defaulting to 1.2). /// </summary> /// <param name="baseMap">The underlying goal map to use.</param> /// <param name="magnitude">Magnitude to multiply by during calculation.</param> public FleeMap(GoalMap baseMap, double magnitude = 1.2) { _baseMap = baseMap ?? throw new ArgumentNullException(nameof(baseMap)); Magnitude = magnitude; _goalMap = new ArrayView <double?>(baseMap.Width, baseMap.Height); _nodes = new ArrayView <PositionNode>(baseMap.Width, baseMap.Height); _openSet = new GenericPriorityQueue <PositionNode, double>(baseMap.Width * baseMap.Height); _edgeSet = new Queue <Point>(); _closedSet = new BitArray(baseMap.Width * baseMap.Height); foreach (var pos in _nodes.Positions()) { _nodes[pos] = new PositionNode(pos); } _baseMap.Updated += Update; // Necessary to ensure the FleeMap is valid immediately after construction Update(); }
protected override void PreStart() { this._queue = new GenericPriorityQueue <FutureMessageNode, DateTimeOffset>(this._defaultQueueSize); this._map = new Dictionary <string, FutureMessageNode>(this._defaultQueueSize); }
public SimplePriorityQueue() { _queue = new GenericPriorityQueue <SimpleNode, TPriority>(INITIAL_QUEUE_SIZE); }
private static Path shortestPath(Tile a, Tile b, Map m) { //If runtime is too high, use a hash table to store indices dijkstraNode[] nodes = new dijkstraNode[moveList.Count]; index[] indices = new index[moveList.Count]; for (int i = 0; i < moveList.Count; ++i) { nodes[i].found = false; nodes[i].dist = 999; nodes[i].previous = -1; indices[i] = new index(i); } int start = moveList.IndexOf(a); int end = moveList.IndexOf(b); //<index,weight> GenericPriorityQueue <index, int> queue = new GenericPriorityQueue <index, int>(moveList.Count); nodes[start].dist = 0; for (int i = 0; i < moveList.Count; i++) { if (i == start) { queue.Enqueue(indices[i], 0); } else { queue.Enqueue(indices[i], 999); } } index tempi; int x, y, r; while (queue.Count != 0) { //for(int k = 0; k<queue.Count; k++) { tempi = queue.Dequeue(); nodes[tempi.i].found = true; if (tempi.i == end) { break; } x = moveList[tempi.i].getX(); y = moveList[tempi.i].getY(); //ideally we shorten this; maybe do some sin/cos stuff in UserMath //east r = moveList.IndexOf(m.findTile(x + 1, y)); if (x < m.getMapWidth() - 1 && r != -1 && !nodes[r].found) { if (!nodes[r].found && nodes[r].dist > movementCosts[moveList[r].getType()] + nodes[tempi.i].dist) { nodes[r].dist = movementCosts[moveList[r].getType()] + nodes[tempi.i].dist; nodes[r].previous = tempi.i; queue.UpdatePriority(indices[r], nodes[r].dist); } } //north r = moveList.IndexOf(m.findTile(x, y + 1)); if (r != -1 && !nodes[r].found && y < m.getMapHeight() - 1) { if (!nodes[r].found && nodes[r].dist > movementCosts[moveList[r].getType()] + nodes[tempi.i].dist) { nodes[r].dist = movementCosts[moveList[r].getType()] + nodes[tempi.i].dist; nodes[r].previous = tempi.i; queue.UpdatePriority(indices[r], nodes[r].dist); } } //west r = moveList.IndexOf(m.findTile(x - 1, y)); if (r != -1 && !nodes[r].found && x > 0) { if (!nodes[r].found && nodes[r].dist > movementCosts[moveList[r].getType()] + nodes[tempi.i].dist) { nodes[r].dist = movementCosts[moveList[r].getType()] + nodes[tempi.i].dist; nodes[r].previous = tempi.i; queue.UpdatePriority(indices[r], nodes[r].dist); } } //south r = moveList.IndexOf(m.findTile(x, y - 1)); if (r != -1 && !nodes[r].found && y > 0) { if (!nodes[r].found && nodes[r].dist > movementCosts[moveList[r].getType()] + nodes[tempi.i].dist) { nodes[r].dist = movementCosts[moveList[r].getType()] + nodes[tempi.i].dist; nodes[r].previous = tempi.i; queue.UpdatePriority(indices[r], nodes[r].dist); } } } //end of while-loop Path ret = new Path(); if (nodes[end].previous == -1) { return(null); } for (int i = end; i != start;) { ret.insertHead(moveList[i], movementCosts[moveList[i].getType()]); i = nodes[i].previous; } //To impelement; high runtimes might result in reworking this algorithm return(ret); }
public static List <IPathNode> FindPath(IPathNode start, IPathNode finish) { if (start == finish) { return(new List <IPathNode>()); } var frontier = new GenericPriorityQueue <IPathNode, int>(1000); frontier.Enqueue(start, 0); var cameFrom = new Dictionary <IPathNode, IPathNode>(); var costSoFar = new Dictionary <IPathNode, int>(); IPathNode current; IPathNode next; List <IPathNode> neighbours; int newCost; int priority; cameFrom[start] = null; costSoFar[start] = 0; while (frontier.Count > 0) { current = frontier.Dequeue(); if (current == finish) { break; } neighbours = current.GetNeighbours(); for (int i = 0; i < neighbours.Count; i++) { next = neighbours[i]; newCost = costSoFar[current] + (next == finish || next.IsWalkable ? 1 : 10000); if (!costSoFar.ContainsKey(next) || newCost < costSoFar[next]) { if (costSoFar.ContainsKey(next)) { costSoFar[next] = newCost; } else { costSoFar.Add(next, newCost); } priority = newCost + finish.GetHeuristic(next); if (frontier.Contains(next)) { frontier.UpdatePriority(next, priority); } else { frontier.Enqueue(next, priority); } cameFrom[next] = current; } } } var path = new List <IPathNode>(); if (cameFrom.ContainsKey(finish)) { IPathNode last = finish; path.Add(finish); while (cameFrom[last] != start) { path.Add(cameFrom[last]); last = cameFrom[last]; } path.Reverse(); } return(path); }
/// <summary> /// Instantiate a new Priority Queue /// </summary> /// <param name="comparer">The comparison function to use to compare TPriority values</param> public SimplePriorityQueue(Comparison <TPriority> comparer) { _queue = new GenericPriorityQueue <SimpleNode, TPriority>(INITIAL_QUEUE_SIZE, comparer); _itemToNodesCache = new Dictionary <TItem, IList <SimpleNode> >(); _nullNodesCache = new List <SimpleNode>(); }
/// <summary> /// A* search /// </summary> /// <param name="allowReverse"></param> /// <param name="carsToIgnore"></param> /// <param name="consistLength"></param> protected async System.Threading.Tasks.Task Astar(bool allowReverse, HashSet <string> carsToIgnore, double consistLength, List <TrackTransition> bannedTransitions) { await Await.BackgroundSyncContext(); cameFrom = new Dictionary <RailTrack, RailTrack>(); costSoFar = new Dictionary <RailTrack, double>(); //var queue = new PriorityQueue<RailTrack>(); var queue = new GenericPriorityQueue <RailTrackNode, double>(10000); queue.Enqueue(new RailTrackNode(start), 0.0); cameFrom.Add(start, start); costSoFar.Add(start, 0.0); RailTrack current = null; while (queue.Count > 0) { current = queue.Dequeue().track; RailTrack prev = null; cameFrom.TryGetValue(current, out prev); string debug = $"ID: {current.logicTrack.ID.FullID} Prev: {prev?.logicTrack.ID.FullID}"; List <RailTrack> neighbors = new List <RailTrack>(); if (current.outIsConnected) { neighbors.AddRange(current.GetAllOutBranches().Select(b => b.track)); } if (current.inIsConnected) { neighbors.AddRange(current.GetAllInBranches().Select(b => b.track)); } string branches = DumpNodes(neighbors, current); debug += "\n" + $"all branches: {branches}"; #if DEBUG2 Terminal.Log(debug); #endif foreach (var neighbor in neighbors) { if (bannedTransitions != null && bannedTransitions.All(t => t.track == current && t.nextTrack == neighbor)) { Terminal.Log($"{current.logicTrack.ID.FullID}->{neighbor.logicTrack.ID.FullID} banned"); continue; } //if non start/end track is not free omit it if (neighbor != start && neighbor != goal && !neighbor.logicTrack.IsFree(carsToIgnore)) { Terminal.Log($"{neighbor.logicTrack.ID.FullID} not free"); continue; } //if we could go through junction directly (without reversing) bool isDirect = current.CanGoToDirectly(prev, neighbor); if (!allowReverse && !isDirect) { Terminal.Log($"{neighbor.logicTrack.ID.FullID} reverse needed"); continue; } // compute exact cost //double newCost = costSoFar[current] + neighbor.logicTrack.length; double newCost = costSoFar[current] + neighbor.logicTrack.length / neighbor.GetAverageSpeed(); if (!isDirect) { // if we can't fit consist on this track to reverse, drop this neighbor if (prev != null && !current.IsDirectLengthEnough(prev, consistLength)) { Terminal.Log($"{neighbor.logicTrack.ID.FullID} not long enough to reverse"); continue; } //add penalty when we must reverse //newCost += 2.0 * consistLength + 30.0; } // If there's no cost assigned to the neighbor yet, or if the new // cost is lower than the assigned one, add newCost for this neighbor if (!costSoFar.ContainsKey(neighbor) || newCost < costSoFar[neighbor]) { // If we're replacing the previous cost, remove it if (costSoFar.ContainsKey(neighbor)) { costSoFar.Remove(neighbor); cameFrom.Remove(neighbor); } //Terminal.Log($"neighbor {neighbor.logicTrack.ID.FullID} update {newCost}"); costSoFar.Add(neighbor, newCost); cameFrom.Add(neighbor, current); double priority = newCost + Heuristic(neighbor, goal) / 20.0f; //convert distance to time (t = s / v) queue.Enqueue(new RailTrackNode(neighbor), priority); } } } await Await.UnitySyncContext(); }