Changeset 702 for trunk/sources/HeuristicLab.GP.StructureIdentification
- Timestamp:
- 10/29/08 11:21:04 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.GP.StructureIdentification
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification/BakedTreeEvaluator.cs
r699 r702 30 30 31 31 namespace HeuristicLab.GP.StructureIdentification { 32 /// <summary> 33 /// Evaluates FunctionTrees recursively by interpretation of the function symbols in each node. 34 /// Not thread-safe! 35 /// </summary> 32 36 public class BakedTreeEvaluator { 33 37 private const double EPSILON = 1.0e-7; 38 private double estimatedValueMax; 39 private double estimatedValueMin; 34 40 35 41 private class Instr { … … 43 49 44 50 private List<Instr> code; 51 private Instr[] codeArr; 45 52 private int PC; 46 53 private Dataset dataset; … … 52 59 } 53 60 54 public void ResetEvaluator(BakedFunctionTree functionTree, Dataset dataset ) {61 public void ResetEvaluator(BakedFunctionTree functionTree, Dataset dataset, int targetVariable, int start, int end, double punishmentFactor) { 55 62 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 56 70 List<LightWeightFunction> linearRepresentation = functionTree.LinearRepresentation; 57 71 code.Clear(); … … 61 75 code.Add(curInstr); 62 76 } 77 78 codeArr = code.ToArray<Instr>(); 63 79 } 64 80 … … 88 104 PC = 0; 89 105 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; 91 116 } 92 117 … … 101 126 102 127 private double EvaluateBakedCode() { 103 Instr currInstr = code [PC++];128 Instr currInstr = codeArr[PC++]; 104 129 switch(currInstr.symbol) { 105 130 case EvaluatorSymbolTable.VARIABLE: { -
trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/CoefficientOfDeterminationEvaluator.cs
r656 r702 30 30 namespace HeuristicLab.GP.StructureIdentification { 31 31 public class CoefficientOfDeterminationEvaluator : GPEvaluatorBase { 32 private DoubleData r2;33 32 public override string Description { 34 33 get { … … 43 42 } 44 43 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) { 56 45 double errorsSquaredSum = 0.0; 57 46 double originalDeviationTotalSumOfSquares = 0.0; 47 double targetMean = dataset.GetMean(targetVariable, start, end); 58 48 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 } 62 54 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 63 55 double error = estimated - original; 64 56 errorsSquaredSum += error * error; 65 57 66 double origDeviation = original - TargetMean;58 double origDeviation = original - targetMean; 67 59 originalDeviationTotalSumOfSquares += origDeviation * origDeviation; 68 60 } … … 74 66 if(double.IsNaN(quality) || double.IsInfinity(quality)) 75 67 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 76 75 r2.Data = quality; 77 76 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/EarlyStoppingMeanSquaredErrorEvaluator.cs
r656 r702 30 30 namespace HeuristicLab.GP.StructureIdentification { 31 31 public class EarlyStoppingMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator { 32 private double qualityLimit;33 32 public override string Description { 34 33 get { … … 44 43 } 45 44 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 } 50 53 51 // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE52 public override void Evaluate(int start, int end) {53 54 double errorsSquaredSum = 0; 54 55 int rows = end - start; 55 56 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 } 59 62 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 60 63 double error = estimated - original; … … 71 74 errorsSquaredSum = double.MaxValue; 72 75 } 76 73 77 mse.Data = errorsSquaredSum; 74 78 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/GPEvaluatorBase.cs
r656 r702 31 31 namespace HeuristicLab.GP.StructureIdentification { 32 32 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 48 33 public GPEvaluatorBase() 49 34 : base() { … … 61 46 public override IOperation Apply(IScope scope) { 62 47 // 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); 65 50 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; 69 54 int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data; 70 55 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; 72 58 // prepare for autoregressive modelling by saving the original values of the target-variable to a backup array 73 59 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)) { 77 61 backupValues = new double[end - start]; 78 62 for(int i = start; i < end; i++) { … … 80 64 } 81 65 } 82 // get the mean of the values of the target variable to determin the max and min bounds of the estimated value83 targetMean = dataset.GetMean(targetVariable, start, end - 1);84 estimatedValueMin = targetMean - maximumPunishment;85 estimatedValueMax = targetMean + maximumPunishment;86 66 87 67 // 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); 91 70 92 Evaluate(s tart, end);71 Evaluate(scope, evaluator, dataset, targetVariable, start, end, useEstimatedValues); 93 72 94 73 // 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 96 80 // 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); 98 82 return null; 99 83 } 100 84 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); 131 86 } 132 87 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/MeanAbsolutePercentageErrorEvaluator.cs
r656 r702 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Operators; 29 using HeuristicLab.DataAnalysis; 29 30 30 31 namespace HeuristicLab.GP.StructureIdentification { 31 32 public class MeanAbsolutePercentageErrorEvaluator : GPEvaluatorBase { 32 private DoubleData mape;33 33 public override string Description { 34 34 get { … … 43 43 } 44 44 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) { 56 46 double errorsSum = 0.0; 57 47 int n = 0; 58 48 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) { 63 57 double percent_error = Math.Abs((estimated - original) / original); 64 58 errorsSum += percent_error; … … 69 63 if(double.IsNaN(quality) || double.IsInfinity(quality)) 70 64 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 71 73 mape.Data = quality; 72 74 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/MeanSquaredErrorEvaluator.cs
r656 r702 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Operators; 29 using HeuristicLab.DataAnalysis; 29 30 30 31 namespace HeuristicLab.GP.StructureIdentification { 31 32 public class MeanSquaredErrorEvaluator : GPEvaluatorBase { 32 protected DoubleData mse;33 33 public override string Description { 34 34 get { … … 43 43 } 44 44 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) { 56 46 double errorsSquaredSum = 0; 57 47 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 } 61 53 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 62 54 double error = estimated - original; … … 69 61 errorsSquaredSum = double.MaxValue; 70 62 } 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 71 70 mse.Data = errorsSquaredSum; 72 71 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/SimpleEvaluator.cs
r656 r702 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Operators; 29 using HeuristicLab.DataAnalysis; 29 30 30 31 namespace HeuristicLab.GP.StructureIdentification { 31 32 public class SimpleEvaluator : GPEvaluatorBase { 32 private ItemList values;33 33 public SimpleEvaluator() 34 34 : base() { … … 36 36 } 37 37 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); 40 40 if(values == null) { 41 41 values = new ItemList(); … … 47 47 } 48 48 values.Clear(); 49 return base.Apply(scope);50 }51 49 52 public override void Evaluate(int start, int end) {53 50 for(int sample = start; sample < end; sample++) { 54 51 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 } 58 57 row.Add(new DoubleData(estimated)); 59 58 row.Add(new DoubleData(original)); -
trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/VarianceAccountedForEvaluator.cs
r656 r702 31 31 namespace HeuristicLab.GP.StructureIdentification { 32 32 public class VarianceAccountedForEvaluator : GPEvaluatorBase { 33 private DoubleData vaf;34 33 public override string Description { 35 34 get { … … 54 53 } 55 54 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) { 67 56 int nSamples = end - start; 68 57 double[] errors = new double[nSamples]; 69 58 double[] originalTargetVariableValues = new double[nSamples]; 70 59 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 } 74 65 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 75 66 errors[sample - start] = original - estimated; … … 84 75 quality = double.MaxValue; 85 76 } 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 86 83 vaf.Data = quality; 87 84 }
Note: See TracChangeset
for help on using the changeset viewer.