[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-ahmedfgad--GeneticAlgorithmPython":3,"tool-ahmedfgad--GeneticAlgorithmPython":61},[4,18,26,36,44,53],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":17},4358,"openclaw","openclaw\u002Fopenclaw","OpenClaw 是一款专为个人打造的本地化 AI 助手，旨在让你在自己的设备上拥有完全可控的智能伙伴。它打破了传统 AI 助手局限于特定网页或应用的束缚，能够直接接入你日常使用的各类通讯渠道，包括微信、WhatsApp、Telegram、Discord、iMessage 等数十种平台。无论你在哪个聊天软件中发送消息，OpenClaw 都能即时响应，甚至支持在 macOS、iOS 和 Android 设备上进行语音交互，并提供实时的画布渲染功能供你操控。\n\n这款工具主要解决了用户对数据隐私、响应速度以及“始终在线”体验的需求。通过将 AI 部署在本地，用户无需依赖云端服务即可享受快速、私密的智能辅助，真正实现了“你的数据，你做主”。其独特的技术亮点在于强大的网关架构，将控制平面与核心助手分离，确保跨平台通信的流畅性与扩展性。\n\nOpenClaw 非常适合希望构建个性化工作流的技术爱好者、开发者，以及注重隐私保护且不愿被单一生态绑定的普通用户。只要具备基础的终端操作能力（支持 macOS、Linux 及 Windows WSL2），即可通过简单的命令行引导完成部署。如果你渴望拥有一个懂你",349277,3,"2026-04-06T06:32:30",[13,14,15,16],"Agent","开发框架","图像","数据工具","ready",{"id":19,"name":20,"github_repo":21,"description_zh":22,"stars":23,"difficulty_score":10,"last_commit_at":24,"category_tags":25,"status":17},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,"2026-04-05T11:01:52",[14,15,13],{"id":27,"name":28,"github_repo":29,"description_zh":30,"stars":31,"difficulty_score":32,"last_commit_at":33,"category_tags":34,"status":17},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",142651,2,"2026-04-06T23:34:12",[14,13,35],"语言模型",{"id":37,"name":38,"github_repo":39,"description_zh":40,"stars":41,"difficulty_score":32,"last_commit_at":42,"category_tags":43,"status":17},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",107888,"2026-04-06T11:32:50",[14,15,13],{"id":45,"name":46,"github_repo":47,"description_zh":48,"stars":49,"difficulty_score":32,"last_commit_at":50,"category_tags":51,"status":17},4721,"markitdown","microsoft\u002Fmarkitdown","MarkItDown 是一款由微软 AutoGen 团队打造的轻量级 Python 工具，专为将各类文件高效转换为 Markdown 格式而设计。它支持 PDF、Word、Excel、PPT、图片（含 OCR）、音频（含语音转录）、HTML 乃至 YouTube 链接等多种格式的解析，能够精准提取文档中的标题、列表、表格和链接等关键结构信息。\n\n在人工智能应用日益普及的今天，大语言模型（LLM）虽擅长处理文本，却难以直接读取复杂的二进制办公文档。MarkItDown 恰好解决了这一痛点，它将非结构化或半结构化的文件转化为模型“原生理解”且 Token 效率极高的 Markdown 格式，成为连接本地文件与 AI 分析 pipeline 的理想桥梁。此外，它还提供了 MCP（模型上下文协议）服务器，可无缝集成到 Claude Desktop 等 LLM 应用中。\n\n这款工具特别适合开发者、数据科学家及 AI 研究人员使用，尤其是那些需要构建文档检索增强生成（RAG）系统、进行批量文本分析或希望让 AI 助手直接“阅读”本地文件的用户。虽然生成的内容也具备一定可读性，但其核心优势在于为机器",93400,"2026-04-06T19:52:38",[52,14],"插件",{"id":54,"name":55,"github_repo":56,"description_zh":57,"stars":58,"difficulty_score":10,"last_commit_at":59,"category_tags":60,"status":17},4487,"LLMs-from-scratch","rasbt\u002FLLMs-from-scratch","LLMs-from-scratch 是一个基于 PyTorch 的开源教育项目，旨在引导用户从零开始一步步构建一个类似 ChatGPT 的大型语言模型（LLM）。它不仅是同名技术著作的官方代码库，更提供了一套完整的实践方案，涵盖模型开发、预训练及微调的全过程。\n\n该项目主要解决了大模型领域“黑盒化”的学习痛点。许多开发者虽能调用现成模型，却难以深入理解其内部架构与训练机制。通过亲手编写每一行核心代码，用户能够透彻掌握 Transformer 架构、注意力机制等关键原理，从而真正理解大模型是如何“思考”的。此外，项目还包含了加载大型预训练权重进行微调的代码，帮助用户将理论知识延伸至实际应用。\n\nLLMs-from-scratch 特别适合希望深入底层原理的 AI 开发者、研究人员以及计算机专业的学生。对于不满足于仅使用 API，而是渴望探究模型构建细节的技术人员而言，这是极佳的学习资源。其独特的技术亮点在于“循序渐进”的教学设计：将复杂的系统工程拆解为清晰的步骤，配合详细的图表与示例，让构建一个虽小但功能完备的大模型变得触手可及。无论你是想夯实理论基础，还是为未来研发更大规模的模型做准备",90106,"2026-04-06T11:19:32",[35,15,13,14],{"id":62,"github_repo":63,"name":64,"description_en":65,"description_zh":66,"ai_summary_zh":66,"readme_en":67,"readme_zh":68,"quickstart_zh":69,"use_case_zh":70,"hero_image_url":71,"owner_login":72,"owner_name":73,"owner_avatar_url":74,"owner_bio":75,"owner_company":76,"owner_location":77,"owner_email":78,"owner_twitter":72,"owner_website":79,"owner_url":80,"languages":81,"stars":90,"forks":91,"last_commit_at":92,"license":93,"difficulty_score":94,"env_os":95,"env_gpu":96,"env_ram":96,"env_deps":97,"category_tags":104,"github_topics":105,"view_count":32,"oss_zip_url":76,"oss_zip_packed_at":76,"status":17,"created_at":115,"updated_at":116,"faqs":117,"releases":147},4786,"ahmedfgad\u002FGeneticAlgorithmPython","GeneticAlgorithmPython","Source code of PyGAD, a Python 3 library for building the genetic algorithm and training machine learning algorithms (Keras & PyTorch).","PyGAD 是一个基于 Python 3 开发的开源库，旨在帮助用户轻松构建遗传算法并优化机器学习模型。它主要解决了在复杂问题中寻找最优解的难题，特别是在需要调整大量参数或处理多目标优化的场景下，能够自动化地搜索最佳解决方案，从而减少人工试错的成本。\n\n这款工具非常适合开发者、数据科学家以及研究人员使用。无论你是希望提升 Keras 或 PyTorch 模型的训练效果，还是需要解决传统的单目标或多目标优化问题，PyGAD 都能提供强有力的支持。其设计初衷是“易用性”，即使没有深厚的进化计算背景，用户也能通过自定义适应度函数快速上手。\n\nPyGAD 的技术亮点在于其高度的灵活性和广泛的兼容性。它不仅支持多种交叉、变异和父代选择策略，允许用户根据具体问题定制算法行为，还无缝集成了主流深度学习框架。此外，项目背后还有一个名为\"Optimization Gadget\"的云端的可视化工具，进一步降低了使用门槛，让优化过程更加直观。作为一个持续活跃更新的开源项目，PyGAD 致力于为用户提供稳定且高效的优化体验。","# PyGAD:  Genetic Algorithm in Python\n\n[PyGAD](https:\u002F\u002Fpypi.org\u002Fproject\u002Fpygad) is an open-source easy-to-use Python 3 library for building the genetic algorithm and optimizing machine learning algorithms. It supports Keras and PyTorch. PyGAD supports optimizing both single-objective and multi-objective problems.\n\n> Try the [Optimization Gadget](https:\u002F\u002Foptimgadget.com), a free cloud-based tool powered by PyGAD. It simplifies optimization by reducing or eliminating the need for coding while providing insightful visualizations.\n\nCheck documentation of the [PyGAD](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest).\n\n[![PyPI Downloads](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_150dfe653354.png)](https:\u002F\u002Fpepy.tech\u002Fproject\u002Fpygad) [![Conda Downloads](https:\u002F\u002Fimg.shields.io\u002Fconda\u002Fdn\u002Fconda-forge\u002Fpygad.svg?label=Conda%20Downloads)](\nhttps:\u002F\u002Fanaconda.org\u002Fconda-forge\u002FPyGAD) [![PyPI version](https:\u002F\u002Fbadge.fury.io\u002Fpy\u002Fpygad.svg)](https:\u002F\u002Fbadge.fury.io\u002Fpy\u002Fpygad)![Docs](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_13d664e1afd7.png)[![PyGAD PyTest \u002F Python 3.13](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py313.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py313.yml) [![PyGAD PyTest \u002F Python 3.12](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py312.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py312.yml) [![PyGAD PyTest \u002F Python 3.11](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py311.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py311.yml) [![PyGAD PyTest \u002F Python 3.10](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py310.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py310.yml) [![PyGAD PyTest \u002F Python 3.9](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py39.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py39.yml) [![PyGAD PyTest \u002F Python 3.8](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py38.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py38.yml) [![License](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FLicense-BSD_3--Clause-blue.svg)](https:\u002F\u002Fopensource.org\u002Flicenses\u002FBSD-3-Clause) [![Translation](https:\u002F\u002Fhosted.weblate.org\u002Fwidgets\u002Fweblate\u002F-\u002Fsvg-badge.svg)](https:\u002F\u002Fhosted.weblate.org\u002Fengage\u002Fweblate\u002F) [![REUSE](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_42921db67a80.png)](https:\u002F\u002Fapi.reuse.software\u002Finfo\u002Fgithub.com\u002FWeblateOrg\u002Fweblate) [![Stack Overflow](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002Fstackoverflow-Ask%20questions-blue.svg)](\nhttps:\u002F\u002Fstackoverflow.com\u002Fquestions\u002Ftagged\u002Fpygad) [![OpenSSF Scorecard](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_951bb6f00eab.png)](https:\u002F\u002Fsecurityscorecards.dev\u002Fviewer\u002F?uri=github.com\u002Fahmedfgad\u002FGeneticAlgorithmPython) [![DOI](https:\u002F\u002Fzenodo.org\u002Fbadge\u002FDOI\u002F10.1007\u002Fs11042-023-17167-y.svg)](https:\u002F\u002Fdoi.org\u002F10.1007\u002Fs11042-023-17167-y)\n\n![PYGAD-LOGO](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_9ae7408017c1.png)\n\n[PyGAD](https:\u002F\u002Fpypi.org\u002Fproject\u002Fpygad) supports different types of crossover, mutation, and parent selection. [PyGAD](https:\u002F\u002Fpypi.org\u002Fproject\u002Fpygad) allows different types of problems to be optimized using the genetic algorithm by customizing the fitness function. \n\nThe library is under active development and more features are added regularly. If you want a feature to be supported, please check the **Contact Us** section to send a request.\n\n# Donation\n\n* [Credit\u002FDebit Card](https:\u002F\u002Fdonate.stripe.com\u002FeVa5kO866elKgM0144): https:\u002F\u002Fdonate.stripe.com\u002FeVa5kO866elKgM0144\n* [Open Collective](https:\u002F\u002Fopencollective.com\u002Fpygad): [opencollective.com\u002Fpygad](https:\u002F\u002Fopencollective.com\u002Fpygad)\n* PayPal: Use either this link: [paypal.me\u002Fahmedfgad](https:\u002F\u002Fpaypal.me\u002Fahmedfgad) or the e-mail address ahmed.f.gad@gmail.com\n* Interac e-Transfer: Use e-mail address ahmed.f.gad@gmail.com\n\n# Installation\n\nTo install [PyGAD](https:\u002F\u002Fpypi.org\u002Fproject\u002Fpygad), simply use pip to download and install the library from [PyPI](https:\u002F\u002Fpypi.org\u002Fproject\u002Fpygad) (Python Package Index). The library is at PyPI at this page https:\u002F\u002Fpypi.org\u002Fproject\u002Fpygad.\n\nInstall PyGAD with the following command:\n\n```python\npip install pygad\n```\n\nTo get started with PyGAD, please read the documentation at [Read The Docs](https:\u002F\u002Fpygad.readthedocs.io\u002F) https:\u002F\u002Fpygad.readthedocs.io.\n\n# PyGAD Source Code\n\nThe source code of the PyGAD' modules is found in the following GitHub projects:\n\n- [pygad](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython): (https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython)\n- [pygad.nn](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FNumPyANN): https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FNumPyANN\n- [pygad.gann](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FNeuralGenetic): https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FNeuralGenetic\n- [pygad.cnn](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FNumPyCNN): https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FNumPyCNN\n- [pygad.gacnn](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FCNNGenetic): https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FCNNGenetic\n- [pygad.kerasga](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FKerasGA): https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FKerasGA\n- [pygad.torchga](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FTorchGA): https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FTorchGA\n\nThe documentation of PyGAD is available at [Read The Docs](https:\u002F\u002Fpygad.readthedocs.io\u002F) https:\u002F\u002Fpygad.readthedocs.io.\n\n# PyGAD Documentation\n\nThe documentation of the PyGAD library is available at [Read The Docs](https:\u002F\u002Fpygad.readthedocs.io) at this link: https:\u002F\u002Fpygad.readthedocs.io. It discusses the modules supported by PyGAD, all its classes, methods, attribute, and functions. For each module, a number of examples are given.\n\nIf there is an issue using PyGAD, feel free to post at issue in this [GitHub repository](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython) https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython or by sending an e-mail to ahmed.f.gad@gmail.com. \n\nIf you built a project that uses PyGAD, then please drop an e-mail to ahmed.f.gad@gmail.com with the following information so that your project is included in the documentation.\n\n- Project title\n- Brief description\n- Preferably, a link that directs the readers to your project\n\nPlease check the **Contact Us** section for more contact details.\n\n# Life Cycle of PyGAD\n\nThe next figure lists the different stages in the lifecycle of an instance of the `pygad.GA` class. Note that PyGAD stops when either all generations are completed or when the function passed to the `on_generation` parameter returns the string `stop`.\n\n![PyGAD Lifecycle](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_cee4f7be1d18.jpg)\n\nThe next code implements all the callback functions to trace the execution of the genetic algorithm. Each callback function prints its name.\n\n```python\nimport pygad\nimport numpy\n\nfunction_inputs = [4,-2,3.5,5,-11,-4.7]\ndesired_output = 44\n\ndef fitness_func(ga_instance, solution, solution_idx):\n    output = numpy.sum(solution*function_inputs)\n    fitness = 1.0 \u002F (numpy.abs(output - desired_output) + 0.000001)\n    return fitness\n\nfitness_function = fitness_func\n\ndef on_start(ga_instance):\n    print(\"on_start()\")\n\ndef on_fitness(ga_instance, population_fitness):\n    print(\"on_fitness()\")\n\ndef on_parents(ga_instance, selected_parents):\n    print(\"on_parents()\")\n\ndef on_crossover(ga_instance, offspring_crossover):\n    print(\"on_crossover()\")\n\ndef on_mutation(ga_instance, offspring_mutation):\n    print(\"on_mutation()\")\n\ndef on_generation(ga_instance):\n    print(\"on_generation()\")\n\ndef on_stop(ga_instance, last_population_fitness):\n    print(\"on_stop()\")\n\nga_instance = pygad.GA(num_generations=3,\n                       num_parents_mating=5,\n                       fitness_func=fitness_function,\n                       sol_per_pop=10,\n                       num_genes=len(function_inputs),\n                       on_start=on_start,\n                       on_fitness=on_fitness,\n                       on_parents=on_parents,\n                       on_crossover=on_crossover,\n                       on_mutation=on_mutation,\n                       on_generation=on_generation,\n                       on_stop=on_stop)\n\nga_instance.run()\n```\n\nBased on the used 3 generations as assigned to the `num_generations` argument, here is the output.\n\n```\non_start()\n\non_fitness()\non_parents()\non_crossover()\non_mutation()\non_generation()\n\non_fitness()\non_parents()\non_crossover()\non_mutation()\non_generation()\n\non_fitness()\non_parents()\non_crossover()\non_mutation()\non_generation()\n\non_stop()\n```\n\n# Example\n\nCheck the [PyGAD's documentation](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002Fpygad.html) for information about the implementation of this example. It solves a single-objective problem.\n\n```python\nimport pygad\nimport numpy\n\n\"\"\"\nGiven the following function:\n    y = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6\n    where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7) and y=44\nWhat are the best values for the 6 weights (w1 to w6)? We are going to use the genetic algorithm to optimize this function.\n\"\"\"\n\nfunction_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs.\ndesired_output = 44 # Function output.\n\ndef fitness_func(ga_instance, solution, solution_idx):\n    # Calculating the fitness value of each solution in the current population.\n    # The fitness function calulates the sum of products between each input and its corresponding weight.\n    output = numpy.sum(solution*function_inputs)\n    fitness = 1.0 \u002F numpy.abs(output - desired_output)\n    return fitness\n\nfitness_function = fitness_func\n\nnum_generations = 100 # Number of generations.\nnum_parents_mating = 7 # Number of solutions to be selected as parents in the mating pool.\n\n# To prepare the initial population, there are 2 ways:\n# 1) Prepare it yourself and pass it to the initial_population parameter. This way is useful when the user wants to start the genetic algorithm with a custom initial population.\n# 2) Assign valid integer values to the sol_per_pop and num_genes parameters. If the initial_population parameter exists, then the sol_per_pop and num_genes parameters are useless.\nsol_per_pop = 50 # Number of solutions in the population.\nnum_genes = len(function_inputs)\n\nlast_fitness = 0\ndef callback_generation(ga_instance):\n    global last_fitness\n    print(f\"Generation = {ga_instance.generations_completed}\")\n    print(f\"Fitness    = {ga_instance.best_solution()[1]}\")\n    print(f\"Change     = {ga_instance.best_solution()[1] - last_fitness}\")\n    last_fitness = ga_instance.best_solution()[1]\n\n# Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor.\nga_instance = pygad.GA(num_generations=num_generations,\n                       num_parents_mating=num_parents_mating, \n                       fitness_func=fitness_function,\n                       sol_per_pop=sol_per_pop, \n                       num_genes=num_genes,\n                       on_generation=callback_generation)\n\n# Running the GA to optimize the parameters of the function.\nga_instance.run()\n\n# After the generations complete, some plots are showed that summarize the how the outputs\u002Ffitenss values evolve over generations.\nga_instance.plot_fitness()\n\n# Returning the details of the best solution.\nsolution, solution_fitness, solution_idx = ga_instance.best_solution()\nprint(f\"Parameters of the best solution : {solution}\")\nprint(f\"Fitness value of the best solution = {solution_fitness}\")\nprint(f\"Index of the best solution : {solution_idx}\")\n\nprediction = numpy.sum(numpy.array(function_inputs)*solution)\nprint(f\"Predicted output based on the best solution : {prediction}\")\n\nif ga_instance.best_solution_generation != -1:\n    print(f\"Best fitness value reached after {ga_instance.best_solution_generation} generations.\")\n\n# Saving the GA instance.\nfilename = 'genetic' # The filename to which the instance is saved. The name is without extension.\nga_instance.save(filename=filename)\n\n# Loading the saved GA instance.\nloaded_ga_instance = pygad.load(filename=filename)\nloaded_ga_instance.plot_fitness()\n```\n\n# For More Information\n\nThere are different resources that can be used to get started with the genetic algorithm and building it in Python. \n\n## Tutorial: Implementing Genetic Algorithm in Python\n\nTo start with coding the genetic algorithm, you can check the tutorial titled [**Genetic Algorithm Implementation in Python**](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fgenetic-algorithm-implementation-python-ahmed-gad) available at these links:\n\n- [LinkedIn](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fgenetic-algorithm-implementation-python-ahmed-gad)\n- [Towards Data Science](https:\u002F\u002Ftowardsdatascience.com\u002Fgenetic-algorithm-implementation-in-python-5ab67bb124a6)\n- [KDnuggets](https:\u002F\u002Fwww.kdnuggets.com\u002F2018\u002F07\u002Fgenetic-algorithm-implementation-python.html)\n\n[This tutorial](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fgenetic-algorithm-implementation-python-ahmed-gad) is prepared based on a previous version of the project but it still a good resource to start with coding the genetic algorithm.\n\n[![Genetic Algorithm Implementation in Python](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_7844a7f9ba02.png)](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fgenetic-algorithm-implementation-python-ahmed-gad)\n\n## Tutorial: Introduction to Genetic Algorithm\n\nGet started with the genetic algorithm by reading the tutorial titled [**Introduction to Optimization with Genetic Algorithm**](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fintroduction-optimization-genetic-algorithm-ahmed-gad) which is available at these links:\n\n* [LinkedIn](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fintroduction-optimization-genetic-algorithm-ahmed-gad)\n* [Towards Data Science](https:\u002F\u002Ftowardsdatascience.com\u002Fintroduction-to-optimization-with-genetic-algorithm-2f5001d9964b)\n* [KDnuggets](https:\u002F\u002Fwww.kdnuggets.com\u002F2018\u002F03\u002Fintroduction-optimization-with-genetic-algorithm.html)\n\n[![Introduction to Genetic Algorithm](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_4f24e8ee79ea.jpg)](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fintroduction-optimization-genetic-algorithm-ahmed-gad)\n\n## Tutorial: Optimize Neural Networks with Genetic Algorithm\n\nRead about training neural networks using the genetic algorithm through the tutorial titled [**Artificial Neural Networks Optimization using Genetic Algorithm with Python**](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fartificial-neural-networks-optimization-using-genetic-ahmed-gad) available at these links:\n\n- [LinkedIn](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fartificial-neural-networks-optimization-using-genetic-ahmed-gad)\n- [Towards Data Science](https:\u002F\u002Ftowardsdatascience.com\u002Fartificial-neural-networks-optimization-using-genetic-algorithm-with-python-1fe8ed17733e)\n- [KDnuggets](https:\u002F\u002Fwww.kdnuggets.com\u002F2019\u002F03\u002Fartificial-neural-networks-optimization-genetic-algorithm-python.html)\n\n[![Training Neural Networks using Genetic Algorithm Python](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_913447204355.jpg)](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fartificial-neural-networks-optimization-using-genetic-ahmed-gad)\n\n## Book: Practical Computer Vision Applications Using Deep Learning with CNNs\n\nYou can also check my book cited as [**Ahmed Fawzy Gad 'Practical Computer Vision Applications Using Deep Learning with CNNs'. Dec. 2018, Apress, 978-1-4842-4167-7**](https:\u002F\u002Fwww.amazon.com\u002FPractical-Computer-Vision-Applications-Learning\u002Fdp\u002F1484241665) which discusses neural networks, convolutional neural networks, deep learning, genetic algorithm, and more.\n\nFind the book at these links:\n\n- [Amazon](https:\u002F\u002Fwww.amazon.com\u002FPractical-Computer-Vision-Applications-Learning\u002Fdp\u002F1484241665)\n- [Springer](https:\u002F\u002Flink.springer.com\u002Fbook\u002F10.1007\u002F978-1-4842-4167-7)\n- [Apress](https:\u002F\u002Fwww.apress.com\u002Fgp\u002Fbook\u002F9781484241660)\n- [O'Reilly](https:\u002F\u002Fwww.oreilly.com\u002Flibrary\u002Fview\u002Fpractical-computer-vision\u002F9781484241677)\n- [Google Books](https:\u002F\u002Fbooks.google.com.eg\u002Fbooks?id=xLd9DwAAQBAJ)\n\n![Fig04](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_4aba9998f41f.jpg)\n\n# Citing PyGAD - Bibtex Formatted Citation\n\nIf you used PyGAD, please consider adding a citation to the following paper about PyGAD:\n\n```\n@article{gad2023pygad,\n  title={Pygad: An intuitive genetic algorithm python library},\n  author={Gad, Ahmed Fawzy},\n  journal={Multimedia Tools and Applications},\n  pages={1--14},\n  year={2023},\n  publisher={Springer}\n}\n```\n\n# Contact Us\n\n* E-mail: ahmed.f.gad@gmail.com\n* [LinkedIn](https:\u002F\u002Fwww.linkedin.com\u002Fin\u002Fahmedfgad)\n* [Paperspace](https:\u002F\u002Fblog.paperspace.com\u002Fauthor\u002Fahmed)\n* [KDnuggets](https:\u002F\u002Fkdnuggets.com\u002Fauthor\u002Fahmed-gad)\n* [TowardsDataScience](https:\u002F\u002Ftowardsdatascience.com\u002F@ahmedfgad)\n* [GitHub](https:\u002F\u002Fgithub.com\u002Fahmedfgad)\n","# PyGAD：Python中的遗传算法\n\n[PyGAD](https:\u002F\u002Fpypi.org\u002Fproject\u002Fpygad) 是一个开源、易于使用的 Python 3 库，用于构建遗传算法并优化机器学习算法。它支持 Keras 和 PyTorch。PyGAD 可以用于优化单目标和多目标问题。\n\n> 试试 [Optimization Gadget](https:\u002F\u002Foptimgadget.com)，这是一款由 PyGAD 提供支持的免费云端工具。它通过减少或消除编码需求来简化优化过程，同时提供富有洞察力的可视化效果。\n\n请查阅 [PyGAD](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest) 的文档。\n\n[![PyPI 下载量](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_150dfe653354.png)](https:\u002F\u002Fpepy.tech\u002Fproject\u002Fpygad) [![Conda 下载量](https:\u002F\u002Fimg.shields.io\u002Fconda\u002Fdn\u002Fconda-forge\u002Fpygad.svg?label=Conda%20Downloads)](\nhttps:\u002F\u002Fanaconda.org\u002Fconda-forge\u002FPyGAD) [![PyPI 版本](https:\u002F\u002Fbadge.fury.io\u002Fpy\u002Fpygad.svg)](https:\u002F\u002Fbadge.fury.io\u002Fpy\u002Fpygad)![文档](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_13d664e1afd7.png)[![PyGAD PyTest \u002F Python 3.13](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py313.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py313.yml) [![PyGAD PyTest \u002F Python 3.12](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py312.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py312.yml) [![PyGAD PyTest \u002F Python 3.11](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py311.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py311.yml) [![PyGAD PyTest \u002F Python 3.10](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py310.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py310.yml) [![PyGAD PyTest \u002F Python 3.9](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py39.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py39.yml) [![PyGAD PyTest \u002F Python 3.8](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py38.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Factions\u002Fworkflows\u002Fmain_py38.yml) [![许可证](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FLicense-BSD_3--Clause-blue.svg)](https:\u002F\u002Fopensource.org\u002Flicenses\u002FBSD-3-Clause) [![翻译](https:\u002F\u002Fhosted.weblate.org\u002Fwidgets\u002Fweblate\u002F-\u002Fsvg-badge.svg)](https:\u002F\u002Fhosted.weblate.org\u002Fengage\u002Fweblate\u002F) [![REUSE](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_42921db67a80.png)](https:\u002F\u002Fapi.reuse.software\u002Finfo\u002Fgithub.com\u002FWeblateOrg\u002Fweblate) [![Stack Overflow](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002Fstackoverflow-Ask%20questions-blue.svg)](\nhttps:\u002F\u002Fstackoverflow.com\u002Fquestions\u002Ftagged\u002Fpygad) [![OpenSSF Scorecard](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_951bb6f00eab.png)](https:\u002F\u002Fsecurityscorecards.dev\u002Fviewer\u002F?uri=github.com\u002Fahmedfgad\u002FGeneticAlgorithmPython) [![DOI](https:\u002F\u002Fzenodo.org\u002Fbadge\u002FDOI\u002F10.1007\u002Fs11042-023-17167-y.svg)](https:\u002F\u002Fdoi.org\u002F10.1007\u002Fs11042-023-17167-y)\n\n![PYGAD-LOGO](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_9ae7408017c1.png)\n\n[PyGAD](https:\u002F\u002Fpypi.org\u002Fproject\u002Fpygad) 支持多种类型的交叉、变异和父代选择。通过自定义适应度函数，[PyGAD](https:\u002F\u002Fpypi.org\u002Fproject\u002Fpygad) 能够使用遗传算法优化不同类型的问题。\n\n该库目前仍在积极开发中，功能也在不断更新。如果您希望新增某项功能，请查看“联系我们”部分提交请求。\n\n# 捐赠\n\n* [信用卡\u002F借记卡](https:\u002F\u002Fdonate.stripe.com\u002FeVa5kO866elKgM0144): https:\u002F\u002Fdonate.stripe.com\u002FeVa5kO866elKgM0144\n* [Open Collective](https:\u002F\u002Fopencollective.com\u002Fpygad): [opencollective.com\u002Fpygad](https:\u002F\u002Fopencollective.com\u002Fpygad)\n* PayPal: 使用此链接：[paypal.me\u002Fahmedfgad](https:\u002F\u002Fpaypal.me\u002Fahmedfgad) 或电子邮件地址 ahmed.f.gad@gmail.com\n* Interac e-Transfer：使用电子邮件地址 ahmed.f.gad@gmail.com\n\n# 安装\n\n要安装 [PyGAD](https:\u002F\u002Fpypi.org\u002Fproject\u002Fpygad)，只需使用 pip 从 [PyPI](https:\u002F\u002Fpypi.org\u002Fproject\u002Fpygad)（Python 包索引）下载并安装该库。该库在 PyPI 上的页面为：https:\u002F\u002Fpypi.org\u002Fproject\u002Fpygad。\n\n使用以下命令安装 PyGAD：\n\n```python\npip install pygad\n```\n\n要开始使用 PyGAD，请阅读位于 [Read The Docs](https:\u002F\u002Fpygad.readthedocs.io\u002F) 的文档：https:\u002F\u002Fpygad.readthedocs.io。\n\n# PyGAD 源代码\n\nPyGAD 各模块的源代码位于以下 GitHub 项目中：\n\n- [pygad](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython): (https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython)\n- [pygad.nn](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FNumPyANN): https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FNumPyANN\n- [pygad.gann](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FNeuralGenetic): https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FNeuralGenetic\n- [pygad.cnn](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FNumPyCNN): https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FNumPyCNN\n- [pygad.gacnn](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FCNNGenetic): https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FCNNGenetic\n- [pygad.kerasga](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FKerasGA): https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FKerasGA\n- [pygad.torchga](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FTorchGA): https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FTorchGA\n\nPyGAD 的文档可在 [Read The Docs](https:\u002F\u002Fpygad.readthedocs.io) 上找到：https:\u002F\u002Fpygad.readthedocs.io。\n\n# PyGAD 文档\n\nPyGAD 库的文档可在 [Read The Docs](https:\u002F\u002Fpygad.readthedocs.io) 上找到，链接为：https:\u002F\u002Fpygad.readthedocs.io。文档详细介绍了 PyGAD 支持的模块、所有类、方法、属性和函数，并为每个模块提供了多个示例。\n\n如果在使用 PyGAD 时遇到问题，欢迎在 [GitHub 仓库](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython) 中提交问题：https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython，或者发送邮件至 ahmed.f.gad@gmail.com。\n\n如果您使用 PyGAD 构建了某个项目，请将以下信息发送至 ahmed.f.gad@gmail.com，以便将您的项目收录到文档中：\n\n- 项目标题\n- 简要描述\n- 最好附上指向您项目的链接\n\n更多联系方式请参阅“联系我们”部分。\n\n# PyGAD 的生命周期\n\n下图列出了 `pygad.GA` 类实例生命周期中的各个阶段。请注意，当所有代数都已完成，或者当传递给 `on_generation` 参数的函数返回字符串 `stop` 时，PyGAD 将停止运行。\n\n![PyGAD 生命周期](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_cee4f7be1d18.jpg)\n\n以下代码实现了所有回调函数，以跟踪遗传算法的执行过程。每个回调函数都会打印其名称。\n\n```python\nimport pygad\nimport numpy\n\nfunction_inputs = [4,-2,3.5,5,-11,-4.7]\ndesired_output = 44\n\ndef fitness_func(ga_instance, solution, solution_idx):\n    output = numpy.sum(solution*function_inputs)\n    fitness = 1.0 \u002F (numpy.abs(output - desired_output) + 0.000001)\n    return fitness\n\nfitness_function = fitness_func\n\ndef on_start(ga_instance):\n    print(\"on_start()\")\n\ndef on_fitness(ga_instance, population_fitness):\n    print(\"on_fitness()\")\n\ndef on_parents(ga_instance, selected_parents):\n    print(\"on_parents()\")\n\ndef on_crossover(ga_instance, offspring_crossover):\n    print(\"on_crossover()\")\n\ndef on_mutation(ga_instance, offspring_mutation):\n    print(\"on_mutation()\")\n\ndef on_generation(ga_instance):\n    print(\"on_generation()\")\n\ndef on_stop(ga_instance, last_population_fitness):\n    print(\"on_stop()\")\n\nga_instance = pygad.GA(num_generations=3,\n                       num_parents_mating=5,\n                       fitness_func=fitness_function,\n                       sol_per_pop=10,\n                       num_genes=len(function_inputs),\n                       on_start=on_start,\n                       on_fitness=on_fitness,\n                       on_parents=on_parents,\n                       on_crossover=on_crossover,\n                       on_mutation=on_mutation,\n                       on_generation=on_generation,\n                       on_stop=on_stop)\n\nga_instance.run()\n```\n\n根据为 `num_generations` 参数指定的 3 个代数，以下是输出结果。\n\n```\non_start()\n\non_fitness()\non_parents()\non_crossover()\non_mutation()\non_generation()\n\non_fitness()\non_parents()\non_crossover()\non_mutation()\non_generation()\n\non_fitness()\non_parents()\non_crossover()\non_mutation()\non_generation()\n\non_stop()\n```\n\n# 示例\n\n有关此示例实现的信息，请参阅 [PyGAD 的文档](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002Fpygad.html)。它解决的是一个单目标优化问题。\n\n```python\nimport pygad\nimport numpy\n\n\"\"\"\n已知以下函数：\n    y = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + w6x6\n    其中 (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7)，且 y=44\n那么，这 6 个权重（w1 至 w6）的最佳值是多少？我们将使用遗传算法来优化这个函数。\n\"\"\"\n\nfunction_inputs = [4,-2,3.5,5,-11,-4.7] # 函数输入。\ndesired_output = 44 # 函数输出。\n\ndef fitness_func(ga_instance, solution, solution_idx):\n    # 计算当前种群中每个解的适应度值。\n    # 适应度函数计算每个输入与其对应权重的乘积之和。\n    output = numpy.sum(solution*function_inputs)\n    fitness = 1.0 \u002F numpy.abs(output - desired_output)\n    return fitness\n\nfitness_function = fitness_func\n\nnum_generations = 100 # 代数。\nnum_parents_mating = 7 # 作为交配池中父母的解的数量。\n\n# 准备初始种群有两种方式：\n# 1) 自己准备并将其传递给 initial_population 参数。这种方式适用于用户希望从自定义初始种群开始运行遗传算法的情况。\n# 2) 为 sol_per_pop 和 num_genes 参数分配有效的整数值。如果提供了 initial_population 参数，则 sol_per_pop 和 num_genes 参数将不起作用。\nsol_per_pop = 50 # 种群中的解的数量。\nnum_genes = len(function_inputs)\n\nlast_fitness = 0\ndef callback_generation(ga_instance):\n    global last_fitness\n    print(f\"代数 = {ga_instance.generations_completed}\")\n    print(f\"适应度值 = {ga_instance.best_solution()[1]}\")\n    print(f\"变化 = {ga_instance.best_solution()[1] - last_fitness}\")\n    last_fitness = ga_instance.best_solution()[1]\n\n# 在 ga 模块中创建 GA 类的实例。一些参数在构造函数中初始化。\nga_instance = pygad.GA(num_generations=num_generations,\n                       num_parents_mating=num_parents_mating, \n                       fitness_func=fitness_function,\n                       sol_per_pop=sol_per_pop, \n                       num_genes=num_genes,\n                       on_generation=callback_generation)\n\n# 运行 GA 以优化函数的参数。\nga_instance.run()\n\n# 代数完成后，会显示一些图表，总结输出\u002F适应度值随代数的变化情况。\nga_instance.plot_fitness()\n\n# 返回最佳解的详细信息。\nsolution, solution_fitness, solution_idx = ga_instance.best_solution()\nprint(f\"最佳解的参数：{solution}\")\nprint(f\"最佳解的适应度值 = {solution_fitness}\")\nprint(f\"最佳解的索引：{solution_idx}\")\n\nprediction = numpy.sum(numpy.array(function_inputs)*solution)\nprint(f\"基于最佳解的预测输出：{prediction}\")\n\nif ga_instance.best_solution_generation != -1:\n    print(f\"最佳适应度值在第 {ga_instance.best_solution_generation} 代达到。\")\n\n# 保存 GA 实例。\nfilename = 'genetic' # 保存实例的文件名，不带扩展名。\nga_instance.save(filename=filename)\n\n# 加载保存的 GA 实例。\nloaded_ga_instance = pygad.load(filename=filename)\nloaded_ga_instance.plot_fitness()\n```\n\n# 更多信息\n\n有许多资源可以帮助您入门遗传算法，并在 Python 中构建它。\n\n## 教程：在 Python 中实现遗传算法\n\n要开始编写遗传算法代码，可以查看题为[**Python 中的遗传算法实现**](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fgenetic-algorithm-implementation-python-ahmed-gad)的教程，该教程可通过以下链接访问：\n\n- [LinkedIn](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fgenetic-algorithm-implementation-python-ahmed-gad)\n- [Towards Data Science](https:\u002F\u002Ftowardsdatascience.com\u002Fgenetic-algorithm-implementation-in-python-5ab67bb124a6)\n- [KDnuggets](https:\u002F\u002Fwww.kdnuggets.com\u002F2018\u002F07\u002Fgenetic-algorithm-implementation-python.html)\n\n[本教程](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fgenetic-algorithm-implementation-python-ahmed-gad)基于该项目的早期版本编写，但仍然是开始学习遗传算法编程的一个不错资源。\n\n[![Python 中的遗传算法实现](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_7844a7f9ba02.png)](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fgenetic-algorithm-implementation-python-ahmed-gad)\n\n## 教程：遗传算法简介\n\n通过阅读题为[**遗传算法优化入门**](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fintroduction-optimization-genetic-algorithm-ahmed-gad)的教程，开始学习遗传算法。该教程可通过以下链接访问：\n\n* [LinkedIn](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fintroduction-optimization-genetic-algorithm-ahmed-gad)\n* [Towards Data Science](https:\u002F\u002Ftowardsdatascience.com\u002Fintroduction-to-optimization-with-genetic-algorithm-2f5001d9964b)\n* [KDnuggets](https:\u002F\u002Fwww.kdnuggets.com\u002F2018\u002F03\u002Fintroduction-optimization-with-genetic-algorithm.html)\n\n[![遗传算法简介](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_4f24e8ee79ea.jpg)](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fintroduction-optimization-genetic-algorithm-ahmed-gad)\n\n## 教程：用遗传算法优化神经网络\n\n通过题为[**使用遗传算法和 Python 优化人工神经网络**](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fartificial-neural-networks-optimization-using-genetic-ahmed-gad)的教程，了解如何利用遗传算法训练神经网络。该教程可通过以下链接访问：\n\n- [LinkedIn](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fartificial-neural-networks-optimization-using-genetic-ahmed-gad)\n- [Towards Data Science](https:\u002F\u002Ftowardsdatascience.com\u002Fartificial-neural-networks-optimization-using-genetic-algorithm-with-python-1fe8ed17733e)\n- [KDnuggets](https:\u002F\u002Fwww.kdnuggets.com\u002F2019\u002F03\u002Fartificial-neural-networks-optimization-genetic-algorithm-python.html)\n\n[![使用遗传算法和 Python 训练神经网络](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_913447204355.jpg)](https:\u002F\u002Fwww.linkedin.com\u002Fpulse\u002Fartificial-neural-networks-optimization-using-genetic-ahmed-gad)\n\n## 书籍：使用 CNN 的深度学习进行实用计算机视觉应用\n\n您还可以查阅我的著作，即[**Ahmed Fawzy Gad，《使用 CNN 的深度学习进行实用计算机视觉应用》。2018 年 12 月，Apress 出版社，ISBN：978-1-4842-4167-7**](https:\u002F\u002Fwww.amazon.com\u002FPractical-Computer-Vision-Applications-Learning\u002Fdp\u002F148424165)，书中讨论了神经网络、卷积神经网络、深度学习、遗传算法等内容。\n\n您可以通过以下链接找到该书：\n\n- [Amazon](https:\u002F\u002Fwww.amazon.com\u002FPractical-Computer-Vision-Applications-Learning\u002Fdp\u002F1484241665)\n- [Springer](https:\u002F\u002Flink.springer.com\u002Fbook\u002F10.1007\u002F978-1-4842-4167-7)\n- [Apress](https:\u002F\u002Fwww.apress.com\u002Fgp\u002Fbook\u002F9781484241660)\n- [O'Reilly](https:\u002F\u002Fwww.oreilly.com\u002Flibrary\u002Fview\u002Fpractical-computer-vision\u002F9781484241677)\n- [Google Books](https:\u002F\u002Fbooks.google.com.eg\u002Fbooks?id=xLd9DwAAQBAJ)\n\n![Fig04](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_readme_4aba9998f41f.jpg)\n\n# 引用 PyGAD - BibTeX 格式引用\n\n如果您使用了 PyGAD，请考虑在关于 PyGAD 的论文中添加以下引用：\n\n```\n@article{gad2023pygad,\n  title={Pygad: 一个直观的遗传算法 Python 库},\n  author={Gad, Ahmed Fawzy},\n  journal={多媒体工具与应用},\n  pages={1--14},\n  year={2023},\n  publisher={Springer}\n}\n```\n\n# 联系我们\n\n* 邮箱：ahmed.f.gad@gmail.com\n* [LinkedIn](https:\u002F\u002Fwww.linkedin.com\u002Fin\u002Fahmedfgad)\n* [Paperspace](https:\u002F\u002Fblog.paperspace.com\u002Fauthor\u002Fahmed)\n* [KDnuggets](https:\u002F\u002Fkdnuggets.com\u002Fauthor\u002Fahmed-gad)\n* [TowardsDataScience](https:\u002F\u002Ftowardsdatascience.com\u002F@ahmedfgad)\n* [GitHub](https:\u002F\u002Fgithub.com\u002Fahmedfgad)","# PyGAD 快速上手指南\n\nPyGAD 是一个易于使用的 Python 3 开源库，用于构建遗传算法（Genetic Algorithm）并优化机器学习模型。它支持 Keras 和 PyTorch 框架，适用于单目标和多目标优化问题。\n\n## 环境准备\n\n*   **操作系统**：Windows、Linux 或 macOS\n*   **Python 版本**：Python 3.8 至 3.13\n*   **前置依赖**：\n    *   `numpy` (核心依赖)\n    *   `matplotlib` (用于绘图可视化，可选但推荐)\n    *   若需优化深度学习模型，请确保已安装 `tensorflow`\u002F`keras` 或 `torch`\n\n## 安装步骤\n\n使用 pip 从 PyPI 安装最新稳定版：\n\n```bash\npip install pygad\n```\n\n**国内加速方案**：\n如果您在中国大陆，建议使用清华或阿里镜像源以加快下载速度：\n\n```bash\npip install pygad -i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple\n```\n\n或者使用阿里云镜像：\n\n```bash\npip install pygad -i https:\u002F\u002Fmirrors.aliyun.com\u002Fpypi\u002Fsimple\u002F\n```\n\n## 基本使用\n\n以下示例演示如何使用 PyGAD 解决一个简单的线性方程权重优化问题。\n目标：找到权重 $w_1$ 到 $w_6$，使得 $y = \\sum(w_i \\times x_i)$ 最接近期望值 44。\n\n```python\nimport pygad\nimport numpy\n\n# 定义函数输入和期望输出\nfunction_inputs = [4, -2, 3.5, 5, -11, -4.7]\ndesired_output = 44\n\n# 定义适应度函数\ndef fitness_func(ga_instance, solution, solution_idx):\n    output = numpy.sum(solution * function_inputs)\n    # 适应度值越大越好，这里使用误差倒数的逻辑\n    fitness = 1.0 \u002F numpy.abs(output - desired_output)\n    return fitness\n\n# 配置遗传算法参数\nga_instance = pygad.GA(\n    num_generations=100,          # 迭代代数\n    num_parents_mating=7,         # 参与交配的父母数量\n    sol_per_pop=50,               # 种群大小\n    num_genes=len(function_inputs), # 基因数量（即权重数量）\n    fitness_func=fitness_func     # 适应度函数\n)\n\n# 运行遗传算法\nga_instance.run()\n\n# 可视化适应度变化\nga_instance.plot_fitness()\n\n# 获取最佳结果\nsolution, solution_fitness, solution_idx = ga_instance.best_solution()\nprint(f\"最佳解决方案的参数：{solution}\")\nprint(f\"最佳解决方案的适应度值：{solution_fitness}\")\n\n# 验证预测结果\nprediction = numpy.sum(numpy.array(function_inputs) * solution)\nprint(f\"基于最佳解的预测输出：{prediction}\")\n```\n\n### 代码说明\n1.  **fitness_func**: 核心逻辑，计算当前解（权重组合）与目标值的接近程度。\n2.  **pygad.GA**: 初始化算法实例，设置迭代次数、种群大小等超参数。\n3.  **run()**: 启动进化过程。\n4.  **best_solution()**: 算法结束后提取最优解。","某金融科技团队正在构建一个高频交易预测模型，需要利用深度学习网络从海量历史数据中挖掘规律，但模型超参数组合过于庞大，传统方法难以找到全局最优解。\n\n### 没有 GeneticAlgorithmPython 时\n- 工程师只能依赖手动试错或简单的网格搜索来调整神经网络的层数、学习率等关键参数，耗时数周仍可能陷入局部最优。\n- 面对多目标优化需求（如同时最大化收益率并最小化回撤），缺乏原生支持，需自行编写复杂的加权算法，代码维护成本极高。\n- 从零实现遗传算法的交叉、变异和选择算子不仅容易出错，还难以适配 Keras 或 PyTorch 框架，导致模型训练流程频繁中断。\n- 每次参数迭代都需要重写大量样板代码，团队无法快速验证新的假设，严重拖慢了策略上线的节奏。\n\n### 使用 GeneticAlgorithmPython 后\n- 通过调用现成的 API，团队在几天内就完成了对深度学习模型超参数的自动化寻优，迅速锁定了全局最佳配置。\n- 利用其内置的多目标优化功能，轻松平衡了收益与风险指标，无需再为设计复杂的适应度函数而头疼。\n- 直接无缝对接现有的 Keras 和 PyTorch 模型，灵活配置多种交叉与变异策略，大幅降低了集成难度和报错率。\n- 标准化的开发流程让研究人员能专注于策略逻辑本身，将原本数周的参数调优工作压缩至数小时，显著提升了迭代效率。\n\nGeneticAlgorithmPython 将复杂的进化计算转化为简洁的代码调用，让开发者能以最低成本释放机器学习模型的最大潜能。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fahmedfgad_GeneticAlgorithmPython_9ae74080.png","ahmedfgad","Ahmed Gad","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fahmedfgad_6fdf3b67.jpg","Machine Learning Researcher & Technical Author \u002F\u002F Open to Work",null,"Ottawa, ON, Canada","ahmed.f.gad@gmail.com","https:\u002F\u002Fwww.linkedin.com\u002Fin\u002Fahmedfgad","https:\u002F\u002Fgithub.com\u002Fahmedfgad",[82,86],{"name":83,"color":84,"percentage":85},"Python","#3572A5",99.3,{"name":87,"color":88,"percentage":89},"Cython","#fedf5b",0.7,2182,497,"2026-04-05T21:07:00","BSD-3-Clause",1,"Linux, macOS, Windows","未说明",{"notes":98,"python":99,"dependencies":100},"该库是一个纯 Python 实现的遗传算法工具，核心依赖仅为 NumPy。Keras 和 PyTorch 仅在需要优化深度学习模型时为可选依赖。支持单目标和多目标优化问题。可通过 pip 或 conda 安装。","3.8, 3.9, 3.10, 3.11, 3.12, 3.13",[101,102,103],"numpy","keras (可选)","pytorch (可选)",[14],[106,107,108,101,109,110,111,112,113,114],"python","genetic-algorithm","optimization","pygad","pygad-documentation","neural-networks","machine-learning","deep-learning","evolutionary-algorithms","2026-03-27T02:49:30.150509","2026-04-07T10:40:37.315632",[118,123,128,133,138,142],{"id":119,"question_zh":120,"answer_zh":121,"source_url":122},21733,"如何避免多次重复计算适应度值（fitness value）？","PyGAD 2.19.0 版本引入了一个名为 `fitness_batch_size` 的参数，允许一次性为一批解决方案调用适应度函数，从而优化计算效率。此外，虽然开启了 `save_solutions` 选项，但它主要用于保存解而非直接缓存适应度计算结果。对于需要大量计算的场景，建议升级到此版本并使用批处理功能。","https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F160",{"id":124,"question_zh":125,"answer_zh":126,"source_url":127},21734,"教程中的代码与 GitHub 仓库中的代码不一致导致报错怎么办？","由于库的版本迭代，教程（如 Towards Data Science 上的文章）中的代码可能与最新的 GitHub 源码或 PyPI 包不一致。例如，`cal_pop_fitness()` 函数的参数可能已变更。解决方法是：1. 优先参考 GitHub 仓库中最新源码或官方文档；2. 确认您安装的 PyPI 版本（如 `pygad 1.0.20`）是否与教程对应的版本一致；3. 根据当前版本的函数签名调整您的调用代码。","https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F7",{"id":129,"question_zh":130,"answer_zh":131,"source_url":132},21735,"设置 `allow_duplicate_genes=False` 后为什么仍然出现重复基因？","该参数主要保证初始种群的唯一性。如果在进化过程中出现重复，通常是因为变异或交叉操作产生的新基因落在了 `gene_space` 定义的范围内但与现有基因冲突。维护者指出，只要初始种群是唯一的，库生成的基因在尊重 `gene_space` 范围的前提下通常是唯一的。如果遇到特定边界情况，可能需要自定义变异逻辑来微调生成的基因值以避开重复。","https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F164",{"id":134,"question_zh":135,"answer_zh":136,"source_url":137},21736,"导入库时遇到 \"module 'numpy' has no attribute 'int'\" 错误如何解决？","这是由于新版 NumPy（1.24+）移除了 `numpy.int` 和 `numpy.float` 别名导致的。解决方法是更新 `pygad.py` 源文件中的类型定义列表：\n1. 找到 `supported_int_types` 行，删除 `numpy.int`，保留 `[int, numpy.int8, numpy.int16, numpy.int32, numpy.int64, ...]`。\n2. 找到 `supported_float_types` 行，删除 `numpy.float`，保留 `[float, numpy.float16, numpy.float32, numpy.float64]`。\n或者直接升级到修复了此问题的最新 PyGAD 版本。","https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F151",{"id":139,"question_zh":140,"answer_zh":141,"source_url":122},21737,"在使用多进程（multiprocessing）时，`keep_elitism` 参数为何失效？","这是一个已知问题。当使用 `parallel_processing=['process', N]` 启用多进程并行处理时，`keep_elitism` 参数可能无法正确保留精英个体到下一代。这是因为进程间内存隔离导致状态同步问题。目前的变通方案是：如果必须保留精英策略，请暂时禁用多进程（即不使用 `parallel_processing` 参数），或者在 `on_generation` 回调中手动检查并处理精英个体的保留逻辑。",{"id":143,"question_zh":144,"answer_zh":145,"source_url":146},21738,"如何针对长时间运行的物理实验（如每 15 分钟评估一次）配置遗传算法？","对于涉及硬件交互或长时间等待的实验场景，建议将遗传算法的每一步控制放在主循环中，而不是依赖库的自动连续运行。您可以：1. 手动控制每一代的生成；2. 在每一代结束后，将当前的光水平组合发送给 Arduino；3. 等待 15 分钟收集数据；4. 计算适应度值并反馈给算法；5. 手动触发下一代的生成。这种“手动步进”的方式比让库自动运行更适合长周期任务。","https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F4",[148,153,158,163,168,173,178,183,188,193,198,202,207,212,217,222,227,232,237,242],{"id":149,"version":150,"summary_zh":151,"released_at":152},127760,"3.5.0","1. 修复多目标问题中，`stop_criteria` 参数内使用负号（-）时的错误。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F314 https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F323\n2. 修复多目标问题中，`stop_criteria` 参数以可迭代对象（如列表）形式传递时的错误（例如 `['reach_50_60', 'reach_20, 40']`）。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F314\n3. 在 `pygad.visualize.plot.Plot` 类中的 `plot_genes()` 方法内调用 `get_matplotlib()` 函数，以导入 matplotlib 库。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F315\n4. 在 `pygad\u002Fhelper\u002Funique.py` 脚本中创建一个新的辅助方法 `select_unique_value()`，用于从值数组中选择一个唯一的基因。\n5. 在 `pygad\u002Futils\u002Fmutation.py` 脚本中创建一个新的辅助方法 `get_random_mutation_range()`，该方法根据基因的索引返回单个基因的随机突变范围（最小值和最大值）。\n6. 在 `pygad\u002Futils\u002Fmutation.py` 脚本中创建一个新的辅助方法 `change_random_mutation_value_dtype`，用于更改用于随机突变的基因值的数据类型。\n7. 在 `pygad\u002Futils\u002Fmutation.py` 脚本中创建一个新的辅助方法 `round_random_mutation_value()`，用于对用于随机突变的基因值进行四舍五入。\n8. 创建 `pygad\u002Fhelper\u002Fmisc.py` 脚本，并在其中定义一个名为 `Helper` 的类，包含以下辅助方法：\n   1. `change_population_dtype_and_round()`：对种群中的每个基因值进行四舍五入并更改数据类型。\n   2. `change_gene_dtype_and_round()`：对单个基因值进行四舍五入并更改数据类型。\n   3. `mutation_change_gene_dtype_and_round()`：决定是否通过替换方式进行突变，然后对新基因值进行四舍五入并更改数据类型。\n   4. `validate_gene_constraint_callable_output()`：验证用户自定义可调用函数的输出，以检查是否满足 `gene_constraint` 参数中定义的基因约束条件。\n   5. `get_gene_dtype()`：从 `gene_type` 实例属性中返回基因的数据类型。\n   6. `get_random_mutation_range()`：使用 `random_mutation_min_val` 和 `random_mutation_max_val` 实例属性返回随机突变范围。\n   7. `get_initial_population_range()`：使用 `init_range_low` 和 `init_range_high` 实例属性返回初始种群的取值范围。\n   8. `generate_gene_value_from_space()`：利用 `gene_space` 实例属性为基因生成或选择一个值。\n   9. `generate_gene_value_randomly()`：为基因生成一个随机值。仅当 `gene_space` 为 `None` 时使用。\n   10. `generate_gene_value()`：为基因生成一个值。该方法会检查 `gene_space` 是否为 `None`，并调用 `generate_gene_value_randomly()` 或 `generate_gene_value_from_space()`。\n   11. `filter_gene_values_by_constraint()`：接收一个基因值列表，并对其进行筛选。","2025-07-09T02:24:03",{"id":154,"version":155,"summary_zh":156,"released_at":157},127761,"3.4.0","1. 从 `pygad.GA` 类的构造函数中移除了 `delay_after_gen` 参数。因此，它不再是 `pygad.GA` 类实例的一个属性。若需在每一代结束后添加延迟，可在 `on_generation` 回调函数中实现。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F283\n2. 在 `pygad.utils.crossover.Crossover` 类的 `single_point_crossover()` 方法中，所有随机交叉点都在 `for` 循环之前一次性返回。具体做法是在循环开始前仅调用一次 `numpy.random.randint()` 函数，生成全部 K 个点（其中 K 为子代数量），而不是在 `for` 循环内针对每个子代个体分别调用该函数 K 次。\n3. 修复了 `examples\u002Fexample_custom_operators.py` 脚本中的一个错误。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fpull\u002F285\n4. 使用 `pygad.torchga.predict()` 函数进行预测时，不会计算梯度。\n5. `pygad.helper.unique.Unique.unique_int_gene_from_range()` 方法的 `gene_type` 参数现在仅接受当前基因的类型，而非完整的 `gene_type` 列表。\n6. 在 `pygad.helper.unique.Unique` 类中新增了一个名为 `unique_float_gene_from_range()` 的方法，用于从指定范围内找到一个唯一的浮点数。\n7. 修复了 `pygad.helper.unique.Unique.unique_gene_by_space()` 方法中的一个错误，使其仅返回数值，而非 NumPy 数组。\n8. 对 `pygad\u002Fhelper\u002Funique.py` 脚本进行了重构，以去除重复代码并重新格式化文档字符串。\n9. 向 `pygad.visualize.plot.Plot` 类中添加了 `plot_pareto_front_curve()` 方法，用于可视化多目标问题的帕累托前沿。该方法目前仅支持两个目标。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F279\n10. 修复了一个将嵌套 NumPy 数组转换为嵌套列表的错误。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F300\n11. `Matplotlib` 库仅在使用 `pygad\u002Fvisualize\u002Fplot.py` 脚本中的某个方法时才会被导入。这种按需导入的方式比在模块级别直接使用 `import matplotlib.pyplot` 更加高效，因为后者会导致即使不需要时也会在导入 `pygad` 时加载 `Matplotlib`。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F292\n12. 修复了在 `stop_criteria` 参数中使用负号（例如 `stop_criteria=[\"saturate_10\", \"reach_-0.5\"]`）时出现的错误。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F296\n13. 确保在 `cal_pop_fitness` 方法中，`self.best_solutions` 是一个列表的列表。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F293\n14. 修复了一个错误：`cal_pop_fitness()` 方法曾使用 `previous_generation_fitness` 属性来返回父代的适应度值。然而，该实例属性并未反映最新种群的适应度，而是使用了倒数第二代的适应度值。现已通过将 `previous_generation_fitness` 属性更新为最新的值来解决此问题。","2025-01-07T23:53:04",{"id":159,"version":160,"summary_zh":161,"released_at":162},127762,"3.3.1","1. 在上一代结束后、`run()` 方法完成之前，更新两个实例属性：1) `last_generation_parents`；2) `last_generation_parents_indices`。这样做是为了使父代列表与最新的种群适应度 `last_generation_fitness` 保持同步。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F275\n2. 有4个方法的名称以 `run_` 开头。它们的作用是使 `run()` 方法中的主循环保持简洁。更多信息请参阅 [[其他方法](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002Fpygad.html#other-methods)](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002Fpygad.html#other-methods) 部分。","2024-02-17T21:05:43",{"id":164,"version":165,"summary_zh":166,"released_at":167},127763,"3.3.0","发布日期：2024年1月29日\n\n1. 修复了在使用多目标优化时的若干错误。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F238\n2. 当 `stop_criteria` 参数与 `reach` 关键字一起使用时，在求解多目标问题时可以传递多个数值。例如，如果一个问题有3个目标函数，则 `stop_criteria=\"reach_10_20_30\"` 表示当3个目标的适应度分别至少达到10、20和30时，遗传算法将停止。这些数值必须与目标函数的数量相匹配。如果在求解多目标问题时只提供了一个值（如 `stop_criteria=reach_5`），则该值将应用于所有目标。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F238\n3. `delay_after_gen` 参数现已弃用，并将在未来的版本中移除。如果确实需要在每一代之后添加延迟时间，可将一个回调函数或方法赋值给 `on_generation` 参数来暂停进化过程。\n4. 并行处理现在支持在自适应变异过程中计算适应度。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F201\n5. 可以通过更改所有会影响遗传算法所用对象大小的参数，在运行时动态调整种群规模。更多信息请参阅[[运行时更改种群规模]](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002Fpygad_more.html#change-population-size-during-runtime)一节。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fdiscussions\u002F234\n6. 当 `gene_space` 参数中存在没有步长的字典时，变异操作会通过向基因值添加一个随机值来实现。该随机值根据 `random_mutation_min_val` 和 `random_mutation_max_val` 两个参数生成。更多信息请参阅[[基因空间参数下的变异如何工作？]](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002Fpygad_more.html#how-mutation-works-with-the-gene-space-parameter)一节。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F229\n7. 将 `object` 类型添加到整数（GA.supported_int_types）和浮点数（GA.supported_float_types）的支持数据类型中。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F174\n8. 使用 `raise` 语句代替 `sys.exit(-1)` 来终止程序执行。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F213\n9. 修复了在使用批处理适应度计算（例如将 `fitness_batch_size` 设置为非零值）进行多目标优化时的一个错误。\n10. 修复了 `pygad.py` 脚本中在查找最佳解索引时的一个错误。该错误在多目标优化且 `self.best_solutions_fitness` 具有多列的情况下无法正常工作。\n\n   ```python\n            self.best_solution_generation = numpy","2024-01-29T06:39:10",{"id":169,"version":170,"summary_zh":171,"released_at":172},127764,"3.2.0","1. 新创建了一个模块 `pygad.utils.nsga2`，其中包含实现了 NSGA-II 功能的 `NSGA2` 类。该类具有以下方法：1) `get_non_dominated_set()`；2) `non_dominated_sorting()`；3) `crowding_distance()`；4) `sort_solutions_nsga2()`。示例请参见 [[此部分](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002Fpygad_more.html#multi-objective-optimization)](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002Fpygad_more.html#multi-objective-optimization)。\n2. 支持使用 `pygad.utils.nsga2` 模块中的 `NSGA2` 类，通过非支配排序遗传算法 II (NSGA-II) 进行多目标优化。只需在适应度函数中返回一个 `list`、`tuple` 或 `numpy.ndarray`，库就会将问题视为多目标优化问题。所有目标均假定为最大化问题。示例请参见 [[此部分](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002Fpygad_more.html#multi-objective-optimization)](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002Fpygad_more.html#multi-objective-optimization)。\n3. 对父代选择方法和自适应变异进行了修改，以支持多目标优化。\n4. 在 `pygad.utils.parent_selection` 模块中新增了两种 NSGA-II 父代选择方法：1) NSGA-II 托 tournament 选择；2) NSGA-II 选择。\n5. `pygad.plot` 模块中的 `plot_fitness()` 方法新增了一个可选参数 `label`，用于接收图表的标签。此参数仅适用于多目标问题，否则将被忽略。其默认值为 `None`，可接受 `list`、`tuple` 或 `numpy.ndarray`。这些标签将显示在图表内的图例中。\n6. 将 `pygad.plot` 模块中各方法的默认颜色更改为带绿色调的 `#64f20c`。\n7. 向 `pygad.GA` 实例添加了一个名为 `pareto_fronts` 的实例属性，用于存储求解多目标问题时得到的帕累托前沿。\n8. 当精度设置为 `None` 时，`gene_type` 参数现在可以接受整数数据类型的 `list`、`tuple` 或 `numpy.ndarray`（例如 `gene_type=[float, [int, None]]`）。\n9. 在 `cal_pop_fitness()` 方法中，如果设置了 `save_best_solutions=True` 且解存在于 `best_solutions` 属性中，则会重用该解的适应度值。此外，`keep_elitism`、`keep_parents` 和 `save_solutions` 等参数也可帮助避免重复调用适应度函数，从而直接重用解的适应度值。\n10. 在 `pygad.utils.parent_selection.ParentSelection` 类中的 `wheel_cumulative_probs()` 和 `stochastic_universal_selection()` 两个方法中，将数值 `99999999999` 替换为 `float('inf')`。\n11. 移除了 `pygad.visualize.plot.Plot` 类中的 `plot_result()` 方法。如果您尚未升级，请改用 `plot_fitness()`。","2023-09-08T00:47:14",{"id":174,"version":175,"summary_zh":176,"released_at":177},127765,"3.1.0","发布日期：2023年6月20日\n\n1. 修复了在使用嵌套基因空间时，初始种群中可能出现重复基因的错误。\n2. `gene_space` 参数不再允许赋值为元组。\n3. 修复了当 `gene_space` 参数包含元组类型成员时出现的错误。\n4. 新增了一个实例属性 `gene_space_unpacked`，用于存储展开后的基因空间。该属性用于解决基因重复问题。对于基因空间中的无限范围，会将其展开为有限数量的值（例如100个）。\n5. 修复了使用 `gene_space` 属性创建初始种群时的若干错误。\n6. 当 `gene_space` 属性使用字典时，新基因值原本是通过将两个值相加计算得出的：1) 从字典中采样的值；2) 根据 `random_mutation_min_val` 和 `random_mutation_max_val` 定义的随机变异范围返回的随机值。这可能导致基因值超出 `gene_space` 中定义的范围限制。为确保符合 `gene_space` 的范围限制，本版本仅返回字典中的值，不再与随机值相加。\n7. 使用 f-string 替代 `format()` 方法来格式化字符串。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fpull\u002F189\n8. 在 `pygad.GA` 类的 `__init__()` 方法中，日志记录的错误信息处理方式由重复调用 `logger.error()` 改为使用 `try-except` 块。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fpull\u002F189\n9. 在 `pygad.cnn` 模块中新增了一个名为 `CustomLogger` 的类，用于通过 `logging` 模块创建一个默认的日志记录器，并将其赋值给 `logger` 属性。该类被模块中的其他所有类继承扩展。这些类的构造函数新增了一个名为 `logger` 的参数，默认值为 `None`。如果未传入日志记录器，则使用 `CustomLogger` 类中的默认日志记录器。\n10. 除 `pygad.nn` 模块外，其他所有模块中的 `print()` 函数均被替换为 `logging` 模块，以记录日志信息。\n11. 回调函数或方法 `on_fitness()`、`on_parents()`、`on_crossover()` 和 `on_mutation()` 现在可以返回值。这些返回值将覆盖相应的属性：`on_fitness()` 的返回值将覆盖种群适应度；`on_parents()` 函数或方法必须返回两个值，分别表示父代个体及其索引；`on_crossover()` 的返回值将覆盖交叉产生的后代；`on_mutation()` 的返回值将覆盖变异产生的后代。\n12. 修复了在启用自适应变异且 `fitness_batch_size` 大于1时出现的错误。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F195\n13. 当 `allow_duplicate_genes=False` 并使用用户自定义的基因空间时，有时会出现无法仅通过用另一个基因替换某一基因的值来解决两个基因重复的情况。本版本尝试通过寻找第三个基因来帮助解决这种重复问题。具体工作原理请参见[[本节](","2023-06-20T19:32:17",{"id":179,"version":180,"summary_zh":181,"released_at":182},127766,"3.0.1","修复了在父代选择中传递用户自定义函数\u002F方法的问题。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F179","2023-04-20T13:35:43",{"id":184,"version":185,"summary_zh":186,"released_at":187},127767,"3.0.0","**此版本有一个重大变更：适应度函数现在必须接收一个参数，该参数指向 `pygad.GA` 类的实例。**\n\n以下是本次发布说明：\n\n1. 库的结构发生了变化，`pygad.py` 模块中定义的一些方法被移至 `pygad.utils`、`pygad.helper` 和 `pygad.visualize` 子模块中。\n2. `pygad.utils.parent_selection` 模块中新增了一个名为 `ParentSelection` 的类，所有父代选择算子都定义在这个类中。`pygad.GA` 类继承了该类。\n3. `pygad.utils.crossover` 模块中新增了一个名为 `Crossover` 的类，所有交叉算子都定义在这个类中。`pygad.GA` 类继承了该类。\n4. `pygad.utils.mutation` 模块中新增了一个名为 `Mutation` 的类，所有变异算子都定义在这个类中。`pygad.GA` 类继承了该类。\n5. `pygad.helper.unique` 模块中新增了一个名为 `Unique` 的类，其中包含一些用于解决基因重复问题并确保每个基因唯一的辅助方法。`pygad.GA` 类继承了该类。\n6. `pygad.visualize.plot` 模块中新增了一个名为 `Plot` 的类，所有用于创建图表的方法都定义在这个类中。`pygad.GA` 类继承了该类。\n\n```python\n...\nclass GA(utils.parent_selection.ParentSelection, \n         utils.crossover.Crossover, \n         utils.mutation.Mutation, \n         helper.unique.Unique,\n         visualize.plot.Plot):\n...\n```\n\n2. 现在支持使用 `logging` 模块将输出同时记录到控制台和文本文件中，而不再使用 `print()` 函数。只需将 `logging.Logger` 对象赋值给新的 `logger` 参数即可。更多信息请参阅[[日志输出](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#logging-outputs)](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#logging-outputs)。\n3. 新增了一个名为 `logger` 的实例属性，用于保存日志记录器。\n4. 传递给 `fitness_func` 参数的函数或方法现在必须接受一个新参数，该参数指向 `pygad.GA` 类的实例。示例请参阅[[使用函数和方法构建适应度函数与回调函数](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#use-functions-and-methods-to-build-fitness-and-callbacks)](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#use-functions-and-methods-to-build-fitness-and-callbacks)。相关议题：https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F163\n5. 更新了文档，添加了使用函数和方法计算适应度以及构建回调函数的示例。更多详情请参阅[[使用函数和方法构建适应度函数与回调函数](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#use-functions-and-methods-to-build-fitness-and-callbacks)](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#use-functions-and-methods-to-build-fitness-and-callbacks)。相关合并请求：https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fpull\u002F92#issuecomment","2023-04-08T19:19:32",{"id":189,"version":190,"summary_zh":191,"released_at":192},127768,"2.19.2","1. 修复在使用并行处理时，精英保留解的适应度值未被重用的问题。https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F160#issuecomment-1441718184","2023-02-23T13:37:57",{"id":194,"version":195,"summary_zh":196,"released_at":197},127769,"2.19.1","1. 新增 `summary()` 方法，用于返回类似 Keras 样式的 PyGAD 生命周期概览。\n2. 新增可选参数 `fitness_batch_size`，支持以批次方式计算适应度函数。若将其设置为 `1` 或 `None`（默认值），则采用正常流程，即对每个个体解分别调用适应度函数；若 `fitness_batch_size` 的值满足条件 `1 \u003C fitness_batch_size \u003C= sol_per_pop`，则将解分组为大小为 `fitness_batch_size` 的批次，并对每个批次调用一次适应度函数。此时，适应度函数必须返回一个长度等于传入解数量的列表、元组或 NumPy 数组。参见：https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F136。\n3. 使用 `cloudpickle` 库（https:\u002F\u002Fgithub.com\u002Fcloudpipe\u002Fcloudpickle）替代 `pickle` 库来序列化 `pygad.GA` 对象，从而解决需要重新定义函数（如适应度函数）的问题。同时，已将 `cloudpickle` 添加到 `requirements.txt` 文件中作为依赖项。参见：https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F159。\n4. 支持为以下参数赋值方法：`fitness_func`、`crossover_type`、`mutation_type`、`parent_selection_type`、`on_start`、`on_fitness`、`on_parents`、`on_crossover`、`on_mutation`、`on_generation` 和 `on_stop`。参见：https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fpull\u002F92 和 https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F138。\n5. 对父代选择、交叉和变异函数的输出进行验证。\n6. 内置的父代选择算子现在以 NumPy 数组的形式返回父代的索引。\n7. 父代选择、交叉和变异算子的输出必须是 NumPy 数组。\n8. 修复了当 `allow_duplicate_genes=True` 时出现的问题。参见：https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F39。\n9. 修复了绘制解的适应度散点图时出现的问题。\n10. 在 Python 3.11 中不再支持从 `set()` 中采样，现改为从 `list()` 中采样。感谢 `Marco Brenna` 指出此问题。\n11. 更新生命周期流程，使新种群的适应度在生命周期结束时而非开始时计算。参见：https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F154#issuecomment-1438739483。\n12. 当 `save_solutions=True` 时，曾出现一个问题，导致对已探索且预先计算过适应度的解再次调用适应度函数。参见：https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F160。\n13. 新增实例属性 `last_generation_elitism_indices`，用于存储被选中的精英个体的索引。该属性有助于复用精英个体的适应度值，而无需再次调用适应度函数。\n14. 减少了对 `best_solution()` 方法的调用次数，从而相应地减少了对适应度函数的调用次数。\n15. 对文档进行了一些更新，以提供更多详细信息。","2023-02-22T15:21:53",{"id":199,"version":200,"summary_zh":76,"released_at":201},127770,"2.18.3","2023-02-14T20:39:51",{"id":203,"version":204,"summary_zh":205,"released_at":206},127771,"2.18.2","PyGAD 2.18.2 release notes\r\n1. Remove `numpy.int` and `numpy.float` from the list of supported data types. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F151 https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fpull\u002F152\r\n2. Call the `on_crossover()` callback function even if `crossover_type` is `None`. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F138\r\n3. Call the `on_mutation()` callback function even if `mutation_type` is `None`. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F138","2023-02-14T19:25:36",{"id":208,"version":209,"summary_zh":210,"released_at":211},127772,"2.18.1","PyGAD 2.18.1\r\n1. A big fix when `keep_elitism` is used. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F132","2022-09-19T19:08:15",{"id":213,"version":214,"summary_zh":215,"released_at":216},127773,"2.18.0","1. Raise an exception if the sum of fitness values is zero while either roulette wheel or stochastic universal parent selection is used. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F129\r\n2. Initialize the value of the `run_completed` property to `False`. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F122\r\n3. The values of these properties are no longer reset with each call to the `run()` method `self.best_solutions, self.best_solutions_fitness, self.solutions, self.solutions_fitness`: https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F123. Now, the user can have the flexibility of calling the `run()` method more than once while extending the data collected after each generation. Another advantage happens when the instance is loaded and the `run()` method is called, as the old fitness value are shown on the graph alongside with the new fitness values. Read more in this section: [[Continue without Loosing Progress](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#continue-without-loosing-progress)](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#continue-without-loosing-progress)\r\n4. Thanks [[Prof. Fernando Jiménez Barrionuevo](http:\u002F\u002Fwebs.um.es\u002Ffernan)](http:\u002F\u002Fwebs.um.es\u002Ffernan) (Dept. of Information and Communications Engineering, University of Murcia, Murcia, Spain) for editing this [[comment](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fblob\u002F5315bbec02777df96ce1ec665c94dece81c440f4\u002Fpygad.py#L73)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fblob\u002F5315bbec02777df96ce1ec665c94dece81c440f4\u002Fpygad.py#L73) in the code. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fcommit\u002F5315bbec02777df96ce1ec665c94dece81c440f4\r\n5. A bug fixed when `crossover_type=None`.\r\n6. Support of elitism selection through a new parameter named `keep_elitism`. It defaults to 1 which means for each generation keep only the best solution in the next generation. If assigned 0, then it has no effect. Read more in this section: [[Elitism Selection](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#elitism-selection)](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#elitism-selection). https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F74\r\n7. A new instance attribute named `last_generation_elitism` added to hold the elitism in the last generation.\r\n8. A new parameter called `random_seed` added to accept a seed for the random function generators. Credit to this issue https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F70 and [[Prof. Fernando Jiménez Barrionuevo](http:\u002F\u002Fwebs.um.es\u002Ffernan)](http:\u002F\u002Fwebs.um.es\u002Ffernan). Read more in this section: [[Random Seed](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#random-seed)](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#random-seed).\r\n9. Editing the `pygad.TorchGA` module to make sure the tensor data is moved from GPU to CPU. Thanks to Rasmus Johansson for opening this pull request: https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FTorchGA\u002Fpull\u002F2","2022-09-09T05:39:53",{"id":218,"version":219,"summary_zh":220,"released_at":221},127774,"2.17.0","## PyGAD 2.17.0\r\n\r\nRelease Date: 8 July 2022\r\n\r\n1. An issue is solved when the `gene_space` parameter is given a fixed value. e.g. gene_space=[range(5), 4]. The second gene's value is static (4) which causes an exception.\r\n2. Fixed the issue where the `allow_duplicate_genes` parameter did not work when mutation is disabled (i.e. `mutation_type=None`). This is by checking for duplicates after crossover directly. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F39\r\n3. Solve an issue in the `tournament_selection()` method as the indices of the selected parents were incorrect. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F89\r\n4. Reuse the fitness values of the previously explored solutions rather than recalculating them. This feature only works if `save_solutions=True`.\r\n5. Parallel processing is supported. This is by the introduction of a new parameter named `parallel_processing` in the constructor of the `pygad.GA` class. Thanks to [[@windowshopr](https:\u002F\u002Fgithub.com\u002Fwindowshopr)](https:\u002F\u002Fgithub.com\u002Fwindowshopr) for opening the issue [[#78](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F78)](https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F78) at GitHub. Check the [[Parallel Processing in PyGAD](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#parallel-processing-in-pygad)](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#parallel-processing-in-pygad) section for more information and examples.","2022-07-08T04:10:04",{"id":223,"version":224,"summary_zh":225,"released_at":226},127775,"2.16.3","Changes in PyGAD 2.16.3\r\n1. A new instance attribute called `previous_generation_fitness` added in the `pygad.GA` class. It holds the fitness values of one generation before the fitness values saved in the `last_generation_fitness`.\r\n2. Issue in the `cal_pop_fitness()` method in getting the correct indices of the previous parents. This is solved by using the previous generation's fitness saved in the new attribute `previous_generation_fitness` to return the parents' fitness values. Thanks to Tobias Tischhauser (M.Sc. - [[Mitarbeiter Institut EMS, Departement Technik, OST – Ostschweizer Fachhochschule, Switzerland](https:\u002F\u002Fwww.ost.ch\u002Fde\u002Fforschung-und-dienstleistungen\u002Ftechnik\u002Fsystemtechnik\u002Fems\u002Fteam)](https:\u002F\u002Fwww.ost.ch\u002Fde\u002Fforschung-und-dienstleistungen\u002Ftechnik\u002Fsystemtechnik\u002Fems\u002Fteam)) for detecting this bug.\r\n3. Validate the fitness value returned from the fitness function. An exception is raised if something is wrong. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F67","2022-02-03T01:36:10",{"id":228,"version":229,"summary_zh":230,"released_at":231},127776,"2.16.1","1. Reuse the fitness of previously explored solutions rather than recalculating them. This feature only works if `save_solutions=True`.\r\n2. The user can use the `tqdm` library to show a progress bar. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fdiscussions\u002F50\r\n\r\n```python\r\nimport pygad\r\nimport numpy\r\nimport tqdm\r\n\r\nequation_inputs = [4,-2,3.5]\r\ndesired_output = 44\r\n\r\ndef fitness_func(solution, solution_idx):\r\n    output = numpy.sum(solution * equation_inputs)\r\n    fitness = 1.0 \u002F (numpy.abs(output - desired_output) + 0.000001)\r\n    return fitness\r\n\r\nnum_generations = 10000\r\nwith tqdm.tqdm(total=num_generations) as pbar:\r\n    ga_instance = pygad.GA(num_generations=num_generations,\r\n                           sol_per_pop=5,\r\n                           num_parents_mating=2,\r\n                           num_genes=len(equation_inputs),\r\n                           fitness_func=fitness_func,\r\n                           on_generation=lambda _: pbar.update(1))\r\n    \r\n    ga_instance.run()\r\n\r\nga_instance.plot_result()\r\n```\r\n\r\n3. Solved the issue of unequal length between the `solutions` and `solutions_fitness` when the `save_solutions` parameter is set to `True`. Now, the fitness of the last population is appended to the `solutions_fitness` array. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F64\r\n4. There was an issue of getting the length of these 4 variables (`solutions`, `solutions_fitness`, `best_solutions`, and `best_solutions_fitness`) doubled after each call of the `run()` method. This is solved by resetting these variables at the beginning of the `run()` method. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F62\r\n5. Bug fixes when adaptive mutation is used (`mutation_type=\"adaptive\"`). https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F65","2021-09-29T01:46:30",{"id":233,"version":234,"summary_zh":235,"released_at":236},127777,"2.16.0","A user-defined function can be passed to the mutation_type, crossover_type, and parent_selection_type parameters in the pygad.GA class to create a custom mutation, crossover, and parent selection operators. Check the User-Defined Crossover, Mutation, and Parent Selection Operators section in the documentation: https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#user-defined-crossover-mutation-and-parent-selection-operators\r\nThe example_custom_operators.py script gives an example of building and using custom functions for the 3 operators.\r\nhttps:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fdiscussions\u002F50","2021-06-20T02:23:03",{"id":238,"version":239,"summary_zh":240,"released_at":241},127778,"2.15.1","Fix a bug when `keep_parents` is set to a positive integer. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fissues\u002F49","2021-06-18T06:17:09",{"id":243,"version":244,"summary_zh":245,"released_at":246},127779,"2.15.0","1. Control the precision of all genes\u002Findividual genes. Thanks to [Rainer](https:\u002F\u002Fgithub.com\u002Frengel8) for asking about this feature: https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fdiscussions\u002F43#discussioncomment-763452\r\n2. A new attribute named `last_generation_parents_indices` holds the indices of the selected parents in the last generation.\r\n3. In adaptive mutation, no need to recalculate the fitness values of the parents selected in the last generation as these values can be returned based on the `last_generation_fitness` and `last_generation_parents_indices` attributes. This speeds-up the adaptive mutation.\r\n4. When a sublist has a value of `None` in the `gene_space` parameter (e.g. `gene_space=[[1, 2, 3], [5, 6, None]]`), then its value will be randomly generated for each solution rather than being generated once for all solutions. Previously, a value of `None` in a sublist of the `gene_space` parameter was identical across all solutions.\r\n5. The dictionary assigned to the `gene_space` parameter itself or one of its elements has a new key called `\"step\"` to specify the step of moving from the start to the end of the range specified by the 2 existing keys `\"low\"` and `\"high\"`.  An example is `{\"low\": 0, \"high\": 30, \"step\": 2}` to have only even values for the gene(s) starting from 0 to 30. For more information, check the [More about the `gene_space` Parameter](https:\u002F\u002Fpygad.readthedocs.io\u002Fen\u002Flatest\u002FREADME_pygad_ReadTheDocs.html#more-about-the-gene-space-parameter) section. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fdiscussions\u002F48\r\n6. A new function called `predict()` is added in both the `pygad.kerasga` and `pygad.torchga` modules to make predictions. This makes it easier than using custom code each time a prediction is to be made.\r\n7. A new parameter called `stop_criteria` allows the user to specify one or more stop criteria to stop the evolution based on some conditions. Each criterion is passed as `str` which has a stop word. The current 2 supported words are `reach` and `saturate`. `reach` stops the `run()` method if the fitness value is equal to or greater than a given fitness value. An example for `reach` is `\"reach_40\"` which stops the evolution if the fitness is >= 40. `saturate` means stop the evolution if the fitness saturates for a given number of consecutive generations. An example for `saturate` is `\"saturate_7\"` which means stop the `run()` method if the fitness does not change for 7 consecutive generations. Thanks to [Rainer](https:\u002F\u002Fgithub.com\u002Frengel8) for asking about this feature: https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fdiscussions\u002F44\r\n8. A new bool parameter, defaults to `False`, named `save_solutions` is added to the constructor of the `pygad.GA` class. If `True`, then all solutions in each generation are appended into an attribute called `solutions` which is NumPy array.\r\n9. The `plot_result()` method is renamed to `plot_fitness()`. The users should migrate to the new name as the old name will be removed in the future.\r\n10. Four new optional parameters are added to the `plot_fitness()` function in the `pygad.GA` class which are `font_size=14`, `save_dir=None`, `color=\"#3870FF\"`, and `plot_type=\"plot\"`. Use `font_size` to change the font of the plot title and labels. `save_dir` accepts the directory to which the figure is saved. It defaults to `None` which means do not save the figure. `color` changes the color of the plot. `plot_type` changes the plot type which can be either `\"plot\"` (default), `\"scatter\"`, or `\"bar\"`. https:\u002F\u002Fgithub.com\u002Fahmedfgad\u002FGeneticAlgorithmPython\u002Fpull\u002F47\r\n11. The default value of the `title` parameter in the `plot_fitness()` method is `\"PyGAD - Generation vs. Fitness\"` rather than `\"PyGAD - Iteration vs. Fitness\"`.\r\n12. A new method named `plot_new_solution_rate()` creates, shows, and returns a figure showing the rate of new\u002Funique solutions explored in each generation. It accepts the same parameters as in the `plot_fitness()` method. This method only works when `save_solutions=True` in the `pygad.GA` class's constructor.\r\n13. A new method named `plot_genes()` creates, shows, and returns a figure to show how each gene changes per each generation. It accepts similar parameters like the `plot_fitness()` method in addition to the `graph_type`, `fill_color`, and `solutions` parameters. The `graph_type` parameter can be either `\"plot\"` (default), `\"boxplot\"`, or `\"histogram\"`. `fill_color` accepts the fill color which works when `graph_type` is either `\"boxplot\"` or `\"histogram\"`. `solutions` can be either `\"all\"` or `\"best\"` to decide whether all solutions or only best solutions are used.\r\n14. The `gene_type` parameter now supports controlling the precision of `float` data types. For a gene, rather than assigning just the data type like `float`, assign a `list`\u002F`tuple`\u002F`numpy.ndarray` with 2 elements where the first one is the type and the second one is the precision. For example, `[float, 2]` forces a gene with a value like `0.1234` ","2021-06-18T03:20:02"]