Exemplo 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);
        }
Exemplo n.º 2
0
        public Form1()
        {
            InitializeComponent();
            JSONHandler       = new CardJsonManager();
            currentPopulation = new List <HSGAIndividual>();
            newPopulation     = new List <HSGAIndividual>();
            rand   = new Random();
            logger = new GeneLogger(listBox1);

            // deserialize all cards on startup
            numOfFiles = Directory.GetDirectories(initialDirectory, "*", SearchOption.TopDirectoryOnly).Length;
            //deserialize all in directory
            JSONHandler.GetAllCards(numOfFiles, initialDirectory);
            JSONHandler.filePath = deckDirectory;
            //enable/disable the other buttons
            TestValidationButton.Enabled = true;
            GenButton.Enabled            = true;
        }
Exemplo 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);
        }