|
| 1 | +package chapter5; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Random; |
| 7 | + |
| 8 | +public class GeneticAlgorithm<C extends Chromosome<C>> { |
| 9 | + |
| 10 | + public enum SelectionType { |
| 11 | + ROULETTE, TOURNAMENT; |
| 12 | + } |
| 13 | + |
| 14 | + private ArrayList<C> population; |
| 15 | + private double mutationChance; |
| 16 | + private double crossoverChance; |
| 17 | + private SelectionType selectionType; |
| 18 | + private Random random; |
| 19 | + |
| 20 | + public GeneticAlgorithm(List<C> initialPopulation, |
| 21 | + double mutationChance, double crossoverChance, SelectionType selectionType) { |
| 22 | + this.population = new ArrayList<>(initialPopulation); |
| 23 | + this.mutationChance = mutationChance; |
| 24 | + this.crossoverChance = crossoverChance; |
| 25 | + this.selectionType = selectionType; |
| 26 | + this.random = new Random(); |
| 27 | + } |
| 28 | + |
| 29 | + // Use the probability distribution wheel to pick numPicks individuals |
| 30 | + private List<C> pickRoulette(double[] wheel, int numPicks) { |
| 31 | + List<C> picks = new ArrayList<>(); |
| 32 | + for (int i = 0; i < numPicks; i++) { |
| 33 | + double pick = random.nextDouble(); |
| 34 | + for (int j = 0; j < wheel.length; j++) { |
| 35 | + pick -= wheel[j]; |
| 36 | + if (pick <= 0) { // we had one that took us over, leads to a pick |
| 37 | + picks.add(population.get(j)); |
| 38 | + break; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + return picks; |
| 43 | + } |
| 44 | + |
| 45 | + // Pick a certain number of individuals via a tournament |
| 46 | + private List<C> pickTournament(int numParticipants, int numPicks) { |
| 47 | + // Find numParticipants random participants to be in the tournament |
| 48 | + Collections.shuffle(population); |
| 49 | + List<C> tournament = population.subList(0, numParticipants); |
| 50 | + // Find the numPicks highest fitnesses in the tournament |
| 51 | + Collections.sort(tournament, Collections.reverseOrder()); |
| 52 | + return tournament.subList(0, numPicks); |
| 53 | + } |
| 54 | + |
| 55 | + // Replace the population with a new generation of individuals |
| 56 | + private void reproduceAndReplace() { |
| 57 | + ArrayList<C> nextPopulation = new ArrayList<>(); |
| 58 | + // keep going until we've filled the new generation |
| 59 | + while (nextPopulation.size() < population.size()) { |
| 60 | + // pick the two parents |
| 61 | + List<C> parents; |
| 62 | + if (selectionType == SelectionType.ROULETTE) { |
| 63 | + // create the probability distribution wheel |
| 64 | + double totalFitness = population.stream().mapToDouble(C::fitness).sum(); |
| 65 | + double[] wheel = population.stream() |
| 66 | + .mapToDouble(C -> C.fitness() / totalFitness).toArray(); |
| 67 | + parents = pickRoulette(wheel, 2); |
| 68 | + } else { // tournament |
| 69 | + parents = pickTournament(population.size() / 2, 2); |
| 70 | + } |
| 71 | + // potentially crossover the 2 parents |
| 72 | + if (random.nextDouble() < crossoverChance) { |
| 73 | + C parent1 = parents.get(0); |
| 74 | + C parent2 = parents.get(1); |
| 75 | + nextPopulation.addAll(parent1.crossover(parent2)); |
| 76 | + } else { // just add the two parents |
| 77 | + nextPopulation.addAll(parents); |
| 78 | + } |
| 79 | + } |
| 80 | + // if we have an odd number, we'll have 1 exra, so we remove it |
| 81 | + if (nextPopulation.size() > population.size()) { |
| 82 | + nextPopulation.remove(0); |
| 83 | + } |
| 84 | + population = nextPopulation; // replace the reference/generation |
| 85 | + } |
| 86 | + |
| 87 | + // With mutationChance probability, mutate each individual |
| 88 | + private void mutate() { |
| 89 | + for (C individual : population) { |
| 90 | + if (random.nextDouble() < mutationChance) { |
| 91 | + individual.mutate(); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Run the genetic algorithm for maxGenerations iterations |
| 97 | + // and return the best individual found |
| 98 | + public C run(int maxGenerations, double threshold) { |
| 99 | + C best = Collections.max(population).copy(); |
| 100 | + for (int generation = 0; generation < maxGenerations; generation++) { |
| 101 | + // early exit if we beat threshold |
| 102 | + if (best.fitness() >= threshold) { |
| 103 | + return best; |
| 104 | + } |
| 105 | + // Debug printout |
| 106 | + System.out.println("Generation " + generation + |
| 107 | + " Best " + best.fitness() + |
| 108 | + " Avg " + population.stream() |
| 109 | + .mapToDouble(C::fitness).average().orElse(0.0)); |
| 110 | + reproduceAndReplace(); |
| 111 | + mutate(); |
| 112 | + C highest = Collections.max(population); |
| 113 | + if (highest.fitness() > best.fitness()) { |
| 114 | + best = highest.copy(); |
| 115 | + } |
| 116 | + } |
| 117 | + return best; |
| 118 | + } |
| 119 | +} |
0 commit comments