Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1894


Ignore:
Timestamp:
05/25/09 18:43:26 (16 years ago)
Author:
gkronber
Message:

Fixed #643 (Simple evaluation operators in HL.Modeling and GP specific evaluation operators in HL.GP.StructureIdentification should be unified).

Location:
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3
Files:
1 added
6 edited

Legend:

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

    r1891 r1894  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.Operators;
     29using HeuristicLab.Modeling;
    2930
    3031namespace HeuristicLab.GP.StructureIdentification {
    31   public class CoefficientOfDeterminationEvaluator : GPEvaluatorBase {
     32  public class CoefficientOfDeterminationEvaluator : SimpleGPEvaluatorBase {
     33
     34    public override string OutputVariableName {
     35      get {
     36        return "R2";
     37      }
     38    }
     39   
    3240    public override string Description {
    3341      get {
     
    3745    }
    3846
    39     public CoefficientOfDeterminationEvaluator()
    40       : base() {
    41       AddVariableInfo(new VariableInfo("R2", "The coefficient of determination of the model", typeof(DoubleData), VariableKind.New));
    42     }
     47    public override double Evaluate(double[,] values) {
    4348
    44     public override void Evaluate(IScope scope, ITreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    45       double errorsSquaredSum = 0.0;
    46       double originalDeviationTotalSumOfSquares = 0.0;
    47       double targetMean = dataset.GetMean(targetVariable, start, end);
    48 
    49       double originalSum = 0.0;
    50       int n = 0;
    51       for (int sample = start; sample < end; sample++) {
    52         double estimated = evaluator.Evaluate(sample);
    53         double original = dataset.GetValue(sample, targetVariable);
    54         if (updateTargetValues) {
    55           dataset.SetValue(sample, targetVariable, estimated);
    56         }
    57         if (!double.IsNaN(original) && !double.IsInfinity(original)) {
    58           double error = estimated - original;
    59           errorsSquaredSum += error * error;
    60 
    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;
    73         }
    74       }
    75 
    76       double quality = 1 - errorsSquaredSum / originalDeviationTotalSumOfSquares;
    77       if (quality > 1)
    78         throw new InvalidProgramException();
     49      double quality = SimpleR2Evaluator.Calculate(values);
    7950      if (double.IsNaN(quality) || double.IsInfinity(quality))
    8051        quality = double.MaxValue;
    8152
    82       DoubleData r2 = GetVariableValue<DoubleData>("R2", scope, false, false);
    83       if (r2 == null) {
    84         r2 = new DoubleData();
    85         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("R2"), r2));
    86       }
    87 
    88       r2.Data = quality;
     53      return quality;
    8954    }
    9055  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanAbsolutePercentageErrorEvaluator.cs

    r1891 r1894  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.Operators;
     29using HeuristicLab.Modeling;
    2930using HeuristicLab.DataAnalysis;
    3031
    3132namespace HeuristicLab.GP.StructureIdentification {
    32   public class MeanAbsolutePercentageErrorEvaluator : GPEvaluatorBase {
     33  public class MeanAbsolutePercentageErrorEvaluator : SimpleGPEvaluatorBase {
     34    public override string OutputVariableName {
     35      get {
     36        return "MAPE";
     37      }
     38    }
     39
    3340    public override string Description {
    3441      get {
     
    3845    }
    3946
    40     public MeanAbsolutePercentageErrorEvaluator()
    41       : base() {
    42       AddVariableInfo(new VariableInfo("MAPE", "The mean absolute percentage error of the model", typeof(DoubleData), VariableKind.New));
    43     }
    44 
    45     public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    46       double errorsSum = 0.0;
    47       int n = 0;
    48       for (int sample = start; sample < end; sample++) {
    49         double estimated = evaluator.Evaluate(sample);
    50         double original = dataset.GetValue(sample, targetVariable);
    51 
    52         if (updateTargetValues) {
    53           dataset.SetValue(sample, targetVariable, estimated);
    54         }
    55 
    56         if (!double.IsNaN(original) && !double.IsInfinity(original) && original != 0.0) {
    57           double percent_error = Math.Abs((estimated - original) / original);
    58           errorsSum += percent_error;
    59           n++;
    60         }
    61       }
    62       double quality = errorsSum / n;
     47    public override double Evaluate(double[,] values) {
     48      double quality = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(values);
    6349      if (double.IsNaN(quality) || double.IsInfinity(quality))
    6450        quality = double.MaxValue;
    6551
    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 
    73       mape.Data = quality;
     52      return quality;
    7453    }
    7554  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanAbsolutePercentageOfRangeErrorEvaluator.cs

    r1891 r1894  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.Operators;
     29using HeuristicLab.Modeling;
    2930using HeuristicLab.DataAnalysis;
    3031
    3132namespace HeuristicLab.GP.StructureIdentification {
    32   public class MeanAbsolutePercentageOfRangeErrorEvaluator : GPEvaluatorBase {
     33  public class MeanAbsolutePercentageOfRangeErrorEvaluator : SimpleGPEvaluatorBase {
     34    public override string OutputVariableName {
     35      get {
     36        return "MAPRE";
     37      }
     38    }
    3339    public override string Description {
    3440      get {
     
    3844    }
    3945
    40     public MeanAbsolutePercentageOfRangeErrorEvaluator()
    41       : base() {
    42       AddVariableInfo(new VariableInfo("MAPRE", "The mean absolute percentage range error of the model", typeof(DoubleData), VariableKind.New));
    43     }
    44 
    45     public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    46       double errorsSum = 0.0;
    47       int n = 0;
    48       double range = dataset.GetRange(targetVariable, start, end);
    49       for (int sample = start; sample < end; sample++) {
    50         double estimated = evaluator.Evaluate(sample);
    51         double original = dataset.GetValue(sample, targetVariable);
    52 
    53         if (updateTargetValues) {
    54           dataset.SetValue(sample, targetVariable, estimated);
    55         }
    56 
    57         if (!double.IsNaN(original) && !double.IsInfinity(original) && original != 0.0) {
    58           double percent_error = Math.Abs((estimated - original) / range);
    59           errorsSum += percent_error;
    60           n++;
    61         }
    62       }
    63       double quality = errorsSum / n;
     46    public override double Evaluate(double[,] values) {
     47      double quality = SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.Calculate(values);
    6448      if (double.IsNaN(quality) || double.IsInfinity(quality))
    6549        quality = double.MaxValue;
    6650
    67       // create a variable for the MAPRE
    68       DoubleData mapre = GetVariableValue<DoubleData>("MAPRE", scope, false, false);
    69       if (mapre == null) {
    70         mapre = new DoubleData();
    71         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MAPRE"), mapre));
    72       }
    73 
    74       mapre.Data = quality;
     51      return quality;
    7552    }
    7653  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanSquaredErrorEvaluator.cs

    r1891 r1894  
    2828using HeuristicLab.Operators;
    2929using HeuristicLab.DataAnalysis;
     30using HeuristicLab.Modeling;
    3031
    3132namespace HeuristicLab.GP.StructureIdentification {
    32   public class MeanSquaredErrorEvaluator : GPEvaluatorBase {
     33  public class MeanSquaredErrorEvaluator : SimpleGPEvaluatorBase {
     34    public override string OutputVariableName {
     35      get {
     36        return "MSE";
     37      }
     38    }
    3339    public override string Description {
    3440      get {
     
    3844    }
    3945
    40     public MeanSquaredErrorEvaluator()
    41       : base() {
    42       AddVariableInfo(new VariableInfo("MSE", "The mean squared error of the model", typeof(DoubleData), VariableKind.New));
    43     }
     46    public override double Evaluate(double[,] values) {
     47      double quality = SimpleMSEEvaluator.Calculate(values);
    4448
    45     public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    46       double errorsSquaredSum = 0;
    47       int n = 0;
    48       for (int sample = start; sample < end; sample++) {
    49         double original = dataset.GetValue(sample, targetVariable);
    50         double estimated = evaluator.Evaluate(sample);
    51         if (updateTargetValues) {
    52           dataset.SetValue(sample, targetVariable, estimated);
    53         }
    54         if (!double.IsNaN(original) && !double.IsInfinity(original)) {
    55           double error = estimated - original;
    56           errorsSquaredSum += error * error;
    57           n++;
    58         }
     49      if (double.IsNaN(quality) || double.IsInfinity(quality)) {
     50        quality = double.MaxValue;
    5951      }
    6052
    61       errorsSquaredSum /= n;
    62       if (double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) {
    63         errorsSquaredSum = double.MaxValue;
    64       }
    65 
    66       DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
    67       if (mse == null) {
    68         mse = new DoubleData();
    69         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse));
    70       }
    71 
    72       mse.Data = errorsSquaredSum;
     53      return quality;
    7354    }
    7455  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VarianceAccountedForEvaluator.cs

    r1891 r1894  
    2828using HeuristicLab.Operators;
    2929using HeuristicLab.DataAnalysis;
     30using HeuristicLab.Modeling;
    3031
    3132namespace HeuristicLab.GP.StructureIdentification {
    32   public class VarianceAccountedForEvaluator : GPEvaluatorBase {
     33  /// <summary>
     34  /// The Variance Accounted For (VAF) function calculates is computed as
     35  /// VAF(y,y') = ( 1 - var(y-y')/var(y) )
     36  /// where y' denotes the predicted / modelled values for y and var(x) the variance of a signal x.
     37  /// </summary>
     38  public class VarianceAccountedForEvaluator : SimpleGPEvaluatorBase {
     39    public override string OutputVariableName {
     40      get {
     41        return "VAF";
     42      }
     43    }
    3344    public override string Description {
    3445      get {
     
    4253    }
    4354
    44     /// <summary>
    45     /// The Variance Accounted For (VAF) function calculates is computed as
    46     /// VAF(y,y') = ( 1 - var(y-y')/var(y) )
    47     /// where y' denotes the predicted / modelled values for y and var(x) the variance of a signal x.
    48     /// </summary>
    49     public VarianceAccountedForEvaluator()
    50       : base() {
    51       AddVariableInfo(new VariableInfo("VAF", "The variance-accounted-for quality of the model", typeof(DoubleData), VariableKind.New));
    52 
    53     }
    54 
    55     public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    56       int nSamples = end - start;
    57       double[] errors = new double[nSamples];
    58       double[] originalTargetVariableValues = new double[nSamples];
    59       for (int sample = start; sample < end; sample++) {
    60         double estimated = evaluator.Evaluate(sample);
    61         double original = dataset.GetValue(sample, targetVariable);
    62         if (updateTargetValues) {
    63           dataset.SetValue(sample, targetVariable, estimated);
    64         }
    65         if (!double.IsNaN(original) && !double.IsInfinity(original)) {
    66           errors[sample - start] = original - estimated;
    67           originalTargetVariableValues[sample - start] = original;
    68         } else {
    69           errors[sample - start] = double.NaN;
    70           originalTargetVariableValues[sample - start] = double.NaN;
    71         }
    72       }
    73       double errorsVariance = Statistics.Variance(errors);
    74       double originalsVariance = Statistics.Variance(originalTargetVariableValues);
    75       double quality = 1 - errorsVariance / originalsVariance;
     55    public override double Evaluate(double[,] values) {
     56      double quality = SimpleVarianceAccountedForEvaluator.Calculate(values);
    7657
    7758      if (double.IsNaN(quality) || double.IsInfinity(quality)) {
    7859        quality = double.MaxValue;
    7960      }
    80       DoubleData vaf = GetVariableValue<DoubleData>("VAF", scope, false, false);
    81       if (vaf == null) {
    82         vaf = new DoubleData();
    83         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("VAF"), vaf));
    84       }
    85 
    86       vaf.Data = quality;
     61      return quality;
    8762    }
    8863  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/HeuristicLab.GP.StructureIdentification-3.3.csproj

    r1856 r1894  
    8989    <Compile Include="Constant.cs" />
    9090    <Compile Include="AlgorithmBase.cs" />
     91    <Compile Include="Evaluators\SimpleGPEvaluatorBase.cs" />
    9192    <Compile Include="TreeEvaluatorBase.cs" />
    9293    <Compile Include="HL2TreeEvaluator.cs" />
Note: See TracChangeset for help on using the changeset viewer.