Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GBT/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GPMultiplexerSampleTest.cs @ 12329

Last change on this file since 12329 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 SampleFileName = "GP_Multiplexer";
36
37    [TestMethod]
38    [TestCategory("Samples.Create")]
39    [TestProperty("Time", "medium")]
40    public void CreateGpMultiplexerSampleTest() {
41      var ga = CreateGpMultiplexerSample();
42      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
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.