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.Operators;
|
---|
29 | using HeuristicLab.Functions;
|
---|
30 | using HeuristicLab.DataAnalysis;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.StructureIdentification {
|
---|
33 | public class AvergePercentageChangeEvaluator : GPEvaluatorBase {
|
---|
34 | protected DoubleData apc;
|
---|
35 | private bool differential;
|
---|
36 | public override string Description {
|
---|
37 | get {
|
---|
38 | return @"TASK";
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public AvergePercentageChangeEvaluator()
|
---|
43 | : base() {
|
---|
44 | AddVariableInfo(new VariableInfo("Differential", "Wether to transform the target variable to percentage change first or not.", typeof(BoolData), VariableKind.In));
|
---|
45 | AddVariableInfo(new VariableInfo("APC", "The average percentage change of the model", typeof(DoubleData), VariableKind.New));
|
---|
46 | }
|
---|
47 |
|
---|
48 | public override IOperation Apply(IScope scope) {
|
---|
49 | differential = GetVariableValue<BoolData>("Differential", scope, true).Data;
|
---|
50 | apc = GetVariableValue<DoubleData>("APC", scope, false, false);
|
---|
51 | if(apc == null) {
|
---|
52 | apc = new DoubleData();
|
---|
53 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("APC"), apc));
|
---|
54 | }
|
---|
55 |
|
---|
56 | return base.Apply(scope);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override void Evaluate(int start, int end) {
|
---|
60 | double percentageSum = 0;
|
---|
61 | for(int sample = start; sample < end; sample++) {
|
---|
62 | double prevOriginal;
|
---|
63 | double originalPercentageChange;
|
---|
64 | double estimatedPercentageChange;
|
---|
65 | if(differential) {
|
---|
66 | prevOriginal = GetOriginalValue(sample - 1);
|
---|
67 | originalPercentageChange = (GetOriginalValue(sample) - prevOriginal) / prevOriginal;
|
---|
68 | estimatedPercentageChange = (GetEstimatedValue(sample) - prevOriginal) / prevOriginal;
|
---|
69 | SetOriginalValue(sample, estimatedPercentageChange*prevOriginal + prevOriginal);
|
---|
70 | } else {
|
---|
71 | originalPercentageChange = GetOriginalValue(sample);
|
---|
72 | estimatedPercentageChange = GetEstimatedValue(sample);
|
---|
73 | SetOriginalValue(sample, estimatedPercentageChange);
|
---|
74 | }
|
---|
75 | if(!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) {
|
---|
76 | if((estimatedPercentageChange > 0 && originalPercentageChange > 0) ||
|
---|
77 | (estimatedPercentageChange < 0 && originalPercentageChange < 0)) {
|
---|
78 | percentageSum += Math.Abs(originalPercentageChange);
|
---|
79 | } else if((estimatedPercentageChange > 0 && originalPercentageChange < 0) ||
|
---|
80 | (estimatedPercentageChange < 0 && originalPercentageChange > 0)) {
|
---|
81 | percentageSum -= Math.Abs(originalPercentageChange);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | percentageSum /= (end - start);
|
---|
87 | if(double.IsNaN(percentageSum) || double.IsInfinity(percentageSum)) {
|
---|
88 | percentageSum = double.MinValue;
|
---|
89 | }
|
---|
90 | apc.Data = percentageSum;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|