using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Core; using HeuristicLab.ExpressionGenerator.Interfaces; namespace HeuristicLab.ExpressionGenerator { // wrapper which calls NextDouble() of a PRNG to sample from a distribution public class DoublePrngDistribution : IDistribution { private readonly IRandom prng; public DoublePrngDistribution(IRandom random) { this.prng = random; } public double Sample() { return prng.NextDouble(); } public IEnumerable SampleN(int n) { return Enumerable.Range(0, n).Select(_ => prng.NextDouble()); } } // wrapper which calls Next() of a PRNG to sample from a distribution public class IntPrngDistribution : IDistribution { private readonly IRandom prng; public IntPrngDistribution(IRandom random) { this.prng = random; } public int Sample() { return prng.Next(); } public IEnumerable SampleN(int n) { return Enumerable.Range(0, n).Select(_ => prng.Next()); } } // p(x): x == value ? 1 : 0 public class PointDistribution : IDistribution where T : struct { public readonly T value; public PointDistribution(T value) { this.value = value; } public T Sample() { return value; } public IEnumerable SampleN(int n) { return Enumerable.Repeat(value, n); } } public class DiscreteUniformDistribution : IDistribution { public readonly int minValue; // inclusive public readonly int maxValue; // exclusive public readonly IRandom random; public DiscreteUniformDistribution(IRandom random, int minValue, int maxValue) { this.minValue = minValue; this.maxValue = maxValue; this.random = random; } public int Sample() { return random.Next(minValue, maxValue); } public IEnumerable SampleN(int n) { return Enumerable.Range(0, n).Select(_ => Sample()); } } }