1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing.Printing;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis {
|
---|
33 | [Item("Mean squared error Evaluator", "Calculates the mean squared error of a symbolic time-series prognosis solution.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator : SymbolicTimeSeriesPrognosisSingleObjectiveEvaluator {
|
---|
36 | [StorableConstructor]
|
---|
37 | protected SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator(bool deserializing) : base(deserializing) { }
|
---|
38 | protected SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator(SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator original, Cloner cloner)
|
---|
39 | : base(original, cloner) {
|
---|
40 | }
|
---|
41 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
42 | return new SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator(this, cloner);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator() : base() { }
|
---|
46 |
|
---|
47 | public override bool Maximization { get { return false; } }
|
---|
48 |
|
---|
49 | public override IOperation Apply() {
|
---|
50 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
51 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
52 |
|
---|
53 | double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
|
---|
54 | solution,
|
---|
55 | EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
|
---|
56 | ProblemDataParameter.ActualValue,
|
---|
57 | rows, HorizonParameter.ActualValue.Value);
|
---|
58 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
59 |
|
---|
60 | return base.Apply();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public static double Calculate(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows, int horizon) {
|
---|
64 | double[] alpha;
|
---|
65 | double[] beta;
|
---|
66 | DetermineScalingFactors(solution, problemData, interpreter, rows, out alpha, out beta);
|
---|
67 | var scaledSolution = Scale(solution, alpha, beta);
|
---|
68 | string[] targetVariables = problemData.TargetVariables.ToArray();
|
---|
69 | var meanSquaredErrorCalculators = Enumerable.Range(0, problemData.TargetVariables.Count())
|
---|
70 | .Select(i => new OnlineMeanSquaredErrorCalculator()).ToArray();
|
---|
71 |
|
---|
72 | var allContinuationsEnumerator = interpreter.GetSymbolicExpressionTreeValues(scaledSolution, problemData.Dataset,
|
---|
73 | targetVariables,
|
---|
74 | rows, horizon).GetEnumerator();
|
---|
75 | allContinuationsEnumerator.MoveNext();
|
---|
76 | // foreach row
|
---|
77 | foreach (var row in rows) {
|
---|
78 | // foreach horizon
|
---|
79 | for (int h = 0; h < horizon; h++) {
|
---|
80 | // foreach component
|
---|
81 | for (int i = 0; i < meanSquaredErrorCalculators.Length; i++) {
|
---|
82 | double e = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, allContinuationsEnumerator.Current));
|
---|
83 | meanSquaredErrorCalculators[i].Add(problemData.Dataset.GetDoubleValue(targetVariables[i], row + h), e);
|
---|
84 | if (meanSquaredErrorCalculators[i].ErrorState == OnlineCalculatorError.InvalidValueAdded)
|
---|
85 | return double.MaxValue;
|
---|
86 | allContinuationsEnumerator.MoveNext();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | var meanCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
91 | foreach (var calc in meanSquaredErrorCalculators) {
|
---|
92 | if (calc.ErrorState != OnlineCalculatorError.None) return double.MaxValue;
|
---|
93 | meanCalculator.Add(calc.MeanSquaredError);
|
---|
94 | }
|
---|
95 |
|
---|
96 | return meanCalculator.MeanErrorState == OnlineCalculatorError.None ? meanCalculator.Mean : double.MaxValue;
|
---|
97 | }
|
---|
98 |
|
---|
99 | private static ISymbolicExpressionTree Scale(ISymbolicExpressionTree solution, double[] alpha, double[] beta) {
|
---|
100 | var clone = (ISymbolicExpressionTree)solution.Clone();
|
---|
101 | int n = alpha.Length;
|
---|
102 | for (int i = 0; i < n; i++) {
|
---|
103 | var parent = clone.Root.GetSubtree(0);
|
---|
104 | var rpb = clone.Root.GetSubtree(0).GetSubtree(i);
|
---|
105 | var scaledRpb = MakeSum(
|
---|
106 | MakeProduct(rpb,
|
---|
107 | MakeConstant(beta[i], clone.Root.Grammar), clone.Root.Grammar),
|
---|
108 | MakeConstant(alpha[i], clone.Root.Grammar), clone.Root.Grammar);
|
---|
109 | parent.RemoveSubtree(i);
|
---|
110 | parent.InsertSubtree(i, scaledRpb);
|
---|
111 | }
|
---|
112 | return clone;
|
---|
113 | }
|
---|
114 |
|
---|
115 | private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, ISymbolicExpressionTreeGrammar grammar) {
|
---|
116 | var sum = grammar.Symbols.Where(s => s is Addition).First().CreateTreeNode();
|
---|
117 | sum.AddSubtree(a);
|
---|
118 | sum.AddSubtree(b);
|
---|
119 | return sum;
|
---|
120 | }
|
---|
121 |
|
---|
122 | private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, ISymbolicExpressionTreeGrammar grammar) {
|
---|
123 | var prod = grammar.Symbols.Where(s => s is Multiplication).First().CreateTreeNode();
|
---|
124 | prod.AddSubtree(a);
|
---|
125 | prod.AddSubtree(b);
|
---|
126 | return prod;
|
---|
127 | }
|
---|
128 |
|
---|
129 | private static ISymbolicExpressionTreeNode MakeConstant(double c, ISymbolicExpressionTreeGrammar grammar) {
|
---|
130 | var node = (ConstantTreeNode)grammar.Symbols.Where(s => s is Constant).First().CreateTreeNode();
|
---|
131 | node.Value = c;
|
---|
132 | return node;
|
---|
133 | }
|
---|
134 |
|
---|
135 | private static void DetermineScalingFactors(ISymbolicExpressionTree solution, ITimeSeriesPrognosisProblemData problemData, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, IEnumerable<int> rows, out double[] alpha, out double[] beta) {
|
---|
136 | string[] targetVariables = problemData.TargetVariables.ToArray();
|
---|
137 | int nComponents = targetVariables.Length;
|
---|
138 | alpha = new double[nComponents];
|
---|
139 | beta = new double[nComponents];
|
---|
140 | var oneStepPredictionsEnumerator = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, targetVariables, rows).GetEnumerator();
|
---|
141 | var scalingParameterCalculators =
|
---|
142 | Enumerable.Repeat(0, nComponents).Select(x => new OnlineLinearScalingParameterCalculator()).ToArray();
|
---|
143 | var targetValues = problemData.Dataset.GetVectorEnumerable(targetVariables, rows);
|
---|
144 | var targetValueEnumerator = targetValues.GetEnumerator();
|
---|
145 |
|
---|
146 | var more = oneStepPredictionsEnumerator.MoveNext() & targetValueEnumerator.MoveNext();
|
---|
147 | while (more) {
|
---|
148 | for (int i = 0; i < nComponents; i++) {
|
---|
149 | scalingParameterCalculators[i].Add(oneStepPredictionsEnumerator.Current, targetValueEnumerator.Current);
|
---|
150 | more = oneStepPredictionsEnumerator.MoveNext() & targetValueEnumerator.MoveNext();
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | for (int i = 0; i < nComponents; i++) {
|
---|
155 | if (scalingParameterCalculators[i].ErrorState == OnlineCalculatorError.None) {
|
---|
156 | alpha[i] = scalingParameterCalculators[i].Alpha;
|
---|
157 | beta[i] = scalingParameterCalculators[i].Beta;
|
---|
158 | } else {
|
---|
159 | alpha[i] = 0.0;
|
---|
160 | beta[i] = 1.0;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows) {
|
---|
166 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
167 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
168 | HorizonParameter.ExecutionContext = context;
|
---|
169 |
|
---|
170 | double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, HorizonParameter.ActualValue.Value);
|
---|
171 |
|
---|
172 | HorizonParameter.ExecutionContext = null;
|
---|
173 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
174 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
175 |
|
---|
176 | return mse;
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|