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.DataAnalysis;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.GP.StructureIdentification.ConditionalEvaluation {
|
---|
31 | public abstract class ConditionalEvaluatorBase : GPEvaluatorBase {
|
---|
32 | public virtual string OutputVariableName { get { return "Quality"; } }
|
---|
33 |
|
---|
34 | public ConditionalEvaluatorBase()
|
---|
35 | : base() {
|
---|
36 | AddVariableInfo(new VariableInfo("MaxTimeOffset", "Maximal time offset for all feature", typeof(IntData), VariableKind.In));
|
---|
37 | AddVariableInfo(new VariableInfo("MinTimeOffset", "Minimal time offset for all feature", typeof(IntData), VariableKind.In));
|
---|
38 | AddVariableInfo(new VariableInfo("ConditionVariable", "Variable index which indicates if the row should be evaluated (0 means do not evaluate, != 0 evaluate)", typeof(IntData), VariableKind.In));
|
---|
39 | AddVariableInfo(new VariableInfo(OutputVariableName, OutputVariableName, typeof(DoubleData), VariableKind.New | VariableKind.Out));
|
---|
40 | }
|
---|
41 |
|
---|
42 | public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
|
---|
43 | int maxTimeOffset = GetVariableValue<IntData>("MaxTimeOffset", scope, true).Data;
|
---|
44 | int minTimeOffset = GetVariableValue<IntData>("MinTimeOffset", scope, true).Data;
|
---|
45 | int conditionVariable = GetVariableValue<IntData>("ConditionVariable", scope, true).Data;
|
---|
46 |
|
---|
47 | int skippedSampels = 0;
|
---|
48 | // store original and estimated values in a double array
|
---|
49 | double[,] values = new double[end - start, 2];
|
---|
50 | for (int sample = start; sample < end; sample++) {
|
---|
51 | // check if condition variable is true between sample - minTimeOffset and sample - maxTimeOffset
|
---|
52 | bool skip = false;
|
---|
53 | for (int checkIndex = sample + minTimeOffset; checkIndex <= sample + maxTimeOffset && !skip; checkIndex++) {
|
---|
54 | if (dataset.GetValue(checkIndex, conditionVariable) == 0) {
|
---|
55 | skip = true;
|
---|
56 | skippedSampels++;
|
---|
57 | }
|
---|
58 | }
|
---|
59 | if (!skip) {
|
---|
60 | double original = dataset.GetValue(sample, targetVariable);
|
---|
61 | double estimated = evaluator.Evaluate(sample);
|
---|
62 | if (updateTargetValues) {
|
---|
63 | dataset.SetValue(sample, targetVariable, estimated);
|
---|
64 | }
|
---|
65 | values[sample - start - skippedSampels, 0] = estimated;
|
---|
66 | values[sample - start - skippedSampels, 1] = original;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | //needed because otherwise the array is too larged dimension and therefore the sample count is false during calculation
|
---|
70 | ResizeArray(ref values, 2, end - start - skippedSampels);
|
---|
71 |
|
---|
72 |
|
---|
73 | // calculate quality value
|
---|
74 | double quality = Evaluate(values);
|
---|
75 |
|
---|
76 | DoubleData qualityData = GetVariableValue<DoubleData>(OutputVariableName, scope, false, false);
|
---|
77 | if (qualityData == null) {
|
---|
78 | qualityData = new DoubleData();
|
---|
79 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(OutputVariableName), qualityData));
|
---|
80 | }
|
---|
81 | qualityData.Data = quality;
|
---|
82 | scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data -= skippedSampels;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | private void ResizeArray(ref double[,] original, int cols, int rows) {
|
---|
87 | double[,] newArray = new double[rows, cols];
|
---|
88 | Array.Copy(original, newArray, cols * rows);
|
---|
89 | original = newArray;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public abstract double Evaluate(double[,] values);
|
---|
93 | }
|
---|
94 | }
|
---|