Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. public class Individual {
  2.     private String chromosome;
  3.     private double fitness;
  4.     private String target = "Hello World";
  5.    
  6.     public Individual(String chromosome) {
  7.         this.chromosome = chromosome;
  8.         setFitness(chromosome);
  9.     }
  10.    
  11.     public void setTarget(String target) {
  12.         this.target = target;
  13.     }
  14.  
  15.  
  16.     public String getChromosome() {
  17.         return this.chromosome;
  18.     }
  19.  
  20.     public double getFitness() {
  21.         return this.fitness;
  22.     }
  23.     /**
  24.      * Sets the fitness for a given chromosome based on the Edit Distance.
  25.      * @param fitness
  26.      * @param chromosome
  27.      */
  28.     private void setFitness(String chromosome) {
  29.         int f = 0;
  30.         //Iterates through the chromosome and looks for a match.
  31.         //Therefore perfect match = 11.
  32.         for(int i = 0;i < chromosome.length(); i++) {
  33.             if(chromosome.charAt(i) == this.target.charAt(i)) {
  34.                 f++;
  35.             }
  36.         }
  37.        
  38.         //Converts to a normalised number
  39.        
  40.         //double percentage = (f/11.0)*100;
  41.         this.fitness = f; //percentage
  42.     }
  43.    
  44.     public void setChromosome(String chromosome) {
  45.         this.chromosome = chromosome;
  46.         setFitness(chromosome);
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement