Exemple #1
0
 public Individual saveIndividual(int index, Individual indiv)
 {
     if(index < 0)
     {
         individuals[individuals.Length - 1] = indiv;
     } else
     {
         individuals[index] = indiv;
     }
     return indiv;
 }
Exemple #2
0
 // Calculate inidividuals fittness by comparing it to our candidate solution
 public static double getFitness(Individual individual)
 {
     //fitness is just calculate de function
     //------------------------------------
     _exp.Parameters["x"] = individual.numericValue();
     double result = double.Parse(_exp.Evaluate().ToString());
     if(double.IsPositiveInfinity(result) || double.IsNegativeInfinity(result) || double.IsNaN(result)) {
         return double.Epsilon;
     } else {
         return result;
     }
 }
Exemple #3
0
 public Population(int populationSize, bool initialise)
 {
     individuals = new Individual[populationSize];
     if (initialise)
     {
         for (int i = 0; i<size();i++)
         {
             Individual newIndividual = new Individual();
             newIndividual.generateIndividual();
             saveIndividual(i, newIndividual);
         }
     }
 }
Exemple #4
0
 public static double getProb(Individual individual)
 {
     if(individual.getFitness() != double.Epsilon) {
         double prob = individual.getFitness() / _sumPopulationFitness;
         if (_op == Operation.Max) {
             return prob;
         } else {
             return (1 - prob);
         }
     } else {
         return 0.0;
     }
 }
Exemple #5
0
 public static double getActualCount(Individual individual)
 {
     return Math.Round(getExpectedCount(individual));
 }
Exemple #6
0
 public static double getExpectedCount(Individual individual)
 {
     double expectedCount = individual.getFitness() / _avgPopulationFitness;
     return expectedCount;
 }
Exemple #7
0
 private static bool betterFitness(Individual fittest, int i)
 {
     if (fittest.getFitness() != double.Epsilon) {
         if (_op == Operation.Max) {
             return fittest.getFitness() <= currentPopulation.getIndividual(i).getFitness();
         } else {
             return fittest.getFitness() >= currentPopulation.getIndividual(i).getFitness();
         }
     } else { return false; }
 }
Exemple #8
0
 // Mutate an individual
 private static void mutate(Individual indiv)
 {
     // Loop through genes
     for (int i = 0; i < indiv.size(); i++) {
         if (Rnd.NextDouble() <= MutationRate) {
             // Create random gene
             byte gene = (byte)Math.Round(Rnd.NextDouble());
             indiv.setGene(i, gene);
         }
     }
 }
Exemple #9
0
 // Crossover individuals
 private static Individual crossover(Individual indiv1, Individual indiv2)
 {
     Individual newSol = new Individual();
     // Loop through genes
     for (int i = 0; i < indiv1.size(); i++) {
         // Crossover
         if (Rnd.NextDouble() <= UniformRate) {
             newSol.setGene(i, indiv1.getGene(i));
         } else {
             newSol.setGene(i, indiv2.getGene(i));
         }
     }
     return newSol;
 }
Exemple #10
0
        //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;
        }
Exemple #11
0
 public static bool betterFitness(Individual bestSol, Individual newBestSol)
 {
     if (bestSol.getFitness() != double.Epsilon) {
         if (FitnessCalc._op == Operation.Max) {
             return bestSol.getFitness() <= newBestSol.getFitness();
         } else {
             return bestSol.getFitness() >= newBestSol.getFitness();
         }
     } else { return false; }
 }