Changeset 702
- Timestamp:
- 10/29/08 11:21:04 (16 years ago)
- Location:
- trunk/sources
- Files:
-
- 1 added
- 1 deleted
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/AccuracyEvaluator.cs
r668 r702 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.GP.StructureIdentification; 29 using HeuristicLab.DataAnalysis; 29 30 30 31 namespace HeuristicLab.GP.StructureIdentification.Classification { 31 public class AccuracyEvaluator : GP EvaluatorBase {32 public class AccuracyEvaluator : GPClassificationEvaluatorBase { 32 33 private const double EPSILON = 1.0E-6; 33 private double[] classesArr;34 private double[] thresholds;35 private DoubleData accuracy;36 34 public override string Description { 37 35 get { … … 43 41 : base() { 44 42 AddVariableInfo(new VariableInfo("Accuracy", "The total accuracy of the model (ratio of correctly classified instances to total number of instances)", typeof(DoubleData), VariableKind.New)); 45 AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));46 43 } 47 44 48 public override IOperation Apply(IScope scope) {49 accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false);45 public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) { 46 DoubleData accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false); 50 47 if(accuracy == null) { 51 48 accuracy = new DoubleData(); … … 53 50 } 54 51 55 ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);56 classesArr = new double[classes.Count];57 for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;58 Array.Sort(classesArr);59 thresholds = new double[classes.Count - 1];60 for(int i = 0; i < classesArr.Length - 1; i++) {61 thresholds[i] = (classesArr[i] + classesArr[i + 1]) / 2.0;62 }63 64 return base.Apply(scope);65 }66 67 public override void Evaluate(int start, int end) {68 52 int nSamples = end - start; 69 53 int nCorrect = 0; 70 54 for(int sample = start; sample < end; sample++) { 71 double est = GetEstimatedValue(sample);72 double origClass = GetOriginalValue(sample);55 double est = evaluator.Evaluate(sample); 56 double origClass = dataset.GetValue(targetVariable, sample); 73 57 double estClass = double.NaN; 74 58 // if estimation is lower than the smallest threshold value -> estimated class is the lower class 75 if(est < thresholds[0]) estClass = classes Arr[0];59 if(est < thresholds[0]) estClass = classes[0]; 76 60 // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class 77 else if(est >= thresholds[thresholds.Length - 1]) estClass = classes Arr[classesArr.Length - 1];61 else if(est >= thresholds[thresholds.Length - 1]) estClass = classes[classes.Length - 1]; 78 62 else { 79 63 // otherwise the estimated class is the class which upper threshold is larger than the estimated value 80 64 for(int k = 0; k < thresholds.Length; k++) { 81 65 if(thresholds[k] > est) { 82 estClass = classes Arr[k];66 estClass = classes[k]; 83 67 break; 84 68 } 85 69 } 86 70 } 87 SetOriginalValue(sample, estClass);88 71 if(Math.Abs(estClass - origClass) < EPSILON) nCorrect++; 89 72 } -
trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/ClassificationMeanSquaredErrorEvaluator.cs
r668 r702 29 29 30 30 namespace HeuristicLab.GP.StructureIdentification.Classification { 31 public class ClassificationMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator { 32 private const double EPSILON = 1.0E-6; 33 private double[] classesArr; 31 public class ClassificationMeanSquaredErrorEvaluator : GPClassificationEvaluatorBase { 32 private const double EPSILON = 1.0E-7; 34 33 public override string Description { 35 34 get { … … 41 40 public ClassificationMeanSquaredErrorEvaluator() 42 41 : base() { 43 AddVariableInfo(new VariableInfo(" TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));42 AddVariableInfo(new VariableInfo("MSE", "The mean squared error of the model", typeof(DoubleData), VariableKind.New)); 44 43 } 45 44 46 public override IOperation Apply(IScope scope) { 47 ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true); 48 classesArr = new double[classes.Count]; 49 for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data; 50 Array.Sort(classesArr); 51 return base.Apply(scope); 52 } 53 54 public override void Evaluate(int start, int end) { 45 public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) 46 { 55 47 double errorsSquaredSum = 0; 56 48 for(int sample = start; sample < end; sample++) { 57 double estimated = GetEstimatedValue(sample); 58 double original = GetOriginalValue(sample); 59 SetOriginalValue(sample, estimated); 49 double estimated = evaluator.Evaluate(sample); 50 double original = dataset.GetValue(targetVariable, sample); 60 51 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 61 52 double error = estimated - original; … … 63 54 // on the lower end and upper end only add linear error if the absolute error is larger than 1 64 55 // the error>1.0 constraint is needed for balance because in the interval ]-1, 1[ the squared error is smaller than the absolute error 65 if((IsEqual(original, classes Arr[0]) && error < -1.0) ||66 (IsEqual(original, classes Arr[classesArr.Length - 1]) && error > 1.0)) {56 if((IsEqual(original, classes[0]) && error < -1.0) || 57 (IsEqual(original, classes[classes.Length - 1]) && error > 1.0)) { 67 58 errorsSquaredSum += Math.Abs(error); // only add linear error below the smallest class or above the largest class 68 59 } else { … … 76 67 errorsSquaredSum = double.MaxValue; 77 68 } 69 70 DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false); 71 if(mse == null) { 72 mse = new DoubleData(); 73 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse)); 74 } 75 78 76 mse.Data = errorsSquaredSum; 79 77 } -
trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/ConfusionMatrixEvaluator.cs
r668 r702 29 29 30 30 namespace HeuristicLab.GP.StructureIdentification.Classification { 31 public class ConfusionMatrixEvaluator : GPEvaluatorBase { 32 private const double EPSILON = 1.0E-6; 33 private double[] classesArr; 34 private double[] thresholds; 35 private IntMatrixData matrix; 31 public class ConfusionMatrixEvaluator : GPClassificationEvaluatorBase { 36 32 public override string Description { 37 33 get { … … 43 39 : base() { 44 40 AddVariableInfo(new VariableInfo("ConfusionMatrix", "The confusion matrix of the model", typeof(IntMatrixData), VariableKind.New)); 45 AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));46 41 } 47 42 48 public override IOperation Apply(IScope scope) { 49 ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true); 50 classesArr = new double[classes.Count]; 51 for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data; 52 Array.Sort(classesArr); 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; 43 public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) { 44 IntMatrixData matrix = GetVariableValue<IntMatrixData>("ConfusionMatrix", scope, false, false); 45 if(matrix == null) { 46 matrix = new IntMatrixData(new int[classes.Length, classes.Length]); 47 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ConfusionMatrix"), matrix)); 56 48 } 57 49 58 matrix = GetVariableValue<IntMatrixData>("ConfusionMatrix", scope, false, false);59 if(matrix == null) {60 matrix = new IntMatrixData(new int[classesArr.Length, classesArr.Length]);61 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ConfusionMatrix"), matrix));62 }63 return base.Apply(scope);64 }65 66 public override void Evaluate(int start, int end) {67 50 int nSamples = end - start; 68 51 for(int sample = start; sample < end; sample++) { 69 double est = GetEstimatedValue(sample);70 double origClass = GetOriginalValue(sample);52 double est = evaluator.Evaluate(sample); 53 double origClass = dataset.GetValue(targetVariable,sample); 71 54 int estClassIndex = -1; 72 55 // if estimation is lower than the smallest threshold value -> estimated class is the lower class 73 56 if(est < thresholds[0]) estClassIndex = 0; 74 57 // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class 75 else if(est >= thresholds[thresholds.Length - 1]) estClassIndex = classes Arr.Length - 1;58 else if(est >= thresholds[thresholds.Length - 1]) estClassIndex = classes.Length - 1; 76 59 else { 77 60 // otherwise the estimated class is the class which upper threshold is larger than the estimated value … … 83 66 } 84 67 } 85 SetOriginalValue(sample, classesArr[estClassIndex]);86 68 87 int origClassIndex = -1; 88 for(int i = 0; i < classesArr.Length; i++) { 89 if(IsEqual(origClass, classesArr[i])) origClassIndex = i; 69 // find the first threshold index that is larger to the original value 70 int origClassIndex = classes.Length-1; 71 for(int i = 0; i < thresholds.Length; i++) { 72 if(origClass < thresholds[i]) { 73 origClassIndex = i; 74 break; 75 } 90 76 } 91 77 matrix.Data[origClassIndex, estClassIndex]++; 92 78 } 93 79 } 94 95 private bool IsEqual(double x, double y) {96 return Math.Abs(x - y) < EPSILON;97 }98 80 } 99 81 } -
trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/HeuristicLab.GP.StructureIdentification.Classification.csproj
r669 r702 67 67 <Compile Include="ClassificationMeanSquaredErrorEvaluator.cs" /> 68 68 <Compile Include="ConfusionMatrixEvaluator.cs" /> 69 <Compile Include="GPClassificationEvaluatorBase.cs" /> 69 70 <Compile Include="CrossValidation.cs" /> 70 71 <Compile Include="FunctionLibraryInjector.cs"> … … 72 73 </Compile> 73 74 <Compile Include="HeuristicLabGPClassificationPlugin.cs" /> 74 <Compile Include="MCCEvaluator.cs" />75 75 <Compile Include="MulticlassModeller.cs" /> 76 76 <Compile Include="MulticlassOneVsOneAnalyzer.cs" /> -
trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/MulticlassOneVsOneAnalyzer.cs
r668 r702 79 79 80 80 BakedTreeEvaluator evaluator = new BakedTreeEvaluator(); 81 evaluator.ResetEvaluator(functionTree, dataset );81 evaluator.ResetEvaluator(functionTree, dataset, targetVariable, samplesStart, samplesEnd, 1.0); 82 82 83 83 for(int i = 0; i < (samplesEnd - samplesStart); i++) { -
trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/AveragePercentageChangeEvaluator.cs
r686 r702 30 30 namespace HeuristicLab.GP.StructureIdentification.TimeSeries { 31 31 public class AvergePercentageChangeEvaluator : GPEvaluatorBase { 32 protected DoubleData apc;33 private bool differential;34 32 public override string Description { 35 33 get { … … 44 42 } 45 43 46 public override IOperation Apply(IScope scope) {47 differential = GetVariableValue<BoolData>("Differential", scope, true).Data;48 apc = GetVariableValue<DoubleData>("APC", scope, false, false);44 public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) { 45 bool differential = GetVariableValue<BoolData>("Differential", scope, true).Data; 46 DoubleData apc = GetVariableValue<DoubleData>("APC", scope, false, false); 49 47 if(apc == null) { 50 48 apc = new DoubleData(); … … 52 50 } 53 51 54 return base.Apply(scope);55 }56 57 public override void Evaluate(int start, int end) {58 52 double percentageSum = 0; 59 53 for(int sample = start; sample < end; sample++) { … … 62 56 double estimatedPercentageChange; 63 57 if(differential) { 64 prevOriginal = GetOriginalValue(sample - 1); 65 originalPercentageChange = (GetOriginalValue(sample) - prevOriginal) / prevOriginal; 66 estimatedPercentageChange = (GetEstimatedValue(sample) - prevOriginal) / prevOriginal; 67 SetOriginalValue(sample, estimatedPercentageChange*prevOriginal + prevOriginal); 58 prevOriginal = dataset.GetValue(targetVariable,sample - 1); 59 originalPercentageChange = (dataset.GetValue(targetVariable,sample) - prevOriginal) / prevOriginal; 60 estimatedPercentageChange = (evaluator.Evaluate(sample) - prevOriginal) / prevOriginal; 61 if(updateTargetValues) { 62 dataset.SetValue(targetVariable, sample, estimatedPercentageChange * prevOriginal + prevOriginal); 63 } 68 64 } else { 69 originalPercentageChange = GetOriginalValue(sample); 70 estimatedPercentageChange = GetEstimatedValue(sample); 71 SetOriginalValue(sample, estimatedPercentageChange); 65 originalPercentageChange = dataset.GetValue(targetVariable,sample); 66 estimatedPercentageChange = evaluator.Evaluate(sample); 67 if(updateTargetValues) { 68 dataset.SetValue(targetVariable, sample, estimatedPercentageChange); 69 } 72 70 } 73 71 if(!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) { -
trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/ProfitEvaluator.cs
r686 r702 30 30 namespace HeuristicLab.GP.StructureIdentification.TimeSeries { 31 31 public class ProfitEvaluator : GPEvaluatorBase { 32 protected DoubleData profit;33 private int exchangeRateVarIndex;34 private double transactionCost;35 32 public override string Description { 36 33 get { … … 46 43 } 47 44 48 public override IOperation Apply(IScope scope) {49 exchangeRateVarIndex = GetVariableValue<IntData>("ExchangeRate", scope, true).Data;50 transactionCost = GetVariableValue<DoubleData>("TransactionCost", scope, true).Data;51 profit = GetVariableValue<DoubleData>("Profit", scope, false, false);45 public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) { 46 int exchangeRateVarIndex = GetVariableValue<IntData>("ExchangeRate", scope, true).Data; 47 double transactionCost = GetVariableValue<DoubleData>("TransactionCost", scope, true).Data; 48 DoubleData profit = GetVariableValue<DoubleData>("Profit", scope, false, false); 52 49 if(profit == null) { 53 50 profit = new DoubleData(); … … 55 52 } 56 53 57 return base.Apply(scope);58 }59 60 public override void Evaluate(int start, int end) {61 54 double cA = 1.0; // start with a capital of one entity of A 62 55 double cB = 0; … … 64 57 for(int sample = start; sample < end; sample++) { 65 58 exchangeRate = dataset.GetValue(sample, exchangeRateVarIndex); 66 double originalPercentageChange = GetOriginalValue(sample); 67 double estimatedPercentageChange = GetEstimatedValue(sample); 68 SetOriginalValue(sample, estimatedPercentageChange); 59 double originalPercentageChange = dataset.GetValue(targetVariable, sample); 60 double estimatedPercentageChange = evaluator.Evaluate(sample); 61 if(updateTargetValues) { 62 dataset.SetValue(targetVariable, sample, estimatedPercentageChange); 63 } 69 64 if(!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) { 70 65 if(estimatedPercentageChange > 0) { 71 66 // prediction is the rate of B/A will increase (= get more B for one A) => exchange all B to A 72 cA += (cB / exchangeRate) * (1 -transactionCost);67 cA += (cB / exchangeRate) * (1 - transactionCost); 73 68 cB = 0; 74 69 } else if(estimatedPercentageChange < 0) { 75 70 // prediction is the rate of B/A will drop (= get more A for one B) => exchange all A to B 76 cB += (cA * exchangeRate) * (1 -transactionCost);71 cB += (cA * exchangeRate) * (1 - transactionCost); 77 72 cA = 0; 78 73 } … … 86 81 // at the end we must exchange all B back to A 87 82 // (because we want to buy the local beer not import one from B-country) 88 profit.Data = cA + ((cB / exchangeRate) * (1 -transactionCost));83 profit.Data = cA + ((cB / exchangeRate) * (1 - transactionCost)); 89 84 profit.Data -= 1.0; // substract the start capital to determine actual profit 90 85 } -
trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/TheilInequalityCoefficientEvaluator.cs
r695 r702 31 31 namespace HeuristicLab.GP.StructureIdentification.TimeSeries { 32 32 public class TheilInequalityCoefficientEvaluator : GPEvaluatorBase { 33 private DoubleData theilInequaliy;34 private DoubleData uBias;35 private DoubleData uVariance;36 private DoubleData uCovariance;37 38 33 public override string Description { 39 34 get { … … 59 54 } 60 55 61 public override IOperation Apply(IScope scope) { 62 theilInequaliy = GetVariableValue<DoubleData>("TheilInequalityCoefficient", scope, false, false); 56 public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) { 57 #region create result variables 58 DoubleData theilInequaliy = GetVariableValue<DoubleData>("TheilInequalityCoefficient", scope, false, false); 63 59 if(theilInequaliy == null) { 64 60 theilInequaliy = new DoubleData(); 65 61 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficient"), theilInequaliy)); 66 62 } 67 uBias = GetVariableValue<DoubleData>("TheilInequalityCoefficientBias", scope, false, false);63 DoubleData uBias = GetVariableValue<DoubleData>("TheilInequalityCoefficientBias", scope, false, false); 68 64 if(uBias == null) { 69 65 uBias = new DoubleData(); 70 66 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientBias"), uBias)); 71 67 } 72 uVariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientVariance", scope, false, false);68 DoubleData uVariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientVariance", scope, false, false); 73 69 if(uVariance == null) { 74 70 uVariance = new DoubleData(); 75 71 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientVariance"), uVariance)); 76 72 } 77 uCovariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientCovariance", scope, false, false);73 DoubleData uCovariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientCovariance", scope, false, false); 78 74 if(uCovariance == null) { 79 75 uCovariance = new DoubleData(); 80 76 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientCovariance"), uCovariance)); 81 77 } 82 return base.Apply(scope); 83 } 78 #endregion 84 79 85 public override void Evaluate(int start, int end) {86 80 double errorsSquaredSum = 0.0; 87 81 double originalSquaredSum = 0.0; 88 double[] estimatedChanges = new double[end -start];89 double[] originalChanges = new double[end -start];82 double[] estimatedChanges = new double[end - start]; 83 double[] originalChanges = new double[end - start]; 90 84 int nSamples = 0; 91 85 for(int sample = start; sample < end; sample++) { 92 double prevValue = GetOriginalValue(sample - 1); 93 double estimatedChange = GetEstimatedValue(sample) - prevValue; 94 double originalChange = GetOriginalValue(sample) - prevValue; 95 SetOriginalValue(sample, estimatedChange + prevValue); 86 double prevValue = dataset.GetValue(targetVariable, sample - 1); 87 double estimatedChange = evaluator.Evaluate(sample) - prevValue; 88 double originalChange = dataset.GetValue(targetVariable, sample) - prevValue; 89 if(updateTargetValues) { 90 dataset.SetValue(targetVariable, sample, estimatedChange + prevValue); 91 } 96 92 if(!double.IsNaN(originalChange) && !double.IsInfinity(originalChange)) { 97 93 double error = estimatedChange - originalChange; … … 118 114 119 115 // all parts add up to one so I don't have to calculate the correlation coefficient for the covariance propotion 120 uCovariance.Data = 1.0 - uBias.Data - uVariance.Data; 116 uCovariance.Data = 1.0 - uBias.Data - uVariance.Data; 121 117 } 122 118 } -
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.