Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 PushGP HL Integration

File size: 5.6 KB
Line 
1namespace HeuristicLab.Tests.Benchmark.Problem {
2  using System;
3  using System.Linq;
4  using System.Threading.Tasks;
5
6  using HeuristicLab.Algorithms.PushGP.Data.Random;
7  using HeuristicLab.Algorithms.PushGP.Expressions;
8  using HeuristicLab.Algorithms.PushGP.Generators;
9  using HeuristicLab.Algorithms.PushGP.Interpreter;
10  using HeuristicLab.BenchmarkSuite;
11  using HeuristicLab.BenchmarkSuite.Problems;
12  using HeuristicLab.Problems.Instances;
13
14  using Microsoft.VisualStudio.TestTools.UnitTesting;
15
16  [TestClass]
17  public class ProblemTests {
18
19    private void Test(params IDataDescriptor[] descriptors) {
20      var instance = new BenchmarkSuitePushInstanceProvider();
21
22      foreach (var d in descriptors) {
23        var examples = instance.LoadData(d);
24      }
25    }
26
27    [TestMethod]
28    [TestProperty("Time", "Medium")]
29    [TestCategory("ProblemTest")]
30    public void CountOdds() {
31      RandomWalk(new CountOdds());
32    }
33
34    [TestMethod]
35    [TestProperty("Time", "Medium")]
36    [TestCategory("ProblemTest")]
37    public void Checksum() {
38      RandomWalk(new Checksum());
39    }
40
41    [TestMethod]
42    [TestProperty("Time", "Medium")]
43    [TestCategory("ProblemTest")]
44    public void CollatzNumbers() {
45      RandomWalk(new CollatzNumbers());
46    }
47
48    [TestMethod]
49    [TestProperty("Time", "Medium")]
50    [TestCategory("ProblemTest")]
51    public void CompareStringLengths() {
52      RandomWalk(new CompareStringLengths());
53    }
54
55    [TestMethod]
56    [TestProperty("Time", "Medium")]
57    [TestCategory("ProblemTest")]
58    public void Digits() {
59      RandomWalk(new Digits());
60    }
61
62    [TestMethod]
63    [TestProperty("Time", "Medium")]
64    [TestCategory("ProblemTest")]
65    public void DoubleLetters() {
66      RandomWalk(new DoubleLetters());
67    }
68
69    [TestMethod]
70    [TestProperty("Time", "Medium")]
71    [TestCategory("ProblemTest")]
72    public void EvenSquares() {
73      RandomWalk(new EvenSquares());
74    }
75
76    [TestMethod]
77    [TestProperty("Time", "Medium")]
78    [TestCategory("ProblemTest")]
79    public void ForLoopIndex() {
80      RandomWalk(new ForLoopIndex());
81    }
82
83    [TestMethod]
84    [TestProperty("Time", "Medium")]
85    [TestCategory("ProblemTest")]
86    public void Grades() {
87      RandomWalk(new Grades());
88    }
89
90    [TestMethod]
91    [TestProperty("Time", "Medium")]
92    [TestCategory("ProblemTest")]
93    public void LastIndexOfZero() {
94      RandomWalk(new LastIndexOfZero());
95    }
96
97    [TestMethod]
98    [TestProperty("Time", "Medium")]
99    [TestCategory("ProblemTest")]
100    public void Median() {
101      RandomWalk(new Median());
102    }
103
104    [TestMethod]
105    [TestProperty("Time", "Medium")]
106    [TestCategory("ProblemTest")]
107    public void MirrorImage() {
108      RandomWalk(new MirrorImage());
109    }
110
111    [TestMethod]
112    [TestProperty("Time", "Medium")]
113    [TestCategory("ProblemTest")]
114    public void NegativeToZero() {
115      RandomWalk(new NegativeToZero());
116    }
117
118    [TestMethod]
119    [TestProperty("Time", "Medium")]
120    [TestCategory("ProblemTest")]
121    public void NumberIo() {
122      RandomWalk(new NumberIo());
123    }
124
125    [TestMethod]
126    [TestProperty("Time", "Medium")]
127    [TestCategory("ProblemTest")]
128    public void PigLatin() {
129      RandomWalk(new PigLatin());
130    }
131
132    [TestMethod]
133    [TestProperty("Time", "Medium")]
134    [TestCategory("ProblemTest")]
135    public void ReplaceSpaceWithNewLine() {
136      RandomWalk(new ReplaceSpaceWithNewline());
137    }
138
139    private static void RandomWalk(IDataDescriptor descriptor) {
140      var maxProgramSizeLimit = 1024;
141      var iterations = 1; //4000;
142      var best = double.MaxValue;
143      var globalExecCounter = 0;
144      var lockObj = new object();
145      var lockCount = new object();
146      RandomFactory.Seed = 1337;
147
148      Expression bestProgram = null;
149      var config = new Configuration { EvalPushLimit = 500 };
150      var pool = new PushGpInterpreterPool(config);
151
152      var instance = new BenchmarkSuitePushInstanceProvider();
153      var examples = instance.LoadData(descriptor);
154
155      Parallel.For(0, iterations, i => {
156        var execCounter = 0;
157        var program = CodeGenerator.RandomProgram(maxProgramSizeLimit);
158        var results = new double[examples.TrainingCount];
159
160        using (var interpreter = pool.GetInstance()) {
161          for (var j = 0; j < examples.TrainingCount; j++) {
162            examples.ApplyTrainingInput(interpreter, j);
163            interpreter.Run(program);
164
165            results[j] = examples.CompareTrainingOutput(interpreter, j);
166
167            execCounter += interpreter.ExecCounter;
168            interpreter.Clear();
169          }
170        }
171
172        lock (lockCount) {
173          globalExecCounter += execCounter;
174        }
175
176        var avg = results.Average();
177
178        if (avg >= best) return;
179
180        lock (lockObj) {
181          if (avg < best) {
182            best = avg;
183            bestProgram = program;
184          }
185        }
186      });
187
188      var resultsTest = new double[examples.TrainingCount];
189      Parallel.For(0, examples.TrainingCount, i => {
190        using (var interpreter = pool.GetInstance()) {
191          examples.ApplyTestInput(interpreter, i);
192          interpreter.Run(bestProgram);
193
194          resultsTest[i] = examples.CompareTestOutput(interpreter, i);
195        }
196      });
197
198      var averageTestResult = resultsTest.Average();
199
200      Console.WriteLine("Best Training: {0}", best);
201      Console.WriteLine("Test: {0}", averageTestResult);
202      Console.WriteLine("Best trainig program: {0}", bestProgram);
203      Console.WriteLine("ExecCounter: {0}", globalExecCounter);
204    }
205  }
206}
Note: See TracBrowser for help on using the repository browser.