Ejemplo n.º 1
0
        public HSGAIndividual SelectIndividual(List <HSGAIndividual> pop, GeneLogger l)
        {
            /*float winRateSum = 0f;
             * float legalitySum = 0f;
             * float deviationSum = 0f;
             *
             * for(int i = 0; i < pop.Count; i++)
             * {
             *  winRateSum += pop[i].winRateFitness;
             *  legalitySum += pop[i].legalFitness;
             *  deviationSum += pop[i].standardDeviationFitness;
             * }*/

            // check legality fitness first
            // retrieve a new list of individuals which are legal
            // we have no need of using illegal individuals
            // because their overall fitness will be too low.
            List <HSGAIndividual> editedList = new List <HSGAIndividual>();

            for (int i = 0; i < pop.Count; i++)
            {
                if (pop[i].legalFitness < 0)
                {
                    editedList.Add(pop[i]);
                }
            }

            // K tournament selection
            // select 2 to 4 members from current population at random
            List <HSGAIndividual> tourneyList       = new List <HSGAIndividual>();
            HSGAIndividual        currentIndToCheck = new HSGAIndividual();

            int tourneyCount = 4;//rand.Next(1, 5);

            for (int j = 0; j < tourneyCount; j++)
            {
                int index = rand.Next(0, editedList.Count);
                tourneyList.Add(editedList[index]);
            }

            HSGAIndividual selectedParent = new HSGAIndividual();

            for (int p = 0; p < tourneyList.Count; p++)
            {
                currentIndToCheck = tourneyList[p];
                if (selectedParent.cardList == null)
                {
                    selectedParent = currentIndToCheck;
                }

                if (currentIndToCheck.winRateFitness > selectedParent.winRateFitness &&
                    currentIndToCheck.standardDeviationFitness < selectedParent.standardDeviationFitness)
                {
                    selectedParent = currentIndToCheck;
                }
            }

            l.AddToLog("Parent Selected:" + selectedParent.winRateFitness);
            return(selectedParent);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates the initial population, which is given a specified hero class.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GenInitialPopulationButton_Click(object sender, EventArgs e)
        {
            // remove all previous generation files
            DirectoryInfo di = new DirectoryInfo(generationDirectory);

            foreach (DirectoryInfo file in di.EnumerateDirectories())
            {
                file.Delete(true);
            }

            logger.AddToLog("Initialising Logger.");
            logger.AddToLog("Creating Initial Generation.");

            int currentGenerationNum         = 0;
            List <HSGAIndividual> currentPop = new List <HSGAIndividual>();

            Directory.CreateDirectory(generationDirectory + "\\Generation" + currentGenerationNum);
            currentPop = ProduceInitialPopulation(currentGenerationNum);
            ProduceGenerationStats(0, currentPop);

            for (currentGenerationNum = 1; currentGenerationNum < _MaxGenerations; currentGenerationNum++)
            {
                mutationProbability -= 0.01f;
                newPopulation.Clear();
                Directory.CreateDirectory(generationDirectory + "\\Generation" + currentGenerationNum);
                newPopulation = ProduceNextGeneration(currentPop);
                currentPop.Clear();
                currentPop.AddRange(newPopulation);

                logger.AddToLog("Beginning testing of Generation " + currentGenerationNum);

                TestNewPopulation(currentPop, currentGenerationNum);
                ProduceGenerationStats(currentGenerationNum, currentPop);
            }
            if (currentGenerationNum == 50)
            {
                MessageBox.Show("Generation Complete.");
            }
        }
Ejemplo n.º 3
0
        public List <HSGAIndividual> Crossover(List <HSGAIndividual> parents, float crossoverProb, GeneLogger l)
        {
            // child 1 left = parent 1, right = parent 2
            // child 2 left = parent 2, right = parent 1
            HSGAIndividual child1 = new HSGAIndividual();

            child1.cardList = new List <Card>();
            HSGAIndividual child2 = new HSGAIndividual();

            child2.cardList = new List <Card>();

            List <HSGAIndividual> children = new List <HSGAIndividual>();



            /* for(int i = 0; i < 30; i++)
             * {
             *   Card c = new Card("", "", "", "", "", "", "");
             *   child1.cardList.Add(c);
             *   child2.cardList.Add(c);
             * }*/

            if (rand.NextDouble() <= crossoverProb)
            {
                l.AddToLog("Performing Crossover.");
                int point1 = rand.Next(0, parents[0].cardList.Count);

                if (point1 == 0)
                {
                    point1 = rand.Next(0, parents[0].cardList.Count);
                }

                // need to replace item in list
                for (int i = 0; i <= point1; i++)
                {
                    child1.cardList.Add(parents[0].cardList[i]);
                    child2.cardList.Add(parents[1].cardList[i]);
                }
                for (int i = point1 + 1; i < parents[0].cardList.Count; i++)
                {
                    // set index to 1 above crossover point so we dont replace
                    // that card which is set in the previous for loop
                    child1.cardList.Add(parents[1].cardList[i]);
                    child2.cardList.Add(parents[0].cardList[i]);
                }

                children.Add(child1);
                children.Add(child2);
            }
            else
            {
                // coin flip to determine which parent to clone if
                // crossover is not chosen.
                l.AddToLog("Not Performing Crossover.");
                //Console.WriteLine("Not Performing Crossover.");
                if (rand.NextDouble() > 0.5f)
                {
                    child1.cardList = parents[0].cardList;
                    child2.cardList = parents[0].cardList;
                }
                else
                {
                    child1.cardList = parents[1].cardList;
                    child2.cardList = parents[1].cardList;
                }

                children.Add(child1);
                children.Add(child2);
            }



            return(children);
        }