11package JavaRacer ;
22import java .util .ArrayList ;
3- import java .util .Arrays ;
43import java .util .Collections ;
54import java .util .Random ;
65
@@ -11,10 +10,11 @@ public class GenerationAlgorithm {
1110 public final int MAX_MUTATION_COUNT = 5 ;
1211 public final int ELITISM_NUMBER = 2 ;
1312 public final int NUMBER_OF_GENERATIONS_PER_INCREASE = 10 ;
14- static Random rand = new Random (0 );
13+ static Random rand = new Random ();
1514 ArrayList <Integer > eliteIndexes = new ArrayList <Integer >();
1615 int generationNumber = 0 ;
1716 int instructionSize = 20 ;
17+ double bestFit = 0 ;
1818 Agent [] agents ;
1919
2020 public GenerationAlgorithm (Agent [] agents ){
@@ -27,9 +27,8 @@ public void checkGeneration(){
2727 ArrayList <Double > fitnesses = getFitness ();
2828 ArrayList <Double > weights = getWeights (fitnesses );
2929 ArrayList <Integer []> parentsList = getParents (weights );
30- for (Integer [] integers : parentsList ) {
31- System .out .println (Arrays .toString (integers ));
32- }
30+ this .bestFit = fitnesses .get (eliteIndexes .get (0 ));
31+ System .out .println ("Best fit: " + fitnesses .get (eliteIndexes .get (0 )) + " " + fitnesses .get (eliteIndexes .get (1 )));
3332 System .out .printf ("Generation completed number: %d\n " ,generationNumber );
3433 crossOver (parentsList );
3534 for (Agent agent : agents ){
@@ -55,6 +54,7 @@ public ArrayList<Double> getFitness(){
5554 }
5655 ArrayList <Double > fitnessCopy = new ArrayList <>(fitness );
5756 Collections .sort (fitnessCopy , Collections .reverseOrder (null ));
57+ eliteIndexes .clear ();
5858 for (int i = 0 ; i <ELITISM_NUMBER ;i ++){
5959 eliteIndexes .add (fitness .indexOf (fitnessCopy .get (i )));
6060 }
@@ -95,28 +95,29 @@ public ArrayList<Integer[]> getParents(ArrayList<Double> weights){
9595 public void crossOver (ArrayList <Integer []> parents ){
9696 ArrayList <ArrayList <Integer >> instructionsTemp = new ArrayList <ArrayList <Integer >>();
9797 for (int i = 0 ; i <POPULATION ; i ++){
98- instructionsTemp .add (agents [i ].instructions );
98+ instructionsTemp .add (new ArrayList < Integer >( agents [i ].instructions ) );
9999 }
100100 for (int i = 0 ; i <parents .size ();i ++) {
101- Integer [] parentPair = parents .get (i );
102- if (eliteIndexes .contains (parentPair [0 ])){
101+ if (eliteIndexes .contains (i )){
103102 continue ;
104103 }
105- if (parentPair [0 ] != parentPair [1 ]){
106- int crossOverLength = rand .nextInt (10 );
107- int indexOfCrossing = rand .nextInt (agents [parentPair [0 ]].instructions .size ()-crossOverLength );
108- for (int j = indexOfCrossing ; j <indexOfCrossing +crossOverLength ;j ++){
109- agents [parentPair [0 ]].instructions .set (j ,instructionsTemp .get (parentPair [1 ]).get (j ));
110- }
104+ Integer [] parentPair = parents .get (i );
105+ int crossOverLength = rand .nextInt (10 );
106+ int indexOfCrossing = rand .nextInt (agents [parentPair [0 ]].instructions .size ()-crossOverLength );
107+ ArrayList <Integer > instruction1 = new ArrayList <Integer >(instructionsTemp .get (parentPair [0 ]));
108+ ArrayList <Integer > instruction2 = new ArrayList <Integer >(instructionsTemp .get (parentPair [1 ]));
109+ for (int j = indexOfCrossing ; j <indexOfCrossing +crossOverLength ;j ++){
110+ instruction1 .set (j , instruction2 .get (j ));
111111 }
112+ agents [i ].instructions .clear ();
113+ agents [i ].instructions .addAll (instruction1 );
112114 if (Math .random ()<MUTATION_CHANCE ){
113115 int numberOfMutations = rand .nextInt (MAX_MUTATION_COUNT );
114116 for (int m = 0 ; m <numberOfMutations ;m ++){
115- int randomInstructionIndex = rand .nextInt (agents [parentPair [ 0 ] ].instructions .size ());
116- agents [parentPair [ 0 ] ].instructions .set (randomInstructionIndex ,rand .nextInt (5 )); //randomize instructions
117+ int randomInstructionIndex = rand .nextInt (agents [i ].instructions .size ());
118+ agents [i ].instructions .set (randomInstructionIndex ,rand .nextInt (5 )); //randomize instructions
117119 }
118120 }
119-
120121 }
121122 }
122123}
0 commit comments