Example #1
0
 public Chromosome(Chromosome <T> ao_chrom)
 {
     order   = ao_chrom.order;
     fitness = Fitness <T> .EvaluateTotal(order);
 }
Example #2
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));
        }
Example #3
0
 public int CompareTo(Chromosome <T> other)
 {
     return(this.fitness.CompareTo(other.fitness));
 }