source: branches/symbreg-factors-2650/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GPTimeSeriesSampleTest.cs @ 14542

Last change on this file since 14542 was 14542, checked in by gkronber, 3 years ago

#2650: merged r14504:14533 from trunk to branch

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 SampleFileName = "OSGP_TimeSeries";
35
36    [TestMethod]
37    [TestCategory("Samples.Create")]
38    [TestProperty("Time", "medium")]
39    public void CreateGpTimeSeriesSampleTest() {
40      var ga = CreateGpTimeSeriesSample();
41      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
42      XmlGenerator.Serialize(ga, path);
43    }
44    [TestMethod]
45    [TestCategory("Samples.Execute")]
46    [TestProperty("Time", "long")]
47    public void RunGpTimeSeriesSampleTest() {
48      var osga = CreateGpTimeSeriesSample();
49      osga.SetSeedRandomly.Value = false;
50      SamplesUtils.RunAlgorithm(osga);
51
52      Assert.AreEqual(0.015441526903606416, SamplesUtils.GetDoubleResult(osga, "BestQuality"), 1E-8);
53      Assert.AreEqual(0.017420834241279298, SamplesUtils.GetDoubleResult(osga, "CurrentAverageQuality"), 1E-8);
54      Assert.AreEqual(0.065195703753298972, SamplesUtils.GetDoubleResult(osga, "CurrentWorstQuality"), 1E-8);
55      Assert.AreEqual(92000, SamplesUtils.GetIntResult(osga, "EvaluatedSolutions"));
56    }
57
58    public static OffspringSelectionGeneticAlgorithm CreateGpTimeSeriesSample() {
59      var problem = new SymbolicTimeSeriesPrognosisSingleObjectiveProblem();
60      problem.Name = "Symbolic time series prognosis problem (Mackey Glass t=17)";
61      problem.ProblemData.Name = "Mackey Glass t=17";
62      problem.MaximumSymbolicExpressionTreeLength.Value = 125;
63      problem.MaximumSymbolicExpressionTreeDepth.Value = 12;
64      problem.EvaluatorParameter.Value.HorizonParameter.Value.Value = 10;
65      problem.ApplyLinearScaling.Value = true;
66
67      foreach (var symbol in problem.SymbolicExpressionTreeGrammar.Symbols) {
68        if (symbol is Exponential || symbol is Logarithm) {
69          symbol.Enabled = false;
70        } else if (symbol is AutoregressiveTargetVariable) {
71          symbol.Enabled = true;
72          var autoRegressiveSymbol = symbol as AutoregressiveTargetVariable;
73          autoRegressiveSymbol.MinLag = -30;
74          autoRegressiveSymbol.MaxLag = -1;
75        }
76      }
77
78      var osga = new OffspringSelectionGeneticAlgorithm();
79      osga.Name = "Genetic Programming - Time Series Prediction (Mackey-Glass-17)";
80      osga.Description = "A genetic programming algorithm for creating a time-series model for the Mackey-Glass-17 time series.";
81      osga.Problem = problem;
82      SamplesUtils.ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>
83        (osga, popSize: 100, elites: 1, maxGens: 25, mutationRate: 0.15);
84      osga.MaximumSelectionPressure.Value = 100;
85      return osga;
86
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.