source: stable/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GPMultiplexerSampleTest.cs @ 11907

Last change on this file since 11907 was 11907, checked in by jkarder, 5 years ago

#2211: merged r11450, r11466, r11483, r11514, r11515 and r11890 into stable
#2234: merged r11308, r11309, r11326, r11337, r11340, r11339, r11342, r11361, r11427, r11447, r11464, r11542, r11544, r11545, r11547, r11548 into stable
#2239: merged r11437, r11439 and r11472 into stable
#2262: merged r11436, r11440, r11471, r11474, r11477, r11479, r11480, r11605, r11657, r11721, r11734, r11735, r11787, r11788, r11789 and r11826 into stable

File size: 5.0 KB
RevLine 
[11051]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
22using System.IO;
23using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25using HeuristicLab.Persistence.Default.Xml;
26using HeuristicLab.Problems.DataAnalysis.Symbolic;
27using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
28using HeuristicLab.Problems.Instances.DataAnalysis;
29using HeuristicLab.Selection;
30using Microsoft.VisualStudio.TestTools.UnitTesting;
31
32namespace HeuristicLab.Tests {
33  [TestClass]
34  public class GPMultiplexerSampleTest {
[11907]35    private const string SampleFileName = "GP_Multiplexer";
[11051]36
37    [TestMethod]
38    [TestCategory("Samples.Create")]
39    [TestProperty("Time", "medium")]
40    public void CreateGpMultiplexerSampleTest() {
41      var ga = CreateGpMultiplexerSample();
[11907]42      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
[11051]43      XmlGenerator.Serialize(ga, path);
44    }
45    [TestMethod]
46    [TestCategory("Samples.Execute")]
47    [TestProperty("Time", "long")]
48    public void RunGpMultiplexerSampleTest() {
49      var osga = CreateGpMultiplexerSample();
50      osga.SetSeedRandomly.Value = false;
51      SamplesUtils.RunAlgorithm(osga);
52
53      Assert.AreEqual(0.125, SamplesUtils.GetDoubleResult(osga, "BestQuality"), 1E-8);
54      Assert.AreEqual(0.237275390625, SamplesUtils.GetDoubleResult(osga, "CurrentAverageQuality"), 1E-8);
55      Assert.AreEqual(1.181640625, SamplesUtils.GetDoubleResult(osga, "CurrentWorstQuality"), 1E-8);
56      Assert.AreEqual(105500, SamplesUtils.GetIntResult(osga, "EvaluatedSolutions"));
57    }
58
59    public static OffspringSelectionGeneticAlgorithm CreateGpMultiplexerSample() {
60      var instanceProvider = new RegressionCSVInstanceProvider();
61      var regressionImportType = new RegressionImportType();
62      regressionImportType.TargetVariable = "output";
63      regressionImportType.TrainingPercentage = 100;
64      var dataAnalysisCSVFormat = new DataAnalysisCSVFormat();
65      dataAnalysisCSVFormat.Separator = ',';
66      dataAnalysisCSVFormat.VariableNamesAvailable = true;
67
68      var problemData = instanceProvider.ImportData(@"Test Resources\Multiplexer11.csv", regressionImportType, dataAnalysisCSVFormat);
69      problemData.Name = "11-Multiplexer";
70
71      var problem = new SymbolicRegressionSingleObjectiveProblem();
72      problem.Name = "11-Multiplexer Problem";
73      problem.ProblemData = problemData;
74      problem.MaximumSymbolicExpressionTreeLength.Value = 50;
75      problem.MaximumSymbolicExpressionTreeDepth.Value = 50;
76      problem.EvaluatorParameter.Value = new SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator();
77      problem.ApplyLinearScaling.Value = false;
78
79
80      var grammar = new FullFunctionalExpressionGrammar();
81      problem.SymbolicExpressionTreeGrammar = grammar;
82      foreach (var symbol in grammar.Symbols) {
83        if (symbol is ProgramRootSymbol) symbol.Enabled = true;
84        else if (symbol is StartSymbol) symbol.Enabled = true;
85        else if (symbol is IfThenElse) symbol.Enabled = true;
86        else if (symbol is And) symbol.Enabled = true;
87        else if (symbol is Or) symbol.Enabled = true;
88        else if (symbol is Xor) symbol.Enabled = true;
89        else if (symbol.GetType() == typeof(Variable)) {
90          //necessary as there are multiple classes derived from Variable (e.g., VariableCondition)
91          symbol.Enabled = true;
92          var variableSymbol = (Variable)symbol;
93          variableSymbol.MultiplicativeWeightManipulatorSigma = 0.0;
94          variableSymbol.WeightManipulatorSigma = 0.0;
95          variableSymbol.WeightSigma = 0.0;
96        } else symbol.Enabled = false;
97      }
98
99      var osga = new OffspringSelectionGeneticAlgorithm();
100      osga.Name = "Genetic Programming - Multiplexer 11 problem";
101      osga.Description = "A genetic programming algorithm that solves the 11-bit multiplexer problem.";
102      osga.Problem = problem;
103      SamplesUtils.ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>
104        (osga, popSize: 100, elites: 1, maxGens: 50, mutationRate: 0.25);
105      osga.MaximumSelectionPressure.Value = 200;
106      return osga;
107
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.