Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionCrossoverTest.cs @ 11763

Last change on this file since 11763 was 11576, checked in by swagner, 10 years ago

#2205: Merged changes r11062:11557 from trunk/sources into branches/OptimizationNetworks

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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    [TestCategory("Problems.DataAnalysis")]
51    [TestProperty("Time", "long")]
52    public void SymbolicDataAnalysisExpressionSemanticSimilarityCrossoverPerformanceTest() {
53      var problem = new SymbolicRegressionSingleObjectiveProblem();
54      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<IRegressionProblemData>>().First();
55      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
56    }
57
58    [TestMethod]
59    [TestCategory("Problems.DataAnalysis")]
60    [TestProperty("Time", "long")]
61    public void SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossoverPerformanceTest() {
62      var problem = new SymbolicRegressionSingleObjectiveProblem();
63      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<IRegressionProblemData>>().First();
64      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
65    }
66
67    [TestMethod]
68    [TestCategory("Problems.DataAnalysis")]
69    [TestProperty("Time", "long")]
70    public void SymbolicDataAnalysisExpressionDeterministicBestCrossoverPerformanceTest() {
71      var problem = new SymbolicRegressionSingleObjectiveProblem();
72      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionDeterministicBestCrossover<IRegressionProblemData>>().First();
73      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
74    }
75
76    [TestMethod]
77    [TestCategory("Problems.DataAnalysis")]
78    [TestProperty("Time", "long")]
79    public void SymbolicDataAnalysisExpressionContextAwareCrossoverPerformanceTest() {
80      var problem = new SymbolicRegressionSingleObjectiveProblem();
81      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionContextAwareCrossover<IRegressionProblemData>>().First();
82      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
83    }
84
85    [TestMethod]
86    [TestCategory("Problems.DataAnalysis")]
87    [TestProperty("Time", "long")]
88    public void SymbolicDataAnalysisExpressionDepthConstrainedCrossoverPerformanceTest() {
89      var problem = new SymbolicRegressionSingleObjectiveProblem();
90      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionDepthConstrainedCrossover<IRegressionProblemData>>().First();
91
92      crossover.DepthRangeParameter.Value = crossover.DepthRangeParameter.ValidValues.First(s => s.Value == "HighLevel");
93      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
94      crossover.DepthRangeParameter.Value = crossover.DepthRangeParameter.ValidValues.First(s => s.Value == "Standard");
95      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
96      crossover.DepthRangeParameter.Value = crossover.DepthRangeParameter.ValidValues.First(s => s.Value == "LowLevel");
97      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
98    }
99
100
101    private static void SymbolicDataAnalysisCrossoverPerformanceTest(ISymbolicDataAnalysisExpressionCrossover<IRegressionProblemData> crossover) {
102      var twister = new MersenneTwister(31415);
103      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
104      var grammar = new FullFunctionalExpressionGrammar();
105      var stopwatch = new Stopwatch();
106
107      grammar.MaximumFunctionArguments = 0;
108      grammar.MaximumFunctionDefinitions = 0;
109      grammar.MinimumFunctionArguments = 0;
110      grammar.MinimumFunctionDefinitions = 0;
111
112      var trees = Util.CreateRandomTrees(twister, dataset, grammar, PopulationSize, 1, MaxTreeLength, 0, 0);
113      foreach (ISymbolicExpressionTree tree in trees) {
114        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
115      }
116      var problemData = new RegressionProblemData(dataset, dataset.VariableNames, dataset.VariableNames.Last());
117      var problem = new SymbolicRegressionSingleObjectiveProblem();
118      problem.ProblemData = problemData;
119
120      var globalScope = new Scope("Global Scope");
121      globalScope.Variables.Add(new Core.Variable("Random", twister));
122      var context = new ExecutionContext(null, problem, globalScope);
123      context = new ExecutionContext(context, crossover, globalScope);
124
125      stopwatch.Start();
126      for (int i = 0; i != PopulationSize; ++i) {
127        var parent0 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
128        var scopeParent0 = new Scope();
129        scopeParent0.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent0));
130        context.Scope.SubScopes.Add(scopeParent0);
131
132        var parent1 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
133        var scopeParent1 = new Scope();
134        scopeParent1.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent1));
135        context.Scope.SubScopes.Add(scopeParent1);
136
137        crossover.Execute(context, new CancellationToken());
138
139        context.Scope.SubScopes.Remove(scopeParent0); // clean the scope in preparation for the next iteration
140        context.Scope.SubScopes.Remove(scopeParent1); // clean the scope in preparation for the next iteration
141      }
142      stopwatch.Stop();
143      double msPerCrossover = 2 * stopwatch.ElapsedMilliseconds / (double)PopulationSize;
144      Console.WriteLine(crossover.Name + ": " + Environment.NewLine +
145                        msPerCrossover + " ms per crossover (~" + Math.Round(1000.0 / (msPerCrossover)) + " crossover operations / s)");
146
147      foreach (var tree in trees)
148        HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Tests.Util.IsValid(tree);
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.