[11091] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17246] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11091] | 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 |
|
---|
| 22 | using System.IO;
|
---|
[17035] | 23 | using HEAL.Attic;
|
---|
[11091] | 24 | using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm;
|
---|
| 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 26 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 27 | using HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis;
|
---|
| 28 | using HeuristicLab.Selection;
|
---|
| 29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Tests {
|
---|
| 32 | [TestClass]
|
---|
| 33 | public class GPTimeSeriesSampleTest {
|
---|
[11514] | 34 | private const string SampleFileName = "OSGP_TimeSeries";
|
---|
[11091] | 35 |
|
---|
[17035] | 36 | private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
| 37 |
|
---|
[11091] | 38 | [TestMethod]
|
---|
| 39 | [TestCategory("Samples.Create")]
|
---|
| 40 | [TestProperty("Time", "medium")]
|
---|
| 41 | public void CreateGpTimeSeriesSampleTest() {
|
---|
| 42 | var ga = CreateGpTimeSeriesSample();
|
---|
[11514] | 43 | string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
|
---|
[17035] | 44 | serializer.Serialize(ga, path);
|
---|
[11091] | 45 | }
|
---|
| 46 | [TestMethod]
|
---|
| 47 | [TestCategory("Samples.Execute")]
|
---|
| 48 | [TestProperty("Time", "long")]
|
---|
| 49 | public void RunGpTimeSeriesSampleTest() {
|
---|
| 50 | var osga = CreateGpTimeSeriesSample();
|
---|
| 51 | osga.SetSeedRandomly.Value = false;
|
---|
| 52 | SamplesUtils.RunAlgorithm(osga);
|
---|
| 53 |
|
---|
[14529] | 54 | Assert.AreEqual(0.015441526903606416, SamplesUtils.GetDoubleResult(osga, "BestQuality"), 1E-8);
|
---|
| 55 | Assert.AreEqual(0.017420834241279298, SamplesUtils.GetDoubleResult(osga, "CurrentAverageQuality"), 1E-8);
|
---|
| 56 | Assert.AreEqual(0.065195703753298972, SamplesUtils.GetDoubleResult(osga, "CurrentWorstQuality"), 1E-8);
|
---|
| 57 | Assert.AreEqual(92000, SamplesUtils.GetIntResult(osga, "EvaluatedSolutions"));
|
---|
[11091] | 58 | }
|
---|
| 59 |
|
---|
| 60 | public static OffspringSelectionGeneticAlgorithm CreateGpTimeSeriesSample() {
|
---|
| 61 | var problem = new SymbolicTimeSeriesPrognosisSingleObjectiveProblem();
|
---|
| 62 | problem.Name = "Symbolic time series prognosis problem (Mackey Glass t=17)";
|
---|
| 63 | problem.ProblemData.Name = "Mackey Glass t=17";
|
---|
| 64 | problem.MaximumSymbolicExpressionTreeLength.Value = 125;
|
---|
| 65 | problem.MaximumSymbolicExpressionTreeDepth.Value = 12;
|
---|
| 66 | problem.EvaluatorParameter.Value.HorizonParameter.Value.Value = 10;
|
---|
[14529] | 67 | problem.ApplyLinearScaling.Value = true;
|
---|
[11091] | 68 |
|
---|
| 69 | foreach (var symbol in problem.SymbolicExpressionTreeGrammar.Symbols) {
|
---|
| 70 | if (symbol is Exponential || symbol is Logarithm) {
|
---|
| 71 | symbol.Enabled = false;
|
---|
| 72 | } else if (symbol is AutoregressiveTargetVariable) {
|
---|
| 73 | symbol.Enabled = true;
|
---|
| 74 | var autoRegressiveSymbol = symbol as AutoregressiveTargetVariable;
|
---|
| 75 | autoRegressiveSymbol.MinLag = -30;
|
---|
| 76 | autoRegressiveSymbol.MaxLag = -1;
|
---|
| 77 | }
|
---|
[14832] | 78 | if (symbol is VariableBase) {
|
---|
| 79 | var varSy = symbol as VariableBase;
|
---|
| 80 | varSy.VariableChangeProbability = 1.0; // backwards compatibility
|
---|
| 81 | }
|
---|
[11091] | 82 | }
|
---|
| 83 |
|
---|
| 84 | var osga = new OffspringSelectionGeneticAlgorithm();
|
---|
| 85 | osga.Name = "Genetic Programming - Time Series Prediction (Mackey-Glass-17)";
|
---|
| 86 | osga.Description = "A genetic programming algorithm for creating a time-series model for the Mackey-Glass-17 time series.";
|
---|
| 87 | osga.Problem = problem;
|
---|
| 88 | SamplesUtils.ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>
|
---|
| 89 | (osga, popSize: 100, elites: 1, maxGens: 25, mutationRate: 0.15);
|
---|
| 90 | osga.MaximumSelectionPressure.Value = 100;
|
---|
| 91 | return osga;
|
---|
| 92 |
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|