|
| 1 | +package com.baeldung.algorithms.ga.binary; |
| 2 | + |
| 3 | +import lombok.Data; |
| 4 | + |
| 5 | +@Data |
| 6 | +public class SimpleGeneticAlgorithm { |
| 7 | + |
| 8 | + private static final double uniformRate = 0.5; |
| 9 | + private static final double mutationRate = 0.025; |
| 10 | + private static final int tournamentSize = 5; |
| 11 | + private static final boolean elitism = true; |
| 12 | + private static byte[] solution = new byte[64]; |
| 13 | + |
| 14 | + public static boolean runAlgorithm(int populationSize, String solution) { |
| 15 | + if (solution.length() != SimpleGeneticAlgorithm.solution.length) { |
| 16 | + throw new RuntimeException( |
| 17 | + "The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes"); |
| 18 | + } |
| 19 | + SimpleGeneticAlgorithm.setSolution(solution); |
| 20 | + Population myPop = new Population(populationSize, true); |
| 21 | + |
| 22 | + int generationCount = 1; |
| 23 | + while (myPop.getFittest().getFitness() < SimpleGeneticAlgorithm.getMaxFitness()) { |
| 24 | + System.out.println( |
| 25 | + "Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness()); |
| 26 | + myPop = SimpleGeneticAlgorithm.evolvePopulation(myPop); |
| 27 | + generationCount++; |
| 28 | + } |
| 29 | + System.out.println("Solution found!"); |
| 30 | + System.out.println("Generation: " + generationCount); |
| 31 | + System.out.println("Genes: "); |
| 32 | + System.out.println(myPop.getFittest()); |
| 33 | + return true; |
| 34 | + } |
| 35 | + |
| 36 | + public static Population evolvePopulation(Population pop) { |
| 37 | + int elitismOffset; |
| 38 | + Population newPopulation = new Population(pop.getIndividuals().size(), false); |
| 39 | + |
| 40 | + if (elitism) { |
| 41 | + newPopulation.getIndividuals().add(0, pop.getFittest()); |
| 42 | + elitismOffset = 1; |
| 43 | + } else { |
| 44 | + elitismOffset = 0; |
| 45 | + } |
| 46 | + |
| 47 | + for (int i = elitismOffset; i < pop.getIndividuals().size(); i++) { |
| 48 | + Individual indiv1 = tournamentSelection(pop); |
| 49 | + Individual indiv2 = tournamentSelection(pop); |
| 50 | + Individual newIndiv = crossover(indiv1, indiv2); |
| 51 | + newPopulation.getIndividuals().add(i, newIndiv); |
| 52 | + } |
| 53 | + |
| 54 | + for (int i = elitismOffset; i < newPopulation.getIndividuals().size(); i++) { |
| 55 | + mutate(newPopulation.getIndividual(i)); |
| 56 | + } |
| 57 | + |
| 58 | + return newPopulation; |
| 59 | + } |
| 60 | + |
| 61 | + private static Individual crossover(Individual indiv1, Individual indiv2) { |
| 62 | + Individual newSol = new Individual(); |
| 63 | + for (int i = 0; i < newSol.getDefaultGeneLength(); i++) { |
| 64 | + if (Math.random() <= uniformRate) { |
| 65 | + newSol.setSingleGene(i, indiv1.getSingleGene(i)); |
| 66 | + } else { |
| 67 | + newSol.setSingleGene(i, indiv2.getSingleGene(i)); |
| 68 | + } |
| 69 | + } |
| 70 | + return newSol; |
| 71 | + } |
| 72 | + |
| 73 | + private static void mutate(Individual indiv) { |
| 74 | + for (int i = 0; i < indiv.getDefaultGeneLength(); i++) { |
| 75 | + if (Math.random() <= mutationRate) { |
| 76 | + byte gene = (byte) Math.round(Math.random()); |
| 77 | + indiv.setSingleGene(i, gene); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static Individual tournamentSelection(Population pop) { |
| 83 | + Population tournament = new Population(tournamentSize, false); |
| 84 | + for (int i = 0; i < tournamentSize; i++) { |
| 85 | + int randomId = (int) (Math.random() * pop.getIndividuals().size()); |
| 86 | + tournament.getIndividuals().add(i, pop.getIndividual(randomId)); |
| 87 | + } |
| 88 | + Individual fittest = tournament.getFittest(); |
| 89 | + return fittest; |
| 90 | + } |
| 91 | + |
| 92 | + protected static int getFitness(Individual individual) { |
| 93 | + int fitness = 0; |
| 94 | + for (int i = 0; i < individual.getDefaultGeneLength() && i < solution.length; i++) { |
| 95 | + if (individual.getSingleGene(i) == solution[i]) { |
| 96 | + fitness++; |
| 97 | + } |
| 98 | + } |
| 99 | + return fitness; |
| 100 | + } |
| 101 | + |
| 102 | + protected static int getMaxFitness() { |
| 103 | + int maxFitness = solution.length; |
| 104 | + return maxFitness; |
| 105 | + } |
| 106 | + |
| 107 | + protected static void setSolution(String newSolution) { |
| 108 | + solution = new byte[newSolution.length()]; |
| 109 | + for (int i = 0; i < newSolution.length(); i++) { |
| 110 | + String character = newSolution.substring(i, i + 1); |
| 111 | + if (character.contains("0") || character.contains("1")) { |
| 112 | + solution[i] = Byte.parseByte(character); |
| 113 | + } else { |
| 114 | + solution[i] = 0; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments