Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

* (this is GA.java) */ public class GA { int PopulationSize; int iter; int numBits; double crossoverRate; double mutationRate; public GA(int PopulationSize, int iter, int

* (this is GA.java) */ public class GA { int PopulationSize; int iter; int numBits; double crossoverRate; double mutationRate;

public GA(int PopulationSize, int iter, int numBits, double crossoverRate, double mutationRate){ this.PopulationSize = PopulationSize; this.iter = iter; this.numBits = numBits; this.crossoverRate = crossoverRate; this.mutationRate = mutationRate;

}

public void runGA(){

//encoding and initialized the first population int[][] Population = Encoding(PopulationSize, numBits); //calculate the fitness

double[] fitness = fitnessEvaluation(Population);

for(int i=0; i

int[][] newPopulation = new int[PopulationSize][numBits];

int j = 0; while(j< PopulationSize){ int[][] parents=Selection(Population, fitness);

//crossover int[][] children = Crossover(crossoverRate,parents); //mutation int[][] Newchildren = Mutation(mutationRate,children);

//put the new children into the new population newPopulation[j]=Newchildren[0]; j++; newPopulation[j]=Newchildren[1]; j++;

}

//Evaluate the new generation Population = newPopulation; fitness = fitnessEvaluation(Population);

} display(Population); findBest(fitness, Population);

} /** * Encode and initialize the first population * @param n * @param numBits * @return */ private int[][] Encoding(int n, int numBits){ int[][] Population = new int[n][numBits]; for(int i=0; i

private double[] fitnessEvaluation(int[][] Population){ double[] fitness = new double[Population.length]; //for each individual, //Decode the value from Binary to Decimal Number, and calculat it use the function 15x - x^2

return fitness; } /** * Use the Roulettle Wheel selection * Remember that the sum of the fitness ratio is equal to 1. */ private int[][] Selection(int[][] Population, double[] fitness){

int[][] Parents=new int[2][Population[0].length]; //To do

return Parents; } /** * Crossover */ private int[][] Crossover(double crossoverRate, int[][] children){ int[][] newChildren = new int[2][children[0].length]; //To Do, use the one-point crossover

return newChildren; } /** * Mutation with a probability * @param mutationRate, probability that a pair of children will apply mutation * @param children, a pair of chromosomes/individuals * @return newChildren, a new pair of children after mutation */ private int[][] Mutation(double mutationRate, int[][] children){ int[][] newChildren = new int[2][children[0].length]; //To Do use the mutationRate to select one bit to do the mutation

return newChildren; } /** * Find the best individual and print the result */ private void findBest(double[] fitness, int[][] Population){ if(fitness == null){ System.out.println("No Fitness"); } double best = fitness[0]; int index = 0; for(int i=1; ibest){ best = fitness[i]; index = i; } System.out.println("The best individual is: " + index);

System.out.println("The integer is "+ best);

}

/** * Display */ private void display(int[][] Population){ if(Population == null){ System.out.println("Empty"); } for (int[] Population1 : Population) { for (int j = 0; j

}

**(This runGA.java) * * */ public class runGA {

/** * @param args the command line arguments */ public static void main(String[] args) {

int populationSize = 20; int maxIteration = 20; int numBits = 10; double crossoverRate = 0.7; double mutationRate = 0.001; GA ga = new GA(populationSize,maxIteration,numBits,crossoverRate,mutationRate); ga.runGA(); }

}

Implement a Genetic Algorithm (GA) to compute a function 1023X-X2 , where 0

1023. In order to represent X in binary strings, we need to use 10 bits.

The following GA parameters should be used for the initial run of the implementation:

The chromosome population N = 10;

Crossover probability pc = 0.7;

Mutation probability pm = 0.001;

Fitness function is defined by:

f(X) = 1023X X2

Stop criterion: set the maximum generation (or iteration) = 10. (Which means that

you will stop after 10 genetic cycles).

Once the initial population is generated, the fitness of each chromosome needs to be

calculated (use the fitness function provided).

The chromosomes chance of being selected for mating is calculated as:

Individual chromosomes fitness / populations total fitness.

(populations total fitness = sum of all individual chromosomes fitness).

You are going to use the Roulette Wheel Selection to do the selection.

Please go through the lecture slides for details.

Code provided:

Part of the code is given to you, which have two java classes. GA.java to do: code to be implemented for the GA part. All methods/classes such as

for initialization (encoding), selection, crossover and mutation.

runGA.java main class to start the code.

You jobs is to finish the GA.java class and test your results.

1. Show the best fitness youve found and the best individual after 10 iterations.

2. Show a plot displaying the best fitness vs. the number of iteration starting with the

initial parameter setting. (You can use excel, matlab, or mathematica to generate the

plots). Sample plot see below. (this is only a sample)

3. Show a plot displaying the average fitness vs. the number of iteration starting with the

initial parameter setting. (The average fitness is the populations total fitness/# of

individuals).

4. Screenshots of successful running of your code.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions