1 | using System;
|
---|
2 | using System.Threading;
|
---|
3 | using HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction;
|
---|
4 | using HeuristicLab.Problems.GeneticProgramming.ArtificialAnt;
|
---|
5 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
6 |
|
---|
7 | namespace Tests {
|
---|
8 | [TestClass]
|
---|
9 | public class IteratedSymbolicExpressionConstructionTest {
|
---|
10 | [TestMethod]
|
---|
11 | public void TestArtificialAnt() {
|
---|
12 |
|
---|
13 | // see Langdon and Poli, "Why Ants are Hard", Table 2 (http://marvin.cs.uidaho.edu/Teaching/CS472/santaFeTrailSpace.pdf)
|
---|
14 | TestAlg(problem: new Problem(), policy: new RandomSymbolicExpressionConstructionPolicy(), maxLen: 3, expectedEvals: 3, expectedBestQuality: 3);
|
---|
15 | TestAlg(problem: new Problem(), policy: new RandomSymbolicExpressionConstructionPolicy(), maxLen: 4, expectedEvals: 3, expectedBestQuality: 3);
|
---|
16 | TestAlg(problem: new Problem(), policy: new RandomSymbolicExpressionConstructionPolicy(), maxLen: 5, expectedEvals: 21, expectedBestQuality: 11);
|
---|
17 | TestAlg(problem: new Problem(), policy: new RandomSymbolicExpressionConstructionPolicy(), maxLen: 6, expectedEvals: 48, expectedBestQuality: 11);
|
---|
18 | TestAlg(problem: new Problem(), policy: new RandomSymbolicExpressionConstructionPolicy(), maxLen: 10, expectedEvals: 25455, expectedBestQuality: 47);
|
---|
19 | TestAlg(problem: new Problem(), policy: new RandomSymbolicExpressionConstructionPolicy(), maxLen: 13, expectedEvals: 3191259, expectedBestQuality: 89);
|
---|
20 |
|
---|
21 | // log of runtimes (28.08.2015), single core, Intel Core i5 @3.2 GHz
|
---|
22 | // hh:mm:ss.ms
|
---|
23 | // 00:00:00.037
|
---|
24 | // 00:00:00.001
|
---|
25 | // 00:00:00.003
|
---|
26 | // 00:00:00.008
|
---|
27 | // 00:00:05.499
|
---|
28 | // 00:13:17.395
|
---|
29 | }
|
---|
30 |
|
---|
31 | [TestMethod]
|
---|
32 | public void TestStateAggregation() {
|
---|
33 | var stateValueFunction = new TabularMaxStateValueFunction();
|
---|
34 | stateValueFunction.StateFunction = new ParentChildStateFunction();
|
---|
35 | var pol = new UcbSymbolicExpressionConstructionPolicy();
|
---|
36 | pol.StateValueFunction = stateValueFunction;
|
---|
37 | TestAlg(new Problem(), pol, maxLen: 13, expectedEvals: 3191259, expectedBestQuality: 89);
|
---|
38 | }
|
---|
39 |
|
---|
40 | [TestMethod]
|
---|
41 | public void TestStateValueApproximation() {
|
---|
42 | var stateValueFunction = new GbtApproximateStateValueFunction();
|
---|
43 | var pol = new EpsGreedySymbolicExpressionConstructionPolicy();
|
---|
44 | pol.StateValueFunction = stateValueFunction;
|
---|
45 | TestAlg(new Problem(), pol, maxLen: 13, expectedEvals: 3191259, expectedBestQuality: 89);
|
---|
46 | }
|
---|
47 |
|
---|
48 | private void TestAlg(Problem problem, ISymbolicExpressionConstructionPolicy policy, int maxLen, int expectedEvals, double expectedBestQuality) {
|
---|
49 | var alg = new IteratedSymbolicExpressionConstruction();
|
---|
50 | problem.Encoding.TreeLength = maxLen;
|
---|
51 | alg.Problem = problem;
|
---|
52 | alg.PolicyParameter.Value = policy;
|
---|
53 | alg.MaximumEvaluations = 5000000;
|
---|
54 |
|
---|
55 | var wh = new ManualResetEvent(false);
|
---|
56 |
|
---|
57 | alg.Stopped += (sender, args) => { wh.Set(); };
|
---|
58 | alg.ExceptionOccurred += (sender, args) => {
|
---|
59 | wh.Set();
|
---|
60 | };
|
---|
61 |
|
---|
62 | alg.Start();
|
---|
63 | wh.WaitOne();
|
---|
64 |
|
---|
65 | Assert.AreEqual(expectedEvals, alg.ResultsEvaluations);
|
---|
66 | Assert.AreEqual(expectedBestQuality, alg.ResultsBestQuality);
|
---|
67 |
|
---|
68 | Console.WriteLine(alg.ExecutionTime);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|