1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 | using System.IO;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 | using HeuristicLab.Persistence.Default.Xml;
|
---|
27 | using HeuristicLab.Problems.DataAnalysis;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
30 | using HeuristicLab.Problems.Instances.DataAnalysis;
|
---|
31 | using HeuristicLab.Selection;
|
---|
32 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Tests {
|
---|
35 | /// <summary>
|
---|
36 | /// Summary description for GPSymbolicRegressionSampleTest
|
---|
37 | /// </summary>
|
---|
38 | [TestClass]
|
---|
39 | public class GPSymbolicRegressionSampleTest {
|
---|
40 | private const string samplesDirectory = SamplesUtils.Directory;
|
---|
41 | [ClassInitialize]
|
---|
42 | public static void MyClassInitialize(TestContext testContext) {
|
---|
43 | if (!Directory.Exists(samplesDirectory))
|
---|
44 | Directory.CreateDirectory(samplesDirectory);
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | [TestMethod]
|
---|
49 | [TestCategory("Samples.Create")]
|
---|
50 | [TestProperty("Time", "medium")]
|
---|
51 | public void CreateGpSymbolicRegressionSampleTest() {
|
---|
52 | var ga = CreateGpSymbolicRegressionSample();
|
---|
53 | XmlGenerator.Serialize(ga, @"Samples\SGP_SymbReg.hl");
|
---|
54 | }
|
---|
55 | [TestMethod]
|
---|
56 | [TestCategory("Samples.Execute")]
|
---|
57 | [TestProperty("Time", "long")]
|
---|
58 | public void RunGpSymbolicRegressionSampleTest() {
|
---|
59 | var ga = CreateGpSymbolicRegressionSample();
|
---|
60 | ga.SetSeedRandomly.Value = false;
|
---|
61 | SamplesUtils.RunAlgorithm(ga);
|
---|
62 | Assert.AreEqual(0.858344291534625, SamplesUtils.GetDoubleResult(ga, "BestQuality"), 1E-8);
|
---|
63 | Assert.AreEqual(0.56758466520692641, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
|
---|
64 | Assert.AreEqual(0, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
|
---|
65 | Assert.AreEqual(50950, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
|
---|
66 | var bestTrainingSolution = (IRegressionSolution)ga.Results["Best training solution"].Value;
|
---|
67 | Assert.AreEqual(0.85504801557844745, bestTrainingSolution.TrainingRSquared, 1E-8);
|
---|
68 | Assert.AreEqual(0.86259381948647817, bestTrainingSolution.TestRSquared, 1E-8);
|
---|
69 | var bestValidationSolution = (IRegressionSolution)ga.Results["Best validation solution"].Value;
|
---|
70 | Assert.AreEqual(0.84854338315539746, bestValidationSolution.TrainingRSquared, 1E-8);
|
---|
71 | Assert.AreEqual(0.8662813452656678, bestValidationSolution.TestRSquared, 1E-8);
|
---|
72 | }
|
---|
73 |
|
---|
74 | private GeneticAlgorithm CreateGpSymbolicRegressionSample() {
|
---|
75 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
76 | #region Problem Configuration
|
---|
77 | SymbolicRegressionSingleObjectiveProblem symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
|
---|
78 | symbRegProblem.Name = "Tower Symbolic Regression Problem";
|
---|
79 | symbRegProblem.Description = "Tower Dataset (downloaded from: http://www.symbolicregression.com/?q=towerProblem)";
|
---|
80 | RegressionRealWorldInstanceProvider provider = new RegressionRealWorldInstanceProvider();
|
---|
81 | var instance = provider.GetDataDescriptors().Where(x => x.Name.Equals("Tower")).Single();
|
---|
82 | var towerProblemData = (RegressionProblemData)provider.LoadData(instance);
|
---|
83 | towerProblemData.TargetVariableParameter.Value = towerProblemData.TargetVariableParameter.ValidValues
|
---|
84 | .First(v => v.Value == "towerResponse");
|
---|
85 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
86 | towerProblemData.InputVariables.Single(x => x.Value == "x1"), true);
|
---|
87 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
88 | towerProblemData.InputVariables.Single(x => x.Value == "x7"), false);
|
---|
89 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
90 | towerProblemData.InputVariables.Single(x => x.Value == "x11"), false);
|
---|
91 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
92 | towerProblemData.InputVariables.Single(x => x.Value == "x16"), false);
|
---|
93 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
94 | towerProblemData.InputVariables.Single(x => x.Value == "x21"), false);
|
---|
95 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
96 | towerProblemData.InputVariables.Single(x => x.Value == "x25"), false);
|
---|
97 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
98 | towerProblemData.InputVariables.Single(x => x.Value == "towerResponse"), false);
|
---|
99 | towerProblemData.TrainingPartition.Start = 0;
|
---|
100 | towerProblemData.TrainingPartition.End = 3136;
|
---|
101 | towerProblemData.TestPartition.Start = 3136;
|
---|
102 | towerProblemData.TestPartition.End = 4999;
|
---|
103 | towerProblemData.Name = "Data imported from towerData.txt";
|
---|
104 | towerProblemData.Description = "Chemical concentration at top of distillation tower, dataset downloaded from: http://vanillamodeling.com/realproblems.html, best R² achieved with nu-SVR = 0.97";
|
---|
105 | symbRegProblem.ProblemData = towerProblemData;
|
---|
106 |
|
---|
107 | // configure grammar
|
---|
108 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
109 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
110 | grammar.Symbols.OfType<VariableCondition>().Single().InitialFrequency = 0.0;
|
---|
111 | var varSymbol = grammar.Symbols.OfType<Variable>().Where(x => !(x is LaggedVariable)).Single();
|
---|
112 | varSymbol.WeightMu = 1.0;
|
---|
113 | varSymbol.WeightSigma = 1.0;
|
---|
114 | varSymbol.WeightManipulatorMu = 0.0;
|
---|
115 | varSymbol.WeightManipulatorSigma = 0.05;
|
---|
116 | varSymbol.MultiplicativeWeightManipulatorSigma = 0.03;
|
---|
117 | var constSymbol = grammar.Symbols.OfType<Constant>().Single();
|
---|
118 | constSymbol.MaxValue = 20;
|
---|
119 | constSymbol.MinValue = -20;
|
---|
120 | constSymbol.ManipulatorMu = 0.0;
|
---|
121 | constSymbol.ManipulatorSigma = 1;
|
---|
122 | constSymbol.MultiplicativeManipulatorSigma = 0.03;
|
---|
123 | symbRegProblem.SymbolicExpressionTreeGrammar = grammar;
|
---|
124 |
|
---|
125 | // configure remaining problem parameters
|
---|
126 | symbRegProblem.BestKnownQuality.Value = 0.97;
|
---|
127 | symbRegProblem.FitnessCalculationPartition.Start = 0;
|
---|
128 | symbRegProblem.FitnessCalculationPartition.End = 2300;
|
---|
129 | symbRegProblem.ValidationPartition.Start = 2300;
|
---|
130 | symbRegProblem.ValidationPartition.End = 3136;
|
---|
131 | symbRegProblem.RelativeNumberOfEvaluatedSamples.Value = 1;
|
---|
132 | symbRegProblem.MaximumSymbolicExpressionTreeLength.Value = 150;
|
---|
133 | symbRegProblem.MaximumSymbolicExpressionTreeDepth.Value = 12;
|
---|
134 | symbRegProblem.MaximumFunctionDefinitions.Value = 0;
|
---|
135 | symbRegProblem.MaximumFunctionArguments.Value = 0;
|
---|
136 |
|
---|
137 | symbRegProblem.EvaluatorParameter.Value = new SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator();
|
---|
138 | #endregion
|
---|
139 | #region Algorithm Configuration
|
---|
140 | ga.Problem = symbRegProblem;
|
---|
141 | ga.Name = "Genetic Programming - Symbolic Regression";
|
---|
142 | ga.Description = "A standard genetic programming algorithm to solve a symbolic regression problem (tower dataset)";
|
---|
143 | SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(
|
---|
144 | ga, 1000, 1, 50, 0.15, 5);
|
---|
145 | var mutator = (MultiSymbolicExpressionTreeManipulator)ga.Mutator;
|
---|
146 | mutator.Operators.OfType<FullTreeShaker>().Single().ShakingFactor = 0.1;
|
---|
147 | mutator.Operators.OfType<OnePointShaker>().Single().ShakingFactor = 1.0;
|
---|
148 |
|
---|
149 | ga.Analyzer.Operators.SetItemCheckedState(
|
---|
150 | ga.Analyzer.Operators
|
---|
151 | .OfType<SymbolicRegressionSingleObjectiveOverfittingAnalyzer>()
|
---|
152 | .Single(), false);
|
---|
153 | ga.Analyzer.Operators.SetItemCheckedState(
|
---|
154 | ga.Analyzer.Operators
|
---|
155 | .OfType<SymbolicDataAnalysisAlleleFrequencyAnalyzer>()
|
---|
156 | .First(), false);
|
---|
157 | #endregion
|
---|
158 | return ga;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|