Simple Genetic Algorithms improvements (#1146)
* Modifications to model on Hibernate One to manyTutorial * Modifications to model on Hibernate One to manyTutorial * Modifications to model on Hibernate One to manyTutorial * Simple Genetic Algorithm improvements
This commit is contained in:
parent
30ed03aea1
commit
c95097e294
|
@ -8,28 +8,30 @@ import com.baeldung.algorithms.slope_one.SlopeOne;
|
||||||
|
|
||||||
public class RunAlgorithm {
|
public class RunAlgorithm {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner in = new Scanner(System.in);
|
Scanner in = new Scanner(System.in);
|
||||||
System.out.println("Run algorithm:");
|
System.out.println("Run algorithm:");
|
||||||
System.out.println("1 - Simulated Annealing");
|
System.out.println("1 - Simulated Annealing");
|
||||||
System.out.println("2 - Slope One");
|
System.out.println("2 - Slope One");
|
||||||
System.out.println("3 - Simple Genetic Algorithm");
|
System.out.println("3 - Simple Genetic Algorithm");
|
||||||
int decision = in.nextInt();
|
int decision = in.nextInt();
|
||||||
switch (decision) {
|
switch (decision) {
|
||||||
case 1:
|
case 1:
|
||||||
System.out.println("Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
|
System.out.println(
|
||||||
break;
|
"Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
|
||||||
case 2:
|
break;
|
||||||
SlopeOne.slopeOne(3);
|
case 2:
|
||||||
break;
|
SlopeOne.slopeOne(3);
|
||||||
case 3:
|
break;
|
||||||
SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111");
|
case 3:
|
||||||
break;
|
SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm();
|
||||||
default:
|
ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111");
|
||||||
System.out.println("Unknown option");
|
break;
|
||||||
break;
|
default:
|
||||||
}
|
System.out.println("Unknown option");
|
||||||
in.close();
|
break;
|
||||||
}
|
}
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,17 +11,17 @@ public class SimpleGeneticAlgorithm {
|
||||||
private static final boolean elitism = true;
|
private static final boolean elitism = true;
|
||||||
private static byte[] solution = new byte[64];
|
private static byte[] solution = new byte[64];
|
||||||
|
|
||||||
public static boolean runAlgorithm(int populationSize, String solution) {
|
public boolean runAlgorithm(int populationSize, String solution) {
|
||||||
if (solution.length() != SimpleGeneticAlgorithm.solution.length) {
|
if (solution.length() != SimpleGeneticAlgorithm.solution.length) {
|
||||||
throw new RuntimeException("The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes");
|
throw new RuntimeException("The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes");
|
||||||
}
|
}
|
||||||
SimpleGeneticAlgorithm.setSolution(solution);
|
setSolution(solution);
|
||||||
Population myPop = new Population(populationSize, true);
|
Population myPop = new Population(populationSize, true);
|
||||||
|
|
||||||
int generationCount = 1;
|
int generationCount = 1;
|
||||||
while (myPop.getFittest().getFitness() < SimpleGeneticAlgorithm.getMaxFitness()) {
|
while (myPop.getFittest().getFitness() < getMaxFitness()) {
|
||||||
System.out.println("Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness());
|
System.out.println("Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness());
|
||||||
myPop = SimpleGeneticAlgorithm.evolvePopulation(myPop);
|
myPop = evolvePopulation(myPop);
|
||||||
generationCount++;
|
generationCount++;
|
||||||
}
|
}
|
||||||
System.out.println("Solution found!");
|
System.out.println("Solution found!");
|
||||||
|
@ -31,7 +31,7 @@ public class SimpleGeneticAlgorithm {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Population evolvePopulation(Population pop) {
|
public Population evolvePopulation(Population pop) {
|
||||||
int elitismOffset;
|
int elitismOffset;
|
||||||
Population newPopulation = new Population(pop.getIndividuals().size(), false);
|
Population newPopulation = new Population(pop.getIndividuals().size(), false);
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ public class SimpleGeneticAlgorithm {
|
||||||
return newPopulation;
|
return newPopulation;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Individual crossover(Individual indiv1, Individual indiv2) {
|
private Individual crossover(Individual indiv1, Individual indiv2) {
|
||||||
Individual newSol = new Individual();
|
Individual newSol = new Individual();
|
||||||
for (int i = 0; i < newSol.getDefaultGeneLength(); i++) {
|
for (int i = 0; i < newSol.getDefaultGeneLength(); i++) {
|
||||||
if (Math.random() <= uniformRate) {
|
if (Math.random() <= uniformRate) {
|
||||||
|
@ -68,7 +68,7 @@ public class SimpleGeneticAlgorithm {
|
||||||
return newSol;
|
return newSol;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void mutate(Individual indiv) {
|
private void mutate(Individual indiv) {
|
||||||
for (int i = 0; i < indiv.getDefaultGeneLength(); i++) {
|
for (int i = 0; i < indiv.getDefaultGeneLength(); i++) {
|
||||||
if (Math.random() <= mutationRate) {
|
if (Math.random() <= mutationRate) {
|
||||||
byte gene = (byte) Math.round(Math.random());
|
byte gene = (byte) Math.round(Math.random());
|
||||||
|
@ -77,7 +77,7 @@ public class SimpleGeneticAlgorithm {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Individual tournamentSelection(Population pop) {
|
private Individual tournamentSelection(Population pop) {
|
||||||
Population tournament = new Population(tournamentSize, false);
|
Population tournament = new Population(tournamentSize, false);
|
||||||
for (int i = 0; i < tournamentSize; i++) {
|
for (int i = 0; i < tournamentSize; i++) {
|
||||||
int randomId = (int) (Math.random() * pop.getIndividuals().size());
|
int randomId = (int) (Math.random() * pop.getIndividuals().size());
|
||||||
|
@ -97,12 +97,12 @@ public class SimpleGeneticAlgorithm {
|
||||||
return fitness;
|
return fitness;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static int getMaxFitness() {
|
protected int getMaxFitness() {
|
||||||
int maxFitness = solution.length;
|
int maxFitness = solution.length;
|
||||||
return maxFitness;
|
return maxFitness;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void setSolution(String newSolution) {
|
protected void setSolution(String newSolution) {
|
||||||
solution = new byte[newSolution.length()];
|
solution = new byte[newSolution.length()];
|
||||||
for (int i = 0; i < newSolution.length(); i++) {
|
for (int i = 0; i < newSolution.length(); i++) {
|
||||||
String character = newSolution.substring(i, i + 1);
|
String character = newSolution.substring(i, i + 1);
|
||||||
|
|
|
@ -7,9 +7,10 @@ import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
|
||||||
|
|
||||||
public class BinaryGeneticAlgorithmUnitTest {
|
public class BinaryGeneticAlgorithmUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGA() {
|
public void testGA() {
|
||||||
Assert.assertTrue(SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"));
|
SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm();
|
||||||
}
|
Assert.assertTrue(ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,3 +12,4 @@
|
||||||
*.war
|
*.war
|
||||||
*.ear
|
*.ear
|
||||||
/target/
|
/target/
|
||||||
|
/target/
|
||||||
|
|
Loading…
Reference in New Issue