Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10894 was 10403, checked in by bburlacu, 11 years ago

#1837: Added SlidingWindowQualitiesAnalyzer.

File size: 7.3 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.Linq;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.SlidingWindow {
32  [StorableClass]
33  [Item("Sliding Window Qualities Analyzer", "Analyzer that computes the qualities of the best solution on past, current, and future regions of the sliding window training data.")]
34  public sealed class SlidingWindowQualitiesAnalyzer : SymbolicDataAnalysisAnalyzer {
35    private const string SlidingWindowQualitiesResultName = "Sliding Window Qualities";
36    private const string ProblemDataParameterName = "ProblemData";
37    private const string EvaluatorParameterName = "Evaluator";
38    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
39    private const string ValidationPartitionParameterName = "ValidationPartition";
40    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
41
42    public IValueLookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
43      get { return (IValueLookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
44    }
45    public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
46      get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
47    }
48    public ILookupParameter<IntRange> ValidationPartitionParameter {
49      get { return (ILookupParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
50    }
51    public ILookupParameter<IEvaluator> EvaluatorParameter {
52      get { return (ILookupParameter<IEvaluator>)Parameters[EvaluatorParameterName]; }
53    }
54
55    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreter {
56      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
57    }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new SlidingWindowQualitiesAnalyzer(this, cloner);
61    }
62    private SlidingWindowQualitiesAnalyzer(SlidingWindowQualitiesAnalyzer original, Cloner cloner)
63      : base(original, cloner) {
64    }
65
66
67
68    [StorableConstructor]
69    private SlidingWindowQualitiesAnalyzer(bool deserializing) : base(deserializing) { }
70
71    public SlidingWindowQualitiesAnalyzer() {
72      Parameters.Add(new ValueLookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
73      Parameters.Add(new LookupParameter<IEvaluator>(EvaluatorParameterName, ""));
74      Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, ""));
75      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, ""));
76
77      ProblemDataParameter.Hidden = true;
78    }
79
80    [StorableHook(HookType.AfterDeserialization)]
81    private void AfterDeserialization() {
82    }
83
84    public override IOperation Apply() {
85      if (FitnessCalculationPartitionParameter.ActualValue == null)
86        // do nothing because the sliding window hasn't been initialized yet
87        return base.Apply();
88
89
90      var results = ResultCollectionParameter.ActualValue;
91      if (!results.ContainsKey("Best training solution")) return base.Apply();
92
93      var problemData = (IRegressionProblemData)ProblemDataParameter.ActualValue;
94      var evaluator = (SymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>)EvaluatorParameter.ActualValue;
95      var context = new Core.ExecutionContext(ExecutionContext, evaluator, new Scope());
96      var fitnessCalculationPartition = FitnessCalculationPartitionParameter.ActualValue;
97
98      var bestSolution = (ISymbolicDataAnalysisSolution)results["Best training solution"].Value;
99      var bestModel = bestSolution.Model;
100      var bestTree = bestModel.SymbolicExpressionTree;
101      // add result
102      if (!results.ContainsKey(SlidingWindowQualitiesResultName)) {
103        results.Add(new Result(SlidingWindowQualitiesResultName, new DataTable(SlidingWindowQualitiesResultName)));
104      }
105      var swQualitiesTable = (DataTable)results[SlidingWindowQualitiesResultName].Value;
106
107      // compute before quality
108      var beforeQuality = 0.0;
109      if (!swQualitiesTable.Rows.ContainsKey("Before Quality"))
110        swQualitiesTable.Rows.Add(new DataRow("Before Quality") { VisualProperties = { StartIndexZero = true } });
111      if (fitnessCalculationPartition.Start > problemData.TrainingPartition.Start) {
112        var beforeRange = new IntRange(problemData.TrainingPartition.Start, fitnessCalculationPartition.Start);
113        beforeQuality = evaluator.Evaluate(context, bestTree, problemData, Enumerable.Range(beforeRange.Start, beforeRange.Size));
114      }
115      swQualitiesTable.Rows["Before Quality"].Values.Add(beforeQuality);
116
117      // compute current quality
118      var currentQuality = ((DoubleValue)results["CurrentBestQuality"].Value).Value;
119      if (!swQualitiesTable.Rows.ContainsKey("Current Quality"))
120        swQualitiesTable.Rows.Add(new DataRow("Current Quality") { VisualProperties = { StartIndexZero = true } });
121      swQualitiesTable.Rows["Current Quality"].Values.Add(currentQuality);
122
123      // compute after quality
124      if (fitnessCalculationPartition.End < problemData.TrainingPartition.End) {
125        var afterRange = new IntRange(fitnessCalculationPartition.End, problemData.TrainingPartition.End);
126        var afterQuality = evaluator.Evaluate(context, bestTree, problemData,
127          Enumerable.Range(afterRange.Start, afterRange.Size));
128        if (!swQualitiesTable.Rows.ContainsKey("After Quality"))
129          swQualitiesTable.Rows.Add(new DataRow("After Quality") { VisualProperties = { StartIndexZero = true } });
130        swQualitiesTable.Rows["After Quality"].Values.Add(afterQuality);
131      }
132      // compute test quality
133      if (!swQualitiesTable.Rows.ContainsKey("Test Quality"))
134        swQualitiesTable.Rows.Add(new DataRow("Test Quality") { VisualProperties = { StartIndexZero = true } });
135      var regressionSolution = (IRegressionSolution)bestSolution;
136      swQualitiesTable.Rows["Test Quality"].Values.Add(regressionSolution.TestRSquared);
137      return base.Apply();
138    }
139
140    public override bool EnabledByDefault { get { return false; } }
141  }
142}
Note: See TracBrowser for help on using the repository browser.