{"id":24329,"library":"pygad","title":"PyGAD - Genetic Algorithm in Python","description":"PyGAD is a Python library for building genetic algorithms and training machine learning models (Keras & PyTorch). Current version 3.6.0, released March 2025, with a cadence of several minor releases per year. Supports single- and multi-objective optimization, custom operators, and integration with deep learning frameworks.","status":"active","version":"3.6.0","language":"python","source_language":"en","source_url":"https://github.com/ahmedfgad/GeneticAlgorithmPython","tags":["genetic-algorithm","optimization","machine-learning","evolutionary-algorithms"],"install":[{"cmd":"pip install pygad","lang":"bash","label":"Install from PyPI"}],"dependencies":[{"reason":"Core dependency for numerical operations.","package":"numpy","optional":false},{"reason":"Library itself.","package":"pygad","optional":false}],"imports":[{"note":"GA is the class name, but you import the module and use pygad.GA.","symbol":"GA","correct":"import pygad"}],"quickstart":{"code":"import pygad\nimport numpy as np\n\n# Define fitness function (must accept two arguments: solution and solution_index)\ndef fitness_func(solution, solution_idx):\n    return -np.sum(solution)\n\n# Define gene space\nnum_genes = 6\ngene_space = {'low': 0, 'high': 2, 'step': 1}  # integers 0 or 1\n\n# Create GA instance\nga_instance = pygad.GA(\n    num_generations=10,\n    num_parents_mating=4,\n    fitness_func=fitness_func,\n    sol_per_pop=8,\n    num_genes=num_genes,\n    gene_space=gene_space,\n    parent_selection_type=\"sss\",\n    keep_parents=1,\n    crossover_type=\"single_point\",\n    mutation_type=\"random\",\n    mutation_num_genes=1\n)\n\n# Run GA\nga_instance.run()\n\n# Show best solution\nsolution, solution_fitness, _ = ga_instance.best_solution()\nprint(f\"Best solution: {solution}\")\nprint(f\"Fitness: {solution_fitness}\")","lang":"python","description":"Minimal example: maximize sum of binary array (genetic algorithm optimization)."},"warnings":[{"fix":"Update fitness function from fitness_func(solution) to fitness_func(solution, solution_idx).","message":"In PyGAD 3.0.0+, the fitness function signature changed: it now receives two arguments (function, solution_idx). The first argument is the solution array, the second is the solution index.","severity":"breaking","affected_versions":"<3.0.0"},{"fix":"Ensure gene_space step matches desired data type.","message":"When using 'gene_space' with a step, the gene type is inferred: if step is integer, genes are integers; if step is float, genes are floats. Mixing types can cause unexpected results.","severity":"gotcha","affected_versions":"all"},{"fix":"Replace delay_after_gen with a custom on_generation function containing time.sleep().","message":"The 'delay_after_gen' parameter was removed in v3.4.0. Use 'on_generation' callback to add delay.","severity":"deprecated","affected_versions":">=3.4.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use 'import pygad' and then 'ga = pygad.GA(...)'.","cause":"In versions <3.0.0, the GA class was instantiated directly; in 3.0.0+, the module import changed.","error":"TypeError: 'GA' object is not callable"},{"fix":"Change your fitness function to accept a second argument: def fitness_func(solution, solution_idx):","cause":"After PyGAD 3.0.0, fitness function signature changed to (solution, solution_idx).","error":"Fitness function must have 2 arguments, but it has 1"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}