55
66public class GenerationAlgorithm {
77 public final int POPULATION ;
8- public final int NUMBER_OF_GENERATIONS = 200 ;
9- public final double MUTATION_CHANCE = 0.2 ;
10- public final int MAX_MUTATION_COUNT = 5 ;
8+ public final int NUMBER_OF_GENERATIONS = 500 ;
9+ public final double MUTATION_CHANCE = 0.3 ;
10+ public final int MAX_MUTATION_COUNT = 8 ;
1111 public final int ELITISM_NUMBER = 2 ;
1212 public final int NUMBER_OF_GENERATIONS_PER_INCREASE = 10 ;
1313 public final int INSTRUCTION_SIZE_INCREASE = 10 ;
14+ public final boolean LOAD_FILE = false ;
1415 static Random rand = new Random ();
16+ SaveLoader saveLoader = new SaveLoader ();
1517 ArrayList <Integer > eliteIndexes = new ArrayList <Integer >();
1618 int generationNumber = 0 ;
1719 int instructionSize = 20 ;
20+ double improvement = 0 ;
1821 double bestFit = 0 ;
1922 Agent [] agents ;
2023
2124 public GenerationAlgorithm (Agent [] agents ){
2225 this .agents = agents ;
2326 this .POPULATION = agents .length ;
27+ if (LOAD_FILE ){
28+ saveLoader .loadSave (this );
29+ }
30+ else {
31+ createPopulation ();
32+ }
2433 createPopulation ();
34+
2535 }
2636 public void checkGeneration (){
2737 if (agents [agents .length -1 ].isFinished ){
2838 ArrayList <Double > fitnesses = getFitness ();
2939 ArrayList <Double > weights = getWeights (fitnesses );
3040 ArrayList <Integer []> parentsList = getParents (weights );
41+ this .improvement = fitnesses .get (eliteIndexes .get (0 )) - this .bestFit ;
3142 this .bestFit = fitnesses .get (eliteIndexes .get (0 ));
3243 System .out .println ("Best fit: " + fitnesses .get (eliteIndexes .get (0 )) + " " + fitnesses .get (eliteIndexes .get (1 )));
3344 System .out .printf ("Generation completed number: %d\n " ,generationNumber );
@@ -41,6 +52,7 @@ public void checkGeneration(){
4152 assignRandomInstructions (agent , INSTRUCTION_SIZE_INCREASE );
4253 }
4354 instructionSize += INSTRUCTION_SIZE_INCREASE ;
55+ saveLoader .save (this );
4456 }
4557 }
4658 }
@@ -101,6 +113,11 @@ public ArrayList<Integer[]> getParents(ArrayList<Double> weights){
101113 }
102114 public void crossOver (ArrayList <Integer []> parents ){
103115 ArrayList <ArrayList <Integer >> instructionsTemp = new ArrayList <ArrayList <Integer >>();
116+ double mutationAgression = 0 ;
117+ if (improvement <0.5 ){
118+ mutationAgression = 0.2 ;
119+ System .out .println ("Mutation Agression Activated" );
120+ }
104121 for (int i = 0 ; i <POPULATION ; i ++){
105122 instructionsTemp .add (new ArrayList <Integer >(agents [i ].instructions ));
106123 }
@@ -118,19 +135,18 @@ public void crossOver(ArrayList<Integer[]> parents){
118135 }
119136 agents [i ].instructions .clear ();
120137 agents [i ].instructions .addAll (instruction1 );
121-
122- if (Math .random ()<MUTATION_CHANCE ){
123- if (Math .random ()<0.4 ){ //randomize anywhere in the instructions %40 chance to mutate anywhere, %60 chance to mutate last 10
124- int numberOfMutations = rand .nextInt (MAX_MUTATION_COUNT );
138+ if (Math .random ()<MUTATION_CHANCE +mutationAgression ){
139+ if (Math .random ()<0.4 ){ //randomize anywhere in the instructions %40 chance to mutate anywhere, %60 chance to mutate last 20
140+ int numberOfMutations = rand .nextInt (MAX_MUTATION_COUNT ) + (int )mutationAgression *10 ;
125141 for (int m = 0 ; m <numberOfMutations ;m ++){
126142 int randomInstructionIndex = rand .nextInt (agents [i ].instructions .size ());
127143 agents [i ].instructions .set (randomInstructionIndex ,rand .nextInt (5 )); //randomize instructions
128144 }
129145 }
130- else { //only randomize the last 10 (%60)
131- int numberOfMutations = rand .nextInt (MAX_MUTATION_COUNT );
146+ else { //only randomize the last 20 (%60)
147+ int numberOfMutations = rand .nextInt (MAX_MUTATION_COUNT ) + ( int ) mutationAgression * 10 ;
132148 for (int m = 0 ; m <numberOfMutations ;m ++){
133- int randomInstructionIndex = rand .nextInt (agents [i ].instructions .size ()-10 ,agents [i ].instructions .size ());
149+ int randomInstructionIndex = rand .nextInt (agents [i ].instructions .size ()-20 ,agents [i ].instructions .size ());
134150 agents [i ].instructions .set (randomInstructionIndex ,rand .nextInt (5 )); //randomize instructions
135151 }
136152 }
0 commit comments