Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Random; public class GA { int PopulationSize; int iter; int numBits; double crossoverRate; double mutationRate; int maxIntValue = 1023; int minIntValue = 0; public

import java.util.Random;

public class GA {

int PopulationSize;

int iter;

int numBits;

double crossoverRate;

double mutationRate;

int maxIntValue = 1023;

int minIntValue = 0;

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);

for (int i = 0; i < iter; i++) {

double[] fitness = fitnessEvaluation(Population);

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

for (int j = 0; j < PopulationSize; j += 2) {

int[][] parents = Selection(Population, fitness);

int[][] children;

if (Math.random() < crossoverRate) {

children = Crossover(parents);

} else {

children = parents;

}

for (int k = 0; k < 2; k++) {

if (Math.random() < mutationRate) {

children[k] = Mutation(children[k]);

}

newPopulation[j + k] = children[k];

}

}

Population = newPopulation;

}

double[] 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 < n; i++)

for (int j = 0; j < numBits; j++) {

double x = Math.random();

if (x < .5)

Population[i][j] = 0;

else {

Population[i][j] = 1;

}

}

return Population;

}

/**

* Calculate the fitness value of each individual

*/

private double[] fitnessEvaluation(int[][] Population) {

double[] fitness = new double[Population.length];

for (int i = 0; i < Population.length; i++) {

int x = decode(Population[i]);

fitness[i] = 1023 * x - x * x;

}

return fitness;

}

/**

* Use the Roulette Wheel selection Remember that the sum of the fitness ratio

* is equal to 1.

*/

/**

* Selects two individuals from the population using fitness-proportional selection.

* @param Population the current population of individuals

* @param fitness an array of fitness values for each individual in the population

* @return a 2D array containing the selected parents

*/

private int[][] Selection(int[][] Population, double[] fitness) {

// create a 2D array to hold the selected parents

int[][] Parents = new int[2][numBits];

// calculate the total fitness of the population

double totalFitness = 0;

for (int i = 0; i < fitness.length; i++) {

totalFitness += fitness[i];

}

// create a random number generator

Random rand = new Random();

// generate a random number between 0 and the total fitness of the population

double r = rand.nextDouble() * totalFitness;

// initialize a variable to keep track of the partial sum of fitness values

double partialSum = 0;

// initialize a variable to keep track of the index of the selected individual

int selected = -1;

// loop through the fitness values, adding them to the partial sum and checking if it exceeds the random number

for (int i = 0; i < fitness.length; i++) {

partialSum += fitness[i];

if (partialSum >= r) {

// if the partial sum is greater than or equal to the random number, select the current individual and break out of the loop

selected = i;

break;

}

}

// add the selected individual to the first parent in the Parents array

Parents[0] = Population[selected];

// initialize the selected variable to 0 for the second parent selection

selected = 0;

// loop until a different individual is selected for the second parent

while (selected == 0) {

// generate a new random number

r = rand.nextDouble() * totalFitness;

// reset the partial sum to 0

partialSum = 0;

// loop through the fitness values again, adding them to the partial sum and checking if it exceeds the random number

for (int i = 0; i < fitness.length; i++) {

partialSum += fitness[i];

if (partialSum >= r) {

// if the partial sum is greater than or equal to the random number, select the current individual and break out of the loop

// ensure that the selected individual is different from the first parent

if (i != selected) {

Parents[1] = Population[i];

selected = 1;

break;

}

}

}

}

// return the selected parents

return Parents;

}

/**

* Crossover two parents to create two children

*/

private int[][] Crossover(int[][] parents) {

int[][] children = new int[2][numBits];

int crossoverPoint = (int) (Math.random() * numBits);

for (int i = 0; i < crossoverPoint; i++) {

children[0][i] = parents[0][i];

children[1][i] = parents[1][i];

}

for (int i = crossoverPoint; i < numBits; i++) {

children[0][i] = parents[1][i];

children[1][i] = parents[0][i];

}

return children;

}

/**

* Mutate a child based on the mutation rate

*/

private int[] Mutation(int[] child) {

for (int i = 0; i < numBits; i++) {

if (Math.random() < mutationRate) {

child[i] = child[i] ^ 1;

}

}

return child;

}

/**

* Decode a binary string to an integer value

*/

private int decode(int[] binary) {

int value = 0;

for (int i = 0; i < binary.length; i++) {

value += binary[i] * Math.pow(2, binary.length - i - 1);

}

return value;

}

/**

* Display the population

*/

private void display(int[][] Population) {

for (int i = 0; i < Population.length; i++) {

for (int j = 0; j < numBits; j++) {

System.out.print(Population[i][j] + " ");

}

System.out.println();

}

}

/**

* Find the best solution in the final population

*/

private void findBest(double[] fitness, int[][] Population) {

int bestIndex = 0;

for (int i = 1; i < fitness.length; i++) {

if (fitness[i] > fitness[bestIndex]) {

bestIndex = i;

}

}

int x = decode(Population[bestIndex]);

double y = 1023 * x - x * x;

System.out.println("Best Solution: x=" + x + ", y=" + y);

}

public static void main(String[] args) {

GA ga = new GA(50, 500, 10, 0.7, 0.01);

ga.runGA();

}

}

when i run this code error is showing as (

Main.java:16: error: class GA is public, should be declared in a file named GA.java public class GA { ^ 1 error)

can u resolve this and give me the modified error free 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

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions