0% found this document useful (0 votes)
6 views3 pages

!DOCTYPE HTML

The document presents an HTML page that implements a simple genetic algorithm for classification. It includes a button to run the simulation, which initializes a population, evaluates fitness, selects parents, and breeds new individuals over several generations. The best solution is displayed on the page after the algorithm runs.

Uploaded by

nekkonda21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

!DOCTYPE HTML

The document presents an HTML page that implements a simple genetic algorithm for classification. It includes a button to run the simulation, which initializes a population, evaluates fitness, selects parents, and breeds new individuals over several generations. The best solution is displayed on the page after the algorithm runs.

Uploaded by

nekkonda21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

<!

DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Genetic Algorithm for Classification</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.container {
max-width: 600px;
margin: auto;
}
button {
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>Genetic Algorithm for Classification</h1>
<p>Click the button to run the genetic algorithm simulation.</p>
<button id="run-algorithm">Run Genetic Algorithm</button>
<div id="output"></div>
</div>

<script>
// Simple representation of a genetic algorithm for classification
function runGeneticAlgorithm() {
const populationSize = 10;
const generations = 5;
let population = initializePopulation(populationSize);
let bestSolution = null;

for (let generation = 0; generation < generations; generation++) {


population = evaluatePopulation(population);
bestSolution = getBestSolution(population);
population = breedNewPopulation(population);
}

return bestSolution;
}

function initializePopulation(size) {
const population = [];
for (let i = 0; i < size; i++) {
// Randomly generate a solution (individual)
const individual = {
genes: Array.from({ length: 5 }, () =>
Math.round(Math.random())),
fitness: 0
};
population.push(individual);
}
return population;
}
function evaluatePopulation(population) {
population.forEach(individual => {
// Simple fitness function: count the number of 1s in genes
individual.fitness = individual.genes.reduce((sum, gene) => sum +
gene, 0);
});
return population;
}

function getBestSolution(population) {
return population.reduce((best, individual) => {
return (best.fitness > individual.fitness) ? best : individual;
});
}

function breedNewPopulation(population) {
const newPopulation = [];
while (newPopulation.length < population.length) {
const parent1 = selectParent(population);
const parent2 = selectParent(population);
const child = crossover(parent1, parent2);
newPopulation.push(mutate(child));
}
return newPopulation;
}

function selectParent(population) {
// Simple selection based on fitness
const totalFitness = population.reduce((sum, individual) => sum +
individual.fitness, 0);
const random = Math.random() * totalFitness;
let runningSum = 0;

for (const individual of population) {


runningSum += individual.fitness;
if (runningSum >= random) {
return individual;
}
}
return population[0]; // Fallback
}

function crossover(parent1, parent2) {


const childGenes = parent1.genes.map((gene, index) => {
return Math.random() < 0.5 ? gene : parent2.genes[index];
});
return { genes: childGenes, fitness: 0 };
}

function mutate(individual) {
const mutationRate = 0.1;
individual.genes = individual.genes.map(gene => {
return Math.random() < mutationRate ? 1 - gene : gene; // Flip the
gene
});
return individual;
}

document.getElementById('run-algorithm').addEventListener('click',
function() {
const bestSolution = runGeneticAlgorithm();
document.getElementById('output').innerText = `Best Solution: Genes: [$
{bestSolution.genes.join(', ')}], Fitness: ${bestSolution.fitness}`;
});
</script>
</body>
</html>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy