/// <summary>
        /// Evalutate the fitness of the population.
        /// </summary>
        /// <param name="ad_fitness"></param>
        /// <param name="ad_bestfitness"></param>
        /// <param name="ao_pop"></param>
        /// <param name="ao_notablechroms"></param>
        public static void EvaluateFitness(ref double ad_fitness, ref double ad_bestfitness, Chromosome <T>[] ao_pop, NotableChromosomes <T> ao_notablechroms = null)
        {
            ad_bestfitness = 0;
            double ld_tfitness = 0;

            Globals <T> .MODIFYBONUS = 0;
            for (int i = 0; i < Globals <T> .POOLSIZE; i++)
            {
                ld_tfitness += ao_pop[i].fitness;

                if (ao_pop[i].fitness < ad_bestfitness || ad_bestfitness == 0)
                {
                    ad_bestfitness = ao_pop[i].fitness;
                }
                if (ao_notablechroms != null)
                {
                    ao_notablechroms.UpdateFinalBest(ao_pop[i]);
                }
            }

            ad_fitness = (ld_tfitness / ao_pop.Length);
            GeneticAPI.Fitness.Elitism <T> .MarkElite(ao_pop);
        }
        /// <summary>
        /// Initialize the population by calling the initialize chromosome factory until the population is full.
        /// </summary>
        /// <param name="ad_fitness"></param>
        /// <param name="ad_bestfitness"></param>
        /// <param name="ao_pop"></param>
        /// <param name="ao_notablechroms"></param>
        public static void Initialize(ref double ad_fitness, ref double ad_bestfitness, Chromosome <T>[] ao_pop, NotableChromosomes <T> ao_notablechroms = null)
        {
            for (int i = 0; i < Globals <T> .POOLSIZE; i++)
            {
                Chromosome <T> temp = InitialChromosomeFactory <T> .GenerateChromosome();

                ao_pop[i] = temp;
                if (ao_notablechroms != null)
                {
                    ao_notablechroms.UpdateInitialBest(temp);
                }
            }
            //Evaluate the fitness of the population.
            EvaluateFitness(ref ad_fitness, ref ad_bestfitness, ao_pop);
        }
Exemple #3
0
        /// <summary>
        /// Starts the Genetic Algorithm.
        /// </summary>
        /// <param name="ao_data">List of IData to be converted into Genes and stored in Chromosome.</param>
        /// <param name="ai_poolsize">Size of population.</param>
        /// <param name="ai_generations">Number of generations to iterate algorithm.</param>
        /// <param name="ad_modifyprob">Probability of modification/mutation.</param>
        /// <param name="ad_recomprob">Probability of recombination/crossover.</param>
        /// <param name="aen_selector">Selector Operator.</param>
        /// <param name="aen_recomb">Recombination Operator.</param>
        /// <param name="aen_random">Random basic vs adv</param>
        /// <param name="ai_elites">Number of Elites.</param>
        /// <param name="ai_ts_contestants">Number of Contestants for Tournament Selector.</param>
        /// <param name="ab_adaptivemut">Adaptive Mutation Enabler.</param>
        /// <param name="ab_rog">ROG Enabler.</param>
        /// <param name="ab_lrog">LROG Enabler.</param>
        public void Execute
        (
            List <T> ao_data,
            int ai_poolsize,
            int ai_generations,
            double ad_modifyprob,
            double ad_recomprob,
            Selectors aen_selector,
            Recombinators aen_recomb,
            Randoms aen_random,
            int ai_elites         = 1,
            int ai_ts_contestants = 2,
            bool ab_adaptivemut   = true,
            bool ab_rog           = false,
            bool ab_lrog          = true
        )
        {
            //Initialize global variables.
            Globals <T> .DATA = DataEncoder <T> .EncodeListFromData(ao_data);

            Globals <T> .GENERATIONS = ai_generations;
            Globals <T> .POOLSIZE    = ai_poolsize;
            Globals <T> .MODIFYPROB  = ad_modifyprob;
            Globals <T> .MODIFYBONUS = 0;
            Globals <T> .RECOMPROB   = ad_recomprob;
            Globals <T> .ELITENUM    = ai_elites;
            Globals <T> .ADAPTMUT    = ab_adaptivemut;
            Globals <T> .ROG         = ab_rog;
            Globals <T> .LROG        = ab_lrog;

            if (aen_random == Randoms.Basic)
            {
                Globals <T> .RAND = new BasicRandom();
            }
            else
            {
                Globals <T> .RAND = new AdvRandom();
            }


            //Initialize local variables.
            NotableChromosomes <T> lo_noteablechroms = new NotableChromosomes <T>();

            Chromosome <T>[] lo_pop            = new Chromosome <T> [Globals <T> .POOLSIZE];
            double           ld_fitness        = 0;
            double           ld_popbestfitness = 0;
            double           ld_inifitness     = 0;
            int li_generation = 0;

            //Start LROG thread if it's active.
            //if (Globals<T>.LROG && io_thread == null)
            //{
            //    Globals<T>.CPQ = new ConcurrentPriorityQueue<Chromosome<T>>(100);
            //    LessROG<T> lo_seeder = new LessROG<T>(ref Globals<T>.CPQ);
            //    io_thread = new Thread(new ThreadStart(lo_seeder.Run));
            //    io_thread.Start();
            //}

            if (Globals <T> .LROG && Globals <T> .SROGTHREAD == null)
            {
                Globals <T> .CPQ = new ConcurrentPriorityQueue <Chromosome <T> >(100);
                LessROG <T> lo_seeder = new LessROG <T>(ref Globals <T> .CPQ);
                Globals <T> .SROGTHREAD = new Thread(new ThreadStart(lo_seeder.Run));
                Globals <T> .SROGTHREAD.Start();
            }


            //Initialize population.
            ExecutionFunctions <T> .Initialize(ref ld_inifitness, ref ld_popbestfitness, lo_pop, lo_noteablechroms);

            Chromosome <T>[] lo_newpop = new Chromosome <T> [Globals <T> .POOLSIZE];
            //Iterate Genetic Algorithm.
            while (ContinueGA(ref li_generation))
            {
                ExecutionFunctions <T> .EvaluateElite(lo_pop);

                ExecutionFunctions <T> .Select(lo_pop, lo_newpop, aen_selector, ai_ts_contestants);

                ExecutionFunctions <T> .Recombination(lo_newpop, aen_recomb);

                ExecutionFunctions <T> .Modification(lo_newpop);

                ExecutionFunctions <T> .EvaluateFitness(ref ld_fitness, ref ld_popbestfitness, lo_newpop, lo_noteablechroms);

                lo_pop = lo_newpop;
                //Send statistics to UI.
                OnChanged(new APIEventArgs("", false, ld_fitness, ld_popbestfitness, lo_noteablechroms.GetFinalBest().fitness, lo_noteablechroms.GetFinalBest().ToString()));
            }

            //Send final statistics to UI.
            OnChanged(new APIEventArgs("Initial avg fitness: ", false, ld_inifitness, ld_popbestfitness, lo_noteablechroms.GetFinalBest().fitness, lo_noteablechroms.GetFinalBest().ToString()));
            OnChanged(new APIEventArgs("Final avg fitness: ", false, ld_fitness, ld_popbestfitness, lo_noteablechroms.GetFinalBest().fitness, lo_noteablechroms.GetFinalBest().ToString()));
            OnChanged(new APIEventArgs("Initial best fitness: ", false, ld_fitness, ld_popbestfitness, lo_noteablechroms.GetFinalBest().fitness, lo_noteablechroms.GetFinalBest().ToString()));
            OnChanged(new APIEventArgs("Overall best fitness: ", false, ld_fitness, ld_popbestfitness, lo_noteablechroms.GetFinalBest().fitness, lo_noteablechroms.GetFinalBest().ToString(), true));
        }