Browse Source

integration of genetic Algo framework into holeg Project

Dominik Rieder 6 years ago
parent
commit
1dd5ba6de8

+ 31 - 0
src/algorithms/geneticAlgorithm/Components/GACrossoverStrategy.java

@@ -0,0 +1,31 @@
+package algorithms.geneticAlgorithm.Components;
+
+import java.util.ArrayList;
+import java.util.Random;
+
+public abstract class GACrossoverStrategy<I extends GAIndividual> {
+	
+	protected double crossProb;
+	protected Random rng;
+	
+	public GACrossoverStrategy(double prob){
+		crossProb = prob;
+		rng = new Random();
+	}
+	
+	public GACrossoverStrategy(){
+		crossProb = 0.7;
+		rng = new Random();
+	}
+	
+	public abstract ArrayList<I> crossOver(ArrayList<I> parents);
+		
+	public void setCrossoverProbability(double prob){
+		crossProb = prob;
+	}
+	
+	public double getCrossoverProbability(){
+		return crossProb;
+	}
+	
+}

+ 8 - 0
src/algorithms/geneticAlgorithm/Components/GAFittnessFunctionStrategy.java

@@ -0,0 +1,8 @@
+package algorithms.geneticAlgorithm.Components;
+
+
+public interface GAFittnessFunctionStrategy<I extends GAIndividual>{
+	
+	public double calculateFittness(I candidate);
+
+}

+ 14 - 0
src/algorithms/geneticAlgorithm/Components/GAIndividual.java

@@ -0,0 +1,14 @@
+package algorithms.geneticAlgorithm.Components;
+
+public class GAIndividual {
+	
+	double fittness;
+	
+	public double getFittness(){
+		return fittness;
+	}
+	
+	public void setFittness(double fittness){
+		this.fittness = fittness;
+	}
+}

+ 8 - 0
src/algorithms/geneticAlgorithm/Components/GAIndividualFactory.java

@@ -0,0 +1,8 @@
+package algorithms.geneticAlgorithm.Components;
+
+
+public interface GAIndividualFactory<I extends GAIndividual> {
+	
+	public I createRandomIndividual();
+
+}

+ 30 - 0
src/algorithms/geneticAlgorithm/Components/GAMutationStrategy.java

@@ -0,0 +1,30 @@
+package algorithms.geneticAlgorithm.Components;
+
+import java.util.Random;
+
+public abstract class GAMutationStrategy<I extends GAIndividual> {
+	
+	protected double mutationProb;
+	protected Random rng;
+	
+	public GAMutationStrategy(double prob){
+		mutationProb = prob;
+		rng = new Random();
+	}
+	
+	public GAMutationStrategy(){
+		mutationProb = 0.001;
+		rng = new Random();
+	}
+	
+	public abstract I mutateIndividual(I mutant);
+	
+	public void setMutationProbability(double probability){
+		mutationProb = probability;
+	}
+	
+	public double getMutationProbability(){
+		return mutationProb;
+	}
+
+}

+ 11 - 0
src/algorithms/geneticAlgorithm/Components/GASelectionStrategy.java

@@ -0,0 +1,11 @@
+package algorithms.geneticAlgorithm.Components;
+
+import java.util.ArrayList;
+
+public interface GASelectionStrategy<I extends GAIndividual>{
+	
+	public void setCurrentPopulation(ArrayList<I> population);
+	
+	public ArrayList<I> selectIndividuals();
+
+}

+ 62 - 0
src/algorithms/geneticAlgorithm/Components/GeneticAlgo.java

@@ -0,0 +1,62 @@
+package algorithms.geneticAlgorithm.Components;
+
+import java.util.ArrayList;
+import java.util.Random;
+
+public class GeneticAlgo<I extends GAIndividual> {
+
+	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 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>();
+		
+		generateRandomPopulation();
+	}
+
+	public void generateRandomPopulation() {
+		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>();
+		
+		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;
+	}
+}

+ 101 - 0
src/algorithms/geneticAlgorithm/Components/Selections/AdvancedSelectionStrategy.java

@@ -0,0 +1,101 @@
+package algorithms.geneticAlgorithm.Components.Selections;
+
+import java.util.ArrayList;
+import java.util.Random;
+
+import algorithms.geneticAlgorithm.Components.GAIndividual;
+import algorithms.geneticAlgorithm.Components.GASelectionStrategy;
+
+public class AdvancedSelectionStrategy<I extends GAIndividual> implements GASelectionStrategy<I>{
+	
+	public class Range{
+		
+		public double min;
+		public double max;
+		public Range(double min, double max){
+			this.min = min;
+			this.max = max;
+		}
+	}
+	
+	ArrayList<Range> rouletteWheel;
+	ArrayList<I> currentPop;
+	Random random;
+	double maxOffset;
+	double minRange;
+	double accFittness;
+	double highestFittness;
+	double lowestFittness;
+	
+	public AdvancedSelectionStrategy(){
+		rouletteWheel = new ArrayList<Range>();
+		random = new Random();
+		currentPop = new ArrayList<I>();
+	}
+
+	
+	@Override
+	public void setCurrentPopulation(ArrayList<I> currentPopulation) {
+		currentPop = currentPopulation;
+		rouletteWheel.clear();
+		double minOffset = 0;
+		maxOffset = 0;
+		initValues();
+		
+		//y = a*x + b mit y zwischen 0 und 1 
+		double a = 1/(highestFittness - lowestFittness);
+		double b = -(a*lowestFittness);
+		if(lowestFittness != highestFittness){
+			//b += 1/highestFittness - lowestFittness;
+		}
+		
+		for(I individual : currentPop){
+			double space = a*individual.getFittness() + b;
+			minOffset = maxOffset;
+			maxOffset = minOffset + space;
+			
+			Range range = new Range(minOffset, maxOffset);
+			rouletteWheel.add(range);
+		}
+	}
+	
+	public void initValues(){
+		lowestFittness = currentPop.get(0).getFittness();
+		highestFittness = currentPop.get(0).getFittness();
+		
+		for(I i : currentPop){
+			if(i.getFittness() < lowestFittness){
+				lowestFittness = i.getFittness();
+			}
+			if(i.getFittness() > highestFittness){
+				highestFittness = i.getFittness();
+			}
+		}
+	}
+
+	
+	@Override
+	public ArrayList<I> selectIndividuals(){
+		ArrayList<I> parents = new ArrayList<I>();
+		parents.add(selectIndividual());
+		parents.add(selectIndividual());
+		return parents;
+	}
+	
+	public I selectIndividual() {
+		double randomValue = random.nextDouble() * maxOffset;
+		
+		for(int i = 0; i < rouletteWheel.size(); i++){
+			Range current = rouletteWheel.get(i);
+			if(current.min <= randomValue && randomValue < current.max){
+				return currentPop.get(i);
+			}
+		}
+		
+		return currentPop.get(random.nextInt(currentPop.size() - 1));
+	}
+
+	
+	
+
+}

+ 78 - 0
src/algorithms/geneticAlgorithm/Components/Selections/SimpleSelectionStrategy.java

@@ -0,0 +1,78 @@
+package algorithms.geneticAlgorithm.Components.Selections;
+
+import java.util.ArrayList;
+import java.util.Random;
+
+import algorithms.geneticAlgorithm.Components.GAIndividual;
+import algorithms.geneticAlgorithm.Components.GASelectionStrategy;
+
+public class SimpleSelectionStrategy<I extends GAIndividual> implements GASelectionStrategy<I>{
+	
+	public class Range{
+		
+		public double min;
+		public double max;
+		public Range(double min, double max){
+			this.min = min;
+			this.max = max;
+		}
+	}
+	
+	ArrayList<Range> rouletteWheel;
+	ArrayList<I> currentPop;
+	Random random;
+	double maxRange;
+	double minRange;
+	
+	public SimpleSelectionStrategy(){
+		rouletteWheel = new ArrayList<Range>();
+		random = new Random();
+		currentPop = new ArrayList<I>();
+	}
+
+	
+	@Override
+	public void setCurrentPopulation(ArrayList<I> currentPopulation) {
+		currentPop = currentPopulation;
+		rouletteWheel.clear();
+		double offSet = 0;
+		double min = 0;
+		double max = 0;
+		for(I individual : currentPop){
+			min = offSet;
+			max = min + individual.getFittness();
+			offSet = max; 
+			
+			Range range = new Range(min, max);
+			rouletteWheel.add(range);
+		}
+		
+		maxRange = max;
+		
+	}
+	
+	@Override
+	public ArrayList<I> selectIndividuals(){
+		ArrayList<I> parents = new ArrayList<I>();
+		parents.add(selectIndividual());
+		parents.add(selectIndividual());
+		return parents;
+	}
+	
+	public I selectIndividual() {
+		double randomValue = random.nextDouble() * maxRange;
+		
+		for(int i = 0; i < rouletteWheel.size(); i++){
+			Range current = rouletteWheel.get(i);
+			if(current.min <= randomValue && randomValue < current.max){
+				return currentPop.get(i);
+			}
+		}
+		
+		return currentPop.get(random.nextInt(currentPop.size() - 1));
+	}
+
+	
+	
+
+}

+ 110 - 0
src/algorithms/geneticAlgorithm/Components/Selections/TournamentSelectionStrategy.java

@@ -0,0 +1,110 @@
+package algorithms.geneticAlgorithm.Components.Selections;
+
+import java.util.ArrayList;
+import java.util.Random;
+
+import algorithms.geneticAlgorithm.Components.GAIndividual;
+import algorithms.geneticAlgorithm.Components.GASelectionStrategy;
+
+public class TournamentSelectionStrategy<I extends GAIndividual> implements GASelectionStrategy<I> {
+
+	public class Range{
+		
+		public double min;
+		public double max;
+		public Range(double min, double max){
+			this.min = min;
+			this.max = max;
+		}
+	}
+	
+	private ArrayList<I> currentPop;
+	public int tournamentSize;
+	private Random rng; 
+	private ArrayList<Range> tournamentWheel;
+	public ArrayList<I> competitors;
+	public double selectProb;
+	private double maxRange;
+	
+	
+	public TournamentSelectionStrategy(){
+		currentPop = new ArrayList<I>();
+		tournamentSize = 1;
+		rng = new Random();
+		selectProb = 1;
+	}
+	
+	public TournamentSelectionStrategy(int tSize, double selectProb){
+		currentPop = new ArrayList<I>();
+		rng = new Random();
+		tournamentSize = tSize;
+		this.selectProb = selectProb;
+	}
+	
+	@Override
+	public void setCurrentPopulation(ArrayList<I> population) {
+		currentPop = population;
+	}
+	
+	public void setTournamentSize(int size){
+		tournamentSize = size;
+	}
+
+	@Override
+	public ArrayList<I> selectIndividuals() {
+		ArrayList<I> parents = new ArrayList<I>();
+		parents.add(selectSingleIndividual());
+		parents.add(selectSingleIndividual());
+		return parents;
+	}
+	
+	public I selectSingleIndividual(){
+		tournamentSelection();
+		initWheel();
+		double index = rng.nextDouble() * maxRange;
+		for(int i = 0; i < tournamentWheel.size(); i++){
+			if(index >= tournamentWheel.get(i).min && index <= tournamentWheel.get(i).max){
+				return competitors.get(i);
+			}
+		}
+		return competitors.get(rng.nextInt(competitors.size()));
+	}
+	
+	public void tournamentSelection(){
+		competitors = new ArrayList<I>();
+		for(int i = 0; i < tournamentSize; i++){
+			I candidate = chooseCandidate();
+			int size = competitors.size();
+			for(int j = 0; j <= size; j++){
+				if(j != size){
+					if(competitors.get(j).getFittness() < candidate.getFittness()){
+						competitors.add(j, candidate);
+						break;
+					}
+				}else{
+					competitors.add(candidate);
+				}
+			}
+		}		
+	}
+	
+	public I chooseCandidate(){
+		int index = rng.nextInt(currentPop.size());
+		I candidate = currentPop.get(index);
+		return candidate;
+	}
+	
+	public void initWheel(){
+		tournamentWheel = new ArrayList<Range>();
+		int power = 0;
+		maxRange = 0;
+		for(int i = 0; i < tournamentSize; i++){
+			double currentProb = selectProb * Math.pow((1- selectProb), power);
+			Range range = new Range(maxRange, maxRange + currentProb);
+			tournamentWheel.add(range);
+			maxRange += currentProb;
+			power++;
+		}
+	}
+
+}

+ 1 - 1
src/classes/HolonObject.java

@@ -5,7 +5,7 @@ import com.google.gson.annotations.Expose;
 import java.awt.*;
 import java.util.ArrayList;
 
-import javafx.util.converter.PercentageStringConverter;
+//import javafx.util.converter.PercentageStringConverter;
 
 /**
  * The class HolonObject represents any Object on the system which capability of