public List<Monk> Selection(Population population)
        {
            List<Monk> result = new List<Monk>();
            Random r  = new Random();
            int length = population.Chromosomes.Count;

            List<List<Monk>> groups = new List<List<Monk>>();
            List<Monk> group = new List<Monk>();

            for (int i = 0; i < length; i++)
            {
                if (i % GroupSize == 0 && i!=0)
                {
                    groups.Add(group);
                    group.Clear();
                }
                int index = r.Next(population.Chromosomes.Count);
                group.Add(population.Chromosomes.ElementAt(index));
                population.Chromosomes.RemoveAt(index);
            }
            if (group.Count > 0 )
            {
                groups.Add(group);
            }

            foreach (var gr in groups)
            {
                result.Add(gr.OrderByDescending(m => m.Fitness).First());
            }

            return result;
        }
 public List<Monk> Selection(Population population)
 {
     //blank
     return new List<Monk>();
 }
 public List<Monk> Selection(Population population)
 {
     throw new NotImplementedException();
 }
Beispiel #4
0
        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            List<object> args= e.Argument as List<object>;
            int loops = (int)args.ElementAt(0);
            int max_fitness = (int) args.ElementAt(1);
            elitarism_on = (bool)args.ElementAt(2);

            if (board != null)
            {
                int i = 0;
                Population pop = new Population(board);
                pop.GenerateFirstPopulation(pop_size, rand);
                worker.ReportProgress(0, pop.ToString());

                while ((i < loops) && (!pop.Chromosomes.First().Fitness.Equals(max_fitness)) )
                {
                    pop.Selection(elite_rate, selection,elitarism_on);
                    pop.Breed(rand,mutation_chance);
                    pop.Sort();
                    i++;
                    worker.ReportProgress(0, pop.ToString());
                }
                if (i != loops)
                {
                    e.Result = i;
                }
                else
                    e.Result = -1;
            }
            else
            {
                MessageBox.Show("Board not yet loaded");
            }
        }