Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3057_DynamicALPS/TestProblems/oesr-alps-master/HeuristicLab.Algorithms.OESRALPS/Analyzers/Regression/SymbolicRegressionAdaptiveSlidingWindowAnalyzer.cs

Last change on this file 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: 7.1 KB
Line 
1using HEAL.Attic;
2using HeuristicLab.Algorithms.OESRALPS.DriftDetection;
3using HeuristicLab.Analysis;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
8using HeuristicLab.Operators;
9using HeuristicLab.Optimization;
10using HeuristicLab.Parameters;
11using HeuristicLab.Problems.DataAnalysis;
12using HeuristicLab.Problems.DataAnalysis.Symbolic;
13using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
14using System;
15using System.Collections.Generic;
16using System.Linq;
17using System.Text;
18using System.Threading.Tasks;
19
20namespace HeuristicLab.Algorithms.OESRALPS.Analyzers.Regression
21{
22    [Item("SymbolicRegressionGenerationalAdaptiveSlidingWindowAnalyzer", "Symbolic Regression Analyzer which moves a sliding window every n-th generation over the training partition and adapts the window size, when a change is detected.")]
23    [StorableType("936D341B-0AC1-49A8-B4D9-DCE13BDB7114")]
24    public sealed class SymbolicRegressionAdaptiveSlidingWindowAnalyzer : SlidingWindowAnalyzer<ISymbolicRegressionSingleObjectiveEvaluator, IRegressionProblemData>
25    {
26        private const string LayerResultsParameterName = "LayerResults";
27
28        private const string DriftDetectedParameterName = "Drift Detected";
29        private const string AdwinWindowSizeParameterName = "ADWIN Window Size";
30
31        private const string SlidingWindowSizeChart = "Adaptive Sliding Window Size";
32
33        private const string DeltaParameterName = "Delta";
34
35        [Storable]
36        private ADWINWrapper adwin;
37
38        #region parameter properties
39        public IScopeTreeLookupParameter<ResultCollection> LayerResultsParameterParameter {
40            get { return (IScopeTreeLookupParameter<ResultCollection>)Parameters[LayerResultsParameterName]; }
41        }
42        public IFixedValueParameter<DoubleValue> DeltaParameter {
43            get { return (IFixedValueParameter<DoubleValue>)Parameters[DeltaParameterName]; }
44        }
45        #endregion
46
47        [StorableConstructor]
48        private SymbolicRegressionAdaptiveSlidingWindowAnalyzer(StorableConstructorFlag _) : base(_) { }
49        private SymbolicRegressionAdaptiveSlidingWindowAnalyzer(SymbolicRegressionAdaptiveSlidingWindowAnalyzer original, Cloner cloner) : base(original, cloner) { }
50        public SymbolicRegressionAdaptiveSlidingWindowAnalyzer()
51          : base()
52        {
53            Parameters.Add(new ScopeTreeLookupParameter<ResultCollection>(LayerResultsParameterName, "Results of all Layers.") { Depth = 1 });
54            Parameters.Add(new FixedValueParameter<DoubleValue>(DeltaParameterName, "The confidence value for hypothesis test.", new DoubleValue(0.1)));
55
56            SlidingWindowSize.Value = 100;
57            SlidingWindowStepWidth.Value = (int)(SlidingWindowSize.Value * 0.1);
58
59            SlidingWindowSizeParameter.Hidden = true;
60            SlidingWindowStepWidthParameter.Hidden = true;
61
62            adwin = new ADWINWrapper(DeltaParameter.Value.Value, 200, 20, 20);
63        }
64        public override IDeepCloneable Clone(Cloner cloner)
65        {
66            return new SymbolicRegressionAdaptiveSlidingWindowAnalyzer(this, cloner);
67        }
68
69        public override IOperation Apply()
70        {
71            #region DataTable Parameters
72            var results = ResultCollection;
73            if (!results.ContainsKey(SlidingWindowSizeChart))
74                results.Add(new Result(SlidingWindowSizeChart, new DataTable(SlidingWindowSizeChart)));
75
76            var slidingWindowSizeChart = (DataTable)results[SlidingWindowSizeChart].Value;
77
78            if (!slidingWindowSizeChart.Rows.ContainsKey(AdwinWindowSizeParameterName))
79                slidingWindowSizeChart.Rows.Add(new DataRow(AdwinWindowSizeParameterName));
80            if (!slidingWindowSizeChart.Rows.ContainsKey(DriftDetectedParameterName))
81                slidingWindowSizeChart.Rows.Add(new DataRow(DriftDetectedParameterName) { VisualProperties = { SecondYAxis = true } });
82            #endregion
83
84            if (TrainingPartitionParameter.ActualValue == null
85                || ValidationPartitionParameter == null) {
86                adwin = new ADWINWrapper(DeltaParameter.Value.Value, 200, 20, 20);
87                SlidingWindowSize.Value = 100;
88                SlidingWindowStepWidth.Value = (int)(SlidingWindowSize.Value * 0.1);
89                TerminateSlidingWindowParameter.ActualValue.Value = false;
90                InitializeSlidingWindow(StartSlidingWindow.Value, SlidingWindowSize.Value);
91                return base.Apply();
92            }
93            if (!IsMinimumIterationIntervalPassed())
94                return base.Apply();
95
96            #region set up evaluation
97            var growingTerm = 1 + (int)(SlidingWindowSize.Value * 0.10); //1 + (int)(SlidingWindowStepWidth.Value * 0.5);
98            if (ValidationPartitionParameter.ActualValue.End >= ProblemData.TestPartition.Start)
99            {
100                TerminateSlidingWindowParameter.ActualValue.Value = true;
101                return base.Apply();
102            }
103            else if (ValidationPartitionParameter.ActualValue.End > ProblemData.TestPartition.Start - growingTerm)
104            {
105                growingTerm = ProblemData.TestPartition.Start - ValidationPartitionParameter.ActualValue.End;
106            }
107           
108            IntRange rangeToEvaluate;
109            if (adwin.NumElementsProcessed == 0)
110                rangeToEvaluate = new IntRange(adwin.Count,
111                    ValidationPartitionParameter.ActualValue.End + growingTerm + 1);
112            else
113                rangeToEvaluate =
114                    new IntRange(adwin.Count,
115                    ValidationPartitionParameter.ActualValue.End + growingTerm);
116
117            var rows = Enumerable.Range(rangeToEvaluate.Start, rangeToEvaluate.Size);
118            #endregion
119           
120            var changeDetected = false;
121            var targetValues = ProblemDataParameter.ActualValue.Dataset.GetDoubleValues(ProblemDataParameter.ActualValue.TargetVariable, rows);
122
123            IList<double> changeBuffer = new List<double>();
124            IList<double> windowSizeBuffer = new List<double>();
125
126            var targetValuesEnumerator = targetValues.GetEnumerator();
127            while (!changeDetected && targetValuesEnumerator.MoveNext())
128            {
129                changeDetected |= adwin.AddElement(targetValuesEnumerator.Current);
130                //changeBuffer.Add(changeDetected ? 1 : 0);
131                //windowSizeBuffer.Add(adwin.WindowSize);
132            }
133
134            InitializeSlidingWindow(
135                adwin.Count - adwin.WindowSize,
136                adwin.WindowSize);
137
138            SlidingWindowSize.Value = adwin.WindowSize;
139            SlidingWindowStepWidth.Value = (int)(SlidingWindowSize.Value * 0.1);
140
141            //slidingWindowSizeChart.Rows[DriftDetectedParameterName].Values.AddRange(changeBuffer);
142            //slidingWindowSizeChart.Rows[AdwinWindowSizeParameterName].Values.AddRange(windowSizeBuffer);
143            results[SlidingWindowSizeChart].Value = slidingWindowSizeChart;
144
145            return base.Apply();
146        }
147    }
148}
Note: See TracBrowser for help on using the repository browser.