Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SlidingWindow/SlidingWindowQualitiesAnalyzer.cs @ 13401

Last change on this file since 13401 was 10464, checked in by bburlacu, 11 years ago

#1772: Maintenance commit, removed some misplaced files, updated license header.

File size: 7.3 KB
RevLine 
[10269]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
[10347]55    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreter {
56      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
[10269]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
[10347]67
[10269]68    [StorableConstructor]
69    private SlidingWindowQualitiesAnalyzer(bool deserializing) : base(deserializing) { }
70
[10347]71    public SlidingWindowQualitiesAnalyzer() {
[10269]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
[10347]77      ProblemDataParameter.Hidden = true;
78    }
[10269]79
80    [StorableHook(HookType.AfterDeserialization)]
81    private void AfterDeserialization() {
82    }
83
[10347]84    public override IOperation Apply() {
[10269]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
[10347]93      var problemData = (IRegressionProblemData)ProblemDataParameter.ActualValue;
[10464]94      var evaluator = (SymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>)EvaluatorParameter.ActualValue;
[10269]95      var context = new Core.ExecutionContext(ExecutionContext, evaluator, new Scope());
96      var fitnessCalculationPartition = FitnessCalculationPartitionParameter.ActualValue;
97
[10347]98      var bestSolution = (ISymbolicDataAnalysisSolution)results["Best training solution"].Value;
[10269]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      }
[10347]105      var swQualitiesTable = (DataTable)results[SlidingWindowQualitiesResultName].Value;
[10269]106
107      // compute before quality
108      var beforeQuality = 0.0;
109      if (!swQualitiesTable.Rows.ContainsKey("Before Quality"))
[10347]110        swQualitiesTable.Rows.Add(new DataRow("Before Quality") { VisualProperties = { StartIndexZero = true } });
111      if (fitnessCalculationPartition.Start > problemData.TrainingPartition.Start) {
[10269]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
[10347]118      var currentQuality = ((DoubleValue)results["CurrentBestQuality"].Value).Value;
[10269]119      if (!swQualitiesTable.Rows.ContainsKey("Current Quality"))
[10347]120        swQualitiesTable.Rows.Add(new DataRow("Current Quality") { VisualProperties = { StartIndexZero = true } });
[10269]121      swQualitiesTable.Rows["Current Quality"].Values.Add(currentQuality);
122
123      // compute after quality
[10347]124      if (fitnessCalculationPartition.End < problemData.TrainingPartition.End) {
[10269]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"))
[10347]129          swQualitiesTable.Rows.Add(new DataRow("After Quality") { VisualProperties = { StartIndexZero = true } });
[10269]130        swQualitiesTable.Rows["After Quality"].Values.Add(afterQuality);
131      }
132      // compute test quality
133      if (!swQualitiesTable.Rows.ContainsKey("Test Quality"))
[10347]134        swQualitiesTable.Rows.Add(new DataRow("Test Quality") { VisualProperties = { StartIndexZero = true } });
135      var regressionSolution = (IRegressionSolution)bestSolution;
[10269]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.