Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Crossovers/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionCrossoverTest.cs @ 7477

Last change on this file since 7477 was 7477, checked in by bburlacu, 12 years ago

#1682: Added missing files (that were previously incorrectly referencing the old branch), added unit tests, recommitted lost changes.

File size: 16.8 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.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Random;
29using HeuristicLab.Core;
30using Microsoft.VisualStudio.TestTools.UnitTesting;
31using ExecutionContext = HeuristicLab.Core.ExecutionContext;
32using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
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      SemanticSimilarityCrossoverPerformanceTest();
52    }
53
54    [TestMethod]
55    public void SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossoverPerformanceTest() {
56      ProbabilisticFunctionalCrossoverPerformanceTest();
57    }
58
59    [TestMethod]
60    public void SymbolicDataAnalysisExpressionDeterministicBestCrossoverPerformanceTest() {
61      DeterministicBestCrossoverPerformanceTest();
62    }
63
64    [TestMethod]
65    public void SymbolicDataAnalysisExpressionContextAwareCrossoverPerformanceTest() {
66      ContextAwareCrossoverPerformanceTest();
67    }
68
69    [TestMethod]
70    public void SymbolicDataAnalysisExpressionDepthConstrainedCrossoverPerformanceTest() {
71      DepthConstrainedCrossoverPerformanceTest();
72    }
73
74    private static void DepthConstrainedCrossoverPerformanceTest() {
75      var twister = new MersenneTwister(31415);
76      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
77      var grammar = new FullFunctionalExpressionGrammar();
78      var stopwatch = new Stopwatch();
79
80      grammar.MaximumFunctionArguments = 0;
81      grammar.MaximumFunctionDefinitions = 0;
82      grammar.MinimumFunctionArguments = 0;
83      grammar.MinimumFunctionDefinitions = 0;
84
85      var trees = Util.CreateRandomTrees(twister, dataset, grammar, PopulationSize, 1, MaxTreeLength, 0, 0);
86      foreach (ISymbolicExpressionTree tree in trees) {
87        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
88      }
89      var problemData = new RegressionProblemData(dataset, dataset.VariableNames, dataset.VariableNames.Last());
90      var problem = new SymbolicRegressionSingleObjectiveProblem();
91      problem.ProblemData = problemData;
92
93      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionDepthConstrainedCrossover<IRegressionProblemData>>().First();
94      //crossover.DepthRange.Value = "HighLevel";
95      //crossover.DepthRange.Value = "Standard";
96      crossover.DepthRange.Value = "LowLevel";
97
98      var globalScope = new Scope("Global Scope");
99      globalScope.Variables.Add(new Core.Variable("Random", twister));
100      var context = new ExecutionContext(null, problem, globalScope);
101      context = new ExecutionContext(context, crossover, globalScope);
102
103      stopwatch.Start();
104      for (int i = 0; i != PopulationSize; ++i) {
105        var parent0 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
106        var scopeParent0 = new Scope();
107        scopeParent0.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent0));
108        context.Scope.SubScopes.Add(scopeParent0);
109
110        var parent1 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
111        var scopeParent1 = new Scope();
112        scopeParent1.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent1));
113        context.Scope.SubScopes.Add(scopeParent1);
114
115        crossover.Execute(context, new CancellationToken());
116
117        context.Scope.SubScopes.Remove(scopeParent0); // clean the scope in preparation for the next iteration
118        context.Scope.SubScopes.Remove(scopeParent1); // clean the scope in preparation for the next iteration
119      }
120      stopwatch.Stop();
121      double msPerCrossover = 2 * stopwatch.ElapsedMilliseconds / (double)PopulationSize;
122      Console.WriteLine("DepthConstrainedCrossover: " + Environment.NewLine +
123                        msPerCrossover + " ms per crossover (~" + Math.Round(1000.0 / (msPerCrossover)) + " crossover operations / s)");
124
125      foreach (var tree in trees)
126        Util.IsValid(tree);
127    }
128
129    private static void SemanticSimilarityCrossoverPerformanceTest() {
130      var twister = new MersenneTwister(31415);
131      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
132      var grammar = new FullFunctionalExpressionGrammar();
133      var stopwatch = new Stopwatch();
134
135      grammar.MaximumFunctionArguments = 0;
136      grammar.MaximumFunctionDefinitions = 0;
137      grammar.MinimumFunctionArguments = 0;
138      grammar.MinimumFunctionDefinitions = 0;
139
140      var trees = Util.CreateRandomTrees(twister, dataset, grammar, PopulationSize, 1, MaxTreeLength, 0, 0);
141      foreach (ISymbolicExpressionTree tree in trees) {
142        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
143      }
144      var problemData = new RegressionProblemData(dataset, dataset.VariableNames, dataset.VariableNames.Last());
145      var problem = new SymbolicRegressionSingleObjectiveProblem();
146      problem.ProblemData = problemData;
147
148      var interpreter = problem.SymbolicExpressionTreeInterpreter;
149      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<IRegressionProblemData>>().First();
150
151      var globalScope = new Scope("Global Scope");
152      globalScope.Variables.Add(new Core.Variable("Random", twister));
153      var context = new ExecutionContext(null, problem, globalScope);
154      context = new ExecutionContext(context, crossover, globalScope);
155
156      stopwatch.Start();
157      for (int i = 0; i != PopulationSize; ++i) {
158        var parent0 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
159        var scopeParent0 = new Scope();
160        scopeParent0.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent0));
161        context.Scope.SubScopes.Add(scopeParent0);
162
163        var parent1 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
164        var scopeParent1 = new Scope();
165        scopeParent1.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent1));
166        context.Scope.SubScopes.Add(scopeParent1);
167
168        crossover.Execute(context, new CancellationToken());
169
170        context.Scope.SubScopes.Remove(scopeParent0); // clean the scope in preparation for the next iteration
171        context.Scope.SubScopes.Remove(scopeParent1); // clean the scope in preparation for the next iteration
172      }
173      stopwatch.Stop();
174      double msPerCrossover = 2 * stopwatch.ElapsedMilliseconds / (double)PopulationSize;
175      Console.WriteLine("SemanticSimilarityCrossover: " + Environment.NewLine +
176                        interpreter.EvaluatedSolutions + " evaluations" + Environment.NewLine +
177                        msPerCrossover + " ms per crossover (~" + Math.Round(1000.0 / (msPerCrossover)) + " crossover operations / s)");
178
179      foreach (var tree in trees)
180        Util.IsValid(tree);
181    }
182
183    private static void ProbabilisticFunctionalCrossoverPerformanceTest() {
184      var twister = new MersenneTwister(31415);
185      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
186      var grammar = new FullFunctionalExpressionGrammar();
187      var stopwatch = new Stopwatch();
188
189      grammar.MaximumFunctionArguments = 0;
190      grammar.MaximumFunctionDefinitions = 0;
191      grammar.MinimumFunctionArguments = 0;
192      grammar.MinimumFunctionDefinitions = 0;
193
194      var trees = Util.CreateRandomTrees(twister, dataset, grammar, PopulationSize, 1, MaxTreeLength, 0, 0);
195      foreach (ISymbolicExpressionTree tree in trees) {
196        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
197      }
198      var problemData = new RegressionProblemData(dataset, dataset.VariableNames, dataset.VariableNames.Last());
199      var problem = new SymbolicRegressionSingleObjectiveProblem();
200      problem.ProblemData = problemData;
201      var interpreter = problem.SymbolicExpressionTreeInterpreter;
202
203      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<IRegressionProblemData>>().First();
204
205      var globalScope = new Scope("Global Scope");
206      globalScope.Variables.Add(new Core.Variable("Random", twister));
207      var context = new ExecutionContext(null, problem, globalScope);
208      context = new ExecutionContext(context, crossover, globalScope);
209
210      stopwatch.Start();
211      for (int i = 0; i != PopulationSize; ++i) {
212        var parent0 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
213        var scopeParent0 = new Scope();
214        scopeParent0.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent0));
215        context.Scope.SubScopes.Add(scopeParent0);
216
217        var parent1 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
218        var scopeParent1 = new Scope();
219        scopeParent1.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent1));
220        context.Scope.SubScopes.Add(scopeParent1);
221
222        crossover.Execute(context, new CancellationToken());
223
224        context.Scope.SubScopes.Remove(scopeParent0); // clean the scope in preparation for the next iteration
225        context.Scope.SubScopes.Remove(scopeParent1); // clean the scope in preparation for the next iteration
226      }
227      stopwatch.Stop();
228
229      double msPerCrossover = 2 * stopwatch.ElapsedMilliseconds / (double)PopulationSize;
230      Console.WriteLine("ProbabilisticFunctionalCrossover: " + Environment.NewLine +
231                        interpreter.EvaluatedSolutions + " evaluations" + Environment.NewLine +
232                        msPerCrossover + " ms per crossover (~" + Math.Round(1000.0 / (msPerCrossover)) + " crossover operations / s)");
233      foreach (var tree in trees)
234        Util.IsValid(tree);
235    }
236
237    private static void DeterministicBestCrossoverPerformanceTest() {
238      var twister = new MersenneTwister(31415);
239      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
240      var grammar = new FullFunctionalExpressionGrammar();
241      var stopwatch = new Stopwatch();
242
243      grammar.MaximumFunctionArguments = 0;
244      grammar.MaximumFunctionDefinitions = 0;
245      grammar.MinimumFunctionArguments = 0;
246      grammar.MinimumFunctionDefinitions = 0;
247
248      var trees = Util.CreateRandomTrees(twister, dataset, grammar, PopulationSize, 1, MaxTreeLength, 0, 0);
249      foreach (ISymbolicExpressionTree tree in trees) {
250        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
251      }
252      var problemData = new RegressionProblemData(dataset, dataset.VariableNames, dataset.VariableNames.Last());
253      var problem = new SymbolicRegressionSingleObjectiveProblem();
254      problem.ProblemData = problemData;
255
256      var interpreter = problem.SymbolicExpressionTreeInterpreter;
257      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionDeterministicBestCrossover<IRegressionProblemData>>().First();
258
259      var globalScope = new Scope("Global Scope");
260      globalScope.Variables.Add(new Core.Variable("Random", twister));
261      var context = new ExecutionContext(null, problem, globalScope);
262      context = new ExecutionContext(context, crossover, globalScope);
263
264      stopwatch.Start();
265      for (int i = 0; i != PopulationSize; ++i) {
266        var parent0 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
267        var scopeParent0 = new Scope();
268        scopeParent0.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent0));
269        context.Scope.SubScopes.Add(scopeParent0);
270
271        var parent1 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
272        var scopeParent1 = new Scope();
273        scopeParent1.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent1));
274        context.Scope.SubScopes.Add(scopeParent1);
275
276        crossover.Execute(context, new CancellationToken());
277
278        context.Scope.SubScopes.Remove(scopeParent0); // clean the scope in preparation for the next iteration
279        context.Scope.SubScopes.Remove(scopeParent1); // clean the scope in preparation for the next iteration
280      }
281      stopwatch.Stop();
282      double msPerCrossover = 2 * stopwatch.ElapsedMilliseconds / (double)PopulationSize;
283      Console.WriteLine("DeterministicBestCrossover: " + Environment.NewLine +
284                        interpreter.EvaluatedSolutions + " evaluations" + Environment.NewLine +
285                        msPerCrossover + " ms per crossover (~" + Math.Round(1000.0 / (msPerCrossover)) + " crossover operations / s)");
286
287      foreach (var tree in trees)
288        Util.IsValid(tree);
289    }
290
291    private static void ContextAwareCrossoverPerformanceTest() {
292      var twister = new MersenneTwister(31415);
293      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
294      var grammar = new FullFunctionalExpressionGrammar();
295      var stopwatch = new Stopwatch();
296
297      grammar.MaximumFunctionArguments = 0;
298      grammar.MaximumFunctionDefinitions = 0;
299      grammar.MinimumFunctionArguments = 0;
300      grammar.MinimumFunctionDefinitions = 0;
301
302      var trees = Util.CreateRandomTrees(twister, dataset, grammar, PopulationSize, 1, MaxTreeLength, 0, 0);
303      foreach (ISymbolicExpressionTree tree in trees) {
304        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
305      }
306      var problemData = new RegressionProblemData(dataset, dataset.VariableNames, dataset.VariableNames.Last());
307      var problem = new SymbolicRegressionSingleObjectiveProblem();
308      problem.ProblemData = problemData;
309
310      var interpreter = problem.SymbolicExpressionTreeInterpreter;
311      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionContextAwareCrossover<IRegressionProblemData>>().First();
312
313      var globalScope = new Scope("Global Scope");
314      globalScope.Variables.Add(new Core.Variable("Random", twister));
315      var context = new ExecutionContext(null, problem, globalScope);
316      context = new ExecutionContext(context, crossover, globalScope);
317
318      stopwatch.Start();
319      for (int i = 0; i != PopulationSize; ++i) {
320        var parent0 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
321        var scopeParent0 = new Scope();
322        scopeParent0.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent0));
323        context.Scope.SubScopes.Add(scopeParent0);
324
325        var parent1 = (ISymbolicExpressionTree)trees.SelectRandom(twister).Clone();
326        var scopeParent1 = new Scope();
327        scopeParent1.Variables.Add(new Core.Variable(crossover.ParentsParameter.ActualName, parent1));
328        context.Scope.SubScopes.Add(scopeParent1);
329
330        crossover.Execute(context, new CancellationToken());
331
332        context.Scope.SubScopes.Remove(scopeParent0); // clean the scope in preparation for the next iteration
333        context.Scope.SubScopes.Remove(scopeParent1); // clean the scope in preparation for the next iteration
334      }
335      stopwatch.Stop();
336      double msPerCrossover = 2 * stopwatch.ElapsedMilliseconds / (double)PopulationSize;
337      Console.WriteLine("ContextAwareCrossover: " + Environment.NewLine +
338                        interpreter.EvaluatedSolutions + " evaluations" + Environment.NewLine +
339                        msPerCrossover + " ms per crossover (~" + Math.Round(1000.0 / (msPerCrossover)) + " crossover operations / s)");
340
341      foreach (var tree in trees)
342        Util.IsValid(tree);
343    }
344  }
345}
Note: See TracBrowser for help on using the repository browser.