/// <summary>
        /// Select two parents, based upon fitness, for a crossover operation.
        /// Place the two children into the new Population.
        /// </summary>
        /// <param name="PopNew">Population to add the newly created program to</param>
        private void Crossover(GPPopulation PopNew, GPFitnessObjectiveBase FitnessSelection)
        {
            GPProgram Child1 = (GPProgram)FitnessSelection.Programs(ExecProgramSelection()).Clone();
            GPProgram Child2 = (GPProgram)FitnessSelection.Programs(ExecProgramSelection()).Clone();

            //
            // Convert to trees
            Child1.ConvertToTree(m_ModelerConfig.FunctionSet, false);
            Child2.ConvertToTree(m_ModelerConfig.FunctionSet, false);

            //
            // Perform the crossover
            m_TreeFactory.Attach(Child1);
            Child2 = m_TreeFactory.Crossover(Child2);

            //
            // Convert to arrays
            Child1.ConvertToArray(m_ModelerConfig.FunctionSet);
            Child2.ConvertToArray(m_ModelerConfig.FunctionSet);

            //
            // Add them to the new Population
            PopNew.Programs.Add(Child1);
            if (PopNew.Count < m_PopCurrent.Count)
            {
                PopNew.Programs.Add(Child2);
            }
        }
        /// <summary>
        /// Select a program based upon fitness to reproduce into the next generation.
        /// </summary>
        /// <param name="PopNew">Population to add the reproduced program into</param>
        private void Reproduce(GPPopulation PopNew, GPFitnessObjectiveBase FitnessSelection)
        {
            GPProgram Copy = (GPProgram)FitnessSelection.Programs(ExecProgramSelection()).Clone();

            //
            // Add this copied child into the new Population
            PopNew.Programs.Add(Copy);
        }
Exemple #3
0
        /// <summary>
        /// Works through the Population to compute the fitness for each program, keeping
        /// track of a few statistics, such as best program, worst fitness and
        /// average fitness.  Makes a call to the abstract method "ComputeFitness" that
        /// must be implemented by all GPFitness derived classes.
        /// </summary>
        /// <param name="Generation">Current modeling generation</param>
        /// <param name="Population">Current generation Population of programs</param>
        /// <returns></returns>
        private int EvaluatePopulation(int Generation, GPPopulation Population)
        {
            //
            // Check to see if the Population size changed, if so, reallocate the array
            // that holds the fitness values
            if (Population.Count != m_PrevPopulationSize)
            {
                if (m_Config.Profile.SPEA2MultiObjective)
                {
                    m_FitnessSelection = new GPFitnessSPEA2(Population.Count);
                }
                else
                {
                    m_FitnessSelection = new GPFitnessSingle(Population.Count);
                }
            }

            FitnessMaximum = 0.0;
            FitnessAverage = 0.0;

            //
            // Go through each program in the Population.  First, compute the raw
            // fitness result of the program.  Once that is done, a call to the
            // abstract ComputeFitness function is made where the User Defined Fitness
            // function is called.
            m_BestProgramGeneration  = 0;
            m_WorstProgramGeneration = 0;
            m_CurrentProgram         = 0;
            m_CompletedPrograms      = 0;
            m_Population             = Population;

            //
            // Create the event to be signed when the last program is done processing
            wh_LastProgramDone = new EventWaitHandle(false, EventResetMode.ManualReset);

            //
            // Signal the suspend event
            wh_Suspend.Set();

            //
            // Wait for the last program to be computed
            wh_LastProgramDone.WaitOne();

            //
            // Finish up the average Population fitness
            FitnessAverage /= Population.Count;

            return(m_BestProgramGeneration);
        }
        /// <summary>
        /// Select a program, based on fitness, for mutation.  Place
        // the mutated program into the new Population.
        /// </summary>
        /// <param name="PopNew">Population to add the newly created program to</param>
        private void Mutate(GPPopulation PopNew, GPFitnessObjectiveBase FitnessSelection)
        {
            GPProgram Copy = (GPProgram)FitnessSelection.Programs(ExecProgramSelection()).Clone();

            //
            // Convert it to a tree
            Copy.ConvertToTree(m_ModelerConfig.FunctionSet, false);

            //
            // Perform the mutation
            m_TreeFactory.Attach(Copy);
            m_TreeFactory.Mutate();

            //
            // Return it back to an array
            Copy.ConvertToArray(m_ModelerConfig.FunctionSet);

            //
            // Add this mutated individual to the new Population
            PopNew.Programs.Add(Copy);
        }