Question: Help with genetic algo implementation package homework1; public class GA { int PopulationSize; int iter; int numBits; double crossoverRate; double mutationRate; public GA(int PopulationSize, int

 Help with genetic algo implementation package homework1; public class GA {

Help with genetic algo implementation

package homework1;

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

//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 for(int j= 0; j double x = java.lang.Math.random(); if(x<.5 population else return calculate the fitness value of each individual>

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

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; i if(fitness[i]>best){ 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 System.out.print(Population1[j] + " "); } System.out.println(); } }

}

package homework1;

public class runGA {

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

int populationSize = 10; int maxIteration = 10; int numBits = 6; 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 63XX, where 0sX s 63. In order to represent X in binary strings, we need to use 6 bits. x-1 *25+1*24+1 *23+1 *22+1 *24 1 *20-32+ 16 + 8 + 4 + 2 + 1 63 X-1 25+0 2023 0 2202+0*2-32+0+0+0+0+0 32 1 00 0 0 X-0 250 240 230 22+0*2o20-00+0+0+0+0 0 Implement a Genetic Algorithm (GA) to compute a function 63XX, where 0sX s 63. In order to represent X in binary strings, we need to use 6 bits. x-1 *25+1*24+1 *23+1 *22+1 *24 1 *20-32+ 16 + 8 + 4 + 2 + 1 63 X-1 25+0 2023 0 2202+0*2-32+0+0+0+0+0 32 1 00 0 0 X-0 250 240 230 22+0*2o20-00+0+0+0+0 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!