diff --git a/pygad.egg-info/PKG-INFO b/pygad.egg-info/PKG-INFO new file mode 100644 index 0000000..b3c1478 --- /dev/null +++ b/pygad.egg-info/PKG-INFO @@ -0,0 +1,357 @@ +Metadata-Version: 2.4 +Name: pygad +Version: 3.6.0 +Summary: PyGAD: A Python Library for Building the Genetic Algorithm and Training Machine Learning Algoithms (Keras & PyTorch). +Home-page: https://github.com/ahmedfgad/GeneticAlgorithmPython +Author: Ahmed Fawzy Gad +Author-email: Ahmed Gad +Maintainer-email: Ahmed Gad +License: Copyright GeneticAlgorithmPython Contributors + + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Project-URL: Homepage, https://github.com/ahmedfgad/GeneticAlgorithmPython +Project-URL: Documentation, https://pygad.readthedocs.io +Project-URL: GitHub Repository, https://github.com/ahmedfgad/GeneticAlgorithmPython +Project-URL: PyPI Project, https://pypi.org/project/pygad +Project-URL: Conda Forge Project, https://anaconda.org/conda-forge/pygad +Project-URL: Donation Stripe, https://donate.stripe.com/eVa5kO866elKgM0144 +Project-URL: Donation Open Collective, https://opencollective.com/pygad +Project-URL: Donation Paypal, http://paypal.me/ahmedfgad +Keywords: genetic algorithm,GA,optimization,evolutionary algorithm,natural evolution,pygad,machine learning,deep learning,neural networks,tensorflow,keras,pytorch +Classifier: License :: OSI Approved :: BSD License +Classifier: Programming Language :: Python +Classifier: Programming Language :: Python :: 3 +Classifier: Natural Language :: English +Classifier: Operating System :: OS Independent +Classifier: Topic :: Scientific/Engineering +Classifier: Topic :: Scientific/Engineering :: Bio-Informatics +Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence +Classifier: Topic :: Software Development +Classifier: Topic :: Utilities +Classifier: Intended Audience :: Information Technology +Classifier: Intended Audience :: Science/Research +Classifier: Intended Audience :: Developers +Classifier: Intended Audience :: Education +Classifier: Intended Audience :: Other Audience +Requires-Python: >=3 +Description-Content-Type: text/markdown +License-File: LICENSE +Requires-Dist: numpy +Requires-Dist: matplotlib +Requires-Dist: cloudpickle +Provides-Extra: deep-learning +Requires-Dist: keras; extra == "deep-learning" +Requires-Dist: torch; extra == "deep-learning" +Dynamic: author +Dynamic: home-page +Dynamic: license-file + +# PyGAD: Genetic Algorithm in Python + +[PyGAD](https://pypi.org/project/pygad) 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. + +> Try the [Optimization Gadget](https://optimgadget.com), a free cloud-based tool powered by PyGAD. It simplifies optimization by reducing or eliminating the need for coding while providing insightful visualizations. + +Check documentation of the [PyGAD](https://pygad.readthedocs.io/en/latest). + +[![PyPI Downloads](https://pepy.tech/badge/pygad)](https://pepy.tech/project/pygad) [![Conda Downloads](https://img.shields.io/conda/dn/conda-forge/pygad.svg?label=Conda%20Downloads)]( +https://anaconda.org/conda-forge/PyGAD) [![PyPI version](https://badge.fury.io/py/pygad.svg)](https://badge.fury.io/py/pygad)![Docs](https://readthedocs.org/projects/pygad/badge)[![PyGAD PyTest / Python 3.13](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py313.yml/badge.svg)](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py313.yml) [![PyGAD PyTest / Python 3.12](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py312.yml/badge.svg)](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py312.yml) [![PyGAD PyTest / Python 3.11](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py311.yml/badge.svg)](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py311.yml) [![PyGAD PyTest / Python 3.10](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py310.yml/badge.svg)](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py310.yml) [![PyGAD PyTest / Python 3.9](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py39.yml/badge.svg)](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py39.yml) [![PyGAD PyTest / Python 3.8](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py38.yml/badge.svg)](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py38.yml) [![License](https://img.shields.io/badge/License-BSD_3--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause) [![Translation](https://hosted.weblate.org/widgets/weblate/-/svg-badge.svg)](https://hosted.weblate.org/engage/weblate/) [![REUSE](https://api.reuse.software/badge/github.com/WeblateOrg/weblate)](https://api.reuse.software/info/github.com/WeblateOrg/weblate) [![Stack Overflow](https://img.shields.io/badge/stackoverflow-Ask%20questions-blue.svg)]( +https://stackoverflow.com/questions/tagged/pygad) [![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/ahmedfgad/GeneticAlgorithmPython/badge)](https://securityscorecards.dev/viewer/?uri=github.com/ahmedfgad/GeneticAlgorithmPython) [![DOI](https://zenodo.org/badge/DOI/10.1007/s11042-023-17167-y.svg)](https://doi.org/10.1007/s11042-023-17167-y) + +![PYGAD-LOGO](https://user-images.githubusercontent.com/16560492/101267295-c74c0180-375f-11eb-9ad0-f8e37bd796ce.png) + +[PyGAD](https://pypi.org/project/pygad) supports different types of crossover, mutation, and parent selection. [PyGAD](https://pypi.org/project/pygad) allows different types of problems to be optimized using the genetic algorithm by customizing the fitness function. + +The 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. + +# Donation + +* [Credit/Debit Card](https://donate.stripe.com/eVa5kO866elKgM0144): https://donate.stripe.com/eVa5kO866elKgM0144 +* [Open Collective](https://opencollective.com/pygad): [opencollective.com/pygad](https://opencollective.com/pygad) +* PayPal: Use either this link: [paypal.me/ahmedfgad](https://paypal.me/ahmedfgad) or the e-mail address ahmed.f.gad@gmail.com +* Interac e-Transfer: Use e-mail address ahmed.f.gad@gmail.com + +# Installation + +To install [PyGAD](https://pypi.org/project/pygad), simply use pip to download and install the library from [PyPI](https://pypi.org/project/pygad) (Python Package Index). The library is at PyPI at this page https://pypi.org/project/pygad. + +Install PyGAD with the following command: + +```python +pip install pygad +``` + +To get started with PyGAD, please read the documentation at [Read The Docs](https://pygad.readthedocs.io/) https://pygad.readthedocs.io. + +# PyGAD Source Code + +The source code of the PyGAD' modules is found in the following GitHub projects: + +- [pygad](https://github.com/ahmedfgad/GeneticAlgorithmPython): (https://github.com/ahmedfgad/GeneticAlgorithmPython) +- [pygad.nn](https://github.com/ahmedfgad/NumPyANN): https://github.com/ahmedfgad/NumPyANN +- [pygad.gann](https://github.com/ahmedfgad/NeuralGenetic): https://github.com/ahmedfgad/NeuralGenetic +- [pygad.cnn](https://github.com/ahmedfgad/NumPyCNN): https://github.com/ahmedfgad/NumPyCNN +- [pygad.gacnn](https://github.com/ahmedfgad/CNNGenetic): https://github.com/ahmedfgad/CNNGenetic +- [pygad.kerasga](https://github.com/ahmedfgad/KerasGA): https://github.com/ahmedfgad/KerasGA +- [pygad.torchga](https://github.com/ahmedfgad/TorchGA): https://github.com/ahmedfgad/TorchGA + +The documentation of PyGAD is available at [Read The Docs](https://pygad.readthedocs.io/) https://pygad.readthedocs.io. + +# PyGAD Documentation + +The documentation of the PyGAD library is available at [Read The Docs](https://pygad.readthedocs.io) at this link: https://pygad.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. + +If there is an issue using PyGAD, feel free to post at issue in this [GitHub repository](https://github.com/ahmedfgad/GeneticAlgorithmPython) https://github.com/ahmedfgad/GeneticAlgorithmPython or by sending an e-mail to ahmed.f.gad@gmail.com. + +If 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. + +- Project title +- Brief description +- Preferably, a link that directs the readers to your project + +Please check the **Contact Us** section for more contact details. + +# Life Cycle of PyGAD + +The 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`. + +![PyGAD Lifecycle](https://user-images.githubusercontent.com/16560492/220486073-c5b6089d-81e4-44d9-a53c-385f479a7273.jpg) + +The next code implements all the callback functions to trace the execution of the genetic algorithm. Each callback function prints its name. + +```python +import pygad +import numpy + +function_inputs = [4,-2,3.5,5,-11,-4.7] +desired_output = 44 + +def fitness_func(ga_instance, solution, solution_idx): + output = numpy.sum(solution*function_inputs) + fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) + return fitness + +fitness_function = fitness_func + +def on_start(ga_instance): + print("on_start()") + +def on_fitness(ga_instance, population_fitness): + print("on_fitness()") + +def on_parents(ga_instance, selected_parents): + print("on_parents()") + +def on_crossover(ga_instance, offspring_crossover): + print("on_crossover()") + +def on_mutation(ga_instance, offspring_mutation): + print("on_mutation()") + +def on_generation(ga_instance): + print("on_generation()") + +def on_stop(ga_instance, last_population_fitness): + print("on_stop()") + +ga_instance = pygad.GA(num_generations=3, + num_parents_mating=5, + fitness_func=fitness_function, + sol_per_pop=10, + num_genes=len(function_inputs), + on_start=on_start, + on_fitness=on_fitness, + on_parents=on_parents, + on_crossover=on_crossover, + on_mutation=on_mutation, + on_generation=on_generation, + on_stop=on_stop) + +ga_instance.run() +``` + +Based on the used 3 generations as assigned to the `num_generations` argument, here is the output. + +``` +on_start() + +on_fitness() +on_parents() +on_crossover() +on_mutation() +on_generation() + +on_fitness() +on_parents() +on_crossover() +on_mutation() +on_generation() + +on_fitness() +on_parents() +on_crossover() +on_mutation() +on_generation() + +on_stop() +``` + +# Example + +Check the [PyGAD's documentation](https://pygad.readthedocs.io/en/latest/pygad.html) for information about the implementation of this example. It solves a single-objective problem. + +```python +import pygad +import numpy + +""" +Given the following function: + y = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6 + where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7) and y=44 +What are the best values for the 6 weights (w1 to w6)? We are going to use the genetic algorithm to optimize this function. +""" + +function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs. +desired_output = 44 # Function output. + +def fitness_func(ga_instance, solution, solution_idx): + # Calculating the fitness value of each solution in the current population. + # The fitness function calulates the sum of products between each input and its corresponding weight. + output = numpy.sum(solution*function_inputs) + fitness = 1.0 / numpy.abs(output - desired_output) + return fitness + +fitness_function = fitness_func + +num_generations = 100 # Number of generations. +num_parents_mating = 7 # Number of solutions to be selected as parents in the mating pool. + +# To prepare the initial population, there are 2 ways: +# 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. +# 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. +sol_per_pop = 50 # Number of solutions in the population. +num_genes = len(function_inputs) + +last_fitness = 0 +def callback_generation(ga_instance): + global last_fitness + print(f"Generation = {ga_instance.generations_completed}") + print(f"Fitness = {ga_instance.best_solution()[1]}") + print(f"Change = {ga_instance.best_solution()[1] - last_fitness}") + last_fitness = ga_instance.best_solution()[1] + +# Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor. +ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_function, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + on_generation=callback_generation) + +# Running the GA to optimize the parameters of the function. +ga_instance.run() + +# After the generations complete, some plots are showed that summarize the how the outputs/fitenss values evolve over generations. +ga_instance.plot_fitness() + +# Returning the details of the best solution. +solution, solution_fitness, solution_idx = ga_instance.best_solution() +print(f"Parameters of the best solution : {solution}") +print(f"Fitness value of the best solution = {solution_fitness}") +print(f"Index of the best solution : {solution_idx}") + +prediction = numpy.sum(numpy.array(function_inputs)*solution) +print(f"Predicted output based on the best solution : {prediction}") + +if ga_instance.best_solution_generation != -1: + print(f"Best fitness value reached after {ga_instance.best_solution_generation} generations.") + +# Saving the GA instance. +filename = 'genetic' # The filename to which the instance is saved. The name is without extension. +ga_instance.save(filename=filename) + +# Loading the saved GA instance. +loaded_ga_instance = pygad.load(filename=filename) +loaded_ga_instance.plot_fitness() +``` + +# For More Information + +There are different resources that can be used to get started with the genetic algorithm and building it in Python. + +## Tutorial: Implementing Genetic Algorithm in Python + +To start with coding the genetic algorithm, you can check the tutorial titled [**Genetic Algorithm Implementation in Python**](https://www.linkedin.com/pulse/genetic-algorithm-implementation-python-ahmed-gad) available at these links: + +- [LinkedIn](https://www.linkedin.com/pulse/genetic-algorithm-implementation-python-ahmed-gad) +- [Towards Data Science](https://towardsdatascience.com/genetic-algorithm-implementation-in-python-5ab67bb124a6) +- [KDnuggets](https://www.kdnuggets.com/2018/07/genetic-algorithm-implementation-python.html) + +[This tutorial](https://www.linkedin.com/pulse/genetic-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. + +[![Genetic Algorithm Implementation in Python](https://user-images.githubusercontent.com/16560492/78830052-a3c19300-79e7-11ea-8b9b-4b343ea4049c.png)](https://www.linkedin.com/pulse/genetic-algorithm-implementation-python-ahmed-gad) + +## Tutorial: Introduction to Genetic Algorithm + +Get started with the genetic algorithm by reading the tutorial titled [**Introduction to Optimization with Genetic Algorithm**](https://www.linkedin.com/pulse/introduction-optimization-genetic-algorithm-ahmed-gad) which is available at these links: + +* [LinkedIn](https://www.linkedin.com/pulse/introduction-optimization-genetic-algorithm-ahmed-gad) +* [Towards Data Science](https://towardsdatascience.com/introduction-to-optimization-with-genetic-algorithm-2f5001d9964b) +* [KDnuggets](https://www.kdnuggets.com/2018/03/introduction-optimization-with-genetic-algorithm.html) + +[![Introduction to Genetic Algorithm](https://user-images.githubusercontent.com/16560492/82078259-26252d00-96e1-11ea-9a02-52a99e1054b9.jpg)](https://www.linkedin.com/pulse/introduction-optimization-genetic-algorithm-ahmed-gad) + +## Tutorial: Optimize Neural Networks with Genetic Algorithm + +Read about training neural networks using the genetic algorithm through the tutorial titled [**Artificial Neural Networks Optimization using Genetic Algorithm with Python**](https://www.linkedin.com/pulse/artificial-neural-networks-optimization-using-genetic-ahmed-gad) available at these links: + +- [LinkedIn](https://www.linkedin.com/pulse/artificial-neural-networks-optimization-using-genetic-ahmed-gad) +- [Towards Data Science](https://towardsdatascience.com/artificial-neural-networks-optimization-using-genetic-algorithm-with-python-1fe8ed17733e) +- [KDnuggets](https://www.kdnuggets.com/2019/03/artificial-neural-networks-optimization-genetic-algorithm-python.html) + +[![Training Neural Networks using Genetic Algorithm Python](https://user-images.githubusercontent.com/16560492/82078300-376e3980-96e1-11ea-821c-aa6b8ceb44d4.jpg)](https://www.linkedin.com/pulse/artificial-neural-networks-optimization-using-genetic-ahmed-gad) + +## Book: Practical Computer Vision Applications Using Deep Learning with CNNs + +You 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://www.amazon.com/Practical-Computer-Vision-Applications-Learning/dp/1484241665) which discusses neural networks, convolutional neural networks, deep learning, genetic algorithm, and more. + +Find the book at these links: + +- [Amazon](https://www.amazon.com/Practical-Computer-Vision-Applications-Learning/dp/1484241665) +- [Springer](https://link.springer.com/book/10.1007/978-1-4842-4167-7) +- [Apress](https://www.apress.com/gp/book/9781484241660) +- [O'Reilly](https://www.oreilly.com/library/view/practical-computer-vision/9781484241677) +- [Google Books](https://books.google.com.eg/books?id=xLd9DwAAQBAJ) + +![Fig04](https://user-images.githubusercontent.com/16560492/78830077-ae7c2800-79e7-11ea-980b-53b6bd879eeb.jpg) + +# Citing PyGAD - Bibtex Formatted Citation + +If you used PyGAD, please consider adding a citation to the following paper about PyGAD: + +``` +@article{gad2023pygad, + title={Pygad: An intuitive genetic algorithm python library}, + author={Gad, Ahmed Fawzy}, + journal={Multimedia Tools and Applications}, + pages={1--14}, + year={2023}, + publisher={Springer} +} +``` + +# Contact Us + +* E-mail: ahmed.f.gad@gmail.com +* [LinkedIn](https://www.linkedin.com/in/ahmedfgad) +* [Paperspace](https://blog.paperspace.com/author/ahmed) +* [KDnuggets](https://kdnuggets.com/author/ahmed-gad) +* [TowardsDataScience](https://towardsdatascience.com/@ahmedfgad) +* [GitHub](https://github.com/ahmedfgad) diff --git a/pygad.egg-info/SOURCES.txt b/pygad.egg-info/SOURCES.txt new file mode 100644 index 0000000..61e3426 --- /dev/null +++ b/pygad.egg-info/SOURCES.txt @@ -0,0 +1,53 @@ +LICENSE +README.md +pyproject.toml +setup.py +pygad/__init__.py +pygad/pygad.py +pygad.egg-info/PKG-INFO +pygad.egg-info/SOURCES.txt +pygad.egg-info/dependency_links.txt +pygad.egg-info/requires.txt +pygad.egg-info/top_level.txt +pygad/cnn/__init__.py +pygad/cnn/cnn.py +pygad/gacnn/__init__.py +pygad/gacnn/gacnn.py +pygad/gann/__init__.py +pygad/gann/gann.py +pygad/helper/__init__.py +pygad/helper/misc.py +pygad/helper/unique.py +pygad/kerasga/__init__.py +pygad/kerasga/kerasga.py +pygad/nn/__init__.py +pygad/nn/nn.py +pygad/torchga/__init__.py +pygad/torchga/torchga.py +pygad/utils/__init__.py +pygad/utils/crossover.py +pygad/utils/engine.py +pygad/utils/mutation.py +pygad/utils/nsga2.py +pygad/utils/parent_selection.py +pygad/utils/validation.py +pygad/visualize/__init__.py +pygad/visualize/plot.py +tests/test_adaptive_mutation.py +tests/test_allow_duplicate_genes.py +tests/test_best_solution.py +tests/test_constrained_nsga2.py +tests/test_crossover_mutation.py +tests/test_gann.py +tests/test_gene_constraint.py +tests/test_gene_space.py +tests/test_gene_space_allow_duplicate_genes.py +tests/test_gene_type.py +tests/test_lifecycle_callbacks_calls.py +tests/test_number_fitness_function_calls.py +tests/test_operators.py +tests/test_parallel.py +tests/test_save_solutions.py +tests/test_stop_criteria.py +tests/test_summary.py +tests/test_visualize.py \ No newline at end of file diff --git a/pygad.egg-info/dependency_links.txt b/pygad.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/pygad.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/pygad.egg-info/requires.txt b/pygad.egg-info/requires.txt new file mode 100644 index 0000000..c5556ef --- /dev/null +++ b/pygad.egg-info/requires.txt @@ -0,0 +1,7 @@ +numpy +matplotlib +cloudpickle + +[deep_learning] +keras +torch diff --git a/pygad.egg-info/top_level.txt b/pygad.egg-info/top_level.txt new file mode 100644 index 0000000..6659799 --- /dev/null +++ b/pygad.egg-info/top_level.txt @@ -0,0 +1 @@ +pygad diff --git a/pygad/utils/nsga2.py b/pygad/utils/nsga2.py index 99b2762..1bf1778 100644 --- a/pygad/utils/nsga2.py +++ b/pygad/utils/nsga2.py @@ -75,18 +75,28 @@ def get_non_dominated_set(self, curr_solutions, constraint_violations=None): return dominated_set, non_dominated_set - def _get_constraint_violations(self): + def _get_constraint_violations(self, solutions=None): """ Helper method to get constraint violations if constraint_func is defined. + Parameters + ---------- + solutions : numpy.ndarray, optional + The solutions to calculate constraint violations for. + If None, uses self.population. + Returns ------- constraint_violations : numpy.ndarray or None Total constraint violations for each solution, or None if no constraint_func. """ if hasattr(self, 'constraint_func') and self.constraint_func is not None: - if hasattr(self, 'population') and self.population is not None: - return self.calculate_constraint_violations(self.constraint_func, self.population)[0] + if solutions is None: + if hasattr(self, 'population') and self.population is not None: + solutions = self.population + else: + return None + return self.calculate_constraint_violations(self.constraint_func, solutions)[0] return None def non_dominated_sorting(self, fitness, constraint_violations=None): @@ -257,8 +267,27 @@ def sort_solutions_nsga2(self, crowding_dist_pop_sorted_indices = list(crowding_dist_pop_sorted_indices) solutions_sorted.extend(crowding_dist_pop_sorted_indices) elif type(fitness[0]) in pygad.GA.supported_int_float_types: - solutions_sorted = sorted(range(len(fitness)), key=lambda k: fitness[k]) - solutions_sorted.reverse() + if constraint_violations is None: + constraint_violations = self._get_constraint_violations() + + if constraint_violations is not None: + # Constraint domination for single-objective: + # 1. Feasible solutions (cv <= 0) always come before infeasible ones + # 2. Between feasible solutions: higher fitness is better + # 3. Between infeasible solutions: smaller constraint violation is better + # Sort key: (is_infeasible, secondary_key) + # - is_infeasible: 0 for feasible, 1 for infeasible + # - secondary_key: for feasible, -fitness (higher first); for infeasible, cv (smaller first) + solutions_sorted = sorted( + range(len(fitness)), + key=lambda k: ( + constraint_violations[k] > 0, # 0 for feasible (first), 1 for infeasible (second) + -fitness[k] if constraint_violations[k] <= 0 else constraint_violations[k] + ) + ) + else: + solutions_sorted = sorted(range(len(fitness)), key=lambda k: fitness[k]) + solutions_sorted.reverse() else: raise TypeError(f'Each element in the fitness array must be of a number of an iterable (list, tuple, numpy.ndarray). But the type {type(fitness[0])} found') diff --git a/tests/test_constrained_nsga2.py b/tests/test_constrained_nsga2.py index 9364cec..6eeaaf5 100644 --- a/tests/test_constrained_nsga2.py +++ b/tests/test_constrained_nsga2.py @@ -208,6 +208,121 @@ def test_feasible_pareto_domination(): return True +def test_single_objective_constraint_domination(): + """ + Test constraint domination for single-objective optimization. + + For single-objective: + 1. Feasible solutions always come before infeasible ones. + 2. Between feasible solutions: higher fitness is better. + 3. Between infeasible solutions: smaller constraint violation is better. + """ + print("\n" + "="*60) + print("Testing: Single-Objective Constraint Domination") + print("="*60) + + nsga2 = pygad.utils.nsga2.NSGA2() + nsga2.supported_int_float_types = [int, float, numpy.float64, numpy.int64] + + # Test case: + # Solution 0: feasible (cv=0), fitness=10 (best feasible) + # Solution 1: feasible (cv=0), fitness=5 (worse feasible) + # Solution 2: infeasible (cv=1), fitness=100 (good fitness but violates constraint + # Solution 3: infeasible (cv=5), fitness=1000 (best fitness but worst violation) + + fitness = numpy.array([10.0, 5.0, 100.0, 1000.0]) + constraint_violations = numpy.array([0.0, 0.0, 1.0, 5.0]) + + # Test: sort_solutions_nsga2 with single-objective fitness + sorted_indices = nsga2.sort_solutions_nsga2( + fitness, + constraint_violations=constraint_violations + ) + + print(f"\nFitness values:") + for i in range(len(fitness)): + status = "feasible" if constraint_violations[i] <= 0 else "infeasible" + print(f" Solution {i}: fitness={fitness[i]}, violation={constraint_violations[i]} ({status})") + + print(f"\nExpected order: [0, 1, 2, 3]") + print(f" - Solutions 0,1 (feasible) come before 2,3 (infeasible)") + print(f" - Between feasible: 0 (fitness=10) > 1 (fitness=5)") + print(f" - Between infeasible: 2 (cv=1) < 3 (cv=5)") + print(f"\nActual sorted indices: {sorted_indices}") + + # Assertions + feasible_indices = [i for i in sorted_indices if constraint_violations[i] <= 0] + infeasible_indices = [i for i in sorted_indices if constraint_violations[i] > 0] + + print(f"\nFeasible in sorted order: {feasible_indices}") + print(f"Infeasible in sorted order: {infeasible_indices}") + + # All feasible should come before infeasible + all_feasible_first = all( + sorted_indices.index(f) < sorted_indices.index(inf) + for f in feasible_indices + for inf in infeasible_indices + ) + assert all_feasible_first, "All feasible solutions should come before infeasible ones" + print("\nāœ“ All feasible solutions come before infeasible ones") + + # Between feasible: higher fitness first + if len(feasible_indices) >= 2: + assert fitness[feasible_indices[0]] >= fitness[feasible_indices[1]], \ + "Between feasible solutions, higher fitness should come first" + print("āœ“ Between feasible solutions, higher fitness comes first") + + # Between infeasible: smaller violation first + if len(infeasible_indices) >= 2: + assert constraint_violations[infeasible_indices[0]] <= constraint_violations[infeasible_indices[1]], \ + "Between infeasible solutions, smaller violation should come first" + print("āœ“ Between infeasible solutions, smaller violation comes first") + + return True + + +def test_get_constraint_violations_with_solutions(): + """ + Test that _get_constraint_violations accepts solutions parameter. + """ + print("\n" + "="*60) + print("Testing: _get_constraint_violations with solutions parameter") + print("="*60) + + nsga2 = pygad.utils.nsga2.NSGA2() + nsga2.supported_int_float_types = [int, float, numpy.float64, numpy.int64] + + # Define constraint_func: sum of genes should be <= 0 + def constraint_func(solution, solution_idx): + return [numpy.sum(solution)] + + nsga2.constraint_func = constraint_func + + # Test with custom solutions + custom_solutions = numpy.array([ + [-1.0, -2.0, -3.0], # sum = -6 (feasible) + [1.0, 2.0, 3.0], # sum = 6 (infeasible) + [0.0, 0.0, 0.0], # sum = 0 (feasible) + ]) + + # Without solutions parameter (should use self.population, but it's None) + result_none = nsga2._get_constraint_violations() + print(f"\nWithout solutions parameter (self.population is None): {result_none}") + assert result_none is None, "Should return None when no solutions available" + + # With solutions parameter + result_with_solutions = nsga2._get_constraint_violations(solutions=custom_solutions) + print(f"With solutions parameter: {result_with_solutions}") + + expected = numpy.array([0.0, 6.0, 0.0]) # Only positive violations are summed + print(f"Expected: {expected}") + + numpy.testing.assert_array_almost_equal(result_with_solutions, expected) + print("\nāœ“ _get_constraint_violations correctly uses provided solutions") + + return True + + def test_backward_compatibility(): """ Test that NSGA-II works without constraint_func (backward compatibility). @@ -262,6 +377,8 @@ def fitness_func(ga_instance, solution, solution_idx): test_feasible_vs_infeasible_domination() test_infeasible_vs_infeasible_domination() test_feasible_pareto_domination() + test_single_objective_constraint_domination() + test_get_constraint_violations_with_solutions() print("\n" + "="*60) print("Running full GA test with constraints...")