00001 using System; 00002 using System.Collections.Generic; 00003 using System.Linq; 00004 using System.Text; 00005 00006 namespace GEP.Core.Common.GeneticOperators.GenotypeSelection 00007 { 00011 [Serializable] 00012 public class Fittest : GenotypeSelection 00013 { 00019 protected int _selectionSize; 00025 protected int[][] _selection; 00026 00033 public Fittest(int selectionSize) 00034 { 00035 _selectionSize = selectionSize; 00036 _selection = new int[1][]; 00037 _selection[0] = new int[selectionSize]; 00038 } 00039 00040 public int[][] Select(ref int selectionStartIndex, ref int selectionEndIndex, Population population) 00041 { 00042 Select(population); 00043 selectionStartIndex = 0; 00044 selectionEndIndex = 0; 00045 return _selection; 00046 } 00047 00048 public int[][] Select(Population population) 00049 { 00050 double optimalFitness; 00051 double lastOptimalFitness = 00052 (population._operators._matingProbabilityFunction._fitnessCloserToZeroIsBetter ? 0 : double.MaxValue); 00053 int optimalIndex = 0; 00054 bool isSelected; 00055 for (int i = 0; i < _selectionSize; i++) 00056 { 00057 optimalFitness = 00058 (population._operators._matingProbabilityFunction._fitnessCloserToZeroIsBetter ? double.MaxValue : 0); 00059 for (int j = 0; j < population.Size; j++) 00060 { 00061 if (population[j]._fitness == lastOptimalFitness) 00062 { 00063 isSelected = false; 00064 for (int k = 0; k < i; k++) 00065 { 00066 if (j == _selection[0][k]) 00067 isSelected = true; 00068 } 00069 if (!isSelected) 00070 { 00071 optimalIndex = j; 00072 optimalFitness = lastOptimalFitness; 00073 break; 00074 } 00075 } 00076 if (population._operators._matingProbabilityFunction._fitnessCloserToZeroIsBetter) 00077 { 00078 if (population[j]._fitness > lastOptimalFitness) 00079 { 00080 if (population[j]._fitness < optimalFitness) 00081 { 00082 optimalIndex = j; 00083 optimalFitness = population[j]._fitness; 00084 } 00085 } 00086 } 00087 else 00088 { 00089 if (population[j]._fitness < lastOptimalFitness) 00090 { 00091 if (population[j]._fitness > optimalFitness) 00092 { 00093 optimalIndex = j; 00094 optimalFitness = population[j]._fitness; 00095 } 00096 } 00097 } 00098 } 00099 _selection[0][i] = optimalIndex; 00100 lastOptimalFitness = optimalFitness; 00101 } 00102 return _selection; 00103 } 00104 } 00105 }