Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionCrossoverTest.cs @ 16988

Last change on this file since 16988 was 7915, checked in by mkommend, 12 years ago

#1777: Extracted unit tests into a separate solution.

File size: 7.0 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;
30using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
31using HeuristicLab.Random;
32using Microsoft.VisualStudio.TestTools.UnitTesting;
33using ExecutionContext = HeuristicLab.Core.ExecutionContext;
34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic_34.Tests {
36  [TestClass()]
37  public class SymbolicDataAnalysisExpressionCrossoverTest {
38    private const int PopulationSize = 10000;
39    private const int MaxTreeDepth = 10;
40    private const int MaxTreeLength = 100;
41    private const int Rows = 1000;
42    private const int Columns = 50;
43
44    /// <summary>
45    ///Gets or sets the test context which provides
46    ///information about and functionality for the current test run.
47    ///</summary>
48    public TestContext TestContext { get; set; }
49
50    [TestMethod]
51    public void SymbolicDataAnalysisExpressionSemanticSimilarityCrossoverPerformanceTest() {
52      var problem = new SymbolicRegressionSingleObjectiveProblem();
53      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<IRegressionProblemData>>().First();
54      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
55    }
56
57    [TestMethod]
58    public void SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossoverPerformanceTest() {
59      var problem = new SymbolicRegressionSingleObjectiveProblem();
60      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<IRegressionProblemData>>().First();
61      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
62    }
63
64    [TestMethod]
65    public void SymbolicDataAnalysisExpressionDeterministicBestCrossoverPerformanceTest() {
66      var problem = new SymbolicRegressionSingleObjectiveProblem();
67      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionDeterministicBestCrossover<IRegressionProblemData>>().First();
68      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
69    }
70
71    [TestMethod]
72    public void SymbolicDataAnalysisExpressionContextAwareCrossoverPerformanceTest() {
73      var problem = new SymbolicRegressionSingleObjectiveProblem();
74      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionContextAwareCrossover<IRegressionProblemData>>().First();
75      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
76    }
77
78    [TestMethod]
79    public void SymbolicDataAnalysisExpressionDepthConstrainedCrossoverPerformanceTest() {
80      var problem = new SymbolicRegressionSingleObjectiveProblem();
81      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionDepthConstrainedCrossover<IRegressionProblemData>>().First();
82
83      crossover.DepthRangeParameter.Value = crossover.DepthRangeParameter.ValidValues.First(s => s.Value == "HighLevel");
84      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
85      crossover.DepthRangeParameter.Value = crossover.DepthRangeParameter.ValidValues.First(s => s.Value == "Standard");
86      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
87      crossover.DepthRangeParameter.Value = crossover.DepthRangeParameter.ValidValues.First(s => s.Value == "LowLevel");
88      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
89    }
90
91
92    private static void SymbolicDataAnalysisCrossoverPerformanceTest(ISymbolicDataAnalysisExpressionCrossover<IRegressionProblemData> crossover) {
93      var twister = new MersenneTwister(31415);
94      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
95      var grammar = new FullFunctionalExpressionGrammar();
96      var stopwatch = new Stopwatch();
97
98      grammar.MaximumFunctionArguments = 0;
99      grammar.MaximumFunctionDefinitions = 0;
100      grammar.MinimumFunctionArguments = 0;
101      grammar.MinimumFunctionDefinitions = 0;
102
103      var trees = Util.CreateRandomTrees(twister, dataset, grammar, PopulationSize, 1, MaxTreeLength, 0, 0);
104      foreach (ISymbolicExpressionTree tree in trees) {
105        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
106      }
107      var problemData = new RegressionProblemData(dataset, dataset.VariableNames, dataset.VariableNames.Last());
108      var problem = new SymbolicRegressionSingleObjectiveProblem();
109      problem.ProblemData = problemData;
110
111      var globalScope = new Scope("Global Scope");
112      globalScope.Variables.Add(new Core.Variable("Random", twister));
113      var context = new ExecutionContext(null, problem, globalScope);
114      context = new ExecutionContext(context, crossover, globalScope);
115
116      stopwatch.Start();
117      for (int i = 0; i != PopulationSize; ++i) {
118        var parent0 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
119        var scopeParent0 = new Scope();
120        scopeParent0.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent0));
121        context.Scope.SubScopes.Add(scopeParent0);
122
123        var parent1 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
124        var scopeParent1 = new Scope();
125        scopeParent1.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent1));
126        context.Scope.SubScopes.Add(scopeParent1);
127
128        crossover.Execute(context, new CancellationToken());
129
130        context.Scope.SubScopes.Remove(scopeParent0); // clean the scope in preparation for the next iteration
131        context.Scope.SubScopes.Remove(scopeParent1); // clean the scope in preparation for the next iteration
132      }
133      stopwatch.Stop();
134      double msPerCrossover = 2 * stopwatch.ElapsedMilliseconds / (double)PopulationSize;
135      Console.WriteLine(crossover.Name + ": " + Environment.NewLine +
136                        msPerCrossover + " ms per crossover (~" + Math.Round(1000.0 / (msPerCrossover)) + " crossover operations / s)");
137
138      foreach (var tree in trees)
139        HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_34.Tests.Util.IsValid(tree);
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.