Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/02/10 18:10:15 (15 years ago)
Author:
gkronber
Message:

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

Location:
trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalEvaluatorBase.cs

    r2577 r2578  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.DataAnalysis;
     29using HeuristicLab.GP.Interfaces;
     30using HeuristicLab.Modeling;
    2931
    3032namespace HeuristicLab.GP.StructureIdentification.ConditionalEvaluation {
     
    4042    }
    4143
    42     public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
     44    public override void Evaluate(IScope scope, IFunctionTree tree, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
    4345      int maxTimeOffset = GetVariableValue<IntData>("MaxTimeOffset", scope, true).Data;
    4446      int minTimeOffset = GetVariableValue<IntData>("MinTimeOffset", scope, true).Data;
    4547      int conditionVariable = GetVariableValue<IntData>("ConditionVariable", scope, true).Data;
    4648
    47       int skippedSampels = 0;
     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
    4858      // 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          
    63           values[sample - start - skippedSampels, 0] = estimated;
    64           values[sample - start - skippedSampels, 1] = original;
    65         }
    66       }
    67       //needed because otherwise the array is too large and therefore the sample count is incorrect during calculation
    68       ResizeArray(ref values, 2, end - start - skippedSampels);
    69 
     59      double[,] values = Matrix<double>.Create(
     60        evaluator.Evaluate(dataset, tree, rows).ToArray(),
     61        (from row in rows select dataset.GetValue(row, targetVariable)).ToArray());
    7062
    7163      // calculate quality value
     
    7870      }
    7971      qualityData.Data = quality;
    80       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data -= skippedSampels;
    81     }
    82 
    83 
    84     private void ResizeArray(ref double[,] original, int cols, int rows) {
    85       double[,] newArray = new double[rows, cols];
    86       Array.Copy(original, newArray, cols * rows);
    87       original = newArray;
     72      scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data -= (end - start) - rows.Count();
    8873    }
    8974
  • trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalSimpleEvaluator.cs

    r2577 r2578  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.DataAnalysis;
     29using HeuristicLab.GP.Interfaces;
    2930
    3031namespace HeuristicLab.GP.StructureIdentification.ConditionalEvaluation {
     
    3839    }
    3940
    40     public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
     41    public override void Evaluate(IScope scope, IFunctionTree tree, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
    4142      ItemList values = GetVariableValue<ItemList>("Values", scope, false, false);
    4243      if (values == null) {
     
    5354      int minTimeOffset = GetVariableValue<IntData>("MinTimeOffset", scope, true).Data;
    5455      int conditionVariable = GetVariableValue<IntData>("ConditionVariable", scope, true).Data;
    55       int skippedSampels = 0;
    5656
    57       for (int sample = start; sample < end; sample++) {
    58         // check if condition variable is true between sample - minTimeOffset and sample - maxTimeOffset
    59         bool skip = false;
    60         for (int checkIndex = sample + minTimeOffset; checkIndex <= sample + maxTimeOffset && !skip ; checkIndex++) {
    61           if (dataset.GetValue(checkIndex, conditionVariable) == 0) {
    62             skip = true;
    63             skippedSampels++;
    64           }
    65         }
    66         if (!skip) {
    67           ItemList row = new ItemList();
    68           double estimated = evaluator.Evaluate(sample);
    69           double original = dataset.GetValue(sample, targetVariable);
    70          
    71           row.Add(new DoubleData(estimated));
    72           row.Add(new DoubleData(original));
    73           values.Add(row);
    74         }
     57      var rows = from row in Enumerable.Range(start, end - start)
     58                 // check if condition variable is true between sample - minTimeOffset and sample - maxTimeOffset
     59                 // => select rows where the value of the condition variable is different from zero in the whole range
     60                 where (from neighbour in Enumerable.Range(row + minTimeOffset, maxTimeOffset - minTimeOffset)
     61                        let value = dataset.GetValue(neighbour, conditionVariable)
     62                        where value == 0
     63                        select neighbour).Any() == false
     64                 select row;
     65
     66
     67      double[] estimatedValues = evaluator.Evaluate(dataset, tree, rows).ToArray();
     68      double[] originalValues = (from row in rows select dataset.GetValue(row, targetVariable)).ToArray();
     69      for (int i = 0; i < rows.Count(); i++) {
     70        ItemList row = new ItemList();
     71        row.Add(new DoubleData(estimatedValues[i]));
     72        row.Add(new DoubleData(originalValues[i]));
     73        values.Add(row);
    7574      }
    76       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data -= skippedSampels;
     75
     76      scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data -= (end - start) - rows.Count();
    7777    }
    7878  }
Note: See TracChangeset for help on using the changeset viewer.