Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3057_DynamicALPS/TestProblems/oesr-alps-master/HeuristicLab.Algorithms.OESRALPS/Analyzers/SlidingWindowAnalyzer.cs @ 17717

Last change on this file since 17717 was 17479, checked in by kyang, 5 years ago

#3057

  1. upload the latest version of ALPS with SMS-EMOA
  2. upload the related dynamic test problems (dynamic, single-objective symbolic regression), written by David Daninel.
File size: 14.3 KB
Line 
1using HEAL.Attic;
2using HeuristicLab.Algorithms.OESRALPS.Evaluators;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Operators;
7using HeuristicLab.Optimization;
8using HeuristicLab.Parameters;
9using HeuristicLab.Problems.DataAnalysis;
10using HeuristicLab.Problems.DataAnalysis.Symbolic;
11using System;
12using System.Collections.Generic;
13using System.Linq;
14using System.Text;
15using System.Threading.Tasks;
16
17namespace HeuristicLab.Algorithms.OESRALPS.Analyzers
18{
19    [Item("SlidingWindowAnalyzer", "An analyzer which implements a sliding window.")]
20    [StorableType("136DA41B-0AC1-49A8-B419-DCE93BDB7114")]
21    public abstract class SlidingWindowAnalyzer<T, U> : SymbolicDataAnalysisSingleObjectiveAnalyzer,
22        ISlidingWindowAnalyzer, IIterationBasedOperator
23        where T : class, ISymbolicDataAnalysisSingleObjectiveEvaluator<U>
24        where U : class, IDataAnalysisProblemData
25    {
26        private const string ProblemDataParameterName = "ProblemData";
27        private const string EvaluatorParameterName = "Evaluator";
28
29        private const string SlidingWindowSizeParameterName = "SlidingWindowSize";
30        private const string SlidingWindowStepWidthParameterName = "SlidingWindowStepWidth";
31        private const string StartSlidingWindowParameterName = "StartSlidingWindow";
32        private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
33
34        private const string TrainingPartitionParameterName = "TrainingPartition";
35        private const string TestPartitionParameterName = "TestPartition";
36        private const string ValidationPartitionParameterName = "ValidationPartition";
37
38        private const string IterationsParameterName = "Generations";
39        private const string MaximumIterationsParameterName = "MaximumIterations";
40        private const string MinimumIterationsUntilNextMoveParameterName = "MinimumIterationsUntilNextMove";
41
42        private const string LastSlidingWindowMoveIterationParameterName = "LastSlidingWindowMoveIteration";
43
44        private const string PredefinedTestsetParameterName = "PredefinedTestset";
45
46        private const string TerminateSlidingWindowParameterName = "TerminateSlidingWindow";
47
48        protected const double TrainingToValidationRatio = 2.0 / 3.0;
49
50        #region parameter properties
51        public ILookupParameter<U> ProblemDataParameter {
52            get { return (ILookupParameter<U>)Parameters[ProblemDataParameterName]; }
53        }
54        public IFixedValueParameter<IntValue> SlidingWindowSizeParameter {
55            get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowSizeParameterName]; }
56        }
57        public IFixedValueParameter<IntValue> SlidingWindowStepWidthParameter {
58            get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowStepWidthParameterName]; }
59        }
60        public IFixedValueParameter<IntValue> StartSlidingWindowParameter {
61            get { return (IFixedValueParameter<IntValue>)Parameters[StartSlidingWindowParameterName]; }
62        }
63        public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
64            get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
65        }
66        public IValueLookupParameter<IntRange> TrainingPartitionParameter {
67            get { return (IValueLookupParameter<IntRange>)Parameters[TrainingPartitionParameterName]; }
68        }
69        public IValueLookupParameter<IntRange> ValidationPartitionParameter {
70            get { return (IValueLookupParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
71        }
72        public IValueLookupParameter<IntRange> TestPartitionParameter {
73            get { return (IValueLookupParameter<IntRange>)Parameters[TestPartitionParameterName]; }
74        }
75        public ILookupParameter<T> EvaluatorParameter {
76            get { return (ILookupParameter<T>)Parameters[EvaluatorParameterName]; }
77        }
78        public IValueParameter<IntValue> MinimumIterationsUntilNextMoveParameter {
79            get { return (IValueParameter<IntValue>)Parameters[MinimumIterationsUntilNextMoveParameterName]; }
80        }
81        public ILookupParameter<IntValue> IterationsParameter {
82            get { return (ILookupParameter<IntValue>)Parameters[IterationsParameterName]; }
83        }
84        public IValueLookupParameter<IntValue> MaximumIterationsParameter {
85            get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsParameterName]; }
86        }
87        public IValueLookupParameter<IntValue> LastSlidingWindowMoveIterationParameter {
88            get { return (IValueLookupParameter<IntValue>)Parameters[LastSlidingWindowMoveIterationParameterName]; }
89        }
90        public IFixedValueParameter<BoolValue> PredefinedTestsetParameter {
91            get { return (IFixedValueParameter<BoolValue>)Parameters[PredefinedTestsetParameterName]; }
92        }
93        public ILookupParameter<BoolValue> TerminateSlidingWindowParameter {
94            get { return (ILookupParameter<BoolValue>)Parameters[TerminateSlidingWindowParameterName]; }
95        }
96        #endregion
97
98        #region properties
99        public U ProblemData { get { return ProblemDataParameter.ActualValue; } }
100        public IntValue SlidingWindowSize { get { return SlidingWindowSizeParameter.Value; } }
101        public IntValue SlidingWindowStepWidth { get { return SlidingWindowStepWidthParameter.Value; } }
102        public IntValue StartSlidingWindow { get { return StartSlidingWindowParameter.Value; } }
103        public IntRange FitnessCalculationPartition { get { return FitnessCalculationPartitionParameter.ActualValue; } }
104        public IntValue Iterations { get { return IterationsParameter.ActualValue; } }
105        public IntValue MaximumIterations { get { return MaximumIterationsParameter.Value; } }
106        public BoolValue PredefinedTestset {
107            get { return PredefinedTestsetParameter.Value; }
108        }
109        public IntRange TrainingPartition { get { return TrainingPartitionParameter.ActualValue; } }
110
111        public IntRange TestPartition { get { return TestPartitionParameter.ActualValue; } }
112
113        #endregion
114
115        [StorableConstructor]
116        protected SlidingWindowAnalyzer(StorableConstructorFlag _) : base(_) { }
117        protected SlidingWindowAnalyzer(SlidingWindowAnalyzer<T, U> original, Cloner cloner) : base(original, cloner) { }
118        public SlidingWindowAnalyzer()
119          : base()
120        {
121            Parameters.Add(new LookupParameter<U>(ProblemDataParameterName, "The problem data."));
122            Parameters.Add(new LookupParameter<T>(EvaluatorParameterName, ""));
123
124            Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowSizeParameterName, "The sliding window size.", new IntValue(400)));
125            Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowStepWidthParameterName, "The number of steps by which the sliding window is moved.", new IntValue(10)));
126            Parameters.Add(new FixedValueParameter<IntValue>(StartSlidingWindowParameterName, "The starting position of the sliding window.", new IntValue(0)));
127            Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, "The current sliding window position or range."));
128
129            Parameters.Add(new ValueLookupParameter<IntRange>(TrainingPartitionParameterName, "The current training sliding window position or range."));
130            Parameters.Add(new ValueLookupParameter<IntRange>(ValidationPartitionParameterName, "The current validation sliding window position or range."));
131            Parameters.Add(new ValueLookupParameter<IntRange>(TestPartitionParameterName, "The current test sliding window position or range."));
132
133            Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
134            Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
135
136            Parameters.Add(new ValueParameter<IntValue>(MinimumIterationsUntilNextMoveParameterName, "The minimum number of iterations that have to pass before the next sliding window move.", new IntValue(1)));
137
138            Parameters.Add(new ValueLookupParameter<IntValue>(LastSlidingWindowMoveIterationParameterName, "Iteration the sliding window has been moved the last time.") { Hidden = true });
139
140            Parameters.Add(new FixedValueParameter<BoolValue>(PredefinedTestsetParameterName, "Indicates if a predefined split between training and test data is available.", new BoolValue(true)));
141
142            Parameters.Add(new LookupParameter<BoolValue>(TerminateSlidingWindowParameterName, "Indicates if sliding window reached end of training data."));
143        }
144
145        public event EventHandler MoveWindow;
146
147        public IOperation OnMoveWindow()
148        {
149            if (TrainingPartition == null)
150            {
151                TerminateSlidingWindowParameter.ActualValue.Value = false;
152                InitializeSlidingWindow(StartSlidingWindow.Value, SlidingWindowSize.Value);
153                return base.Apply();
154            }
155
156            if (ValidationPartitionParameter.ActualValue.End + SlidingWindowStepWidth.Value
157                > ProblemData.TestPartition.Start)
158            {
159                TerminateSlidingWindowParameter.ActualValue.Value = true;
160                return base.Apply();
161            }
162
163            if(!IsMinimumIterationIntervalPassed())
164                return base.Apply();
165
166            TrainingPartition.Start += SlidingWindowStepWidth.Value;
167            TrainingPartition.End += SlidingWindowStepWidth.Value;
168
169            if (!PredefinedTestsetParameter.Value.Value)
170            {
171                TestPartitionParameter.ActualValue.Start += SlidingWindowStepWidth.Value;
172                TestPartitionParameter.ActualValue.End += SlidingWindowStepWidth.Value;
173            }
174
175            ValidationPartitionParameter.ActualValue.Start += SlidingWindowStepWidth.Value;
176            ValidationPartitionParameter.ActualValue.End += SlidingWindowStepWidth.Value;
177
178            FitnessCalculationPartitionParameter.ActualValue.Start = TrainingPartition.Start;
179            FitnessCalculationPartitionParameter.ActualValue.End = TrainingPartition.End;
180
181            LastSlidingWindowMoveIterationParameter.ActualValue.Value = IterationsParameter.ActualValue.Value;
182
183            VisualizeSlidingWindow();
184
185            // Reevaluate all individuals with new sliding window
186            UniformSubScopesProcessor subScopesProcessor = new UniformSubScopesProcessor();
187            subScopesProcessor.Operator = EvaluatorParameter.ActualValue;
188            subScopesProcessor.Depth.Value = 2;
189            var operation = ExecutionContext.CreateChildOperation(subScopesProcessor);
190            var successor = base.Apply();
191
192            MoveWindow?.Invoke(this, EventArgs.Empty);
193
194            return new OperationCollection() { operation, successor };
195        }
196
197        protected void InitializeSlidingWindow(int startSlidingWindow, int slidingWindowSize)
198        {
199            if (startSlidingWindow + slidingWindowSize > ProblemData.Dataset.Rows)
200                return;
201
202            LastSlidingWindowMoveIterationParameter.ActualValue = new IntValue(IterationsParameter.ActualValue.Value);
203
204            // Training partition is 66 percent of window size
205            TrainingPartitionParameter.ActualValue
206                = new IntRange(
207                    startSlidingWindow,
208                    startSlidingWindow + (int)(slidingWindowSize * TrainingToValidationRatio)
209                    );
210
211            if (PredefinedTestsetParameter.Value.Value)
212            {
213                // Validation partition is 33.3 percent of window size
214                ValidationPartitionParameter.ActualValue.Start = TrainingPartitionParameter.ActualValue.End;
215                ValidationPartitionParameter.ActualValue.End = TrainingPartitionParameter.ActualValue.Start + slidingWindowSize;             
216            }
217            else
218            {
219                // Test partition is 16.6 percent of window size
220                TestPartitionParameter.ActualValue
221                = new IntRange(
222                TrainingPartitionParameter.ActualValue.End,
223                TrainingPartitionParameter.ActualValue.End + (int)((slidingWindowSize - TrainingPartitionParameter.ActualValue.End) * 0.5)
224                );
225
226                // Validation partition is 16.6 percent of window size
227                ValidationPartitionParameter.ActualValue.Start = TestPartitionParameter.ActualValue.End;
228                ValidationPartitionParameter.ActualValue.End = TrainingPartitionParameter.ActualValue.Start + slidingWindowSize;
229            }
230
231            FitnessCalculationPartitionParameter.ActualValue.Start = TrainingPartitionParameter.ActualValue.Start;
232            FitnessCalculationPartitionParameter.ActualValue.End = TrainingPartitionParameter.ActualValue.End;
233
234            VisualizeSlidingWindow();
235
236            return;
237        }
238
239        protected void VisualizeSlidingWindow()
240        {
241            var results = ResultCollection;
242            if (!results.ContainsKey(TrainingPartitionParameterName))
243                results.Add(new Result(TrainingPartitionParameterName, TrainingPartitionParameter.ActualValue));
244            else
245                results[TrainingPartitionParameterName].Value = TrainingPartitionParameter.ActualValue;
246
247            if (!results.ContainsKey(TestPartitionParameterName))
248                results.Add(new Result(TestPartitionParameterName, TestPartitionParameter.ActualValue));
249            else
250                results[TestPartitionParameterName].Value = TestPartitionParameter.ActualValue;
251
252            if (!results.ContainsKey(ValidationPartitionParameterName))
253                results.Add(new Result(ValidationPartitionParameterName, ValidationPartitionParameter.ActualValue));
254            else
255                results[ValidationPartitionParameterName].Value = ValidationPartitionParameter.ActualValue;
256        }
257
258        protected bool IsMinimumIterationIntervalPassed()
259        {
260            return IterationsParameter.ActualValue.Value - LastSlidingWindowMoveIterationParameter.ActualValue.Value
261                >= MinimumIterationsUntilNextMoveParameter.Value.Value;
262        }
263    }
264}
Note: See TracBrowser for help on using the repository browser.