Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Individual {
- private String chromosome;
- private double fitness;
- private String target = "Hello World";
- public Individual(String chromosome) {
- this.chromosome = chromosome;
- setFitness(chromosome);
- }
- public void setTarget(String target) {
- this.target = target;
- }
- public String getChromosome() {
- return this.chromosome;
- }
- public double getFitness() {
- return this.fitness;
- }
- /**
- * Sets the fitness for a given chromosome based on the Edit Distance.
- * @param fitness
- * @param chromosome
- */
- private void setFitness(String chromosome) {
- int f = 0;
- //Iterates through the chromosome and looks for a match.
- //Therefore perfect match = 11.
- for(int i = 0;i < chromosome.length(); i++) {
- if(chromosome.charAt(i) == this.target.charAt(i)) {
- f++;
- }
- }
- //Converts to a normalised number
- //double percentage = (f/11.0)*100;
- this.fitness = f; //percentage
- }
- public void setChromosome(String chromosome) {
- this.chromosome = chromosome;
- setFitness(chromosome);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement