1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.GP.StructureIdentification;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
|
---|
31 | public class AvergePercentageChangeEvaluator : GPEvaluatorBase {
|
---|
32 | public override string Description {
|
---|
33 | get {
|
---|
34 | return @"TASK";
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public AvergePercentageChangeEvaluator()
|
---|
39 | : base() {
|
---|
40 | AddVariableInfo(new VariableInfo("Differential", "Wether to transform the target variable to percentage change first or not.", typeof(BoolData), VariableKind.In));
|
---|
41 | AddVariableInfo(new VariableInfo("APC", "The average percentage change of the model", typeof(DoubleData), VariableKind.New));
|
---|
42 | }
|
---|
43 |
|
---|
44 | public override void Evaluate(IScope scope, ITreeEvaluator evaluator, IFunctionTree tree, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
|
---|
45 | bool differential = GetVariableValue<BoolData>("Differential", scope, true).Data;
|
---|
46 | DoubleData apc = GetVariableValue<DoubleData>("APC", scope, false, false);
|
---|
47 | if (apc == null) {
|
---|
48 | apc = new DoubleData();
|
---|
49 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("APC"), apc));
|
---|
50 | }
|
---|
51 |
|
---|
52 | double percentageSum = 0;
|
---|
53 | for (int sample = start; sample < end; sample++) {
|
---|
54 | double prevOriginal;
|
---|
55 | double originalPercentageChange;
|
---|
56 | double estimatedPercentageChange;
|
---|
57 | if (differential) {
|
---|
58 | prevOriginal = dataset.GetValue(sample - 1, targetVariable);
|
---|
59 | originalPercentageChange = (dataset.GetValue(sample, targetVariable) - prevOriginal) / prevOriginal;
|
---|
60 | estimatedPercentageChange = (evaluator.Evaluate(tree, sample) - prevOriginal) / prevOriginal;
|
---|
61 | if (updateTargetValues) {
|
---|
62 | dataset.SetValue(sample, targetVariable, estimatedPercentageChange * prevOriginal + prevOriginal);
|
---|
63 | }
|
---|
64 | } else {
|
---|
65 | originalPercentageChange = dataset.GetValue(sample, targetVariable);
|
---|
66 | estimatedPercentageChange = evaluator.Evaluate(tree, sample);
|
---|
67 | if (updateTargetValues) {
|
---|
68 | dataset.SetValue(sample, targetVariable, estimatedPercentageChange);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | if (!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) {
|
---|
72 | if ((estimatedPercentageChange > 0 && originalPercentageChange > 0) ||
|
---|
73 | (estimatedPercentageChange < 0 && originalPercentageChange < 0)) {
|
---|
74 | percentageSum += Math.Abs(originalPercentageChange);
|
---|
75 | } else if ((estimatedPercentageChange > 0 && originalPercentageChange < 0) ||
|
---|
76 | (estimatedPercentageChange < 0 && originalPercentageChange > 0)) {
|
---|
77 | percentageSum -= Math.Abs(originalPercentageChange);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | percentageSum /= (end - start);
|
---|
83 | if (double.IsNaN(percentageSum) || double.IsInfinity(percentageSum)) {
|
---|
84 | percentageSum = double.MinValue;
|
---|
85 | }
|
---|
86 | apc.Data = percentageSum;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|