123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package algorithms.geneticAlgorithm.Components;
- import java.util.ArrayList;
- import java.util.Random;
- import algorithms.geneticAlgorithm.holegGA.Components.HolegIndividual;
- public class GeneticAlgo<I extends HolegIndividual> {
- public int popSize;
- public ArrayList<I> population;
-
- public GAIndividualFactory<I> randomFactory;
- public GASelectionStrategy<I> selector;
- public GACrossoverStrategy<I> reproducer;
- public GAMutationStrategy<I> mutator;
- public GAFittnessFunctionStrategy<I> fittnessFunction;
- public double topPercent;
- public double buttomPercent;
-
- public GeneticAlgo(GASelectionStrategy<I> selection, GACrossoverStrategy<I> crossover, GAMutationStrategy<I> mutator,
- GAFittnessFunctionStrategy<I> fittnessFkt, GAIndividualFactory<I> factory, int popSize){
-
- this.selector = selection;
- this.reproducer = crossover;
- this.mutator = mutator;
- this.fittnessFunction = fittnessFkt;
- randomFactory = factory;
- this.popSize = popSize;
-
- population = new ArrayList<I>();
- topPercent = 0.0;
-
- //generateRandomPopulation();
- }
- public void generateRandomPopulation() {
- population = new ArrayList<I>();
- for(int i = 0; i < popSize; i++){
- population.add(randomFactory.createRandomIndividual());
- fittnessFunction.calculateFittness(population.get(i));
- }
- }
-
- public void createNextGeneration(){
- ArrayList<I> nextGen = new ArrayList<I>();
-
-
- int topBound = (int) Math.ceil(topPercent * popSize);
- //int topBound = 1;
- population = getSortedPopulation();
-
- for(I individual : population.subList(0, topBound)){
- ArrayList<I> childs = new ArrayList<I>();
- childs.add(individual);
- childs.add(individual);
- childs = reproducer.crossOver(childs);
- for(I i : childs){
- i.addLogEntry("Top % from previous gen");
- fittnessFunction.calculateFittness(i);
- nextGen.add(i);
- }
- }
-
- selector.setCurrentPopulation(population);
- while(nextGen.size() < popSize){
- ArrayList<I> freshInds = new ArrayList<I>();
- freshInds = reproducer.crossOver(selector.selectIndividuals());
- for(I i : freshInds){
- i = mutator.mutateIndividual(i);
- fittnessFunction.calculateFittness(i);
- }
- nextGen.addAll(freshInds);
- }
-
- if(popSize % 2 == 1){
- Random rnd = new Random();
- nextGen.remove(rnd.nextInt(popSize));
- }
- population = nextGen;
- }
-
- public ArrayList<I> getPopulation(){
- return population;
- }
-
- public ArrayList<I> getSortedPopulation(){
- return sortPopulation(population);
- }
-
- public ArrayList<I> sortPopulation(ArrayList<I> individuals){
- ArrayList<I> sortedList = new ArrayList<I>();
- for(I hI : individuals){
- for(int i = 0; i <= sortedList.size(); i++){
- if(i == sortedList.size()){
- sortedList.add(hI);
- break;
- }else if(sortedList.get(i).getFittness() < hI.getFittness()){
- sortedList.add(i, hI);
- break;
- }
- }
- }
- return sortedList;
- }
- }
|