Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/Distributions.cs @ 14880

Last change on this file since 14880 was 14880, checked in by gkronber, 7 years ago

#2704: added more expression templates

File size: 2.0 KB
Line 
1
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using HeuristicLab.Core;
6using HeuristicLab.ExpressionGenerator.Interfaces;
7
8namespace HeuristicLab.ExpressionGenerator {
9  // wrapper which calls NextDouble() of a PRNG to sample from a distribution
10  public class DoublePrngDistribution : IDistribution<double> {
11    private readonly IRandom prng;
12
13    public DoublePrngDistribution(IRandom random) {
14      this.prng = random;
15    }
16
17    public double Sample() {
18      return prng.NextDouble();
19    }
20
21    public IEnumerable<double> SampleN(int n) {
22      return Enumerable.Range(0, n).Select(_ => prng.NextDouble());
23    }
24  }
25  // wrapper which calls Next() of a PRNG to sample from a distribution
26  public class IntPrngDistribution : IDistribution<int> {
27    private readonly IRandom prng;
28
29    public IntPrngDistribution(IRandom random) {
30      this.prng = random;
31    }
32
33    public int Sample() {
34      return prng.Next();
35    }
36
37    public IEnumerable<int> SampleN(int n) {
38      return Enumerable.Range(0, n).Select(_ => prng.Next());
39    }
40  }
41
42  // p(x): x == value ? 1 : 0
43  public class PointDistribution<T> : IDistribution<T> where T : struct {
44    public readonly T value;
45
46    public PointDistribution(T value) {
47      this.value = value;
48    }
49
50    public T Sample() {
51      return value;
52    }
53
54    public IEnumerable<T> SampleN(int n) {
55      return Enumerable.Repeat(value, n);
56    }
57  }
58
59  public class DiscreteUniformDistribution : IDistribution<int> {
60    public readonly int minValue; // inclusive
61    public readonly int maxValue; // exclusive
62    public readonly IRandom random;
63
64    public DiscreteUniformDistribution(IRandom random, int minValue, int maxValue) {
65      this.minValue = minValue;
66      this.maxValue = maxValue;
67      this.random = random;
68    }
69
70    public int Sample() {
71      return random.Next(minValue, maxValue);
72    }
73
74    public IEnumerable<int> SampleN(int n) {
75      return Enumerable.Range(0, n).Select(_ => Sample());
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.