using System; using System.Threading; using HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction; using HeuristicLab.Problems.GeneticProgramming.ArtificialAnt; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Tests { [TestClass] public class IteratedSymbolicExpressionConstructionTest { [TestMethod] public void TestArtificialAnt() { // see Langdon and Poli, "Why Ants are Hard", Table 2 (http://marvin.cs.uidaho.edu/Teaching/CS472/santaFeTrailSpace.pdf) TestAlg(problem: new Problem(), policy: new RandomSymbolicExpressionConstructionPolicy(), maxLen: 3, expectedEvals: 3, expectedBestQuality: 3); TestAlg(problem: new Problem(), policy: new RandomSymbolicExpressionConstructionPolicy(), maxLen: 4, expectedEvals: 3, expectedBestQuality: 3); TestAlg(problem: new Problem(), policy: new RandomSymbolicExpressionConstructionPolicy(), maxLen: 5, expectedEvals: 21, expectedBestQuality: 11); TestAlg(problem: new Problem(), policy: new RandomSymbolicExpressionConstructionPolicy(), maxLen: 6, expectedEvals: 48, expectedBestQuality: 11); TestAlg(problem: new Problem(), policy: new RandomSymbolicExpressionConstructionPolicy(), maxLen: 10, expectedEvals: 25455, expectedBestQuality: 47); TestAlg(problem: new Problem(), policy: new RandomSymbolicExpressionConstructionPolicy(), maxLen: 13, expectedEvals: 3191259, expectedBestQuality: 89); // log of runtimes (28.08.2015), single core, Intel Core i5 @3.2 GHz // hh:mm:ss.ms // 00:00:00.037 // 00:00:00.001 // 00:00:00.003 // 00:00:00.008 // 00:00:05.499 // 00:13:17.395 } [TestMethod] public void TestStateAggregation() { var stateValueFunction = new TabularMaxStateValueFunction(); stateValueFunction.StateFunction = new ParentChildStateFunction(); var pol = new UcbSymbolicExpressionConstructionPolicy(); pol.StateValueFunction = stateValueFunction; TestAlg(new Problem(), pol, maxLen: 13, expectedEvals: 3191259, expectedBestQuality: 89); } [TestMethod] public void TestStateValueApproximation() { var stateValueFunction = new GbtApproximateStateValueFunction(); var pol = new EpsGreedySymbolicExpressionConstructionPolicy(); pol.StateValueFunction = stateValueFunction; TestAlg(new Problem(), pol, maxLen: 13, expectedEvals: 3191259, expectedBestQuality: 89); } private void TestAlg(Problem problem, ISymbolicExpressionConstructionPolicy policy, int maxLen, int expectedEvals, double expectedBestQuality) { var alg = new IteratedSymbolicExpressionConstruction(); problem.Encoding.TreeLength = maxLen; alg.Problem = problem; alg.PolicyParameter.Value = policy; alg.MaximumEvaluations = 5000000; var wh = new ManualResetEvent(false); alg.Stopped += (sender, args) => { wh.Set(); }; alg.ExceptionOccurred += (sender, args) => { wh.Set(); }; alg.Start(); wh.WaitOne(); Assert.AreEqual(expectedEvals, alg.ResultsEvaluations); Assert.AreEqual(expectedBestQuality, alg.ResultsBestQuality); Console.WriteLine(alg.ExecutionTime); } } }