Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Tests/Erc/ErcTests.cs @ 16958

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

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File size: 8.3 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 IntegerConstantErc(true, 0.8, 11, 12, 13),
33                        new IntegerRangeErc(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 FloatConstantErc(true, 0.8, 11.1, 12.1, 13.1),
54                        new FloatRangeErc(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 IntegerConstantErc(true, 0.8, 11, 12, 13),
75                        new IntegerRangeErc(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 StringConstantErc(true, 0.8, "_1", "_2", "_3"),
96        new StringRandomErc(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 StringStringErcValueTests() {
115      var options = new StringErcOptions(
116        new StringConstantErc(true, 1.0, "_1", "_2", "_3"));
117
118      Assert.IsTrue(options.IsEnabled);
119
120      var distribution = Enumerable.Range(0, 1000)
121        .Select(_ => options.GetErcValue(random))
122        .GroupBy(x => x)
123        .ToDictionary(group => group.Key, group => group.Count());
124
125      var estimatedRatio = 1.0 / 3.0;
126
127      Assert.IsTrue(IsAlmost(estimatedRatio, distribution["_1"] / 1000.0, tolerance));
128      Assert.IsTrue(IsAlmost(estimatedRatio, distribution["_2"] / 1000.0, tolerance));
129      Assert.IsTrue(IsAlmost(estimatedRatio, distribution["_3"] / 1000.0, tolerance));
130    }
131
132    [TestMethod]
133    [TestProperty("Time", "Short")]
134    [TestCategory("ErcTests")]
135    public void MixedIntegerVectorErcValueTests() {
136      var options = new IntegerVectorErcOptions(
137        new IntegerVectorConstantsErc(true, 0.8, new[] { 1, 2, 3 }, new[] { 4, 5, 6 }),
138        new IntegerVectorConstantsErc(true, 0.2, new[] { 10, 11, 12 }, new[] { 13, 14, 15 }));
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 DisabledIntegerVectorErcValueTests() {
157      var options = new IntegerVectorErcOptions(
158        new IntegerVectorConstantsErc(false, 0.8, new[] { 1, 2, 3 }, new[] { 4, 5, 6 }),
159        new IntegerVectorConstantsErc(true, 0.2, new[] { 10, 11, 12 }, new[] { 13, 14, 15 }));
160
161      Assert.IsTrue(options.IsEnabled);
162
163      var distribution = Enumerable.Range(0, 1000)
164        .Select(_ => options.GetErcValue(random))
165        .GroupBy(x => x.All(_ => _ < 10) ? "constants1" : "constants2")
166        .ToDictionary(group => group.Key, group => group.Count());
167
168      Assert.IsFalse(distribution.ContainsKey("constants1"));
169      Assert.AreEqual(1000, distribution["constants2"]);
170    }
171
172    [TestMethod]
173    [TestProperty("Time", "Short")]
174    [TestCategory("ErcTests")]
175    public void MixedFloatVectorErcValueTests() {
176      var options = new FloatVectorErcOptions(
177        new FloatVectorConstantsErc(true, 0.8, new[] { 1.0, 2.2, 3.1 }, new[] { 4.1, 5.2, 6.3 }),
178        new FloatVectorConstantsErc(true, 0.2, new[] { 10.1, 11.1, 12.2 }, new[] { 13.1, 14.2, 15.3 }));
179
180      Assert.IsTrue(options.IsEnabled);
181
182      var distribution = Enumerable.Range(0, 1000)
183        .Select(_ => options.GetErcValue(random))
184        .GroupBy(x => x.All(_ => _ < 10) ? "constants1" : "constants2")
185        .ToDictionary(group => group.Key, group => group.Count());
186
187      var estimatedRatio = 0.8 / 0.2;
188      var ratio = distribution["constants1"] / (double)distribution["constants2"];
189
190      Assert.IsTrue(IsAlmost(estimatedRatio, ratio, tolerance));
191    }
192
193    [TestMethod]
194    [TestProperty("Time", "Short")]
195    [TestCategory("ErcTests")]
196    public void MixedStringVectorErcValueTests() {
197      var options = new StringVectorErcOptions(
198        new StringVectorConstantsErc(true, 0.8, new StringArray(new[] { "_1", "_2", "_3" }), new StringArray(new[] { "_4", "_5", "_5" })),
199        new StringVectorConstantsErc(true, 0.2, new StringArray(new[] { "-1", "-2", "-3" }), new StringArray(new[] { "-4", "-5", "-5" })));
200
201      Assert.IsTrue(options.IsEnabled);
202
203      var distribution = Enumerable.Range(0, 1000)
204        .Select(_ => options.GetErcValue(random))
205        .GroupBy(x => x.Any(_ => _.StartsWith("_")) ? "constants1" : "constants2")
206        .ToDictionary(group => group.Key, group => group.Count());
207
208      var estimatedRatio = 0.8 / 0.2;
209      var ratio = distribution["constants1"] / (double)distribution["constants2"];
210
211      Assert.IsTrue(IsAlmost(estimatedRatio, ratio, tolerance));
212    }
213
214    private static bool IsAlmost(double x, double y, double tolerance) {
215      return y < x * (1 + tolerance) && y > x * (1 - tolerance);
216    }
217  }
218}
Note: See TracBrowser for help on using the repository browser.