Changeset 7508


Ignore:
Timestamp:
02/23/12 17:43:34 (7 years ago)
Author:
mkommend
Message:

#1682: Added performance unit tests for new GP crossovers.

Location:
trunk/sources/HeuristicLab.Tests
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionCrossoverTest.cs

    r7505 r7508  
    2525using System.Linq;
    2626using System.Threading;
     27using HeuristicLab.Core;
    2728using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     29using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    2830using HeuristicLab.Random;
    29 using HeuristicLab.Core;
    3031using Microsoft.VisualStudio.TestTools.UnitTesting;
    3132using ExecutionContext = HeuristicLab.Core.ExecutionContext;
    32 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    3333
    3434namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
     
    4949    [TestMethod]
    5050    public void SymbolicDataAnalysisExpressionSemanticSimilarityCrossoverPerformanceTest() {
    51       SemanticSimilarityCrossoverPerformanceTest();
     51      var problem = new SymbolicRegressionSingleObjectiveProblem();
     52      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<IRegressionProblemData>>().First();
     53      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
    5254    }
    5355
    5456    [TestMethod]
    5557    public void SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossoverPerformanceTest() {
    56       ProbabilisticFunctionalCrossoverPerformanceTest();
     58      var problem = new SymbolicRegressionSingleObjectiveProblem();
     59      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<IRegressionProblemData>>().First();
     60      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
    5761    }
    5862
    5963    [TestMethod]
    6064    public void SymbolicDataAnalysisExpressionDeterministicBestCrossoverPerformanceTest() {
    61       DeterministicBestCrossoverPerformanceTest();
     65      var problem = new SymbolicRegressionSingleObjectiveProblem();
     66      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionDeterministicBestCrossover<IRegressionProblemData>>().First();
     67      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
    6268    }
    6369
    6470    [TestMethod]
    6571    public void SymbolicDataAnalysisExpressionContextAwareCrossoverPerformanceTest() {
    66       ContextAwareCrossoverPerformanceTest();
     72      var problem = new SymbolicRegressionSingleObjectiveProblem();
     73      var crossover = problem.OperatorsParameter.Value.OfType<SymbolicDataAnalysisExpressionContextAwareCrossover<IRegressionProblemData>>().First();
     74      SymbolicDataAnalysisCrossoverPerformanceTest(crossover);
    6775    }
    6876
    6977    [TestMethod]
    7078    public void SymbolicDataAnalysisExpressionDepthConstrainedCrossoverPerformanceTest() {
    71       DepthConstrainedCrossoverPerformanceTest();
     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);
    7288    }
    7389
    74     private static void DepthConstrainedCrossoverPerformanceTest() {
     90
     91    private static void SymbolicDataAnalysisCrossoverPerformanceTest(ISymbolicDataAnalysisExpressionCrossover<IRegressionProblemData> crossover) {
    7592      var twister = new MersenneTwister(31415);
    7693      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
     
    90107      var problem = new SymbolicRegressionSingleObjectiveProblem();
    91108      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";
    97109
    98110      var globalScope = new Scope("Global Scope");
     
    120132      stopwatch.Stop();
    121133      double msPerCrossover = 2 * stopwatch.ElapsedMilliseconds / (double)PopulationSize;
    122       Console.WriteLine("DepthConstrainedCrossover: " + Environment.NewLine +
     134      Console.WriteLine(crossover.Name + ": " + Environment.NewLine +
    123135                        msPerCrossover + " ms per crossover (~" + Math.Round(1000.0 / (msPerCrossover)) + " crossover operations / s)");
    124136
    125137      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);
     138        HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests.Util.IsValid(tree);
    343139    }
    344140  }
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Tests.csproj

    r7294 r7508  
    266266    <Compile Include="HeuristicLab.Problems.DataAnalysis-3.4\StatisticCalculatorsTest.cs" />
    267267    <Compile Include="HeuristicLab.Problems.DataAnalysis-3.4\TableFileParserTest.cs" />
     268    <Compile Include="HeuristicLab.Problems.DataAnalysis.Symbolic-3.4\SymbolicDataAnalysisExpressionCrossoverTest.cs" />
    268269    <Compile Include="HeuristicLab.Problems.DataAnalysis.Symbolic-3.4\SymbolicDataAnalysisExpressionTreeInterpreterTest.cs" />
    269270    <Compile Include="HeuristicLab.Problems.DataAnalysis.Symbolic-3.4\SymbolicDataAnalysisExpressionTreeSimplifierTest.cs" />
Note: See TracChangeset for help on using the changeset viewer.