/// <summary> /// Generates an RGBA color sample /// </summary> /// <returns>The generated RGBA sample</returns> public override Color Sample() { var color = Color.HSVToRGB(hue.Sample(), saturation.Sample(), value.Sample()); color.a = alpha.Sample(); return(color); }
/// <summary> /// Generates a set of fractional Brownian noise samples using the Hosking method. /// </summary> /// <param name="output">An array to fill with samples.</param> public void SampleNoise(double[] output) { if (output.Length != _m) { throw new ArgumentException("Invalid length"); } // Initialization. output[0] = _gaussian.Sample(); double v = 1; _phi[0] = 0.0; // Simulation. for (int i = 1; i < _m; i++) { _phi[i - 1] = _cov[i]; for (int j = 0; j < i - 1; j++) { _psi[j] = _phi[j]; _phi[i - 1] -= _psi[j] * _cov[i - j - 1]; } _phi[i - 1] /= v; for (int j = 0; j < i - 1; j++) { _phi[j] = _psi[j] - (_phi[i - 1] * _psi[i - j - 2]); } v *= (1.0 - (_phi[i - 1] * _phi[i - 1])); output[i] = 0.0; for (int j = 0; j < i; j++) { output[i] += _phi[j] * output[i - j - 1]; } output[i] += Math.Sqrt(v) * _gaussian.Sample(); } // Rescale to obtain a sample of size 2^n on [0,L]. for (int i = 0; i < _m; i++) { output[i] *= _scaling; } }
public SamplingStatus Sample(string operationName, TraceId traceId) { var sample1 = sampler1.Sample(operationName, traceId); if (sample1) { var sample2 = sampler2.Sample(operationName, traceId); return(sample2); } return(sample1); }
private static void Max_Inner(ISampler <int> sampler, int len) { // Alloc arrays and fill with uniform random noise. int[] a = new int[len]; sampler.Sample(a); // Calc results and compare. int expected = PointwiseMax(a); int actual = MathSpan.Max(a); Assert.Equal(expected, actual); }
private static void Sum_Inner(ISampler <int> sampler, int len) { // Alloc array and fill with uniform random noise. int[] x = new int[len]; sampler.Sample(x); // Sum the array elements. int expected = PointwiseSum(x); int actual = MathSpan.Sum(x); // Compare expected and actual sum. Assert.Equal(expected, actual); }
public static void TestSimpleStats(ISampler <double> sampler) { const int sampleCount = 20_000_000; RunningStatistics runningStats = new RunningStatistics(); for (int i = 0; i < sampleCount; i++) { runningStats.Push(sampler.Sample()); } Assert.True(Math.Abs(runningStats.Mean) < 0.001); Assert.True(Math.Abs(runningStats.StandardDeviation - 1.0) < 0.0005); Assert.True(Math.Abs(runningStats.Skewness) < 0.01); Assert.True(Math.Abs(runningStats.Kurtosis) < 0.01); }
private void ExtractCurrentPieceSampling(int searchHeight) { // already extracted the piece? if (_extractedPiece != null) { return; } // extract the current piece var screenshot = Screenshot; var currentPiece = Agent.Extractor.ExtractCurrentPiece(screenshot, _currentTetrimino, searchHeight); if (currentPiece == null) { // reject (threshold not reached or piece is touched) _logger.Warn("Reject extracted current piece"); #if DEBUG Screenshot.Save(Agent.Quantizer, "reject_cp"); #endif return; } if (!currentPiece.IsUntouched) { // reject (threshold not reached or piece is touched) _logger.Warn($"Reject extracted current piece: not untouched ({currentPiece.Tetrimino})"); #if DEBUG Screenshot.Save(Agent.Quantizer, "reject_cp"); #endif return; } // add sample _logger.Info($"Added sample for extracted current piece ({currentPiece.Tetrimino})"); _currentPieceSampler.Sample(new ProbabilisticResult <Piece>(currentPiece)); #if DEBUG Screenshot.Save(Agent.Quantizer, "sample_cp"); #endif if (_currentPieceSampler.IsComplete) { // we have enought samples // evaluate our "true" result var acceptedCurrentPiece = _currentPieceSampler.Result; _logger.Info($"Accept extracted current piece by sampling ({currentPiece.Tetrimino})"); AcceptCurrentPiece(acceptedCurrentPiece); } }
private static void Clip_Inner(ISampler <int> sampler, int len) { // Alloc array and fill with uniform random noise. int[] x = new int[len]; sampler.Sample(x); // Clip the elements of the array with the safe routine. int[] expected = (int[])x.Clone(); PointwiseClip(expected, -1, 18); // Clip the elements of the array. int[] actual = (int[])x.Clone(); MathSpan.Clip(actual, -1, 18); // Compare expected with actual array. Assert.True(SpanUtils.Equal <int>(expected, actual)); }
public void Transfer(Stash stash) { if (_isQueueEmpty) { return; } var sample = _sampler.Sample(); lock (_queue) { foreach (var l in _queue) { stash.Add(l, sample); } _isQueueEmpty = true; _queue.Clear(); } }
private void ExtractNextPieceSampling() { if (!CanExtractNextPiece) { return; } // already extracted the piece? if (_extractedNextPiece != null) { return; } // extract the next piece var screenshot = Screenshot; var nextPiece = Agent.Extractor.ExtractNextPiece(screenshot); if (!nextPiece.HasValue) { // reject (threshold not reached or piece is touched) _logger.Warn("Reject extracted next piece"); #if DEBUG Screenshot.Save(Agent.Quantizer, "reject_np"); #endif return; } // add sample _logger.Info($"Added sample for extracted next piece ({nextPiece})"); _nextPieceSampler.Sample(new ProbabilisticResult <Tetrimino>(nextPiece.Value)); #if DEBUG Screenshot.Save(Agent.Quantizer, "sample_np"); #endif if (_nextPieceSampler.IsComplete) { // we have enought samples // evaluate our "true" result var acceptedNextPiece = _nextPieceSampler.Result; _logger.Info($"Accept extracted next piece by sampling ({acceptedNextPiece})"); AcceptNextPiece(acceptedNextPiece); } }
public static void TestDistribution(ISampler <double> sampler, double mean, double stdDev) { // Take a set of samples. const int sampleCount = 10_000_000; double[] sampleArr = new double[sampleCount]; for (int i = 0; i < sampleCount; i++) { sampleArr[i] = sampler.Sample(); } // Sort the ample so that we can use SortedArrayStatistics. Array.Sort(sampleArr); //// Test a range of centile/quantile values. double lowerBound = -5; double upperBound = 5; double tauStep = (upperBound - lowerBound) / 30.0; for (double tau = 0; tau <= 1.0; tau += 0.1) { // Notes. // Here we calc the tau'th quartile over a range of values in he interval [0,1], // the resulting quantile is the sample value (and CDF x-axis value) at which the // CDF crosses tau on the y-axis. // // We then take that sample x-axis value, pass it through the CDF function for the // gaussian to obtain the expected y value at that x, and compare with tau. // Determine the x value at which tau (as a proportion) of samples are <= x. double sample_x = SortedArrayStatistics.Quantile(sampleArr, tau); // Put sample_x into the gaussian CDF function, to obtain a CDF y coord.. double cdf_y = 0.5 * SpecialFunctions.Erfc((mean - sample_x) / (stdDev * Constants.Sqrt2)); // Compare the expected and actual CDF y values. double y_error = Math.Abs(tau - cdf_y); Assert.IsTrue(y_error < 0.0005); } }
/// <summary> /// Generates a boolean sample /// </summary> /// <returns>The generated sample</returns> public override bool Sample() { return(Sample(value.Sample())); }
/// <summary> /// Generates a Vector2 sample /// </summary> /// <returns>The generated sample</returns> public override Vector2 Sample() { return(new Vector2(x.Sample(), y.Sample())); }
/// <summary> /// Generates an integer sample /// </summary> /// <returns>The generated sample</returns> public override int Sample() => (int)value.Sample();
/// <summary> /// Generates a Vector4 sample /// </summary> /// <returns>The generated sample</returns> public override Vector4 Sample() { return(new Vector4(x.Sample(), y.Sample(), z.Sample(), w.Sample())); }
/// <summary> /// Generates an RGBA color sample /// </summary> /// <returns>The generated RGBA sample</returns> public override Color Sample() { return(new Color(red.Sample(), green.Sample(), blue.Sample(), alpha.Sample())); }
/// <summary> /// Generates a Vector3 sample /// </summary> /// <returns>The generated sample</returns> public override Vector3 Sample() { return(new Vector3(x.Sample(), y.Sample(), z.Sample())); }
/// <summary> /// Generates a float sample /// </summary> /// <returns>The generated sample</returns> public override float Sample() { return(value.Sample()); }