public Chromosome Recombine(Chromosome a, Chromosome b) { if (a == b) return null; // Chance to recombine means 2 fenotypes that meet // don't always create a new one. if (Chromosome.random.NextDouble() > crossoverProbability) return null; int partitionIndex = Chromosome.random.Next(Math.Min(a.Genes.Count, b.Genes.Count)); Gene gene = new Gene((Gene) a.Genes[partitionIndex], (Gene) b.Genes[partitionIndex]); ArrayList list = new ArrayList(); for (int i = 0; i < partitionIndex; i++) { list.Add(a.Genes[i]); } list.Add(gene); for (int i = partitionIndex + 1; i < b.Genes.Count; i++) { list.Add(b.Genes[i]); } Chromosome returnValue = new Chromosome(list); returnValue.Mutate(); returnValue.ComputeFitness(target); return returnValue; }
public void Randomize() { Chromosome chromosome; for (int i = 1; i <= populationSize; i++) { chromosome = new Chromosome(); chromosome.ComputeFitness(target); genotypes.Add(chromosome); } }