55
66public class GenerationAlgorithm {
77 public final int POPULATION ;
8- public final int NUMBER_OF_GENERATIONS = 10 ;
9- public final double MUTATION_CHANCE = 0.1 ;
8+ public final int NUMBER_OF_GENERATIONS = 200 ;
9+ public final double MUTATION_CHANCE = 0.2 ;
1010 public final int MAX_MUTATION_COUNT = 5 ;
1111 public final int ELITISM_NUMBER = 2 ;
1212 public final int NUMBER_OF_GENERATIONS_PER_INCREASE = 10 ;
13+ public final int INSTRUCTION_SIZE_INCREASE = 10 ;
1314 static Random rand = new Random ();
1415 ArrayList <Integer > eliteIndexes = new ArrayList <Integer >();
1516 int generationNumber = 0 ;
@@ -35,6 +36,12 @@ public void checkGeneration(){
3536 agent .reset ();
3637 }
3738 generationNumber ++;
39+ if (generationNumber >=instructionSize && generationNumber %NUMBER_OF_GENERATIONS_PER_INCREASE == 0 ){
40+ for (Agent agent : agents ){
41+ assignRandomInstructions (agent , INSTRUCTION_SIZE_INCREASE );
42+ }
43+ instructionSize += INSTRUCTION_SIZE_INCREASE ;
44+ }
3845 }
3946 }
4047 public void createPopulation (){
@@ -111,11 +118,21 @@ public void crossOver(ArrayList<Integer[]> parents){
111118 }
112119 agents [i ].instructions .clear ();
113120 agents [i ].instructions .addAll (instruction1 );
121+
114122 if (Math .random ()<MUTATION_CHANCE ){
115- int numberOfMutations = rand .nextInt (MAX_MUTATION_COUNT );
116- for (int m = 0 ; m <numberOfMutations ;m ++){
117- int randomInstructionIndex = rand .nextInt (agents [i ].instructions .size ());
118- agents [i ].instructions .set (randomInstructionIndex ,rand .nextInt (5 )); //randomize instructions
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 );
125+ for (int m = 0 ; m <numberOfMutations ;m ++){
126+ int randomInstructionIndex = rand .nextInt (agents [i ].instructions .size ());
127+ agents [i ].instructions .set (randomInstructionIndex ,rand .nextInt (5 )); //randomize instructions
128+ }
129+ }
130+ else { //only randomize the last 10 (%60)
131+ int numberOfMutations = rand .nextInt (MAX_MUTATION_COUNT );
132+ for (int m = 0 ; m <numberOfMutations ;m ++){
133+ int randomInstructionIndex = rand .nextInt (agents [i ].instructions .size ()-10 ,agents [i ].instructions .size ());
134+ agents [i ].instructions .set (randomInstructionIndex ,rand .nextInt (5 )); //randomize instructions
135+ }
119136 }
120137 }
121138 }
0 commit comments