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;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
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.")]
|
---|
33 | public abstract class SlidingWindowAnalyzer<T> : SingleSuccessorOperator, IAnalyzer where T : class,IDataAnalysisProblemData {
|
---|
34 | private const string ProblemDataParameterName = "ProblemData";
|
---|
35 | private const string ResultCollectionParameterName = "Results";
|
---|
36 | private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
|
---|
37 | private const string SlidingWindowParameterName = "Sliding Window";
|
---|
38 | private const string SlidingWindowSizeParameterName = "Sliding Window Size";
|
---|
39 | private const string SlidingWindowStepWidthParameterName = "Sliding Window Step Width";
|
---|
40 |
|
---|
41 | private const string SlidingWindowDataResultName = "Sliding Window";
|
---|
42 |
|
---|
43 | #region parameter properties
|
---|
44 | public IValueLookupParameter<T> ProblemDataParameter {
|
---|
45 | get { return (IValueLookupParameter<T>)Parameters[ProblemDataParameterName]; }
|
---|
46 | }
|
---|
47 | public ILookupParameter<ResultCollection> ResultCollectionParameter {
|
---|
48 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; }
|
---|
49 | }
|
---|
50 | public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
|
---|
51 | get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public IValueLookupParameter<IntRange> SlidingWindowParameter {
|
---|
55 | get { return (IValueLookupParameter<IntRange>)Parameters[SlidingWindowParameterName]; }
|
---|
56 | }
|
---|
57 | public IFixedValueParameter<IntValue> SlidingWindowSizeParameter {
|
---|
58 | get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowSizeParameterName]; }
|
---|
59 | }
|
---|
60 | public IFixedValueParameter<IntValue> SlidingWindowStepWidthParameter {
|
---|
61 | get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowStepWidthParameterName]; }
|
---|
62 | }
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 | #region properties
|
---|
66 | public bool EnabledByDefault { get { return false; } }
|
---|
67 | public IntRange SlidingWindow { get { return SlidingWindowParameter.Value; } }
|
---|
68 | public IntValue SlidingWindowSize { get { return SlidingWindowSizeParameter.Value; } }
|
---|
69 | public IntValue SlidingWindowStepWidth { get { return SlidingWindowStepWidthParameter.Value; } }
|
---|
70 | #endregion
|
---|
71 |
|
---|
72 |
|
---|
73 | [StorableConstructor]
|
---|
74 | protected SlidingWindowAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
75 | protected SlidingWindowAnalyzer(SlidingWindowAnalyzer<T> original, Cloner cloner)
|
---|
76 | : base(original, cloner) { }
|
---|
77 |
|
---|
78 | protected SlidingWindowAnalyzer() {
|
---|
79 | Parameters.Add(new ValueLookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
|
---|
80 | Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, ""));
|
---|
81 | Parameters.Add(new ValueLookupParameter<IntRange>(SlidingWindowParameterName, ""));
|
---|
82 | Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowSizeParameterName, "", new IntValue(1)));
|
---|
83 | Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowStepWidthParameterName, "", new IntValue(1)));
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override IOperation Apply() {
|
---|
87 | if (!ExecutionContext.Scope.Variables.ContainsKey(FitnessCalculationPartitionParameter.ActualName))
|
---|
88 | ExecutionContext.Scope.Variables.Add(new Core.Variable(FitnessCalculationPartitionParameter.ActualName, (IntRange)SlidingWindow.Clone()));
|
---|
89 |
|
---|
90 | if (CheckForUpdate()) {
|
---|
91 | var fitnessPartition = FitnessCalculationPartitionParameter.ActualValue;
|
---|
92 |
|
---|
93 | fitnessPartition.End += SlidingWindowStepWidth.Value;
|
---|
94 | if (fitnessPartition.End - fitnessPartition.Start > SlidingWindowStepWidth.Value)
|
---|
95 | fitnessPartition.End = fitnessPartition.Start + SlidingWindowSize.Value;
|
---|
96 |
|
---|
97 | if (fitnessPartition.End - fitnessPartition.Start == SlidingWindowSize.Value) {
|
---|
98 | fitnessPartition.Start += SlidingWindowStepWidth.Value;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | //create and update result
|
---|
103 | var results = ResultCollectionParameter.ActualValue;
|
---|
104 | SlidingWindowData slidingWindowData;
|
---|
105 | if (!results.ContainsKey(SlidingWindowDataResultName)) {
|
---|
106 | slidingWindowData = new SlidingWindowData(FitnessCalculationPartitionParameter.ActualValue, new double[10]);
|
---|
107 | results.Add(new Result(SlidingWindowDataResultName, slidingWindowData));
|
---|
108 | } else slidingWindowData = (SlidingWindowData)results[SlidingWindowDataResultName];
|
---|
109 |
|
---|
110 | slidingWindowData.SlidingWindowPosition.Start = FitnessCalculationPartitionParameter.ActualValue.Start;
|
---|
111 | slidingWindowData.SlidingWindowPosition.End = FitnessCalculationPartitionParameter.ActualValue.End;
|
---|
112 | return base.Apply();
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | protected abstract bool CheckForUpdate();
|
---|
117 | }
|
---|
118 | }
|
---|