You are on page 1of 3

{

"cells": [
{
"cell_type": "code",
"execution_count": 15,
"id": "7d61c226",
"metadata": {},
"outputs": [],
"source": [
"#name:AHMED NADEEM BUTT\n",
"#ROLL:21001376004\n",
"#program:BSAI\n",
"from random import randint\n",
"from copy import deepcopy\n",
"data = [(1,1.5), (0.5, 1), (2, 1.5), (2.5, 2.5), (3, 3.5), (4, 5), (5, 5.5),
(6, 5.6), (6.5, 5.8), (6.7, 5.9)]\n",
"# Generate a random population of 100 individuals\n",
"population = [[randint(0, 10) for _ in range(6)] for _ in range(100)]\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "3035ac13",
"metadata": {},
"outputs": [],
"source": [
"# Define the fitness function\n",
"def fitness(individual):\n",
" sse = 0\n",
" for x, y in data:\n",
" predicted_y = (individual[0]*x**5 + individual[1]*x**4 +
individual[2]*x**3 + \n",
" individual[3]*x**2 + individual[4]*x + individual[5])\n",
" sse += (predicted_y - y)**2\n",
" return sse\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "943259a3",
"metadata": {},
"outputs": [],
"source": [
"# Set the number of generations\n",
"num_generations = 100\n",
"\n",
"# Iterate for the specified number of generations\n",
"for generation in range(num_generations):\n",
" # Evaluate the fitness of each individual\n",
" fitness_values = [fitness(individual) for individual in population]\n",
" \n",
" # Select the fittest individuals as parents\n",
" parents = [population[i] for i in range(len(population)) if
fitness_values[i] == min(fitness_values)]\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "c5bb2180",
"metadata": {},
"outputs": [],
"source": [
"# Create the offspring population\n",
"offspring = []\n",
"while len(offspring) < len(population):\n",
" # Select two parents at random\n",
" parent1, parent2 = parents[randint(0, len(parents)-1)], parents[randint(0,
len(parents)-1)]\n",
" \n",
" # Create a child by crossover (combining the genes of the parents)\n",
" child = []\n",
" for i in range(6):\n",
" if randint(0, 1) == 0:\n",
" child.append(parent1[i])\n",
" else:\n",
" child.append(parent2[i])\n",
" \n",
" # Mutate the child by randomly changing the values of some genes\n",
" for i in range(6):\n",
" if randint(0, 1) == 0:\n",
" child[i] = randint(0, 10)\n",
" \n",
" # Add the child to the offspring population\n",
" offspring.append(child)\n",
" \n",
" # Replace the current population with the offspring population\n",
"population = deepcopy(offspring)\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "ed61428e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 0, 0, 7, 7, 9]\n"
]
}
],
"source": [
"# Print the gene values of the fittest individual in the final population\n",
"print(min(population, key=fitness))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a12e172",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

You might also like