The writer is very fast, professional and responded to the review request fast also. Thank you.
Hi can you assist fix my code? I think im missing some functionalities in the class organism or toString method that is making the program unable to read, guess or converge the string correctly. I have run it using short string like “HELLO” and it worked well. However, i tried to input a long string to guess it but that didnt work. Im kinda stuck on it.I have tried increased and decreased the the number of generation, mutation probability yet it didnt work. The Java program uses Genetic Algorithms to recognize a string. Below is my code, sample out and a Flow Chart diagram that described the process of how the program should work. Thank you.
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.
· The program should “guesses” the string “We the people of the United States in order to perform a perfect union” and print result
Sample of the Output:
Enter string to guess–›Four score and seven years ago our fathers brought forth on this continent a new nation conceived in liberty
Enter number of organisms per generation–>120
Enter number of generations–>3000
Enter mutation probability–>0.01
GENERATION: 0 GOAL: Four score and seven years ago our fathers brought forth on this continent a new nation conceived in liberty
Strongest
GHtA YwNVHswQYKvCzwjynGgQPGSY [OpXMFDVifLOf×hptHNdNTrMthprgudzs {r [GoCJjwzfnkkJpIgxZJkFeCfznDk (INupztzZAhG
GENERATION: 1 GOAL: Four score and seven years ago our fathers brought forth on this continent a new nation conceived in liberty
Strongest
GHtA YwNVHswQYKvCzwveynGgQPGSY [OpXMFDVifL0fxhptHNdNTrMthprgudzs {r [GoCJjwzfnkkJpPg×ZJkFeCfznDk (INupztzZAhG
GENERATION: 2 GOAL: Four score and seven years ago our fathers brought forth on this continent a new nation conceived in liberty
Strongest:
GENERATION: 4 GOAL: Four score and seven years ago our fathers brought forth on this continent a new nation conceived in liberty
GENERATION:
Strongest :
1857 GOAL: Four score and seven years ago our fathers brought forth on this continent a new nation conceived in liberty
Four score and seven years ago our fathers brought forth on this continent a Aew nation conceived in liberty
GENERATION:
Strongest:
858 GOAL: Four score and seven years ago our fathers brought forth on this continent a new nation conceived in liberty
: Four score and seven years ago our fathers brought forth on this continent a new nation conceived in liberty
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class GATest // class
{
public static void main(String[] arg) {
boolean repeat = true;
Scanner myScanner = new Scanner(System.in);
while (repeat) {
System.out.print(“nEnter string to guess: “); // enter input1
String goal = myScanner.nextLine();
int popSize;
while (true) {
try {
System.out.print(“Enter number of organisms per generation: “);//enter input2
popSize = Integer.parseInt(myScanner.next());
myScanner.nextLine();
break; // exit the loop if parsing is successful
} catch (NumberFormatException e) {
System.out.println(“Invalid input. Please enter a valid integer.”);
myScanner.nextLine();
}
}
int generations;
while (true) {
try {
System.out.print(“Enter number of generations: “); //enter input3
generations = Integer.parseInt(myScanner.next());
myScanner.nextLine();
break; // exit the loop if parsing is successful
} catch (NumberFormatException e) {
System.out.println(“Invalid input. Please enter a valid integer.”);
myScanner.nextLine();
}
}
double mutateProb;
while (true) {
try {
System.out.print(“Enter mutation probability (between 0 and 1): “); //enter input 4
mutateProb = Double.parseDouble(myScanner.next());
if (mutateProb >= 0 && mutateProb <= 1) {
myScanner.nextLine();
break; // exit the loop if parsing is successful and within the correct range
} else {
System.out.println(“Invalid input. Please enter a valid value between 0 and 1.”);
myScanner.nextLine();
}
} catch (NumberFormatException e) {
System.out.println(“Invalid input. Please enter a valid decimal number.”);
myScanner.nextLine();
}
}
Population aPopulation = new Population(goal, popSize, generations, mutateProb, myScanner);
for (int gen = 1; gen <= generations; gen++) {
aPopulation.iterate();
if (gen % 10 == 0) {
Organism bestOrganism = aPopulation.getBestOrganism();
double avgFitness = aPopulation.getAverageFitness();
System.out.println(“Generation ” + gen + “: Best Answer – ” + bestOrganism.getValue() +
“, Average Fitness: ” + avgFitness);
}
}
Organism finalAnswer = aPopulation.getBestOrganism();
System.out.println(“Final Answer: ” + finalAnswer.getValue());
System.out.print(“Repeat? y/n: “);
String answer = myScanner.next();
repeat = answer.trim().equalsIgnoreCase(“Y”);
myScanner.nextLine();
}
myScanner.close();
}
}
class Population {
private List<Organism> organisms;
private String goalString;
private int popSize;
private int generations;
private double mutateProb;
private double averageFitness;
private Scanner myScanner;
public Population(String goalString, int popSize, int generations, double mutateProb, Scanner myScanner) {
this.goalString = goalString;
this.popSize = popSize;
this.generations = generations;
this.mutateProb = mutateProb;
this.organisms = new ArrayList<>();
this.myScanner = myScanner;
// Initialize the population
for (int i = 0; i < popSize; i++) {
organisms.add(new Organism(goalString));
}
}
public void iterate() {
List<Organism> newPopulation = new ArrayList<>();
for (int i = 0; i < popSize; i++) {
Organism parent1 = selectParent();
Organism parent2 = selectParent();
Organism[] children = parent1.mate(parent2);
newPopulation.add(children[0]);
newPopulation.add(children[1]);
}
for (Organism organism : newPopulation) { // mutate organinsm
organism.mutate(mutateProb);
}
organisms = newPopulation;
updateAverageFitness(); // Update average fitness
}
private Organism selectParent() { // Select a random organism
double totalFitness = 0;
for (Organism organism : organisms) {
totalFitness += organism.getFitness(goalString);
}
double rand = Math.random() * totalFitness;
double runningTotal = 0;
for (Organism organism : organisms) {
runningTotal += organism.getFitness(goalString);
if (runningTotal >= rand) {
return organism;
}
}
return organisms.get(organisms.size() – 1);
}
private void updateAverageFitness() {
double totalFitness = 0;
for (Organism organism : organisms) {
totalFitness += organism.getFitness(goalString);
}
double avgFitness = totalFitness / popSize;
setAverageFitness(avgFitness);
}
public Organism getBestOrganism() {
Collections.sort(organisms);
return organisms.get(0);
}
public double getAverageFitness() {
return averageFitness;
}
private void setAverageFitness(double averageFitness) {
this.averageFitness = averageFitness;
}
}
class Organism implements Comparable<Organism> {
private String value, goalString;
private int n;
private 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: ” + value + “, Goal String: ” + goalString + “, Fitness: ” + 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);
}
}
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more