Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalEvaluatorBase.cs @ 4483

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

Implemented #824 (Refactor: ITreeEvaluator interface to provide a method that evaluates a tree on a range of samples.)

File size: 3.8 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;
29using HeuristicLab.GP.Interfaces;
30using HeuristicLab.Modeling;
31
32namespace HeuristicLab.GP.StructureIdentification.ConditionalEvaluation {
33  public abstract class ConditionalEvaluatorBase : GPEvaluatorBase {
34    public virtual string OutputVariableName { get { return "Quality"; } }
35
36    public ConditionalEvaluatorBase()
37      : base() {
38      AddVariableInfo(new VariableInfo("MaxTimeOffset", "Maximal time offset for all feature", typeof(IntData), VariableKind.In));
39      AddVariableInfo(new VariableInfo("MinTimeOffset", "Minimal time offset for all feature", typeof(IntData), VariableKind.In));
40      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));
41      AddVariableInfo(new VariableInfo(OutputVariableName, OutputVariableName, typeof(DoubleData), VariableKind.New | VariableKind.Out));
42    }
43
44    public override void Evaluate(IScope scope, IFunctionTree tree, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
45      int maxTimeOffset = GetVariableValue<IntData>("MaxTimeOffset", scope, true).Data;
46      int minTimeOffset = GetVariableValue<IntData>("MinTimeOffset", scope, true).Data;
47      int conditionVariable = GetVariableValue<IntData>("ConditionVariable", scope, true).Data;
48
49      var rows = from row in Enumerable.Range(start, end - start)
50                 // check if condition variable is true between sample - minTimeOffset and sample - maxTimeOffset
51                 // => select rows where the value of the condition variable is different from zero in the whole range
52                 where (from neighbour in Enumerable.Range(row + minTimeOffset, maxTimeOffset - minTimeOffset)
53                        let value = dataset.GetValue(neighbour, conditionVariable)
54                        where value == 0
55                        select neighbour).Any() == false
56                 select row;
57
58      // store original and estimated values in a double array
59      double[,] values = Matrix<double>.Create(
60        evaluator.Evaluate(dataset, tree, rows).ToArray(),
61        (from row in rows select dataset.GetValue(row, targetVariable)).ToArray());
62
63      // calculate quality value
64      double quality = Evaluate(values);
65
66      DoubleData qualityData = GetVariableValue<DoubleData>(OutputVariableName, scope, false, false);
67      if (qualityData == null) {
68        qualityData = new DoubleData();
69        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(OutputVariableName), qualityData));
70      }
71      qualityData.Data = quality;
72      scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data -= (end - start) - rows.Count();
73    }
74
75    public abstract double Evaluate(double[,] values);
76  }
77}
Note: See TracBrowser for help on using the repository browser.