- Timestamp:
- 08/10/08 11:08:07 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.StructureIdentification/Evaluation
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/AccuracyEvaluator.cs
r476 r479 33 33 public class AccuracyEvaluator : GPEvaluatorBase { 34 34 private const double EPSILON = 1.0E-6; 35 private double[] classesArr; 36 private double[] thresholds; 35 37 public override string Description { 36 38 get { … … 44 46 } 45 47 46 private double[] original = new double[1]; 47 private double[] estimated = new double[1]; 48 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) { 49 int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data; 50 int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data; 51 int nSamples = trainingEnd-trainingStart; 48 public override IOperation Apply(IScope scope) { 52 49 ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true); 53 double[]classesArr = new double[classes.Count];54 for(int i =0;i<classesArr.Length;i++) classesArr[i] = classes[i].Data;50 classesArr = new double[classes.Count]; 51 for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data; 55 52 Array.Sort(classesArr); 56 double[]thresholds = new double[classes.Count - 1];57 for(int i =0;i<classesArr.Length-1;i++) {58 thresholds[i] = (classesArr[i] +classesArr[i+1]) / 2.0;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; 59 56 } 60 57 58 return base.Apply(scope); 59 } 60 61 public override double Evaluate(int start, int end) { 62 int nSamples = end-start; 61 63 int nCorrect = 0; 62 for(int sample = trainingStart; sample < trainingEnd; sample++) {63 double est = evaluator.Evaluate(sample);64 double origClass = dataset.GetValue(sample, targetVariable);64 for(int sample = start; sample < end; sample++) { 65 double est = GetEstimatedValue(sample); 66 double origClass = GetOriginalValue(sample); 65 67 double estClass = double.NaN; 66 68 // if estimation is lower than the smallest threshold value -> estimated class is the lower class … … 79 81 if(Math.Abs(estClass - origClass) < EPSILON) nCorrect++; 80 82 } 81 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * nSamples;82 83 return nCorrect / (double)nSamples; 83 84 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/ClassificationMeanSquaredErrorEvaluator.cs
r478 r479 32 32 namespace HeuristicLab.StructureIdentification { 33 33 public class ClassificationMeanSquaredErrorEvaluator : GPEvaluatorBase { 34 protected double[] backupValues;35 34 private const double EPSILON = 1.0E-6; 35 private double[] classesArr; 36 36 public override string Description { 37 37 get { … … 46 46 } 47 47 48 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) { 49 int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data; 50 int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data; 48 public override IOperation Apply(IScope scope) { 51 49 ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true); 52 double[]classesArr = new double[classes.Count];50 classesArr = new double[classes.Count]; 53 51 for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data; 54 52 Array.Sort(classesArr); 53 return base.Apply(scope); 54 } 55 56 public override double Evaluate(int start, int end) { 55 57 56 58 double errorsSquaredSum = 0; 57 double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd); 58 for(int sample = trainingStart; sample < trainingEnd; sample++) { 59 double estimated = evaluator.Evaluate(sample); 60 double original = dataset.GetValue(sample, targetVariable); 61 if(double.IsNaN(estimated) || double.IsInfinity(estimated)) { 62 estimated = targetMean + maximumPunishment; 63 } else if(estimated > targetMean + maximumPunishment) { 64 estimated = targetMean + maximumPunishment; 65 } else if(estimated < targetMean - maximumPunishment) { 66 estimated = targetMean - maximumPunishment; 67 } 68 double error = estimated - original; 69 // between classes use squared error 70 // on the lower end and upper end only add linear error if the absolute error is larger than 1 71 // the error>1.0 constraint is needed for balance because in the interval ]-1, 1[ the squared error is smaller than the absolute error 72 if(error < -1.0 && IsEqual(original, classesArr[0]) && estimated < classesArr[0] || 73 error > 1.0 && IsEqual(original, classesArr[classesArr.Length - 1]) && estimated > classesArr[classesArr.Length - 1]) { 74 errorsSquaredSum += Math.Abs(error); // only add linear error below the smallest class or above the largest class 75 } else { 76 errorsSquaredSum += error * error; 59 for(int sample = start; sample < end; sample++) { 60 double estimated = GetEstimatedValue(sample); 61 double original = GetOriginalValue(sample); 62 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 63 double error = estimated - original; 64 // between classes use squared error 65 // on the lower end and upper end only add linear error if the absolute error is larger than 1 66 // the error>1.0 constraint is needed for balance because in the interval ]-1, 1[ the squared error is smaller than the absolute error 67 if(error < -1.0 && IsEqual(original, classesArr[0]) && estimated < classesArr[0] || 68 error > 1.0 && IsEqual(original, classesArr[classesArr.Length - 1]) && estimated > classesArr[classesArr.Length - 1]) { 69 errorsSquaredSum += Math.Abs(error); // only add linear error below the smallest class or above the largest class 70 } else { 71 errorsSquaredSum += error * error; 72 } 77 73 } 78 74 } 79 75 80 errorsSquaredSum /= ( trainingEnd - trainingStart);76 errorsSquaredSum /= (end - start); 81 77 if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) { 82 78 errorsSquaredSum = double.MaxValue; 83 79 } 84 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd - trainingStart);85 80 return errorsSquaredSum; 86 81 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/CoefficientOfDeterminationEvaluator.cs
r400 r479 43 43 } 44 44 45 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) { 46 int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data; 47 int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data; 45 public override double Evaluate(int start, int end) { 48 46 double errorsSquaredSum = 0.0; 49 47 double originalDeviationTotalSumOfSquares = 0.0; 50 double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd); 51 for(int sample = trainingStart; sample < trainingEnd; sample++) { 52 double estimated = evaluator.Evaluate(sample); 53 double original = dataset.GetValue(sample, targetVariable); 48 for(int sample = start; sample < end; sample++) { 49 double estimated = GetEstimatedValue(sample); 50 double original = GetOriginalValue(sample); 54 51 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 55 if(double.IsNaN(estimated) || double.IsInfinity(estimated))56 estimated = targetMean + maximumPunishment;57 else if(estimated > (targetMean + maximumPunishment))58 estimated = targetMean + maximumPunishment;59 else if(estimated < (targetMean - maximumPunishment))60 estimated = targetMean - maximumPunishment;61 62 52 double error = estimated - original; 63 53 errorsSquaredSum += error * error; 64 54 65 double origDeviation = original - targetMean;55 double origDeviation = original - TargetMean; 66 56 originalDeviationTotalSumOfSquares += origDeviation * origDeviation; 67 57 } 68 58 } 69 70 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd - trainingStart);71 59 72 60 double quality = 1 - errorsSquaredSum / originalDeviationTotalSumOfSquares; -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/EarlyStoppingMeanSquaredErrorEvaluator.cs
r396 r479 32 32 namespace HeuristicLab.StructureIdentification { 33 33 public class EarlyStoppingMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator { 34 private double qualityLimit; 34 35 public override string Description { 35 36 get { … … 45 46 } 46 47 48 public override IOperation Apply(IScope scope) { 49 qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data; 50 return base.Apply(scope); 51 } 52 47 53 // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE 48 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) { 49 double qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data; 50 bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, false).Data; 51 int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data; 52 int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data; 53 int rows = trainingEnd-trainingStart; 54 if(useEstimatedValues && backupValues == null) { 55 backupValues = new double[rows]; 56 for(int i = trainingStart; i < trainingEnd; i++) { 57 backupValues[i-trainingStart] = dataset.GetValue(i, targetVariable); 54 public override double Evaluate(int start, int end) { 55 double errorsSquaredSum = 0; 56 int rows = end - start; 57 for(int sample = start; sample < end; sample++) { 58 double estimated = GetEstimatedValue(sample); 59 double original = GetOriginalValue(sample); 60 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 61 double error = estimated - original; 62 errorsSquaredSum += error * error; 63 } 64 // check the limit and stop as soon as we hit the limit 65 if(errorsSquaredSum / rows >= qualityLimit) { 66 return errorsSquaredSum / (sample - start + 1); // return estimated MSE (when the remaining errors are on average the same) 58 67 } 59 68 } 60 double errorsSquaredSum = 0;61 double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);62 for(int sample = trainingStart; sample < trainingEnd; sample++) {63 double estimated = evaluator.Evaluate(sample);64 double original = dataset.GetValue(sample, targetVariable);65 if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {66 estimated = targetMean + maximumPunishment;67 } else if(estimated > targetMean + maximumPunishment) {68 estimated = targetMean + maximumPunishment;69 } else if(estimated < targetMean - maximumPunishment) {70 estimated = targetMean - maximumPunishment;71 }72 73 double error = estimated - original;74 errorsSquaredSum += error * error;75 76 // check the limit and stop as soon as we hit the limit77 if(errorsSquaredSum / rows >= qualityLimit) {78 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (sample-trainingStart + 1);79 if(useEstimatedValues) RestoreDataset(dataset, targetVariable, trainingStart, sample);80 return errorsSquaredSum / (sample-trainingStart + 1); // return estimated MSE (when the remaining errors are on average the same)81 }82 if(useEstimatedValues) {83 dataset.SetValue(sample, targetVariable, estimated);84 }85 }86 if(useEstimatedValues) RestoreDataset(dataset, targetVariable, trainingStart, trainingEnd);87 69 errorsSquaredSum /= rows; 88 70 if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) { 89 71 errorsSquaredSum = double.MaxValue; 90 72 } 91 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * rows;92 73 return errorsSquaredSum; 93 }94 95 private void RestoreDataset(Dataset dataset, int targetVariable, int from, int to) {96 for(int i = from; i < to; i++) {97 dataset.SetValue(i, targetVariable, backupValues[i-from]);98 }99 74 } 100 75 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/GPEvaluatorBase.cs
r396 r479 32 32 namespace HeuristicLab.StructureIdentification { 33 33 public abstract class GPEvaluatorBase : OperatorBase { 34 protected double maximumPunishment; 35 protected int treeSize; 36 protected double totalEvaluatedNodes; 37 protected IEvaluator evaluator; 34 private IEvaluator evaluator; 35 private int targetVariable; 36 private int trainingStart; 37 private int trainingEnd; 38 private bool useEstimatedValues; 39 private double[] backupValues; 40 private int evaluatedSamples; 41 private double estimatedValueMax; 42 private double estimatedValueMin; 43 private int treeSize; 44 private double totalEvaluatedNodes; 45 private Dataset dataset; 46 private double targetMean; 47 protected double TargetMean { get { return targetMean; } } 38 48 39 49 public GPEvaluatorBase() … … 47 57 AddVariableInfo(new VariableInfo("TrainingSamplesStart", "Start index of training samples in dataset", typeof(IntData), VariableKind.In)); 48 58 AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "End index of training samples in dataset", typeof(IntData), VariableKind.In)); 59 AddVariableInfo(new VariableInfo("UseEstimatedTargetValue", "Wether to use the original (measured) or the estimated (calculated) value for the targat variable when doing autoregressive modelling", typeof(BoolData), VariableKind.In)); 49 60 AddVariableInfo(new VariableInfo("Quality", "The evaluated quality of the model", typeof(DoubleData), VariableKind.New)); 50 61 } 51 62 52 63 public override IOperation Apply(IScope scope) { 53 int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data; 54 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); 64 // get all variable values 65 targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data; 66 dataset = GetVariableValue<Dataset>("Dataset", scope, true); 55 67 IFunctionTree functionTree = GetVariableValue<IFunctionTree>("FunctionTree", scope, true); 56 this.maximumPunishment = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data * dataset.GetRange(targetVariable); 57 this.treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data; 58 this.totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data; 68 double maximumPunishment = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data * dataset.GetRange(targetVariable); 69 treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data; 70 totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data; 71 trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data; 72 trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data; 73 useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, true).Data; 74 // prepare for autoregressive modelling by saving the original values of the target-variable to a backup array 75 if(useEstimatedValues && backupValues == null) { 76 backupValues = new double[trainingEnd - trainingStart]; 77 for(int i = trainingStart; i < trainingEnd; i++) { 78 backupValues[i - trainingStart] = dataset.GetValue(i, targetVariable); 79 } 80 } 81 // get the mean of the values of the target variable to determin the max and min bounds of the estimated value 82 targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd); 83 estimatedValueMin = targetMean - maximumPunishment; 84 estimatedValueMax = targetMean + maximumPunishment; 85 86 // initialize and reset the evaluator 59 87 if(evaluator == null) evaluator = functionTree.CreateEvaluator(dataset); 60 88 evaluator.ResetEvaluator(functionTree); 61 double result = Evaluate(scope, functionTree, targetVariable, dataset);89 evaluatedSamples = 0; 62 90 91 // calculate the quality measure 92 double result = Evaluate(trainingStart, trainingEnd); 93 94 // restore the values of the target variable from the backup array if necessary 95 if(useEstimatedValues) RestoreDataset(dataset, targetVariable, trainingStart, trainingEnd); 96 // update the value of total evaluated nodes 97 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * evaluatedSamples; 98 // write the calculate quality value 63 99 DoubleData quality = GetVariableValue<DoubleData>("Quality", scope, false, false); 64 100 if(quality == null) { … … 67 103 quality.Data = result; 68 104 } 69 70 105 return null; 71 106 } 72 107 73 public abstract double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset); 108 private void RestoreDataset(Dataset dataset, int targetVariable, int from, int to) { 109 for(int i = from; i < to; i++) { 110 dataset.SetValue(i, targetVariable, backupValues[i - from]); 111 } 112 } 113 114 public abstract double Evaluate(int start, int end); 115 116 public double GetOriginalValue(int sample) { 117 if(useEstimatedValues) 118 return backupValues[sample - trainingStart]; 119 else 120 return dataset.GetValue(sample, targetVariable); 121 } 122 123 public double GetEstimatedValue(int sample) { 124 evaluatedSamples++; 125 double estimated = evaluator.Evaluate(sample); 126 if(double.IsNaN(estimated) || double.IsInfinity(estimated)) { 127 estimated = estimatedValueMax; 128 } else if(estimated > estimatedValueMax) { 129 estimated = estimatedValueMax; 130 } else if(estimated < estimatedValueMin) { 131 estimated = estimatedValueMin; 132 } 133 134 if(useEstimatedValues) { 135 dataset.SetValue(sample, targetVariable, estimated); 136 } 137 138 return estimated; 139 } 74 140 } 75 141 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MCCEvaluator.cs
r453 r479 32 32 namespace HeuristicLab.StructureIdentification { 33 33 public class MCCEvaluator : GPEvaluatorBase { 34 private double limit; 35 private double[] original = new double[1]; 36 private double[] estimated = new double[1]; 34 37 public override string Description { 35 38 get { … … 37 40 } 38 41 } 39 40 42 public MCCEvaluator() 41 43 : base() { … … 43 45 } 44 46 45 p rivate double[] original = new double[1];46 private double[] estimated = new double[1];47 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {48 int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;49 int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data; 50 int nSamples = trainingEnd-trainingStart;51 double limit = GetVariableValue<DoubleData>("ClassSeparation", scope, true).Data;47 public override IOperation Apply(IScope scope) { 48 limit = GetVariableValue<DoubleData>("ClassSeparation", scope, true).Data; 49 return base.Apply(scope); 50 } 51 52 public override double Evaluate(int start, int end) { 53 int nSamples = end - start; 52 54 if(estimated.Length != nSamples) { 53 55 estimated = new double[nSamples]; … … 57 59 double positive = 0; 58 60 double negative = 0; 59 double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd); 60 for(int sample = trainingStart; sample < trainingEnd; sample++) { 61 double est = evaluator.Evaluate(sample); 62 double orig = dataset.GetValue(sample, targetVariable); 63 if(double.IsNaN(est) || double.IsInfinity(est)) { 64 est = targetMean + maximumPunishment; 65 } else if(est > targetMean + maximumPunishment) { 66 est = targetMean + maximumPunishment; 67 } else if(est < targetMean - maximumPunishment) { 68 est = targetMean - maximumPunishment; 69 } 70 estimated[sample-trainingStart] = est; 71 original[sample-trainingStart] = orig; 61 for(int sample = start; sample < end; sample++) { 62 double est = GetEstimatedValue(sample); 63 double orig = GetOriginalValue(sample); 64 estimated[sample - start] = est; 65 original[sample - start] = orig; 72 66 if(orig >= limit) positive++; 73 67 else negative++; … … 79 73 double tn = negative; 80 74 double fp = 0; 81 for(int i = original.Length -1; i >= 0; i--) {75 for(int i = original.Length - 1; i >= 0; i--) { 82 76 if(original[i] >= limit) { 83 77 tp++; fn--; … … 90 84 } 91 85 } 92 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd - trainingStart);93 86 return best_mcc; 94 87 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MeanAbsolutePercentageErrorEvaluator.cs
r400 r479 43 43 } 44 44 45 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) { 46 int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data; 47 int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data; 45 public override double Evaluate(int start, int end) { 48 46 double errorsSum = 0.0; 49 for(int sample = trainingStart; sample < trainingEnd; sample++) {50 double estimated = evaluator.Evaluate(sample);51 double original = dataset.GetValue(sample, targetVariable);47 for(int sample = start; sample < end; sample++) { 48 double estimated = GetEstimatedValue(sample); 49 double original = GetOriginalValue(sample); 52 50 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 53 if(double.IsNaN(estimated) || double.IsInfinity(estimated))54 estimated = maximumPunishment;55 else if(estimated > maximumPunishment)56 estimated = maximumPunishment;57 else if(estimated < -maximumPunishment)58 estimated = -maximumPunishment;59 60 51 double percent_error = Math.Abs((estimated - original) / original); 61 52 errorsSum += percent_error; 62 53 } 63 54 } 64 int nSamples = trainingEnd - trainingStart; 65 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * nSamples; 66 double quality = errorsSum / nSamples; 55 double quality = errorsSum / (end - start); 67 56 if(double.IsNaN(quality) || double.IsInfinity(quality)) 68 57 quality = double.MaxValue; -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MeanSquaredErrorEvaluator.cs
r396 r479 32 32 namespace HeuristicLab.StructureIdentification { 33 33 public class MeanSquaredErrorEvaluator : GPEvaluatorBase { 34 protected double[] backupValues;35 34 public override string Description { 36 35 get { … … 42 41 public MeanSquaredErrorEvaluator() 43 42 : base() { 44 AddVariableInfo(new VariableInfo("UseEstimatedTargetValue", "Wether to use the original (measured) or the estimated (calculated) value for the targat variable when doing autoregressive modelling", typeof(BoolData), VariableKind.In));45 GetVariableInfo("UseEstimatedTargetValue").Local = true;46 AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData(false)));47 43 } 48 44 49 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) { 50 int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data; 51 int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data; 45 public override double Evaluate(int start, int end) { 52 46 double errorsSquaredSum = 0; 53 double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd); 54 bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, false).Data; 55 if(useEstimatedValues && backupValues == null) { 56 backupValues = new double[trainingEnd - trainingStart]; 57 for(int i = trainingStart; i < trainingEnd; i++) { 58 backupValues[i-trainingStart] = dataset.GetValue(i, targetVariable); 59 } 60 } 61 for(int sample = trainingStart; sample < trainingEnd; sample++) { 62 double estimated = evaluator.Evaluate(sample); 63 double original = dataset.GetValue(sample, targetVariable); 64 if(double.IsNaN(estimated) || double.IsInfinity(estimated)) { 65 estimated = targetMean + maximumPunishment; 66 } else if(estimated > targetMean + maximumPunishment) { 67 estimated = targetMean + maximumPunishment; 68 } else if(estimated < targetMean - maximumPunishment) { 69 estimated = targetMean - maximumPunishment; 70 } 71 double error = estimated - original; 72 errorsSquaredSum += error * error; 73 if(useEstimatedValues) { 74 dataset.SetValue(sample, targetVariable, estimated); 47 for(int sample = start; sample < end; sample++) { 48 double original = GetOriginalValue(sample); 49 double estimated = GetEstimatedValue(sample); 50 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 51 double error = estimated - original; 52 errorsSquaredSum += error * error; 75 53 } 76 54 } 77 55 78 if(useEstimatedValues) RestoreDataset(dataset, targetVariable, trainingStart, trainingEnd); 79 errorsSquaredSum /= (trainingEnd-trainingStart); 56 errorsSquaredSum /= (end - start); 80 57 if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) { 81 58 errorsSquaredSum = double.MaxValue; 82 59 } 83 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd-trainingStart);84 60 return errorsSquaredSum; 85 }86 87 private void RestoreDataset(Dataset dataset, int targetVariable, int from, int to) {88 for(int i = from; i < to; i++) {89 dataset.SetValue(i, targetVariable, backupValues[i-from]);90 }91 61 } 92 62 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/SimpleEvaluator.cs
r396 r479 31 31 32 32 namespace HeuristicLab.StructureIdentification { 33 public class SimpleEvaluator : OperatorBase { 34 protected int treeSize; 35 protected double totalEvaluatedNodes; 36 private IEvaluator evaluator; 37 33 public class SimpleEvaluator : GPEvaluatorBase { 34 private ItemList values; 38 35 public SimpleEvaluator() 39 36 : base() { 40 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IFunctionTree), VariableKind.In));41 AddVariableInfo(new VariableInfo("TreeSize", "Size (number of nodes) of the tree to evaluate", typeof(IntData), 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("TotalEvaluatedNodes", "Number of evaluated nodes", typeof(DoubleData), VariableKind.In | VariableKind.Out));45 AddVariableInfo(new VariableInfo("TrainingSamplesStart", "Start index of training samples in dataset", typeof(IntData), VariableKind.In));46 AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "End index of training samples in dataset", typeof(IntData), VariableKind.In));47 37 AddVariableInfo(new VariableInfo("Values", "The values of the target variable as predicted by the model and the original value of the target variable", typeof(ItemList), VariableKind.New | VariableKind.Out)); 48 38 } 49 39 50 40 public override IOperation Apply(IScope scope) { 51 int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data; 52 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); 53 IFunctionTree functionTree = GetVariableValue<IFunctionTree>("FunctionTree", scope, true); 54 this.treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data; 55 this.totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data; 56 int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data; 57 int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data; 58 59 ItemList values = GetVariableValue<ItemList>("Values", scope, false, false); 41 values = GetVariableValue<ItemList>("Values", scope, false, false); 60 42 if(values == null) { 61 43 values = new ItemList(); … … 67 49 } 68 50 values.Clear(); 69 if(evaluator == null) evaluator = functionTree.CreateEvaluator(dataset); 70 evaluator.ResetEvaluator(functionTree); 71 for(int sample = trainingStart; sample < trainingEnd; sample++) { 51 return base.Apply(scope); 52 } 53 54 public override double Evaluate(int start, int end) { 55 for(int sample = start; sample < end; sample++) { 72 56 ItemList row = new ItemList(); 73 row.Add(new DoubleData( evaluator.Evaluate(sample)));74 row.Add(new DoubleData( dataset.GetValue(sample, targetVariable)));57 row.Add(new DoubleData(GetEstimatedValue(sample))); 58 row.Add(new DoubleData(GetOriginalValue(sample))); 75 59 values.Add(row); 76 60 } 77 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd - trainingStart); 78 return null; 61 return double.NaN; 79 62 } 80 63 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/TheilInequalityCoefficientEvaluator.cs
r400 r479 32 32 namespace HeuristicLab.StructureIdentification { 33 33 public class TheilInequalityCoefficientEvaluator : GPEvaluatorBase { 34 private bool differential; 34 35 public override string Description { 35 36 get { … … 44 45 } 45 46 46 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) { 47 int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data; 48 int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data; 49 bool difference = GetVariableValue<BoolData>("Differential", scope, true).Data; 47 public override IOperation Apply(IScope scope) { 48 differential = GetVariableValue<BoolData>("Differential", scope, true).Data; 49 return base.Apply(scope); 50 } 51 52 public override double Evaluate(int start, int end) { 50 53 double errorsSquaredSum = 0.0; 51 54 double estimatedSquaredSum = 0.0; 52 55 double originalSquaredSum = 0.0; 53 for(int sample = trainingStart; sample < trainingEnd; sample++) {56 for(int sample = start; sample < end; sample++) { 54 57 double prevValue = 0.0; 55 if(differen ce) prevValue = dataset.GetValue(sample - 1, targetVariable);56 double estimatedChange = evaluator.Evaluate(sample) - prevValue;57 double originalChange = dataset.GetValue(sample, targetVariable) - prevValue;58 if(differential) prevValue = GetOriginalValue(sample - 1); 59 double estimatedChange = GetEstimatedValue(sample) - prevValue; 60 double originalChange = GetOriginalValue(sample) - prevValue; 58 61 if(!double.IsNaN(originalChange) && !double.IsInfinity(originalChange)) { 59 if(double.IsNaN(estimatedChange) || double.IsInfinity(estimatedChange))60 estimatedChange = maximumPunishment;61 else if(estimatedChange > maximumPunishment)62 estimatedChange = maximumPunishment;63 else if(estimatedChange < -maximumPunishment)64 estimatedChange = - maximumPunishment;65 66 62 double error = estimatedChange - originalChange; 67 63 errorsSquaredSum += error * error; … … 70 66 } 71 67 } 72 int nSamples = trainingEnd - trainingStart; 73 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * nSamples; 68 int nSamples = end - start; 74 69 double quality = Math.Sqrt(errorsSquaredSum / nSamples) / (Math.Sqrt(estimatedSquaredSum / nSamples) + Math.Sqrt(originalSquaredSum / nSamples)); 75 if(double.IsNaN(quality) || double.IsInfinity(quality)) 70 if(double.IsNaN(quality) || double.IsInfinity(quality)) 76 71 quality = double.MaxValue; 77 72 return quality; -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/VarianceAccountedForEvaluator.cs
r400 r479 53 53 54 54 55 public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) { 56 int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data; 57 int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data; 58 double[] errors = new double[trainingEnd-trainingStart]; 59 double[] originalTargetVariableValues = new double[trainingEnd-trainingStart]; 60 double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd); 61 for(int sample = trainingStart; sample < trainingEnd; sample++) { 62 double estimated = evaluator.Evaluate(sample); 63 double original = dataset.GetValue(sample, targetVariable); 55 public override double Evaluate(int start, int end) { 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 = GetEstimatedValue(sample); 61 double original = GetOriginalValue(sample); 64 62 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 65 if(double.IsNaN(estimated) || double.IsInfinity(estimated)) 66 estimated = targetMean + maximumPunishment; 67 else if(estimated > (targetMean + maximumPunishment)) 68 estimated = targetMean + maximumPunishment; 69 else if(estimated < (targetMean - maximumPunishment)) 70 estimated = targetMean - maximumPunishment; 63 errors[sample - start] = original - estimated; 64 originalTargetVariableValues[sample - start] = original; 71 65 } 72 73 errors[sample-trainingStart] = original - estimated;74 originalTargetVariableValues[sample-trainingStart] = original;75 66 } 76 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd-trainingStart);77 78 67 double errorsVariance = Statistics.Variance(errors); 79 68 double originalsVariance = Statistics.Variance(originalTargetVariableValues);
Note: See TracChangeset
for help on using the changeset viewer.