Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 8.0 KB
RevLine 
[7845]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;
[9835]25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
[7845]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.")]
[9162]33  public abstract class SlidingWindowAnalyzer : SymbolicDataAnalysisAnalyzer {
[7845]34    private const string ProblemDataParameterName = "ProblemData";
[9835]35    private const string EvaluatorParameterName = "Evaluator";
[7845]36    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
[9164]37    private const string ValidationPartitionParameterName = "ValidationPartition";
[7845]38    private const string SlidingWindowSizeParameterName = "Sliding Window Size";
[9164]39    private const string ValidationSlidingWindowSizeParameterName = "Validation Sliding Window Size";
[7845]40    private const string SlidingWindowStepWidthParameterName = "Sliding Window Step Width";
[9145]41    private const string InitialSlidingWindowParameterName = "Initial Sliding Window";
[9162]42    private const string TerminateSlidingWindowParameterName = "TerminateSlidingWindow";
[7845]43
44    #region parameter properties
[9162]45    public IValueLookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
46      get { return (IValueLookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
[7845]47    }
48    public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
49      get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
50    }
[9164]51    public ILookupParameter<IntRange> ValidationPartitionParameter {
52      get { return (ILookupParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
53    }
[7845]54    public IFixedValueParameter<IntValue> SlidingWindowSizeParameter {
55      get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowSizeParameterName]; }
56    }
[9164]57    public IFixedValueParameter<IntValue> ValidationSlidingWindowSizeParameter {
58      get { return (IFixedValueParameter<IntValue>)Parameters[ValidationSlidingWindowSizeParameterName]; }
59    }
[7845]60    public IFixedValueParameter<IntValue> SlidingWindowStepWidthParameter {
61      get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowStepWidthParameterName]; }
62    }
[9145]63    public IFixedValueParameter<IntRange> InitialSlidingWindowParameter {
[9162]64      get { return (IFixedValueParameter<IntRange>)Parameters[InitialSlidingWindowParameterName]; }
[9145]65    }
[9162]66    public ILookupParameter<BoolValue> TerminateSlidingWindowParameter {
67      get { return (ILookupParameter<BoolValue>)Parameters[TerminateSlidingWindowParameterName]; }
68    }
[9835]69    public ILookupParameter<IEvaluator> EvaluatorParameter {
70      get { return (ILookupParameter<IEvaluator>)Parameters[EvaluatorParameterName]; }
71    }
[7845]72    #endregion
73
74    #region properties
[9162]75    public override bool EnabledByDefault { get { return false; } }
[7845]76    public IntValue SlidingWindowSize { get { return SlidingWindowSizeParameter.Value; } }
[9164]77    public IntValue ValidiationSlidingWindowSize { get { return ValidationSlidingWindowSizeParameter.Value; } }
[7845]78    public IntValue SlidingWindowStepWidth { get { return SlidingWindowStepWidthParameter.Value; } }
[9145]79    public IntRange InitialSlidingWindow { get { return InitialSlidingWindowParameter.Value; } }
[7845]80    #endregion
81
82
83    [StorableConstructor]
84    protected SlidingWindowAnalyzer(bool deserializing) : base(deserializing) { }
[9162]85    protected SlidingWindowAnalyzer(SlidingWindowAnalyzer original, Cloner cloner)
[7845]86      : base(original, cloner) { }
87
[9162]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."));
[9835]91      Parameters.Add(new LookupParameter<IEvaluator>(EvaluatorParameterName, ""));
[7845]92      Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, ""));
[9164]93      Parameters.Add(new LookupParameter<IntRange>(ValidationPartitionParameterName, ""));
[7845]94      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowSizeParameterName, "", new IntValue(1)));
[9164]95      Parameters.Add(new FixedValueParameter<IntValue>(ValidationSlidingWindowSizeParameterName, "", new IntValue(0)));
[7845]96      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowStepWidthParameterName, "", new IntValue(1)));
[9145]97      Parameters.Add(new FixedValueParameter<IntRange>(InitialSlidingWindowParameterName, "", new IntRange(0, 1)));
[9162]98      Parameters.Add(new LookupParameter<BoolValue>(TerminateSlidingWindowParameterName, ""));
[9145]99
[9162]100      ProblemDataParameter.Hidden = true;
[7845]101    }
102
[9835]103    [StorableHook(HookType.AfterDeserialization)]
104    private void AfterDeserialization() {
105      if (!Parameters.ContainsKey(EvaluatorParameterName))
106        Parameters.Add(new LookupParameter<IEvaluator>(EvaluatorParameterName, ""));
107    }
108
[7845]109    public override IOperation Apply() {
[9162]110      //intialize sliding window
[9835]111      TerminateSlidingWindowParameter.ActualValue = new BoolValue(false);
112
[9145]113      if (FitnessCalculationPartitionParameter.ActualValue == null) {
114        FitnessCalculationPartitionParameter.ActualValue = (IntRange)InitialSlidingWindow.Clone();
[9164]115        ValidationPartitionParameter.ActualValue = new IntRange(InitialSlidingWindow.End, InitialSlidingWindow.End + ValidiationSlidingWindowSize.Value);
[9162]116        return base.Apply();
117      }
[7845]118
[9162]119      if (!CheckForUpdate()) return base.Apply();
[9145]120
[9162]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;
[7845]125
[9162]126      fitnessPartition.End += SlidingWindowStepWidth.Value;
127      if (fitnessPartition.End - fitnessPartition.Start > SlidingWindowSize.Value)
128        fitnessPartition.End = fitnessPartition.Start + SlidingWindowSize.Value;
[7850]129
[9162]130      //check if update should be performed or if the algorithm should stop
[9835]131      if (fitnessPartition.End > ProblemDataParameter.ActualValue.TrainingPartition.End) {
[9162]132        TerminateSlidingWindowParameter.ActualValue.Value = true;
[9835]133        return base.Apply();
[9162]134      }
[7845]135
[9835]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 };
[7845]148    }
149
150    protected abstract bool CheckForUpdate();
151  }
152}
Note: See TracBrowser for help on using the repository browser.