Extract object creation
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
import domain.annealing.SimulatedAnnealing;
|
import domain.annealing.SimulatedAnnealing;
|
||||||
import domain.ant_colony.AntColonyOptimization;
|
import domain.ant_colony.AntColonyOptimization;
|
||||||
|
import domain.binary.Population;
|
||||||
import domain.binary.algorithm.AbstractGeneticAlgorithm;
|
import domain.binary.algorithm.AbstractGeneticAlgorithm;
|
||||||
import domain.binary.algorithm.AlgorithmEngine;
|
import domain.binary.algorithm.AlgorithmEngine;
|
||||||
import domain.binary.algorithm.CustomGeneticAlgorithm;
|
import domain.binary.algorithm.CustomGeneticAlgorithm;
|
||||||
import domain.binary.algorithm.SimpleGeneticAlgorithm;
|
import domain.binary.algorithm.SimpleGeneticAlgorithm;
|
||||||
|
import domain.binary.factory.GeneticAlgorithmFactory;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
@@ -21,15 +24,27 @@ public class Main {
|
|||||||
System.out.println("Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
|
System.out.println("Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
AbstractGeneticAlgorithm ga = new SimpleGeneticAlgorithm(
|
|
||||||
15,
|
AbstractGeneticAlgorithm simple = GeneticAlgorithmFactory.createSimple(
|
||||||
|
50,
|
||||||
0.5,
|
0.5,
|
||||||
0.025,
|
0.025,
|
||||||
5,
|
5,
|
||||||
true,
|
true,
|
||||||
"1011000100000100010000100000100111001000000100000100000000001111"
|
"1011000100000100010000100000100111001000000100000100000000001111"
|
||||||
);
|
);
|
||||||
AlgorithmEngine.start(ga);
|
|
||||||
|
AbstractGeneticAlgorithm custom = GeneticAlgorithmFactory.createCustom(
|
||||||
|
50,
|
||||||
|
0.5,
|
||||||
|
0.025,
|
||||||
|
5,
|
||||||
|
25,
|
||||||
|
true,
|
||||||
|
"1011000100000100010000100000100111001000000100000100000000001111"
|
||||||
|
);
|
||||||
|
|
||||||
|
AlgorithmEngine.start(simple);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
AntColonyOptimization antColony = new AntColonyOptimization(21);
|
AntColonyOptimization antColony = new AntColonyOptimization(21);
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ public abstract class AbstractGeneticAlgorithm {
|
|||||||
private byte[] solution;
|
private byte[] solution;
|
||||||
private int generationCount;
|
private int generationCount;
|
||||||
|
|
||||||
public AbstractGeneticAlgorithm(String solution) {
|
public AbstractGeneticAlgorithm(byte[] solution) {
|
||||||
generationCount = 0;
|
generationCount = 0;
|
||||||
this.solution = mapSolution(solution);
|
this.solution = solution;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void incrementGenerationCount(){
|
public void incrementGenerationCount(){
|
||||||
@@ -23,17 +23,4 @@ public abstract class AbstractGeneticAlgorithm {
|
|||||||
protected int getMaxFitness() {
|
protected int getMaxFitness() {
|
||||||
return solution.length;
|
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,19 +1,18 @@
|
|||||||
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;
|
private final int maxGenerationCount;
|
||||||
|
|
||||||
public CustomGeneticAlgorithm(
|
public CustomGeneticAlgorithm(
|
||||||
int popSize,
|
Population population,
|
||||||
int maxGenerationCount,
|
int maxGenerationCount,
|
||||||
double uniformRate,
|
|
||||||
double mutationRate,
|
|
||||||
int tournamentSize,
|
|
||||||
boolean elitism,
|
boolean elitism,
|
||||||
String solution
|
byte[] solution
|
||||||
) {
|
) {
|
||||||
super(popSize, uniformRate, mutationRate, tournamentSize, elitism, solution);
|
super(population, elitism, solution);
|
||||||
this.maxGenerationCount = maxGenerationCount;
|
this.maxGenerationCount = maxGenerationCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,20 +9,12 @@ public class SimpleGeneticAlgorithm extends AbstractGeneticAlgorithm{
|
|||||||
private final boolean elitism;
|
private final boolean elitism;
|
||||||
|
|
||||||
public SimpleGeneticAlgorithm(
|
public SimpleGeneticAlgorithm(
|
||||||
int popSize,
|
Population population,
|
||||||
double uniformRate,
|
|
||||||
double mutationRate,
|
|
||||||
int tournamentSize,
|
|
||||||
boolean elitism,
|
boolean elitism,
|
||||||
String solution
|
byte[] solution
|
||||||
){
|
){
|
||||||
super(solution);
|
super(solution);
|
||||||
this.population = new Population(
|
this.population = population;
|
||||||
popSize,
|
|
||||||
uniformRate,
|
|
||||||
mutationRate,
|
|
||||||
tournamentSize,
|
|
||||||
getSolution());
|
|
||||||
this.elitism = elitism;
|
this.elitism = elitism;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package domain.binary.factory;
|
||||||
|
|
||||||
|
import domain.binary.Population;
|
||||||
|
import domain.binary.algorithm.CustomGeneticAlgorithm;
|
||||||
|
import domain.binary.algorithm.SimpleGeneticAlgorithm;
|
||||||
|
|
||||||
|
public class GeneticAlgorithmFactory {
|
||||||
|
|
||||||
|
public static SimpleGeneticAlgorithm createSimple(
|
||||||
|
int popSize,
|
||||||
|
double uniformRate,
|
||||||
|
double mutationRate,
|
||||||
|
int tournamentSize,
|
||||||
|
boolean elitism,
|
||||||
|
String solution
|
||||||
|
){
|
||||||
|
byte[] mappedSolution = mapSolution(solution);
|
||||||
|
Population population = createPopulation(popSize, uniformRate, mutationRate, tournamentSize, mappedSolution);
|
||||||
|
return new SimpleGeneticAlgorithm(population, elitism, mappedSolution);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CustomGeneticAlgorithm createCustom(
|
||||||
|
int popSize,
|
||||||
|
double uniformRate,
|
||||||
|
double mutationRate,
|
||||||
|
int tournamentSize,
|
||||||
|
int maxGenerationCount,
|
||||||
|
boolean elitism,
|
||||||
|
String solution
|
||||||
|
){
|
||||||
|
byte[] mappedSolution = mapSolution(solution);
|
||||||
|
Population population = createPopulation(popSize, uniformRate, mutationRate, tournamentSize, mappedSolution);
|
||||||
|
return new CustomGeneticAlgorithm(population, maxGenerationCount, elitism, mappedSolution);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Population createPopulation(
|
||||||
|
int popSize,
|
||||||
|
double uniformRate,
|
||||||
|
double mutationRate,
|
||||||
|
int tournamentSize,
|
||||||
|
byte[] solution
|
||||||
|
){
|
||||||
|
return new Population(
|
||||||
|
popSize,
|
||||||
|
uniformRate,
|
||||||
|
mutationRate,
|
||||||
|
tournamentSize,
|
||||||
|
solution);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] mapSolution(String newSolution) {
|
||||||
|
byte[] 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user