Free cookie consent management tool by TermsFeed Policy Generator

Changeset 128


Ignore:
Timestamp:
04/18/08 13:53:53 (16 years ago)
Author:
gkronber
Message:
  • created abstract base class for GP evaluators
  • created a version of MSEEvaluator that implements an early stopping criterion (to be combined with offspring selection)

(ticket #29)

Location:
trunk/sources/HeuristicLab.StructureIdentification
Files:
2 added
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/CoefficientOfDeterminationEvaluator.cs

    r2 r128  
    3131
    3232namespace HeuristicLab.StructureIdentification {
    33   public class CoefficientOfDeterminationEvaluator : OperatorBase {
     33  public class CoefficientOfDeterminationEvaluator : GPEvaluatorBase {
    3434    public override string Description {
    35       get { return @"Applies 'OperatorTree' to samples 'FirstSampleIndex' - 'LastSampleIndex' (inclusive) of 'Dataset' and calculates
    36 the 'coefficient of determination' of estimated values vs. real values of 'TargetVariable'."; }
     35      get {
     36        return @"Applies 'OperatorTree' to all samples of 'Dataset' and calculates
     37the 'coefficient of determination' of estimated values vs. real values of 'TargetVariable'.";
     38      }
    3739    }
    3840
    3941    public CoefficientOfDeterminationEvaluator()
    4042      : base() {
    41       AddVariableInfo(new VariableInfo("OperatorTree", "The function tree that should be evaluated", typeof(IFunction), VariableKind.In));
    42       AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
    43       AddVariableInfo(new VariableInfo("TargetVariable", "Index of the target variable in the dataset", typeof(IntData), VariableKind.In));
    44       AddVariableInfo(new VariableInfo("FirstSampleIndex", "Index of the first row of the dataset on which the function should be evaluated", typeof(IntData), VariableKind.In));
    45       AddVariableInfo(new VariableInfo("LastSampleIndex", "Index of the last row of the dataset on which the function should be evaluated (inclusive)", typeof(IntData), VariableKind.In));
    46       AddVariableInfo(new VariableInfo("PunishmentFactor", "Punishment factor for invalid estimations", typeof(DoubleData), VariableKind.In));
    47       AddVariableInfo(new VariableInfo("UseEstimatedTargetValues", "When the function tree contains the target variable this variable determines " +
    48       "if we should use the estimated or the original values of the target variable in the evaluation", typeof(BoolData), VariableKind.In));
    49       AddVariableInfo(new VariableInfo("Quality", "The coefficient of determination of the model", typeof(DoubleData), VariableKind.New));
    50 
    5143    }
    5244
    53 
    54     private double[] savedTargetVariable = new double[1];
    55     public override IOperation Apply(IScope scope) {
    56       int firstSampleIndex = GetVariableValue<IntData>("FirstSampleIndex", scope, true).Data;
    57       int lastSampleIndex = GetVariableValue<IntData>("LastSampleIndex", scope, true).Data;
    58 
    59       if(lastSampleIndex < firstSampleIndex) {
    60         throw new InvalidProgramException();
    61       }
    62 
    63       IFunction function = GetVariableValue<IFunction>("OperatorTree", scope, true);
    64 
    65       Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    66 
    67       int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
    68       bool useEstimatedTargetValues = GetVariableValue<BoolData>("UseEstimatedTargetValues", scope, true).Data;
    69       double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
    70 
    71       if(useEstimatedTargetValues && savedTargetVariable.Length != lastSampleIndex - firstSampleIndex + 1) {
    72         savedTargetVariable = new double[lastSampleIndex - firstSampleIndex + 1];
    73       }
    74 
    75       double maximumPunishment = punishmentFactor * dataset.GetRange(targetVariable, firstSampleIndex, lastSampleIndex);
    76 
     45    public override double Evaluate(IScope scope, IFunction function, int targetVariable, Dataset dataset) {
    7746      double errorsSquaredSum = 0.0;
    78       double originalsSum = 0.0;
    79       double targetMean = dataset.GetMean(targetVariable, firstSampleIndex, lastSampleIndex);
    80 
    81       for(int sample = firstSampleIndex; sample <= lastSampleIndex; sample++) {
     47      double originalDeviationTotalSumOfSquares = 0.0;
     48      double targetMean = dataset.GetMean(targetVariable);
     49      for(int sample = 0; sample < dataset.Rows; sample++) {
    8250        double estimated = function.Evaluate(dataset, sample);
    8351        double original = dataset.GetValue(sample, targetVariable);
    84 
    85         if(useEstimatedTargetValues) {
    86           savedTargetVariable[sample - firstSampleIndex] = original;
    87           dataset.SetValue(sample, targetVariable, estimated);
    88         }
    89 
    9052        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    9153          if(double.IsNaN(estimated) || double.IsInfinity(estimated))
     
    9860          double error = estimated - original;
    9961          errorsSquaredSum += error * error;
    100           originalsSum += original;
     62
     63          double origDeviation = original - targetMean;
     64          originalDeviationTotalSumOfSquares += origDeviation * origDeviation;
    10165        }
    10266      }
    103 
    104       double originalsMean = originalsSum / (lastSampleIndex - firstSampleIndex +1);
    105      
    106       double originalTotalSumOfSquares = 0.0;
    107 
    108       for(int sample=0; sample <savedTargetVariable.Length; sample++) {
    109         double original = savedTargetVariable[sample];
    110 
    111         if(!double.IsInfinity(original) && !double.IsNaN(original)) {
    112           original = original - originalsMean;
    113           originalTotalSumOfSquares += original * original;
    114         }
    115       }
    116 
    117       double quality = 1 - errorsSquaredSum / originalTotalSumOfSquares;
    118 
    119       if(quality > 1) {
     67      double quality = 1 - errorsSquaredSum / originalDeviationTotalSumOfSquares;
     68      if(quality > 1)
    12069        throw new InvalidProgramException();
    121       }
    122 
    123       if(double.IsNaN(quality) || double.IsInfinity(quality)) {
     70      if(double.IsNaN(quality) || double.IsInfinity(quality))
    12471        quality = double.MaxValue;
    125       }
    126 
    127       if(useEstimatedTargetValues) {
    128         // restore original values of the target variable
    129         for(int sample = firstSampleIndex; sample <= lastSampleIndex; sample++) {
    130           dataset.SetValue(sample, targetVariable, savedTargetVariable[sample - firstSampleIndex]);
    131         }
    132       }
    133 
    134       scope.AddVariable(new HeuristicLab.Core.Variable("Quality", new DoubleData(quality)));
    135       return null;
     72      return quality;
    13673    }
    13774  }
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MeanSquaredErrorEvaluator.cs

    r2 r128  
    3131
    3232namespace HeuristicLab.StructureIdentification {
    33   public class MeanSquaredErrorEvaluator : OperatorBase {
     33  public class MeanSquaredErrorEvaluator : GPEvaluatorBase {
    3434    public override string Description {
    35       get { return @"Evaluates 'OperatorTree' for samples 'FirstSampleIndex' - 'LastSampleIndex' (inclusive) and calculates the mean-squared-error
    36 for the estimated values vs. the real values of 'TargetVariable'."; }
     35      get {
     36        return @"Evaluates 'OperatorTree' for all samples of 'DataSet' and calculates the mean-squared-error
     37for the estimated values vs. the real values of 'TargetVariable'.";
     38      }
    3739    }
    3840
    3941    public MeanSquaredErrorEvaluator()
    4042      : base() {
    41       AddVariableInfo(new VariableInfo("OperatorTree", "The function tree that should be evaluated", typeof(IFunction), VariableKind.In));
    42       AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
    43       AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In));
    44       AddVariableInfo(new VariableInfo("FirstSampleIndex", "Index of the first row of the dataset on which the function should be evaluated", typeof(IntData), VariableKind.In));
    45       AddVariableInfo(new VariableInfo("LastSampleIndex", "Index of the last row of the dataset on which the function should be evaluated (inclusive)", typeof(IntData), VariableKind.In));
    46       AddVariableInfo(new VariableInfo("PunishmentFactor", "Punishment factor for invalid estimations", typeof(DoubleData), VariableKind.In));
    47       AddVariableInfo(new VariableInfo("UseEstimatedTargetValues", "When the function tree contains the target variable this variable determines " +
    48       "if we should use the estimated or the original values of the target variable in the evaluation", typeof(BoolData), VariableKind.In));
    49       AddVariableInfo(new VariableInfo("Quality", "The mean squared error of the model", typeof(DoubleData), VariableKind.New));
    5043    }
    5144
    52     private double[] savedTargetVariable = new double[0];
    53 
    54     public override IOperation Apply(IScope scope) {
    55 
    56       int firstSampleIndex = GetVariableValue<IntData>("FirstSampleIndex", scope, true).Data;
    57       int lastSampleIndex = GetVariableValue<IntData>("LastSampleIndex", scope, true).Data;
    58 
    59       if(firstSampleIndex >= lastSampleIndex) {
    60         throw new InvalidProgramException();
    61       }
    62 
    63 
    64       int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
    65       Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    66 
    67       IFunction function = GetVariableValue<IFunction>("OperatorTree", scope, true);
    68 
     45    public override double Evaluate(IScope scope, IFunction function, int targetVariable, Dataset dataset) {
    6946      double errorsSquaredSum = 0;
    70 
    71       double maximumPunishment = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data * dataset.GetRange(targetVariable, firstSampleIndex, lastSampleIndex);
    72       double targetMean = dataset.GetMean(targetVariable, firstSampleIndex, lastSampleIndex);
    73       bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValues", scope, true).Data;
    74 
    75       if(useEstimatedValues && savedTargetVariable.Length != lastSampleIndex - firstSampleIndex + 1) {
    76         savedTargetVariable = new double[lastSampleIndex - firstSampleIndex + 1];
    77       }
    78       for(int sample = firstSampleIndex; sample <= lastSampleIndex; sample++) {
    79 
     47      double targetMean = dataset.GetMean(targetVariable);
     48      for(int sample = 0; sample < dataset.Rows; sample++) {
    8049        double estimated = function.Evaluate(dataset, sample);
    8150        double original = dataset.GetValue(sample, targetVariable);
    82 
    83         if(useEstimatedValues) {
    84           savedTargetVariable[sample - firstSampleIndex] = original;
    85           dataset.SetValue(sample, targetVariable, estimated);
    86         }
    87 
    8851        if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
    8952          estimated = targetMean + maximumPunishment;
     
    9356          estimated = targetMean - maximumPunishment;
    9457        }
    95 
    9658        double error = estimated - original;
    9759        errorsSquaredSum += error * error;
    9860      }
    99 
    100       errorsSquaredSum /= (lastSampleIndex - firstSampleIndex);
     61      errorsSquaredSum /= dataset.Rows;
    10162      if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) {
    10263        errorsSquaredSum = double.MaxValue;
    10364      }
    104 
    105       if(useEstimatedValues) {
    106         // restore original values of the target variable
    107         for(int sample = firstSampleIndex; sample <= lastSampleIndex; sample++) {
    108           dataset.SetValue(sample, targetVariable, savedTargetVariable[sample - firstSampleIndex]);
    109         }
    110       }
    111 
    112       scope.AddVariable(new HeuristicLab.Core.Variable("Quality", new DoubleData(errorsSquaredSum)));
    113       return null;
     65      return errorsSquaredSum;
    11466    }
    11567  }
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/VarianceAccountedForEvaluator.cs

    r2 r128  
    3131
    3232namespace HeuristicLab.StructureIdentification {
    33   public class VarianceAccountedForEvaluator : OperatorBase {
     33  public class VarianceAccountedForEvaluator : GPEvaluatorBase {
    3434    public override string Description {
    35       get { return @"Evaluates 'OperatorTree' for samples 'FirstSampleIndex' - 'LastSampleIndex' (inclusive) and calculates
     35      get {
     36        return @"Evaluates 'OperatorTree' for all samples of 'DataSet' and calculates
    3637the variance-accounted-for quality measure for the estimated values vs. the real values of 'TargetVariable'.
    3738
    3839The Variance Accounted For (VAF) function is computed as
    3940VAF(y,y') = ( 1 - var(y-y')/var(y) )
    40 where y' denotes the predicted / modelled values for y and var(x) the variance of a signal x."; }
     41where y' denotes the predicted / modelled values for y and var(x) the variance of a signal x.";
     42      }
    4143    }
    4244
     
    4850    public VarianceAccountedForEvaluator()
    4951      : base() {
    50       AddVariableInfo(new VariableInfo("OperatorTree", "The function tree that should be evaluated", typeof(IFunction), VariableKind.In));
    51       AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
    52       AddVariableInfo(new VariableInfo("TargetVariable", "Index of the target variable in the dataset", typeof(IntData), VariableKind.In));
    53       AddVariableInfo(new VariableInfo("FirstSampleIndex", "Index of the first row of the dataset on which the function should be evaluated", typeof(IntData), VariableKind.In));
    54       AddVariableInfo(new VariableInfo("LastSampleIndex", "Index of the last row of the dataset on which the function should be evaluated (inclusive)", typeof(IntData), VariableKind.In));
    55       AddVariableInfo(new VariableInfo("PunishmentFactor", "Punishment factor for invalid estimations", typeof(DoubleData), VariableKind.In));
    56       AddVariableInfo(new VariableInfo("UseEstimatedTargetValues", "When the function tree contains the target variable this variable determines " +
    57       "if we should use the estimated or the original values of the target variable in the evaluation", typeof(BoolData), VariableKind.In));
    58       AddVariableInfo(new VariableInfo("Quality", "Variance accounted for quality of the model", typeof(DoubleData), VariableKind.New));
    59 
    6052    }
    6153
    6254
    63     private double[] originalTargetVariableValues = new double[1];
    64     private double[] errors = new double[1];
    65 
    66     public override IOperation Apply(IScope scope) {
    67 
    68       int firstSampleIndex = GetVariableValue<IntData>("FirstSampleIndex", scope, true).Data;
    69       int lastSampleIndex = GetVariableValue<IntData>("LastSampleIndex", scope, true).Data;
    70 
    71       if(lastSampleIndex < firstSampleIndex) {
    72         throw new InvalidProgramException();
    73       }
    74 
    75       IFunction function = GetVariableValue<IFunction>("OperatorTree", scope, true);
    76 
    77       Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    78 
    79       int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
    80       bool useEstimatedTargetValues = GetVariableValue<BoolData>("UseEstimatedTargetValues", scope, true).Data;
    81       double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
    82 
    83       if(originalTargetVariableValues.Length != lastSampleIndex - firstSampleIndex + 1) {
    84         originalTargetVariableValues = new double[lastSampleIndex - firstSampleIndex + 1];
    85         errors = new double[lastSampleIndex - firstSampleIndex + 1];
    86       }
    87 
    88       double maximumPunishment = punishmentFactor * dataset.GetRange(targetVariable, firstSampleIndex, lastSampleIndex);
    89 
    90       double targetMean = dataset.GetMean(targetVariable, firstSampleIndex, lastSampleIndex);
    91 
    92       for(int sample = firstSampleIndex; sample <= lastSampleIndex; sample++) {
    93 
     55    public override double Evaluate(IScope scope, IFunction function, int targetVariable, Dataset dataset) {
     56      double[] errors = new double[dataset.Rows];
     57      double[] originalTargetVariableValues = new double[dataset.Rows];
     58      double targetMean = dataset.GetMean(targetVariable);
     59      for(int sample = 0; sample < dataset.Rows; sample++) {
    9460        double estimated = function.Evaluate(dataset, sample);
    95         double original =  dataset.GetValue(sample, targetVariable);
    96 
     61        double original = dataset.GetValue(sample, targetVariable);
    9762        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    9863          if(double.IsNaN(estimated) || double.IsInfinity(estimated))
     
    10469        }
    10570
    106         errors[sample-firstSampleIndex] = original - estimated;
    107         originalTargetVariableValues[sample-firstSampleIndex] = original;
    108         if(useEstimatedTargetValues) {
    109           dataset.SetValue(sample, targetVariable, estimated);
    110         }
     71        errors[sample] = original - estimated;
     72        originalTargetVariableValues[sample] = original;
    11173      }
    11274
     
    11880        quality = double.MaxValue;
    11981      }
    120 
    121       if(useEstimatedTargetValues) {
    122         // restore original values of the target variable
    123         for(int sample = firstSampleIndex; sample <= lastSampleIndex; sample++) {
    124           dataset.SetValue(sample, targetVariable, originalTargetVariableValues[sample - firstSampleIndex]);
    125         }
    126       }
    127 
    128       scope.AddVariable(new HeuristicLab.Core.Variable("Quality", new DoubleData(quality)));
    129       return null;
     82      return quality;
    13083    }
    13184  }
  • trunk/sources/HeuristicLab.StructureIdentification/HeuristicLab.StructureIdentification.csproj

    r89 r128  
    4949  <ItemGroup>
    5050    <Compile Include="Evaluation\CoefficientOfDeterminationEvaluator.cs" />
    51     <Compile Include="Evaluation\FunctionEvaluator.cs">
    52       <SubType>Code</SubType>
    53     </Compile>
     51    <Compile Include="Evaluation\GPEvaluatorBase.cs" />
     52    <Compile Include="Evaluation\EarlyStoppingMeanSquaredErrorEvaluator.cs" />
    5453    <Compile Include="Evaluation\MeanSquaredErrorEvaluator.cs" />
    5554    <Compile Include="Evaluation\VarianceAccountedForEvaluator.cs" />
Note: See TracChangeset for help on using the changeset viewer.