Changeset 702 for trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/GPEvaluatorBase.cs
- Timestamp:
- 10/29/08 11:21:04 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.