diff --git a/src/main/java/Interfaces/Mutator.java b/src/main/java/Interfaces/Mutator.java index f79e377b55f805bea5e27e5293248b2e83cf8780..39c7c9a17bc268e1696f750df9651f7e60eb1374 100644 --- a/src/main/java/Interfaces/Mutator.java +++ b/src/main/java/Interfaces/Mutator.java @@ -5,10 +5,9 @@ import MainClasses.Candidate; public interface Mutator { - double MINIMUM_CHANCE = 0.0001; // -> 0.01% is not worth mutating for - Candidate[] mutatePopulation(Candidate[] population); + //TODO Remove, when decided on FRL vs NESW static <T extends Enum<?>> boolean isFRLEncoding(Class<T> possibleDirections) { T[] possibleDirectionsEnum = possibleDirections.getEnumConstants(); for (int i = 0; i < possibleDirectionsEnum.length; i++) { diff --git a/src/main/java/MainClasses/Config.java b/src/main/java/MainClasses/Config.java index 94cfac4f7787a7aa0a2b5a651b1f0ec3354efea1..1bc7151f92034a7b883d597fd5a36d42383bb34b 100644 --- a/src/main/java/MainClasses/Config.java +++ b/src/main/java/MainClasses/Config.java @@ -31,8 +31,10 @@ public class Config { static int MUTATION_ATTEMPTS_PER_CANDIDATE; static double MUTATION_CHANCE; static double MUTATION_MULTIPLIER; + static double MUTATION_MINIMAL_CHANCE; // -> 0.01% is not worth mutating for static int CROSSOVER_ATTEMPTS_PER_CANDIDATE; static double CROSSOVER_CHANCE; + static double CROSSOVER_MINIMAL_CHANCE; // -> 0.01% is not worth mutating for static double CROSSOVER_MULTIPLIER; static String LOGFILE; @@ -137,9 +139,11 @@ public class Config { // Mutation settings MUTATION_ATTEMPTS_PER_CANDIDATE = Integer.parseInt(this.properties.getProperty("mutationAttemptsPerCandidate")); MUTATION_CHANCE = Double.parseDouble(this.properties.getProperty("mutationChance")); + MUTATION_MINIMAL_CHANCE = Double.parseDouble(this.properties.getProperty("mutationMinimalChance")); MUTATION_MULTIPLIER = Double.parseDouble(this.properties.getProperty("mutationMultiplier")); CROSSOVER_ATTEMPTS_PER_CANDIDATE = Integer.parseInt(this.properties.getProperty("crossoverAttemptsPerCandidate")); CROSSOVER_CHANCE = Double.parseDouble(this.properties.getProperty("crossoverChance")); + CROSSOVER_MINIMAL_CHANCE = Double.parseDouble(this.properties.getProperty("crossoverMinimalChance")); CROSSOVER_MULTIPLIER = Double.parseDouble(this.properties.getProperty("crossoverMultiplier")); diff --git a/src/main/java/MainClasses/GeneticAlgorithm.java b/src/main/java/MainClasses/GeneticAlgorithm.java index 04bd367debc95f7b41222690dc86f08d2b9de736..0b6105d9f7ee852aadc1d821010e22d5e77b4f0f 100644 --- a/src/main/java/MainClasses/GeneticAlgorithm.java +++ b/src/main/java/MainClasses/GeneticAlgorithm.java @@ -91,11 +91,11 @@ public class GeneticAlgorithm { for (int i = 0; i < Config.MUTATOR_METHODS.length; i++) { if (Config.MUTATOR_METHODS[i].equals(MutatorMethods.SinglePoint)) { this.mutators[i] = new SinglePoint<>(DirectionNESW.class, this.rand, - Config.MUTATION_ATTEMPTS_PER_CANDIDATE, Config.MUTATION_CHANCE, Config.MUTATION_MULTIPLIER); + Config.MUTATION_ATTEMPTS_PER_CANDIDATE, Config.MUTATION_CHANCE, Config.MUTATION_MINIMAL_CHANCE, Config.MUTATION_MULTIPLIER); } else if (Config.MUTATOR_METHODS[i].equals(MutatorMethods.Crossover)) { this.mutators[i] = new Crossover<>(DirectionNESW.class, this.rand, - Config.CROSSOVER_ATTEMPTS_PER_CANDIDATE, Config.CROSSOVER_CHANCE, Config.CROSSOVER_MULTIPLIER); + Config.CROSSOVER_ATTEMPTS_PER_CANDIDATE, Config.CROSSOVER_CHANCE, Config.MUTATION_MINIMAL_CHANCE, Config.CROSSOVER_MULTIPLIER); } } diff --git a/src/main/java/Mutators/Crossover.java b/src/main/java/Mutators/Crossover.java index a76a47ad4e479f61cc948c55cb9bd8677c60c4ae..3b17e2416c43ce73bb766286119ce89052711d70 100644 --- a/src/main/java/Mutators/Crossover.java +++ b/src/main/java/Mutators/Crossover.java @@ -12,22 +12,24 @@ public class Crossover<T extends Enum<?>> implements Mutator { Random rand; int crossoverAttemptsPerCandidate; double crossoverChance; + double crossoverMinimalChance; double crossoverMultiplier; public Crossover(Class<T> possibleDirections, Random rand, int crossoverAttemptsPerCandidate, - double crossoverChance, double crossoverMultiplier) { + double crossoverChance, double crossoverMinimalChance, double crossoverMultiplier) { this.possibleDirections = possibleDirections; this.isFRL = Mutator.isFRLEncoding(possibleDirections); this.rand = rand; this.crossoverAttemptsPerCandidate = crossoverAttemptsPerCandidate; this.crossoverChance = crossoverChance; + this.crossoverMinimalChance = crossoverMinimalChance; this.crossoverMultiplier = crossoverMultiplier; } @Override public Candidate[] mutatePopulation(Candidate[] population) { Candidate[] mutatedPopulation = new Candidate[population.length]; - if (this.crossoverChance > MINIMUM_CHANCE) { + if (this.crossoverChance > crossoverMinimalChance) { int populationSize = population.length; int proteinLength = population[0].getFolding().length; diff --git a/src/main/java/Mutators/SinglePoint.java b/src/main/java/Mutators/SinglePoint.java index 99bb28666ead13473b528188863eefcf4f3837a2..8b1e2fa4f533402bab3026082998c84b88e6bf75 100644 --- a/src/main/java/Mutators/SinglePoint.java +++ b/src/main/java/Mutators/SinglePoint.java @@ -12,22 +12,24 @@ public class SinglePoint<T extends Enum<?>> implements Mutator { Random rand; int mutationAttemptsPerCandidate; double mutationChance; + double mutationMinimalChance; double mutationMultiplier; public SinglePoint(Class<T> possibleDirections, Random rand, int mutationAttemptsPerCandidate, - double mutationChance, double mutationMultiplier) { + double mutationChance, double mutationMinimalChance, double mutationMultiplier) { this.possibleDirections = possibleDirections; this.isFRL = Mutator.isFRLEncoding(possibleDirections); this.rand = rand; this.mutationAttemptsPerCandidate = mutationAttemptsPerCandidate; this.mutationChance = mutationChance; + this.mutationMinimalChance = mutationMinimalChance; this.mutationMultiplier = mutationMultiplier; } @Override public Candidate[] mutatePopulation(Candidate[] population) { Candidate[] mutatedPopulation = new Candidate[population.length]; - if (this.mutationChance > MINIMUM_CHANCE) { + if (this.mutationChance > mutationMinimalChance) { int proteinLength = population[0].getFolding().length; diff --git a/src/main/resources/genetic.properties b/src/main/resources/genetic.properties index ab0022f46c3374e6f4752e62165d55d9281f9e30..8bd44f3fa7a1431471992ff9913eeb530e70c9e5 100644 --- a/src/main/resources/genetic.properties +++ b/src/main/resources/genetic.properties @@ -27,12 +27,16 @@ mutationAttemptsPerCandidate = 1 # Chance for a successful single point mutation [1.0 = 100%] mutationChance = 1.0 + # The mutation chance cannot drop below this value [1.0 = 100%] + mutationMinimalChance = 0.001 # Multiplicand for mutation probability with each generation -> ex with 0.05: 1st 1.0, 2nd 0.95, 3rd 0.9025, 4th 0.857 mutationMultiplier = 0.001 # How often a crossover should be attempted per candidate crossoverAttemptsPerCandidate = 1 # Chance for a successful crossover [1.0 = 100%] crossoverChance = 0.6 + # The crossover chance cannot drop below this value [1.0 = 100%] + crossoverMinimalChance = 0.001 # Multiplicand for mutation probability with each generation -> ex with -0.05: 1st 1.0, 2nd 1.05, 3rd 1,1025, 4th 1,157 crossoverMultiplier = 0.005