internal static IReadOnlyList <T> KnnSearch <T>(this RBush <T> tree, object query, int n, Func <T, bool> predicate = null, double maxDist = -1) where T : ISpatialData { var distCalculator = new DistanceToSpatialCalculator(); if (maxDist > 0) { maxDist = maxDist * maxDist; //compare quadratic distances } List <T> result = new List <T>(); //priority queue var queue = new C5.IntervalHeap <IDistanceToSpatial>(new DistComparer()); RBush <T> .Node node = tree.Root; while (node != null) { foreach (ISpatialData child in node.Children) //for each child { var childDistData = distCalculator.CalculateDistanceToSpatial(query, child); //calc distance to box if (maxDist < 0 || childDistData.SquaredDistanceToBox <= maxDist) //check if distance less than max distance { queue.Add(childDistData); //add to queue } } //dequeue all objects that are items stored in RBush while (queue.Count > 0 && queue.FindMin().SpatialData is T) { var candidateWr = queue.DeleteMin(); //this item goes to result T candidate = (T)candidateWr.SpatialData; if (predicate == null || predicate.Invoke(candidate)) //if element satisfy the condition { result.Add(candidate); //add to result } if (n > 0 && result.Count == n) //if the desired amount is already in the result { return(result); //return result } } //process next element in queue if (queue.Count > 0) { node = queue.DeleteMin().SpatialData as RBush <T> .Node; } else { node = null; } } return(result); }
/// <summary> /// Search k nearest neighbors to given point or to to given line segment /// </summary> /// <typeparam name="T">type of RBush items</typeparam> /// <param name="tree">RBush object</param> /// <param name="x1">x coordinate of query point. /// Or if x2 and y2 not null, x coordinate of first endpoint of query line segment</param> /// <param name="y1">y coordinate of query point. /// Or if x2 and y2 not null, y coordinate of first endpoint of query line segment</param> /// <param name="n">number of nearest neighbors to get</param> /// <param name="predicate">condition for neighbors</param> /// <param name="maxDist">max distance for nearest neighbors</param> /// <param name="x2">if not null is x coordinate of second endpoint of query line segment</param> /// <param name="y2">if not null is y coordinate of second endpoint of query line segment</param> /// <returns></returns> public static IReadOnlyList <T> KnnSearch <T>(this RBush <T> tree, double x1, double y1, int n, Func <T, bool> predicate = null, double maxDist = -1, double?x2 = null, double?y2 = null) where T : ISpatialData { if (maxDist > 0) { maxDist = maxDist * maxDist; //All distances are quadratic!!! } List <T> result = new List <T>(); //priority queue C5.IntervalHeap <SpatialDataWrapper> queue = new C5.IntervalHeap <SpatialDataWrapper>(new DistComparer()); RBush <T> .Node node = tree.Root; while (node != null) { foreach (ISpatialData child in node.Children) //for each child { SpatialDataWrapper childDistData = new SpatialDataWrapper(child, x1, y1, x2, y2); //calc distance to box if (maxDist < 0 || childDistData.SquaredDistanceToBox <= maxDist) //check if distance less than max distance { queue.Add(childDistData); //add to queue } } //dequeue all objects that are items stored in RBush while (queue.Count > 0 && queue.FindMin().SpatialData is T) { SpatialDataWrapper candidate = queue.DeleteMin(); //this item goes to result T _candidate = (T)candidate.SpatialData; if (predicate == null || predicate.Invoke(_candidate)) //if element satisfy the condition { result.Add(_candidate); //add to result } if (n > 0 && result.Count == n) //if the desired amount is already in the result { return(result); //return result } } //process next element in queue if (queue.Count > 0) { node = queue.DeleteMin().SpatialData as RBush <T> .Node; } else { node = null; } } return(result); }
public static Network Bfs(Network start, int target) { Network solution = null; var fringe = new C5.IntervalHeap <Network>( // Use this heuristic for picking the next // item to process new NetworkHeuristic(), C5.MemoryType.Normal ); var discovered = new HashSet <string>(); fringe.Add(start); while (solution == null && fringe.Count > 0) { // Find network with lowest heuristic value var network = fringe.FindMin(); fringe.DeleteMin(); if (network == null) { continue; } #if VERBOSE Console.WriteLine(network.GetInvariant()); if (fringe.Count % 100 == 0) { Console.WriteLine($"Progress Report:\n FringeSize: {fringe.Count}\n Discovered: {discovered.Count}"); } #endif // Solution is found when network is solved // (e.g. has one node) and meets target depth if (network.Solved) { if (network.Depth <= target) { solution = network; } } // Seen this, store invariant discovered.Add(network.GetInvariant()); foreach (var child in network.GetChildren()) { var childinv = child.GetInvariant(); // Dont process if // - seen already // - cannot meet target because of constraints if (!discovered.Contains(childinv) && child.CanReachTarget(target)) { fringe.Add(child); } } } return(solution); }
//private void Triangulate() //{ // List<TriangulationPoint> points = new List<TriangulationPoint>(); // foreach (Vertex data in VertexList) // points.Add(new TriangulationPoint(data.X, data.Y)); // PointSet set = new PointSet(points); // IList<PolygonPoint> ppoint = new List<PolygonPoint>(); // foreach (Vertex data in VertexList) // ppoint.Add(new PolygonPoint(data.X, data.Y)); // Polygon pol = new Polygon(ppoint); // try // { // P2T.Triangulate(set); // IList<DelaunayTriangle> triangles = set.Triangles; // foreach (DelaunayTriangle angle in triangles) // { // FixedArray3<TriangulationPoint> point = angle.Points; // EdgeList.Add(new Edge(new Vertex((int) point[0].X, (int) point[0].Y), new Vertex((int) point[1].X, (int) point[1].Y))); // EdgeList.Add(new Edge(new Vertex((int) point[2].X, (int) point[2].Y), new Vertex((int) point[1].X, (int) point[1].Y))); // EdgeList.Add(new Edge(new Vertex((int) point[0].X, (int) point[0].Y), new Vertex((int) point[2].X, (int) point[2].Y))); // Debug.WriteLine(String.Format("X:{0}, Y:{0}", point[0].X, point[0].Y)); // //if (VertexList.Contains(aaa)) // // Debug.WriteLine("ada yang sama"); // } // } // catch (Exception ex) // { // Debug.WriteLine(ex.StackTrace); // } //} //public int Distance(Vertex a, Vertex b) //{ // return (int)(Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2))); //} #endregion #region MST With Edge public void GenerateKruskalMST() { ClearSolution(); if (VertexList.Count == 0) { return; } DisjointSet <Vertex> disjointSet = new DisjointSet <Vertex>(VertexList); C5.IPriorityQueue <Edge> heap = new C5.IntervalHeap <Edge>(new EdgeComparer()); foreach (Edge e in EdgeList) { heap.Add(e); } while (heap.Count > 0 && disjointSet.Count > 0) { Edge s = heap.DeleteMin(); if (!disjointSet.IsSameSet(s.VertexFirst, s.VertexSecond)) { disjointSet.Union(s.VertexFirst, s.VertexSecond); EdgeSolution.Add(s); } } }
/// <summary> /// Dequeues the highest priority events /// </summary> /// <param name="count">Maximum number of events to dequeue</param> public void DequeueEvents(int count) { Dictionary <string, List <QueuedInterestListEvent> > dequeued = new Dictionary <string, List <QueuedInterestListEvent> >(); List <QueuedInterestListEvent> events; // Fetch the requested number of events, or as many as are available. // Put them in collections sorted by event type lock (m_syncRoot) { while (!m_eventHeap.IsEmpty && count-- > 0) { QueuedInterestListEvent qile = m_eventHeap.DeleteMin(); m_eventIDs.Remove(qile.Event.ID); if (!dequeued.TryGetValue(qile.Event.Type, out events)) { events = new List <QueuedInterestListEvent>(); dequeued.Add(qile.Event.Type, events); } events.Add(qile); } } // Fire a SendEventCallback for each unique event type foreach (List <QueuedInterestListEvent> eventsList in dequeued.Values) { QueuedInterestListEvent[] eventsArray = eventsList.ToArray(); InterestListEventHandler handler = eventsArray[0].Handler; handler.SendCallback(eventsArray, m_presence); } }
private (Point3D, Point3D) FindClosestPointNearNotVoidedVoxels(List <Point3D> points) { var bot = _scene.GetFirstBot(); var pointsSet = new C5.IntervalHeap <Vector3D>( new PriorityQueueComparer(Vector3D.FromPoint(bot.Current)) ); var map = new Dictionary <Point3D, Point3D>(); var boundBox = _scene.PadBoundBox; foreach (var pointToFill in points) { foreach (var candidatePoint in EnumeratePointsToVoidFrom(pointToFill)) { if (!boundBox.Contains(candidatePoint)) { continue; } if (_scene.SceneState.Matrix[candidatePoint] == VoxelStatus.Full) { continue; } map[candidatePoint] = pointToFill; pointsSet.Add(Vector3D.FromPoint(candidatePoint)); } } var pointToMove = pointsSet.DeleteMin().ToPoint(); return(map[pointToMove], pointToMove); }
public static List <Directions> AStar(Board board, Func <Board, int> heuristic) { if (board.IsGoal()) { return(new List <Directions>()); } var queue = new C5.IntervalHeap <Board>(100, new BoardComparer(heuristic)) { board }; HashSet <string> memo = new HashSet <string>(); while (!queue.IsEmpty) { Board current = queue.DeleteMin(); foreach (var element in current.Neighbors()) { if (element.IsGoal()) { return(element.Path); } if (!memo.Contains(element.BoardToString)) { queue.Add(element); memo.Add(element.BoardToString); } } } return(null); }
/// <summary> /// Get line path without care obstacle. /// </summary> /// <param name="startTile">Start tile positon</param> /// <param name="endTile">End tile positon</param> /// <param name="maxTry">Max strep</param> /// <param name="tilePositon">If true, path position in path list is tile position.Otherwise is postion in world.</param> /// <returns>Path positition in world list.</returns> public static LinkedList <Vector2> GetLinePath(Vector2 startTile, Vector2 endTile, int maxTry, bool tilePositon = false) { if (startTile == endTile) { return(null); } var path = new LinkedList <Vector2>(); var frontier = new C5.IntervalHeap <Node <Vector2> >(); frontier.Add(new Node <Vector2>(startTile, 0f)); while (!frontier.IsEmpty) { if (maxTry-- < 0) { break; } var current = frontier.DeleteMin().Location; path.AddLast(tilePositon? current : MapBase.ToPixelPosition(current)); if (current == endTile) { break; } foreach (var neighbor in FindAllNeighbors(current)) { frontier.Add(new Node <Vector2>(neighbor, GetTilePositionCost(neighbor, endTile))); } } return(path); }
public override IList <Node> Solve() { C5.IntervalHeap <Node> heap = new C5.IntervalHeap <Node>(Comparer <Node> .Create((Node f, Node s) => (f.TotalCost).CompareTo(s.TotalCost))); HashSet <Tuple <int, int> > set = new HashSet <Tuple <int, int> >(); for (int i = 0; i < Initial.GetLength(0); i++) { for (int j = 0; j < Initial.GetLength(1); j++) { set.Add(new Tuple <int, int>(i, j)); } } var ini = new Node(set) { Board = Initial }; heap.Add(ini); ISet <Node> visited = new HashSet <Node>(); while (!heap.IsEmpty) { var n = heap.DeleteMin(); if (n.IsFinal) { return(n.Parents); } visited.Add(n); n.GenerateChildren().Where(e => !visited.Contains(e)).ToList().ForEach(e => heap.Add(e)); } return(new List <Node>()); }
public KeyValuePair <TKey, TValue> PopMin() { var item = _heap.DeleteMin(); _ = _handles.Remove(item.Key); _ = _dict.Remove(item.Key); return(item); }
/// <summary> /// Removes and returns the minimum item /// </summary> /// <returns></returns> public T DeleteMin() { IndexedItem item = _priQueue.DeleteMin(); _index.Remove(item.Key); return(item.Value); }
public List <Vector3> AStar(Vector3 goal) { ClearNodeData(); foreach (Node n in nodes) { n.SetCostToGo(goal); } Node startN = GetNode(start); Node endN = GetNode(goal); if (startN == null || endN == null) { return(null); } var leafsPQueue = new C5.IntervalHeap <Node>(); // Priority Queue startN.costSoFar = 0; leafsPQueue.Add(startN); while (!leafsPQueue.IsEmpty) { Node leaf = leafsPQueue.FindMin(); leafsPQueue.DeleteMin(); if (leaf == endN) { break; } foreach (Node neighbor in leaf.neighbors) { int newCostSoFar = leaf.costSoFar + Game1.world.cubeDist(leaf.pos, neighbor.pos); if (neighbor.costSoFar > newCostSoFar) { neighbor.prev = leaf; neighbor.costSoFar = newCostSoFar; leafsPQueue.Add(neighbor); } } } if (endN.prev != null) { List <Vector3> result = new List <Vector3>(); Node node = endN; while (node.prev != null) { result.Add(node.prev.cube.gridPos); node = node.prev; } result.Reverse(); return(result); } else { return(null); } }
public static void DijstraSPA(int[,] Graph, int startNode, int EndNode) { char[] Color = new char[size]; Distance = new int[size]; Parent = new int?[size]; for (int v = 0; v < size; v++) { Color[v] = 'w'; Distance[v] = big; } var minHeap = new C5.IntervalHeap <Tuple <int, int> >(); Distance[startNode - 1] = 0; Parent[startNode - 1] = null; Color[startNode - 1] = 'g'; minHeap.Add(new Tuple <int, int>(Distance[startNode - 1], startNode)); while (!minHeap.IsEmpty) { int selMin = minHeap.FindMin().Item2; minHeap.DeleteMin(); // Black node = out of the heap for examination Color[selMin - 1] = 'b'; //if (selMin == EndNode) //{ // return Parent; //} for (int i = 0; i < size; i++) { if (Color[i] == 'b') { continue; } int currentNodeDist = Distance[selMin - 1]; int selectionDist = Distance[i]; int potentialDist = EdgeWeight[selMin - 1, i].Value; if (potentialDist == big) { continue; } if (currentNodeDist + potentialDist < selectionDist) { Distance[i] = currentNodeDist + potentialDist; Parent[i] = selMin; minHeap.Add(new Tuple <int, int>(Distance[i], i + 1)); Color[i] = 'g'; } } // for } // while } // DijstraSPA
public static IEnumerable <IPathNode <N> > GetReachableNodes <N>(N start, float maxCost) where N : INode <N> { C5.IDictionary <N, PathNode <N> > nodeDictionary = new C5.HashDictionary <N, PathNode <N> >(); C5.IPriorityQueue <PathNode <N> > openSet = new C5.IntervalHeap <PathNode <N> >(new PathNodeComparer <N>(), C5.MemoryType.Normal); C5.ICollection <N> closedSet = new C5.HashSet <N>(); C5.ArrayList <IPathNode <N> > res = new C5.ArrayList <IPathNode <N> >(C5.MemoryType.Normal); PathNode <N> curNode = new PathNode <N>(start); curNode.g = 0; nodeDictionary.Add(start, curNode); while (true) { res.Add(curNode); foreach (IEdge <N> edge in curNode.node) { N other = edge.GetEnd(); if (!closedSet.Contains(other)) { PathNode <N> otherNode = null; if (!nodeDictionary.Find(ref other, out otherNode)) { otherNode = new PathNode <N>(other); nodeDictionary.Add(other, otherNode); } float newG = edge.GetCost() + curNode.g; if (otherNode.g > newG) { otherNode.g = newG; if (otherNode.queueHandle != null) { openSet.Replace(otherNode.queueHandle, otherNode); } otherNode.prev = curNode; } if (otherNode.queueHandle == null) { C5.IPriorityQueueHandle <PathNode <N> > handle = null; openSet.Add(ref handle, otherNode); otherNode.queueHandle = handle; } } } if (openSet.IsEmpty) { return(res); } closedSet.Add(curNode.node); curNode = openSet.DeleteMin(); if (curNode.g > maxCost) { return(res); } } }
public void GenerateEuclideanKruskalMST() { if (VertexList.Count == 0) { return; } DateTime start = DateTime.Now; DisjointSet <Vertex> disjointSet = new DisjointSet <Vertex>(VertexList); EdgeSolution.Clear(); C5.IPriorityQueue <Edge> heap = new C5.IntervalHeap <Edge>(new EdgeComparer()); //Triangulate(); //DelaunayTriangulation2d triangulator = new DelaunayTriangulation2d(); //IList<Triangle> triangles = triangulator.Triangulate(new List<Vertex>(VertexList)); //foreach (Triangle triangle in triangles) //{ // Edge e = new Edge(triangle.Vertex1, triangle.Vertex2); // heap.Add(e); // EdgeList.Add(e); // e = new Edge(triangle.Vertex1, triangle.Vertex3); // heap.Add(e); // EdgeList.Add(e); // e = new Edge(triangle.Vertex3, triangle.Vertex2); // heap.Add(e); // EdgeList.Add(e); //} //this.VisitedVertex = VertexList.Count; for (int i = 0; i < VertexList.Count - 1; i++) { for (int j = i + 1; j < VertexList.Count; j++) { Edge e = new Edge(VertexList[i], VertexList[j]); heap.Add(e); } } while (heap.Count > 0 && disjointSet.Count > 0) { Edge s = heap.DeleteMin(); if (!disjointSet.IsSameSet(s.VertexFirst, s.VertexSecond)) { disjointSet.Union(s.VertexFirst, s.VertexSecond); EdgeSolution.Add(s); } } Debug.WriteLine((DateTime.Now - start).TotalMilliseconds + " ms"); //this.VisitedVertex = VertexList.Count; }
public List <Vector2> FindPath(Vector2 start, Vector2 goal, Predicate <Tile> blockedFilter) { if (start == goal || !IsOnMap(goal) || !blockedFilter(GetTileByPosition(goal))) { return(null); } var exactCosts = new Dictionary <Vector2, double>(); var estimates = new Dictionary <Vector2, double>(); var openNodes = new C5.IntervalHeap <Vector2>(new AStarComparer(estimates)); var closedNodes = new HashSet <Vector2>(); var path = new Dictionary <Vector2, Vector2>(); openNodes.Add(start); exactCosts[start] = 0d; while (!openNodes.IsEmpty) { var currentCheapest = openNodes.DeleteMin(); if (currentCheapest == goal) { return(ReconstructPath(path, currentCheapest)); } closedNodes.Add(currentCheapest); var neighbors = MathUtils.GetAdjacentPoints(currentCheapest); foreach (var neighbor in neighbors) { var neighborTile = GetTileByPosition(neighbor); if (closedNodes.Contains(neighbor) || IsOnMap(neighbor) && neighborTile.Obstructed) { continue; } var tentativeScore = exactCosts[currentCheapest] + CalculateDistance(currentCheapest, neighbor); if (!estimates.ContainsKey(neighbor)) { openNodes.Add(neighbor); } else if (tentativeScore >= exactCosts[neighbor]) { continue; } var heuristicScore = tentativeScore + EstimateDistance(neighbor, goal); estimates[neighbor] = heuristicScore; exactCosts[neighbor] = tentativeScore; path[neighbor] = currentCheapest; } } return(null); }
public void GenerateEuclideanOurNeoKruskalMST(int bucket) { if (VertexList.Count == 0) { return; } DateTime start = DateTime.Now; NeoDisjointSet <Vertex> disjointSet = new NeoDisjointSet <Vertex>(VertexList); EdgeSolution.Clear(); C5.IPriorityQueue <Edge>[] heap2 = new C5.IntervalHeap <Edge> [bucket]; C5.IPriorityQueue <Edge> heap = new C5.IntervalHeap <Edge>(new EdgeComparer()); for (int i = 0; i < heap2.Length; i++) { heap2[i] = new C5.IntervalHeap <Edge>(new EdgeComparer()); } //this.VisitedVertex = VertexList.Count; for (int i = 0; i < VertexList.Count - 1; i++) { for (int j = i + 1; j < VertexList.Count; j++) { Edge e = new Edge(VertexList[i], VertexList[j]); heap.Add(e); } } double max = heap.FindMax().Length; double min = heap.FindMin().Length; while (heap.Count > 0) { Edge s = heap.DeleteMin(); heap2[(int)Math.Floor((((s.Length - min) / (max - min)) * (bucket - 1)))].Add(s); } for (int i = 0; i < bucket && disjointSet.Count > 0; i++) { while (heap2[i].Count > 0 && disjointSet.Count > 0) { Edge s = heap2[i].DeleteMin(); if (!disjointSet.IsSameSet(s.VertexFirst, s.VertexSecond)) { disjointSet.Union(s.VertexFirst, s.VertexSecond); EdgeSolution.Add(s); } } } Debug.WriteLine((DateTime.Now - start).TotalMilliseconds + " ms"); //this.VisitedVertex = VertexList.Count; }
private void SetCurrentNode() { // get the best node, remove from open nodes, set to closed CurrentMetaData = OpenNodes.DeleteMin(); ClosedCount++; CurrentMetaData.Status = NodeStatus.Closed; // check if this node is closest to the target if (CurrentMetaData.ToCost < _closest.ToCost) { _closest = CurrentMetaData; } }
public static LinkedList<Node> BuildNodePathUsingAStarWithoutSystem(Node Start, Node Goal, HeuristicCalculator Calculator, NextStatesProvider Provider) { int expandedNodesCount = 0; bool found = false; TreeNode start = new TreeNode(Start); TreeNode goal = new TreeNode(null); var toExpand = new C5.IntervalHeap<TreeNode>(new TreeNodeCostComparer()); var discoveredTreeNodes = new Dictionary<int, TreeNode>(); start.Cost = Calculator.Calculate(Start.State, Goal.State); toExpand.Add(start); discoveredTreeNodes.Add(start.Node.State.GetHashCode(), start); while (toExpand.Count > 0) { TreeNode Current = toExpand.FindMin(); toExpand.DeleteMin(); if (Current.Node.State.Equals(Goal.State)) { goal = Current; found = true; break; } expandedNodesCount++; foreach (var nextNode in Provider.GetNodesWithNextStates(Current.Node)) { double Cost = Calculator.Calculate(nextNode.State, Goal.State); if (!discoveredTreeNodes.ContainsKey(nextNode.State.GetHashCode())) { TreeNode Tem = new TreeNode(nextNode, Current, Current.Cost + Cost); Current.Children.AddLast(Tem); discoveredTreeNodes.Add(nextNode.State.GetHashCode(), Tem); toExpand.Add(Tem); } else { CheckIfBetterPathWasFound(discoveredTreeNodes, Current, nextNode, Cost); } } } Console.WriteLine("Expanded {0} nodes", expandedNodesCount); if (!found) throw new PathNotFoundException("Path not found!"); return createPath(goal, start); }
protected void StoreSARS(SARS sars) { if (PrioritySweeping) { PutPredecessor(sars); EnqueueSARS(sars); while (Pq.Count > MaxStoreSize) { Pq.DeleteMin(); } } else { Qexp.Store(sars, MaxStoreSize); } }
/// <summary> /// Checks for retries that are ready to be applied /// </summary> private void CheckForReadyRetries() { List <DelayedMutation> needsRetry = new List <DelayedMutation>(); while (_delayedMutations.Count > 0) { DelayedMutation mut; lock (_delayedMutations) { mut = _delayedMutations.FindMin(); if (mut.ReadyOn > DateTime.Now) { break; } _delayedMutations.DeleteMin(); } try { mut.Execute(); } catch (Exception e) { _log.ErrorFormat("[Inworldz.Data.Inventory.Cassandra] Error while applying mutation {0} on retry {1}: {2}", mut.Identifier, mut.RetryCount + 1, e); mut.RetryCount++; if (mut.RetryCount == MAX_RETRIES) { _log.ErrorFormat("[Inworldz.Data.Inventory.Cassandra] CRITICAL: Retry limit reached, discarding mutation {0}", mut.Identifier); } else { needsRetry.Add(mut); } } } foreach (DelayedMutation delayedMutation in needsRetry) { ScheduleMutationRetry(delayedMutation); } }
/// <summary> /// Test whether can view target within vision radius. /// </summary> /// <param name="startTile">Viewer tile position.</param> /// <param name="endTile">Target tile position.</param> /// <param name="visionRadius">Viewr vision radius</param> /// <returns>True if can view target without map obstacle.Otherwise false.</returns> public static bool CanViewTarget(Vector2 startTile, Vector2 endTile, int visionRadius) { const int maxVisionRadious = 80; if (visionRadius > maxVisionRadious) { //Vision radius is too big, for performace reason return false. return(false); } if (startTile != endTile) { if (MapBase.Instance.IsObstacleForMagic(endTile)) { return(false); } var path = new LinkedList <Vector2>(); var frontier = new C5.IntervalHeap <Node <Vector2> >(); frontier.Add(new Node <Vector2>(startTile, 0f)); while (!frontier.IsEmpty) { var current = frontier.DeleteMin().Location; if (current == endTile) { return(true); } if (MapBase.Instance.IsObstacle(current) || visionRadius < 0) { return(false); } path.AddLast(current); foreach (var neighbor in FindAllNeighbors(current)) { frontier.Add(new Node <Vector2>(neighbor, GetTilePositionCost(neighbor, endTile))); } visionRadius--; } return(false); } return(true); }
//Returned path is in pixel position public static LinkedList <Vector2> FindPathSimple(Character finder, Vector2 startTile, Vector2 endTile, int maxTry) { if (startTile == endTile) { return(null); } if (MapBase.Instance.IsObstacleForCharacter(endTile)) { return(null); } var cameFrom = new Dictionary <Vector2, Vector2>(); var frontier = new C5.IntervalHeap <Node <Vector2> >(); frontier.Add(new Node <Vector2>(startTile, 0f)); var tryCount = 0; while (!frontier.IsEmpty) { if (tryCount++ > maxTry) { break; } var current = frontier.DeleteMin().Location; if (current == endTile) { break; } if (finder.HasObstacle(current) && current != startTile) { continue; } foreach (var neighbor in FindNeighbors(current, finder.CanMoveDirCount)) { if (!cameFrom.ContainsKey(neighbor)) { var priority = GetTilePositionCost(neighbor, endTile); frontier.Add(new Node <Vector2>(neighbor, priority)); cameFrom[neighbor] = current; } } } return(GetPath(cameFrom, startTile, endTile)); }
public void MergeChunks(string outputFilePath, string[] chunkFiles) { Console.WriteLine(nameof(PriorityQueueMergeStrategy)); using (var writer = File.CreateText(outputFilePath)) { List<StreamReader> readers = chunkFiles.Select(s => new StreamReader(s, Encoding.UTF8)).ToList(); var priorityQueue = new C5.IntervalHeap<string>(_comparer); var readerForHandle = new Dictionary<C5.IPriorityQueueHandle<string>, StreamReader>(); // Fill priority queue with initial strings. Each string is associated with reader it came from readers.ForEach(reader => { C5.IPriorityQueueHandle<string> handle = null; priorityQueue.Add(ref handle, reader.ReadLine()); readerForHandle[handle] = reader; }); // Get minimal element from queue and read the next line from respective stream // repeat until all streams are exhausted while (!priorityQueue.IsEmpty) { C5.IPriorityQueueHandle<string> handleMin; string line = priorityQueue.DeleteMin(out handleMin); StreamReader reader = readerForHandle[handleMin]; readerForHandle.Remove(handleMin); writer.WriteLine(line); string nextLine = reader.ReadLine(); if (nextLine != null) { C5.IPriorityQueueHandle<string> handle = null; priorityQueue.Add(ref handle, nextLine); readerForHandle[handle] = reader; } } readers.ForEach(r => r.Dispose()); } chunkFiles.ForEach(f => File.Delete(f)); }
public static ImageNode CropFrameInTiles(Bitmap bp) { int[] ymax = new int[bp.Width]; int[] ymin = new int[bp.Width]; getMinMaxH(bp, ymax, ymin); LinkedList <ImageNode> imns = ImageNode.GetNodes(ymin, ymax); C5.IntervalHeap <ImageNode> ih = new C5.IntervalHeap <ImageNode>(); foreach (ImageNode imn in imns) { ih.Add(imn); } List <ImageNode> close = new List <ImageNode>(); ImageNode imnaux; LinkedList <ImageNode> newnodes; while (ih.Count > 0) { imnaux = ih.DeleteMin(); close.Add(imnaux); newnodes = ImageNode.GetNodes(imnaux); if (newnodes == null) { return(imnaux); } else { foreach (ImageNode imn in newnodes) { ih.Add(imn); } } } return(null); }
private static void PrimMain() { while (Heap.Count > 0) { p z = Heap.FindMin(); int u = z.v; int du = z.w; Heap.DeleteMin(); if (!kt[u]) { continue; } kt[u] = false; d[u] = 0; if (u != S) { SaveGraph(trace[u]); } foreach (p canh in listCanh[u]) { int v = canh.v; int w = canh.w; if (d[v] > d[u] + w) { d[v] = d[u] + w; trace[v] = canh.ID; p xx = new p(); xx.v = v; xx.w = d[v]; Heap.Add(xx); } } } }
private static void DijkstraMain() { while (Heap.Count > 0) { p z = Heap.FindMin(); int u = z.v; int du = z.w; Heap.DeleteMin(); if (!kt[u]) { continue; } kt[u] = false; SaveGraph(u); if (u == T) { return; } foreach (p canh in listCanh[u]) { int v = canh.v; int w = canh.w; if (d[v] > d[u] + w) { d[v] = d[u] + w; trace[v] = u; p xx = new p(); xx.v = v; xx.w = d[v]; Heap.Add(xx); } } } }
public void AnalyzeQueries() { while (queries.Count > 0) { int rating; var query = queries.DeleteMin(); // Get first-priority queries first //var cacheElement = stock2rating.Find(x => x.StockID == query.StockID); if (stock2rating.ContainsKey(query.StockID)) { rating = stock2rating[query.StockID]; } else { rating = CalculateRating(query.StockID); stock2rating.Add(query.StockID, rating); } if (rating > 80) // Let's say that a rating of 80 triggers a stock trade { stockTrader.EnqueueStockForTrading(query); } } }
//Returned path is in pixel position public static LinkedList <Vector2> FindPathPerfect(Character finder, Vector2 startTile, Vector2 endTile, int maxTryCount) { if (startTile == endTile) { return(null); } if (MapBase.Instance.IsObstacleForCharacter(endTile)) { return(null); } var cameFrom = new Dictionary <Vector2, Vector2>(); var costSoFar = new Dictionary <Vector2, float>(); var frontier = new C5.IntervalHeap <Node <Vector2> >(); frontier.Add(new Node <Vector2>(startTile, 0f)); costSoFar[startTile] = 0f; var tryCount = 0; //For performance //Decrease max try count when fps low //switch ((Fps.FpsValue+5)/10) //{ // case 5: // maxTryCount = 30; // break; // case 4: // case 2: // case 1: // case 0: // maxTryCount = 15; // break; //} while (!frontier.IsEmpty) { if (maxTryCount != -1 && tryCount++ > maxTryCount) { break; } var current = frontier.DeleteMin().Location; if (current.Equals(endTile)) { break; } if (finder.HasObstacle(current) && current != startTile) { continue; } foreach (var next in FindNeighbors(current, finder.CanMoveDirCount)) { var newCost = costSoFar[current] + GetTilePositionCost(current, next); if (!costSoFar.ContainsKey(next) || newCost < costSoFar[next]) { costSoFar[next] = newCost; var priority = newCost + GetTilePositionCost(endTile, next); frontier.Add(new Node <Vector2>(next, priority)); cameFrom[next] = current; } } } return(GetPath(cameFrom, startTile, endTile));; }
public static void Dijkstra(DrawingSurface ds, int start) { if (ds.ContainsNegativeEdge()) { var err = new ErrorBox("Graph contains negative edges"); err.ShowDialog(); return; } Dictionary <int, List <Edge> > adjList = ds.GetDestAdjList(); var que = new C5.IntervalHeap <Edge>(new EdgeCompare()); double[] dist = Enumerable.Repeat((double)int.MaxValue, adjList.Count).ToArray(); int[] parent = Enumerable.Repeat(-1, adjList.Count).ToArray(); var visitedColor = Color.Green; var processedColor = Color.Yellow; var currentEdgeColor = Color.Red; que.Add(new Edge(ds.Vertices[start], ds.Vertices[start], 0, true)); dist[start] = 0; for (var i = 0; i < ds.Vertices.Count; ++i) { ds.Vertices[i].label = "INF"; } ds.Vertices[start].label = "0"; while (que.Count != 0) { var currEdge = que.FindMin(); que.DeleteMin(); var startVertex = (ds.Vertices[currEdge.end].fillColor == visitedColor) ? currEdge.start : currEdge.end; ReDrawCircle(ds, startVertex, visitedColor); foreach (var edge in adjList[startVertex]) { var currVertex = (edge.end != startVertex) ? edge.end : edge.start; ReDrawEdge(ds, edge, currentEdgeColor, 300); if (ds.Vertices[currVertex].fillColor != visitedColor) { ReDrawCircle(ds, currVertex, processedColor, 0); } Thread.Sleep(1000); if (dist[currVertex] > dist[startVertex] + edge.w) { dist[currVertex] = dist[startVertex] + edge.w; parent[currVertex] = startVertex; ds.Vertices[currVertex].label = $"{Math.Round(dist[currEdge.end], 3)}+{Math.Round(edge.w, 3)}"; ds.Invalidate(); Thread.Sleep(1000); que.Add(edge); } ds.Vertices[currVertex].label = (dist[currVertex] == int.MaxValue) ? "INF" : $"{dist[currVertex]}"; ReDrawEdge(ds, edge, Color.Gray, 0); } } msg.SetTitle("Dijkstra algorithm result:"); msg.MoveToCorner(ds.FindForm() as Form1); msg.StartMenu(); for (var target = 0; target < adjList.Count; ++target) { if (start != target) { var currWay = GetWay(start, target, parent); PrintWay(currWay, dist[target]); Thread.Sleep(1000); } } msg.WaitOne(); ClearEdges(ds); ClearVertices(ds); }
/// <summary> /// Finds a path between startPoint and endPoint. /// </summary> /// <param name="startPoint">Starting point</param> /// <param name="endPoint">Ending point</param> /// <returns>Path that connects start and end point</returns> public Path FindPath(Point startPoint, Point endPoint) { if (this.Collides(startPoint) || this.Collides(endPoint)) { return null; } this.StartPoint = startPoint; this.EndPoint = endPoint; // initialize the open set of nodes with start point C5.IPriorityQueue<State> openSet = new C5.IntervalHeap<State>(); // associate handles with points var handles = new Dictionary<Point, C5.IPriorityQueueHandle<State>>(); // add start point to queue and associate point with handle C5.IPriorityQueueHandle<State> handle = null; openSet.Add(ref handle, new State {Point = startPoint, Heuristic = 0}); handles.Add(this.StartPoint, handle); var closedSet = new HashSet<Point>(); // the g-score is the distance from start point to the current point var gScore = new Dictionary<Point, double> {{startPoint, 0}}; // the f-score is the g-score plus heuristic var fScore = new Dictionary<Point, double> {{startPoint, Utils.Distance(startPoint, this.EndPoint)}}; // cameFrom is used to reconstruct path when target is found var cameFrom = new Dictionary<Point, Point>(); // process nodes in the open set... while (!openSet.IsEmpty) { // fetch highest priority node, i.e. the one with // lowest heuristic value State minState = openSet.DeleteMin(); Point x = minState.Point; handles.Remove(x); if (x == this.EndPoint) { // we found a solution return this.ReconstructPath(cameFrom); } closedSet.Add(x); var neighbours = this.GetNeighbours(x); // for each neighbour... foreach (var y in neighbours) { if (closedSet.Contains(y)) { continue; } // total cost from start var tentativeGScore = gScore[x] + Utils.Distance(x, y); bool tentativeIsBetter; Point point = y; handle = null; if (!openSet.Exists(s => s.Point == point) && !closedSet.Contains(y)) { // will get priority adjusted further down openSet.Add(ref handle, new State {Point = point}); handles.Add(point, handle); tentativeIsBetter = true; } else if (tentativeGScore < gScore[y]) { tentativeIsBetter = true; } else { tentativeIsBetter = false; } if (tentativeIsBetter) { // update f-score of y cameFrom[y] = x; gScore[y] = tentativeGScore; fScore[y] = gScore[y] + Utils.Distance(y, this.EndPoint); var heuristic = fScore[y]; // set priority of y if (handle == null) { handle = handles[point]; } openSet.Replace(handle, new State {Heuristic = heuristic, Point = y}); } } } return null; }
public void GenerateEuclideanOurNeoKruskalMST(int bucket) { if (VertexList.Count == 0) return; DateTime start = DateTime.Now; NeoDisjointSet<Vertex> disjointSet = new NeoDisjointSet<Vertex>(VertexList); EdgeSolution.Clear(); C5.IPriorityQueue<Edge>[] heap2 = new C5.IntervalHeap<Edge>[bucket]; C5.IPriorityQueue<Edge> heap = new C5.IntervalHeap<Edge>(new EdgeComparer()); for (int i = 0; i < heap2.Length; i++) { heap2[i] = new C5.IntervalHeap<Edge>(new EdgeComparer()); } //this.VisitedVertex = VertexList.Count; for (int i = 0; i < VertexList.Count - 1; i++) for (int j = i + 1; j < VertexList.Count; j++) { Edge e = new Edge(VertexList[i], VertexList[j]); heap.Add(e); } double max = heap.FindMax().Length; double min = heap.FindMin().Length; while (heap.Count > 0) { Edge s = heap.DeleteMin(); heap2[(int)Math.Floor((((s.Length - min) / (max - min)) * (bucket - 1)))].Add(s); } for (int i = 0; i < bucket && disjointSet.Count > 0; i++) { while (heap2[i].Count > 0 && disjointSet.Count > 0) { Edge s = heap2[i].DeleteMin(); if (!disjointSet.IsSameSet(s.VertexFirst, s.VertexSecond)) { disjointSet.Union(s.VertexFirst, s.VertexSecond); EdgeSolution.Add(s); } } } Debug.WriteLine((DateTime.Now - start).TotalMilliseconds + " ms"); //this.VisitedVertex = VertexList.Count; }
public void GenerateKruskalMST() { ClearSolution(); if (VertexList.Count == 0) return; DisjointSet<Vertex> disjointSet = new DisjointSet<Vertex>(VertexList); C5.IPriorityQueue<Edge> heap = new C5.IntervalHeap<Edge>(new EdgeComparer()); foreach(Edge e in EdgeList) heap.Add(e); while (heap.Count > 0 && disjointSet.Count > 0) { Edge s = heap.DeleteMin(); if (!disjointSet.IsSameSet(s.VertexFirst, s.VertexSecond)) { disjointSet.Union(s.VertexFirst, s.VertexSecond); EdgeSolution.Add(s); } } }
public void GenerateEuclideanKruskalMST() { if (VertexList.Count == 0) return; DateTime start = DateTime.Now; DisjointSet<Vertex> disjointSet = new DisjointSet<Vertex>(VertexList); EdgeSolution.Clear(); C5.IPriorityQueue<Edge> heap = new C5.IntervalHeap<Edge>(new EdgeComparer()); //Triangulate(); //DelaunayTriangulation2d triangulator = new DelaunayTriangulation2d(); //IList<Triangle> triangles = triangulator.Triangulate(new List<Vertex>(VertexList)); //foreach (Triangle triangle in triangles) //{ // Edge e = new Edge(triangle.Vertex1, triangle.Vertex2); // heap.Add(e); // EdgeList.Add(e); // e = new Edge(triangle.Vertex1, triangle.Vertex3); // heap.Add(e); // EdgeList.Add(e); // e = new Edge(triangle.Vertex3, triangle.Vertex2); // heap.Add(e); // EdgeList.Add(e); //} //this.VisitedVertex = VertexList.Count; for (int i = 0; i < VertexList.Count - 1; i++) for (int j = i + 1; j < VertexList.Count; j++) { Edge e = new Edge(VertexList[i], VertexList[j]); heap.Add(e); } while (heap.Count > 0 && disjointSet.Count > 0) { Edge s = heap.DeleteMin(); if (!disjointSet.IsSameSet(s.VertexFirst, s.VertexSecond)) { disjointSet.Union(s.VertexFirst, s.VertexSecond); EdgeSolution.Add(s); } } Debug.WriteLine((DateTime.Now - start).TotalMilliseconds + " ms"); //this.VisitedVertex = VertexList.Count; }
private bool VertexGuidedSearch(int iv, int iw) { var v = _nodes[iv]; var w = _nodes[iw]; f.Clear(); b.Clear(); f.Add(w); w.Value.InF = true; b.Add(v); v.Value.InB = true; w.Value.OutEnum = w.Value.Outgoing.GetEnumerator(); w.Value.OutEnum.MoveNext(); v.Value.InEnum = v.Value.Incoming.GetEnumerator(); v.Value.InEnum.MoveNext(); var fl = new C5.IntervalHeap<SGTNode<HKMSTNode>>(); var bl = new C5.IntervalHeap<SGTNode<HKMSTNode>>(); if (w.Value.OutEnum.Current != null) fl.Add(w); if (v.Value.InEnum.Current != null) bl.Add(v); // For ease of notation, we adopt the convention that the // minimum of an empty set is bigger than any other value and the maximum of an empty // set is smaller than any other value. SGTNode<HKMSTNode> u = null; SGTNode<HKMSTNode> z = null; if(fl.Count > 0) u = fl.FindMin(); if(bl.Count > 0) z = bl.FindMax(); while (fl.Count > 0 && bl.Count > 0 && (u == z || _nodeOrder.Query(z, u))) { // SEARCH-STEP(vertex u, vertex z) var x = u.Value.OutEnum.Current; var y = z.Value.InEnum.Current; u.Value.OutEnum.MoveNext(); z.Value.InEnum.MoveNext(); if (u.Value.OutEnum.Current == null) fl.DeleteMin(); if (z.Value.InEnum.Current == null) bl.DeleteMax(); if(x.Value.InB) { f.ForEach(item => item.Value.InF = false); b.ForEach(item => item.Value.InB = false); return false; // Pair(uz.from, x.Current); } else if (y.Value.InF) { f.ForEach(item => item.Value.InF = false); b.ForEach(item => item.Value.InB = false); return false; // Pair(y.Current, uz.to); } if (!x.Value.InF) { f.Add(x); x.Value.InF = true; x.Value.OutEnum = x.Value.Outgoing.GetEnumerator(); x.Value.OutEnum.MoveNext(); if (x.Value.OutEnum.Current != null) fl.Add(x); } if (!y.Value.InB) { b.Add(y); y.Value.InB = true; y.Value.InEnum = y.Value.Incoming.GetEnumerator(); y.Value.InEnum.MoveNext(); if (y.Value.InEnum.Current != null) bl.Add(y); } // End of SEARCH-STEP(vertex u, vertex z) if (fl.Count > 0) u = fl.FindMin(); if (bl.Count > 0) z = bl.FindMax(); } // let t = min({v}∪{x ∈ F|out(x) = null} and reorder the vertices in F< and B> as discussed previously. var vAndf = f.FindAll(item => item.Value.OutEnum.Current != null); vAndf.Add(v); var t = vAndf.Min(); // Let F< = { x ∈ F | x < t} and var fb = f.FindAll(item => item.Label < t.Label); // B > = { y ∈ B | y > t}. var bf = b.FindAll(item => item.Label > t.Label); if(t == v) { // move all vertices in fb just after t ... bf is empty foreach (var node in fb) _nodeOrder.Remove(node); if(fb.Count > 1) fb = TopoSort(fb); if (fb.Count > 0) { var prev = _nodeOrder.insertAfter(t, fb[0]); for (int i = 1; i < fb.Count; i++) prev = _nodeOrder.insertAfter(prev, fb[i]); } } if (t.Label < v.Label) { // move all vertices in fb just before t and all vertices in bf just before all vertices in fb // This is required as the articles states if (bf.Count > 1) bf = TopoSort(bf); if (fb.Count > 1) fb = TopoSort(fb); foreach (var node in bf) _nodeOrder.Remove(node); foreach (var node in fb) _nodeOrder.Remove(node); foreach (var item in fb) bf.Add(item); if (bf.Count > 0) { var prev = _nodeOrder.insertBefore(t, bf[bf.Count-1]); if(bf.Count > 1) { for (int i = bf.Count - 2; i >= 0; i--) prev = _nodeOrder.insertBefore(prev, bf[i]); } } } // reset bools f.ForEach(item => item.Value.InF = false); b.ForEach(item => item.Value.InB = false); // all done add to Outgoing and Incoming _nodes[iv].Value.Outgoing.Add(_nodes[iw]); _nodes[iw].Value.Incoming.Add(_nodes[iv]); return true; }
/// <summary> /// Find shortest path from one node to one other node /// with modified Dijkstra's DPA /// </summary> /// <param name="graph"></param> /// <param name="startNode"></param> /// <param name="endNode"></param> /// <returns></returns> public static Stack <int> GetSPWithDijkstra(int?[,] graph, int startNode, int endNode) { // Variable declaration and initialization bool[] visited = new bool[size]; int[] distance = new int[size]; int[] parent = new int[size]; Queue <int> ShortestPath = new Queue <int>(); // Assign default values to visited and distance array for (int v = 0; v < size; v++) { visited[v] = false; distance[v] = Helper.big; } // MinHeap for the algorithm // First int is for the distance // Second int is for the node number var minHeap = new C5.IntervalHeap <Tuple <int, int> >(); distance[startNode - 1] = 0; minHeap.Add(new Tuple <int, int>(distance[startNode - 1], startNode)); while (!minHeap.IsEmpty) { int selMin = minHeap.FindMin().Item2; minHeap.DeleteMin(); // This node is being visited for examination visited[selMin - 1] = true; // Found the destination. Terminate if (selMin == endNode) { int current = destination; Stack <int> shortestPathStack = new Stack <int>(); while (current != 0) { shortestPathStack.Push(current); current = parent[current - 1]; } return(shortestPathStack); } for (int i = 0; i < size; i++) { DijkstraCount++; // This node has been visited and examined if (visited[i] == true) { continue; } // There is no edge between the current node and the potential node // Helper.big and null mean the same thing // Prevent overflow If use Helper.big as max int value int?potentialDist = graph[selMin - 1, i]; if (potentialDist == Helper.big || potentialDist == null) { continue; } int currentNodeDist = distance[selMin - 1]; int selectionDist = distance[i]; if (currentNodeDist + potentialDist.Value < selectionDist) { distance[i] = currentNodeDist + potentialDist.Value; parent[i] = selMin; minHeap.Add(new Tuple <int, int>(distance[i], i + 1)); } } // for loop } // while loop // Cannot find the path, return null return(null); } // DijstraSPA