{"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,"search_vec":"'2025':27 '3.6.0':24 'algorithm':3,14,54,61 'build':12 'cadenc':30 'current':22 'custom':44 'deep':49 'evolutionari':60 'evolutionary-algorithm':59 'framework':51 'genet':2,13,53 'genetic-algorithm':52 'integr':47 'kera':20 'learn':18,50,58 'librari':10 'machin':17,57 'machine-learn':56 'march':26 'minor':33 'model':19 'multi':41 'multi-object':40 'object':42 'oper':45 'optim':43,55 'per':35 'pygad':1,6 'python':5,9 'pytorch':21 'releas':25,34 'sever':32 'singl':38 'support':37 'train':16 'version':23 'year':36","created_at":"2026-05-01T08:12:29.491996+00:00","updated_at":"2026-05-01T08:12:29.491996+00:00","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,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"3.7.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/ahmedfgad/GeneticAlgorithmPython","docs":"https://pygad.readthedocs.io","changelog":null,"pypi":"https://pypi.org/project/pygad/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["ai-ml"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-29","last_verified":"2026-06-29","next_check":"2026-07-29","install_tag":null}}