public thing mate(thing one, thing two) { thing offspring = new thing(); //Console.WriteLine("Offsping ft:" + offspring.Fitness); Random r = new Random(); int divPoint = r.Next(1, one.Gene.Length - 2); //Console.WriteLine("divPoint : "+divPoint); for(int i = 0; i < offspring.Gene.Length; i++) { if (i < divPoint) offspring.Gene[i] = one.Gene[i]; else offspring.Gene[i] = two.Gene[i]; } foreach(int i in offspring.Gene) { offspring.Fitness += i; } //Console.WriteLine("fitness(O) : " + offspring.Fitness); return offspring; }
public int getFitness(thing t) { int fitness = 0; foreach (int i in t.Gene) fitness += i; return fitness; }
public void Generation(ref thing[] things) { thing[] candidates = new thing[population]; thing[] newGen = new thing[population]; things = Create(); for(int i = 0; i < population; i++) { candidates[i] = Roulette(things); } Random r1 = new Random(12342); Random r2 = new Random(65313); int j = 0; for(int i = 0; i < population; i++) { int idx1 = r1.Next(0, population); int idx2 = r2.Next(0, population); if (idx1 == idx2) { Console.Write(j+"/"); j++; } newGen[i] = mate(candidates[idx1], candidates[idx2]); } Console.WriteLine("Average Fitness : " + getAvr(newGen)); }
public void print(thing t) { foreach(int i in t.Gene) { Console.Write(" " + i); } Console.WriteLine(" FItness : {0}",t.Fitness); }
thing[] Create() { thing[] things = new thing[population]; for(int i = 0; i < things.Length; i++) { things[i] = new thing(); } return things; }
public int getFitness(thing t) { int fitness = 0; foreach (int i in t.Gene) { fitness += i; } return(fitness); }