1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis.SupportVectorMachine;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Operators;
|
---|
34 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
|
---|
35 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
36 | using System.Collections.Generic;
|
---|
37 | using HeuristicLab.Problems.DataAnalysis.Regression;
|
---|
38 | using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Interfaces;
|
---|
39 |
|
---|
40 | namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Evaluators {
|
---|
41 | [Item("SymbolicTimeSeriesPrognosisEvaluator", "")]
|
---|
42 | [StorableClass]
|
---|
43 | public abstract class SymbolicTimeSeriesPrognosisEvaluator : SingleSuccessorOperator, ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator {
|
---|
44 | private const string RandomParameterName = "Random";
|
---|
45 | private const string DataAnalysisProblemDataParameterName = "MultiVariateDataAnalysisProblemData";
|
---|
46 | private const string TimeSeriesExpressionInterpreterParameterName = "TimeSeriesExpressionInterpreter";
|
---|
47 | private const string TimeSeriesPrognosisModelParameterName = "TimeSeriesPrognosisModel";
|
---|
48 | private const string PredictionHorizontParameterName = "PredictionHorizon";
|
---|
49 | private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
|
---|
50 | private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
|
---|
51 | private const string ConditionVariableParameterName = "ConditionVariableName";
|
---|
52 | private const string SamplesStartParameterName = "SamplesStart";
|
---|
53 | private const string SamplesEndParameterName = "SamplesEnd";
|
---|
54 | private const string QualityParameterName = "Quality";
|
---|
55 | private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
|
---|
56 |
|
---|
57 | #region parameter properties
|
---|
58 | public ILookupParameter<IRandom> RandomParameter {
|
---|
59 | get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
|
---|
60 | }
|
---|
61 | public ILookupParameter<MultiVariateDataAnalysisProblemData> ProblemDataParameter {
|
---|
62 | get { return (ILookupParameter<MultiVariateDataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; }
|
---|
63 | }
|
---|
64 | public ILookupParameter<ISymbolicTimeSeriesExpressionInterpreter> TimeSeriesExpressionInterpreterParameter {
|
---|
65 | get { return (ILookupParameter<ISymbolicTimeSeriesExpressionInterpreter>)Parameters[TimeSeriesExpressionInterpreterParameterName]; }
|
---|
66 | }
|
---|
67 | public IValueLookupParameter<IntValue> PredictionHorizonParameter {
|
---|
68 | get { return (IValueLookupParameter<IntValue>)Parameters[PredictionHorizontParameterName]; }
|
---|
69 | }
|
---|
70 | public OptionalValueParameter<StringValue> ConditionVariableNameParameter {
|
---|
71 | get { return (OptionalValueParameter<StringValue>)Parameters[ConditionVariableParameterName]; }
|
---|
72 | }
|
---|
73 | public IValueLookupParameter<IntValue> SamplesStartParameter {
|
---|
74 | get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; }
|
---|
75 | }
|
---|
76 | public IValueLookupParameter<IntValue> SamplesEndParameter {
|
---|
77 | get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; }
|
---|
78 | }
|
---|
79 | public IValueLookupParameter<DoubleArray> LowerEstimationLimitParameter {
|
---|
80 | get { return (IValueLookupParameter<DoubleArray>)Parameters[LowerEstimationLimitParameterName]; }
|
---|
81 | }
|
---|
82 | public IValueLookupParameter<DoubleArray> UpperEstimationLimitParameter {
|
---|
83 | get { return (IValueLookupParameter<DoubleArray>)Parameters[UpperEstimationLimitParameterName]; }
|
---|
84 | }
|
---|
85 | public ILookupParameter<SymbolicExpressionTree> TimeSeriesPrognosisModelParameter {
|
---|
86 | get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[TimeSeriesPrognosisModelParameterName]; }
|
---|
87 | }
|
---|
88 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
89 | get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
90 | }
|
---|
91 | public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
|
---|
92 | get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
|
---|
93 | }
|
---|
94 | #endregion
|
---|
95 | #region properties
|
---|
96 | public IRandom Random {
|
---|
97 | get { return RandomParameter.ActualValue; }
|
---|
98 | }
|
---|
99 | public MultiVariateDataAnalysisProblemData ProblemData {
|
---|
100 | get { return ProblemDataParameter.ActualValue; }
|
---|
101 | }
|
---|
102 | public ISymbolicTimeSeriesExpressionInterpreter TimeSeriesExpressionInterpreter {
|
---|
103 | get { return TimeSeriesExpressionInterpreterParameter.ActualValue; }
|
---|
104 | }
|
---|
105 | public IntValue PredictionHorizon {
|
---|
106 | get { return PredictionHorizonParameter.ActualValue; }
|
---|
107 | }
|
---|
108 | public StringValue ConditionVariableName {
|
---|
109 | get { return ConditionVariableNameParameter.Value; }
|
---|
110 | }
|
---|
111 | public IntValue SamplesStart {
|
---|
112 | get { return SamplesStartParameter.ActualValue; }
|
---|
113 | }
|
---|
114 | public IntValue SamplesEnd {
|
---|
115 | get { return SamplesEndParameter.ActualValue; }
|
---|
116 | }
|
---|
117 | public DoubleArray LowerEstimationLimit {
|
---|
118 | get { return LowerEstimationLimitParameter.ActualValue; }
|
---|
119 | }
|
---|
120 | public DoubleArray UpperEstimationLimit {
|
---|
121 | get { return UpperEstimationLimitParameter.ActualValue; }
|
---|
122 | }
|
---|
123 | public SymbolicExpressionTree TimeSeriesPrognosisModel {
|
---|
124 | get { return TimeSeriesPrognosisModelParameter.ActualValue; }
|
---|
125 | }
|
---|
126 | public PercentValue RelativeNumberOfEvaluatedSamples {
|
---|
127 | get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
|
---|
128 | }
|
---|
129 | #endregion
|
---|
130 | [StorableConstructor]
|
---|
131 | protected SymbolicTimeSeriesPrognosisEvaluator(bool deserializing) : base(deserializing) { }
|
---|
132 | protected SymbolicTimeSeriesPrognosisEvaluator(SymbolicTimeSeriesPrognosisEvaluator original, Cloner cloner)
|
---|
133 | : base(original, cloner) {
|
---|
134 | }
|
---|
135 | public SymbolicTimeSeriesPrognosisEvaluator()
|
---|
136 | : base() {
|
---|
137 | Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "A random number generator."));
|
---|
138 | Parameters.Add(new LookupParameter<MultiVariateDataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The data analysis problem data to use for training."));
|
---|
139 | Parameters.Add(new LookupParameter<ISymbolicTimeSeriesExpressionInterpreter>(TimeSeriesExpressionInterpreterParameterName, "The interpreter that should be used to evaluate the time series model represented as a symbolic expression tree."));
|
---|
140 | Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The first index of the data set partition to use for training."));
|
---|
141 | Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The last index of the data set partition to use for training."));
|
---|
142 | Parameters.Add(new ValueLookupParameter<IntValue>(PredictionHorizontParameterName, "The number of time steps for which to create a forecast."));
|
---|
143 | Parameters.Add(new ValueLookupParameter<DoubleArray>(LowerEstimationLimitParameterName, "The lower limit for estimated values."));
|
---|
144 | Parameters.Add(new ValueLookupParameter<DoubleArray>(UpperEstimationLimitParameterName, "The upper limit for estimated values."));
|
---|
145 | Parameters.Add(new OptionalValueParameter<StringValue>(ConditionVariableParameterName, "The name of the condition variable indicating if a row should be considered for evaluation or not."));
|
---|
146 | Parameters.Add(new LookupParameter<SymbolicExpressionTree>(TimeSeriesPrognosisModelParameterName, "The time series prognosis model encoded as a symbolic expression tree."));
|
---|
147 | Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality of the time series prognosis model encoded as a symbolic expression tree."));
|
---|
148 | Parameters.Add(new ValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index.", new PercentValue(1)));
|
---|
149 | }
|
---|
150 |
|
---|
151 | public override IOperation Apply() {
|
---|
152 | double quality;
|
---|
153 | string conditionVariableName = ConditionVariableName == null ? null : ConditionVariableName.Value;
|
---|
154 | int nRows = (int)Math.Ceiling((SamplesEnd.Value - SamplesStart.Value) * RelativeNumberOfEvaluatedSamples.Value);
|
---|
155 | IEnumerable<int> rows = RandomEnumerable.SampleRandomNumbers(Random.Next(), SamplesStart.Value, SamplesEnd.Value, nRows);
|
---|
156 | if (conditionVariableName != null) {
|
---|
157 | rows = from row in rows
|
---|
158 | where !ProblemData.Dataset[conditionVariableName, row].IsAlmost(0.0)
|
---|
159 | select row;
|
---|
160 | }
|
---|
161 |
|
---|
162 | quality = Evaluate(TimeSeriesPrognosisModel, ProblemData, TimeSeriesExpressionInterpreter,
|
---|
163 | rows, PredictionHorizon.Value, LowerEstimationLimit, UpperEstimationLimit);
|
---|
164 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
165 | return base.Apply();
|
---|
166 | }
|
---|
167 |
|
---|
168 | public abstract double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData,
|
---|
169 | ISymbolicTimeSeriesExpressionInterpreter interpreter,
|
---|
170 | IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit);
|
---|
171 | }
|
---|
172 | }
|
---|