Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SlidingWindow/SlidingWindowVisualizer.cs @ 9779

Last change on this file since 9779 was 9707, checked in by mkommend, 11 years ago

#1837: Adapted sliding window analyzer to reevaluate all individuals if the sliding window gets moved.

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.SlidingWindow {
33  [StorableClass]
34  [Item("Sliding Window Visualizer", "Visualizes the actual sliding window position.")]
35  public sealed class SlidingWindowVisualizer : SymbolicDataAnalysisAnalyzer {
36    private const string ProblemDataParameterName = "ProblemData";
37    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
38
39    private const string SlidingWindowResultName = "Sliding Window";
40    private const string SlidingWindowDataResultName = "Sliding Window Data";
41    private const string BestTrainingSolutionResultName = "Best training solution";
42
43    #region parameter properties
44    public IValueLookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
45      get { return (IValueLookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
46    }
47    public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
48      get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
49    }
50    #endregion
51
52    [StorableConstructor]
53    private SlidingWindowVisualizer(bool deserializing) : base(deserializing) { }
54    private SlidingWindowVisualizer(SlidingWindowVisualizer original, Cloner cloner) : base(original, cloner) { }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new SlidingWindowVisualizer(this, cloner);
57    }
58
59    public SlidingWindowVisualizer()
60      : base() {
61      Parameters.Add(new ValueLookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
62      Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, ""));
63      ProblemDataParameter.Hidden = true;
64    }
65
66    public override IOperation Apply() {
67      //create and update result
68      var results = ResultCollectionParameter.ActualValue;
69
70      IntRange slidingWindow;
71      if (!results.ContainsKey(SlidingWindowResultName)) {
72        slidingWindow = new IntRange();
73        results.Add(new Result(SlidingWindowResultName, slidingWindow));
74      } else slidingWindow = (IntRange)results[SlidingWindowResultName].Value;
75      slidingWindow.Start = FitnessCalculationPartitionParameter.ActualValue.Start;
76      slidingWindow.End = FitnessCalculationPartitionParameter.ActualValue.End;
77
78      SlidingWindowData slidingWindowData;
79      if (!results.ContainsKey(SlidingWindowDataResultName)) {
80        string targetVariable;
81        var classificationProblemData = ProblemDataParameter.ActualValue as IClassificationProblemData;
82        var regressionProblemData = ProblemDataParameter.ActualValue as IRegressionProblemData;
83        if (classificationProblemData != null) targetVariable = classificationProblemData.TargetVariable;
84        else if (regressionProblemData != null) targetVariable = regressionProblemData.TargetVariable;
85        else throw new NotSupportedException();
86
87        var targetData = ProblemDataParameter.ActualValue.Dataset.GetDoubleValues(targetVariable, ProblemDataParameter.ActualValue.TrainingIndices);
88        slidingWindowData = new SlidingWindowData(FitnessCalculationPartitionParameter.ActualValue, targetData);
89        results.Add(new Result(SlidingWindowDataResultName, slidingWindowData));
90      } else slidingWindowData = (SlidingWindowData)results[SlidingWindowDataResultName].Value;
91
92      IEnumerable<double> estimatedValues = Enumerable.Empty<double>();
93      if (results.ContainsKey(BestTrainingSolutionResultName)) {
94        var trainingSolution = results[BestTrainingSolutionResultName].Value;
95        var regressionSolution = trainingSolution as IRegressionSolution;
96        var classificationSolution = trainingSolution as IClassificationSolution;
97
98        if (regressionSolution != null) estimatedValues = regressionSolution.EstimatedTrainingValues;
99        if (classificationSolution != null) estimatedValues = classificationSolution.EstimatedTrainingClassValues;
100      }
101
102      slidingWindowData.SlidingWindowPosition.Start = FitnessCalculationPartitionParameter.ActualValue.Start;
103      slidingWindowData.SlidingWindowPosition.End = FitnessCalculationPartitionParameter.ActualValue.End;
104      slidingWindowData.EstimatedValues = estimatedValues;
105      return base.Apply();
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.