Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalEvaluatorBase.cs @ 1902

Last change on this file since 1902 was 1902, checked in by mkommend, 15 years ago

added initial version of conditional evaluators (ticket #515)

File size: 3.7 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.DataAnalysis;
29
30namespace 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      // store original and estimated values in a double array
48      double[,] values = new double[end - start, 2];
49      for (int sample = start; sample < end; sample++) {
50        // check if condition variable is true between sample - minTimeOffset and sample - maxTimeOffset
51        bool skip = false;
52        for (int checkIndex = sample - minTimeOffset; checkIndex <= sample - maxTimeOffset && !skip ; checkIndex++) {
53          if (dataset.GetValue(checkIndex, conditionVariable) == 0)
54            skip = true;
55        }
56        if (!skip) {
57          double original = dataset.GetValue(sample, targetVariable);
58          double estimated = evaluator.Evaluate(sample);
59          if (updateTargetValues) {
60            dataset.SetValue(sample, targetVariable, estimated);
61          }
62          values[sample - start, 0] = estimated;
63          values[sample - start, 1] = original;
64        }
65      }
66
67      // calculate quality value
68      double quality = Evaluate(values);
69
70      DoubleData qualityData = GetVariableValue<DoubleData>(OutputVariableName, scope, false, false);
71      if (qualityData == null) {
72        qualityData = new DoubleData();
73        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(OutputVariableName), qualityData));
74      }
75      qualityData.Data = quality;
76    }
77
78    public abstract double Evaluate(double[,] values);
79  }
80}
Note: See TracBrowser for help on using the repository browser.