Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Tests/Benchmark/ProblemTests.cs @ 14733

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

#2665 Storable problem data, Renamings due to typos, Removed GP from class names

File size: 7.2 KB
Line 
1namespace HeuristicLab.Tests.Benchmark.Problem {
2  using System;
3  using System.Collections.Generic;
4  using System.Linq;
5  using System.Threading.Tasks;
6
7  using HeuristicLab.BenchmarkSuite;
8  using HeuristicLab.BenchmarkSuite.Problems;
9  using HeuristicLab.Problems.Instances;
10  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
11  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
12  using HeuristicLab.Problems.ProgramSynthesis.Push.Generators;
13  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
14  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
15  using HeuristicLab.Random;
16
17  using Microsoft.VisualStudio.TestTools.UnitTesting;
18
19  [TestClass]
20  public class ProblemTests {
21    [TestMethod]
22    [TestProperty("Time", "Medium")]
23    [TestCategory("ProblemTest")]
24    public void CountOdds() {
25      RandomWalk(new CountOdds());
26    }
27
28    [TestMethod]
29    [TestProperty("Time", "Medium")]
30    [TestCategory("ProblemTest")]
31    public void Checksum() {
32      RandomWalk(new Checksum());
33    }
34
35    //[TestMethod]
36    //[TestProperty("Time", "Medium")]
37    //[TestCategory("ProblemTest")]
38    //public void CollatzNumbers() {
39    //  RandomWalk(new CollatzNumbers());
40    //}
41
42    //[TestMethod]
43    //[TestProperty("Time", "Medium")]
44    //[TestCategory("ProblemTest")]
45    //public void CompareStringLengths() {
46    //  RandomWalk(new CompareStringLengths());
47    //}
48
49    //[TestMethod]
50    //[TestProperty("Time", "Medium")]
51    //[TestCategory("ProblemTest")]
52    //public void Digits() {
53    //  RandomWalk(new Digits());
54    //}
55
56    //[TestMethod]
57    //[TestProperty("Time", "Medium")]
58    //[TestCategory("ProblemTest")]
59    //public void DoubleLetters() {
60    //  RandomWalk(new DoubleLetters());
61    //}
62
63    //[TestMethod]
64    //[TestProperty("Time", "Medium")]
65    //[TestCategory("ProblemTest")]
66    //public void EvenSquares() {
67    //  RandomWalk(new EvenSquares());
68    //}
69
70    //[TestMethod]
71    //[TestProperty("Time", "Medium")]
72    //[TestCategory("ProblemTest")]
73    //public void ForLoopIndex() {
74    //  RandomWalk(new ForLoopIndex());
75    //}
76
77    //[TestMethod]
78    //[TestProperty("Time", "Medium")]
79    //[TestCategory("ProblemTest")]
80    //public void Grades() {
81    //  RandomWalk(new Grades());
82    //}
83
84    //[TestMethod]
85    //[TestProperty("Time", "Medium")]
86    //[TestCategory("ProblemTest")]
87    //public void LastIndexOfZero() {
88    //  RandomWalk(new LastIndexOfZero());
89    //}
90
91    //[TestMethod]
92    //[TestProperty("Time", "Medium")]
93    //[TestCategory("ProblemTest")]
94    //public void Median() {
95    //  RandomWalk(new Median());
96    //}
97
98    //[TestMethod]
99    //[TestProperty("Time", "Medium")]
100    //[TestCategory("ProblemTest")]
101    //public void MirrorImage() {
102    //  RandomWalk(new MirrorImage());
103    //}
104
105    //[TestMethod]
106    //[TestProperty("Time", "Medium")]
107    //[TestCategory("ProblemTest")]
108    //public void NegativeToZero() {
109    //  RandomWalk(new NegativeToZero());
110    //}
111
112    //[TestMethod]
113    //[TestProperty("Time", "Medium")]
114    //[TestCategory("ProblemTest")]
115    //public void NumberIo() {
116    //  RandomWalk(new NumberIo());
117    //}
118
119    //[TestMethod]
120    //[TestProperty("Time", "Medium")]
121    //[TestCategory("ProblemTest")]
122    //public void PigLatin() {
123    //  RandomWalk(new PigLatin());
124    //}
125
126    //[TestMethod]
127    //[TestProperty("Time", "Medium")]
128    //[TestCategory("ProblemTest")]
129    //public void ReplaceSpaceWithNewLine() {
130    //  RandomWalk(new ReplaceSpaceWithNewline());
131    //}
132
133    private static void RandomWalk(IDataDescriptor descriptor) {
134      var maxProgramSizeLimit = 1024;
135      var iterations = 100;
136      var best = double.MaxValue;
137      var globalExecCounter = 0;
138      var lockObj = new object();
139      var lockCount = new object();
140      var random = new FastRandom(1337);
141
142      Expression bestProgram = null;
143      var config = new PushConfiguration { EvalPushLimit = 4096 };
144      var pool = new PushInterpreterPool(config);
145
146      var instance = new BenchmarkSuiteInstanceProvider();
147      var data = instance.LoadData(descriptor);
148
149      Parallel.For(0, iterations, i => {
150        var execCounter = 0;
151        var program = CodeGenerator.RandomExpandExpression(maxProgramSizeLimit, random);
152        var results = new double[data.OriginalTrainingCount];
153
154        using (var interpreter = pool.GetInstance(random)) {
155          for (var j = 0; j < data.OriginalTrainingCount; j++) {
156            var example = data.Examples[i];
157
158            interpreter.BooleanStack.Push(example.InputBoolean);
159            interpreter.IntegerStack.Push(example.InputInt);
160            interpreter.FloatStack.Push(example.InputFloat);
161
162            interpreter.Run(program);
163
164            var diff = GetDiff(example.OutputInt, interpreter.IntegerStack) +
165                       GetDiff(example.OutputFloat, interpreter.FloatStack) +
166                       GetDiff(example.OutputBoolean, interpreter.BooleanStack);
167
168            results[j] = diff;
169
170            execCounter += interpreter.ExecCounter;
171            interpreter.Clear();
172          }
173        }
174
175        lock (lockCount) {
176          globalExecCounter += execCounter;
177        }
178
179        var avg = results.Average();
180
181        if (avg >= best) return;
182
183        lock (lockObj) {
184          if (avg < best) {
185            best = avg;
186            bestProgram = program;
187          }
188        }
189      });
190
191      var resultsTest = new double[data.OriginalTestCount];
192      Parallel.For(data.OriginalTestCount, data.OriginalTestCount, i => {
193        using (var interpreter = pool.GetInstance()) {
194          var example = data.Examples[i];
195
196          interpreter.BooleanStack.Push(example.InputBoolean);
197          interpreter.IntegerStack.Push(example.InputInt);
198          interpreter.FloatStack.Push(example.InputFloat);
199
200          interpreter.Run(bestProgram);
201
202          var diff = GetDiff(example.OutputInt, interpreter.IntegerStack) +
203                     GetDiff(example.OutputFloat, interpreter.FloatStack) +
204                     GetDiff(example.OutputBoolean, interpreter.BooleanStack);
205
206          resultsTest[i] = diff;
207        }
208      });
209
210      var averageTestResult = resultsTest.Average();
211
212      Console.WriteLine("Best Training: {0}", best);
213      Console.WriteLine("Test: {0}", averageTestResult);
214      Console.WriteLine("Best trainig program: {0}", bestProgram);
215      Console.WriteLine("ExecCounter: {0}", globalExecCounter);
216    }
217
218    private static double GetDiff<T>(IReadOnlyList<T> estimated, IStack<T> resultStack)
219      where T : IComparable {
220      var count = Math.Min(estimated.Count, resultStack.Count);
221      var result = resultStack.Peek(count);
222      var comparableLength = Math.Min(estimated.Count, result.Length);
223      var diff = 0d;
224
225      for (var i = 0; i < comparableLength; i++) {
226        diff += Math.Abs(estimated[i].CompareTo(result[i]));
227      }
228
229      if (estimated.Count > result.Length) {
230        for (var i = comparableLength; i < estimated.Count; i++) {
231          diff += Math.Abs(estimated[i].CompareTo(default(T)));
232        }
233      }
234
235      return diff;
236    }
237  }
238}
Note: See TracBrowser for help on using the repository browser.