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