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