Exemplo n.º 1
0
        //krizenie mnichov
        public Monk(Monk mother, Monk father)
        {
            if(mother.Length != father.Length)
                throw new ArgumentException("Parents must be of the same length");

            Random rand = new Random();
            Chromosome = new List<Gene>();
            Length = father.Length;
            Treshold = father.Treshold;
            Fitness = 0;

            //polovica z matky
            Chromosome.AddRange(mother.Chromosome.Take(Length / 2));
            //z otca to, co tam este nie je
            var remains = father.Chromosome.Except(Chromosome);
            Chromosome.AddRange(remains.Take(Length / 2));
            //ak treba dotvorim nove
            if (Chromosome.Count < Length)
            {
                int i = Chromosome.Count;

                while (i <= Length)
                {
                    bool tr = (rand.Next(2) == 1) ? true : false;
                    var g = new Gene(rand.Next(father.Treshold), tr);
                    if ((null != Chromosome.Where(t => t.Start == g.Start).FirstOrDefault()))
                        continue;
                    else
                    {
                        ++i;
                        Chromosome.Add(g);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Breed(Random randomizer, double mutation_chance)
        {
            int count = Size - Chromosomes.Count;
            List<Monk> children = new List<Monk>();
            List<int> visited = new List<int>();

            for (int i = 0; i < count; i++)
            {
                int m = -1;
                int t = -1;

                //m = randomizer.Next(Chromosomes.Count);
                do
                {
                    t = randomizer.Next(Chromosomes.Count);
                } while (visited.Contains(t) && (visited.Count<Chromosomes.Count));
                visited.Add(t);

                do
                {
                    m = randomizer.Next(Chromosomes.Count);
                } while (visited.Contains(m) && (visited.Count < Chromosomes.Count) || (m == t));
                visited.Add(m);

                Monk mother = Chromosomes.ElementAt(m);
                Monk father = Chromosomes.ElementAt(t);

                Monk kid = null;

                if (randomizer.Next(2) == 1)
                {
                    kid = new Monk(mother, father);
                }
                else
                {
                    kid = new Monk(father, mother);
                }
                kid.Mutate(randomizer, mutation_chance);
                kid.EvaluateOn(Board);
                children.Add(kid);
            }

            Chromosomes.AddRange(children);
        }
Exemplo n.º 3
0
 public void GenerateFirstPopulation(int size, Random randomizer)
 {
     this.Size = size;
     for (int i = 0; i < size; i++)
     {
         int circ = Board.Circumference();
         //maximalna dlzka genomu podla zadania = obvod/2 + pocet prekazok
         var monk = new Monk(circ / 2 + Board.Stones.Count, circ, randomizer);
         monk.EvaluateOn(Board);
         Chromosomes.Add(monk);
     }
     Sort();
 }