public static Dictionary <TNode, GraphNode <TNode> > MakeMap(HashSet <TNode> startingNodes, IGraph <TNode> graph) { var comparer = new FunctionalComparer <GraphNode <TNode> >((a, b) => - a.distance.CompareTo(b.distance)); IHeap <GraphNode <TNode> > queue = new Heap <GraphNode <TNode> >(comparer); Dictionary <TNode, GraphNode <TNode> > nodeData = new Dictionary <TNode, GraphNode <TNode> >(); graph.GetAllNodes().ForEach(n => { var distance = startingNodes.Contains(n) ? 0f : float.MaxValue; var node = new GraphNode <TNode>(n, distance, null); queue.Add(node); nodeData.Add(n, node); }); while (!queue.IsEmpty()) { var u = queue.RemoveFirst(); graph.GetNeighbors(u.data).ForEach(n => { var v = nodeData[n]; var alt = u.distance + graph.GetEdgeCost(u.data, v.data); if (alt < v.distance) { v.distance = alt; v.parent = u; queue.UpdateItem(v); } }); } return(nodeData); }
/// <param name="intPairComparer">Comparer for two positions passed in function arguments: x1, y1, x2, y2.</param> public PriorityPositionQueue(Func <int, int, int, int, int> intPairComparer) { Func <Position, Position, int> positionComparerFunction = (first, second) => intPairComparer(first.X, first.Y, second.X, second.Y); var positionComparer = new FunctionalComparer <Position>(positionComparerFunction); _intervalHeap = new IntervalHeap <Position>(positionComparer); }
//In order to compare solutions, the set must be the same. Because a Hashset structure is being used,3 //the value are not sorted so when the hash is calculated (from comparisson purposes for example) //the set is coppied to a list, then sorted by the index of the cells in the solution. //This enables the correct comparison of two solutions, because the order is neglected (as they are both sorted) public override int GetHashCode() { //sort cells. Cells are sorted by their index (which must be unique, unsuring a deterministic sorting) var sortedList = this.ToArray(); Array.Sort(sortedList, FunctionalComparer <CellBase> .Create((CellBase cellA, CellBase cellB) => (cellA.Index.x * 1000 + cellA.Index.y > cellB.Index.x * 1000 + cellB.Index.y) ? 1 : (cellA.Index.x * 1000 + cellA.Index.y < cellB.Index.x * 1000 + cellB.Index.y) ? -1 : 0)); //combine hashes //source http://stackoverflow.com/questions/1646807/quick-and-simple-hash-code-combinations int hash = 17; foreach (CellBase cell in sortedList) { hash = hash * 31 + cell.GetHashCode(); } return(hash); }
public void playNextMove(object sender, Board p_Board) { for (int x = 0; x < 18; x++) { for (int y = 0; y < 18; y++) { weights[x * 18 + y] = 0; foreach (Threat t in _threats) { weights[x * 18 + y] += t.match(p_Board, new Vector2(x, y)); } indexes[x * 18 + y] = x * 18 + y; } } Array.Sort(indexes, FunctionalComparer <int> .Create((a, b) => { return(weights[b] - weights[a]); })); int i = 0; while (Arbiter.Instance.input(indexes[i] / 18, indexes[i] % 18) == 0 && i < 324) { i++; } }
public void CompareThreeQueuesPerformance() { var rng = new RandomNumberGenerator(456); int size = 100000; int attempts = 10; int[] input = new int[size]; for (int i = 0; i < size; i++) { input[i] = rng.Next(size); } var oldQueueComparer = FunctionalComparer <PQIndexedObject> .Create((first, second) => first.Priority.CompareTo(second.Priority)); var spatialAStarQueue = new PriorityQueue <PQIndexedObject>(oldQueueComparer, size); var fastQueue = new FastPriorityQueue <FPQNode>(size); var intervalHeapQueue = new IntervalHeap <int>(); var stopwatchOverall = Stopwatch.StartNew(); for (int i = 0; i < attempts; i++) { spatialAStarQueue.Clear(); var stopwatch = Stopwatch.StartNew(); for (int itemIndex = 0; itemIndex < size; itemIndex++) { var oldItem = new PQIndexedObject { Priority = itemIndex }; spatialAStarQueue.Push(oldItem); } Console.WriteLine(" Spatial A* queue " + stopwatch.ElapsedMilliseconds + " ms for " + attempts + " times " + size + " items."); } Console.WriteLine(" Spatial A* queue total: " + stopwatchOverall.ElapsedMilliseconds + " ms for " + size + " items."); stopwatchOverall.Restart(); for (int i = 0; i < attempts; i++) { intervalHeapQueue = new IntervalHeap <int>(); var stopwatch = Stopwatch.StartNew(); for (int itemIndex = 0; itemIndex < size; itemIndex++) { intervalHeapQueue.Add(itemIndex); } Console.WriteLine(" Interval Heap " + stopwatch.ElapsedMilliseconds + " ms for " + attempts + " times " + size + " items."); } Console.WriteLine(" Interval Heap total: " + stopwatchOverall.ElapsedMilliseconds + " ms for " + size + " items."); stopwatchOverall.Restart(); for (int i = 0; i < attempts; i++) { fastQueue.Clear(); var stopwatch = Stopwatch.StartNew(); for (int itemIndex = 0; itemIndex < size; itemIndex++) { var newItem = new FPQNode() { Priority = itemIndex }; fastQueue.Enqueue(newItem, itemIndex); } Console.WriteLine(" Fast queue " + stopwatch.ElapsedMilliseconds + " ms for " + size + " items."); } Console.WriteLine(" Fast queue total: " + stopwatchOverall.ElapsedMilliseconds + " ms for " + attempts + " times " + size + " items."); }