Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionCrossoverTest.cs @ 7615

Last change on this file since 7615 was 7615, checked in by gkronber, 12 years ago

#1081 merged r7462:7609 from trunk into time series branch

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Linq;
26using System.Threading;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
30using HeuristicLab.Random;
31using Microsoft.VisualStudio.TestTools.UnitTesting;
32using ExecutionContext = HeuristicLab.Core.ExecutionContext;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
35  [TestClass()]
36  public class SymbolicDataAnalysisExpressionCrossoverTest {
37    private const int PopulationSize = 10000;
38    private const int MaxTreeDepth = 10;
39    private const int MaxTreeLength = 100;
40    private const int Rows = 1000;
41    private const int Columns = 50;
42
43    /// <summary>
44    ///Gets or sets the test context which provides
45    ///information about and functionality for the current test run.
46    ///</summary>
47    public TestContext TestContext { get; set; }
48
49    [TestMethod]
50    public void SymbolicDataAnalysisExpressionSemanticSimilarityCrossoverPerformanceTest() {
51      var problem = new SymbolicRegressionSingleObjectiveProblem();
52      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<IRegressionProblemData>>().First();
53      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
54    }
55
56    [TestMethod]
57    public void SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossoverPerformanceTest() {
58      var problem = new SymbolicRegressionSingleObjectiveProblem();
59      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<IRegressionProblemData>>().First();
60      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
61    }
62
63    [TestMethod]
64    public void SymbolicDataAnalysisExpressionDeterministicBestCrossoverPerformanceTest() {
65      var problem = new SymbolicRegressionSingleObjectiveProblem();
66      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionDeterministicBestCrossover<IRegressionProblemData>>().First();
67      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
68    }
69
70    [TestMethod]
71    public void SymbolicDataAnalysisExpressionContextAwareCrossoverPerformanceTest() {
72      var problem = new SymbolicRegressionSingleObjectiveProblem();
73      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionContextAwareCrossover<IRegressionProblemData>>().First();
74      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
75    }
76
77    [TestMethod]
78    public void SymbolicDataAnalysisExpressionDepthConstrainedCrossoverPerformanceTest() {
79      var problem = new SymbolicRegressionSingleObjectiveProblem();
80      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionDepthConstrainedCrossover<IRegressionProblemData>>().First();
81
82      crossover.DepthRangeParameter.Value = crossover.DepthRangeParameter.ValidValues.First(s => s.Value == "HighLevel");
83      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
84      crossover.DepthRangeParameter.Value = crossover.DepthRangeParameter.ValidValues.First(s => s.Value == "Standard");
85      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
86      crossover.DepthRangeParameter.Value = crossover.DepthRangeParameter.ValidValues.First(s => s.Value == "LowLevel");
87      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
88    }
89
90
91    private static void SymbolicDataAnalysisCrossoverPerformanceTest(ISymbolicDataAnalysisExpressionCrossover<IRegressionProblemData> crossover) {
92      var twister = new MersenneTwister(31415);
93      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
94      var grammar = new FullFunctionalExpressionGrammar();
95      var stopwatch = new Stopwatch();
96
97      grammar.MaximumFunctionArguments = 0;
98      grammar.MaximumFunctionDefinitions = 0;
99      grammar.MinimumFunctionArguments = 0;
100      grammar.MinimumFunctionDefinitions = 0;
101
102      var trees = Util.CreateRandomTrees(twister, dataset, grammar, PopulationSize, 1, MaxTreeLength, 0, 0);
103      foreach (ISymbolicExpressionTree tree in trees) {
104        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
105      }
106      var problemData = new RegressionProblemData(dataset, dataset.VariableNames, dataset.VariableNames.Last());
107      var problem = new SymbolicRegressionSingleObjectiveProblem();
108      problem.ProblemData = problemData;
109
110      var globalScope = new Scope("Global Scope");
111      globalScope.Variables.Add(new Core.Variable("Random", twister));
112      var context = new ExecutionContext(null, problem, globalScope);
113      context = new ExecutionContext(context, crossover, globalScope);
114
115      stopwatch.Start();
116      for (int i = 0; i != PopulationSize; ++i) {
117        var parent0 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
118        var scopeParent0 = new Scope();
119        scopeParent0.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent0));
120        context.Scope.SubScopes.Add(scopeParent0);
121
122        var parent1 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
123        var scopeParent1 = new Scope();
124        scopeParent1.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent1));
125        context.Scope.SubScopes.Add(scopeParent1);
126
127        crossover.Execute(context, new CancellationToken());
128
129        context.Scope.SubScopes.Remove(scopeParent0); // clean the scope in preparation for the next iteration
130        context.Scope.SubScopes.Remove(scopeParent1); // clean the scope in preparation for the next iteration
131      }
132      stopwatch.Stop();
133      double msPerCrossover = 2 * stopwatch.ElapsedMilliseconds / (double)PopulationSize;
134      Console.WriteLine(crossover.Name + ": " + Environment.NewLine +
135                        msPerCrossover + " ms per crossover (~" + Math.Round(1000.0 / (msPerCrossover)) + " crossover operations / s)");
136
137      foreach (var tree in trees)
138        HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests.Util.IsValid(tree);
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.