Example #1
0
        public void CreateNewPopulation()
        {
            Gene[] temppop = new Gene[MAXPOP];
            for (int i = 0; i < MAXPOP; i++)
            {
                int parent1 = 0, parent2 = 0, iterations = 0;
                while (parent1 == parent2 || population[parent1] == population[parent2])
                {
                    parent1 = GetIndex((float)(rand.Next() % 101));
                    parent2 = GetIndex((float)(rand.Next() % 101));
                    if (++iterations > MAXPOP * MAXPOP)
                    {
                        break;
                    }
                }

                temppop[i] = Breed(parent1, parent2);
            }

            for (int i = 0; i < MAXPOP; i++)
            {
                population[i] = temppop[i];
            }
        }
Example #2
0
        public int Fitness(Gene gn)
        {
            int total = ca * gn.alleles[0] + cb * gn.alleles[1] + cc * gn.alleles[2] + cd * gn.alleles[3];

            return(gn.fitness = Math.Abs(total - result));
        }