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 System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HEAL.Attic;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
35 | [Item("Sliding Window GP Analyzer", "Base class for concrete sliding window GP analyzers.")]
|
---|
36 | [StorableType("FDBFFC8A-27BF-44F7-B499-352443B65DBA")]
|
---|
37 | public abstract class SlidingWindowAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer {
|
---|
38 | private const string ProblemDataParameterName = "ProblemData";
|
---|
39 | private const string EvaluatorParameterName = "Evaluator";
|
---|
40 | private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
|
---|
41 | private const string ValidationPartitionParameterName = "ValidationPartition";
|
---|
42 | private const string SlidingWindowSizeParameterName = "Sliding Window Size";
|
---|
43 | private const string ValidationSlidingWindowSizeParameterName = "Validation Sliding Window Size";
|
---|
44 | private const string SlidingWindowStepWidthParameterName = "Sliding Window Step Width";
|
---|
45 | private const string InitialSlidingWindowParameterName = "Initial Sliding Window";
|
---|
46 | private const string TerminateSlidingWindowParameterName = "TerminateSlidingWindow";
|
---|
47 | private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
48 | protected const string BestSlidingWindowSolutionsResultName = "Best Sliding Window Solutions";
|
---|
49 |
|
---|
50 | #region parameter properties
|
---|
51 | private ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter {
|
---|
52 | get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }
|
---|
53 | }
|
---|
54 | public IValueLookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
|
---|
55 | get { return (IValueLookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
56 | }
|
---|
57 | public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
|
---|
58 | get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
|
---|
59 | }
|
---|
60 | public ILookupParameter<IntRange> ValidationPartitionParameter {
|
---|
61 | get { return (ILookupParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
|
---|
62 | }
|
---|
63 | public IFixedValueParameter<IntValue> SlidingWindowSizeParameter {
|
---|
64 | get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowSizeParameterName]; }
|
---|
65 | }
|
---|
66 | public IFixedValueParameter<IntValue> ValidationSlidingWindowSizeParameter {
|
---|
67 | get { return (IFixedValueParameter<IntValue>)Parameters[ValidationSlidingWindowSizeParameterName]; }
|
---|
68 | }
|
---|
69 | public IFixedValueParameter<IntValue> SlidingWindowStepWidthParameter {
|
---|
70 | get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowStepWidthParameterName]; }
|
---|
71 | }
|
---|
72 | public IFixedValueParameter<IntRange> InitialSlidingWindowParameter {
|
---|
73 | get { return (IFixedValueParameter<IntRange>)Parameters[InitialSlidingWindowParameterName]; }
|
---|
74 | }
|
---|
75 | public ILookupParameter<BoolValue> TerminateSlidingWindowParameter {
|
---|
76 | get { return (ILookupParameter<BoolValue>)Parameters[TerminateSlidingWindowParameterName]; }
|
---|
77 | }
|
---|
78 | public ILookupParameter<IEvaluator> EvaluatorParameter {
|
---|
79 | get { return (ILookupParameter<IEvaluator>)Parameters[EvaluatorParameterName]; }
|
---|
80 | }
|
---|
81 | #endregion
|
---|
82 |
|
---|
83 | #region properties
|
---|
84 | public override bool EnabledByDefault { get { return false; } }
|
---|
85 | public IntValue SlidingWindowSize { get { return SlidingWindowSizeParameter.Value; } }
|
---|
86 | public IntValue ValidationSlidingWindowSize { get { return ValidationSlidingWindowSizeParameter.Value; } }
|
---|
87 | public IntValue SlidingWindowStepWidth { get { return SlidingWindowStepWidthParameter.Value; } }
|
---|
88 | public IntRange InitialSlidingWindow { get { return InitialSlidingWindowParameter.Value; } }
|
---|
89 | public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get { return InterpreterParameter.ActualValue; } }
|
---|
90 | public IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }
|
---|
91 | #endregion
|
---|
92 |
|
---|
93 | [StorableConstructor]
|
---|
94 | protected SlidingWindowAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
95 | protected SlidingWindowAnalyzer(SlidingWindowAnalyzer original, Cloner cloner)
|
---|
96 | : base(original, cloner) { }
|
---|
97 |
|
---|
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."));
|
---|
101 | Parameters.Add(new LookupParameter<IEvaluator>(EvaluatorParameterName, ""));
|
---|
102 | Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, ""));
|
---|
103 | Parameters.Add(new LookupParameter<IntRange>(ValidationPartitionParameterName, ""));
|
---|
104 | Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowSizeParameterName, "", new IntValue(1)));
|
---|
105 | Parameters.Add(new FixedValueParameter<IntValue>(ValidationSlidingWindowSizeParameterName, "", new IntValue(0)));
|
---|
106 | Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowStepWidthParameterName, "", new IntValue(1)));
|
---|
107 | Parameters.Add(new FixedValueParameter<IntRange>(InitialSlidingWindowParameterName, "", new IntRange(0, 1)));
|
---|
108 | Parameters.Add(new LookupParameter<BoolValue>(TerminateSlidingWindowParameterName, ""));
|
---|
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;
|
---|
116 |
|
---|
117 | }
|
---|
118 |
|
---|
119 | [StorableHook(HookType.AfterDeserialization)]
|
---|
120 | private void AfterDeserialization() {
|
---|
121 | if (!Parameters.ContainsKey(EvaluatorParameterName))
|
---|
122 | Parameters.Add(new LookupParameter<IEvaluator>(EvaluatorParameterName, ""));
|
---|
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));
|
---|
131 | }
|
---|
132 |
|
---|
133 | public override IOperation Apply() {
|
---|
134 | //intialize sliding window
|
---|
135 | if (FitnessCalculationPartitionParameter.ActualValue == null) {
|
---|
136 | TerminateSlidingWindowParameter.ActualValue = new BoolValue(false);
|
---|
137 | FitnessCalculationPartitionParameter.ActualValue = (IntRange)InitialSlidingWindow.Clone();
|
---|
138 | ValidationPartitionParameter.ActualValue = new IntRange(InitialSlidingWindow.End, InitialSlidingWindow.End + ValidationSlidingWindowSize.Value);
|
---|
139 | return base.Apply();
|
---|
140 | }
|
---|
141 |
|
---|
142 | SaveBestSolution();
|
---|
143 |
|
---|
144 | if (!CheckForUpdate()) return base.Apply();
|
---|
145 |
|
---|
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;
|
---|
150 |
|
---|
151 | fitnessPartition.End += SlidingWindowStepWidth.Value;
|
---|
152 | if (fitnessPartition.End - fitnessPartition.Start > SlidingWindowSize.Value)
|
---|
153 | fitnessPartition.End = fitnessPartition.Start + SlidingWindowSize.Value;
|
---|
154 |
|
---|
155 | //check if update should be performed or if the algorithm should stop
|
---|
156 | if (fitnessPartition.End > ProblemDataParameter.ActualValue.TrainingPartition.End) {
|
---|
157 | TerminateSlidingWindowParameter.ActualValue.Value = true;
|
---|
158 | return base.Apply();
|
---|
159 | }
|
---|
160 |
|
---|
161 | FitnessCalculationPartitionParameter.ActualValue.Start = fitnessPartition.Start;
|
---|
162 | FitnessCalculationPartitionParameter.ActualValue.End = fitnessPartition.End;
|
---|
163 | ValidationPartitionParameter.ActualValue.Start = fitnessPartition.End;
|
---|
164 | ValidationPartitionParameter.ActualValue.End = ValidationPartitionParameter.ActualValue.Start + ValidationSlidingWindowSize.Value;
|
---|
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 };
|
---|
173 | }
|
---|
174 |
|
---|
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() {
|
---|
183 | var bestSolutionsCollection = (SlidingWindowBestSolutionsCollection)ResultCollection[BestSlidingWindowSolutionsResultName].Value;
|
---|
184 |
|
---|
185 | bestSolutionsCollection.ProblemData = ProblemData;
|
---|
186 | bestSolutionsCollection.Interpreter = Interpreter;
|
---|
187 | bestSolutionsCollection.ApplyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
|
---|
188 |
|
---|
189 | var fitnessPartition = FitnessCalculationPartitionParameter.ActualValue;
|
---|
190 | var best = FindBestIndividual();
|
---|
191 | var range = new SlidingWindowRange(fitnessPartition.Start, fitnessPartition.End);
|
---|
192 |
|
---|
193 | bestSolutionsCollection[range] = best;
|
---|
194 | }
|
---|
195 |
|
---|
196 | protected abstract bool CheckForUpdate();
|
---|
197 | }
|
---|
198 | }
|
---|