[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 |
|
---|
[10396] | 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
[7845] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[10396] | 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[9707] | 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
[7845] | 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[17687] | 32 | using HEAL.Attic;
|
---|
[7845] | 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 35 | [Item("Sliding Window GP Analyzer", "Base class for concrete sliding window GP analyzers.")]
|
---|
[17687] | 36 | [StorableType("FDBFFC8A-27BF-44F7-B499-352443B65DBA")]
|
---|
[10396] | 37 | public abstract class SlidingWindowAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer {
|
---|
[7845] | 38 | private const string ProblemDataParameterName = "ProblemData";
|
---|
[9707] | 39 | private const string EvaluatorParameterName = "Evaluator";
|
---|
[7845] | 40 | private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
|
---|
[9164] | 41 | private const string ValidationPartitionParameterName = "ValidationPartition";
|
---|
[7845] | 42 | private const string SlidingWindowSizeParameterName = "Sliding Window Size";
|
---|
[9164] | 43 | private const string ValidationSlidingWindowSizeParameterName = "Validation Sliding Window Size";
|
---|
[7845] | 44 | private const string SlidingWindowStepWidthParameterName = "Sliding Window Step Width";
|
---|
[9145] | 45 | private const string InitialSlidingWindowParameterName = "Initial Sliding Window";
|
---|
[9162] | 46 | private const string TerminateSlidingWindowParameterName = "TerminateSlidingWindow";
|
---|
[10396] | 47 | private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
[10720] | 48 | protected const string BestSlidingWindowSolutionsResultName = "Best Sliding Window Solutions";
|
---|
[7845] | 49 |
|
---|
| 50 | #region parameter properties
|
---|
[10396] | 51 | private ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter {
|
---|
| 52 | get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }
|
---|
| 53 | }
|
---|
[9162] | 54 | public IValueLookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
|
---|
| 55 | get { return (IValueLookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
[7845] | 56 | }
|
---|
| 57 | public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
|
---|
| 58 | get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
|
---|
| 59 | }
|
---|
[9164] | 60 | public ILookupParameter<IntRange> ValidationPartitionParameter {
|
---|
| 61 | get { return (ILookupParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
|
---|
| 62 | }
|
---|
[7845] | 63 | public IFixedValueParameter<IntValue> SlidingWindowSizeParameter {
|
---|
| 64 | get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowSizeParameterName]; }
|
---|
| 65 | }
|
---|
[9164] | 66 | public IFixedValueParameter<IntValue> ValidationSlidingWindowSizeParameter {
|
---|
| 67 | get { return (IFixedValueParameter<IntValue>)Parameters[ValidationSlidingWindowSizeParameterName]; }
|
---|
| 68 | }
|
---|
[7845] | 69 | public IFixedValueParameter<IntValue> SlidingWindowStepWidthParameter {
|
---|
| 70 | get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowStepWidthParameterName]; }
|
---|
| 71 | }
|
---|
[9145] | 72 | public IFixedValueParameter<IntRange> InitialSlidingWindowParameter {
|
---|
[9162] | 73 | get { return (IFixedValueParameter<IntRange>)Parameters[InitialSlidingWindowParameterName]; }
|
---|
[9145] | 74 | }
|
---|
[9162] | 75 | public ILookupParameter<BoolValue> TerminateSlidingWindowParameter {
|
---|
| 76 | get { return (ILookupParameter<BoolValue>)Parameters[TerminateSlidingWindowParameterName]; }
|
---|
| 77 | }
|
---|
[9707] | 78 | public ILookupParameter<IEvaluator> EvaluatorParameter {
|
---|
| 79 | get { return (ILookupParameter<IEvaluator>)Parameters[EvaluatorParameterName]; }
|
---|
| 80 | }
|
---|
[7845] | 81 | #endregion
|
---|
| 82 |
|
---|
| 83 | #region properties
|
---|
[9162] | 84 | public override bool EnabledByDefault { get { return false; } }
|
---|
[7845] | 85 | public IntValue SlidingWindowSize { get { return SlidingWindowSizeParameter.Value; } }
|
---|
[10396] | 86 | public IntValue ValidationSlidingWindowSize { get { return ValidationSlidingWindowSizeParameter.Value; } }
|
---|
[7845] | 87 | public IntValue SlidingWindowStepWidth { get { return SlidingWindowStepWidthParameter.Value; } }
|
---|
[9145] | 88 | public IntRange InitialSlidingWindow { get { return InitialSlidingWindowParameter.Value; } }
|
---|
[10396] | 89 | public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get { return InterpreterParameter.ActualValue; } }
|
---|
| 90 | public IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }
|
---|
[7845] | 91 | #endregion
|
---|
| 92 |
|
---|
| 93 | [StorableConstructor]
|
---|
[17687] | 94 | protected SlidingWindowAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
[9162] | 95 | protected SlidingWindowAnalyzer(SlidingWindowAnalyzer original, Cloner cloner)
|
---|
[7845] | 96 | : base(original, cloner) { }
|
---|
| 97 |
|
---|
[9162] | 98 | protected SlidingWindowAnalyzer()
|
---|
| 99 | : base() {
|
---|
| 100 | Parameters.Add(new ValueLookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
|
---|
[9707] | 101 | Parameters.Add(new LookupParameter<IEvaluator>(EvaluatorParameterName, ""));
|
---|
[7845] | 102 | Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, ""));
|
---|
[9164] | 103 | Parameters.Add(new LookupParameter<IntRange>(ValidationPartitionParameterName, ""));
|
---|
[7845] | 104 | Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowSizeParameterName, "", new IntValue(1)));
|
---|
[9164] | 105 | Parameters.Add(new FixedValueParameter<IntValue>(ValidationSlidingWindowSizeParameterName, "", new IntValue(0)));
|
---|
[7845] | 106 | Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowStepWidthParameterName, "", new IntValue(1)));
|
---|
[9145] | 107 | Parameters.Add(new FixedValueParameter<IntRange>(InitialSlidingWindowParameterName, "", new IntRange(0, 1)));
|
---|
[9162] | 108 | Parameters.Add(new LookupParameter<BoolValue>(TerminateSlidingWindowParameterName, ""));
|
---|
[10396] | 109 | if (!Parameters.ContainsKey(InterpreterParameterName))
|
---|
| 110 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName, ""));
|
---|
| 111 | if (!Parameters.ContainsKey("Quality"))
|
---|
| 112 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
|
---|
| 113 | if (!Parameters.ContainsKey("Maximization"))
|
---|
| 114 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "The direction of optimization."));
|
---|
| 115 | ProblemDataParameter.Hidden = true;
|
---|
[9145] | 116 |
|
---|
[7845] | 117 | }
|
---|
| 118 |
|
---|
[9707] | 119 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 120 | private void AfterDeserialization() {
|
---|
| 121 | if (!Parameters.ContainsKey(EvaluatorParameterName))
|
---|
| 122 | Parameters.Add(new LookupParameter<IEvaluator>(EvaluatorParameterName, ""));
|
---|
[10396] | 123 | if (!Parameters.ContainsKey(InterpreterParameterName))
|
---|
| 124 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName, ""));
|
---|
| 125 | if (!Parameters.ContainsKey("Quality"))
|
---|
| 126 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
|
---|
| 127 | if (!Parameters.ContainsKey("Maximization"))
|
---|
| 128 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "The direction of optimization."));
|
---|
| 129 | if (!Parameters.ContainsKey(ProblemDataParameterName))
|
---|
| 130 | Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName));
|
---|
[9707] | 131 | }
|
---|
| 132 |
|
---|
[7845] | 133 | public override IOperation Apply() {
|
---|
[9162] | 134 | //intialize sliding window
|
---|
[9145] | 135 | if (FitnessCalculationPartitionParameter.ActualValue == null) {
|
---|
[9162] | 136 | TerminateSlidingWindowParameter.ActualValue = new BoolValue(false);
|
---|
[9145] | 137 | FitnessCalculationPartitionParameter.ActualValue = (IntRange)InitialSlidingWindow.Clone();
|
---|
[10396] | 138 | ValidationPartitionParameter.ActualValue = new IntRange(InitialSlidingWindow.End, InitialSlidingWindow.End + ValidationSlidingWindowSize.Value);
|
---|
[9162] | 139 | return base.Apply();
|
---|
| 140 | }
|
---|
[7845] | 141 |
|
---|
[10413] | 142 | SaveBestSolution();
|
---|
| 143 |
|
---|
[9162] | 144 | if (!CheckForUpdate()) return base.Apply();
|
---|
[9145] | 145 |
|
---|
[9162] | 146 | //update necessary - move sliding window
|
---|
| 147 | var fitnessPartition = (IntRange)FitnessCalculationPartitionParameter.ActualValue.Clone();
|
---|
| 148 | if (fitnessPartition.End - fitnessPartition.Start == SlidingWindowSize.Value)
|
---|
| 149 | fitnessPartition.Start += SlidingWindowStepWidth.Value;
|
---|
[7845] | 150 |
|
---|
[9162] | 151 | fitnessPartition.End += SlidingWindowStepWidth.Value;
|
---|
| 152 | if (fitnessPartition.End - fitnessPartition.Start > SlidingWindowSize.Value)
|
---|
| 153 | fitnessPartition.End = fitnessPartition.Start + SlidingWindowSize.Value;
|
---|
[7850] | 154 |
|
---|
[9162] | 155 | //check if update should be performed or if the algorithm should stop
|
---|
[9707] | 156 | if (fitnessPartition.End > ProblemDataParameter.ActualValue.TrainingPartition.End) {
|
---|
[9162] | 157 | TerminateSlidingWindowParameter.ActualValue.Value = true;
|
---|
[9707] | 158 | return base.Apply();
|
---|
[9162] | 159 | }
|
---|
[7845] | 160 |
|
---|
[9707] | 161 | FitnessCalculationPartitionParameter.ActualValue.Start = fitnessPartition.Start;
|
---|
| 162 | FitnessCalculationPartitionParameter.ActualValue.End = fitnessPartition.End;
|
---|
| 163 | ValidationPartitionParameter.ActualValue.Start = fitnessPartition.End;
|
---|
[10396] | 164 | ValidationPartitionParameter.ActualValue.End = ValidationPartitionParameter.ActualValue.Start + ValidationSlidingWindowSize.Value;
|
---|
[9707] | 165 |
|
---|
| 166 | //reevaluate all individuals with new sliding window
|
---|
| 167 | UniformSubScopesProcessor subScopesProcessor = new UniformSubScopesProcessor();
|
---|
| 168 | subScopesProcessor.Operator = EvaluatorParameter.ActualValue;
|
---|
| 169 | subScopesProcessor.Depth.Value = 1;
|
---|
| 170 | var operation = ExecutionContext.CreateChildOperation(subScopesProcessor);
|
---|
| 171 | var successor = base.Apply();
|
---|
| 172 | return new OperationCollection() { operation, successor };
|
---|
[7845] | 173 | }
|
---|
| 174 |
|
---|
[10396] | 175 | private ISymbolicExpressionTree FindBestIndividual() {
|
---|
| 176 | var pop = SymbolicExpressionTree.Zip(Quality, (t, q) => new { Tree = t, Quality = q.Value }).ToList();
|
---|
| 177 | Func<double, double, int> comparer = (a, b) => Maximization.Value ? a.CompareTo(b) : b.CompareTo(a);
|
---|
| 178 | pop.Sort((a, b) => comparer(a.Quality, b.Quality));
|
---|
| 179 | return pop.Last().Tree;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | private void SaveBestSolution() {
|
---|
[10720] | 183 | var bestSolutionsCollection = (SlidingWindowBestSolutionsCollection)ResultCollection[BestSlidingWindowSolutionsResultName].Value;
|
---|
[10396] | 184 |
|
---|
[10720] | 185 | bestSolutionsCollection.ProblemData = ProblemData;
|
---|
| 186 | bestSolutionsCollection.Interpreter = Interpreter;
|
---|
| 187 | bestSolutionsCollection.ApplyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
|
---|
[10396] | 188 |
|
---|
| 189 | var fitnessPartition = FitnessCalculationPartitionParameter.ActualValue;
|
---|
| 190 | var best = FindBestIndividual();
|
---|
[10720] | 191 | var range = new SlidingWindowRange(fitnessPartition.Start, fitnessPartition.End);
|
---|
[10396] | 192 |
|
---|
[10413] | 193 | bestSolutionsCollection[range] = best;
|
---|
[10396] | 194 | }
|
---|
| 195 |
|
---|
[7845] | 196 | protected abstract bool CheckForUpdate();
|
---|
| 197 | }
|
---|
| 198 | }
|
---|