private List <Haiku> Select()           //selection of 4 parents
        {
            List <int>   roulette = Roulette(); //using roulette method of selection
            List <Haiku> parents  = new List <Haiku>();
            var          rnd      = new Random(Guid.NewGuid().GetHashCode());
            int          p;

            if (roulette.Count < 4) //if there are not enough parents in roulette (parents with 0 fitness value cannot be chosen to roulette)
            {
                for (int i = 0; i < roulette.Count; i++)
                {
                    p = rnd.Next(roulette.Count);              //select random parents from roulette
                    parents.Add(this.population[roulette[p]]); //add parent to parents list
                    roulette.RemoveAt(p);                      //remove the individual from roulette
                }
                for (int i = roulette.Count; i < 5; i++)       //random creation of parents
                {
                    Haiku haiku = new Haiku();                 //creates new instance of haiku poem with default fitness values
                    haiku.pickRandomTxt();                     //fills the lines of poem with randomly chosen haiku from the haiku database
                    this.population.Add(haiku);
                }
            }
            else
            {
                for (int i = 0; i < 4; i++)
                {
                    p = rnd.Next(roulette.Count);              //select random parents from roulette
                    parents.Add(this.population[roulette[p]]); //add parent to parents list
                    roulette.RemoveAt(p);                      //removing the individual from roulette
                }
            }

            return(parents);
        }
Esempio n. 2
0
        public Haiku crossOver(Haiku p2)
        {
            Haiku haiku = new Haiku(1);
            var   rnd   = new Random(Guid.NewGuid().GetHashCode());
            int   p;

            p = rnd.Next(3); //randomly choose a verse in poem that will be taken from the 2nd parent
            switch (p)       //replace chosen verse
            {
            case 0:
                haiku.v1 = p2.v1;
                haiku.v2 = this.v2;
                haiku.v3 = this.v3;
                break;

            case 1:
                haiku.v1 = this.v1;
                haiku.v2 = p2.v2;
                haiku.v3 = this.v3;
                break;

            case 2:
                haiku.v1 = this.v1;
                haiku.v2 = this.v2;
                haiku.v3 = p2.v3;
                break;
            }

            return(haiku);
        }
        private void Injection()        //injection method of 2 random individuals chosen from database
        {
            Haiku haiku = new Haiku();  //creates new instance of haiku poem with default fitness values

            haiku.pickRandomTxt();      //fills the lines of poem with randomly chosen haiku from the haiku database
            this.population.Add(haiku); //adds this haiku to population

            haiku.pickRandomTxt();      //fills the lines of poem with randomly chosen haiku from the haiku database
            this.population.Add(haiku); //adds this haiku to population
        }
Esempio n. 4
0
        public PopulationOfHaikus()
        {
            this.population = new List <Haiku>();

            for (int i = 0; i < 10; i++)    //population of individuals
            {
                Haiku haiku = new Haiku();  //creates new instance of haiku poem with default fitness values
                this.population.Add(haiku); //adds this haiku to population
            }
        }
Esempio n. 5
0
        private void Injection()     //injection method of 2 random individuals chosen from database
        {
            Haiku h1 = new Haiku();  //creates new instance of haiku poem, selects random from database with default fitness values

            this.population.Add(h1); //adds this haiku to population

            Haiku h2 = new Haiku();  //creates new instance of haiku poem, selects random from database with default fitness values

            this.population.Add(h2); //adds this haiku to population
        }
        }                                   //generation counter

        public PopulationOfHaikus()
        {
            this.population = new List <Haiku>();
            this.generation = 0;

            for (int i = 0; i < 10; i++)    //population of individuals
            {
                Haiku haiku = new Haiku();  //creates new instance of haiku poem with default fitness values
                haiku.pickRandomTxt();      //fills the lines of poem with randomly chosen haiku from the haiku database
                this.population.Add(haiku); //adds this haiku to population
            }
        }
        private void Reproduce(List <Haiku> parents) //reproducing parents, creates 8 new individuals by crossing over parents
        {
            this.population.Clear();
            var   rnd   = new Random(Guid.NewGuid().GetHashCode());
            Haiku child = new Haiku();

            for (int i = 0; i < parents.Count; i++)       //for each parent
            {
                int x = rnd.Next(parents.Count);          //select random second parent

                child = parents[i].crossOver(parents[x]); //perform cross over
                this.population.Add(child);               //add new individual to population

                child = parents[x].crossOver(parents[i]); //perform cross over
                this.population.Add(child);               //add new individual to population
            }
        }