Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GPMultiplexerSampleTest.cs @ 11247

Last change on this file since 11247 was 11051, checked in by mkommend, 11 years ago

#1638: Extracted samples unit test into separate folder, extracted utility methods and added a unit test for the multiplexer problem.

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