Free cookie consent management tool by TermsFeed Policy Generator

Changeset 702


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

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

Location:
trunk/sources
Files:
1 added
1 deleted
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/AccuracyEvaluator.cs

    r668 r702  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.GP.StructureIdentification;
     29using HeuristicLab.DataAnalysis;
    2930
    3031namespace HeuristicLab.GP.StructureIdentification.Classification {
    31   public class AccuracyEvaluator : GPEvaluatorBase {
     32  public class AccuracyEvaluator : GPClassificationEvaluatorBase {
    3233    private const double EPSILON = 1.0E-6;
    33     private double[] classesArr;
    34     private double[] thresholds;
    35     private DoubleData accuracy;
    3634    public override string Description {
    3735      get {
     
    4341      : base() {
    4442      AddVariableInfo(new VariableInfo("Accuracy", "The total accuracy of the model (ratio of correctly classified instances to total number of instances)", typeof(DoubleData), VariableKind.New));
    45       AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));
    4643    }
    4744
    48     public override IOperation Apply(IScope scope) {
    49       accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false);
     45    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) {
     46      DoubleData accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false);
    5047      if(accuracy == null) {
    5148        accuracy = new DoubleData();
     
    5350      }
    5451
    55       ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
    56       classesArr = new double[classes.Count];
    57       for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
    58       Array.Sort(classesArr);
    59       thresholds = new double[classes.Count - 1];
    60       for(int i = 0; i < classesArr.Length - 1; i++) {
    61         thresholds[i] = (classesArr[i] + classesArr[i + 1]) / 2.0;
    62       }
    63 
    64       return base.Apply(scope);
    65     }
    66 
    67     public override void Evaluate(int start, int end) {
    6852      int nSamples = end - start;
    6953      int nCorrect = 0;
    7054      for(int sample = start; sample < end; sample++) {
    71         double est = GetEstimatedValue(sample);
    72         double origClass = GetOriginalValue(sample);
     55        double est = evaluator.Evaluate(sample);
     56        double origClass = dataset.GetValue(targetVariable, sample);
    7357        double estClass = double.NaN;
    7458        // if estimation is lower than the smallest threshold value -> estimated class is the lower class
    75         if(est < thresholds[0]) estClass = classesArr[0];
     59        if(est < thresholds[0]) estClass = classes[0];
    7660        // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class
    77         else if(est >= thresholds[thresholds.Length - 1]) estClass = classesArr[classesArr.Length - 1];
     61        else if(est >= thresholds[thresholds.Length - 1]) estClass = classes[classes.Length - 1];
    7862        else {
    7963          // otherwise the estimated class is the class which upper threshold is larger than the estimated value
    8064          for(int k = 0; k < thresholds.Length; k++) {
    8165            if(thresholds[k] > est) {
    82               estClass = classesArr[k];
     66              estClass = classes[k];
    8367              break;
    8468            }
    8569          }
    8670        }
    87         SetOriginalValue(sample, estClass);
    8871        if(Math.Abs(estClass - origClass) < EPSILON) nCorrect++;
    8972      }
  • trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/ClassificationMeanSquaredErrorEvaluator.cs

    r668 r702  
    2929
    3030namespace HeuristicLab.GP.StructureIdentification.Classification {
    31   public class ClassificationMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator {
    32     private const double EPSILON = 1.0E-6;
    33     private double[] classesArr;
     31  public class ClassificationMeanSquaredErrorEvaluator : GPClassificationEvaluatorBase {
     32    private const double EPSILON = 1.0E-7;
    3433    public override string Description {
    3534      get {
     
    4140    public ClassificationMeanSquaredErrorEvaluator()
    4241      : base() {
    43       AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));
     42      AddVariableInfo(new VariableInfo("MSE", "The mean squared error of the model", typeof(DoubleData), VariableKind.New));
    4443    }
    4544
    46     public override IOperation Apply(IScope scope) {
    47       ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
    48       classesArr = new double[classes.Count];
    49       for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
    50       Array.Sort(classesArr);
    51       return base.Apply(scope);
    52     }
    53 
    54     public override void Evaluate(int start, int end) {
     45    public override void  Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end)
     46{
    5547      double errorsSquaredSum = 0;
    5648      for(int sample = start; sample < end; sample++) {
    57         double estimated = GetEstimatedValue(sample);
    58         double original = GetOriginalValue(sample);
    59         SetOriginalValue(sample, estimated);
     49        double estimated = evaluator.Evaluate(sample);
     50        double original = dataset.GetValue(targetVariable, sample);
    6051        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    6152          double error = estimated - original;
     
    6354          // on the lower end and upper end only add linear error if the absolute error is larger than 1
    6455          // the error>1.0 constraint is needed for balance because in the interval ]-1, 1[ the squared error is smaller than the absolute error
    65           if((IsEqual(original, classesArr[0]) && error < -1.0) ||
    66             (IsEqual(original, classesArr[classesArr.Length - 1]) && error > 1.0)) {
     56          if((IsEqual(original, classes[0]) && error < -1.0) ||
     57            (IsEqual(original, classes[classes.Length - 1]) && error > 1.0)) {
    6758            errorsSquaredSum += Math.Abs(error); // only add linear error below the smallest class or above the largest class
    6859          } else {
     
    7667        errorsSquaredSum = double.MaxValue;
    7768      }
     69
     70      DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
     71      if(mse == null) {
     72        mse = new DoubleData();
     73        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse));
     74      }
     75
    7876      mse.Data = errorsSquaredSum;
    7977    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/ConfusionMatrixEvaluator.cs

    r668 r702  
    2929
    3030namespace HeuristicLab.GP.StructureIdentification.Classification {
    31   public class ConfusionMatrixEvaluator : GPEvaluatorBase {
    32     private const double EPSILON = 1.0E-6;
    33     private double[] classesArr;
    34     private double[] thresholds;
    35     private IntMatrixData matrix;
     31  public class ConfusionMatrixEvaluator : GPClassificationEvaluatorBase {
    3632    public override string Description {
    3733      get {
     
    4339      : base() {
    4440      AddVariableInfo(new VariableInfo("ConfusionMatrix", "The confusion matrix of the model", typeof(IntMatrixData), VariableKind.New));
    45       AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));
    4641    }
    4742
    48     public override IOperation Apply(IScope scope) {
    49       ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
    50       classesArr = new double[classes.Count];
    51       for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
    52       Array.Sort(classesArr);
    53       thresholds = new double[classes.Count - 1];
    54       for(int i = 0; i < classesArr.Length - 1; i++) {
    55         thresholds[i] = (classesArr[i] + classesArr[i + 1]) / 2.0;
     43    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) {
     44      IntMatrixData matrix = GetVariableValue<IntMatrixData>("ConfusionMatrix", scope, false, false);
     45      if(matrix == null) {
     46        matrix = new IntMatrixData(new int[classes.Length, classes.Length]);
     47        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ConfusionMatrix"), matrix));
    5648      }
    5749
    58       matrix = GetVariableValue<IntMatrixData>("ConfusionMatrix", scope, false, false);
    59       if(matrix == null) {
    60         matrix = new IntMatrixData(new int[classesArr.Length, classesArr.Length]);
    61         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ConfusionMatrix"), matrix));
    62       }
    63       return base.Apply(scope);
    64     }
    65 
    66     public override void Evaluate(int start, int end) {
    6750      int nSamples = end - start;
    6851      for(int sample = start; sample < end; sample++) {
    69         double est = GetEstimatedValue(sample);
    70         double origClass = GetOriginalValue(sample);
     52        double est = evaluator.Evaluate(sample);
     53        double origClass = dataset.GetValue(targetVariable,sample);
    7154        int estClassIndex = -1;
    7255        // if estimation is lower than the smallest threshold value -> estimated class is the lower class
    7356        if(est < thresholds[0]) estClassIndex = 0;
    7457        // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class
    75         else if(est >= thresholds[thresholds.Length - 1]) estClassIndex = classesArr.Length - 1;
     58        else if(est >= thresholds[thresholds.Length - 1]) estClassIndex = classes.Length - 1;
    7659        else {
    7760          // otherwise the estimated class is the class which upper threshold is larger than the estimated value
     
    8366          }
    8467        }
    85         SetOriginalValue(sample, classesArr[estClassIndex]);
    8668
    87         int origClassIndex = -1;
    88         for(int i = 0; i < classesArr.Length; i++) {
    89           if(IsEqual(origClass, classesArr[i])) origClassIndex = i;
     69        // find the first threshold index that is larger to the original value
     70        int origClassIndex = classes.Length-1;
     71        for(int i = 0; i < thresholds.Length; i++) {
     72          if(origClass < thresholds[i]) {
     73            origClassIndex = i;
     74            break;
     75          }
    9076        }
    9177        matrix.Data[origClassIndex, estClassIndex]++;
    9278      }
    9379    }
    94 
    95     private bool IsEqual(double x, double y) {
    96       return Math.Abs(x - y) < EPSILON;
    97     }
    9880  }
    9981}
  • trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/HeuristicLab.GP.StructureIdentification.Classification.csproj

    r669 r702  
    6767    <Compile Include="ClassificationMeanSquaredErrorEvaluator.cs" />
    6868    <Compile Include="ConfusionMatrixEvaluator.cs" />
     69    <Compile Include="GPClassificationEvaluatorBase.cs" />
    6970    <Compile Include="CrossValidation.cs" />
    7071    <Compile Include="FunctionLibraryInjector.cs">
     
    7273    </Compile>
    7374    <Compile Include="HeuristicLabGPClassificationPlugin.cs" />
    74     <Compile Include="MCCEvaluator.cs" />
    7575    <Compile Include="MulticlassModeller.cs" />
    7676    <Compile Include="MulticlassOneVsOneAnalyzer.cs" />
  • trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/MulticlassOneVsOneAnalyzer.cs

    r668 r702  
    7979
    8080        BakedTreeEvaluator evaluator = new BakedTreeEvaluator();
    81         evaluator.ResetEvaluator(functionTree, dataset);
     81        evaluator.ResetEvaluator(functionTree, dataset, targetVariable, samplesStart, samplesEnd, 1.0);
    8282
    8383        for(int i = 0; i < (samplesEnd - samplesStart); i++) {
  • trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/AveragePercentageChangeEvaluator.cs

    r686 r702  
    3030namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
    3131  public class AvergePercentageChangeEvaluator : GPEvaluatorBase {
    32     protected DoubleData apc;
    33     private bool differential;
    3432    public override string Description {
    3533      get {
     
    4442    }
    4543
    46     public override IOperation Apply(IScope scope) {
    47       differential = GetVariableValue<BoolData>("Differential", scope, true).Data;
    48       apc = GetVariableValue<DoubleData>("APC", scope, false, false);
     44    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     45      bool differential = GetVariableValue<BoolData>("Differential", scope, true).Data;
     46      DoubleData apc = GetVariableValue<DoubleData>("APC", scope, false, false);
    4947      if(apc == null) {
    5048        apc = new DoubleData();
     
    5250      }
    5351
    54       return base.Apply(scope);
    55     }
    56 
    57     public override void Evaluate(int start, int end) {
    5852      double percentageSum = 0;
    5953      for(int sample = start; sample < end; sample++) {
     
    6256        double estimatedPercentageChange;
    6357        if(differential) {
    64           prevOriginal = GetOriginalValue(sample - 1);
    65           originalPercentageChange = (GetOriginalValue(sample) - prevOriginal) / prevOriginal;
    66           estimatedPercentageChange = (GetEstimatedValue(sample) - prevOriginal) / prevOriginal;
    67           SetOriginalValue(sample, estimatedPercentageChange*prevOriginal + prevOriginal);
     58          prevOriginal = dataset.GetValue(targetVariable,sample - 1);
     59          originalPercentageChange = (dataset.GetValue(targetVariable,sample) - prevOriginal) / prevOriginal;
     60          estimatedPercentageChange = (evaluator.Evaluate(sample) - prevOriginal) / prevOriginal;
     61          if(updateTargetValues) {
     62            dataset.SetValue(targetVariable, sample, estimatedPercentageChange * prevOriginal + prevOriginal);
     63          }
    6864        } else {
    69           originalPercentageChange = GetOriginalValue(sample);
    70           estimatedPercentageChange = GetEstimatedValue(sample);
    71           SetOriginalValue(sample, estimatedPercentageChange);
     65          originalPercentageChange = dataset.GetValue(targetVariable,sample);
     66          estimatedPercentageChange = evaluator.Evaluate(sample);
     67          if(updateTargetValues) {
     68            dataset.SetValue(targetVariable, sample, estimatedPercentageChange);
     69          }
    7270        }
    7371        if(!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) {
  • trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/ProfitEvaluator.cs

    r686 r702  
    3030namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
    3131  public class ProfitEvaluator : GPEvaluatorBase {
    32     protected DoubleData profit;
    33     private int exchangeRateVarIndex;
    34     private double transactionCost;
    3532    public override string Description {
    3633      get {
     
    4643    }
    4744
    48     public override IOperation Apply(IScope scope) {
    49       exchangeRateVarIndex = GetVariableValue<IntData>("ExchangeRate", scope, true).Data;
    50       transactionCost = GetVariableValue<DoubleData>("TransactionCost", scope, true).Data;
    51       profit = GetVariableValue<DoubleData>("Profit", scope, false, false);
     45    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     46      int exchangeRateVarIndex = GetVariableValue<IntData>("ExchangeRate", scope, true).Data;
     47      double transactionCost = GetVariableValue<DoubleData>("TransactionCost", scope, true).Data;
     48      DoubleData profit = GetVariableValue<DoubleData>("Profit", scope, false, false);
    5249      if(profit == null) {
    5350        profit = new DoubleData();
     
    5552      }
    5653
    57       return base.Apply(scope);
    58     }
    59 
    60     public override void Evaluate(int start, int end) {
    6154      double cA = 1.0; // start with a capital of one entity of A
    6255      double cB = 0;
     
    6457      for(int sample = start; sample < end; sample++) {
    6558        exchangeRate = dataset.GetValue(sample, exchangeRateVarIndex);
    66         double originalPercentageChange = GetOriginalValue(sample);
    67         double estimatedPercentageChange = GetEstimatedValue(sample);
    68         SetOriginalValue(sample, estimatedPercentageChange);
     59        double originalPercentageChange = dataset.GetValue(targetVariable, sample);
     60        double estimatedPercentageChange = evaluator.Evaluate(sample);
     61        if(updateTargetValues) {
     62          dataset.SetValue(targetVariable, sample, estimatedPercentageChange);
     63        }
    6964        if(!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) {
    7065          if(estimatedPercentageChange > 0) {
    7166            // prediction is the rate of B/A will increase (= get more B for one A) => exchange all B to A
    72             cA += (cB / exchangeRate) * (1-transactionCost);
     67            cA += (cB / exchangeRate) * (1 - transactionCost);
    7368            cB = 0;
    7469          } else if(estimatedPercentageChange < 0) {
    7570            // prediction is the rate of B/A will drop (= get more A for one B) => exchange all A to B
    76             cB += (cA * exchangeRate) * (1-transactionCost);
     71            cB += (cA * exchangeRate) * (1 - transactionCost);
    7772            cA = 0;
    7873          }
     
    8681      // at the end we must exchange all B back to A
    8782      // (because we want to buy the local beer not import one from B-country)
    88       profit.Data = cA + ((cB / exchangeRate) * (1-transactionCost));
     83      profit.Data = cA + ((cB / exchangeRate) * (1 - transactionCost));
    8984      profit.Data -= 1.0; // substract the start capital to determine actual profit
    9085    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/TheilInequalityCoefficientEvaluator.cs

    r695 r702  
    3131namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
    3232  public class TheilInequalityCoefficientEvaluator : GPEvaluatorBase {
    33     private DoubleData theilInequaliy;
    34     private DoubleData uBias;
    35     private DoubleData uVariance;
    36     private DoubleData uCovariance;
    37 
    3833    public override string Description {
    3934      get {
     
    5954    }
    6055
    61     public override IOperation Apply(IScope scope) {
    62       theilInequaliy = GetVariableValue<DoubleData>("TheilInequalityCoefficient", scope, false, false);
     56    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     57    #region create result variables
     58      DoubleData theilInequaliy = GetVariableValue<DoubleData>("TheilInequalityCoefficient", scope, false, false);
    6359      if(theilInequaliy == null) {
    6460        theilInequaliy = new DoubleData();
    6561        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficient"), theilInequaliy));
    6662      }
    67       uBias = GetVariableValue<DoubleData>("TheilInequalityCoefficientBias", scope, false, false);
     63      DoubleData uBias = GetVariableValue<DoubleData>("TheilInequalityCoefficientBias", scope, false, false);
    6864      if(uBias == null) {
    6965        uBias = new DoubleData();
    7066        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientBias"), uBias));
    7167      }
    72       uVariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientVariance", scope, false, false);
     68      DoubleData uVariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientVariance", scope, false, false);
    7369      if(uVariance == null) {
    7470        uVariance = new DoubleData();
    7571        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientVariance"), uVariance));
    7672      }
    77       uCovariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientCovariance", scope, false, false);
     73      DoubleData uCovariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientCovariance", scope, false, false);
    7874      if(uCovariance == null) {
    7975        uCovariance = new DoubleData();
    8076        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientCovariance"), uCovariance));
    8177      }
    82       return base.Apply(scope);
    83     }
     78      #endregion
    8479
    85     public override void Evaluate(int start, int end) {
    8680      double errorsSquaredSum = 0.0;
    8781      double originalSquaredSum = 0.0;
    88       double[] estimatedChanges = new double[end-start];
    89       double[] originalChanges = new double[end-start];
     82      double[] estimatedChanges = new double[end - start];
     83      double[] originalChanges = new double[end - start];
    9084      int nSamples = 0;
    9185      for(int sample = start; sample < end; sample++) {
    92         double prevValue = GetOriginalValue(sample - 1);
    93         double estimatedChange = GetEstimatedValue(sample) - prevValue;
    94         double originalChange = GetOriginalValue(sample) - prevValue;
    95         SetOriginalValue(sample, estimatedChange + prevValue);
     86        double prevValue = dataset.GetValue(targetVariable, sample - 1);
     87        double estimatedChange = evaluator.Evaluate(sample) - prevValue;
     88        double originalChange = dataset.GetValue(targetVariable, sample) - prevValue;
     89        if(updateTargetValues) {
     90          dataset.SetValue(targetVariable, sample, estimatedChange + prevValue);
     91        }
    9692        if(!double.IsNaN(originalChange) && !double.IsInfinity(originalChange)) {
    9793          double error = estimatedChange - originalChange;
     
    118114
    119115      // all parts add up to one so I don't have to calculate the correlation coefficient for the covariance propotion
    120       uCovariance.Data = 1.0 - uBias.Data - uVariance.Data; 
     116      uCovariance.Data = 1.0 - uBias.Data - uVariance.Data;
    121117    }
    122118  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/BakedTreeEvaluator.cs

    r699 r702  
    3030
    3131namespace HeuristicLab.GP.StructureIdentification {
     32  /// <summary>
     33  /// Evaluates FunctionTrees recursively by interpretation of the function symbols in each node.
     34  /// Not thread-safe!
     35  /// </summary>
    3236  public class BakedTreeEvaluator {
    3337    private const double EPSILON = 1.0e-7;
     38    private double estimatedValueMax;
     39    private double estimatedValueMin;
    3440
    3541    private class Instr {
     
    4349
    4450    private List<Instr> code;
     51    private Instr[] codeArr;
    4552    private int PC;
    4653    private Dataset dataset;
     
    5259    }
    5360
    54     public void ResetEvaluator(BakedFunctionTree functionTree, Dataset dataset) {
     61    public void ResetEvaluator(BakedFunctionTree functionTree, Dataset dataset, int targetVariable, int start, int end, double punishmentFactor) {
    5562      this.dataset = dataset;
     63      double maximumPunishment = punishmentFactor * dataset.GetRange(targetVariable);
     64
     65      // get the mean of the values of the target variable to determin the max and min bounds of the estimated value
     66      double targetMean = dataset.GetMean(targetVariable, start, end - 1);
     67      estimatedValueMin = targetMean - maximumPunishment;
     68      estimatedValueMax = targetMean + maximumPunishment;
     69
    5670      List<LightWeightFunction> linearRepresentation = functionTree.LinearRepresentation;
    5771      code.Clear();
     
    6175        code.Add(curInstr);
    6276      }
     77
     78      codeArr = code.ToArray<Instr>();
    6379    }
    6480
     
    88104      PC = 0;
    89105      this.sampleIndex = sampleIndex;
    90       return EvaluateBakedCode();
     106
     107      double estimated = EvaluateBakedCode();
     108      if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
     109        estimated = estimatedValueMax;
     110      } else if(estimated > estimatedValueMax) {
     111        estimated = estimatedValueMax;
     112      } else if(estimated < estimatedValueMin) {
     113        estimated = estimatedValueMin;
     114      }
     115      return estimated;
    91116    }
    92117
     
    101126
    102127    private double EvaluateBakedCode() {
    103       Instr currInstr = code[PC++];
     128      Instr currInstr = codeArr[PC++];
    104129      switch(currInstr.symbol) {
    105130        case EvaluatorSymbolTable.VARIABLE: {
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/CoefficientOfDeterminationEvaluator.cs

    r656 r702  
    3030namespace HeuristicLab.GP.StructureIdentification {
    3131  public class CoefficientOfDeterminationEvaluator : GPEvaluatorBase {
    32     private DoubleData r2;
    3332    public override string Description {
    3433      get {
     
    4342    }
    4443
    45     public override IOperation Apply(IScope scope) {
    46       r2 = GetVariableValue<DoubleData>("R2", scope, false, false);
    47       if(r2 == null) {
    48         r2 = new DoubleData();
    49         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("R2"), r2));
    50       }
    51 
    52       return base.Apply(scope);
    53     }
    54 
    55     public override void Evaluate(int start, int end) {
     44    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    5645      double errorsSquaredSum = 0.0;
    5746      double originalDeviationTotalSumOfSquares = 0.0;
     47      double targetMean = dataset.GetMean(targetVariable, start, end);
    5848      for(int sample = start; sample < end; sample++) {
    59         double estimated = GetEstimatedValue(sample);
    60         double original = GetOriginalValue(sample);
    61         SetOriginalValue(sample, estimated);
     49        double estimated = evaluator.Evaluate(sample);
     50        double original = dataset.GetValue(targetVariable, sample);
     51        if(updateTargetValues) {
     52          dataset.SetValue(targetVariable, sample, estimated);
     53        }
    6254        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    6355          double error = estimated - original;
    6456          errorsSquaredSum += error * error;
    6557
    66           double origDeviation = original - TargetMean;
     58          double origDeviation = original - targetMean;
    6759          originalDeviationTotalSumOfSquares += origDeviation * origDeviation;
    6860        }
     
    7466      if(double.IsNaN(quality) || double.IsInfinity(quality))
    7567        quality = double.MaxValue;
     68
     69      DoubleData r2 = GetVariableValue<DoubleData>("R2", scope, false, false);
     70      if(r2 == null) {
     71        r2 = new DoubleData();
     72        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("R2"), r2));
     73      }
     74
    7675      r2.Data = quality;
    7776    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/EarlyStoppingMeanSquaredErrorEvaluator.cs

    r656 r702  
    3030namespace HeuristicLab.GP.StructureIdentification {
    3131  public class EarlyStoppingMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator {
    32     private double qualityLimit;
    3332    public override string Description {
    3433      get {
     
    4443    }
    4544
    46     public override IOperation Apply(IScope scope) {
    47       qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data;
    48       return base.Apply(scope);
    49     }
     45    // 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) {
     47      double qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data;
     48      DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
     49      if(mse == null) {
     50        mse = new DoubleData();
     51        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse));
     52      }
    5053
    51     // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE
    52     public override void Evaluate(int start, int end) {
    5354      double errorsSquaredSum = 0;
    5455      int rows = end - start;
    5556      for(int sample = start; sample < end; sample++) {
    56         double estimated = GetEstimatedValue(sample);
    57         double original = GetOriginalValue(sample);
    58         SetOriginalValue(sample, estimated);
     57        double estimated = evaluator.Evaluate(sample);
     58        double original = dataset.GetValue(targetVariable, sample);
     59        if(updateTargetValues) {
     60          dataset.SetValue(targetVariable, sample, estimated);
     61        }
    5962        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    6063          double error = estimated - original;
     
    7174        errorsSquaredSum = double.MaxValue;
    7275      }
     76
    7377      mse.Data = errorsSquaredSum;
    7478    }
  • 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}
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/MeanAbsolutePercentageErrorEvaluator.cs

    r656 r702  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.Operators;
     29using HeuristicLab.DataAnalysis;
    2930
    3031namespace HeuristicLab.GP.StructureIdentification {
    3132  public class MeanAbsolutePercentageErrorEvaluator : GPEvaluatorBase {
    32     private DoubleData mape;
    3333    public override string Description {
    3434      get {
     
    4343    }
    4444
    45     public override IOperation Apply(IScope scope) {
    46       mape = GetVariableValue<DoubleData>("MAPE", scope, false, false);
    47       if(mape == null) {
    48         mape = new DoubleData();
    49         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MAPE"), mape));
    50       }
    51 
    52       return base.Apply(scope);
    53     }
    54 
    55     public override void Evaluate(int start, int end) {
     45    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    5646      double errorsSum = 0.0;
    5747      int n = 0;
    5848      for(int sample = start; sample < end; sample++) {
    59         double estimated = GetEstimatedValue(sample);
    60         double original = GetOriginalValue(sample);
    61         SetOriginalValue(sample, estimated);
    62         if(!double.IsNaN(original) && !double.IsInfinity(original) && original!=0.0) {
     49        double estimated = evaluator.Evaluate(sample);
     50        double original = dataset.GetValue(targetVariable, sample);
     51
     52        if(updateTargetValues) {
     53          dataset.SetValue(targetVariable, sample, estimated);
     54        }
     55       
     56        if(!double.IsNaN(original) && !double.IsInfinity(original) && original != 0.0) {
    6357          double percent_error = Math.Abs((estimated - original) / original);
    6458          errorsSum += percent_error;
     
    6963      if(double.IsNaN(quality) || double.IsInfinity(quality))
    7064        quality = double.MaxValue;
     65
     66      // create a variable for the MAPE
     67      DoubleData mape = GetVariableValue<DoubleData>("MAPE", scope, false, false);
     68      if(mape == null) {
     69        mape = new DoubleData();
     70        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MAPE"), mape));
     71      }
     72
    7173      mape.Data = quality;
    7274    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/MeanSquaredErrorEvaluator.cs

    r656 r702  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.Operators;
     29using HeuristicLab.DataAnalysis;
    2930
    3031namespace HeuristicLab.GP.StructureIdentification {
    3132  public class MeanSquaredErrorEvaluator : GPEvaluatorBase {
    32     protected DoubleData mse;
    3333    public override string Description {
    3434      get {
     
    4343    }
    4444
    45     public override IOperation Apply(IScope scope) {
    46       mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
    47       if(mse == null) {
    48         mse = new DoubleData();
    49         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse));
    50       }
    51 
    52       return base.Apply(scope);
    53     }
    54 
    55     public override void Evaluate(int start, int end) {
     45    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    5646      double errorsSquaredSum = 0;
    5747      for(int sample = start; sample < end; sample++) {
    58         double original = GetOriginalValue(sample);
    59         double estimated = GetEstimatedValue(sample);
    60         SetOriginalValue(sample, estimated);
     48        double original = dataset.GetValue(targetVariable, sample);
     49        double estimated = evaluator.Evaluate(sample);
     50        if(updateTargetValues) {
     51          dataset.SetValue(targetVariable, sample, estimated);
     52        }
    6153        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    6254          double error = estimated - original;
     
    6961        errorsSquaredSum = double.MaxValue;
    7062      }
     63
     64      DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
     65      if(mse == null) {
     66        mse = new DoubleData();
     67        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse));
     68      }
     69
    7170      mse.Data = errorsSquaredSum;
    7271    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/SimpleEvaluator.cs

    r656 r702  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.Operators;
     29using HeuristicLab.DataAnalysis;
    2930
    3031namespace HeuristicLab.GP.StructureIdentification {
    3132  public class SimpleEvaluator : GPEvaluatorBase {
    32     private ItemList values;
    3333    public SimpleEvaluator()
    3434      : base() {
     
    3636    }
    3737
    38     public override IOperation Apply(IScope scope) {
    39       values = GetVariableValue<ItemList>("Values", scope, false, false);
     38    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     39      ItemList values = GetVariableValue<ItemList>("Values", scope, false, false);
    4040      if(values == null) {
    4141        values = new ItemList();
     
    4747      }
    4848      values.Clear();
    49       return base.Apply(scope);
    50     }
    5149
    52     public override void Evaluate(int start, int end) {
    5350      for(int sample = start; sample < end; sample++) {
    5451        ItemList row = new ItemList();
    55         double estimated = GetEstimatedValue(sample);
    56         double original = GetOriginalValue(sample);
    57         SetOriginalValue(sample, estimated);
     52        double estimated = evaluator.Evaluate(sample);
     53        double original = dataset.GetValue(targetVariable, sample);
     54        if(updateTargetValues) {
     55          dataset.SetValue(targetVariable, sample, estimated);
     56        }
    5857        row.Add(new DoubleData(estimated));
    5958        row.Add(new DoubleData(original));
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/VarianceAccountedForEvaluator.cs

    r656 r702  
    3131namespace HeuristicLab.GP.StructureIdentification {
    3232  public class VarianceAccountedForEvaluator : GPEvaluatorBase {
    33     private DoubleData vaf;
    3433    public override string Description {
    3534      get {
     
    5453    }
    5554
    56     public override IOperation Apply(IScope scope) {
    57       vaf = GetVariableValue<DoubleData>("VAF", scope, false, false);
    58       if(vaf == null) {
    59         vaf = new DoubleData();
    60         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("VAF"), vaf));
    61       }
    62 
    63       return base.Apply(scope);
    64     }
    65 
    66     public override void Evaluate(int start, int end) {
     55    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    6756      int nSamples = end - start;
    6857      double[] errors = new double[nSamples];
    6958      double[] originalTargetVariableValues = new double[nSamples];
    7059      for(int sample = start; sample < end; sample++) {
    71         double estimated = GetEstimatedValue(sample);
    72         double original = GetOriginalValue(sample);
    73         SetOriginalValue(sample, estimated);
     60        double estimated = evaluator.Evaluate(sample);
     61        double original = dataset.GetValue(targetVariable, sample);
     62        if(updateTargetValues) {
     63          dataset.SetValue(targetVariable, sample, estimated);
     64        }
    7465        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    7566          errors[sample - start] = original - estimated;
     
    8475        quality = double.MaxValue;
    8576      }
     77      DoubleData vaf = GetVariableValue<DoubleData>("VAF", scope, false, false);
     78      if(vaf == null) {
     79        vaf = new DoubleData();
     80        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("VAF"), vaf));
     81      }
     82
    8683      vaf.Data = quality;
    8784    }
Note: See TracChangeset for help on using the changeset viewer.