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.DataAnalysis;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
32 | public abstract class GPEvaluatorBase : OperatorBase {
|
---|
33 | public GPEvaluatorBase()
|
---|
34 | : base() {
|
---|
35 | AddVariableInfo(new VariableInfo("TreeEvaluator", "The evaluator that should be used to evaluate the expression tree", typeof(ITreeEvaluator), VariableKind.In));
|
---|
36 | AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IFunctionTree), VariableKind.In));
|
---|
37 | AddVariableInfo(new VariableInfo("TreeSize", "Size (number of nodes) of the tree to evaluate", typeof(IntData), VariableKind.In));
|
---|
38 | AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
|
---|
39 | AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In));
|
---|
40 | AddVariableInfo(new VariableInfo("PunishmentFactor", "Punishment factor for invalid estimations", typeof(DoubleData), VariableKind.In));
|
---|
41 | AddVariableInfo(new VariableInfo("TotalEvaluatedNodes", "Number of evaluated nodes", typeof(DoubleData), VariableKind.In | VariableKind.Out));
|
---|
42 | AddVariableInfo(new VariableInfo("TrainingSamplesStart", "Start index of training samples in dataset", typeof(IntData), VariableKind.In));
|
---|
43 | AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "End index of training samples in dataset", typeof(IntData), VariableKind.In));
|
---|
44 | AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
|
---|
45 | AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
|
---|
46 | 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));
|
---|
47 | }
|
---|
48 |
|
---|
49 | public override IOperation Apply(IScope scope) {
|
---|
50 | // get all variable values
|
---|
51 | int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
|
---|
52 | Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
|
---|
53 | IFunctionTree functionTree = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
|
---|
54 | double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
|
---|
55 | int treeSize = scope.GetVariableValue<IntData>("TreeSize", true).Data;
|
---|
56 | double totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data;
|
---|
57 | int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
|
---|
58 | int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
|
---|
59 | int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
|
---|
60 | int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
|
---|
61 | bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, true).Data;
|
---|
62 | ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true);
|
---|
63 | evaluator.PrepareForEvaluation(dataset, targetVariable, trainingStart, trainingEnd, punishmentFactor, functionTree);
|
---|
64 |
|
---|
65 | double[] backupValues = null;
|
---|
66 | // prepare for autoregressive modelling by saving the original values of the target-variable to a backup array
|
---|
67 | if (useEstimatedValues &&
|
---|
68 | (backupValues == null || backupValues.Length != end - start)) {
|
---|
69 | backupValues = new double[end - start];
|
---|
70 | for (int i = start; i < end; i++) {
|
---|
71 | backupValues[i - start] = dataset.GetValue(i, targetVariable);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | dataset.FireChangeEvents = false;
|
---|
75 |
|
---|
76 | Evaluate(scope, evaluator, dataset, targetVariable, start, end, useEstimatedValues);
|
---|
77 |
|
---|
78 | // restore the values of the target variable from the backup array if necessary
|
---|
79 | if (useEstimatedValues) {
|
---|
80 | for (int i = start; i < end; i++) {
|
---|
81 | dataset.SetValue(i, targetVariable, backupValues[i - start]);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | dataset.FireChangeEvents = true;
|
---|
85 | dataset.FireChanged();
|
---|
86 |
|
---|
87 | // update the value of total evaluated nodes
|
---|
88 | scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (end - start);
|
---|
89 | return null;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public abstract void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues);
|
---|
93 | }
|
---|
94 | }
|
---|