Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ARITIFICAL INTELLIGENCE CODING IS IN JAVA //Generic algo package ga; public class GeneticAlgorithm { protected int mPopulationSize; protected int mTournamentsSize; protected double mCrossoverProb; protected double

ARITIFICAL INTELLIGENCE

CODING IS IN JAVA

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

//Generic algo

package ga;

public class GeneticAlgorithm {

protected int mPopulationSize;

protected int mTournamentsSize;

protected double mCrossoverProb;

protected double mMutationProb;

public GeneticAlgorithm(int populationSize,

int tournamentsSize, double crossoverProb, double mutationProb) {

mPopulationSize = populationSize;

mTournamentsSize = tournamentsSize;

mCrossoverProb = crossoverProb;

mMutationProb = mutationProb;

// ...

createInitialPopulation();

}

public void createInitialPopulation() {

// to be implemented

}

public void runOneGeneration() {

// to be implemented

}

public double getAverageFitness() {

// to be implemented; remove 0.0

return 0.0;

}

public double getBestFitness() {

// to be implemented; remove 0.0

return 0.0;

}

// other methods to be implemented

}

//RunGA

package main;

import javax.swing.SwingUtilities;

import utils.*;

import ga.GeneticAlgorithm;

public class RunGA {

@SuppressWarnings("unchecked")

public static void main(String args[]) {

int populationSize = 20;

int tournamentsSize = 2;

double crossoverProb = 0.7;

double mutationProb = 0.001;

int maxNumOfGenerations = 50;

double bestFitness = 0.0;

final double[] yBestFitness = new double[maxNumOfGenerations];

final double[] yAverageFitness = new double[maxNumOfGenerations];

// run GA

GeneticAlgorithm ga = new GeneticAlgorithm(populationSize,

tournamentsSize, crossoverProb, mutationProb);

yBestFitness[0] = ga.getBestFitness();

yAverageFitness[0] = ga.getAverageFitness();

for (int i = 0; i

double bestFitnessCurrentRound;

ga.runOneGeneration();

bestFitnessCurrentRound = ga.getBestFitness();

if (bestFitnessCurrentRound > bestFitness) {

bestFitness = bestFitnessCurrentRound;

}

yBestFitness[i] = bestFitness;

yAverageFitness[i] = ga.getAverageFitness();

}

System.out.println("Best overall fitness: " + ga.getBestFitness());

System.out.println("Average overall fitness: " + ga.getAverageFitness());

// plot graph

SwingUtilities.invokeLater(new Runnable() {

public void run() {

new Plot(yAverageFitness, yBestFitness).setVisible(true);

}

});

}

}

//Fitness

package utils;

public class Fitness {

private double fitness;

public double calculateFitness(double x, double y) {

fitness = Math.exp(-Math.pow(y+1.0,2.0)-Math.pow(x,2))*Math.pow(x-1.0,2.0)+Math.exp(-Math.pow(x,2)-Math.pow(y,2))*(-x+Math.pow(x,3)+Math.pow(y,5));

return fitness;

}

}

//Plot

package utils;

import java.awt.BorderLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;

import org.jfree.chart.JFreeChart;

import org.jfree.data.xy.XYDataset;

import org.jfree.data.xy.XYSeries;

import org.jfree.data.xy.XYSeriesCollection;

@SuppressWarnings("serial")

public class Plot extends JFrame {

public Plot(double[] avgValues, double[] bestValues) {

super("Genetic Algorithm");

JPanel chartPanel = createChartPanel(avgValues, bestValues);

add(chartPanel, BorderLayout.CENTER);

setSize(800, 600);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

}

private JPanel createChartPanel(double[] avgValues, double[] bestValues) {

String chartTitle = "Fitness Chart";

String xAxisLabel = "Generations";

String yAxisLabel = "Fitness Value";

XYDataset dataset = createDataset(avgValues, bestValues);

JFreeChart chart = ChartFactory.createXYLineChart(chartTitle,

xAxisLabel, yAxisLabel, dataset);

return new ChartPanel(chart);

}

private XYDataset createDataset(double[] avgValues, double[] bestValues) {

XYSeriesCollection dataset = new XYSeriesCollection();

XYSeries series1 = new XYSeries("Average Fitness");

XYSeries series2 = new XYSeries("Best Fitness");

for (int i = 0; i

series1.add(i, avgValues[i]);

series2.add(i, bestValues[i]);

}

dataset.addSeries(series1);

dataset.addSeries(series2);

return dataset;

}

}

Show transcribed image text

Description Your task is to implement a Genetic algorithm (GA) to find the maximum of the following function: 3 .3 where x and y are real numbers between -3 and 3. You can represent the problem variables as a chromosome by concatenating x and y as a binary string using 8-bits for each variable: 0001 0 100 01 101 The calculation of x and y of a chromosome is done as follows: 1. A chromosome (string of 16 bits) is partitioned into two 8-bit strings 10 0 0 1 0 10and0 011 101 1 2. These strings are converted from binary (base 2) to decimal (base 10): (10001010)2 -Ix27 +0x26+0x25 +0x24 +1x23 +0x22+1x21 +0x20 138)10 and (0011 101)2-0x27 +0x26+lx25+lx24+1x23+0x22+1x21 +1x20 - (59)10

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

Question

Write the difference between sexual and asexual reproduction.

Answered: 1 week ago

Question

What your favourite topic in mathematics?

Answered: 1 week ago

Question

Briefly describe vegetative reproduction in plants.

Answered: 1 week ago