Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SlidingWindow/SlidingWindowAnalyzer.cs @ 10879

Last change on this file since 10879 was 9835, checked in by bburlacu, 11 years ago

#1772: Merged remaining trunk changes into the EvolutionaryTracking branch.

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