Beispiel #1
0
        /// <summary>
        /// This Mating Pool Selector returns a pool of the fittest entities from the supplied population.
        /// </summary>
        /// <param name="pPopulation"></param>
        /// <param name="pSize"></param>
        /// <returns>List of GeneticEntity</returns>
        public List <T> SelectPool <T>(List <T> pPopulation, int pSize) where T : GeneticEntity
        {
            T[] matingArray = pPopulation.ToArray();
            TimSort <T> .sort(matingArray, new GeneticEntityComparable());

            return(new List <T>(matingArray));
        }
Beispiel #2
0
        /// <summary>
        /// This Breeding Pair Selector returns pairings descend sequentially in fitness.
        /// </summary>
        /// <param name="pPopulation"></param>
        /// <returns>List of GeneticEntity</returns>
        public List <T> SelectPairs <T>(List <T> pPopulation) where T : GeneticEntity
        {
            T[] popArray = pPopulation.ToArray();
            TimSort <T> .sort(popArray, new GeneticEntityComparable());

            return(new List <T>(popArray));
        }
        /// <summary>
        /// This Breeding Pair Selector returns pairings where the even numbers alternate between the two fittest parents, while the odd numbers descend sequentially through the remaining fittest entities.
        /// </summary>
        /// <param name="pPopulation"></param>
        /// <returns>List of GeneticEntity</returns>
        public List <T> SelectPairs <T>(List <T> pPopulation) where T : GeneticEntity
        {
            //Create a new list to return
            List <T> pairGrouping = new List <T>();

            T[] popArray = pPopulation.ToArray();
            //Sort by fittest
            TimSort <T> .sort(popArray, new GeneticEntityComparable());

            //0A     1B
            //2A     3C
            //4B     5D
            //6A     7E
            //8B     9F


            //Initial princely load
            pairGrouping.Add(popArray[0]);
            pairGrouping.Add(popArray[1]);

            int princelyOffset = 2;

            //The first of the remaining population after the princes
            int index = princelyOffset + 1;

            int lengthOfPairing = (popArray.Length - princelyOffset) * 2;

            for (int i = princelyOffset; i < lengthOfPairing; i++)
            {
                //If even
                if (i % 2 == 0)
                {
                    index = 0; //Prince A

                    //If every second even
                    if (i % 4 == 0)
                    {
                        index = 1; //Prince B
                    }
                }
                else
                {
                    index++;
                }

                pairGrouping.Add(popArray[index]);
            }


            return(pairGrouping);
        }
        /// <summary>
        /// This Breeding Pair Selector returns pairings where the even numbers are always the fittest entity of the entire pool (the alpha) while odd numbers contain the next most fit entity descending sequentially. The first pairing contains two copies of the alpha and will therefore result in an asexually reproduced offspring.
        /// </summary>
        /// <param name="pPopulation"></param>
        /// <returns>List of GeneticEntity</returns>
        public List <T> SelectPairs <T>(List <T> pPopulation) where T : GeneticEntity
        {
            //Create a new list to return
            List <T> pairGrouping = new List <T>();

            T[] popArray = pPopulation.ToArray();
            //Sort by fittest
            TimSort <T> .sort(popArray, new GeneticEntityComparable());

            //On even numbers we will always choose the fittest entity. On odd numbers we will chose
            //the next fittest entity in the pool
            for (int i = 0; i < popArray.Length; i++)
            {
                int index = i % 2 == 0 ?
                            0 :
                            i / 2;

                pairGrouping.Add(popArray[index]);
            }
            return(pairGrouping);
        }
Beispiel #5
0
        /// <summary>
        /// This Breeding Pair Selector returns pairings where the even numbers are the given mating pool's fittest and the odd are its least fit. This grouping is used to pair the fittest entities with the least fit together.
        /// </summary>
        /// <param name="pPopulation"></param>
        /// <returns>List of GeneticEntity</returns>
        public List <T> SelectPairs <T>(List <T> pPopulation) where T : GeneticEntity
        {
            //Create a new list to return
            List <T> pairGrouping = new List <T>();

            T[] popArray = pPopulation.ToArray();
            //Sort our pool by entity fitness
            TimSort <T> .sort(popArray, new GeneticEntityComparable());

            //Loop through the pool
            for (int i = 0; i < popArray.Length; i++)
            {
                //Our chosen index will be i / 2 (the fittest) on even numbers while the least fittest
                //on odd numbers, using list.count - 1 (accounting for zero-indexing) minus
                //the loop's iteration
                int index = i % 2 == 0 ?
                            (i / 2) :
                            (popArray.Length - 1) - i;

                pairGrouping.Add(popArray[index]);
            }
            return(pairGrouping);
        }
Beispiel #6
0
    //Subscribed to PlotStart, which is queued in UIReflect after UI updates (which is queued StartEvolve)
    private void EvolvePopulation(object sender, EventArgs e)
    {
        TimeLogger.LogStart();
        float averageFitness = 0.0f;

        List <Bed> evolvedBeds = new List <Bed>();

        //Actual Evolve process
        for (int g = 0; g < givenIterations; g++)
        {
            //New beds get inited from rand conditions for the first gen
            //Should ideally ahve already been done from Start -> ResetPopulation, just a safety measure
            if (!initialised)
            {
                newBeds = CreateBeds();
            }

            //The previous List gets emptied. This data is now stored in newBeds after the last generation
            if (initialised)
            {
            }

            /*for (int i = 0; i < beds.Count; i++)
             * {
             *  beds[i].ComputeFitness();
             * }*/
            if (evolvedBeds == null)
            {
                //Debug.Log("Evolved null");
            }
            if (newBeds == null)
            {
                //Debug.Log("new beds null");
            }

            //Use the GA to evolve our data
            evolvedBeds = GeneticEvolution.Evolve(newBeds, newBeds.Count, testbench.GetUsedMatingPoolSelector(),
                                                  testbench.GetUsedBreedingPairSelector(),
                                                  testbench.GetUsedCrossoverOperator(),
                                                  testbench.GetUsedMutator());

            for (int i = 0; i < evolvedBeds.Count; i++)
            {
                evolvedBeds[i].LoadGardenAndPatches(this, patches);
            }
            generationNumber++;

            //Debug.Log("Unsorted List " + beds[0].Fitness);
            Bed[] bedArray = evolvedBeds.ToArray();
            TimSort <Bed> .sort(bedArray, new GeneticEntityComparable());

            /* for (int i = 0; i < bedArray.Length; i++)
             * {
             *   Debug.Log("Sorted post-gen i: " + i + " Fitness: " + bedArray[i].Fitness);
             * }*/
            //Debug.Log("Top of Array: " + bedArray[0].Fitness);
            evolvedBeds = new List <Bed>(bedArray);

            //Trimming the solutions
            //Debug.Log("Evolved Beds pre-trim - size: " + evolvedBeds.Count);

            //Certain Pair Selectors create larger numbers of solutions than the intial set of parents.
            //In this case, we require as many solutions (beds) as we put in and therefore remove the excess
            //after they've been fitness sorted
            if (evolvedBeds.Count > numberOfBeds)
            {
                evolvedBeds.RemoveRange(numberOfBeds - 1, (evolvedBeds.Count - numberOfBeds));
            }
            //Debug.Log("Evolved Beds post-trim - size: " + evolvedBeds.Count);


            averageFitness = 0.0f;
            for (int i = 0; i < evolvedBeds.Count; i++)
            {
                averageFitness += evolvedBeds[i].Fitness;
                // Debug.Log("Beds [" + i + "]:" + beds[i].Fitness);
            }
            averageFitness /= evolvedBeds.Count;

            //Clear out the starting beds
            newBeds.Clear();

            //Save the evolved beds into a new starting List to be used for the next generation
            for (int i = 0; i < evolvedBeds.Count; i++)
            {
                newBeds.Add(evolvedBeds[i]);
            }

            //Clear out the evolved one
            evolvedBeds.Clear();

            //Debug.Log("End of gen loop - eb size: " + evolvedBeds.Count + " | nb: " + newBeds.Count);
        }


        TimeLogger.LogEnd();
        EventQueue.QueueEvent(EventQueue.EventType.Plot_End, this, new TimeArgs(Time.realtimeSinceStartup));
        //We use newBeds not evolvedBeds here since at the end of the gen, new holds the next (or last) generation's data
        //and evolved is already cleared for the next gen
        EventQueue.QueueEvent(EventQueue.EventType.BroadcastGeneration, this, new GenerationArgs <Bed>(newBeds, newBeds[0].Fitness, averageFitness, generationNumber, testingBase));
    }
        /// <summary>
        /// This Breeding Pair Selector returns pairings where the first half of the even numbers contains the most fit parent, while the latter half contains the second most fit parent. The odd numbers descend sequentially through the remaining fittest entities.
        /// </summary>
        /// <param name="pPopulation"></param>
        /// <returns>List of GeneticEntity</returns>
        public List <T> SelectPairs <T>(List <T> pPopulation) where T : GeneticEntity
        {
            //Create a new list to return
            List <T> pairGrouping = new List <T>();

            T[] popArray = pPopulation.ToArray();

            //Sort by fittest
            TimSort <T> .sort(popArray, new GeneticEntityComparable());

            //0A     1C
            //2A     3D
            //4A     5E
            //6B     7F
            //8B     9G
            //10B    11H

            //Debug.Log("pairGrouping size is: " + pairGrouping.Count);
            //Partner index is for the non-prince partner of the breeding pair
            int partnerIndex = princeOffset;

            //Prince index controls which prince is plugged in
            int princeIndex = 0;

            //Index is what gets plugged to the list
            int index = 0;

            //The pairing should exclude the two princes, and be equal to the remaining partners x2
            //(since each partner needs the following slot to be filled by a prince)
            int lengthOfPairing = (popArray.Length - partnerIndex) * 2;

            //Debug.Log("poparray: " + popArray.Length);
            for (int i = 0; i < lengthOfPairing; i++)
            {
                //int index = i + 2;

                //To avoid duplication, we assume partner status and negate and switch to prince if need be
                index = partnerIndex;

                //If odd
                if (i % 1 == 0)
                {
                    //First half of pool
                    if (i < popArray.Length / 2)
                    {
                        princeIndex = 0; //Prince A
                    }
                    //Latter half of pool
                    else
                    {
                        princeIndex = 1; //Prince B
                    }
                    index = princeIndex;
                }
                else
                {
                    partnerIndex++;
                }



                //Debug.Log("i is: " + i + " while index is: " + index);
                //There is an unfixed OutOfRangeException here
                pairGrouping.Add(popArray[index]);
            }


            return(pairGrouping);
        }
Beispiel #8
0
 public void Sort()
 {
     TimSort <XPathItem> .sort(_items, 0, _size, new XPathComparer());
 }