Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GPSymbolicRegressionSampleWithOSTest.cs @ 16003

Last change on this file since 16003 was 15271, checked in by gkronber, 7 years ago

#2802 fixed a typo in the sample description

File size: 11.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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 log = grammar.Symbols.OfType<Logarithm>().Single();
107      var exp = grammar.Symbols.OfType<Exponential>().Single();
108      var sine = grammar.Symbols.OfType<Sine>().Single();
109      var cosine = grammar.Symbols.OfType<Cosine>().Single();
110      var tangent = grammar.Symbols.OfType<Tangent>().Single();
111      var variable = grammar.Symbols.OfType<Variable>().Single();
112      var powerSymbols = grammar.Symbols.Single(s => s.Name == "Power Functions");
113      powerSymbols.Enabled = true;
114
115      square.Enabled = true;
116      square.InitialFrequency = 1.0;
117      foreach (var allowed in grammar.GetAllowedChildSymbols(square))
118        grammar.RemoveAllowedChildSymbol(square, allowed);
119      foreach (var allowed in grammar.GetAllowedChildSymbols(square, 0))
120        grammar.RemoveAllowedChildSymbol(square, allowed, 0);
121      grammar.AddAllowedChildSymbol(square, variable);
122
123      power.Enabled = false;
124
125      squareroot.Enabled = false;
126      foreach (var allowed in grammar.GetAllowedChildSymbols(squareroot))
127        grammar.RemoveAllowedChildSymbol(squareroot, allowed);
128      foreach (var allowed in grammar.GetAllowedChildSymbols(squareroot, 0))
129        grammar.RemoveAllowedChildSymbol(squareroot, allowed, 0);
130      grammar.AddAllowedChildSymbol(squareroot, variable);
131
132      root.Enabled = false;
133
134      log.Enabled = true;
135      log.InitialFrequency = 1.0;
136      foreach (var allowed in grammar.GetAllowedChildSymbols(log))
137        grammar.RemoveAllowedChildSymbol(log, allowed);
138      foreach (var allowed in grammar.GetAllowedChildSymbols(log, 0))
139        grammar.RemoveAllowedChildSymbol(log, allowed, 0);
140      grammar.AddAllowedChildSymbol(log, variable);
141
142      exp.Enabled = true;
143      exp.InitialFrequency = 1.0;
144      foreach (var allowed in grammar.GetAllowedChildSymbols(exp))
145        grammar.RemoveAllowedChildSymbol(exp, allowed);
146      foreach (var allowed in grammar.GetAllowedChildSymbols(exp, 0))
147        grammar.RemoveAllowedChildSymbol(exp, allowed, 0);
148      grammar.AddAllowedChildSymbol(exp, variable);
149
150      sine.Enabled = false;
151      foreach (var allowed in grammar.GetAllowedChildSymbols(sine))
152        grammar.RemoveAllowedChildSymbol(sine, allowed);
153      foreach (var allowed in grammar.GetAllowedChildSymbols(sine, 0))
154        grammar.RemoveAllowedChildSymbol(sine, allowed, 0);
155      grammar.AddAllowedChildSymbol(sine, variable);
156
157      cosine.Enabled = false;
158      foreach (var allowed in grammar.GetAllowedChildSymbols(cosine))
159        grammar.RemoveAllowedChildSymbol(cosine, allowed);
160      foreach (var allowed in grammar.GetAllowedChildSymbols(cosine, 0))
161        grammar.RemoveAllowedChildSymbol(cosine, allowed, 0);
162      grammar.AddAllowedChildSymbol(cosine, variable);
163
164      tangent.Enabled = false;
165      foreach (var allowed in grammar.GetAllowedChildSymbols(tangent))
166        grammar.RemoveAllowedChildSymbol(tangent, allowed);
167      foreach (var allowed in grammar.GetAllowedChildSymbols(tangent, 0))
168        grammar.RemoveAllowedChildSymbol(tangent, allowed, 0);
169      grammar.AddAllowedChildSymbol(tangent, variable);
170      #endregion
171
172      problem.SymbolicExpressionTreeGrammar = grammar;
173
174      // configure remaining problem parameters
175      problem.MaximumSymbolicExpressionTreeLength.Value = 50;
176      problem.MaximumSymbolicExpressionTreeDepth.Value = 12;
177      problem.MaximumFunctionDefinitions.Value = 0;
178      problem.MaximumFunctionArguments.Value = 0;
179
180      var evaluator = new SymbolicRegressionConstantOptimizationEvaluator();
181      evaluator.ConstantOptimizationIterations.Value = 5;
182      problem.EvaluatorParameter.Value = evaluator;
183      problem.RelativeNumberOfEvaluatedSamplesParameter.Hidden = true;
184      problem.SolutionCreatorParameter.Hidden = true;
185      #endregion
186
187      #region Algorithm Configuration
188      osga.Problem = problem;
189      osga.Name = "Offspring Selection Genetic Programming - Symbolic Regression";
190      osga.Description = "Genetic programming with strict offspring selection for solving a benchmark regression problem.";
191      SamplesUtils.ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(osga, 100, 1, 25, 0.2, 50);
192      var mutator = (MultiSymbolicExpressionTreeManipulator)osga.Mutator;
193      mutator.Operators.OfType<FullTreeShaker>().Single().ShakingFactor = 0.1;
194      mutator.Operators.OfType<OnePointShaker>().Single().ShakingFactor = 1.0;
195
196      osga.Analyzer.Operators.SetItemCheckedState(
197        osga.Analyzer.Operators
198          .OfType<SymbolicRegressionSingleObjectiveOverfittingAnalyzer>()
199          .Single(), false);
200      osga.Analyzer.Operators.SetItemCheckedState(
201        osga.Analyzer.Operators
202          .OfType<SymbolicDataAnalysisAlleleFrequencyAnalyzer>()
203          .First(), false);
204
205      osga.ComparisonFactorModifierParameter.Hidden = true;
206      osga.ComparisonFactorLowerBoundParameter.Hidden = true;
207      osga.ComparisonFactorUpperBoundParameter.Hidden = true;
208      osga.OffspringSelectionBeforeMutationParameter.Hidden = true;
209      osga.SuccessRatioParameter.Hidden = true;
210      osga.SelectedParentsParameter.Hidden = true;
211      osga.ElitesParameter.Hidden = true;
212
213      #endregion
214      return osga;
215    }
216  }
217}
Note: See TracBrowser for help on using the repository browser.