I need a help with some functionalities to enable me implement my code. The Java program uses Genetic Algorithms to recognize a string. Below is the code and a description of the intended purpose as well as a Flow Chart diagram that described the process of how the program should work. The program is intended to work as follows: The user can input the string to guess. The user can enter the number or organisms in the population. The user can enter the number of generations to create. The user can enter the mutation probability. The user can observe the progress of the GA, for example print the best answer in every 10th generation. After the prescribed number of generations, the program should print best/final answer. import java.util.Scanner; public class GATest { public static void main(String[] arg) { boolean repeat = true; while(repeat) { Scanner myScanner = new Scanner(System.in); System.out.print("\nEnter string to guess--»"); String goal = myScanner.nextLine(); System.out.print("Enter number of organisms per generation--»"); int popSize = Integer.parseInt(myScanner.next()); System.out.print("Enter number of generations--»"); int generations = Integer.parseInt(myScanner.next()); System.out.print("Enter mutation probability--»"); double mutateProb = Double.parseDouble(myScanner.next()); System.out.println(); Population aPopulation = new Population(goal, popSize, generations, mutateProb); aPopulation.iterate(); System.out.println("Repeat? y/n"); String answer = myScanner.next(); if (answer.toUpperCase().equals("Y")) repeat = true; else repeat = false; } } } import java.util.Random; public class Organism implements Comparable { String value, goalString; double fitness; int n; Random myRandom = new Random(); public Organism(String goalString) { value=""; this.goalString=goalString; this.n= goalString.length(); for(int i=0; imutateProb) newString = newString+value.charAt(i); else { int j = myRandom.nextInt(27); if (j==26) newString=newString+" "; else { int which = myRandom.nextInt(2); if (which ==0) j=j +65; else j=j+97; newString = newString+(char)j; } } } this.setValue(newString); } } The Organism class Member variables A String or Array of char or an ArrayList of Character. This String represents the Organism’s guess for the unknown String. The number of characters in the Organism should be the same as the number of characters in the unknown String.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I need a help with some functionalities to enable me implement my code. The Java program uses Genetic Algorithms to recognize a string. Below is the code and a description of the intended purpose as well as a Flow Chart diagram that described the process of how the program should work.

The program is intended to work as follows:

  • The user can input the string to guess.
  • The user can enter the number or organisms in the population.
  • The user can enter the number of generations to create.
  • The user can enter the mutation probability.
  • The user can observe the progress of the GA, for example print the best answer in every 10th generation.
  • After the prescribed number of generations, the program should print best/final answer.

 

import java.util.Scanner;
public class GATest {
    public static void main(String[] arg) {
        boolean repeat = true;
        while(repeat) {
            Scanner myScanner = new Scanner(System.in);
            System.out.print("\nEnter string to guess--»");
            String goal = myScanner.nextLine();
            System.out.print("Enter number of organisms per generation--»");
            int popSize = Integer.parseInt(myScanner.next());
            System.out.print("Enter number of generations--»");
            int generations = Integer.parseInt(myScanner.next());
            System.out.print("Enter mutation probability--»");
            double mutateProb = Double.parseDouble(myScanner.next());
            System.out.println();
            Population aPopulation = new Population(goal, popSize, generations, mutateProb);
            aPopulation.iterate();
            System.out.println("Repeat? y/n");
            String answer = myScanner.next();
            if (answer.toUpperCase().equals("Y"))
                repeat = true;
            else
                repeat = false;
        }
    }
}

import java.util.Random;
public class Organism implements Comparable<Organism> {
    String value, goalString;
    double fitness;
    int n;
    Random myRandom = new Random();
    public Organism(String goalString) {
        value="";
        this.goalString=goalString;
        this.n= goalString.length();
        for(int i=0; i<n; i++) {
            int j = myRandom.nextInt(27);
            if(j==26)
                value=value+" ";
            int which = myRandom.nextInt(2);
            if(which==0)
                j=j+65;
            else
                j=j+97;
            value=value+ (char)j;
        }
    }

    public Organism(String goalString, String value, int n) {
        this.goalString=goalString;
        this.value = value;
        this.n=n;
    }

    public Organism() {
    }

    public String getValue() {
        return this.value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String toString() {
        return value +" "+ goalString+" "+getFitness(goalString);
    }

    public int getFitness(String aString) {
        int count =0;
        for(int i=0; i< this.n; i++)
            if(this.value.charAt(i)== aString.charAt(i))
                count++;
        return count;
    }

    public int compareTo(Organism other) {
        int thisCount, otherCount; thisCount=getFitness(goalString); otherCount=other.getFitness(goalString);
        if (thisCount == otherCount)
            return 0;
        else if (thisCount < otherCount)
            return 1;
        else
            return -1;
    }

    public Organism[] mate(Organism other) {
        Random aRandom = new Random();
        int crossOver = aRandom.nextInt(n);
        String child1="", child2="";
        for (int i=0; i< crossOver; i++) {
            child1=child1+this.value.charAt(i);
            child2 = child2+other.value.charAt(i);
        }
        for (int i= crossOver; i<n; i++) {
            child1=child1+other.value.charAt(i);
            child2=child2+this.value.charAt(i);
        }

        Organism[] children= new Organism[2];
        children[0] = new Organism(goalString, child1,n);
        children[1] = new Organism(goalString, child2, n);
        return children;
    }

    public void mutate(double mutateProb) {
        String newString="";
        for (int i=0; i< n; i++) {
            int k = myRandom.nextInt(100);
            if (k/100.0 >mutateProb)
                newString = newString+value.charAt(i);
            else {
                int j = myRandom.nextInt(27);
                if (j==26)
                    newString=newString+" ";
                else {
                    int which = myRandom.nextInt(2);
                    if (which ==0)
                        j=j +65;
                    else
                        j=j+97;
                    newString = newString+(char)j;
                }
            }
        }
        this.setValue(newString);
    }
}

 

The Organism class

 Member variables

  • A String or Array of char or an ArrayList of Character. This String represents the Organism’s guess for the unknown String. The number of characters in the Organism should be the same as the number of characters in the unknown String.

 

Next
Generation
GA Flowchart
Create initial design population
X=9000 Evaluate obj. function of designs
λ=5000
=
= 1000
Select and Reproduce
(Create new designs)
Replace designs of the old
population with new designs
Stop?
Transcribed Image Text:Next Generation GA Flowchart Create initial design population X=9000 Evaluate obj. function of designs λ=5000 = = 1000 Select and Reproduce (Create new designs) Replace designs of the old population with new designs Stop?
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY