[7477] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7477] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Diagnostics;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Threading;
|
---|
[7508] | 27 | using HeuristicLab.Core;
|
---|
[7477] | 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[7508] | 29 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
[7477] | 30 | using HeuristicLab.Random;
|
---|
| 31 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 32 | using ExecutionContext = HeuristicLab.Core.ExecutionContext;
|
---|
| 33 |
|
---|
[9764] | 34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
|
---|
[7477] | 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]
|
---|
[9785] | 50 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 51 | [TestProperty("Time", "long")]
|
---|
[7477] | 52 | public void SymbolicDataAnalysisExpressionSemanticSimilarityCrossoverPerformanceTest() {
|
---|
[7508] | 53 | var problem = new SymbolicRegressionSingleObjectiveProblem();
|
---|
| 54 | var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<IRegressionProblemData>>().First();
|
---|
| 55 | SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
|
---|
[7477] | 56 | }
|
---|
| 57 |
|
---|
| 58 | [TestMethod]
|
---|
[9785] | 59 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 60 | [TestProperty("Time", "long")]
|
---|
[7477] | 61 | public void SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossoverPerformanceTest() {
|
---|
[7508] | 62 | var problem = new SymbolicRegressionSingleObjectiveProblem();
|
---|
| 63 | var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<IRegressionProblemData>>().First();
|
---|
| 64 | SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
|
---|
[7477] | 65 | }
|
---|
| 66 |
|
---|
| 67 | [TestMethod]
|
---|
[9785] | 68 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 69 | [TestProperty("Time", "long")]
|
---|
[7477] | 70 | public void SymbolicDataAnalysisExpressionDeterministicBestCrossoverPerformanceTest() {
|
---|
[7508] | 71 | var problem = new SymbolicRegressionSingleObjectiveProblem();
|
---|
| 72 | var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionDeterministicBestCrossover<IRegressionProblemData>>().First();
|
---|
| 73 | SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
|
---|
[7477] | 74 | }
|
---|
| 75 |
|
---|
| 76 | [TestMethod]
|
---|
[9785] | 77 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 78 | [TestProperty("Time", "long")]
|
---|
[7477] | 79 | public void SymbolicDataAnalysisExpressionContextAwareCrossoverPerformanceTest() {
|
---|
[7508] | 80 | var problem = new SymbolicRegressionSingleObjectiveProblem();
|
---|
| 81 | var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionContextAwareCrossover<IRegressionProblemData>>().First();
|
---|
| 82 | SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
|
---|
[7477] | 83 | }
|
---|
| 84 |
|
---|
| 85 | [TestMethod]
|
---|
[9785] | 86 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 87 | [TestProperty("Time", "long")]
|
---|
[7477] | 88 | public void SymbolicDataAnalysisExpressionDepthConstrainedCrossoverPerformanceTest() {
|
---|
| 89 | var problem = new SymbolicRegressionSingleObjectiveProblem();
|
---|
| 90 | var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionDepthConstrainedCrossover<IRegressionProblemData>>().First();
|
---|
| 91 |
|
---|
[7508] | 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);
|
---|
[7477] | 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
[7508] | 101 | private static void SymbolicDataAnalysisCrossoverPerformanceTest(ISymbolicDataAnalysisExpressionCrossover<IRegressionProblemData> crossover) {
|
---|
[7477] | 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) {
|
---|
[12482] | 127 | var parent0 = (ISymbolicExpressionTree)trees.SampleRandom(twister).Clone();
|
---|
[7477] | 128 | var scopeParent0 = new Scope();
|
---|
| 129 | scopeParent0.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent0));
|
---|
| 130 | context.Scope.SubScopes.Add(scopeParent0);
|
---|
| 131 |
|
---|
[12482] | 132 | var parent1 = (ISymbolicExpressionTree)trees.SampleRandom(twister).Clone();
|
---|
[7477] | 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;
|
---|
[7508] | 144 | Console.WriteLine(crossover.Name + ": " + Environment.NewLine +
|
---|
[7477] | 145 | msPerCrossover + " ms per crossover (~" + Math.Round(1000.0 / (msPerCrossover)) + " crossover operations / s)");
|
---|
| 146 |
|
---|
| 147 | foreach (var tree in trees)
|
---|
[9764] | 148 | HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Tests.Util.IsValid(tree);
|
---|
[7477] | 149 | }
|
---|
| 150 | }
|
---|
| 151 | }
|
---|