{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "52baa88b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "5bef4e45",
   "metadata": {},
   "outputs": [],
   "source": [
    "nBits=22\n",
    "npop=20\n",
    "vMin=-2\n",
    "vMax=2\n",
    "for i in range(npop):\n",
    "    cromossomo.append(''.join(str(x) for x in np.random.randint(0,2,22)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "404ffb81",
   "metadata": {},
   "outputs": [],
   "source": [
    "#def myfunc(n):\n",
    "#  return len(n)\n",
    "\n",
    "#x = map(myfunc, ('apple', 'banana', 'cherry'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "17612007",
   "metadata": {},
   "outputs": [],
   "source": [
    "def bin2dec(x):\n",
    "    dec=int(x,2)\n",
    "    return vMin + ((vMax-vMin)/(2**(nBits-1)))*dec\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "6a95986b",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=np.array(list(map(bin2dec,cromossomo)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "b82bfa0c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.19853592  5.14130783  0.42078972  5.1574707   2.21915817  2.93527985\n",
      "  0.08328819  3.03129387  0.56348419  5.72499275  1.8185215  -1.64009476\n",
      " -1.86715889  1.82197952  3.91579437  0.43566704  4.79846573  0.14733887\n",
      "  4.43517113  0.43533897  0.70521355  0.96270943  4.94396019  1.84824371\n",
      "  2.51814842  1.13395119  0.81936073  2.11034012  2.1631794  -0.33783913\n",
      "  3.79277992  2.48187065  0.66272354 -1.46735573  5.69465065 -0.69500923\n",
      "  4.30092812 -1.4127903   4.77488136 -0.1621933 ]\n"
     ]
    }
   ],
   "source": [
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4efa249d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4892ebea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['0011100110100101100110',\n",
       " '1110010010000101100110',\n",
       " '0100110101110111000111',\n",
       " '1110010100001010000000',\n",
       " '1000011100000011010110',\n",
       " '1001110111101101110100',\n",
       " '0100001010101010010011',\n",
       " '1010000100000000010111',\n",
       " '0101001000001000000100',\n",
       " '1111011100110011001001',\n",
       " '0111101000110001010101',\n",
       " '0000101110000100010110',\n",
       " '0000010001000000001111',\n",
       " '0111101001001101101010',\n",
       " '1011110101001110001100',\n",
       " '0100110111110000111111',\n",
       " '1101100110001101000010',\n",
       " '0100010010110111000000',\n",
       " '1100110111101100111011',\n",
       " '0100110111101110010011']"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cromossomo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "592e1fea",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "208a8c30",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "1d0e371b",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "int() can't convert non-string with explicit base",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [35]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m dec \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mcromossomo\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: int() can't convert non-string with explicit base"
     ]
    }
   ],
   "source": [
    "dec = int(cromossomo,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "47bd7c6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "vMin=-2\n",
    "vMax=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c2eb4bb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=vMin + ((vMax-vMin)/(2**(nBits-1)))*dec    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "842e37dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.47699737548828125"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b38a2485",
   "metadata": {},
   "source": [
    "# Fitness é o próprio valor da função"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d0115d40",
   "metadata": {},
   "outputs": [],
   "source": [
    "fitness=np.cos(20*x)-abs(x)/2+np.power(x,3)/4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "7983a657",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.2590063879821876"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fitness"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d67428e6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eadf197b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74e6694b",
   "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
}
