コード例 #1
0
        /// <summary>
        /// Mixed version of previous two init methods
        /// </summary>
        /// <param name="size"></param>
        private void HalfHalfInitialization(int size)
        {
            //The max init depth of tree
            int n = gpParameters.maxInitLevel;

            //Faktor ravnomjernog rasporeda dubine hromosoma
            int br = size / (gpParameters.maxInitLevel - 1);

            //Ravomjerna inicijalizacija hromosoma shodno dubini drveta
            for (int i = 2; i <= n; i++)
            {
                if (i == n)//kad i dodje do n onda uzima preostali dio populacije da je inicijalizira
                {
                    br = size - ((i - 2) * br);
                }
                //Promjena dubine drveta shodno metodi Inicijalizacije
                for (int j = 0; j < br; j++)
                {
                    // kreiranje novog hromosoma
                    GPChromosome c = GenerateChromosome(i);

                    // pridruzivanje populaciji
                    population.Add(c);
                }
            }
        }
コード例 #2
0
        void Permutation()
        {
            //Ako je mutacija 0% ne ulazi u petlju
            if (gpParameters.probPermutation == 0)
            {
                return;
            }
            for (int i = 0; i < popSize; i++)
            {
                // generiranje slučajnog broja i provjera vjerojatnosti mutacije
                if (rand.NextDouble() <= gpParameters.probPermutation)
                {
                    // kreiranje novog hromosoma
                    // kloniranje  hromosoma i priprema za mutiranje
                    GPChromosome c = (population[i]).Clone();
                    GPChromosome p = c.Clone();
                    // mutacija
                    PermutateChromosome(c.Root);

                    // pridruživanje mutanta u populaciju
                    if (ParalelComputing)
                    {
                        tempPermutation.Add(c);
                    }
                    else
                    {
                        population.Add(c);
                    }
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Evaluation of chromosome
        /// </summary>
        /// <param name="c"></param>
        private void EvaluateChromosome(GPChromosome c)
        {
            c.Fitness = 0;
            List <int> lst = new List <int>();

            FunctionTree.ToListExpression(lst, c.Root);
            gpParameters.GPFitness.Evaluate(lst, gpFunctionSet, gpTerminalSet, c);
        }
コード例 #4
0
        /// <summary>
        /// Proces of randomly generating chromosome
        /// </summary>
        /// <param name="initLevel"></param>
        /// <returns></returns>
        private GPChromosome GenerateChromosome(int initLevel)
        {
            GPChromosome c = new GPChromosome();

            // Prvi cvor (korijen) generiramo kao funkciju
            GenerateGene(c.Root, true);
            GenerateChromosome(c.Root, initLevel - 1);

            return(c);
        }
コード例 #5
0
 //Generate program strutures with specified level dept
 private void FullInitialization(int size)
 {
     for (int i = 0; i < size; i++)
     {
         // kreiranje novog hromosoma
         GPChromosome c = GenerateChromosome(gpParameters.maxInitLevel);
         //collect chromosome in to population
         population.Add(c);
     }
 }
コード例 #6
0
        private void Crossover()
        {
            if (gpParameters.probCrossover == 0)
            {
                return;
            }
            //for (int i = 1; i < popSize; i += 2)
            if (ParalelComputing)
            {
                Parallel.For(1, popSize / 2, (i) =>
                {
                    // dogadjanje ukrstanja shodno vjerojatnosti
                    if (rand.NextDouble() <= gpParameters.probCrossover)
                    {
                        // kloniranje oba hromosoma i priprema za ukrštanje

                        GPChromosome c1 = (population[i * 2 - 1]).Clone();
                        GPChromosome c2 = (population[i * 2]).Clone();

                        // ukrštanje
                        CrossOverHromosomes(c1.Root, c2.Root);

                        // pridruživanje dva nova potomka u populaciju
                        temCross.Push(c1);
                        temCross.Push(c2);
                    }
                }
                             );
            }
            else
            {
                for (int i = 1; i < popSize; i += 2)
                {
                    // dogadjanje ukrstanja shodno vjerojatnosti
                    if (rand.NextDouble() <= gpParameters.probCrossover)
                    {
                        // kloniranje oba hromosoma i priprema za ukrštanje
                        GPChromosome c1 = (population[i - 1]).Clone();
                        GPChromosome c2 = (population[i]).Clone();

                        // ukrštanje
                        CrossOverHromosomes(c1.Root, c2.Root);

                        // pridruživanje dva nova potomka u populaciju
                        population.Add(c1);
                        population.Add(c2);
                    }
                }
            }
        }
コード例 #7
0
        private void CalculatePopulation()
        {
            // traženje najboljih hromosoma
            fitnessMax = 0;
            fitnessSum = 0;
            if (ParalelComputing)
            {
                var q =
                    (from p in population.AsParallel()
                     orderby p.Fitness descending
                     select p);
                int index = 0;
                foreach (GPChromosome c in q)
                {
                    //The first chromosome is the best
                    if (index == 0)
                    {
                        fitnessMax     = c.Fitness;
                        bestChromosome = c;
                        index++;
                    }
                    fitnessSum += c.Fitness;
                }
                fitnessAvg = fitnessSum / popSize;
            }
            else
            {
                if (bestChromosome == null)
                {
                    bestChromosome = population[0];
                }

                for (int i = 0; i < popSize; i++)
                {
                    float fitness = (population[i]).Fitness;
                    // kakulacija suma funkcija
                    fitnessSum += fitness;

                    // izračuvavanje najboljeg hromosoma
                    if (fitness > fitnessMax)
                    {
                        fitnessMax     = fitness;
                        bestChromosome = population[i];
                    }
                }
                fitnessAvg = fitnessSum / popSize;
            }
        }
コード例 #8
0
        public void Evaluate(List <int> lst, GPFunctionSet gpFunctionSet, GPTerminalSet gpTerminalSet, GPChromosome c)
        {
            c.Fitness = 0;
            double rowFitness = 0.0;
            double SS_tot     = 0.0;
            double y;

            //Translate chromosome to list expressions
            int indexOutput = gpTerminalSet.NumConstants + gpTerminalSet.NumVariables;

            for (int i = 0; i < gpTerminalSet.RowCount; i++)
            {
                // evalue the function
                y = gpFunctionSet.Evaluate(lst, gpTerminalSet, i);
                // check for correct numeric value
                if (double.IsNaN(y) || double.IsInfinity(y))
                {
                    y = 0;
                }

                //Calculate square error
                rowFitness += System.Math.Pow(y - gpTerminalSet.TrainingData[i][indexOutput], 2);
                SS_tot     += System.Math.Pow(gpTerminalSet.TrainingData[i][indexOutput] - gpTerminalSet.AverageValue, 2);
            }

            rowFitness = System.Math.Sqrt(rowFitness / SS_tot);

            if (double.IsNaN(rowFitness) || double.IsInfinity(rowFitness))
            {
                //if output is not a number return zero fitness
                c.Fitness = 0;
                return;
            }

            //Fitness
            c.Fitness = (float)((1.0 / (1.0 + rowFitness / gpTerminalSet.RowCount)) * 1000.0);
        }
コード例 #9
0
        public void Evaluate(List <int> lst, GPFunctionSet gpFunctionSet, GPTerminalSet gpTerminalSet, GPChromosome c)
        {
            c.Fitness = 0;
            double rowFitness = 0.0;
            double val1       = 0;
            double y;
            // copy constants

            //Translate chromosome to list expressions
            int indexOutput = gpTerminalSet.NumConstants + gpTerminalSet.NumVariables;

            for (int i = 0; i < gpTerminalSet.RowCount; i++)
            {
                // evalue the function
                y = gpFunctionSet.Evaluate(lst, gpTerminalSet, i);
                // check for correct numeric value
                if (double.IsNaN(y) || double.IsInfinity(y))
                {
                    y = 0;
                }

                val1 += System.Math.Pow(((y - gpTerminalSet.TrainingData[i][indexOutput]) / gpTerminalSet.TrainingData[i][indexOutput]), 2.0);
            }

            rowFitness = val1 / gpTerminalSet.RowCount;

            if (double.IsNaN(rowFitness) || double.IsInfinity(rowFitness))
            {
                //if output is not a number return infinity fitness
                c.Fitness = 0;
                return;
            }
            //Fitness
            c.Fitness = (float)((1.0 / (1.0 + rowFitness)) * 1000.0);
        }
コード例 #10
0
        public void Evaluate(List <int> lst, GPFunctionSet gpFunctionSet, GPTerminalSet gpTerminalSet, GPChromosome c)
        {
            c.Fitness = 0;
            double rowFitness = 0.0;
            double y, temp;

            //Translate chromosome to list expressions
            int indexOutput = gpTerminalSet.NumConstants + gpTerminalSet.NumVariables;

            for (int i = 0; i < gpTerminalSet.RowCount; i++)
            {
                // evalue the function
                y = gpFunctionSet.Evaluate(lst, gpTerminalSet, i);

                // check for correct numeric value
                if (double.IsNaN(y) || double.IsInfinity(y))
                {
                    y = 0;
                }
                temp        = y - gpTerminalSet.TrainingData[i][indexOutput];
                rowFitness += temp * temp;
            }
            //Fitness
            c.Fitness = (float)((1.0 / (1.0 + rowFitness / indexOutput)) * 1000.0);
        }
コード例 #11
0
        public void Evaluate(List <int> lst, GPFunctionSet gpFunctionSet, GPTerminalSet gpTerminalSet, GPChromosome c)
        {
            c.Fitness = 0;
            double rowFitness = 0.0;

            double CovTP  = 0.0;
            double SigmaP = 0.0;
            double SigmaT = 0.0;

            //number of sample case
            int indexOutput = gpTerminalSet.NumConstants + gpTerminalSet.NumVariables;

            double[] y     = new double[gpTerminalSet.RowCount];
            double   ymean = 0;

            //Calculate output
            for (int i = 0; i < gpTerminalSet.RowCount; i++)
            {
                // evalue the function
                y[i] = gpFunctionSet.Evaluate(lst, gpTerminalSet, i);

                // check for correct numeric value
                if (double.IsNaN(y[i]) || double.IsInfinity(y[i]))
                {
                    //if output is not a number return infinity fitness
                    c.Fitness = 0;
                    return;
                }
                ymean += y[i];
            }

            //calculate AverageValue output
            ymean = ymean / gpTerminalSet.RowCount;

            //Calculate Corelation coeficient
            for (int i = 0; i < gpTerminalSet.RowCount; i++)
            {
                CovTP   = CovTP + ((gpTerminalSet.TrainingData[i][indexOutput] - gpTerminalSet.AverageValue) * (y[i] - ymean));
                SigmaP += System.Math.Pow((y[i] - ymean), 2);
                SigmaT += System.Math.Pow((gpTerminalSet.TrainingData[i][indexOutput] - gpTerminalSet.AverageValue), 2);
            }

            rowFitness = CovTP / (System.Math.Sqrt(SigmaP * SigmaT));

            if (double.IsNaN(rowFitness) || double.IsInfinity(rowFitness))
            {
                //if output is not a number return zero fitness
                c.Fitness = 0;
                return;
            }
            //Fitness
            c.Fitness = (float)(rowFitness * rowFitness * 1000.0);
        }
コード例 #12
0
        /// <summary>
        /// Constructor for population.
        /// </summary>
        /// <param name="size">Size of population.Number must be greather than 0.</param>
        /// <param name="terminalSet">Terminal set.Must be nonnull.</param>
        /// <param name="functionSet">Function set. Must be nonnull.</param>
        /// <param name="parameters">Parameter of GP. If it pass a null value default parameter is initialized.</param>
        /// <param name="paralelComp">True for parallel multy core evaluation. False for secvencial computing.</param>
        public GPPopulation(int size, GPTerminalSet terminalSet,
                            GPFunctionSet functionSet,
                            GPParameters parameters,
                            bool paralelComp)
        {
            ParalelComputing = paralelComp;
            //During population creating the size must be grether than 1,
            //and papameters must be non null
            if (size < 1)
            {
                return;
            }
            //Ako nisu definisani parametri definisi defaultne
            if (parameters == null)
            {
                gpParameters = new GPParameters();
            }
            else
            {
                gpParameters = parameters;
            }

            if (functionSet == null)
            {
                return;
            }
            if (terminalSet == null)
            {
                return;
            }
            //
            gpFunctionSet = functionSet;
            gpTerminalSet = terminalSet;

            //Extend capasity three time cause crossover and mutation
            population = new List <GPChromosome>(3 * size);
            if (ParalelComputing)
            {
                tempCrossover1  = new List <GPChromosome>(size);
                tempCrossover2  = new List <GPChromosome>(size);
                tempMutation    = new List <GPChromosome>();
                tempPermutation = new List <GPChromosome>();
                temCross        = new ConcurrentStack <GPChromosome>();
            }


            //Creating starting Chromosome
            GPChromosome ancestor = new GPChromosome();

            //Initialize population
            InitPopulation(size);

            //Evaluacija pocetne populacije
            if (ParalelComputing)
            {
                EvaluationParallel();
            }
            else
            {
                EvaluationSec();
            }

            //Calculate population statistic parameters
            CalculatePopulation();
        }