00001 using System; 00002 using System.Collections.Generic; 00003 using System.Linq; 00004 using System.Text; 00005 00006 namespace GEP.Core.Float.FitnessFunctions 00007 { 00011 [Serializable] 00012 public class MeanAbsoluteError : FitnessFunction 00013 { 00014 00052 public MeanAbsoluteError(int teachingVariableCasesStartIndex, int teachingVariableCasesEndIndex, 00053 int testVariableCasesStartIndex, int testVariableCasesEndIndex, float[][] expectedOutput, 00054 int maxN_CasesOfNaNorInfinityToIgnore) 00055 : base(teachingVariableCasesStartIndex, teachingVariableCasesEndIndex, testVariableCasesStartIndex, 00056 testVariableCasesEndIndex, expectedOutput, maxN_CasesOfNaNorInfinityToIgnore) { } 00057 00058 public override void SetFitness(Common.Genotype genotype, bool isTest) 00059 { 00060 Genotype fGenotype = (Genotype)genotype; 00061 double fitness = 0; 00062 int ignoredCases = 0; 00063 double distance; 00064 int startIndex = (isTest ? _testVariableCasesStartIndex : _teachingVariableCasesStartIndex); 00065 int endIndex = (isTest ? _testVariableCasesEndIndex : _teachingVariableCasesEndIndex); 00066 if (_expectedOutput[0].Length == 1) 00067 { 00068 for (int i = startIndex; i <= endIndex; i++) 00069 { 00070 fGenotype.CalculateAll(i); 00071 distance = Math.Abs(_expectedOutput[i][0] - fGenotype._value); 00072 if (double.IsNaN(distance) || double.IsInfinity(distance)) 00073 { 00074 if (ignoredCases < _maxN_CasesOfNaNorInfinityToIgnore) 00075 ignoredCases++; 00076 else 00077 { 00078 if (isTest) 00079 fGenotype._testFitness = double.NaN; 00080 else 00081 fGenotype._fitness = double.NaN; 00082 return; 00083 } 00084 } 00085 else 00086 fitness += distance; 00087 } 00088 } 00089 else 00090 { 00091 for (int i = startIndex; i <= endIndex; i++) 00092 { 00093 fGenotype.CalculateAll(i); 00094 distance = Common.Functions.Distance(_expectedOutput[i], fGenotype._geneValues); 00095 if (double.IsNaN(distance) || double.IsInfinity(distance)) 00096 { 00097 if (ignoredCases < _maxN_CasesOfNaNorInfinityToIgnore) 00098 ignoredCases++; 00099 else 00100 { 00101 if (isTest) 00102 fGenotype._testFitness = double.NaN; 00103 else 00104 fGenotype._fitness = double.NaN; 00105 return; 00106 } 00107 } 00108 else 00109 fitness += distance; 00110 } 00111 } 00112 if (isTest) 00113 fGenotype._testFitness = fitness / (endIndex - startIndex - ignoredCases); 00114 else 00115 fGenotype._fitness = fitness / (endIndex - startIndex - ignoredCases); 00116 } 00117 } 00118 }