source: branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SlidingWindow/SlidingWindowAnalyzer.cs @ 9164

Last change on this file since 9164 was 9164, checked in by mkommend, 7 years ago

#1837: Added validation to sliding window algorithms.

File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
29  [StorableClass]
30  [Item("Sliding Window GP Analyzer", "Base class for concrete sliding window GP analyzers.")]
31  public abstract class SlidingWindowAnalyzer : SymbolicDataAnalysisAnalyzer {
32    private const string ProblemDataParameterName = "ProblemData";
33    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
34    private const string ValidationPartitionParameterName = "ValidationPartition";
35    private const string SlidingWindowSizeParameterName = "Sliding Window Size";
36    private const string ValidationSlidingWindowSizeParameterName = "Validation Sliding Window Size";
37    private const string SlidingWindowStepWidthParameterName = "Sliding Window Step Width";
38    private const string InitialSlidingWindowParameterName = "Initial Sliding Window";
39    private const string TerminateSlidingWindowParameterName = "TerminateSlidingWindow";
40
41    #region parameter properties
42    public IValueLookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
43      get { return (IValueLookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
44    }
45    public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
46      get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
47    }
48    public ILookupParameter<IntRange> ValidationPartitionParameter {
49      get { return (ILookupParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
50    }
51    public IFixedValueParameter<IntValue> SlidingWindowSizeParameter {
52      get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowSizeParameterName]; }
53    }
54    public IFixedValueParameter<IntValue> ValidationSlidingWindowSizeParameter {
55      get { return (IFixedValueParameter<IntValue>)Parameters[ValidationSlidingWindowSizeParameterName]; }
56    }
57    public IFixedValueParameter<IntValue> SlidingWindowStepWidthParameter {
58      get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowStepWidthParameterName]; }
59    }
60    public IFixedValueParameter<IntRange> InitialSlidingWindowParameter {
61      get { return (IFixedValueParameter<IntRange>)Parameters[InitialSlidingWindowParameterName]; }
62    }
63    public ILookupParameter<BoolValue> TerminateSlidingWindowParameter {
64      get { return (ILookupParameter<BoolValue>)Parameters[TerminateSlidingWindowParameterName]; }
65    }
66    #endregion
67
68    #region properties
69    public override bool EnabledByDefault { get { return false; } }
70    public IntValue SlidingWindowSize { get { return SlidingWindowSizeParameter.Value; } }
71    public IntValue ValidiationSlidingWindowSize { get { return ValidationSlidingWindowSizeParameter.Value; } }
72    public IntValue SlidingWindowStepWidth { get { return SlidingWindowStepWidthParameter.Value; } }
73    public IntRange InitialSlidingWindow { get { return InitialSlidingWindowParameter.Value; } }
74    #endregion
75
76
77    [StorableConstructor]
78    protected SlidingWindowAnalyzer(bool deserializing) : base(deserializing) { }
79    protected SlidingWindowAnalyzer(SlidingWindowAnalyzer original, Cloner cloner)
80      : base(original, cloner) { }
81
82    protected SlidingWindowAnalyzer()
83      : base() {
84      Parameters.Add(new ValueLookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
85      Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, ""));
86      Parameters.Add(new LookupParameter<IntRange>(ValidationPartitionParameterName, ""));
87      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowSizeParameterName, "", new IntValue(1)));
88      Parameters.Add(new FixedValueParameter<IntValue>(ValidationSlidingWindowSizeParameterName, "", new IntValue(0)));
89      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowStepWidthParameterName, "", new IntValue(1)));
90      Parameters.Add(new FixedValueParameter<IntRange>(InitialSlidingWindowParameterName, "", new IntRange(0, 1)));
91      Parameters.Add(new LookupParameter<BoolValue>(TerminateSlidingWindowParameterName, ""));
92
93      ProblemDataParameter.Hidden = true;
94    }
95
96    public override IOperation Apply() {
97      //intialize sliding window
98      if (FitnessCalculationPartitionParameter.ActualValue == null) {
99        TerminateSlidingWindowParameter.ActualValue = new BoolValue(false);
100        FitnessCalculationPartitionParameter.ActualValue = (IntRange)InitialSlidingWindow.Clone();
101        ValidationPartitionParameter.ActualValue = new IntRange(InitialSlidingWindow.End, InitialSlidingWindow.End + ValidiationSlidingWindowSize.Value);
102        return base.Apply();
103      }
104
105      if (!CheckForUpdate()) return base.Apply();
106
107      //update necessary - move sliding window
108      var fitnessPartition = (IntRange)FitnessCalculationPartitionParameter.ActualValue.Clone();
109      if (fitnessPartition.End - fitnessPartition.Start == SlidingWindowSize.Value)
110        fitnessPartition.Start += SlidingWindowStepWidth.Value;
111
112      fitnessPartition.End += SlidingWindowStepWidth.Value;
113      if (fitnessPartition.End - fitnessPartition.Start > SlidingWindowSize.Value)
114        fitnessPartition.End = fitnessPartition.Start + SlidingWindowSize.Value;
115
116      //check if update should be performed or if the algorithm should stop
117      if (fitnessPartition.End > ProblemDataParameter.ActualValue.TrainingPartition.End)
118        TerminateSlidingWindowParameter.ActualValue.Value = true;
119      else {
120        FitnessCalculationPartitionParameter.ActualValue.Start = fitnessPartition.Start;
121        FitnessCalculationPartitionParameter.ActualValue.End = fitnessPartition.End;
122        ValidationPartitionParameter.ActualValue.Start = fitnessPartition.End;
123        ValidationPartitionParameter.ActualValue.End = ValidationPartitionParameter.ActualValue.Start + ValidiationSlidingWindowSize.Value;
124      }
125
126      return base.Apply();
127    }
128
129    protected abstract bool CheckForUpdate();
130  }
131}
Note: See TracBrowser for help on using the repository browser.