public CGroup Selection() { CGroup rSelectedGroup = new CGroup(m_rUpdater, m_rLaw); m_sSelectionLog = ""; double[] aSelectionPercents = new double[GroupSize]; double fGradesSum = 0; for (int i = 0; i < GroupSize; i++) { fGradesSum += GetGradeAtChromosome(i); } double fAvgGrade = fGradesSum / GroupSize; double fReverseGradesSum = 0; for (int i = 0; i < GroupSize; i++) { //aSelectionPercents[i] = fGradesSum - GetGradeAtChromosome(i); //fReverseGradesSum += aSelectionPercents[i]; aSelectionPercents[i] = fAvgGrade / GetGradeAtChromosome(i); fReverseGradesSum += aSelectionPercents[i]; } for (int i = 0; i < GroupSize; i++) { aSelectionPercents[i] = aSelectionPercents[i] / fReverseGradesSum; } UpdateSelectionLog(aSelectionPercents); for (int i = 0; i < GroupSize; i++) { double fRandom = SCRandom.Random; rSelectedGroup.AddChromosome(m_aChromosomes[GetNumberInGroup(fRandom, aSelectionPercents)]); } return(rSelectedGroup); }
public CGroup Crossbreeding() { CGroup rChilds = new CGroup(m_rUpdater, m_rLaw); int iFirstCrossing = -1; for (int i = 0; i < GroupSize; i++) { double fDecision = SCRandom.Random; if (m_rLaw.CrossbreedingChance > fDecision) { if (iFirstCrossing == -1) { iFirstCrossing = i; } else { rChilds.AddChromosome(m_aChromosomes[iFirstCrossing].Crossbreeding (m_aChromosomes[i])); iFirstCrossing = -1; } } } return(rChilds); }