Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/14/09 13:46:57 (16 years ago)
Author:
gkronber
Message:

Refactored GP evaluation to make it possible to use different evaluators to interpret function trees. #615 (Evaluation of HL3 function trees should be equivalent to evaluation in HL2)

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

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/BakedTreeEvaluator.cs

    r1787 r1796  
    3434  /// Not thread-safe!
    3535  /// </summary>
    36   public class BakedTreeEvaluator {
     36  public class BakedTreeEvaluator : ItemBase, ITreeEvaluator {
    3737    private const double EPSILON = 1.0e-7;
    3838    private double estimatedValueMax;
     
    5353    private int sampleIndex;
    5454
    55     public void ResetEvaluator(BakedFunctionTree functionTree, Dataset dataset, int targetVariable, int start, int end, double punishmentFactor) {
     55    public void ResetEvaluator(Dataset dataset, int targetVariable, int start, int end, double punishmentFactor) {
    5656      this.dataset = dataset;
    5757      double maximumPunishment = punishmentFactor * dataset.GetRange(targetVariable, start, end);
     
    6262      estimatedValueMax = targetMean + maximumPunishment;
    6363
    64       List<LightWeightFunction> linearRepresentation = functionTree.LinearRepresentation;
    65       codeArr = new Instr[linearRepresentation.Count];
    66       int i = 0;
    67       foreach (LightWeightFunction f in linearRepresentation) {
    68         codeArr[i++] = TranslateToInstr(f);
    69       }
    7064    }
    7165
     
    9488    }
    9589
    96     public double Evaluate(int sampleIndex) {
     90    public double Evaluate(IFunctionTree functionTree, int sampleIndex) {
     91      BakedFunctionTree bakedTree = functionTree as BakedFunctionTree;
     92      if (bakedTree == null) throw new ArgumentException("BakedTreeEvaluator can only evaluate BakedFunctionTrees");
     93
     94      List<LightWeightFunction> linearRepresentation = bakedTree.LinearRepresentation;
     95      codeArr = new Instr[linearRepresentation.Count];
     96      int i = 0;
     97      foreach (LightWeightFunction f in linearRepresentation) {
     98        codeArr[i++] = TranslateToInstr(f);
     99      }
     100
    97101      PC = 0;
    98102      this.sampleIndex = sampleIndex;
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/CoefficientOfDeterminationEvaluator.cs

    r712 r1796  
    4242    }
    4343
    44     public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     44    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, IFunctionTree tree, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    4545      double errorsSquaredSum = 0.0;
    4646      double originalDeviationTotalSumOfSquares = 0.0;
    4747      double targetMean = dataset.GetMean(targetVariable, start, end);
     48
     49      double originalSum = 0.0;
     50      int n = 0;
    4851      for (int sample = start; sample < end; sample++) {
    49         double estimated = evaluator.Evaluate(sample);
     52        double estimated = evaluator.Evaluate(tree, sample);
    5053        double original = dataset.GetValue(sample, targetVariable);
    5154        if (updateTargetValues) {
     
    5659          errorsSquaredSum += error * error;
    5760
    58           double origDeviation = original - targetMean;
    59           originalDeviationTotalSumOfSquares += origDeviation * origDeviation;
     61          originalSum += original;
     62          n++;
     63        }
     64      }
     65
     66      double originalMean = originalSum / n;
     67      for(int sample = start; sample < end; sample++){
     68        double original = dataset.GetValue(sample, targetVariable);
     69        if (!double.IsNaN(original) && !double.IsInfinity(original)) {
     70          original = original - originalMean;
     71          original = original * original;
     72          originalDeviationTotalSumOfSquares += original;
    6073        }
    6174      }
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/EarlyStoppingMeanSquaredErrorEvaluator.cs

    r1794 r1796  
    4444
    4545    // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE
    46     public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     46    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, IFunctionTree tree, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    4747      double qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data;
    4848      DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
     
    5656      int n = 0;
    5757      for (int sample = start; sample < end; sample++) {
    58         double estimated = evaluator.Evaluate(sample);
     58        double estimated = evaluator.Evaluate(tree, sample);
    5959        double original = dataset.GetValue(sample, targetVariable);
    6060        if (updateTargetValues) {
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/GPEvaluatorBase.cs

    r712 r1796  
    3333    public GPEvaluatorBase()
    3434      : base() {
     35      AddVariableInfo(new VariableInfo("TreeEvaluator", "The evaluator that should be used to evaluate the expression tree", typeof(ITreeEvaluator), VariableKind.In));
    3536      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IFunctionTree), VariableKind.In));
    3637      AddVariableInfo(new VariableInfo("TreeSize", "Size (number of nodes) of the tree to evaluate", typeof(IntData), VariableKind.In));
     
    4849      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
    4950      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    50       BakedFunctionTree functionTree = GetVariableValue<BakedFunctionTree>("FunctionTree", scope, true);
     51      IFunctionTree functionTree = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
    5152      double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
    5253      int treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data;
     
    5556      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
    5657      bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, true).Data;
     58      ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true);
     59
    5760      double[] backupValues = null;
    5861      // prepare for autoregressive modelling by saving the original values of the target-variable to a backup array
     
    6568      }
    6669
    67       // initialize and reset the evaluator
    68       BakedTreeEvaluator evaluator = new BakedTreeEvaluator();
    69       evaluator.ResetEvaluator(functionTree, dataset, targetVariable, start, end, punishmentFactor);
    70 
    71       Evaluate(scope, evaluator, dataset, targetVariable, start, end, useEstimatedValues);
     70      Evaluate(scope, evaluator, functionTree, dataset, targetVariable, start, end, useEstimatedValues);
    7271
    7372      // restore the values of the target variable from the backup array if necessary
     
    8382    }
    8483
    85     public abstract void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues);
     84    public abstract void Evaluate(IScope scope, ITreeEvaluator evaluator, IFunctionTree tree, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues);
    8685  }
    8786}
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanAbsolutePercentageErrorEvaluator.cs

    r712 r1796  
    4343    }
    4444
    45     public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     45    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, IFunctionTree tree, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    4646      double errorsSum = 0.0;
    4747      int n = 0;
    4848      for (int sample = start; sample < end; sample++) {
    49         double estimated = evaluator.Evaluate(sample);
     49        double estimated = evaluator.Evaluate(tree, sample);
    5050        double original = dataset.GetValue(sample, targetVariable);
    5151
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanAbsolutePercentageOfRangeErrorEvaluator.cs

    r1287 r1796  
    4343    }
    4444
    45     public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     45    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, IFunctionTree tree, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    4646      double errorsSum = 0.0;
    4747      int n = 0;
    4848      double range = dataset.GetRange(targetVariable, start, end);
    4949      for (int sample = start; sample < end; sample++) {
    50         double estimated = evaluator.Evaluate(sample);
     50        double estimated = evaluator.Evaluate(tree, sample);
    5151        double original = dataset.GetValue(sample, targetVariable);
    5252
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanSquaredErrorEvaluator.cs

    r1794 r1796  
    4343    }
    4444
    45     public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     45    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, IFunctionTree tree, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    4646      double errorsSquaredSum = 0;
    4747      int n = 0;
    4848      for (int sample = start; sample < end; sample++) {
    4949        double original = dataset.GetValue(sample, targetVariable);
    50         double estimated = evaluator.Evaluate(sample);
     50        double estimated = evaluator.Evaluate(tree, sample);
    5151        if (updateTargetValues) {
    5252          dataset.SetValue(sample, targetVariable, estimated);
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/SimpleEvaluator.cs

    r712 r1796  
    3636    }
    3737
    38     public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     38    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, IFunctionTree tree, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    3939      ItemList values = GetVariableValue<ItemList>("Values", scope, false, false);
    4040      if (values == null) {
     
    5050      for (int sample = start; sample < end; sample++) {
    5151        ItemList row = new ItemList();
    52         double estimated = evaluator.Evaluate(sample);
     52        double estimated = evaluator.Evaluate(tree, sample);
    5353        double original = dataset.GetValue(sample, targetVariable);
    5454        if (updateTargetValues) {
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/UncertainMeanSquaredErrorEvaluator.cs

    r769 r1796  
    5050
    5151    // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE
    52     public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     52    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, IFunctionTree tree, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    5353      double qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data;
    5454      int minSamples = GetVariableValue<IntData>("MinEvaluatedSamples", scope, true).Data;
     
    7878      int n = 0;
    7979      for (int sample = 0; sample < rows; sample++) {
    80         double estimated = evaluator.Evaluate(indexes[sample]);
     80        double estimated = evaluator.Evaluate(tree, indexes[sample]);
    8181        double original = dataset.GetValue(indexes[sample], targetVariable);
    8282        if (!double.IsNaN(original) && !double.IsInfinity(original)) {
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VarianceAccountedForEvaluator.cs

    r712 r1796  
    5353    }
    5454
    55     public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     55    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, IFunctionTree tree, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    5656      int nSamples = end - start;
    5757      double[] errors = new double[nSamples];
    5858      double[] originalTargetVariableValues = new double[nSamples];
    5959      for (int sample = start; sample < end; sample++) {
    60         double estimated = evaluator.Evaluate(sample);
     60        double estimated = evaluator.Evaluate(tree, sample);
    6161        double original = dataset.GetValue(sample, targetVariable);
    6262        if (updateTargetValues) {
     
    6666          errors[sample - start] = original - estimated;
    6767          originalTargetVariableValues[sample - start] = original;
     68        } else {
     69          errors[sample - start] = double.NaN;
     70          originalTargetVariableValues[sample - start] = double.NaN;
    6871        }
    6972      }
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/HeuristicLab.GP.StructureIdentification-3.3.csproj

    r1572 r1796  
    8989    <Compile Include="Constant.cs" />
    9090    <Compile Include="AlgorithmBase.cs" />
     91    <Compile Include="TreeEvaluatorInjector.cs" />
     92    <Compile Include="ITreeEvaluator.cs" />
    9193    <Compile Include="Evaluators\MeanAbsolutePercentageOfRangeErrorEvaluator.cs" />
    9294    <Compile Include="FunctionLibraryInjector.cs" />
Note: See TracChangeset for help on using the changeset viewer.