{"id":19989767,"url":"https://github.com/inspiaaa/evo","last_synced_at":"2026-06-08T08:32:50.751Z","repository":{"id":108960025,"uuid":"294196961","full_name":"Inspiaaa/Evo","owner":"Inspiaaa","description":"Lightweight Tool for Genetic Algorithms in Python","archived":false,"fork":false,"pushed_at":"2021-01-24T14:00:18.000Z","size":57,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-12T12:20:12.335Z","etag":null,"topics":["evo","evolution","genetic","genetic-algorithms","maximization","optimization","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Inspiaaa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-09T18:31:16.000Z","updated_at":"2021-01-24T14:00:20.000Z","dependencies_parsed_at":"2023-08-25T20:07:35.173Z","dependency_job_id":null,"html_url":"https://github.com/Inspiaaa/Evo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Inspiaaa%2FEvo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Inspiaaa%2FEvo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Inspiaaa%2FEvo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Inspiaaa%2FEvo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Inspiaaa","download_url":"https://codeload.github.com/Inspiaaa/Evo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241430318,"owners_count":19961635,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["evo","evolution","genetic","genetic-algorithms","maximization","optimization","python"],"created_at":"2024-11-13T04:50:03.989Z","updated_at":"2026-06-08T08:32:50.746Z","avatar_url":"https://github.com/Inspiaaa.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Evo\n\nA lightweight library for creating genetic algorithms with ease in Python.\n\n- 100% pure python\n\n- Lightweight: No external dependencies / packages\n\n- Quick start template for multi-parameter optimisation problems\n\n## Getting started\n\n1. Copy the `evo2.py` file into your own project\n\n2. Import the file and create a class which will represent an individual of the population\n\n```python\nfrom evo2 import Individual, Evolution, Selection\nimport random\n\n\nclass Optimisation (Individual):\n   # __slots__ makes the individual class use less memory\n   __slots__ = (\"x\")\n\n   def __init__(self):\n       super().__init__()\n       self.x = 0\n\n   # You can pass in your own data for initialisation\n   # Although a dictionary is handy for that, any data type can be used\n   def create(self, init_params):\n       # Here you randomly initialise the individual\n       self.x = random.uniform(init_params[\"lower\"], init_params[\"upper\"])\n\n   # Mutate is used for introducing some randomness after pairing\n   def mutate(self, mutate_params):\n       self.x += random.random() * mutate_params[\"intensity\"]\n       # Clamp the x value into the desired range, if it goes over\n       self.x = min(mutate_params[\"upper\"], min(mutate_params[\"lower\"], self.x))\n\n   # Create a new offspring (Also known as the crossover operator)\n   def pair(self, other, pair_params):\n       offspring = Optimisation()\n       offspring.x = (self.x + other.x) / 2\n```\n\n3. Create a fitness function\n\n```python\ndef curve(x):\n    return -x*(x-1)*(x-2)*(x-3)*(x-4)\n```\n\n4. Initialise a population\n\n```python\nevo = Evolution(\n    Optimisation,\n    size=20,\n    n_offsprings=10,\n    selection_method=Selection.tournament\n    init_params={\"lower\": 0, \"upper\": 4},\n    mutate_params={\"lower\": 0, \"upper\": 4},\n    fitness_func=lambda obj: curve(obj.x))\n```\n\n5. Run the algorithm\n\n```python\n# Run 100 generations\nevo.evolve(100)\n```\n\n6. Get the best individual\n\n```python\nbest = evo.get_best_n(1)[0]\nbest_fitness = best.fitness\n```\n\n### Other features\n\n- Computing the diversity: Evo uses the standard deviation of the fitness values to describe the diversity of the population\n\n```python\ndiversity = evo.population.compute_diversity()\n```\n\n- Stopping after a certain number of generations without improvement\n\n```python\nwhile True:\n    evo.evolve()\n\n    # Stop after 50 generations of no improvement\n    if evo.stall_gens \u003e 50:\n        break\n```\n\n- Preserving diversity by using \"social disasters\"\n\n```python\nfrom evo2 import SocialDisasters\n\nfor i in range(100):\n    diversity = evo.population.compute_diversity()\n\n    if diversity \u003c 50:\n        # Randomly re-initialise individuals that are too similar\n        SocialDisasters.packing(evo.population, 10)\n\n        # OR only keep the best individual and randomly re-initialise all others\n        SocialDisasters.judgement_day(evo.population)\n```\n\n- Getting the generation number\n  \n  ```python\n  print(f\"Generation #{ evo.gen_number }\")\n  ```\n\n\n\n### Using the optimisation template\n\nThe Evo library has a builtin template for multi parameter optimisation problems\n\n1. Copy the `evo_templates.py` file in to your workspace (as well as the main Evo file itself)\n\n2. Define the problem\n   \n   ```python\n   # 3 parameter function, taking 3 floats\n   def cost(a, b, c):\n       return (a+1)*(b+2)*(c+3)*(a-b-c)*(c-b-a)\n   ```\n\n3. Let the library optimise for you\n   \n   ```python\n   from evo_templates import maximise_multi_param\n   \n   a, b, c = maximise_multi_param(cost, lower_bounds=[-2, -2, -2], upper_bounds=[2, 2, 2]))\n   print(a, b, c)\n   ```\n\n\n\n### Different selection methods\n\nThe selection method tells Evo, which individuals to pair\n\n\n\n- `Selection.random` Randomly chooses individuals\n\n- `Selection.fittest` Chooses  the best individuals\n\n- `Selection.roullette_wheel` Gives the best individuals a better chance of being chosen\n\n- `Selection.tournament` Takes two random individuals and chooses the better one\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finspiaaa%2Fevo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finspiaaa%2Fevo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finspiaaa%2Fevo/lists"}