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/3.3
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/BaseClasses/TreeEvaluatorBase.cs

    r2364 r2578  
    6060    }
    6161
    62     public void PrepareForEvaluation(Dataset dataset, IFunctionTree functionTree) {
     62    [Obsolete]
     63    public virtual void PrepareForEvaluation(Dataset dataset, IFunctionTree functionTree) {
    6364      this.dataset = dataset;
    6465      codeArr = new Instr[functionTree.GetSize()];
     
    9495    }
    9596
    96     public double Evaluate(int sampleIndex) {
     97    [Obsolete]
     98    public virtual double Evaluate(int sampleIndex) {
    9799      PC = 0;
    98100      this.sampleIndex = sampleIndex;
     
    101103      if (double.IsNaN(estimated)) estimated = UpperEvaluationLimit;
    102104      return estimated;
     105    }
     106
     107    public virtual IEnumerable<double> Evaluate(Dataset dataset, IFunctionTree tree, IEnumerable<int> rows) {
     108      PrepareForEvaluation(dataset, tree);
     109      foreach (int row in rows)
     110        yield return Evaluate(row);
    103111    }
    104112
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/EarlyStoppingMeanSquaredErrorEvaluator.cs

    r2577 r2578  
    2222using HeuristicLab.Core;
    2323using HeuristicLab.Data;
     24using System;
     25using HeuristicLab.GP.Interfaces;
     26using HeuristicLab.DataAnalysis;
     27using System.Collections;
     28using System.Collections.Generic;
     29using System.Linq;
    2430
    2531namespace HeuristicLab.GP.StructureIdentification {
     
    3945
    4046    // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE
    41     public override void Evaluate(IScope scope, ITreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end) {
     47    public override void Evaluate(IScope scope, IFunctionTree tree, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
    4248      double qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, true).Data;
    4349      DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
     
    4652        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse));
    4753      }
    48 
    4954      double errorsSquaredSum = 0;
    5055      int rows = end - start;
    5156      int n = 0;
    52       for (int sample = start; sample < end; sample++) {
    53         double estimated = evaluator.Evaluate(sample);
     57      int sample = start;
     58      foreach (var estimatedValue in evaluator.Evaluate(dataset, tree, Enumerable.Range(start, end - start))) {
    5459        double original = dataset.GetValue(sample, targetVariable);
    55        
     60
    5661        if (!double.IsNaN(original) && !double.IsInfinity(original)) {
    57           double error = estimated - original;
     62          double error = estimatedValue - original;
    5863          errorsSquaredSum += error * error;
    5964          n++;
    6065        }
    61         // check the limit and stop as soon as we hit the limit
    62         if (errorsSquaredSum / rows >= qualityLimit) {
     66        // check the limit every 30 samples and stop as soon as we hit the limit
     67        if (n % 30 == 29 && errorsSquaredSum / rows >= qualityLimit) {
    6368          mse.Data = errorsSquaredSum / (n + 1); // return estimated MSE (when the remaining errors are on average the same)
    6469          return;
    6570        }
     71        sample++;
    6672      }
    6773      errorsSquaredSum /= n;
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/GPEvaluatorBase.cs

    r2577 r2578  
    4646      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
    4747      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
     48
    4849      ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true);
    49      
    50       evaluator.PrepareForEvaluation(dataset, gpModel.FunctionTree);
    51       Evaluate(scope, evaluator, dataset, targetVariable, start, end);
     50      Evaluate(scope, gpModel.FunctionTree, evaluator, dataset, targetVariable, start, end);
    5251
    5352      // update the value of total evaluated nodes
     
    5655    }
    5756
    58     public abstract void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end);
     57    public abstract void Evaluate(IScope scope, IFunctionTree tree, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end);
    5958  }
    6059}
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/NodeBasedVariableImpactCalculator.cs

    r2454 r2578  
    138138
    139139    private static double CalculateMSE(Dataset dataset, ITreeEvaluator evaluator, IFunctionTree tree, int targetVariable, int start, int end) {
    140       double[,] values = new double[end - start, 2];
    141       evaluator.PrepareForEvaluation(dataset, tree);
    142       for (int i = start; i < end; i++) {
    143         values[i - start, 0] = dataset.GetValue(i, targetVariable);
    144         values[i - start, 1] = evaluator.Evaluate(i);
    145       }
     140
     141      double[,] values = Matrix<double>.Create(
     142        dataset.GetVariableValues(targetVariable, start, end),
     143        evaluator.Evaluate(dataset, tree, Enumerable.Range(start, end - start)).ToArray());
    146144      return SimpleMSEEvaluator.Calculate(values);
    147145    }
     
    155153
    156154    private static double CalculateReplacementValue(Dataset dataset, ITreeEvaluator evaluator, IFunctionTree tree, int targetVariable, int start, int end) {
    157       double[] values = new double[end - start];
    158       evaluator.PrepareForEvaluation(dataset, tree);
    159       for (int i = start; i < end; i++) {
    160         values[i - start] = evaluator.Evaluate(i);
    161       }
    162       return Statistics.Median(values);
     155      return Statistics.Median(evaluator.Evaluate(dataset, tree, Enumerable.Range(start, end - start)).ToArray());
    163156    }
    164157
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/SimpleEvaluator.cs

    r2577 r2578  
    2323using HeuristicLab.Data;
    2424using HeuristicLab.DataAnalysis;
     25using HeuristicLab.GP.Interfaces;
     26using HeuristicLab.Modeling;
     27using System.Linq;
    2528
    2629namespace HeuristicLab.GP.StructureIdentification {
     
    3134    }
    3235
    33     public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
     36    public override void Evaluate(IScope scope, IFunctionTree tree, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
    3437      DoubleMatrixData values = GetVariableValue<DoubleMatrixData>("Values", scope, false, false);
    3538      if (values == null) {
     
    4245      }
    4346
    44       double[,] v = new double[end - start, 2];
    45 
    46       for (int sample = start; sample < end; sample++) {
    47         double estimated = evaluator.Evaluate(sample);
    48         double original = dataset.GetValue(sample, targetVariable);
    49        
    50         v[sample - start, 0] = original;
    51         v[sample - start, 1] = estimated;
    52       }
     47      double[,] v = Matrix<double>.Create(
     48        dataset.GetVariableValues(targetVariable, start, end),
     49        evaluator.Evaluate(dataset, tree, Enumerable.Range(start, end - start)).ToArray());
    5350      values.Data = v;
    5451    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/SimpleGPEvaluatorBase.cs

    r2577 r2578  
    2323using HeuristicLab.Data;
    2424using HeuristicLab.DataAnalysis;
     25using HeuristicLab.Modeling;
     26using HeuristicLab.GP.Interfaces;
     27using System.Linq;
    2528
    2629namespace HeuristicLab.GP.StructureIdentification {
     
    3336    }
    3437
    35     public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
     38    public override void Evaluate(IScope scope, IFunctionTree tree, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
    3639      // store original and estimated values in a double array
    37       double[,] values = new double[end - start, 2];
    38       for (int sample = start; sample < end; sample++) {
    39         double original = dataset.GetValue(sample, targetVariable);
    40         double estimated = evaluator.Evaluate(sample);
    41        
    42         values[sample - start, 0] = estimated;
    43         values[sample - start, 1] = original;
    44       }
     40      double[,] values = Matrix<double>.Create(
     41        dataset.GetVariableValues(targetVariable, start, end),
     42        evaluator.Evaluate(dataset, tree, Enumerable.Range(start, end - start)).ToArray());
    4543
    4644      double quality = Evaluate(values);
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/UncertainMeanSquaredErrorEvaluator.cs

    r2577 r2578  
    2424using HeuristicLab.Data;
    2525using HeuristicLab.Random;
     26using HeuristicLab.GP.Interfaces;
     27using System.Collections.Generic;
     28using System.Linq;
    2629
    2730namespace HeuristicLab.GP.StructureIdentification {
     
    4548
    4649    // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE
    47     public override void Evaluate(IScope scope, ITreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end) {
     50    public override void Evaluate(IScope scope, IFunctionTree tree, ITreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end) {
    4851      double qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, true).Data;
    4952      int minSamples = GetVariableValue<IntData>("MinEvaluatedSamples", scope, true).Data;
     
    7275      int[] indexes = InitIndexes(mt, start, end);
    7376      int n = 0;
    74       for (int sample = 0; sample < rows; sample++) {
    75         double estimated = evaluator.Evaluate(indexes[sample]);
     77      int sample = 0;
     78      foreach (double estimated in evaluator.Evaluate(dataset, tree, indexes)) {
    7679        double original = dataset.GetValue(indexes[sample], targetVariable);
    7780        if (!double.IsNaN(original) && !double.IsInfinity(original)) {
     
    9194          }
    9295        }
     96        sample++;
    9397      }
    9498
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/HL2TreeEvaluator.cs

    r2328 r2578  
    3131    public HL2TreeEvaluator(double minValue, double maxValue) : base(minValue, maxValue) { }
    3232
     33    [Obsolete]
    3334    protected override double EvaluateBakedCode() {
    3435      Instr currInstr = codeArr[PC++];
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/HL3TreeEvaluator.cs

    r2449 r2578  
    3333    public HL3TreeEvaluator(double minValue, double maxValue) : base(minValue, maxValue) { }
    3434
     35    [Obsolete]
    3536    protected override double EvaluateBakedCode() {
    3637      Instr currInstr = codeArr[PC++];
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/ITreeEvaluator.cs

    r2328 r2578  
    2323using HeuristicLab.DataAnalysis;
    2424using HeuristicLab.GP.Interfaces;
     25using System;
     26using System.Collections.Generic;
    2527
    2628namespace HeuristicLab.GP.StructureIdentification {
     
    2830    double LowerEvaluationLimit { get; set; }
    2931    double UpperEvaluationLimit { get; set; }
     32    [Obsolete]
    3033    void PrepareForEvaluation(Dataset dataset, IFunctionTree functionTree);
     34    [Obsolete]
    3135    double Evaluate(int sampleIndex);
     36    IEnumerable<double> Evaluate(Dataset dataset, IFunctionTree functionTree, IEnumerable<int> rows);
    3237  }
    3338}
Note: See TracChangeset for help on using the changeset viewer.