/// <summary> /// Finds the worst individual thats actually alive /// </summary> /// <returns></returns> public int findWorstAlive() { bool first = true; int worstScore = 0; int worstIndex = 0; for (int i = 1; i < numInPop; i++) { Phenotype p = maps[i]; if (p.alive && first) { first = false; worstScore = p.score; worstIndex = i; continue; } if (p.alive && p.score < worstScore) { worstScore = p.score; worstIndex = i; } } return(worstIndex); }
/// <summary> /// Search for dead individuals - replace them with living newborn ones /// </summary> public void breedPopulation(Random r) { listOfLiving = new int[Params.populationCnt]; countOfLiving = 0; for (int i = 0; i < Params.populationCnt; i++) { if (getPhenotype(i).alive&& (!getPhenotype(i).newborn)) { listOfLiving[i] = i; countOfLiving++; } } for (int i = 0; i < Params.populationCnt; i++) { if (!getPhenotype(i).alive) { int mum = r.Next(0, countOfLiving); int dad = r.Next(0, countOfLiving); mum = listOfLiving[mum]; dad = listOfLiving[dad]; Phenotype mumP = getPhenotype(mum); Phenotype dadP = getPhenotype(dad); Genotype ggg = makeGenome(mumP.genotype, dadP.genotype); if (Params.mutationPercent > r.Next(0, 100)) { mutate(ggg, r); } //checkDuplicateGenes(ggg); maps[i] = new Phenotype(ggg, G.pop.generation); } } }
public void showNum(int num) { Phenotype pt = G.pop.getPhenotype(num); pt.show(G.form2.getPictureBox1()); Messageline1("Individual=" + num.ToString() + " Score = " + pt.score.ToString()); }
public Population(int numInPopZ, Random r) { numInPop = numInPopZ; maps = new Phenotype[numInPop]; for (int i = 0; i < numInPop; i++) { Genotype g = new Genotype(r); Phenotype p = new Phenotype(g, 0); p.createPheno(); p.setScore(); maps[i] = p; } }
/// <summary> /// Returns the index of the best individual and updates bestScore /// </summary> /// <returns></returns> public int findBest() { Phenotype p = maps[0]; bestScore = p.score; bestIndex = 0; for (int i = 1; i < numInPop; i++) { p = maps[i]; if (p.score > bestScore) { bestIndex = i; bestScore = p.score; } } return(bestIndex); }
private void button9_Click(object sender, EventArgs e) { G.init(); if (G.form2 == null) { G.form2 = new Form2(); } G.form2.Show(); G.form2.Activate(); label4.Text = "Running"; Params.checkDuplicateGenes = checkBox2.Checked; Params.checkDuplicateGenomes = Convert.ToInt32(textBox4.Text); Params.mutationPercent = Convert.ToDouble(textBox3.Text); G.mutationCount = 0; G.dupGeneCount = 0; G.dupGeneomeCount = 0; G.rnd = new Random(Convert.ToInt32(textBox5.Text)); Params.dimX = Convert.ToInt32(textBox6.Text); Params.dimY = Convert.ToInt32(textBox7.Text); Params.maxRepeat = Convert.ToInt32(textBox8.Text); Params.populationCnt = Convert.ToInt32(textBox9.Text); Params.genotypeSize = Convert.ToInt32(textBox10.Text); Genotype gt = new Genotype(G.rnd); Phenotype pt = new Phenotype(gt, 0); pt.show(G.form2.getPictureBox1()); G.pop = new Population(Params.populationCnt, G.rnd); G.pop.unsetNewborn(); G.pop.generation = 0; while (G.pop.generation < Convert.ToInt32(textBox2.Text) && !checkBox1.Checked) { G.pop.do1Generation(); int idx = G.pop.findBest(); showNum(idx); label5.Text = G.pop.generation.ToString(); label13.Text = "Mutations :" + G.mutationCount.ToString(); label14.Text = "Duplicate genes fixed :" + G.dupGeneCount.ToString(); label15.Text = "Duplicate genomes fixed:" + G.dupGeneomeCount.ToString(); } label4.Text = "Ended"; }