public float FitnessFunction(int index) { float score = 0; GenerateQP <Gene> dna = ga.Population[index]; for (int i = 0; i < CLOCourse.Count; i++) { for (int j = 0; j < dna.Genes.Length; j++) { //foreach (string i in QuestionPool.Gene) //{ // for (int j = 0; j <dna.Genes.Length; j++) //{ if (CLOCourse[i].Equals(dna.Genes[j].CLO)) { score++; } break; //} } //score /= CLOSum.Count; } return(score); }
public void NewGeneration(int numNewDNA = 0, bool crossoverNewDNA = false) { int finalCount = Population.Count + numNewDNA; if (finalCount <= 0) { return; } if (Population.Count > 0) { CalculateFitness(); Population.Sort(CompareDNA); } newPopulation.Clear(); for (int i = 0; i < Population.Count; i++) { if (i < Elitism && i < Population.Count) { newPopulation.Add(Population[i]); } else if (i < Population.Count || crossoverNewDNA) { GenerateQP <T> parent1 = ChooseParent(); GenerateQP <T> parent2 = ChooseParent(); //if (parent1 == null) //{ // return; //} if (parent1 == null) { HttpContext.Current.Response.Write("NULL"); } GenerateQP <T> child = parent1.Crossover(parent2); // if (child == null) //{ // return; //} child.Mutate(MutationRate); newPopulation.Add(child); } else { newPopulation.Add(new GenerateQP <T>(dnaSize, random, getRandomGene, fitnessFunction, shouldInitGenes: true)); } } List <GenerateQP <T> > tmpList = Population; Population = newPopulation; newPopulation = tmpList; Generation++; }
public GenerateQP <T> Crossover(GenerateQP <T> otherParent) { GenerateQP <T> child = new GenerateQP <T>(Genes.Length, random, getRandomGene, fitnessFunction, shouldInitGenes: false); for (int i = 0; i < Genes.Length; i++) { child.Genes[i] = random.NextDouble() < 0.5 ? Genes[i] : otherParent.Genes[i]; } return(child); }
public void NewGeneration(int numNewDNA = 0, bool crossoverNewDNA = false) { int finalCount = popsize + numNewDNA; if (finalCount <= 0) { return; } if (popsize > 0) { CalculateFitness(); Population.Sort(CompareDNA); } newPopulation.Clear(); for (int i = 0; i < popsize; i++) { if (i < Elitism && i < popsize) { newPopulation.Add(Population[i]); } else if (crossoverNewDNA) { GenerateQP <T> parent1 = ChooseParent(); GenerateQP <T> parent2 = ChooseParent(); //if (parent1 == null) //{ // return; //} GenerateQP <T> child = parent1.Crossover(parent2); // if (child == null) //{ // return; //} //child.Mutate(MutationRate); newPopulation.Add(child); } else { GenerateQP <T> generateclone = new GenerateQP <T>(dnaSize, random, getRandomGene, fitnessFunction, shouldInitGenes: true); newPopulation.Add((GenerateQP <T>)generateclone.Clone()); } } List <GenerateQP <T> > tmpList = new List <GenerateQP <T> >(Population); Population = new List <GenerateQP <T> >(newPopulation); newPopulation = new List <GenerateQP <T> >(tmpList); Generation++; }
private int CompareDNA(GenerateQP <T> a, GenerateQP <T> b) { if (a.Fitness > b.Fitness) { return(-1); } else if (a.Fitness < b.Fitness) { return(1); } else { return(0); } }
private void CalculateFitness() { fitnessSum = 0; GenerateQP <T> best = Population[0]; for (int i = 0; i < Population.Count; i++) { fitnessSum += Population[i].CalculateFitness(i); if (Population[i].Fitness > best.Fitness) { best = Population[i]; } } BestFitness = best.Fitness; best.Genes.CopyTo(BestGenes, 0); }
private void CalculateFitness() { fitnessSum = 0; GenerateQP <T> best = Population[0]; foreach (GenerateQP <T> gene in Population) { fitnessSum += gene.CalculateFitness(); if (gene.Fitness > best.Fitness) { best = gene; } } BestFitness = best.Fitness; best.Genes.CopyTo(BestGenes, 0); }
public GeneticAlgorithm(int populationSize, int dnaSize, Random random, Func <int, T> getRandomGene, Func <GenerateQP <T>, float> fitnessFunction, int elitism, float mutationRate = 0.01f) { popsize = populationSize; Generation = 1; Elitism = elitism; MutationRate = mutationRate; Population = new List <GenerateQP <T> >(); newPopulation = new List <GenerateQP <T> >(); this.random = random; this.dnaSize = dnaSize; this.getRandomGene = getRandomGene; this.fitnessFunction = fitnessFunction; BestGenes = new T[dnaSize]; for (int i = 0; i < popsize; i++) { GenerateQP <T> generateclone = new GenerateQP <T>(dnaSize, random, getRandomGene, fitnessFunction, shouldInitGenes: true); Population.Add((GenerateQP <T>)generateclone.Clone()); } CalculateFitness(); }