1 |
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.ExpressionGenerator.Interfaces;
|
---|
7 |
|
---|
8 | namespace 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 | }
|
---|