Example #1
0
        public Ind <G> Run(int steps, float goalFit)
        {
            // Aux. variable
            Ind <G>[] aux;

            // Run the genetic algorithm until a maximum number of steps or
            // a goal fitness is reached
            for (int i = 0; i < steps && best.Fit < goalFit; step++, i++)
            {
                // Select the next generation of individuals
                select.Invoke(popCurrent, popNext);

                // Clone the selected individuals
                for (int j = 0; j < popSize; j++)
                {
                    popNext[j] = popNext[j].Copy();
                }

                // Crossover (mate)
                for (int j = 0; j < popSize; j += 2)
                {
                    if (random.NextDouble() < crossoverProb)
                    {
                        mate.Invoke(popNext[j], popNext[j + 1]);
                        popNext[j].Fit = null;
                    }
                }

                // Mutation
                foreach (Ind <G> ind in popNext)
                {
                    if (random.NextDouble() < mutateProb)
                    {
                        mutate.Invoke(ind);
                        ind.Fit = null;
                    }
                }

                // Evaluate new generation
                foreach (Ind <G> ind in popNext)
                {
                    if (!ind.Fit.HasValue)
                    {
                        evaluate.Invoke(ind);
                        if (ind.Fit > best.Fit)
                        {
                            best = ind;
                        }
                    }
                }

                // Update population
                aux        = popCurrent;
                popCurrent = popNext;
                popNext    = aux;
            }

            // Return best solution found so far
            return(best);
        }
        public void Mutate(Ind <G> ind)
        {
            for (int i = 0; i < ind.GeneCount; i++)
            {
                if (random.NextDouble() < flipProb)
                {
                    G changed;
                    do
                    {
                        changed = geneValues[random.Next(geneValues.Length)];
                    } while (changed.Equals(ind[i]));

                    ind[i] = changed;
                }
            }
        }
Example #3
0
        public void Init()
        {
            step = 0;

            // Generate a new population and determine each individual's fitness
            for (int i = 0; i < popSize; i++)
            {
                Ind <G> ind = generate.Invoke();
                evaluate.Invoke(ind);
                popCurrent[i] = ind;

                if (i == 0 || ind.Fit > best.Fit)
                {
                    best = popCurrent[i];
                }
            }
        }
        public void Mate(Ind <G> ind1, Ind <G> ind2)
        {
            int split = random.Next(ind1.GeneCount);

            for (int i = 0; i < split; i++)
            {
                G aux = ind1[i];
                ind1[i] = ind2[i];
                ind2[i] = aux;
            }

            for (int i = split; i < ind1.GeneCount; i++)
            {
                G aux = ind2[i];
                ind2[i] = ind1[i];
                ind1[i] = aux;
            }
        }