Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Renamings due to typos, ManagedPool tests, Skip Noops in Debugger

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