Finish refactor
This commit is contained in:
@@ -12,8 +12,9 @@ static classes.
|
|||||||
|
|
||||||
### Prepare support for class inheritance
|
### Prepare support for class inheritance
|
||||||
|
|
||||||
## Limit number of generations
|
---
|
||||||
|
|
||||||
|
## Limit number of generations
|
||||||
|
|
||||||
|
|
||||||
## Roulette selection
|
## Roulette selection
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import domain.annealing.SimulatedAnnealing;
|
import domain.annealing.SimulatedAnnealing;
|
||||||
import domain.ant_colony.AntColonyOptimization;
|
import domain.ant_colony.AntColonyOptimization;
|
||||||
|
import domain.binary.algorithm.CustomGeneticAlgorithm;
|
||||||
import domain.binary.algorithm.SimpleGeneticAlgorithm;
|
import domain.binary.algorithm.SimpleGeneticAlgorithm;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
@@ -19,13 +20,14 @@ public class Main {
|
|||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm(
|
SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm(
|
||||||
|
50,
|
||||||
0.5,
|
0.5,
|
||||||
0.025,
|
0.025,
|
||||||
5,
|
5,
|
||||||
true,
|
true,
|
||||||
new byte[64]
|
"1011000100000100010000100000100111001000000100000100000000001111"
|
||||||
);
|
);
|
||||||
ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111");
|
ga.launch();
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
AntColonyOptimization antColony = new AntColonyOptimization(21);
|
AntColonyOptimization antColony = new AntColonyOptimization(21);
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ public class Population {
|
|||||||
return individuals[index];
|
return individuals[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
public Individual setIndividual(int index, Individual individual) {
|
public void setIndividual(int index, Individual individual) {
|
||||||
return individuals[index] = individual;
|
individuals[index] = individual;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Individual getFittest() {
|
public Individual getFittest() {
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package domain.binary.algorithm;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public abstract class AbstractGeneticAlgorithm {
|
||||||
|
|
||||||
|
private byte[] solution;
|
||||||
|
private int generationCount;
|
||||||
|
|
||||||
|
public AbstractGeneticAlgorithm(String solution) {
|
||||||
|
generationCount = 0;
|
||||||
|
this.solution = mapSolution(solution);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void launch(){
|
||||||
|
while(!stopCriterion()){
|
||||||
|
run();
|
||||||
|
generationCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void run();
|
||||||
|
protected abstract boolean stopCriterion();
|
||||||
|
|
||||||
|
protected int getMaxFitness() {
|
||||||
|
return solution.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] mapSolution(String newSolution) {
|
||||||
|
solution = new byte[newSolution.length()];
|
||||||
|
for (int i = 0; i < newSolution.length(); i++) {
|
||||||
|
String character = newSolution.substring(i, i + 1);
|
||||||
|
if (character.contains("0") || character.contains("1")) {
|
||||||
|
solution[i] = Byte.parseByte(character);
|
||||||
|
} else {
|
||||||
|
solution[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return solution;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,27 +1,24 @@
|
|||||||
package domain.binary.algorithm;
|
package domain.binary.algorithm;
|
||||||
|
|
||||||
import domain.binary.Population;
|
|
||||||
|
|
||||||
public class CustomGeneticAlgorithm extends SimpleGeneticAlgorithm {
|
public class CustomGeneticAlgorithm extends SimpleGeneticAlgorithm {
|
||||||
|
|
||||||
|
private final int maxGenerationCount;
|
||||||
|
|
||||||
public CustomGeneticAlgorithm(
|
public CustomGeneticAlgorithm(
|
||||||
|
int popSize,
|
||||||
|
int maxGenerationCount,
|
||||||
double uniformRate,
|
double uniformRate,
|
||||||
double mutationRate,
|
double mutationRate,
|
||||||
int tournamentSize,
|
int tournamentSize,
|
||||||
boolean elitism,
|
boolean elitism,
|
||||||
byte[] solution
|
String solution
|
||||||
) {
|
) {
|
||||||
super(uniformRate, mutationRate, tournamentSize, elitism, solution);
|
super(popSize, uniformRate, mutationRate, tournamentSize, elitism, solution);
|
||||||
|
this.maxGenerationCount = maxGenerationCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean runAlgorithm(int populationSize, String solution){
|
protected boolean stopCriterion() {
|
||||||
return false;
|
return getGenerationCount() == maxGenerationCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean checkStopCriteria(Population pop) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,81 +1,41 @@
|
|||||||
package domain.binary.algorithm;
|
package domain.binary.algorithm;
|
||||||
|
|
||||||
import domain.binary.Individual;
|
|
||||||
import domain.binary.Population;
|
import domain.binary.Population;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
public class SimpleGeneticAlgorithm extends AbstractGeneticAlgorithm{
|
||||||
public class SimpleGeneticAlgorithm {
|
|
||||||
|
|
||||||
private final double uniformRate;
|
private Population population;
|
||||||
private final double mutationRate;
|
|
||||||
private final int tournamentSize;
|
|
||||||
private final boolean elitism;
|
private final boolean elitism;
|
||||||
private byte[] solution;
|
|
||||||
|
|
||||||
public SimpleGeneticAlgorithm(
|
public SimpleGeneticAlgorithm(
|
||||||
|
int popSize,
|
||||||
double uniformRate,
|
double uniformRate,
|
||||||
double mutationRate,
|
double mutationRate,
|
||||||
int tournamentSize,
|
int tournamentSize,
|
||||||
boolean elitism,
|
boolean elitism,
|
||||||
byte[] solution
|
String solution
|
||||||
){
|
){
|
||||||
this.uniformRate = uniformRate;
|
super(solution);
|
||||||
this.mutationRate = mutationRate;
|
this.population = new Population(
|
||||||
this.tournamentSize = tournamentSize;
|
popSize,
|
||||||
this.elitism = elitism;
|
|
||||||
this.solution = solution;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean runAlgorithm(int populationSize, String solution) {
|
|
||||||
setSolution(solution);
|
|
||||||
Population myPop = new Population(
|
|
||||||
populationSize,
|
|
||||||
uniformRate,
|
uniformRate,
|
||||||
mutationRate,
|
mutationRate,
|
||||||
tournamentSize,
|
tournamentSize,
|
||||||
this.solution);
|
getSolution());
|
||||||
|
this.elitism = elitism;
|
||||||
int generationCount = 1;
|
|
||||||
|
|
||||||
while (checkStopCriteria(myPop)) {
|
|
||||||
System.out.println("Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness());
|
|
||||||
myPop = myPop.evolve(elitism, this.solution);
|
|
||||||
generationCount++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Solution found!");
|
@Override
|
||||||
System.out.println("Generation: " + generationCount);
|
protected void run() {
|
||||||
System.out.println("Genes: ");
|
System.out.println("Generation: " + getGenerationCount() + " Correct genes found: " + population.getFittest().getFitness());
|
||||||
System.out.println(myPop.getFittest());
|
population = population.evolve(elitism, getSolution());
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean checkStopCriteria(Population pop) {
|
@Override
|
||||||
int fitness = pop.getFittest().getFitness();
|
protected boolean stopCriterion() {
|
||||||
|
int fitness = population.getFittest().getFitness();
|
||||||
int maxFitness = getMaxFitness();
|
int maxFitness = getMaxFitness();
|
||||||
return fitness < maxFitness;
|
return fitness >= maxFitness;
|
||||||
}
|
|
||||||
|
|
||||||
protected int getMaxFitness() {
|
|
||||||
int maxFitness = solution.length;
|
|
||||||
return maxFitness;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setSolution(String newSolution) {
|
|
||||||
if (newSolution.length() != this.solution.length) {
|
|
||||||
throw new RuntimeException("The solution needs to have " + this.solution.length + " bytes");
|
|
||||||
}
|
|
||||||
|
|
||||||
solution = new byte[newSolution.length()];
|
|
||||||
for (int i = 0; i < newSolution.length(); i++) {
|
|
||||||
String character = newSolution.substring(i, i + 1);
|
|
||||||
if (character.contains("0") || character.contains("1")) {
|
|
||||||
solution[i] = Byte.parseByte(character);
|
|
||||||
} else {
|
|
||||||
solution[i] = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user