// GA to evolve the string "to be or not to be"
   // Based on Dan Shiffman's Nature of Code, 2012
   // Modified by Deepak Kumar [2021]
class DNA {
   char[] genes;    // target string is 18 chars long
   int n;           // n is the number of genes
   float fitness;   // fitness of this individual
 DNA(int n) {
   this.n = n;
   genes = new char[n];
   for (int i=0; i < genes.length; i++)
   genes[i] = (char) random(32, 128);    // char codes for ' ', 'a', .. 'z'
   this.fitness();
   }
   
   void fitness() {  // fitness is the % of chars that are in place
   int score = 0;
   for (int i=0; i < genes.length; i++) {
   if (genes[i] == target.charAt(i)) {
   score++;
   }
   }
   fitness = float(score)/target.length();
   } // fitness()
   
   DNA crossover(DNA partner) {  // Produces just one offspring - child
   DNA child = new DNA(n);
   
   int midpoint = int(random(genes.length));
   
   for (int i = 0; i < genes.length; i++) {
   if (i > midpoint)
   child.genes[i] = genes[i];
   else
   child.genes[i] = partner.genes[i];
   }
   child.fitness();
   return child;
   } // crossover()
   
   void mutate(float mutationRate){
   for (int i = 0; i < genes.length; i++) {
   if (random(1) < mutationRate) {
   genes[i] = (char)random(32, 128);
   }
   }
   this.fitness();
   } // mutate()
   
   String getPhrase() {
   return new String(genes);
   } // getPhrase()
} // class DNA