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 MeanSquaredError : FitnessFunction 00014 { 00052 public MeanSquaredError(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 double notSquared; 00063 int ignoredCases = 0; 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 notSquared = _expectedOutput[i][0] - fGenotype._value; 00072 if (double.IsNaN(notSquared) || double.IsInfinity(notSquared)) 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 { 00087 fitness += notSquared * notSquared; 00088 } 00089 } 00090 } 00091 else 00092 { 00093 for (int i = startIndex; i <= endIndex; i++) 00094 { 00095 fGenotype.CalculateAll(i); 00096 notSquared = Common.Functions.Distance(_expectedOutput[i], fGenotype._geneValues); 00097 if (double.IsNaN(notSquared) || double.IsInfinity(notSquared)) 00098 { 00099 if (ignoredCases < _maxN_CasesOfNaNorInfinityToIgnore) 00100 ignoredCases++; 00101 else 00102 { 00103 if (isTest) 00104 fGenotype._testFitness = double.NaN; 00105 else 00106 fGenotype._fitness = double.NaN; 00107 return; 00108 } 00109 } 00110 else 00111 fitness += notSquared * notSquared; 00112 } 00113 } 00114 if (isTest) 00115 fGenotype._testFitness = fitness / (endIndex - startIndex - ignoredCases); 00116 else 00117 fGenotype._fitness = fitness / (endIndex - startIndex - ignoredCases); 00118 } 00119 } 00120 }