{"id":6584,"library":"deap","title":"Distributed Evolutionary Algorithms in Python (DEAP)","description":"DEAP is a novel evolutionary computation framework designed for rapid prototyping and testing of ideas. It provides explicit algorithms and transparent data structures for various evolutionary computation techniques, including genetic algorithms, genetic programming, evolution strategies, and multi-objective optimization. It works seamlessly with parallelization mechanisms like multiprocessing. The current stable version is 1.4.3.","status":"active","version":"1.4.3","language":"en","source_language":"en","source_url":"https://github.com/deap/deap","tags":["evolutionary computation","genetic algorithms","genetic programming","optimization","multi-objective","machine learning"],"install":[{"cmd":"pip install deap","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Recommended for Evolution Strategies (e.g., CMA-ES) and creating individuals inheriting from NumPy arrays.","package":"numpy","optional":true},{"reason":"Recommended for visualization of results.","package":"matplotlib","optional":true}],"imports":[{"note":"Used to create custom types for fitness and individuals.","symbol":"creator","correct":"from deap import creator"},{"note":"Contains the Toolbox and base classes for fitness and individuals.","symbol":"base","correct":"from deap import base"},{"note":"Provides a collection of evolutionary operators (selection, crossover, mutation).","symbol":"tools","correct":"from deap import tools"},{"note":"Contains high-level evolutionary algorithms like `eaSimple`, `eaMuPlusLambda`.","symbol":"algorithms","correct":"from deap import algorithms"}],"quickstart":{"code":"import random\nfrom deap import creator, base, tools, algorithms\n\n# 1. Define problem: Maximize sum of bits in a binary string (OneMax problem)\n# Create a FitnessMax class, higher values are better\ncreator.create(\"FitnessMax\", base.Fitness, weights=(1.0,))\n# Create an Individual class, which is a list and has a fitness attribute\ncreator.create(\"Individual\", list, fitness=creator.FitnessMax)\n\n# 2. Initialize Toolbox\ntoolbox = base.Toolbox()\n# Register function to generate random boolean attributes (0 or 1)\ntoolbox.register(\"attr_bool\", random.randint, 0, 1)\n# Register function to create an individual: 100 random booleans\ntoolbox.register(\"individual\", tools.initRepeat, creator.Individual, toolbox.attr_bool, 100)\n# Register function to create a population: a list of individuals\ntoolbox.register(\"population\", tools.initRepeat, list, toolbox.individual)\n\n# 3. Define Evaluation Function\ndef evalOneMax(individual):\n    return sum(individual), # The comma is crucial: returns a tuple\ntoolbox.register(\"evaluate\", evalOneMax)\n\n# 4. Define Genetic Operators\ntoolbox.register(\"mate\", tools.cxTwoPoint) # Two-point crossover\ntoolbox.register(\"mutate\", tools.mutFlipBit, indpb=0.05) # Flip bit mutation with 5% probability\ntoolbox.register(\"select\", tools.selTournament, tournsize=3) # Tournament selection with size 3\n\n# 5. Run the Evolutionary Algorithm\ndef main():\n    random.seed(42) # For reproducibility\n    population = toolbox.population(n=300)\n\n    # Number of generations, crossover probability, mutation probability\n    NGEN, CXPB, MUTPB = 40, 0.7, 0.2\n\n    # The main evolutionary loop\n    print(f\"Start of evolution: Population size {len(population)}\")\n    population, logbook = algorithms.eaSimple(population, toolbox, cxpb=CXPB, mutpb=MUTPB, ngen=NGEN, verbose=False)\n    \n    # Get the best individual(s) from the final population\n    best_ind = tools.selBest(population, 1)[0]\n    print(f\"Best individual: {best_ind}, Fitness: {best_ind.fitness.values[0]}\")\n\nif __name__ == \"__main__\":\n    main()","lang":"python","description":"This quickstart demonstrates the classic OneMax problem, where the goal is to evolve a binary string (list of 0s and 1s) to maximize the number of 1s. It showcases the core DEAP workflow: defining custom types (Fitness and Individual), initializing the Toolbox with genetic operators, and running a simple evolutionary algorithm."},"warnings":[{"fix":"Always return a tuple from your evaluation function.","message":"The evaluation function for an individual MUST return a tuple of fitness values, even for a single objective. For example, `return sum(individual),` (note the comma) or `return (sum(individual),)`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `weights=(1.0,)` for maximization and `weights=(-1.0,)` for minimization.","message":"When defining `creator.create(\"Fitness...\", base.Fitness, weights=...)`, the `weights` tuple determines if the objective is maximization (positive weight, e.g., `(1.0,)`) or minimization (negative weight, e.g., `(-1.0,)`). Ensure the sign matches your optimization goal.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consult the 'Release Highlights' and 'Porting Guide' in the DEAP documentation for the specific version you are upgrading from/to. For `gp.stringify()`, use `str(primitive_tree_object)`. For GP evaluation/lambdification, use `gp.compile()`.","message":"For Genetic Programming (GP), the `gp.stringify()` function was replaced by `PrimitiveTree.__str__()`. Also, `gp.evaluate()` and `gp.lambdify()` were merged and replaced by a single `gp.compile()` function. The `tools.Checkpoint` class was removed in favor of simpler manual checkpointing.","severity":"breaking","affected_versions":"1.1.x to 1.2.x, 1.3.x to 1.4.x (specific changes across versions)"},{"fix":"For current DEAP versions, this is generally not an issue. If encountering `setuptools` related errors during old Python 3 installs, try `pip install setuptools==57.5.0` as a workaround.","message":"Older versions of DEAP (pre-1.x, particularly around 0.8) for Python 3 installations might have required `setuptools<=58` due to the use of `2to3` for source translation. This is unlikely to affect modern installations (Python 3.6+ and DEAP 1.x+).","severity":"deprecated","affected_versions":"<1.0"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}