Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GPTimeSeriesSampleTest.cs @ 11466

Last change on this file since 11466 was 11091, checked in by gkronber, 10 years ago

#1638: created a unit test to create and run a GP time series sample.

File size: 4.1 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.TimeSeriesPrognosis;
28using HeuristicLab.Selection;
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30
31namespace HeuristicLab.Tests {
32  [TestClass]
33  public class GPTimeSeriesSampleTest {
34    private const string samplesDirectory = SamplesUtils.Directory;
35    [ClassInitialize]
36    public static void MyClassInitialize(TestContext testContext) {
37      if (!Directory.Exists(samplesDirectory))
38        Directory.CreateDirectory(samplesDirectory);
39    }
40
41    [TestMethod]
42    [TestCategory("Samples.Create")]
43    [TestProperty("Time", "medium")]
44    public void CreateGpTimeSeriesSampleTest() {
45      var ga = CreateGpTimeSeriesSample();
46      var path = Path.Combine(samplesDirectory, "OSGP_TimeSeries.hl");
47      XmlGenerator.Serialize(ga, path);
48    }
49    [TestMethod]
50    [TestCategory("Samples.Execute")]
51    [TestProperty("Time", "long")]
52    public void RunGpTimeSeriesSampleTest() {
53      var osga = CreateGpTimeSeriesSample();
54      osga.SetSeedRandomly.Value = false;
55      SamplesUtils.RunAlgorithm(osga);
56
57      Assert.AreEqual(0.020952753415199643, SamplesUtils.GetDoubleResult(osga, "BestQuality"), 1E-8);
58      Assert.AreEqual(0.023220938866319357, SamplesUtils.GetDoubleResult(osga, "CurrentAverageQuality"), 1E-8);
59      Assert.AreEqual(0.023716788824595391, SamplesUtils.GetDoubleResult(osga, "CurrentWorstQuality"), 1E-8);
60      Assert.AreEqual(48200, SamplesUtils.GetIntResult(osga, "EvaluatedSolutions"));
61    }
62
63    public static OffspringSelectionGeneticAlgorithm CreateGpTimeSeriesSample() {
64      var problem = new SymbolicTimeSeriesPrognosisSingleObjectiveProblem();
65      problem.Name = "Symbolic time series prognosis problem (Mackey Glass t=17)";
66      problem.ProblemData.Name = "Mackey Glass t=17";
67      problem.MaximumSymbolicExpressionTreeLength.Value = 125;
68      problem.MaximumSymbolicExpressionTreeDepth.Value = 12;
69      problem.EvaluatorParameter.Value.HorizonParameter.Value.Value = 10;
70
71      foreach (var symbol in problem.SymbolicExpressionTreeGrammar.Symbols) {
72        if (symbol is Exponential || symbol is Logarithm) {
73          symbol.Enabled = false;
74        } else if (symbol is AutoregressiveTargetVariable) {
75          symbol.Enabled = true;
76          var autoRegressiveSymbol = symbol as AutoregressiveTargetVariable;
77          autoRegressiveSymbol.MinLag = -30;
78          autoRegressiveSymbol.MaxLag = -1;
79        }
80      }
81
82      var osga = new OffspringSelectionGeneticAlgorithm();
83      osga.Name = "Genetic Programming - Time Series Prediction (Mackey-Glass-17)";
84      osga.Description = "A genetic programming algorithm for creating a time-series model for the Mackey-Glass-17 time series.";
85      osga.Problem = problem;
86      SamplesUtils.ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>
87        (osga, popSize: 100, elites: 1, maxGens: 25, mutationRate: 0.15);
88      osga.MaximumSelectionPressure.Value = 100;
89      return osga;
90
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.