Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Tests/Benchmark/GPTests.cs @ 15692

Last change on this file since 15692 was 15334, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

File size: 9.5 KB
Line 
1using System.Linq;
2// ReSharper disable PossibleNullReferenceException
3
4namespace HeuristicLab.Tests.Benchmark {
5  using System;
6  using System.Threading;
7
8  using HeuristicLab.Algorithms.GeneticAlgorithm;
9  using HeuristicLab.BenchmarkSuite;
10  using HeuristicLab.BenchmarkSuite.Problems;
11  using HeuristicLab.Data;
12  using HeuristicLab.ParallelEngine;
13  using HeuristicLab.Problems.ProgramSynthesis.Push.Analyzer;
14  using HeuristicLab.Problems.ProgramSynthesis.Push.Crossover;
15  using HeuristicLab.Problems.ProgramSynthesis.Push.Manipulator;
16  using HeuristicLab.Problems.ProgramSynthesis.Push.Problem.BenchmarkSuite;
17  using HeuristicLab.Problems.ProgramSynthesis.Push.Selector;
18
19  using Microsoft.VisualStudio.TestTools.UnitTesting;
20
21  [TestClass]
22  public class GpTests {
23
24    [TestMethod]
25    [TestProperty("Time", "Long")]
26    [TestCategory("ProblemTest")]
27    public void Checksum() {
28      Run<Checksum>();
29    }
30
31    [TestMethod]
32    [TestProperty("Time", "Long")]
33    [TestCategory("ProblemTest")]
34    public void CollatzNumbers() {
35      Run<CollatzNumbers>();
36    }
37
38    [TestMethod]
39    [TestProperty("Time", "Long")]
40    [TestCategory("ProblemTest")]
41    public void CompareStringLengths() {
42      Run<CompareStringLengths>();
43    }
44
45    [TestMethod]
46    [TestProperty("Time", "Long")]
47    [TestCategory("ProblemTest")]
48    public void CountOdds() {
49      Run<CountOdds>();
50    }
51
52    [TestMethod]
53    [TestProperty("Time", "Long")]
54    [TestCategory("ProblemTest")]
55    public void Digits() {
56      Run<Digits>();
57    }
58
59    [TestMethod]
60    [TestProperty("Time", "Long")]
61    [TestCategory("ProblemTest")]
62    public void DoubleLetters() {
63      Run<DoubleLetters>();
64    }
65
66    [TestMethod]
67    [TestProperty("Time", "Long")]
68    [TestCategory("ProblemTest")]
69    public void EvenSquares() {
70      Run<EvenSquares>();
71    }
72
73    [TestMethod]
74    [TestProperty("Time", "Long")]
75    [TestCategory("ProblemTest")]
76    public void ForLoopIndex() {
77      Run<ForLoopIndex>();
78    }
79
80    [TestMethod]
81    [TestProperty("Time", "Long")]
82    [TestCategory("ProblemTest")]
83    public void Grades() {
84      Run<Grades>();
85    }
86
87    [TestMethod]
88    [TestProperty("Time", "Long")]
89    [TestCategory("ProblemTest")]
90    public void LastIndexOfZero() {
91      Run<LastIndexOfZero>();
92    }
93
94    [TestMethod]
95    [TestProperty("Time", "Long")]
96    [TestCategory("ProblemTest")]
97    public void Median() {
98      Run<Median>();
99    }
100
101    [TestMethod]
102    [TestProperty("Time", "Long")]
103    [TestCategory("ProblemTest")]
104    public void MirrorImage() {
105      Run<MirrorImage>();
106    }
107
108    [TestMethod]
109    [TestProperty("Time", "Long")]
110    [TestCategory("ProblemTest")]
111    public void NegativeToZero() {
112      Run<NegativeToZero>();
113    }
114
115    [TestMethod]
116    [TestProperty("Time", "Long")]
117    [TestCategory("ProblemTest")]
118    public void NumberIo() {
119      Run<NumberIO>();
120    }
121
122    [TestMethod]
123    [TestProperty("Time", "Long")]
124    [TestCategory("ProblemTest")]
125    public void PigLatin() {
126      Run<PigLatin>();
127    }
128
129    [TestMethod]
130    [TestProperty("Time", "Long")]
131    [TestCategory("ProblemTest")]
132    public void ReplaceSpaceWithNewLine() {
133      Run<ReplaceSpaceWithNewline>();
134    }
135
136    [TestMethod]
137    [TestProperty("Time", "Long")]
138    [TestCategory("ProblemTest")]
139    public void ScrabbleScore() {
140      Run<ScrabbleScore>();
141    }
142
143    [TestMethod]
144    [TestProperty("Time", "Long")]
145    [TestCategory("ProblemTest")]
146    public void Smallest() {
147      Run<Smallest>();
148    }
149
150    [TestMethod]
151    [TestProperty("Time", "Long")]
152    [TestCategory("ProblemTest")]
153    public void SmallOrLarge() {
154      Run<SmallOrLarge>();
155    }
156
157    [TestMethod]
158    [TestProperty("Time", "Long")]
159    [TestCategory("ProblemTest")]
160    public void StringDifferences() {
161      Run<StringDifferences>();
162    }
163
164    [TestMethod]
165    [TestProperty("Time", "Long")]
166    [TestCategory("ProblemTest")]
167    public void StringLengthsBackwards() {
168      Run<StringLengthsBackwards>();
169    }
170
171    [TestMethod]
172    [TestProperty("Time", "Long")]
173    [TestCategory("ProblemTest")]
174    public void SumOfSquares() {
175      Run<SumOfSquares>();
176    }
177
178    [TestMethod]
179    [TestProperty("Time", "Long")]
180    [TestCategory("ProblemTest")]
181    public void SuperAnagrams() {
182      Run<SuperAnagrams>();
183    }
184
185    [TestMethod]
186    [TestProperty("Time", "Long")]
187    [TestCategory("ProblemTest")]
188    public void Syllables() {
189      Run<Syllables>();
190    }
191
192    [TestMethod]
193    [TestProperty("Time", "Long")]
194    [TestCategory("ProblemTest")]
195    public void VectorAverage() {
196      Run<VectorAverage>();
197    }
198
199    [TestMethod]
200    [TestProperty("Time", "Long")]
201    [TestCategory("ProblemTest")]
202    public void VectorSummed() {
203      Run<VectorSummed>();
204    }
205
206    [TestMethod]
207    [TestProperty("Time", "Long")]
208    [TestCategory("ProblemTest")]
209    public void WallisPi() {
210      Run<WallisPi>();
211    }
212
213    [TestMethod]
214    [TestProperty("Time", "Long")]
215    [TestCategory("ProblemTest")]
216    public void WordStats() {
217      Run<WordStats>();
218    }
219
220    [TestMethod]
221    [TestProperty("Time", "Long")]
222    [TestCategory("ProblemTest")]
223    public void XWordLines() {
224      Run<XWordLines>();
225    }
226
227    static void Run<T>() where T : IBenchmarkSuiteDataDescriptor, new() {
228      var trigger = new AutoResetEvent(false);
229      var alg = GetAlgorithm(new T());
230      Exception exception = null;
231
232      alg.ExceptionOccurred += (sender, args) => {
233        exception = args.Value;
234        trigger.Set();
235      };
236
237      alg.Stopped += (sender, args) => {
238        trigger.Set();
239      };
240
241      var lastGeneration = -1;
242      alg.ExecutionTimeChanged += (sender, args) => {
243        if (!alg.Results.Any() || !alg.Results.ContainsKey("CurrentBestQuality")) return;
244
245        var generation = (alg.Results["Generations"].Value as IntValue).Value;
246
247        if (lastGeneration != -1 && generation == lastGeneration) return;
248
249        Console.WriteLine(@"Generation: {0}", generation);
250
251        if (alg.Results.ContainsKey("CurrentBestQuality")) {
252          var best = (alg.Results["CurrentBestQuality"].Value as DoubleValue).Value;
253          Console.WriteLine(@"CurrentBestQuality: {0}", best);
254        }
255
256        if (alg.Results.ContainsKey("CurrentAverageQuality")) {
257          var average = (alg.Results["CurrentAverageQuality"].Value as DoubleValue).Value;
258          Console.WriteLine(@"CurrentAverageQuality: {0}", average);
259        }
260
261        if (alg.Results.ContainsKey("CurrentWorstQuality")) {
262          var worst = (alg.Results["CurrentWorstQuality"].Value as DoubleValue).Value;
263          Console.WriteLine(@"CurrentWorstQuality: {0}", worst);
264        }
265
266        Console.WriteLine();
267        lastGeneration = generation;
268      };
269
270      alg.Prepare();
271      alg.Start();
272      trigger.WaitOne();
273
274      if (exception != null) {
275        Console.WriteLine(exception.Message);
276        throw exception;
277      }
278
279      var finalGeneration = (alg.Results["Generations"].Value as IntValue).Value;
280      var bestQuality = (alg.Results["CurrentBestQuality"].Value as DoubleValue).Value;
281      var averageQuality = (alg.Results["CurrentAverageQuality"].Value as DoubleValue).Value;
282      var worstQuality = (alg.Results["CurrentWorstQuality"].Value as DoubleValue).Value;
283
284      Console.WriteLine(@"Final Generation: {0}", finalGeneration);
285      Console.WriteLine(@"CurrentBestQuality: {0}", bestQuality);
286      Console.WriteLine(@"CurrentAverageQuality: {0}", averageQuality);
287      Console.WriteLine(@"CurrentWorstQuality: {0}", worstQuality);
288    }
289
290    static GeneticAlgorithm GetAlgorithm(IBenchmarkSuiteDataDescriptor descriptor) {
291      var ga = new GeneticAlgorithm();
292
293      var problemData = descriptor.CreateProblemData();
294      var pushProblem = new PlushPushBenchmarkSuiteProblem();
295
296      pushProblem.Load(problemData);
297      ga.Name = "GA " + pushProblem.Name;
298
299      ga.Problem = pushProblem;
300
301      var crossover = ga.CrossoverParameter.ValidValues.OfType<AlternationCrossover>().First();
302
303      switch (problemData.ProblemType) {
304        case ProblemType.NumberIO:
305        case ProblemType.SmallOrLarge:
306        case ProblemType.Median:
307        case ProblemType.Smallest:
308          crossover.AlignmentDeviation = 5;
309          break;
310
311        default:
312          crossover.AlignmentDeviation = 10;
313          break;
314      }
315
316      crossover.AlternationRate = 0.01;
317      ga.Crossover = crossover;
318
319      ga.Selector = ga.SelectorParameter.ValidValues.OfType<LexicaseSelector>().First();
320
321      var mutator = ga.MutatorParameter.ValidValues.OfType<UniformMutation>().First();
322      mutator.InstructionMutationProbability = 0.875;
323      mutator.CloseMutationProbability = 0.125;
324      mutator.CloseIncrementRate = 0.1;
325      ga.Mutator = mutator;
326
327      ga.MutationProbability.Value = 0.8;
328      ga.SetSeedRandomly.Value = true;
329      ga.PopulationSize.Value = 1000;
330
331      ga.MaximumGenerations.Value = 9; // problemData.ProgramExecutionBudget / problemData.TrainingCount / 1000;
332
333      // remove unnecessary analyzers to improve performance
334      pushProblem.OperatorsParameter.Value
335        .RemoveAll(analyzer =>
336          analyzer.GetType() == typeof(IndividualZeroErrorAnalyzer) ||
337          analyzer.GetType() == typeof(PushExpressionFrequencyAnalyzer));
338
339      ga.Engine = new ParallelEngine();
340
341      return ga;
342    }
343  }
344}
Note: See TracBrowser for help on using the repository browser.