Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Tests/Erc/ErcTests.cs @ 16003

Last change on this file since 16003 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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