Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GPSymbolicRegressionSampleWithOSTest.cs @ 16674

Last change on this file since 16674 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

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