Skip to content

Commit

Permalink
Simple Genetic Algorithms improvements (eugenp#1146)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
maibin authored and KevinGilmore committed Feb 10, 2017
1 parent 30ed03a commit c95097e
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 37 deletions.
Empty file removed core-java/0.004102810554955205
Empty file.
Empty file removed core-java/0.04832801936270381
Empty file.
Empty file removed core-java/0.5633433244738808
Empty file.
Empty file removed core-java/0.6256429734439612
Empty file.
Empty file removed core-java/0.9799201796740292
Empty file.
48 changes: 25 additions & 23 deletions core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@

public class RunAlgorithm {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Run algorithm:");
System.out.println("1 - Simulated Annealing");
System.out.println("2 - Slope One");
System.out.println("3 - Simple Genetic Algorithm");
int decision = in.nextInt();
switch (decision) {
case 1:
System.out.println("Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
break;
case 2:
SlopeOne.slopeOne(3);
break;
case 3:
SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111");
break;
default:
System.out.println("Unknown option");
break;
}
in.close();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Run algorithm:");
System.out.println("1 - Simulated Annealing");
System.out.println("2 - Slope One");
System.out.println("3 - Simple Genetic Algorithm");
int decision = in.nextInt();
switch (decision) {
case 1:
System.out.println(
"Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
break;
case 2:
SlopeOne.slopeOne(3);
break;
case 3:
SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm();
ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111");
break;
default:
System.out.println("Unknown option");
break;
}
in.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ public class SimpleGeneticAlgorithm {
private static final boolean elitism = true;
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) {
throw new RuntimeException("The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes");
}
SimpleGeneticAlgorithm.setSolution(solution);
setSolution(solution);
Population myPop = new Population(populationSize, true);

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());
myPop = SimpleGeneticAlgorithm.evolvePopulation(myPop);
myPop = evolvePopulation(myPop);
generationCount++;
}
System.out.println("Solution found!");
Expand All @@ -31,7 +31,7 @@ public static boolean runAlgorithm(int populationSize, String solution) {
return true;
}

public static Population evolvePopulation(Population pop) {
public Population evolvePopulation(Population pop) {
int elitismOffset;
Population newPopulation = new Population(pop.getIndividuals().size(), false);

Expand All @@ -56,7 +56,7 @@ public static Population evolvePopulation(Population pop) {
return newPopulation;
}

private static Individual crossover(Individual indiv1, Individual indiv2) {
private Individual crossover(Individual indiv1, Individual indiv2) {
Individual newSol = new Individual();
for (int i = 0; i < newSol.getDefaultGeneLength(); i++) {
if (Math.random() <= uniformRate) {
Expand All @@ -68,7 +68,7 @@ private static Individual crossover(Individual indiv1, Individual indiv2) {
return newSol;
}

private static void mutate(Individual indiv) {
private void mutate(Individual indiv) {
for (int i = 0; i < indiv.getDefaultGeneLength(); i++) {
if (Math.random() <= mutationRate) {
byte gene = (byte) Math.round(Math.random());
Expand All @@ -77,7 +77,7 @@ private static void mutate(Individual indiv) {
}
}

private static Individual tournamentSelection(Population pop) {
private Individual tournamentSelection(Population pop) {
Population tournament = new Population(tournamentSize, false);
for (int i = 0; i < tournamentSize; i++) {
int randomId = (int) (Math.random() * pop.getIndividuals().size());
Expand All @@ -97,12 +97,12 @@ protected static int getFitness(Individual individual) {
return fitness;
}

protected static int getMaxFitness() {
protected int getMaxFitness() {
int maxFitness = solution.length;
return maxFitness;
}

protected static void setSolution(String newSolution) {
protected void setSolution(String newSolution) {
solution = new byte[newSolution.length()];
for (int i = 0; i < newSolution.length(); i++) {
String character = newSolution.substring(i, i + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

public class BinaryGeneticAlgorithmUnitTest {

@Test
public void testGA() {
Assert.assertTrue(SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"));
}
@Test
public void testGA() {
SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm();
Assert.assertTrue(ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"));
}

}
1 change: 1 addition & 0 deletions spring-hibernate4/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
*.war
*.ear
/target/
/target/

0 comments on commit c95097e

Please sign in to comment.