From 96fa4f75b801d528860a973baf2b405167b3cae9 Mon Sep 17 00:00:00 2001
From: RAVI RANJAN <59027503+astro-ravi@users.noreply.github.com>
Date: Fri, 22 Apr 2022 21:06:37 +0530
Subject: [PATCH 1/2] Created using Colaboratory
---
...c_Algorithm_Implementation_in_Python.ipynb | 613 ++++++++++++++++++
1 file changed, 613 insertions(+)
create mode 100644 Genetic_Algorithm_Implementation_in_Python.ipynb
diff --git a/Genetic_Algorithm_Implementation_in_Python.ipynb b/Genetic_Algorithm_Implementation_in_Python.ipynb
new file mode 100644
index 0000000..789cfc0
--- /dev/null
+++ b/Genetic_Algorithm_Implementation_in_Python.ipynb
@@ -0,0 +1,613 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "name": "Genetic Algorithm Implementation in Python.ipynb",
+ "provenance": [],
+ "authorship_tag": "ABX9TyOX0JIiKfTmh6w8walgqheV",
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "V0UziX_2suiB",
+ "outputId": "e8d5ab5b-3595-4ca1-b77b-78ab6c2768bd"
+ },
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Enter the population size: 10\n",
+ "Sets of values of [a,b,c]\n",
+ "[[6, 10, 10], [6, 6, 9], [10, 3, 2], [6, 3, 6], [9, 8, 1], [10, 10, 6], [8, 6, 10], [9, 7, 6], [7, 9, 10], [3, 2, 8]]\n",
+ "\n",
+ "Fitness\n",
+ "[51, 40, 17, 25, 23, 43, 45, 36, 50, 26]\n",
+ "\n",
+ "Percentage of each individual: \n",
+ "[7.0588235294117645, 9.0, 21.176470588235293, 14.4, 15.652173913043478, 8.372093023255815, 8.0, 10.0, 7.2, 13.846153846153847]\n",
+ "\n",
+ "Percentage occupied on Roulette Wheel\n",
+ "[7.0588235294117645, 16.058823529411764, 37.23529411764706, 51.63529411764706, 67.28746803069053, 75.65956105394635, 83.65956105394635, 93.65956105394635, 100.85956105394635, 114.70571490010019]\n",
+ "\n",
+ "Binary representation of [a,b,c]:\n",
+ "[[[1, 1, 0], [1, 0, 1, 0], [1, 0, 1, 0]], [[1, 1, 0], [1, 1, 0], [1, 0, 0, 1]], [[1, 0, 1, 0], [1, 1], [1, 0]], [[1, 1, 0], [1, 1], [1, 1, 0]], [[1, 0, 0, 1], [1, 0, 0, 0], [1]], [[1, 0, 1, 0], [1, 0, 1, 0], [1, 1, 0]], [[1, 0, 0, 0], [1, 1, 0], [1, 0, 1, 0]], [[1, 0, 0, 1], [1, 1, 1], [1, 1, 0]], [[1, 1, 1], [1, 0, 0, 1], [1, 0, 1, 0]], [[1, 1], [1, 0], [1, 0, 0, 0]]]\n",
+ "\n",
+ "Four-bit binary representation of [a,b,c]:\n",
+ "[[[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 1, 0]], [[0, 1, 1, 0], [0, 1, 1, 0], [1, 0, 0, 1]], [[1, 0, 1, 0], [0, 0, 1, 1], [0, 0, 1, 0]], [[0, 1, 1, 0], [0, 0, 1, 1], [0, 1, 1, 0]], [[1, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1]], [[1, 0, 1, 0], [1, 0, 1, 0], [0, 1, 1, 0]], [[1, 0, 0, 0], [0, 1, 1, 0], [1, 0, 1, 0]], [[1, 0, 0, 1], [0, 1, 1, 1], [0, 1, 1, 0]], [[0, 1, 1, 1], [1, 0, 0, 1], [1, 0, 1, 0]], [[0, 0, 1, 1], [0, 0, 1, 0], [1, 0, 0, 0]]]\n",
+ "\n",
+ "Representation of each individual in 12 bits:\n",
+ "[[0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
+ "Enter the number of generation till which you want to run the algorithm: 5\n",
+ "GENERATION 1\n",
+ "\n",
+ "Pins generated: \n",
+ "[93.51645202640641, 31.759415886226957]\n",
+ "Indices of selected parents: \n",
+ "[6, 1]\n",
+ "\n",
+ "Parents chosen: \n",
+ "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1]]\n",
+ "\n",
+ "Offsprings obtained after crossover: \n",
+ "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0]]\n",
+ "\n",
+ "Calculated Fitness of the offsprings: \n",
+ "[42, 43]\n",
+ "\n",
+ "Maximum fitness: 51\n",
+ "Position: 0\n",
+ "Initial fitness list\n",
+ "[51, 40, 17, 25, 23, 43, 45, 36, 50, 26]\n",
+ "Fitness list after implementing minimisation:\n",
+ "[42, 40, 17, 25, 23, 43, 45, 36, 50, 26]\n",
+ "\n",
+ "New set of individuals: \n",
+ "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
+ "\n",
+ "Maximum fitness: 50\n",
+ "Position: 8\n",
+ "Initial fitness list\n",
+ "[42, 40, 17, 25, 23, 43, 45, 36, 50, 26]\n",
+ "Fitness list after implementing minimisation:\n",
+ "[42, 40, 17, 25, 23, 43, 45, 36, 43, 26]\n",
+ "\n",
+ "New set of individuals: \n",
+ "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
+ "GENERATION 2\n",
+ "\n",
+ "Pins generated: \n",
+ "[35.10203435344747, 95.38090271538356]\n",
+ "Indices of selected parents: \n",
+ "[1, 7]\n",
+ "\n",
+ "Parents chosen: \n",
+ "[[0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0]]\n",
+ "\n",
+ "Offsprings obtained after crossover: \n",
+ "[[0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1]]\n",
+ "\n",
+ "Calculated Fitness of the offsprings: \n",
+ "[33, 43]\n",
+ "\n",
+ "Maximum fitness: 45\n",
+ "Position: 6\n",
+ "Initial fitness list\n",
+ "[42, 40, 17, 25, 23, 43, 45, 36, 43, 26]\n",
+ "Fitness list after implementing minimisation:\n",
+ "[42, 40, 17, 25, 23, 43, 33, 36, 43, 26]\n",
+ "\n",
+ "New set of individuals: \n",
+ "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
+ "\n",
+ "Maximum fitness: 43\n",
+ "Position: 5\n",
+ "Initial fitness list\n",
+ "[42, 40, 17, 25, 23, 43, 33, 36, 43, 26]\n",
+ "Fitness list after implementing minimisation:\n",
+ "[42, 40, 17, 25, 23, 43, 33, 36, 43, 26]\n",
+ "\n",
+ "New set of individuals: \n",
+ "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
+ "GENERATION 3\n",
+ "\n",
+ "Pins generated: \n",
+ "[111.13637701439372, 21.862753899327366]\n",
+ "Indices of selected parents: \n",
+ "[8, 1]\n",
+ "\n",
+ "Parents chosen: \n",
+ "[[0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1]]\n",
+ "\n",
+ "Offsprings obtained after crossover: \n",
+ "[[0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0]]\n",
+ "\n",
+ "Calculated Fitness of the offsprings: \n",
+ "[40, 43]\n",
+ "\n",
+ "Maximum fitness: 43\n",
+ "Position: 5\n",
+ "Initial fitness list\n",
+ "[42, 40, 17, 25, 23, 43, 33, 36, 43, 26]\n",
+ "Fitness list after implementing minimisation:\n",
+ "[42, 40, 17, 25, 23, 40, 33, 36, 43, 26]\n",
+ "\n",
+ "New set of individuals: \n",
+ "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
+ "\n",
+ "Maximum fitness: 43\n",
+ "Position: 8\n",
+ "Initial fitness list\n",
+ "[42, 40, 17, 25, 23, 40, 33, 36, 43, 26]\n",
+ "Fitness list after implementing minimisation:\n",
+ "[42, 40, 17, 25, 23, 40, 33, 36, 43, 26]\n",
+ "\n",
+ "New set of individuals: \n",
+ "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
+ "GENERATION 4\n",
+ "\n",
+ "Pins generated: \n",
+ "[72.14765685747808, 14.411470195046714]\n",
+ "Indices of selected parents: \n",
+ "[4, 0]\n",
+ "\n",
+ "Parents chosen: \n",
+ "[[1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1]]\n",
+ "\n",
+ "Offsprings obtained after crossover: \n",
+ "[[1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1], [1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1]]\n",
+ "\n",
+ "Calculated Fitness of the offsprings: \n",
+ "[47, 18]\n",
+ "\n",
+ "Maximum fitness: 43\n",
+ "Position: 8\n",
+ "Initial fitness list\n",
+ "[42, 40, 17, 25, 23, 40, 33, 36, 43, 26]\n",
+ "Fitness list after implementing minimisation:\n",
+ "[42, 40, 17, 25, 23, 40, 33, 36, 43, 26]\n",
+ "\n",
+ "New set of individuals: \n",
+ "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
+ "\n",
+ "Maximum fitness: 43\n",
+ "Position: 8\n",
+ "Initial fitness list\n",
+ "[42, 40, 17, 25, 23, 40, 33, 36, 43, 26]\n",
+ "Fitness list after implementing minimisation:\n",
+ "[42, 40, 17, 25, 23, 40, 33, 36, 18, 26]\n",
+ "\n",
+ "New set of individuals: \n",
+ "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
+ "GENERATION 5\n",
+ "\n",
+ "Pins generated: \n",
+ "[90.46603589768453, 57.5017928129175]\n",
+ "Indices of selected parents: \n",
+ "[6, 3]\n",
+ "\n",
+ "Parents chosen: \n",
+ "[[0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0]]\n",
+ "\n",
+ "Offsprings obtained after crossover: \n",
+ "[[0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0]]\n",
+ "\n",
+ "Calculated Fitness of the offsprings: \n",
+ "[33, 25]\n",
+ "\n",
+ "Maximum fitness: 42\n",
+ "Position: 0\n",
+ "Initial fitness list\n",
+ "[42, 40, 17, 25, 23, 40, 33, 36, 18, 26]\n",
+ "Fitness list after implementing minimisation:\n",
+ "[33, 40, 17, 25, 23, 40, 33, 36, 18, 26]\n",
+ "\n",
+ "New set of individuals: \n",
+ "[[0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
+ "\n",
+ "Maximum fitness: 40\n",
+ "Position: 1\n",
+ "Initial fitness list\n",
+ "[33, 40, 17, 25, 23, 40, 33, 36, 18, 26]\n",
+ "Fitness list after implementing minimisation:\n",
+ "[33, 25, 17, 25, 23, 40, 33, 36, 18, 26]\n",
+ "\n",
+ "New set of individuals: \n",
+ "[[0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n"
+ ]
+ }
+ ],
+ "source": [
+ "import random\n",
+ "\n",
+ "fl=[] \n",
+ "fit = [] \n",
+ "prw = [] \n",
+ "po = [] \n",
+ "n = int(input(\"Enter the population size: \")) \n",
+ "\n",
+ "for x in range(0,n): \n",
+ " a = random.randint(1, 10)\n",
+ " if a<5:\n",
+ " b = 5 - a;\n",
+ " else:\n",
+ " b = random.randint(1, 10)\n",
+ " c = random.randint(1, 10)\n",
+ "\n",
+ " l = [a, b, c] \n",
+ " fl.append(l) \n",
+ " f = a + 2*b + 3*c - 5\n",
+ " fit.append(f) \n",
+ " p = (1/f)*360\n",
+ " prw.append(p) \n",
+ "\n",
+ "\n",
+ "for i in range(0,n):\n",
+ " s=0\n",
+ " for j in range(0,i+1):\n",
+ " s=s+prw[j]\n",
+ " po.append(s)\n",
+ " \n",
+ "print(\"Sets of values of [a,b,c]\") \n",
+ "print(fl)\n",
+ "print(\"\\nFitness\")\n",
+ "print(fit)\n",
+ "print(\"\\nPercentage of each individual: \")\n",
+ "print(prw)\n",
+ "print(\"\\nPercentage occupied on Roulette Wheel\")\n",
+ "print(po)\n",
+ "print(\"\\nBinary representation of [a,b,c]:\");\n",
+ "\n",
+ "be=[] \n",
+ "\n",
+ "for i in fl: \n",
+ " tl=[] \n",
+ " \n",
+ " for j in i: \n",
+ " v=j \n",
+ " b=0\n",
+ " p=1\n",
+ " tl2=[]\n",
+ " while v!=0:\n",
+ " r = v % 2\n",
+ " b = b + r * p\n",
+ " p = p * 10\n",
+ " v = v // 2\n",
+ " tl2.insert(0,r) \n",
+ " tl.append(tl2) \n",
+ " be.append(tl) \n",
+ "print(be)\n",
+ "\n",
+ "print(\"\\nFour-bit binary representation of [a,b,c]:\");\n",
+ "\n",
+ "fbr=[] \n",
+ "for i in be:\n",
+ " nl1=[]\n",
+ " t=[]\n",
+ " for j in i:\n",
+ " nl= []\n",
+ " for k in j:\n",
+ " \n",
+ " nl.append(k)\n",
+ " ll=len(nl)\n",
+ " \n",
+ " while ll<4:\n",
+ " nl.insert(0,0)\n",
+ " ll = len(nl)\n",
+ " \n",
+ " t.append(nl)\n",
+ " fbr.append(t)\n",
+ "print(fbr)\n",
+ "\n",
+ "print(\"\\nRepresentation of each individual in 12 bits:\")\n",
+ "\n",
+ "ch = [] \n",
+ "for i in fbr:\n",
+ " tch = []\n",
+ " for j in i:\n",
+ " for k in j:\n",
+ " tch.append(k)\n",
+ " #print(tch)\n",
+ " ch.append(tch)\n",
+ "print(ch)\n",
+ "\n",
+ "gn=int(input(\"Enter the number of generation till which you want to run the algorithm: \")) \n",
+ "gc=1\n",
+ "while(gc<=gn):\n",
+ " \n",
+ " print(\"GENERATION \",gc)\n",
+ " pa = [] \n",
+ " chn =[] \n",
+ " pin = [] \n",
+ " k=1\n",
+ " cp = 0.4*5 \n",
+ " while k<=cp:\n",
+ " rn = random.uniform(po[0],po[n-1])\n",
+ " k = k+1\n",
+ " pin.append(rn);\n",
+ " \n",
+ " for i in range(0,n):\n",
+ " if rn < po[i]:\n",
+ " chn.append(i-1)\n",
+ " pa.append(ch[i-1])\n",
+ " break\n",
+ " print(\"\\nPins generated: \")\n",
+ " print(pin)\n",
+ " print(\"Indices of selected parents: \")\n",
+ " print(chn)\n",
+ " print(\"\\nParents chosen: \")\n",
+ " print(pa)\n",
+ "\n",
+ " tp1 =[] \n",
+ " tp2 =[] \n",
+ " o1 = [] \n",
+ " o2 = [] \n",
+ "\n",
+ " tp1 = pa[0]\n",
+ " tp2 = pa[1]\n",
+ " \n",
+ " for i in range(0, 7):\n",
+ " o1.append(tp1[i])\n",
+ " o2.append(tp2[i])\n",
+ "\n",
+ " for j in range (7,12):\n",
+ " o1.append(tp2[j])\n",
+ " o2.append(tp1[j])\n",
+ " \n",
+ " print(\"\\nOffsprings obtained after crossover: \")\n",
+ "\n",
+ " of=[]\n",
+ " of.append(o1)\n",
+ " of.append(o2)\n",
+ " print(of)\n",
+ " o =[]\n",
+ " lfo=[] \n",
+ "\n",
+ " for i in range(0,len(of)):\n",
+ " o = of[i]\n",
+ " a = 8*o[0]+ 4*o[1]+ 2*o[2]+ o[3];\n",
+ " b = 8*o[4]+ 4*o[5]+ 2*o[6]+ o[7];\n",
+ " c = 8*o[8]+ 4*o[9]+ 2*o[10]+ o[11];\n",
+ " \n",
+ " fo = a+2*b+3*c - 5 \n",
+ " lfo.append(fo)\n",
+ " print(\"\\nCalculated Fitness of the offsprings: \") \n",
+ " print(lfo)\n",
+ "\n",
+ "\n",
+ " for k in range(0,len(lfo)):\n",
+ " max = fit[0]\n",
+ " ind = 0\n",
+ " for i in range(1,n):\n",
+ " if fit[i]>max:\n",
+ " max = fit[i]\n",
+ " ind= fit.index(max)\n",
+ " print(\"\\nMaximum fitness: \",max)\n",
+ " print(\"Position: \",ind)\n",
+ " print(\"Initial fitness list\");\n",
+ " print(fit)\n",
+ " if lfo[k]max:\n",
+ " max = fit[i]\n",
+ " ind= fit.index(max)\n",
+ " print(\"\\nMaximum fitness: \",max)\n",
+ " print(\"Position: \",ind)\n",
+ " print(\"Initial fitness list\");\n",
+ " print(fit)\n",
+ " if lfo[k]
Date: Fri, 22 Apr 2022 21:07:47 +0530
Subject: [PATCH 2/2] Delete Genetic_Algorithm_Implementation_in_Python.ipynb
---
...c_Algorithm_Implementation_in_Python.ipynb | 613 ------------------
1 file changed, 613 deletions(-)
delete mode 100644 Genetic_Algorithm_Implementation_in_Python.ipynb
diff --git a/Genetic_Algorithm_Implementation_in_Python.ipynb b/Genetic_Algorithm_Implementation_in_Python.ipynb
deleted file mode 100644
index 789cfc0..0000000
--- a/Genetic_Algorithm_Implementation_in_Python.ipynb
+++ /dev/null
@@ -1,613 +0,0 @@
-{
- "nbformat": 4,
- "nbformat_minor": 0,
- "metadata": {
- "colab": {
- "name": "Genetic Algorithm Implementation in Python.ipynb",
- "provenance": [],
- "authorship_tag": "ABX9TyOX0JIiKfTmh6w8walgqheV",
- "include_colab_link": true
- },
- "kernelspec": {
- "name": "python3",
- "display_name": "Python 3"
- },
- "language_info": {
- "name": "python"
- }
- },
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "view-in-github",
- "colab_type": "text"
- },
- "source": [
- "
"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "V0UziX_2suiB",
- "outputId": "e8d5ab5b-3595-4ca1-b77b-78ab6c2768bd"
- },
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "Enter the population size: 10\n",
- "Sets of values of [a,b,c]\n",
- "[[6, 10, 10], [6, 6, 9], [10, 3, 2], [6, 3, 6], [9, 8, 1], [10, 10, 6], [8, 6, 10], [9, 7, 6], [7, 9, 10], [3, 2, 8]]\n",
- "\n",
- "Fitness\n",
- "[51, 40, 17, 25, 23, 43, 45, 36, 50, 26]\n",
- "\n",
- "Percentage of each individual: \n",
- "[7.0588235294117645, 9.0, 21.176470588235293, 14.4, 15.652173913043478, 8.372093023255815, 8.0, 10.0, 7.2, 13.846153846153847]\n",
- "\n",
- "Percentage occupied on Roulette Wheel\n",
- "[7.0588235294117645, 16.058823529411764, 37.23529411764706, 51.63529411764706, 67.28746803069053, 75.65956105394635, 83.65956105394635, 93.65956105394635, 100.85956105394635, 114.70571490010019]\n",
- "\n",
- "Binary representation of [a,b,c]:\n",
- "[[[1, 1, 0], [1, 0, 1, 0], [1, 0, 1, 0]], [[1, 1, 0], [1, 1, 0], [1, 0, 0, 1]], [[1, 0, 1, 0], [1, 1], [1, 0]], [[1, 1, 0], [1, 1], [1, 1, 0]], [[1, 0, 0, 1], [1, 0, 0, 0], [1]], [[1, 0, 1, 0], [1, 0, 1, 0], [1, 1, 0]], [[1, 0, 0, 0], [1, 1, 0], [1, 0, 1, 0]], [[1, 0, 0, 1], [1, 1, 1], [1, 1, 0]], [[1, 1, 1], [1, 0, 0, 1], [1, 0, 1, 0]], [[1, 1], [1, 0], [1, 0, 0, 0]]]\n",
- "\n",
- "Four-bit binary representation of [a,b,c]:\n",
- "[[[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 1, 0]], [[0, 1, 1, 0], [0, 1, 1, 0], [1, 0, 0, 1]], [[1, 0, 1, 0], [0, 0, 1, 1], [0, 0, 1, 0]], [[0, 1, 1, 0], [0, 0, 1, 1], [0, 1, 1, 0]], [[1, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1]], [[1, 0, 1, 0], [1, 0, 1, 0], [0, 1, 1, 0]], [[1, 0, 0, 0], [0, 1, 1, 0], [1, 0, 1, 0]], [[1, 0, 0, 1], [0, 1, 1, 1], [0, 1, 1, 0]], [[0, 1, 1, 1], [1, 0, 0, 1], [1, 0, 1, 0]], [[0, 0, 1, 1], [0, 0, 1, 0], [1, 0, 0, 0]]]\n",
- "\n",
- "Representation of each individual in 12 bits:\n",
- "[[0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
- "Enter the number of generation till which you want to run the algorithm: 5\n",
- "GENERATION 1\n",
- "\n",
- "Pins generated: \n",
- "[93.51645202640641, 31.759415886226957]\n",
- "Indices of selected parents: \n",
- "[6, 1]\n",
- "\n",
- "Parents chosen: \n",
- "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1]]\n",
- "\n",
- "Offsprings obtained after crossover: \n",
- "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0]]\n",
- "\n",
- "Calculated Fitness of the offsprings: \n",
- "[42, 43]\n",
- "\n",
- "Maximum fitness: 51\n",
- "Position: 0\n",
- "Initial fitness list\n",
- "[51, 40, 17, 25, 23, 43, 45, 36, 50, 26]\n",
- "Fitness list after implementing minimisation:\n",
- "[42, 40, 17, 25, 23, 43, 45, 36, 50, 26]\n",
- "\n",
- "New set of individuals: \n",
- "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
- "\n",
- "Maximum fitness: 50\n",
- "Position: 8\n",
- "Initial fitness list\n",
- "[42, 40, 17, 25, 23, 43, 45, 36, 50, 26]\n",
- "Fitness list after implementing minimisation:\n",
- "[42, 40, 17, 25, 23, 43, 45, 36, 43, 26]\n",
- "\n",
- "New set of individuals: \n",
- "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
- "GENERATION 2\n",
- "\n",
- "Pins generated: \n",
- "[35.10203435344747, 95.38090271538356]\n",
- "Indices of selected parents: \n",
- "[1, 7]\n",
- "\n",
- "Parents chosen: \n",
- "[[0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0]]\n",
- "\n",
- "Offsprings obtained after crossover: \n",
- "[[0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1]]\n",
- "\n",
- "Calculated Fitness of the offsprings: \n",
- "[33, 43]\n",
- "\n",
- "Maximum fitness: 45\n",
- "Position: 6\n",
- "Initial fitness list\n",
- "[42, 40, 17, 25, 23, 43, 45, 36, 43, 26]\n",
- "Fitness list after implementing minimisation:\n",
- "[42, 40, 17, 25, 23, 43, 33, 36, 43, 26]\n",
- "\n",
- "New set of individuals: \n",
- "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
- "\n",
- "Maximum fitness: 43\n",
- "Position: 5\n",
- "Initial fitness list\n",
- "[42, 40, 17, 25, 23, 43, 33, 36, 43, 26]\n",
- "Fitness list after implementing minimisation:\n",
- "[42, 40, 17, 25, 23, 43, 33, 36, 43, 26]\n",
- "\n",
- "New set of individuals: \n",
- "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
- "GENERATION 3\n",
- "\n",
- "Pins generated: \n",
- "[111.13637701439372, 21.862753899327366]\n",
- "Indices of selected parents: \n",
- "[8, 1]\n",
- "\n",
- "Parents chosen: \n",
- "[[0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1]]\n",
- "\n",
- "Offsprings obtained after crossover: \n",
- "[[0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0]]\n",
- "\n",
- "Calculated Fitness of the offsprings: \n",
- "[40, 43]\n",
- "\n",
- "Maximum fitness: 43\n",
- "Position: 5\n",
- "Initial fitness list\n",
- "[42, 40, 17, 25, 23, 43, 33, 36, 43, 26]\n",
- "Fitness list after implementing minimisation:\n",
- "[42, 40, 17, 25, 23, 40, 33, 36, 43, 26]\n",
- "\n",
- "New set of individuals: \n",
- "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
- "\n",
- "Maximum fitness: 43\n",
- "Position: 8\n",
- "Initial fitness list\n",
- "[42, 40, 17, 25, 23, 40, 33, 36, 43, 26]\n",
- "Fitness list after implementing minimisation:\n",
- "[42, 40, 17, 25, 23, 40, 33, 36, 43, 26]\n",
- "\n",
- "New set of individuals: \n",
- "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
- "GENERATION 4\n",
- "\n",
- "Pins generated: \n",
- "[72.14765685747808, 14.411470195046714]\n",
- "Indices of selected parents: \n",
- "[4, 0]\n",
- "\n",
- "Parents chosen: \n",
- "[[1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1]]\n",
- "\n",
- "Offsprings obtained after crossover: \n",
- "[[1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1], [1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1]]\n",
- "\n",
- "Calculated Fitness of the offsprings: \n",
- "[47, 18]\n",
- "\n",
- "Maximum fitness: 43\n",
- "Position: 8\n",
- "Initial fitness list\n",
- "[42, 40, 17, 25, 23, 40, 33, 36, 43, 26]\n",
- "Fitness list after implementing minimisation:\n",
- "[42, 40, 17, 25, 23, 40, 33, 36, 43, 26]\n",
- "\n",
- "New set of individuals: \n",
- "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
- "\n",
- "Maximum fitness: 43\n",
- "Position: 8\n",
- "Initial fitness list\n",
- "[42, 40, 17, 25, 23, 40, 33, 36, 43, 26]\n",
- "Fitness list after implementing minimisation:\n",
- "[42, 40, 17, 25, 23, 40, 33, 36, 18, 26]\n",
- "\n",
- "New set of individuals: \n",
- "[[1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
- "GENERATION 5\n",
- "\n",
- "Pins generated: \n",
- "[90.46603589768453, 57.5017928129175]\n",
- "Indices of selected parents: \n",
- "[6, 3]\n",
- "\n",
- "Parents chosen: \n",
- "[[0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0]]\n",
- "\n",
- "Offsprings obtained after crossover: \n",
- "[[0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0]]\n",
- "\n",
- "Calculated Fitness of the offsprings: \n",
- "[33, 25]\n",
- "\n",
- "Maximum fitness: 42\n",
- "Position: 0\n",
- "Initial fitness list\n",
- "[42, 40, 17, 25, 23, 40, 33, 36, 18, 26]\n",
- "Fitness list after implementing minimisation:\n",
- "[33, 40, 17, 25, 23, 40, 33, 36, 18, 26]\n",
- "\n",
- "New set of individuals: \n",
- "[[0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n",
- "\n",
- "Maximum fitness: 40\n",
- "Position: 1\n",
- "Initial fitness list\n",
- "[33, 40, 17, 25, 23, 40, 33, 36, 18, 26]\n",
- "Fitness list after implementing minimisation:\n",
- "[33, 25, 17, 25, 23, 40, 33, 36, 18, 26]\n",
- "\n",
- "New set of individuals: \n",
- "[[0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1], [0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0]]\n"
- ]
- }
- ],
- "source": [
- "import random\n",
- "\n",
- "fl=[] \n",
- "fit = [] \n",
- "prw = [] \n",
- "po = [] \n",
- "n = int(input(\"Enter the population size: \")) \n",
- "\n",
- "for x in range(0,n): \n",
- " a = random.randint(1, 10)\n",
- " if a<5:\n",
- " b = 5 - a;\n",
- " else:\n",
- " b = random.randint(1, 10)\n",
- " c = random.randint(1, 10)\n",
- "\n",
- " l = [a, b, c] \n",
- " fl.append(l) \n",
- " f = a + 2*b + 3*c - 5\n",
- " fit.append(f) \n",
- " p = (1/f)*360\n",
- " prw.append(p) \n",
- "\n",
- "\n",
- "for i in range(0,n):\n",
- " s=0\n",
- " for j in range(0,i+1):\n",
- " s=s+prw[j]\n",
- " po.append(s)\n",
- " \n",
- "print(\"Sets of values of [a,b,c]\") \n",
- "print(fl)\n",
- "print(\"\\nFitness\")\n",
- "print(fit)\n",
- "print(\"\\nPercentage of each individual: \")\n",
- "print(prw)\n",
- "print(\"\\nPercentage occupied on Roulette Wheel\")\n",
- "print(po)\n",
- "print(\"\\nBinary representation of [a,b,c]:\");\n",
- "\n",
- "be=[] \n",
- "\n",
- "for i in fl: \n",
- " tl=[] \n",
- " \n",
- " for j in i: \n",
- " v=j \n",
- " b=0\n",
- " p=1\n",
- " tl2=[]\n",
- " while v!=0:\n",
- " r = v % 2\n",
- " b = b + r * p\n",
- " p = p * 10\n",
- " v = v // 2\n",
- " tl2.insert(0,r) \n",
- " tl.append(tl2) \n",
- " be.append(tl) \n",
- "print(be)\n",
- "\n",
- "print(\"\\nFour-bit binary representation of [a,b,c]:\");\n",
- "\n",
- "fbr=[] \n",
- "for i in be:\n",
- " nl1=[]\n",
- " t=[]\n",
- " for j in i:\n",
- " nl= []\n",
- " for k in j:\n",
- " \n",
- " nl.append(k)\n",
- " ll=len(nl)\n",
- " \n",
- " while ll<4:\n",
- " nl.insert(0,0)\n",
- " ll = len(nl)\n",
- " \n",
- " t.append(nl)\n",
- " fbr.append(t)\n",
- "print(fbr)\n",
- "\n",
- "print(\"\\nRepresentation of each individual in 12 bits:\")\n",
- "\n",
- "ch = [] \n",
- "for i in fbr:\n",
- " tch = []\n",
- " for j in i:\n",
- " for k in j:\n",
- " tch.append(k)\n",
- " #print(tch)\n",
- " ch.append(tch)\n",
- "print(ch)\n",
- "\n",
- "gn=int(input(\"Enter the number of generation till which you want to run the algorithm: \")) \n",
- "gc=1\n",
- "while(gc<=gn):\n",
- " \n",
- " print(\"GENERATION \",gc)\n",
- " pa = [] \n",
- " chn =[] \n",
- " pin = [] \n",
- " k=1\n",
- " cp = 0.4*5 \n",
- " while k<=cp:\n",
- " rn = random.uniform(po[0],po[n-1])\n",
- " k = k+1\n",
- " pin.append(rn);\n",
- " \n",
- " for i in range(0,n):\n",
- " if rn < po[i]:\n",
- " chn.append(i-1)\n",
- " pa.append(ch[i-1])\n",
- " break\n",
- " print(\"\\nPins generated: \")\n",
- " print(pin)\n",
- " print(\"Indices of selected parents: \")\n",
- " print(chn)\n",
- " print(\"\\nParents chosen: \")\n",
- " print(pa)\n",
- "\n",
- " tp1 =[] \n",
- " tp2 =[] \n",
- " o1 = [] \n",
- " o2 = [] \n",
- "\n",
- " tp1 = pa[0]\n",
- " tp2 = pa[1]\n",
- " \n",
- " for i in range(0, 7):\n",
- " o1.append(tp1[i])\n",
- " o2.append(tp2[i])\n",
- "\n",
- " for j in range (7,12):\n",
- " o1.append(tp2[j])\n",
- " o2.append(tp1[j])\n",
- " \n",
- " print(\"\\nOffsprings obtained after crossover: \")\n",
- "\n",
- " of=[]\n",
- " of.append(o1)\n",
- " of.append(o2)\n",
- " print(of)\n",
- " o =[]\n",
- " lfo=[] \n",
- "\n",
- " for i in range(0,len(of)):\n",
- " o = of[i]\n",
- " a = 8*o[0]+ 4*o[1]+ 2*o[2]+ o[3];\n",
- " b = 8*o[4]+ 4*o[5]+ 2*o[6]+ o[7];\n",
- " c = 8*o[8]+ 4*o[9]+ 2*o[10]+ o[11];\n",
- " \n",
- " fo = a+2*b+3*c - 5 \n",
- " lfo.append(fo)\n",
- " print(\"\\nCalculated Fitness of the offsprings: \") \n",
- " print(lfo)\n",
- "\n",
- "\n",
- " for k in range(0,len(lfo)):\n",
- " max = fit[0]\n",
- " ind = 0\n",
- " for i in range(1,n):\n",
- " if fit[i]>max:\n",
- " max = fit[i]\n",
- " ind= fit.index(max)\n",
- " print(\"\\nMaximum fitness: \",max)\n",
- " print(\"Position: \",ind)\n",
- " print(\"Initial fitness list\");\n",
- " print(fit)\n",
- " if lfo[k]max:\n",
- " max = fit[i]\n",
- " ind= fit.index(max)\n",
- " print(\"\\nMaximum fitness: \",max)\n",
- " print(\"Position: \",ind)\n",
- " print(\"Initial fitness list\");\n",
- " print(fit)\n",
- " if lfo[k]