Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/29/08 11:21:04 (15 years ago)
Author:
gkronber
Message:

fixed #328 by restructuring evaluation operators to remove state in evaluation operators.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/GPEvaluatorBase.cs

    r656 r702  
    3131namespace HeuristicLab.GP.StructureIdentification {
    3232  public abstract class GPEvaluatorBase : OperatorBase {
    33     private int targetVariable;
    34     private int start;
    35     private int end;
    36     private bool useEstimatedValues;
    37     private double[] backupValues;
    38     private int evaluatedSamples;
    39     private double estimatedValueMax;
    40     private double estimatedValueMin;
    41     private int treeSize;
    42     private double totalEvaluatedNodes;
    43     protected Dataset dataset;
    44     private double targetMean;
    45     private BakedTreeEvaluator evaluator;
    46     protected double TargetMean { get { return targetMean; } }
    47 
    4833    public GPEvaluatorBase()
    4934      : base() {
     
    6146    public override IOperation Apply(IScope scope) {
    6247      // get all variable values
    63       targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
    64       dataset = GetVariableValue<Dataset>("Dataset", scope, true);
     48      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
     49      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    6550      BakedFunctionTree functionTree = GetVariableValue<BakedFunctionTree>("FunctionTree", scope, true);
    66       double maximumPunishment = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data * dataset.GetRange(targetVariable);
    67       treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data;
    68       totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data;
     51      double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
     52      int treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data;
     53      double totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data;
    6954      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
    7055      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
    71       useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, true).Data;
     56      bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, true).Data;
     57      double[] backupValues = null;
    7258      // prepare for autoregressive modelling by saving the original values of the target-variable to a backup array
    7359      if(useEstimatedValues &&
    74         (backupValues == null || start != this.start || end != this.end)) {
    75         this.start = start;
    76         this.end = end;
     60        (backupValues == null || backupValues.Length!=end-start)) {
    7761        backupValues = new double[end - start];
    7862        for(int i = start; i < end; i++) {
     
    8064        }
    8165      }
    82       // get the mean of the values of the target variable to determin the max and min bounds of the estimated value
    83       targetMean = dataset.GetMean(targetVariable, start, end - 1);
    84       estimatedValueMin = targetMean - maximumPunishment;
    85       estimatedValueMax = targetMean + maximumPunishment;
    8666
    8767      // initialize and reset the evaluator
    88       if(evaluator == null) evaluator = new BakedTreeEvaluator();
    89       evaluator.ResetEvaluator(functionTree, dataset);
    90       evaluatedSamples = 0;
     68      BakedTreeEvaluator evaluator = new BakedTreeEvaluator();
     69      evaluator.ResetEvaluator(functionTree, dataset, targetVariable, start, end, punishmentFactor);
    9170
    92       Evaluate(start, end);
     71      Evaluate(scope, evaluator, dataset, targetVariable, start, end, useEstimatedValues);
    9372
    9473      // restore the values of the target variable from the backup array if necessary
    95       if(useEstimatedValues) RestoreDataset(dataset, targetVariable, start, end);
     74      if(useEstimatedValues) {
     75        for(int i = start; i < end; i++) {
     76          dataset.SetValue(i, targetVariable, backupValues[i - start]);
     77        }
     78      }
     79
    9680      // update the value of total evaluated nodes
    97       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * evaluatedSamples;
     81      scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (end-start);
    9882      return null;
    9983    }
    10084
    101     private void RestoreDataset(Dataset dataset, int targetVariable, int from, int to) {
    102       for(int i = from; i < to; i++) {
    103         dataset.SetValue(i, targetVariable, backupValues[i - from]);
    104       }
    105     }
    106 
    107     public abstract void Evaluate(int start, int end);
    108 
    109     public void SetOriginalValue(int sample, double value) {
    110       if(useEstimatedValues) {
    111         dataset.SetValue(sample, targetVariable, value);
    112       }
    113     }
    114 
    115     public double GetOriginalValue(int sample) {
    116       return dataset.GetValue(sample, targetVariable);
    117     }
    118 
    119     public double GetEstimatedValue(int sample) {
    120       evaluatedSamples++;
    121       double estimated = evaluator.Evaluate(sample);
    122       if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
    123         estimated = estimatedValueMax;
    124       } else if(estimated > estimatedValueMax) {
    125         estimated = estimatedValueMax;
    126       } else if(estimated < estimatedValueMin) {
    127         estimated = estimatedValueMin;
    128       }
    129       return estimated;
    130     }
     85    public abstract void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues);
    13186  }
    13287}
Note: See TracChangeset for help on using the changeset viewer.