FileDocCategorySizeDatePackage
GARunModule.javaAPI DocExample7534Thu Apr 06 17:01:42 BST 2000None

GARunModule

public class GARunModule extends Object

Fields Summary
static int[]
GenerationFirst
static double[]
firstFitnessArray
static int[]
GenerationSecond
Constructors Summary
Methods Summary
static intbestIndividual(int M)

    //returns the index of the best individual in the current generation
    double bestFitness=0;
    int bestIndex=0;
    for(int row=0; row<M; row++) {
      if(firstFitnessArray[row][1] > bestFitness) {
        bestFitness = firstFitnessArray[row][1];
        bestIndex=row;
      }
    }//end for
    return bestIndex;
  
static voidcalculateFitnesses(int M, int L)

    // work out fitness for each row and store it in array
    double tempTotalFitness=0.0;
    System.out.println("Rows:"+M+"Col:"+L);
    for(int row=0; row<M; row++) {
      firstFitnessArray[row][0] = Fitness.get(GenerationFirst,row,L);
      tempTotalFitness = tempTotalFitness + firstFitnessArray[row][0];
      //GUIMain.display(Float.toString(firstFitnessArray[row][0]));
    }
    System.out.println("Temp tot fit = " + tempTotalFitness);
    //Now work out normalised fitnessess ...
    for(int row=0; row<M; row++) {
      firstFitnessArray[row][1] = firstFitnessArray[row][0] / tempTotalFitness;
      //GUIMain.(Float.toString(firstFitnessArray[row][1]));
      
    } 
      
  
public static voidchooseMethod(int row, int M, int L, double Pr, float Pc, float Pm, int numChildren)

     // Chooses one of reproduction, crossover or mutation
     // Note that we are processing the rowth row of GenerationFirst, but want to
           // fill the numChildrenth row of GenerationSecond

             //now decide on which method to use
             int methodSelector=RandomNos.rangeRand(100); // 0..99
             if(methodSelector <= Pr) { // reproduction
               reproduce(row,numChildren,L);
               //GUIMain.("Reproducing");
             }//end repro decision
             else {
               if(methodSelector<Pr+Pc) { //crossover
                 crossover(row,numChildren,M,L);
                 //GUIMain.("Crossing");
               }
               else  {
                 mutate(row,numChildren,L);
                 //GUIMain.("Mutating");
               }//
             }// end of method selection
   
static voidcrossover(int inRow, int outRow, int M, int L)

   //crosses with another randomly chosen one, chosen with replacement
     boolean gotOne=false;

     int partnerIndex=0;
     while(!gotOne) {//find the partner by scanning down rows
       for(int counter=0; counter<M && !gotOne; counter++) {//out if gotOne is true
         if(RandomNos.floatRand()<firstFitnessArray[counter][1]) {
           gotOne=true;
           partnerIndex=counter; // remember me
         }//end if
       }//end for
     }//end while

     // have a partner and its number is partnerIndex
     int crossPoint = RandomNos.rangeRand(L);//walk along cols to get crosspoint 0..L-1
     for(int col=0; col<crossPoint; col++) { // copy to crosspoint
       GenerationSecond[outRow][col]=GenerationFirst[inRow][col];
       GenerationSecond[partnerIndex][col]=GenerationFirst[partnerIndex][col];
     }//end for
     // and swap for rest
     for(int col=crossPoint; col<L; col++) {
       GenerationSecond[outRow][col]=GenerationFirst[partnerIndex][col];
       GenerationSecond[partnerIndex][col]=GenerationFirst[inRow][col];
     }

   
static voiddumpStrings(int M, int L)

    for(int row=0; row<M; row++) {
      for(int col=0; col<L;col++) {
        System.out.print(GenerationFirst[row][col]);
      }
      System.out.print(" : "+ row+ " : "+ bestIndividual(M)+" :" );
      System.out.println(firstFitnessArray[row][0]+" :" + firstFitnessArray[row][1]);
    }
  
static voidinitialiseArrays(int rows, int cols)

  // Set up initial arrays
    firstFitnessArray = new double[rows][2];
    GenerationFirst = new int[rows][cols];
    GenerationSecond = new int[rows][cols]; 
    for(int i=0; i<rows; i++) {
      for(int j=0; j<cols; j++) {
        GenerationFirst[i][j] = RandomNos.binRand();
      }
    }
  
static voidmutate(int inRow, int outRow, int L)

   // mutates the ith individual
     int mutPlace = RandomNos.rangeRand(L);
     for(int col=0; col<L; col++) {
       if(mutPlace==col) {
         int bit = GenerationFirst[inRow][col];
         GenerationSecond[outRow][col] = (bit==1) ? 0 : 1;
       }
       else {
         GenerationSecond[outRow][col] = GenerationFirst[inRow][col];
       } 
     }
   
static voidreproduce(int inRow, int outRow, int L)

   
           
     // The output row is in another array and is different
     // passes on ith individual to next generation
     for(int col=0; col<L; col++) {
       GenerationSecond[outRow][col] = GenerationFirst[inRow][col];
       //System.out.println("outRow: "+outRow +"inRow "+ inRow+"Col "+col);
     }
   
public static voidrunGA(int M, int L, int G, double Pr, float Pc, float Pm)

     int numChildren; // num produced in this generation
     initialiseArrays(M,L);
     System.out.println("Arrays initialised: "+M+" rows and "+L+" columns");
     //dumpStrings(M,L);
     calculateFitnesses(M,L);
     //dumpStrings(M,L);
     //GUIMain.("Fitnesses assigned");
     System.out.print("Number of generations is "+G);
     //Start the generations
     for(int generation=0; generation<G; generation++) {
       GUIMain.displayGens(generation + " of " + G);
       if(GUIMain.stoppit==true) break;
       System.out.println("Starting generation "+generation);
       // Now do something with each according to probability
       numChildren = 0;
       while(numChildren<M) {
         //walk down the rows, trying each one
         for(int row=0; row<M && numChildren <M ; row++) { // try each individual: bomb out when finished
           if(RandomNos.floatRand() <= firstFitnessArray[row][1]) {
           //if we have a high normalised fitness, better chance of being picked
           // now decide which to do on us
           chooseMethod(row,M,L,Pr,Pc,Pm,numChildren);
           numChildren++; // and record that we've dealt with one. next gen is filling up
           }//end of whether individual is used or not
         }//end of for loop down rows to find individual
       }//end while for this generation

       //and copy the second gen back to the first ready to start again
       for(int row=0; row<M; row++) {
         for(int col=0; col<L; col++) {
           GenerationFirst[row][col]=GenerationSecond[row][col];
         }
       }
       //GUIMain.("Finished Generation "+generation);
       calculateFitnesses(M,L);
       System.out.println("Best fitness: " +firstFitnessArray[bestIndividual(M)][0]);
       GUIMain.displayFit(Double.toString(firstFitnessArray[bestIndividual(M)][0]));
       for(int k=0; k<L; k++) {
         System.out.print(GenerationFirst[bestIndividual(M)][k]);
         
       }
       String s="";
       for(int k=0; k<L; k++) {
         s=s+GenerationFirst[bestIndividual(M)][k];
       }
       GUIMain.displayBest(s);
       dumpStrings(M,L);
       System.out.println("Fitness "+ firstFitnessArray[bestIndividual(M)][0]);
       System.out.println(" ");
       
       if(firstFitnessArray[bestIndividual(M)][0] >= 0.9999f) System.exit(1);
     }// end generation loop