Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Tests/Erc/ErcTests.cs @ 14909

Last change on this file since 14909 was 14909, checked in by pkimmesw, 8 years ago

#2665 Fixed VectorExpression errors, Fixed BenchmarkSuite Problem Data View issues

File size: 6.8 KB
Line 
1namespace HeuristicLab.Tests.Erc {
2  using System.Linq;
3
4  using HeuristicLab.Core;
5  using HeuristicLab.Data;
6  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Char;
7  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Float;
8  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.FloatVector;
9  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Integer;
10  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.IntegerVector;
11  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.String;
12  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.StringVector;
13  using HeuristicLab.Random;
14
15  using Microsoft.VisualStudio.TestTools.UnitTesting;
16
17  [TestClass]
18  public class ErcTests {
19    private const double tolerance = 0.15;
20    private IRandom random;
21
22    [TestInitialize]
23    public void BeforeTest() {
24      random = new MersenneTwister(1337);
25    }
26
27    [TestMethod]
28    [TestProperty("Time", "Short")]
29    [TestCategory("ErcTests")]
30    public void MixedIntegerErcValueTests() {
31      var options = new IntegerErcOptions(
32                        new IntegerConstantErcValue(true, 0.8, 11, 12, 13),
33                        new IntegerRangeErcValue(true, -10, 10, 0.2));
34
35      Assert.IsTrue(options.IsEnabled);
36
37      var distribution = Enumerable.Range(0, 1000)
38        .Select(_ => options.GetErcValue(random))
39        .GroupBy(x => x <= 10 ? "range" : "constants")
40        .ToDictionary(group => group.Key, group => group.Count());
41
42      var estimatedRatio = 0.8 / 0.2;
43      var ratio = distribution["constants"] / (double)distribution["range"];
44
45      Assert.IsTrue(IsAlmost(estimatedRatio, ratio, tolerance));
46    }
47
48    [TestMethod]
49    [TestProperty("Time", "Short")]
50    [TestCategory("ErcTests")]
51    public void MixedFloatErcValueTests() {
52      var options = new FloatErcOptions(
53                        new FloatConstantErcValue(true, 0.8, 11.1, 12.1, 13.1),
54                        new FloatRangeErcValue(true, -10.5, 10.5, 0.2));
55
56      Assert.IsTrue(options.IsEnabled);
57
58      var distribution = Enumerable.Range(0, 1000)
59        .Select(_ => options.GetErcValue(random))
60        .GroupBy(x => x <= 10.5 ? "range" : "constants")
61        .ToDictionary(group => group.Key, group => group.Count());
62
63      var estimatedRatio = 0.8 / 0.2;
64      var ratio = distribution["constants"] / (double)distribution["range"];
65
66      Assert.IsTrue(IsAlmost(estimatedRatio, ratio, tolerance));
67    }
68
69    [TestMethod]
70    [TestProperty("Time", "Short")]
71    [TestCategory("ErcTests")]
72    public void MixedCharErcValueTests() {
73      var options = new CharErcOptions(
74                        new IntegerConstantErcValue(true, 0.8, 11, 12, 13),
75                        new IntegerRangeErcValue(true, 65, 98, 0.2));
76
77      Assert.IsTrue(options.IsEnabled);
78
79      var distribution = Enumerable.Range(0, 1000)
80        .Select(_ => options.GetErcValue(random))
81        .GroupBy(x => x >= 65 ? "range" : "constants")
82        .ToDictionary(group => group.Key, group => group.Count());
83
84      var estimatedRatio = 0.8 / 0.2;
85      var ratio = distribution["constants"] / (double)distribution["range"];
86
87      Assert.IsTrue(IsAlmost(estimatedRatio, ratio, tolerance));
88    }
89
90    [TestMethod]
91    [TestProperty("Time", "Short")]
92    [TestCategory("ErcTests")]
93    public void MixedStringErcValueTests() {
94      var options = new StringErcOptions(
95        new StringConstantErcValue(true, 0.8, "_1", "_2", "_3"),
96        new StringRandomErcValue(true, 0.2));
97
98      Assert.IsTrue(options.IsEnabled);
99
100      var distribution = Enumerable.Range(0, 1000)
101        .Select(_ => options.GetErcValue(random))
102        .GroupBy(x => x.StartsWith("_") ? "constants" : "random")
103        .ToDictionary(group => group.Key, group => group.Count());
104
105      var estimatedRatio = 0.8 / 0.2;
106      var ratio = distribution["constants"] / (double)distribution["random"];
107
108      Assert.IsTrue(IsAlmost(estimatedRatio, ratio, tolerance));
109    }
110
111    [TestMethod]
112    [TestProperty("Time", "Short")]
113    [TestCategory("ErcTests")]
114    public void MixedIntegerVectorErcValueTests() {
115      var options = new IntegerVectorErcOptions(
116        new IntegerVectorConstantsErcValue(true, 0.8, new[] { 1, 2, 3 }, new[] { 4, 5, 6 }),
117        new IntegerVectorConstantsErcValue(true, 0.2, new[] { 10, 11, 12 }, new[] { 13, 14, 15 }));
118
119      Assert.IsTrue(options.IsEnabled);
120
121      var distribution = Enumerable.Range(0, 1000)
122        .Select(_ => options.GetErcValue(random))
123        .GroupBy(x => x.All(_ => _ < 10) ? "constants1" : "constants2")
124        .ToDictionary(group => group.Key, group => group.Count());
125
126      var estimatedRatio = 0.8 / 0.2;
127      var ratio = distribution["constants1"] / (double)distribution["constants2"];
128
129      Assert.IsTrue(IsAlmost(estimatedRatio, ratio, tolerance));
130    }
131
132    [TestMethod]
133    [TestProperty("Time", "Short")]
134    [TestCategory("ErcTests")]
135    public void MixedFloatVectorErcValueTests() {
136      var options = new FloatVectorErcOptions(
137        new FloatVectorConstantsErcValue(true, 0.8, new[] { 1.0, 2.2, 3.1 }, new[] { 4.1, 5.2, 6.3 }),
138        new FloatVectorConstantsErcValue(true, 0.2, new[] { 10.1, 11.1, 12.2 }, new[] { 13.1, 14.2, 15.3 }));
139
140      Assert.IsTrue(options.IsEnabled);
141
142      var distribution = Enumerable.Range(0, 1000)
143        .Select(_ => options.GetErcValue(random))
144        .GroupBy(x => x.All(_ => _ < 10) ? "constants1" : "constants2")
145        .ToDictionary(group => group.Key, group => group.Count());
146
147      var estimatedRatio = 0.8 / 0.2;
148      var ratio = distribution["constants1"] / (double)distribution["constants2"];
149
150      Assert.IsTrue(IsAlmost(estimatedRatio, ratio, tolerance));
151    }
152
153    [TestMethod]
154    [TestProperty("Time", "Short")]
155    [TestCategory("ErcTests")]
156    public void MixedStringVectorErcValueTests() {
157      var options = new StringVectorErcOptions(
158        new StringVectorConstantsErcValue(true, 0.8, new StringArray(new[] { "_1", "_2", "_3" }), new StringArray(new[] { "_4", "_5", "_5" })),
159        new StringVectorConstantsErcValue(true, 0.2, new StringArray(new[] { "-1", "-2", "-3" }), new StringArray(new[] { "-4", "-5", "-5" })));
160
161      Assert.IsTrue(options.IsEnabled);
162
163      var distribution = Enumerable.Range(0, 1000)
164        .Select(_ => options.GetErcValue(random))
165        .GroupBy(x => x.Any(_ => _.StartsWith("_")) ? "constants1" : "constants2")
166        .ToDictionary(group => group.Key, group => group.Count());
167
168      var estimatedRatio = 0.8 / 0.2;
169      var ratio = distribution["constants1"] / (double)distribution["constants2"];
170
171      Assert.IsTrue(IsAlmost(estimatedRatio, ratio, tolerance));
172    }
173
174    private static bool IsAlmost(double x, double y, double tolerance) {
175      return y < x * (1 + tolerance) && y > x * (1 - tolerance);
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.