Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9779 was 9707, checked in by mkommend, 11 years ago

#1837: Adapted sliding window analyzer to reevaluate all individuals if the sliding window gets moved.

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      if (FitnessCalculationPartitionParameter.ActualValue == null) {
112        TerminateSlidingWindowParameter.ActualValue = new BoolValue(false);
113        FitnessCalculationPartitionParameter.ActualValue = (IntRange)InitialSlidingWindow.Clone();
114        ValidationPartitionParameter.ActualValue = new IntRange(InitialSlidingWindow.End, InitialSlidingWindow.End + ValidiationSlidingWindowSize.Value);
115        return base.Apply();
116      }
117
118      if (!CheckForUpdate()) return base.Apply();
119
120      //update necessary - move sliding window
121      var fitnessPartition = (IntRange)FitnessCalculationPartitionParameter.ActualValue.Clone();
122      if (fitnessPartition.End - fitnessPartition.Start == SlidingWindowSize.Value)
123        fitnessPartition.Start += SlidingWindowStepWidth.Value;
124
125      fitnessPartition.End += SlidingWindowStepWidth.Value;
126      if (fitnessPartition.End - fitnessPartition.Start > SlidingWindowSize.Value)
127        fitnessPartition.End = fitnessPartition.Start + SlidingWindowSize.Value;
128
129      //check if update should be performed or if the algorithm should stop
130      if (fitnessPartition.End > ProblemDataParameter.ActualValue.TrainingPartition.End) {
131        TerminateSlidingWindowParameter.ActualValue.Value = true;
132        return base.Apply();
133      }
134
135      FitnessCalculationPartitionParameter.ActualValue.Start = fitnessPartition.Start;
136      FitnessCalculationPartitionParameter.ActualValue.End = fitnessPartition.End;
137      ValidationPartitionParameter.ActualValue.Start = fitnessPartition.End;
138      ValidationPartitionParameter.ActualValue.End = ValidationPartitionParameter.ActualValue.Start + ValidiationSlidingWindowSize.Value;
139
140      //reevaluate all individuals with new sliding window
141      UniformSubScopesProcessor subScopesProcessor = new UniformSubScopesProcessor();
142      subScopesProcessor.Operator = EvaluatorParameter.ActualValue;
143      subScopesProcessor.Depth.Value = 1;
144      var operation = ExecutionContext.CreateChildOperation(subScopesProcessor);
145      var successor = base.Apply();
146      return new OperationCollection() { operation, successor };
147    }
148
149    protected abstract bool CheckForUpdate();
150  }
151}
Note: See TracBrowser for help on using the repository browser.