public static List <int> ProcessOutput(TreeDictionary <int, int> summedRangeEdges, List <int> numbers) { var outputs = new List <int>(); var firstSummedRangeEdge = summedRangeEdges.First().Key; foreach (var number in numbers) { if (summedRangeEdges.TryPredecessor(number, out var floorEntry)) { var value = floorEntry.Value; // Use the value of the summed range this number belongs to // If this number is on top of the left boundary of that range, if there is another range before this one, count that as well by adding 1 if (floorEntry.Key != firstSummedRangeEdge && floorEntry.Key == number) { value++; } Debug.WriteLine(value); outputs.Add(value); } else { Debug.WriteLine(0); outputs.Add(0); } } return(outputs); }
// public TreeDictionary<double, IEnumerable<B>> AStar<A, B>(Func<B, AStarNode<A, B>> nextState, A state) { // var openSet = new TreeDictionary<double, B>(); // Known, but unexplored // var closedSet = new TreeDictionary<double, B>(); // Fully explored // // AStarNode<A, B> parentStub = new AStarNode<A, B> (null, null, state); // addChildrenToOpenSet(openSet, parentStub, state, heuristic); // // int maxIters = 100; // int nRepetitions = 5; // // AStarNode<A, B> best; // // int i = 0; // do { // var bestKV = openSet.First (); // openSet.Remove(bestKV.Key); // // best = bestKV.Value; // // var bestNextMove = best.Input; // // // repeat the same input a few times // B resultState = best.Value; // for (int j = 0; j < nRepetitions; j++) { // resultState = nextState(new AStarNode<A, B>(best, bestNextMove, resultState)); // } // // addChildrenToOpenSet(openSet, best, resultState, heuristic); // // var stateNode = new AStarNode<A, B> (best, bestNextMove, resultState); // var score = AStar.addNoise(stateNodeScorer (heuristic, stateNode)); // // closedSet.Add(score, stateNode); // // } while(i++ < maxIters && closedSet.First().Key > 0 && openSet.Count > 0); // // return closedSet; // } // // public List<Tuple<Input, Input>> nextInputsList2(GameState state) { // // } public List<Tuple<Input, Input>> nextInputsList(GameState state) { var openSet = new TreeDictionary<double, StateNode>(); // Known, but unexplored var closedSet = new TreeDictionary<double, StateNode>(); // Fully explored StateNode parentStub = new StateNode (null, Tuple.Create(Input.Noop, Input.Noop), state); addChildrenToOpenSet(openSet, parentStub, state, heuristic); int maxIters = 100; int nRepetitions = 5; StateNode bestOpen; int i = 0; do { var bestOpenKV = openSet.First (); openSet.Remove(bestOpenKV.Key); bestOpen = bestOpenKV.Value; var bestNextMove = bestOpen.Input; // repeat the same input a few times GameState resultState = bestOpen.Value; for (int j = 0; j < nRepetitions; j++) { resultState = AStar.nextState(forwardModel, resultState, bestNextMove.Item1, bestNextMove.Item2); } addChildrenToOpenSet(openSet, bestOpen, resultState, heuristic); var stateNode = new StateNode (bestOpen, bestNextMove, resultState); var score = AStar.addNoise(stateNodeScorer (heuristic, stateNode)); closedSet.Add(score, stateNode); } while(i++ < maxIters && !closedSet.First().Value.Value.PlayStatus.isWon() && openSet.Count > 0); // Debug.WriteLine ("closedSet size: {0}", closedSet.Count); // int k = 0; // foreach (var pth in closedSet) { // var pathStr = string.Join(", ", pth.Value.ToPath().Select(tup1 => tup1.Item1.Item1 + "|" + tup1.Item1.Item2)); // Debug.WriteLine("closedSet[{0}]: {1} - {2}", k++, pth.Key, pathStr); // } lock (allPaths) { allPaths = closedSet; } var path = closedSet.First().Value.ToPath (); var deRooted = path.Count > 1 ? path.Skip (1) : path; // Ignore the root node // Debug.Print("bestPath1: {0}", moveListStr(deRooted.Select(x => x.Item1.Item1))); // Debug.Print("bestPath2: {0}", moveListStr(deRooted.Select(x => x.Item1.Item2))); var res = deRooted .SelectMany (t => Enumerable.Repeat(t.Item1,nRepetitions)).ToList(); return res; }
/// <summary> /// Estimates the specified quantile /// </summary> /// <param name="quantile">The quantile to estimate. Must be between 0 and 1.</param> /// <returns>The value for the estimated quantile</returns> public double Quantile(double quantile) { if (quantile < 0 || quantile > 1) { throw new ArgumentOutOfRangeException(nameof(quantile), "must be between 0 and 1"); } if (centroids.Count == 0) { throw new InvalidOperationException( "Cannot call Quantile() method until first Adding values to the digest"); } if (centroids.Count == 1) { return(centroids.First().Value.Mean); } double index = quantile * count; if (index < 1) { return(Min); } if (index > Count - 1) { return(Max); } Centroid currentNode = centroids.First().Value; Centroid lastNode = centroids.Last().Value; double currentWeight = currentNode.Count; if (Math.Abs(currentWeight - 2) < Tolerance && index <= 2) { // first node is a double weight with one sample at min, sou we can infer location of other sample return(2 * currentNode.Mean - Min); } if (Math.Abs(centroids.Last().Value.Count - 2) < Tolerance && index > Count - 2) { // likewise for last centroid return(2 * lastNode.Mean - Max); } double weightSoFar = currentWeight / 2.0; if (index < weightSoFar) { return(WeightedAvg(Min, weightSoFar - index, currentNode.Mean, index - 1)); } foreach (Centroid nextNode in centroids.Values.Skip(1)) { double nextWeight = nextNode.Count; double dw = (currentWeight + nextWeight) / 2.0; if (index < weightSoFar + dw) { double leftExclusion = 0; double rightExclusion = 0; if (Math.Abs(currentWeight - 1) < Tolerance) { if (index < weightSoFar + 0.5) { return(currentNode.Mean); } leftExclusion = 0.5; } if (Math.Abs(nextWeight - 1) < Tolerance) { if (index >= weightSoFar + dw - 0.5) { return(nextNode.Mean); } rightExclusion = 0.5; } // centroids i and i+1 bracket our current point // we interpolate, but the weights are diminished if singletons are present double weight1 = index - weightSoFar - leftExclusion; double weight2 = weightSoFar + dw - index - rightExclusion; return(WeightedAvg(currentNode.Mean, weight2, nextNode.Mean, weight1)); } weightSoFar += dw; currentNode = nextNode; currentWeight = nextWeight; } double w1 = index - weightSoFar; double w2 = Count - 1 - index; return(WeightedAvg(currentNode.Mean, w2, Max, w1)); }