Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/GPEvaluatorBase.cs @ 656

Last change on this file since 656 was 656, checked in by gkronber, 15 years ago

merged changesets r644:647 and r651:655 from the GpPluginsRefactoringBranch back into the trunk (#177)

File size: 6.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.DataAnalysis;
30
31namespace HeuristicLab.GP.StructureIdentification {
32  public abstract class GPEvaluatorBase : OperatorBase {
33    private int targetVariable;
34    private int start;
35    private int end;
36    private bool useEstimatedValues;
37    private double[] backupValues;
38    private int evaluatedSamples;
39    private double estimatedValueMax;
40    private double estimatedValueMin;
41    private int treeSize;
42    private double totalEvaluatedNodes;
43    protected Dataset dataset;
44    private double targetMean;
45    private BakedTreeEvaluator evaluator;
46    protected double TargetMean { get { return targetMean; } }
47
48    public GPEvaluatorBase()
49      : base() {
50      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IFunctionTree), VariableKind.In));
51      AddVariableInfo(new VariableInfo("TreeSize", "Size (number of nodes) of the tree to evaluate", typeof(IntData), VariableKind.In));
52      AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
53      AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In));
54      AddVariableInfo(new VariableInfo("PunishmentFactor", "Punishment factor for invalid estimations", typeof(DoubleData), VariableKind.In));
55      AddVariableInfo(new VariableInfo("TotalEvaluatedNodes", "Number of evaluated nodes", typeof(DoubleData), VariableKind.In | VariableKind.Out));
56      AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
57      AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
58      AddVariableInfo(new VariableInfo("UseEstimatedTargetValue", "Wether to use the original (measured) or the estimated (calculated) value for the targat variable when doing autoregressive modelling", typeof(BoolData), VariableKind.In));
59    }
60
61    public override IOperation Apply(IScope scope) {
62      // get all variable values
63      targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
64      dataset = GetVariableValue<Dataset>("Dataset", scope, true);
65      BakedFunctionTree functionTree = GetVariableValue<BakedFunctionTree>("FunctionTree", scope, true);
66      double maximumPunishment = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data * dataset.GetRange(targetVariable);
67      treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data;
68      totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data;
69      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
70      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
71      useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, true).Data;
72      // prepare for autoregressive modelling by saving the original values of the target-variable to a backup array
73      if(useEstimatedValues &&
74        (backupValues == null || start != this.start || end != this.end)) {
75        this.start = start;
76        this.end = end;
77        backupValues = new double[end - start];
78        for(int i = start; i < end; i++) {
79          backupValues[i - start] = dataset.GetValue(i, targetVariable);
80        }
81      }
82      // get the mean of the values of the target variable to determin the max and min bounds of the estimated value
83      targetMean = dataset.GetMean(targetVariable, start, end - 1);
84      estimatedValueMin = targetMean - maximumPunishment;
85      estimatedValueMax = targetMean + maximumPunishment;
86
87      // initialize and reset the evaluator
88      if(evaluator == null) evaluator = new BakedTreeEvaluator();
89      evaluator.ResetEvaluator(functionTree, dataset);
90      evaluatedSamples = 0;
91
92      Evaluate(start, end);
93
94      // restore the values of the target variable from the backup array if necessary
95      if(useEstimatedValues) RestoreDataset(dataset, targetVariable, start, end);
96      // update the value of total evaluated nodes
97      scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * evaluatedSamples;
98      return null;
99    }
100
101    private void RestoreDataset(Dataset dataset, int targetVariable, int from, int to) {
102      for(int i = from; i < to; i++) {
103        dataset.SetValue(i, targetVariable, backupValues[i - from]);
104      }
105    }
106
107    public abstract void Evaluate(int start, int end);
108
109    public void SetOriginalValue(int sample, double value) {
110      if(useEstimatedValues) {
111        dataset.SetValue(sample, targetVariable, value);
112      }
113    }
114
115    public double GetOriginalValue(int sample) {
116      return dataset.GetValue(sample, targetVariable);
117    }
118
119    public double GetEstimatedValue(int sample) {
120      evaluatedSamples++;
121      double estimated = evaluator.Evaluate(sample);
122      if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
123        estimated = estimatedValueMax;
124      } else if(estimated > estimatedValueMax) {
125        estimated = estimatedValueMax;
126      } else if(estimated < estimatedValueMin) {
127        estimated = estimatedValueMin;
128      }
129      return estimated;
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.