コード例 #1
0
ファイル: Algorithm.cs プロジェクト: PabloSzPz/sGA
        // Select individuals for crossover
        private static Individual rouletteSelection(Population pop)
        {
            double[] weights = new double[pop.size()];
            for (int i = 0; i < pop.size(); i++)
            {
                weights[i] = FitnessCalc.getProb(pop.getIndividual(i));
            }

            double weight_sum = 0;

            for (int i = 0; i < weights.Length; i++)
            {
                weight_sum += weights[i];
            }
            // get a random value
            double value = randUniformPositive() * weight_sum;

            // locate the random value based on the weights
            for (int i = 0; i < weights.Length; i++)
            {
                value -= weights[i];
                if (value <= 0)
                {
                    return(pop.getIndividual(i));
                }
            }
            // only when rounding errors occur
            return(pop.getIndividual(weights.Length - 1));
        }
コード例 #2
0
        public static Individual getFittest()
        {
            Individual fittest = currentPopulation.getIndividual(0);

            for (int i = 0; i < currentPopulation.size(); i++)
            {
                if (betterFitness(fittest, i))
                {
                    fittest = currentPopulation.getIndividual(i);
                }
            }
            return(fittest);
        }
コード例 #3
0
ファイル: Algorithm.cs プロジェクト: PabloSzPz/sGA
        //Evolve population
        public static Population evolvePopulation(Population pop)
        {
            Population newPopulation    = new Population(pop.size(), false);
            Individual fittestOfThisPop = null;

            //keep your best individual
            if (Elitism)
            {
                //get fittest of the CURRENT pop
                fittestOfThisPop = newPopulation.saveIndividual(0, FitnessCalc.getFittest());

                //SAVE best of the algorithm
                if (bestSolution != null)
                {
                    if (betterFitness(bestSolution, fittestOfThisPop))
                    {
                        bestSolution = fittestOfThisPop;
                    }
                }
                else
                {
                    bestSolution = fittestOfThisPop;
                }
            }

            //Crossover population
            int elitismOffset;

            if (Elitism)
            {
                elitismOffset = 1;
            }
            else
            {
                elitismOffset = 0;
            }

            //loop over tje population size and create new individuals with crossover
            for (int i = elitismOffset; i < pop.size(); i++)
            {
                Individual indiv1   = rouletteSelection(pop);
                Individual indiv2   = rouletteSelection(pop);
                Individual newIndiv = crossover(indiv1, indiv2);
                newPopulation.saveIndividual(i, newIndiv);
            }

            //Mutate population
            for (int i = elitismOffset; i < newPopulation.size(); i++)
            {
                mutate(newPopulation.getIndividual(i));
            }

            // fitless die & replace by fittest
            if (DieFactor)
            {
                if (elitism)
                {
                    newPopulation.saveIndividual(FitnessCalc.getFitlessIndex(), fittestOfThisPop);
                }
                else
                {
                    newPopulation.saveIndividual(FitnessCalc.getFitlessIndex(), FitnessCalc.getFittest());
                }
            }



            return(newPopulation);
        }
コード例 #4
0
ファイル: Algorithm.cs プロジェクト: PabloSzPz/sGA
        // Select individuals for crossover
        private static Individual rouletteSelection(Population pop)
        {
            double[] weights = new double[pop.size()];
            for(int i =0; i < pop.size(); i++) {
                weights[i] = FitnessCalc.getProb(pop.getIndividual(i));
            }

            double weight_sum = 0;
            for (int i = 0; i < weights.Length; i++) {
                weight_sum += weights[i];
            }
            // get a random value
            double value = randUniformPositive() * weight_sum;
            // locate the random value based on the weights
            for (int i = 0; i < weights.Length; i++) {
                value -= weights[i];
                if (value <= 0) return pop.getIndividual(i);
            }
            // only when rounding errors occur
            return pop.getIndividual(weights.Length - 1);
        }
コード例 #5
0
ファイル: Algorithm.cs プロジェクト: PabloSzPz/sGA
        //Evolve population
        public static Population evolvePopulation(Population pop)
        {
            Population newPopulation = new Population(pop.size(), false);
            Individual fittestOfThisPop = null;
            //keep your best individual
            if (Elitism) {
                //get fittest of the CURRENT pop
                fittestOfThisPop = newPopulation.saveIndividual(0, FitnessCalc.getFittest());

                //SAVE best of the algorithm
                if(bestSolution != null) {
                    if (betterFitness(bestSolution, fittestOfThisPop)) {
                        bestSolution = fittestOfThisPop;
                    }
                } else {
                    bestSolution = fittestOfThisPop;
                }
            }

            //Crossover population
            int elitismOffset;
            if (Elitism) {
                elitismOffset = 1;
            } else {
                elitismOffset = 0;
            }

            //loop over tje population size and create new individuals with crossover
            for(int i = elitismOffset; i<pop.size();i++) {
                Individual indiv1 = rouletteSelection(pop);
                Individual indiv2 = rouletteSelection(pop);
                Individual newIndiv = crossover(indiv1, indiv2);
                newPopulation.saveIndividual(i, newIndiv);
            }

            //Mutate population
            for (int i=elitismOffset; i<newPopulation.size();i++) {
                mutate(newPopulation.getIndividual(i));
            }

            // fitless die & replace by fittest
            if (DieFactor) {
                if (elitism) {
                    newPopulation.saveIndividual(FitnessCalc.getFitlessIndex(), fittestOfThisPop);
                } else {
                    newPopulation.saveIndividual(FitnessCalc.getFitlessIndex(), FitnessCalc.getFittest());
                }
            }

            return newPopulation;
        }