[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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
[9707] | 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
[7845] | 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace 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";
|
---|
[9707] | 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 | }
|
---|
[9707] | 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."));
|
---|
[9707] | 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 |
|
---|
[9707] | 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
|
---|
[9145] | 111 | if (FitnessCalculationPartitionParameter.ActualValue == null) {
|
---|
[9162] | 112 | TerminateSlidingWindowParameter.ActualValue = new BoolValue(false);
|
---|
[9145] | 113 | FitnessCalculationPartitionParameter.ActualValue = (IntRange)InitialSlidingWindow.Clone();
|
---|
[9164] | 114 | ValidationPartitionParameter.ActualValue = new IntRange(InitialSlidingWindow.End, InitialSlidingWindow.End + ValidiationSlidingWindowSize.Value);
|
---|
[9162] | 115 | return base.Apply();
|
---|
| 116 | }
|
---|
[7845] | 117 |
|
---|
[9162] | 118 | if (!CheckForUpdate()) return base.Apply();
|
---|
[9145] | 119 |
|
---|
[9162] | 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;
|
---|
[7845] | 124 |
|
---|
[9162] | 125 | fitnessPartition.End += SlidingWindowStepWidth.Value;
|
---|
| 126 | if (fitnessPartition.End - fitnessPartition.Start > SlidingWindowSize.Value)
|
---|
| 127 | fitnessPartition.End = fitnessPartition.Start + SlidingWindowSize.Value;
|
---|
[7850] | 128 |
|
---|
[9162] | 129 | //check if update should be performed or if the algorithm should stop
|
---|
[9707] | 130 | if (fitnessPartition.End > ProblemDataParameter.ActualValue.TrainingPartition.End) {
|
---|
[9162] | 131 | TerminateSlidingWindowParameter.ActualValue.Value = true;
|
---|
[9707] | 132 | return base.Apply();
|
---|
[9162] | 133 | }
|
---|
[7845] | 134 |
|
---|
[9707] | 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 };
|
---|
[7845] | 147 | }
|
---|
| 148 |
|
---|
| 149 | protected abstract bool CheckForUpdate();
|
---|
| 150 | }
|
---|
| 151 | }
|
---|