{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"left\">\n",
    "<img src=\"README_files/gevopy-logo2.png\" width=\"600\">\n",
    "</h1><br>\n",
    "\n",
    "![qc.sec](https://github.com/BorjaEst/gevopy/actions/workflows/qc-sec.yml/badge.svg)\n",
    "![qc.sty](https://github.com/BorjaEst/gevopy/actions/workflows/qc-sty.yml/badge.svg)\n",
    "![qc.uni](https://github.com/BorjaEst/gevopy/actions/workflows/qc-uni.yml/badge.svg)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Awesome Genetics for Evolutionary Algorithms library created by Borja Esteban."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Install it from PyPI\n",
    "```bash\n",
    "$ pip install gevopy\n",
    "```\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Usage\n",
    "This package is designed in order to create your own evolution scripts based on the following concepts:\n",
    " - **Chromosomes**: Genetic instructions for phenotypes.\n",
    " - **Genotype**: Genetic design to instantiate phenotypes.\n",
    " - **Phenotypes**: Genotype instances which perform a task.\n",
    " - **Fitness**: Provide the methods to evaluate phenotypes.\n",
    " - **Algorithm**: Evolution procedure for phenotypes.\n",
    " - **Experiment**: Evolution session with phenotypes.\n",
    "\n",
    "Now the following sections will introduce a fast initialization to the package.\n",
    "Do not hesitate to extend your knowledge by using all the additional provided\n",
    "examples at the folder [examples](./examples).\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Genotypes\n",
    "Define your Genotypes following the `dataclass` principles from `pydantic` by\n",
    "using the base model `GenotypeModel`. All dataclass attributes are accepted in \n",
    "addition to an special type `Chromosome` provided in the module `genetics`.\n",
    "To start use the already defined chromosome subclasses such `Haploid` and\n",
    "`Diploid` depending on the complexity of your genetic model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'id': UUID('8aa2c907-479b-444c-8c15-995a9015183a'),\n",
       "  'experiment': None,\n",
       "  'created': datetime.datetime(2023, 3, 27, 11, 25, 55, 969290),\n",
       "  'parents': [],\n",
       "  'generation': 1,\n",
       "  'score': None,\n",
       "  'chromosome_1': Haploid([1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0], dtype=uint8),\n",
       "  'chromosome_2': Haploid([0, 1, 0, 0, 0, 1, 0, 0, 1, 0], dtype=uint8),\n",
       "  'simple_attribute': 1.0},\n",
       " {'id': UUID('94f78963-e4bf-4059-8b81-03b62587edd3'),\n",
       "  'experiment': None,\n",
       "  'created': datetime.datetime(2023, 3, 27, 11, 25, 55, 969420),\n",
       "  'parents': [],\n",
       "  'generation': 1,\n",
       "  'score': None,\n",
       "  'chromosome_1': Haploid([1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1], dtype=uint8),\n",
       "  'chromosome_2': Haploid([0, 0, 0, 1, 0, 1, 0, 1, 1, 1], dtype=uint8),\n",
       "  'simple_attribute': 1.0}]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from gevopy import genetics, random\n",
    "from gevopy.genetics import Field\n",
    "\n",
    "class MyGenotype(genetics.GenotypeModel):\n",
    "    chromosome_1: genetics.Haploid = Field(default_factory=lambda: random.haploid(12))\n",
    "    chromosome_2: genetics.Haploid = Field(default_factory=lambda: random.haploid(10))\n",
    "    simple_attribute: float = 1.0\n",
    "\n",
    "[MyGenotype() for _ in range(2)]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Note Genotype attrubutes *id*, *experiment*, *created*, *parents*,\n",
    "*generation*, *score* and *clone* are attributes used by the library.\n",
    "Overwriting of this attributes might lead to unexpected behaviors."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Fitness\n",
    "Create your fitness using the parent class `fitness.FitnessModel` and defining\n",
    "the class method `score`. The fitness to use on the experiment will be an \n",
    "instance of the defined class. You can use the init arguments `cache` and\n",
    "`scheduler` (from Dask) to optimize how the evaluation flow is executed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.MyFitness at 0x7fdb4574c490>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from gevopy import fitness\n",
    "\n",
    "class MyFitness(fitness.FitnessModel):\n",
    "    def score(self, phenotype):\n",
    "        x1 = phenotype.chromosome_1.count(1)\n",
    "        x2 = phenotype.chromosome_2.count(0)\n",
    "        return x1 - x2\n",
    "\n",
    "MyFitness(cache=True, scheduler=\"threads\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> You can additionally define `setup` as method to execute once at the begining\n",
    "of each generation before phenotypes are evaluated.\n",
    "\n",
    "> The only accepted values for scheduler are `synchronous`, `threads` and `processes`.\n",
    "By default `threads` is used."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Algorithm\n",
    "The algorithm is the core of your experiment. It defines the rules of the\n",
    "evolution process. You can create your own algorithm or use the already\n",
    "existing templates. Algorithms are generally composed by 4 components:\n",
    " - **Selection**: Callable which provides the first list of candidates.\n",
    " - **Mating**: Callable which provides the second list of candidates.\n",
    " - **Crossover**: Callable to generate offspring from candidates.\n",
    " - **Mutation**: Callable to mutate phenotype's chromosomes.\n",
    "\n",
    "Additionally, each algorithm template might contain additional arguments such a\n",
    "`survival_rate` or `similarity`. Make sure you read and understand each of the \n",
    "arguments and steps."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MyAlgorithm(selection1=<gevopy.tools.selection.Tournaments object at 0x7fdb4574fac0>, mutation=<gevopy.tools.mutation.SinglePoint object at 0x7fdb4574c4f0>, selection2=<gevopy.tools.selection.Uniform object at 0x7fdb4574faf0>, crossover=<gevopy.tools.crossover.Uniform object at 0x7fdb4574fb50>, survival_rate=0.4)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from gevopy.tools import crossover, mutation, selection\n",
    "from gevopy import algorithms\n",
    "\n",
    "class MyAlgorithm(algorithms.Standard):\n",
    "    selection1 = selection.Tournaments(tournsize=3)\n",
    "    selection2 = selection.Uniform()\n",
    "    crossover = crossover.Uniform(indpb=0.01)\n",
    "    mutation = mutation.SinglePoint(mutpb=0.2)\n",
    "\n",
    "MyAlgorithm()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> The modules `tools.crossover`, `tools.mutation` and `tools.selection` contain\n",
    "templates and utilities to simplify your algorithm definition."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Experiment\n",
    "The experiment is the final expression of your evolutionary algorithm.\n",
    "it provides the methods to evolve and store phenotypes. Once an experiment\n",
    "is instantiated, use the method `run` to force the evolution of the population\n",
    "until a desired state.\n",
    "\n",
    "The results of the experiment can be collected from the method output, calling\n",
    "`best` method or adding a [Neo4j]() connection as `database` input when\n",
    "instantiating the experiment to store all phenotypes during the execution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Evolutionary algorithm execution report:\n",
       "  Executed generations: 17\n",
       "  Best phenotype: 4558f0a4-26c7-4803-8c65-91b110a3d84e\n",
       "  Best score: 10"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import gevopy as ea\n",
    "\n",
    "experiment = ea.Experiment()\n",
    "with experiment.session() as session:\n",
    "    session.add_phenotypes([MyGenotype() for _ in range(20)])\n",
    "    session.algorithm = MyAlgorithm(survival_rate=0.2)\n",
    "    session.fitness = MyFitness(cache=True, scheduler=\"synchronous\")\n",
    "    statistics = session.run(max_generation=20, max_score=10)\n",
    "\n",
    "experiment.close()\n",
    "statistics"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">The method `run` forces the evolution of the experiment which is updated on\n",
    "each cycle. After the method is completed, you can force again te evolution\n",
    "process using higher inputs for `max_generations` or `max_score`."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Development\n",
    "Fork the repository, pick one of the issues at the [issues](https://github.com/BorjaEst/gevopy/issues)\n",
    "and create a [Pull request](https://github.com/BorjaEst/gevopy/pulls).\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FAQ and Notes\n",
    "\n",
    "### Why Graph Database?\n",
    "Storing relationships at the record level makes sense in genotype \n",
    "relationships as it provides index-free adjacency.\n",
    "Graph traversal operations such 'genealogy tree' or certain matches can\n",
    "be performed with no index lookups leading to much better performance.\n",
    "\n",
    "### Why pydantic instead of dataclass?\n",
    "Pydantic supports validation of fields during and after the\n",
    "initialization process and makes parsing easier. \n",
    "Parsing is a relevant step if you are planing to save your\n",
    "phenotypes into the connected database.\n",
    "\n",
    "### Limitations\n",
    "Collections containing collections can not be stored in properties.\n",
    "Property values can only be of primitive types or arrays in Neo4J Cypher queries."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.10.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
