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 HeuristicLab.Core;
|
---|
23 | using HeuristicLab.Data;
|
---|
24 | using HeuristicLab.DataAnalysis;
|
---|
25 | using HeuristicLab.GP.Interfaces;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
28 | public abstract class GPEvaluatorBase : OperatorBase {
|
---|
29 | public GPEvaluatorBase()
|
---|
30 | : base() {
|
---|
31 | AddVariableInfo(new VariableInfo("TreeEvaluator", "The evaluator that should be used to evaluate the expression tree", typeof(ITreeEvaluator), VariableKind.In));
|
---|
32 | AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IGeneticProgrammingModel), VariableKind.In));
|
---|
33 | AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
|
---|
34 | AddVariableInfo(new VariableInfo("TargetVariable", "Name of the target variable", typeof(StringData), VariableKind.In));
|
---|
35 | AddVariableInfo(new VariableInfo("TotalEvaluatedNodes", "Number of evaluated nodes", typeof(DoubleData), VariableKind.In | VariableKind.Out));
|
---|
36 | AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
|
---|
37 | AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
|
---|
38 | AddVariableInfo(new VariableInfo("UseEstimatedTargetValue", "(optional) Wether to use the original (measured) or the estimated (calculated) value for the target variable for autoregressive modelling", typeof(BoolData), VariableKind.In));
|
---|
39 | }
|
---|
40 |
|
---|
41 | public override IOperation Apply(IScope scope) {
|
---|
42 | // get all variable values
|
---|
43 | Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
|
---|
44 | int targetVariable = dataset.GetVariableIndex(GetVariableValue<StringData>("TargetVariable", scope, true).Data);
|
---|
45 | IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
|
---|
46 | double totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data;
|
---|
47 | int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
|
---|
48 | int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
|
---|
49 | BoolData useEstimatedValuesData = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, true, false);
|
---|
50 | bool useEstimatedValues = useEstimatedValuesData == null ? false : useEstimatedValuesData.Data;
|
---|
51 | ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true);
|
---|
52 | evaluator.PrepareForEvaluation(dataset, gpModel.FunctionTree);
|
---|
53 |
|
---|
54 | double[] backupValues = null;
|
---|
55 | // prepare for autoregressive modelling by saving the original values of the target-variable to a backup array
|
---|
56 | if (useEstimatedValues &&
|
---|
57 | (backupValues == null || backupValues.Length != end - start)) {
|
---|
58 | backupValues = new double[end - start];
|
---|
59 | for (int i = start; i < end; i++) {
|
---|
60 | backupValues[i - start] = dataset.GetValue(i, targetVariable);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | dataset.FireChangeEvents = false;
|
---|
64 |
|
---|
65 | Evaluate(scope, evaluator, dataset, targetVariable, start, end, useEstimatedValues);
|
---|
66 |
|
---|
67 | // restore the values of the target variable from the backup array if necessary
|
---|
68 | if (useEstimatedValues) {
|
---|
69 | for (int i = start; i < end; i++) {
|
---|
70 | dataset.SetValue(i, targetVariable, backupValues[i - start]);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | dataset.FireChangeEvents = true;
|
---|
74 | dataset.FireChanged();
|
---|
75 |
|
---|
76 | // update the value of total evaluated nodes
|
---|
77 | scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + gpModel.Size * (end - start);
|
---|
78 | return null;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public abstract void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues);
|
---|
82 | }
|
---|
83 | }
|
---|