Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GPSymbolicRegressionSampleWithOSTest.cs @ 17246

Last change on this file since 17246 was 17246, checked in by gkronber, 5 years ago

#2925: merged r17037:17242 from trunk to branch

File size: 11.4 KB
RevLine 
[15235]1#region License Information
2/* HeuristicLab
[17246]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[15235]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
23using System;
24using System.IO;
25using System.Linq;
[17035]26using HEAL.Attic;
[15235]27using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Problems.DataAnalysis;
30using HeuristicLab.Problems.DataAnalysis.Symbolic;
31using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
32using HeuristicLab.Problems.Instances.DataAnalysis;
33using HeuristicLab.Selection;
34using Microsoft.VisualStudio.TestTools.UnitTesting;
35
36namespace HeuristicLab.Tests {
37  [TestClass]
38  public class GPSymbolicRegressionSampleWithOSTest {
39    private const string SampleFileName = "OSGP_SymReg";
40    private const int seed = 12345;
41
[17035]42    private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
43
[15235]44    [TestMethod]
45    [TestCategory("Samples.Create")]
46    [TestProperty("Time", "medium")]
47    public void CreateGPSymbolicRegressionSampleWithOSTest() {
48      var osga = CreateGpSymbolicRegressionSample();
49      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
[17035]50      serializer.Serialize(osga, path);
[15235]51    }
52    [TestMethod]
53    [TestCategory("Samples.Execute")]
54    [TestProperty("Time", "long")]
55    public void RunGPSymbolicRegressionSampleWithOSTest() {
56      var osga = CreateGpSymbolicRegressionSample();
57      osga.SetSeedRandomly.Value = false;
58      osga.Seed.Value = seed;
59      osga.MaximumGenerations.Value = 10; //reduce unit test runtime
60
61      SamplesUtils.RunAlgorithm(osga);
62      var bestTrainingSolution = (IRegressionSolution)osga.Results["Best training solution"].Value;
63
64      if (Environment.Is64BitProcess) {
[15246]65        // the following are the result values as produced on builder.heuristiclab.com
66        // Unfortunately, running the same test on a different machine results in different values
67        // For x86 environments the results below match but on x64 there is a difference
68        // We tracked down the ConstantOptimizationEvaluator as a possible cause but have not
69        // been able to identify the real cause. Presumably, execution on a Xeon and a Core i7 processor
70        // leads to different results.
[16892]71        Assert.AreEqual(0.99174959007940156, SamplesUtils.GetDoubleResult(osga, "BestQuality"), 1E-8, Environment.NewLine + "Best Quality differs.");
72        Assert.AreEqual(0.9836083751914968, SamplesUtils.GetDoubleResult(osga, "CurrentAverageQuality"), 1E-8, Environment.NewLine + "Current Average Quality differs.");
73        Assert.AreEqual(0.98298394717065463, SamplesUtils.GetDoubleResult(osga, "CurrentWorstQuality"), 1E-8, Environment.NewLine + "Current Worst Quality differs.");
74        Assert.AreEqual(10100, SamplesUtils.GetIntResult(osga, "EvaluatedSolutions"), Environment.NewLine + "Evaluated Solutions differ.");
75        Assert.AreEqual(0.99174959007940156, bestTrainingSolution.TrainingRSquared, 1E-8, Environment.NewLine + "Best Training Solution Training R² differs.");
76        Assert.AreEqual(0.8962902319942232, bestTrainingSolution.TestRSquared, 1E-8, Environment.NewLine + "Best Training Solution Test R² differs.");
[15235]77      } else {
78        Assert.AreEqual(0.9971536312165723, SamplesUtils.GetDoubleResult(osga, "BestQuality"), 1E-8, Environment.NewLine + "Best Qualitiy differs.");
79        Assert.AreEqual(0.98382832370544937, SamplesUtils.GetDoubleResult(osga, "CurrentAverageQuality"), 1E-8, Environment.NewLine + "Current Average Quality differs.");
80        Assert.AreEqual(0.960805603777699, SamplesUtils.GetDoubleResult(osga, "CurrentWorstQuality"), 1E-8, Environment.NewLine + "Current Worst Quality differs.");
81        Assert.AreEqual(10500, SamplesUtils.GetIntResult(osga, "EvaluatedSolutions"), Environment.NewLine + "Evaluated Solutions differ.");
82        Assert.AreEqual(0.9971536312165723, bestTrainingSolution.TrainingRSquared, 1E-8, Environment.NewLine + "Best Training Solution Training R² differs.");
83        Assert.AreEqual(0.010190137960908724, bestTrainingSolution.TestRSquared, 1E-8, Environment.NewLine + "Best Training Solution Test R² differs.");
84      }
85    }
86
87    private OffspringSelectionGeneticAlgorithm CreateGpSymbolicRegressionSample() {
88      var osga = new OffspringSelectionGeneticAlgorithm();
89      #region Problem Configuration
90      var provider = new VariousInstanceProvider(seed);
91      var instance = provider.GetDataDescriptors().First(x => x.Name.StartsWith("Spatial co-evolution"));
92      var problemData = (RegressionProblemData)provider.LoadData(instance);
93      var problem = new SymbolicRegressionSingleObjectiveProblem();
94      problem.ProblemData = problemData;
95      problem.Load(problemData);
96      problem.BestKnownQuality.Value = 1.0;
97
98      #region configure grammar
99
100      var grammar = (TypeCoherentExpressionGrammar)problem.SymbolicExpressionTreeGrammar;
101      grammar.ConfigureAsDefaultRegressionGrammar();
102
103      //symbols square, power, squareroot, root, log, exp, sine, cosine, tangent, variable
104      var square = grammar.Symbols.OfType<Square>().Single();
105      var power = grammar.Symbols.OfType<Power>().Single();
106      var squareroot = grammar.Symbols.OfType<SquareRoot>().Single();
107      var root = grammar.Symbols.OfType<Root>().Single();
[16386]108      var cube = grammar.Symbols.OfType<Cube>().Single();
109      var cuberoot = grammar.Symbols.OfType<CubeRoot>().Single();
[15235]110      var log = grammar.Symbols.OfType<Logarithm>().Single();
111      var exp = grammar.Symbols.OfType<Exponential>().Single();
112      var sine = grammar.Symbols.OfType<Sine>().Single();
113      var cosine = grammar.Symbols.OfType<Cosine>().Single();
114      var tangent = grammar.Symbols.OfType<Tangent>().Single();
115      var variable = grammar.Symbols.OfType<Variable>().Single();
116      var powerSymbols = grammar.Symbols.Single(s => s.Name == "Power Functions");
117      powerSymbols.Enabled = true;
118
119      square.Enabled = true;
120      square.InitialFrequency = 1.0;
121      foreach (var allowed in grammar.GetAllowedChildSymbols(square))
122        grammar.RemoveAllowedChildSymbol(square, allowed);
123      foreach (var allowed in grammar.GetAllowedChildSymbols(square, 0))
124        grammar.RemoveAllowedChildSymbol(square, allowed, 0);
125      grammar.AddAllowedChildSymbol(square, variable);
126
127      power.Enabled = false;
128
129      squareroot.Enabled = false;
130      foreach (var allowed in grammar.GetAllowedChildSymbols(squareroot))
131        grammar.RemoveAllowedChildSymbol(squareroot, allowed);
132      foreach (var allowed in grammar.GetAllowedChildSymbols(squareroot, 0))
133        grammar.RemoveAllowedChildSymbol(squareroot, allowed, 0);
134      grammar.AddAllowedChildSymbol(squareroot, variable);
135
[16386]136      cube.Enabled = false;
137      cuberoot.Enabled = false;
[15235]138      root.Enabled = false;
139
140      log.Enabled = true;
141      log.InitialFrequency = 1.0;
142      foreach (var allowed in grammar.GetAllowedChildSymbols(log))
143        grammar.RemoveAllowedChildSymbol(log, allowed);
144      foreach (var allowed in grammar.GetAllowedChildSymbols(log, 0))
145        grammar.RemoveAllowedChildSymbol(log, allowed, 0);
146      grammar.AddAllowedChildSymbol(log, variable);
147
148      exp.Enabled = true;
149      exp.InitialFrequency = 1.0;
150      foreach (var allowed in grammar.GetAllowedChildSymbols(exp))
151        grammar.RemoveAllowedChildSymbol(exp, allowed);
152      foreach (var allowed in grammar.GetAllowedChildSymbols(exp, 0))
153        grammar.RemoveAllowedChildSymbol(exp, allowed, 0);
154      grammar.AddAllowedChildSymbol(exp, variable);
155
156      sine.Enabled = false;
157      foreach (var allowed in grammar.GetAllowedChildSymbols(sine))
158        grammar.RemoveAllowedChildSymbol(sine, allowed);
159      foreach (var allowed in grammar.GetAllowedChildSymbols(sine, 0))
160        grammar.RemoveAllowedChildSymbol(sine, allowed, 0);
161      grammar.AddAllowedChildSymbol(sine, variable);
162
163      cosine.Enabled = false;
164      foreach (var allowed in grammar.GetAllowedChildSymbols(cosine))
165        grammar.RemoveAllowedChildSymbol(cosine, allowed);
166      foreach (var allowed in grammar.GetAllowedChildSymbols(cosine, 0))
167        grammar.RemoveAllowedChildSymbol(cosine, allowed, 0);
168      grammar.AddAllowedChildSymbol(cosine, variable);
169
170      tangent.Enabled = false;
171      foreach (var allowed in grammar.GetAllowedChildSymbols(tangent))
172        grammar.RemoveAllowedChildSymbol(tangent, allowed);
173      foreach (var allowed in grammar.GetAllowedChildSymbols(tangent, 0))
174        grammar.RemoveAllowedChildSymbol(tangent, allowed, 0);
175      grammar.AddAllowedChildSymbol(tangent, variable);
176      #endregion
177
178      problem.SymbolicExpressionTreeGrammar = grammar;
179
180      // configure remaining problem parameters
181      problem.MaximumSymbolicExpressionTreeLength.Value = 50;
182      problem.MaximumSymbolicExpressionTreeDepth.Value = 12;
183      problem.MaximumFunctionDefinitions.Value = 0;
184      problem.MaximumFunctionArguments.Value = 0;
185
186      var evaluator = new SymbolicRegressionConstantOptimizationEvaluator();
187      evaluator.ConstantOptimizationIterations.Value = 5;
188      problem.EvaluatorParameter.Value = evaluator;
[15247]189      problem.RelativeNumberOfEvaluatedSamplesParameter.Hidden = true;
190      problem.SolutionCreatorParameter.Hidden = true;
[15235]191      #endregion
192
193      #region Algorithm Configuration
194      osga.Problem = problem;
195      osga.Name = "Offspring Selection Genetic Programming - Symbolic Regression";
[15271]196      osga.Description = "Genetic programming with strict offspring selection for solving a benchmark regression problem.";
[15235]197      SamplesUtils.ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(osga, 100, 1, 25, 0.2, 50);
198      var mutator = (MultiSymbolicExpressionTreeManipulator)osga.Mutator;
199      mutator.Operators.OfType<FullTreeShaker>().Single().ShakingFactor = 0.1;
200      mutator.Operators.OfType<OnePointShaker>().Single().ShakingFactor = 1.0;
201
202      osga.Analyzer.Operators.SetItemCheckedState(
203        osga.Analyzer.Operators
204          .OfType<SymbolicRegressionSingleObjectiveOverfittingAnalyzer>()
205          .Single(), false);
206      osga.Analyzer.Operators.SetItemCheckedState(
207        osga.Analyzer.Operators
208          .OfType<SymbolicDataAnalysisAlleleFrequencyAnalyzer>()
209          .First(), false);
210
211      osga.ComparisonFactorModifierParameter.Hidden = true;
212      osga.ComparisonFactorLowerBoundParameter.Hidden = true;
213      osga.ComparisonFactorUpperBoundParameter.Hidden = true;
214      osga.OffspringSelectionBeforeMutationParameter.Hidden = true;
215      osga.SuccessRatioParameter.Hidden = true;
216      osga.SelectedParentsParameter.Hidden = true;
[15247]217      osga.ElitesParameter.Hidden = true;
[15235]218
219      #endregion
220      return osga;
221    }
222  }
223}
Note: See TracBrowser for help on using the repository browser.