@@ -11,17 +11,17 @@ public class SimpleGeneticAlgorithm {
1111 private static final boolean elitism = true ;
1212 private static byte [] solution = new byte [64 ];
1313
14- public static boolean runAlgorithm (int populationSize , String solution ) {
14+ public boolean runAlgorithm (int populationSize , String solution ) {
1515 if (solution .length () != SimpleGeneticAlgorithm .solution .length ) {
1616 throw new RuntimeException ("The solution needs to have " + SimpleGeneticAlgorithm .solution .length + " bytes" );
1717 }
18- SimpleGeneticAlgorithm . setSolution (solution );
18+ setSolution (solution );
1919 Population myPop = new Population (populationSize , true );
2020
2121 int generationCount = 1 ;
22- while (myPop .getFittest ().getFitness () < SimpleGeneticAlgorithm . getMaxFitness ()) {
22+ while (myPop .getFittest ().getFitness () < getMaxFitness ()) {
2323 System .out .println ("Generation: " + generationCount + " Correct genes found: " + myPop .getFittest ().getFitness ());
24- myPop = SimpleGeneticAlgorithm . evolvePopulation (myPop );
24+ myPop = evolvePopulation (myPop );
2525 generationCount ++;
2626 }
2727 System .out .println ("Solution found!" );
@@ -31,7 +31,7 @@ public static boolean runAlgorithm(int populationSize, String solution) {
3131 return true ;
3232 }
3333
34- public static Population evolvePopulation (Population pop ) {
34+ public Population evolvePopulation (Population pop ) {
3535 int elitismOffset ;
3636 Population newPopulation = new Population (pop .getIndividuals ().size (), false );
3737
@@ -56,7 +56,7 @@ public static Population evolvePopulation(Population pop) {
5656 return newPopulation ;
5757 }
5858
59- private static Individual crossover (Individual indiv1 , Individual indiv2 ) {
59+ private Individual crossover (Individual indiv1 , Individual indiv2 ) {
6060 Individual newSol = new Individual ();
6161 for (int i = 0 ; i < newSol .getDefaultGeneLength (); i ++) {
6262 if (Math .random () <= uniformRate ) {
@@ -68,7 +68,7 @@ private static Individual crossover(Individual indiv1, Individual indiv2) {
6868 return newSol ;
6969 }
7070
71- private static void mutate (Individual indiv ) {
71+ private void mutate (Individual indiv ) {
7272 for (int i = 0 ; i < indiv .getDefaultGeneLength (); i ++) {
7373 if (Math .random () <= mutationRate ) {
7474 byte gene = (byte ) Math .round (Math .random ());
@@ -77,7 +77,7 @@ private static void mutate(Individual indiv) {
7777 }
7878 }
7979
80- private static Individual tournamentSelection (Population pop ) {
80+ private Individual tournamentSelection (Population pop ) {
8181 Population tournament = new Population (tournamentSize , false );
8282 for (int i = 0 ; i < tournamentSize ; i ++) {
8383 int randomId = (int ) (Math .random () * pop .getIndividuals ().size ());
@@ -97,12 +97,12 @@ protected static int getFitness(Individual individual) {
9797 return fitness ;
9898 }
9999
100- protected static int getMaxFitness () {
100+ protected int getMaxFitness () {
101101 int maxFitness = solution .length ;
102102 return maxFitness ;
103103 }
104104
105- protected static void setSolution (String newSolution ) {
105+ protected void setSolution (String newSolution ) {
106106 solution = new byte [newSolution .length ()];
107107 for (int i = 0 ; i < newSolution .length (); i ++) {
108108 String character = newSolution .substring (i , i + 1 );
0 commit comments