diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..464ffbf6 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,72 @@ +# PyGAD GitHub Actions Workflow +# This workflow uses a Matrix Strategy to test PyGAD across multiple Python versions in parallel. +name: PyGAD PyTest Matrix + +permissions: + contents: read + +on: + push: + # Trigger the workflow on pushes to the 'github-actions' branch. + branches: + - github-actions + # - master + # Only run the workflow if changes are made in these directories. + paths: + - 'pygad/**' + - 'tests/**' + - 'examples/**' + # Allows manual triggering of the workflow from the GitHub Actions tab. + workflow_dispatch: + +jobs: + pytest: + # Use the latest Ubuntu runner for all jobs. + runs-on: ubuntu-latest + + # The Strategy Matrix defines the environments to test. + # GitHub Actions will spawn a separate, parallel job for each version in the list. + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14"] + + # Dynamic name that appears in the GitHub Actions UI for each parallel job. + name: PyTest / Python ${{ matrix.python-version }} + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + # Setup the specific Python version defined in the current matrix iteration. + - name: Setup Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + + # Install project dependencies from requirements.txt. + - name: Install Dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + # Build the PyGAD package distribution (generating .tar.gz and .whl files). + # This ensures the package build process is valid on this Python version. + - name: Build PyGAD + run: | + python3 -m pip install --upgrade build + python3 -m build + + # Install the newly built .whl file to verify the package is installable. + - name: Install PyGAD from Wheel + run: | + find ./dist/*.whl | xargs pip install + + - name: Install PyTest + run: pip install pytest + + # Run the entire test suite using PyTest. + # PyTest automatically discovers all test files in the 'tests/' directory. + # This includes our new tests for visualization, operators, parallel processing, etc. + - name: Run Tests + run: | + pytest diff --git a/.github/workflows/main_py310.yml b/.github/workflows/main_py310.yml deleted file mode 100644 index 9e701d15..00000000 --- a/.github/workflows/main_py310.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: PyGAD PyTest / Python 3.10 - -on: - push: - branches: - - github-actions - # - master - # Manually trigger the workflow. - workflow_dispatch: - -jobs: - job_id_1: - runs-on: ubuntu-latest - name: PyTest Workflow Job - - steps: - - name: Checkout Pre-Built Action - uses: actions/checkout@v3 - - - name: Setup Python 3.10 - uses: actions/setup-python@v4 - with: - python-version: '3.10' - - - name: Install Dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - - - name: Build PyGAD from the Repository - run: | - python3 -m pip install --upgrade build - python3 -m build - - - name: Install PyGAD after Building the .whl File - run: | - find ./dist/*.whl | xargs pip install - - - name: Install PyTest - run: pip install pytest - - - name: Run the Tests by Calling PyTest - run: | - pytest diff --git a/.github/workflows/main_py311.yml b/.github/workflows/main_py311.yml deleted file mode 100644 index 0ca059bd..00000000 --- a/.github/workflows/main_py311.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: PyGAD PyTest / Python 3.11 - -on: - push: - branches: - - github-actions - # - master - # Manually trigger the workflow. - workflow_dispatch: - -jobs: - job_id_1: - runs-on: ubuntu-latest - name: PyTest Workflow Job - - steps: - - name: Checkout Pre-Built Action - uses: actions/checkout@v3 - - - name: Setup Python 3.11 - uses: actions/setup-python@v4 - with: - python-version: '3.11' - - - name: Install Dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - - - name: Build PyGAD from the Repository - run: | - python3 -m pip install --upgrade build - python3 -m build - - - name: Install PyGAD after Building the .whl File - run: | - find ./dist/*.whl | xargs pip install - - - name: Install PyTest - run: pip install pytest - - - name: Run the Tests by Calling PyTest - run: | - pytest diff --git a/.github/workflows/main_py312.yml b/.github/workflows/main_py312.yml deleted file mode 100644 index 91cc504e..00000000 --- a/.github/workflows/main_py312.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: PyGAD PyTest / Python 3.12 - -on: - push: - branches: - - github-actions - # - master - # Manually trigger the workflow. - workflow_dispatch: - -jobs: - job_id_1: - runs-on: ubuntu-latest - name: PyTest Workflow Job - - steps: - - name: Checkout Pre-Built Action - uses: actions/checkout@v3 - - - name: Setup Python 3.12 - uses: actions/setup-python@v4 - with: - python-version: '3.12' - - - name: Install Dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - - - name: Build PyGAD from the Repository - run: | - python3 -m pip install --upgrade build - python3 -m build - - - name: Install PyGAD after Building the .whl File - run: | - find ./dist/*.whl | xargs pip install - - - name: Install PyTest - run: pip install pytest - - - name: Run the Tests by Calling PyTest - run: | - pytest diff --git a/.github/workflows/main_py313.yml b/.github/workflows/main_py313.yml deleted file mode 100644 index 0e0301cc..00000000 --- a/.github/workflows/main_py313.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: PyGAD PyTest / Python 3.13 - -on: - push: - branches: - - github-actions - # - master - # Manually trigger the workflow. - workflow_dispatch: - -jobs: - job_id_1: - runs-on: ubuntu-latest - name: PyTest Workflow Job - - steps: - - name: Checkout Pre-Built Action - uses: actions/checkout@v3 - - - name: Setup Python 3.13 - uses: actions/setup-python@v4 - with: - python-version: '3.13' - - - name: Install Dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - - - name: Build PyGAD from the Repository - run: | - python3 -m pip install --upgrade build - python3 -m build - - - name: Install PyGAD after Building the .whl File - run: | - find ./dist/*.whl | xargs pip install - - - name: Install PyTest - run: pip install pytest - - - name: Run the Tests by Calling PyTest - run: | - pytest diff --git a/.github/workflows/main_py38.yml b/.github/workflows/main_py38.yml deleted file mode 100644 index 7838c645..00000000 --- a/.github/workflows/main_py38.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: PyGAD PyTest / Python 3.8 - -on: - push: - branches: - - github-actions - # - master - # Manually trigger the workflow. - workflow_dispatch: - -jobs: - job_id_1: - runs-on: ubuntu-latest - name: PyTest Workflow Job - - steps: - - name: Checkout Pre-Built Action - uses: actions/checkout@v3 - - - name: Setup Python 3.8 - uses: actions/setup-python@v4 - with: - python-version: '3.8' - - - name: Install Dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - - - name: Build PyGAD from the Repository - run: | - python3 -m pip install --upgrade build - python3 -m build - - - name: Install PyGAD after Building the .whl File - run: | - find ./dist/*.whl | xargs pip install - - - name: Install PyTest - run: pip install pytest - - - name: Run the Tests by Calling PyTest - run: | - pytest diff --git a/.github/workflows/main_py39.yml b/.github/workflows/main_py39.yml deleted file mode 100644 index be32ab48..00000000 --- a/.github/workflows/main_py39.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: PyGAD PyTest / Python 3.9 - -on: - push: - branches: - - github-actions - # - master - # Manually trigger the workflow. - workflow_dispatch: - -jobs: - job_id_1: - runs-on: ubuntu-latest - name: PyTest Workflow Job - - steps: - - name: Checkout Pre-Built Action - uses: actions/checkout@v3 - - - name: Setup Python 3.9 - uses: actions/setup-python@v4 - with: - python-version: '3.9' - - - name: Install Dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - - - name: Build PyGAD from the Repository - run: | - python3 -m pip install --upgrade build - python3 -m build - - - name: Install PyGAD after Building the .whl File - run: | - find ./dist/*.whl | xargs pip install - - - name: Install PyTest - run: pip install pytest - - - name: Run the Tests by Calling PyTest - run: | - pytest diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml index 30d789a6..24fca762 100644 --- a/.github/workflows/scorecard.yml +++ b/.github/workflows/scorecard.yml @@ -3,7 +3,11 @@ # policy, and support documentation. name: Scorecard supply-chain security + on: + # This allows you to run the workflow manually from the Actions tab + workflow_dispatch: + # For Branch-Protection check. Only the default branch is supported. See # https://github.com/ossf/scorecard/blob/main/docs/checks.md#branch-protection branch_protection_rule: @@ -32,12 +36,12 @@ jobs: steps: - name: "Checkout code" - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + uses: actions/checkout@v4 # v4.1.1 with: persist-credentials: false - name: "Run analysis" - uses: ossf/scorecard-action@0864cf19026789058feabb7e87baa5f140aac736 # v2.3.1 + uses: ossf/scorecard-action@v2.3.1 # v2.3.1 with: results_file: results.sarif results_format: sarif @@ -59,9 +63,9 @@ jobs: # Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF # format to the repository Actions tab. - name: "Upload artifact" - uses: actions/upload-artifact@97a0fba1372883ab732affbe8f94b823f91727db # v3.pre.node20 + uses: actions/upload-artifact@v4 # v3.pre.node20 with: - name: SARIF file + name: SARIF-file path: results.sarif retention-days: 5 diff --git a/docs/md/helper.md b/docs/md/helper.md index 39e67fe0..a2a393af 100644 --- a/docs/md/helper.md +++ b/docs/md/helper.md @@ -38,4 +38,5 @@ The `pygad.helper.misc` module has a class called `Helper` with some methods to 10. `generate_gene_value()`: Generates a value for the gene. It checks whether `gene_space` is `None` and calls either `generate_gene_value_randomly()` or `generate_gene_value_from_space()`. 11. `filter_gene_values_by_constraint()`: Receives a list of values for a gene. Then it filters such values using the gene constraint. 12. `get_valid_gene_constraint_values()`: Selects one valid gene value that satisfy the gene constraint. It simply calls `generate_gene_value()` to generate some gene values then it filters such values using `filter_gene_values_by_constraint()`. +13. `initialize_parents_array()`: Usually called from the methods in the `ParentSelection` class in the `pygad/utils/parent_selection.py` script to initialize the parents array. diff --git a/docs/md/pygad.md b/docs/md/pygad.md index 501916c8..32bb4847 100644 --- a/docs/md/pygad.md +++ b/docs/md/pygad.md @@ -62,13 +62,34 @@ It is OK to set the value of any of the 2 parameters `init_range_low` and `init_ If the 2 parameters `mutation_type` and `crossover_type` are `None`, this disables any type of evolution the genetic algorithm can make. As a result, the genetic algorithm cannot find a better solution that the best solution in the initial population. -The parameters are validated within the constructor. If at least a parameter is not correct, an exception is thrown. - -## Plotting Methods in `pygad.GA` Class - -- `plot_fitness()`: Shows how the fitness evolves by generation. -- `plot_genes()`: Shows how the gene value changes for each generation. -- `plot_new_solution_rate()`: Shows the number of new solutions explored in each solution. +The parameters are validated by calling the `validate_parameters()` method of the `utils.validation.Validation` class within the constructor. If at least a parameter is not correct, an exception is thrown and the `valid_parameters` attribute is set to `False`. + +# Extended Classes + +To make the library modular and structured, different scripts are created where each script has one or more classes. Each class has its own objective. + +This is the list of scripts and classes within them where the `pygad.GA` class extends: + +1. `utils/engine.py`: + 1. `utils.engine.GAEngine`: +2. `utils/validation.py` + 1. `utils.validation.Validation` +3. `utils/parent_selection.py` + 1. `utils.parent_selection.ParentSelection` +4. `utils/crossover.py` + 1. `utils.crossover.Crossover` +5. `utils/mutation.py` + 1. `utils.mutation.Mutation` +6. `utils/nsga2.py` + 1. `utils.nsga2.NSGA2` +7. `helper/unique.py` + 1. `helper.unique.Unique` +8. `helper/misc.py` + 1. `helper.misc.Helper` +9. `visualize/plot.py` + 1. `visualize.plot.Plot` + +Since the `pygad.GA` class extends such classes, the attributes and methods inside them can be retrieved by instances of the `pygad.GA` class. ## Class Attributes @@ -82,10 +103,12 @@ All the parameters and functions passed to the `pygad.GA` class constructor are The next 2 subsections list such attributes and methods. +> The `GA` class gains the attributes of its parent classes via inheritance, making them accessible through the `GA` object even if they are defined externally to its specific class body. + ### Other Attributes - `generations_completed`: Holds the number of the last completed generation. -- `population`: A NumPy array holding the initial population. +- `population`: A NumPy array that initially holds the initial population and is later updated after each generation. - `valid_parameters`: Set to `True` when all the parameters passed in the `GA` class constructor are valid. - `run_completed`: Set to `True` only after the `run()` method completes gracefully. - `pop_size`: The population size. @@ -93,7 +116,7 @@ The next 2 subsections list such attributes and methods. - `best_solution_generation`: The generation number at which the best fitness value is reached. It is only assigned the generation number after the `run()` method completes. Otherwise, its value is -1. - `best_solutions`: A NumPy array holding the best solution per each generation. It only exists when the `save_best_solutions` parameter in the `pygad.GA` class constructor is set to `True`. - `last_generation_fitness`: The fitness values of the solutions in the last generation. [Added in PyGAD 2.12.0](https://pygad.readthedocs.io/en/latest/releases.html#pygad-2-12-0). -- `previous_generation_fitness`: At the end of each generation, the fitness of the most recent population is saved in the `last_generation_fitness` attribute. The fitness of the population exactly preceding this most recent population is saved in the `last_generation_fitness` attribute. This `previous_generation_fitness` attribute is used to fetch the pre-calculated fitness instead of calling the fitness function for already explored solutions. [Added in PyGAD 2.16.2](https://pygad.readthedocs.io/en/latest/releases.html#pygad-2-16-2). +- `previous_generation_fitness`: At the end of each generation, the fitness of the most recent population is saved in the `last_generation_fitness` attribute. The fitness of the population exactly preceding this most recent population is saved in the `previous_generation_fitness` attribute. This `previous_generation_fitness` attribute is used to fetch the pre-calculated fitness instead of calling the fitness function for already explored solutions. [Added in PyGAD 2.16.2](https://pygad.readthedocs.io/en/latest/releases.html#pygad-2-16-2). - `last_generation_parents`: The parents selected from the last generation. [Added in PyGAD 2.12.0](https://pygad.readthedocs.io/en/latest/releases.html#pygad-2-12-0). - `last_generation_offspring_crossover`: The offspring generated after applying the crossover in the last generation. [Added in PyGAD 2.12.0](https://pygad.readthedocs.io/en/latest/releases.html#pygad-2-12-0). - `last_generation_offspring_mutation`: The offspring generated after applying the mutation in the last generation. [Added in PyGAD 2.12.0](https://pygad.readthedocs.io/en/latest/releases.html#pygad-2-12-0). @@ -115,215 +138,20 @@ Note that the attributes with names starting with `last_generation_` are updated - `select_parents()`: Refers to a method that selects the parents based on the parent selection type specified in the `parent_selection_type` attribute. - `adaptive_mutation_population_fitness()`: Returns the average fitness value used in the adaptive mutation to filter the solutions. - `summary()`: Prints a Keras-like summary of the PyGAD lifecycle. This helps to have an overview of the architecture. Supported in [PyGAD 2.19.0](https://pygad.readthedocs.io/en/latest/releases.html#pygad-2-19-0). Check the [Print Lifecycle Summary](https://pygad.readthedocs.io/en/latest/pygad_more.html#print-lifecycle-summary) section for more details and examples. -- 4 methods with names starting with `run_`. Their purpose is to keep the main loop inside the `run()` method clean. The details inside the loop are moved to 4 individual methods. Generally, any method with a name starting with `run_` is meant to be called by PyGAD from inside the `run()` method. Supported in [PyGAD 3.3.1](https://pygad.readthedocs.io/en/latest/releases.html#pygad-3-3-1). - 1. `run_select_parents(call_on_parents=True)`: Select the parents and call the callable `on_parents()` if defined. If `call_on_parents` is `True`, then the callable `on_parents()` is called. It must be `False` when the `run_select_parents()` method is called to update the parents at the end of the `run()` method. - 2. `run_crossover()`: Apply crossover and call the callable `on_crossover()` if defined. - 3. `run_mutation()`: Apply mutation and call the callable `on_mutation()` if defined. - 4. `run_update_population()`: Update the `population` attribute after completing the processes of crossover and mutation. +- 5 methods with names starting with `run_`. Their purpose is to keep the main loop inside the `run()` method clean. The details inside the loop are moved to 4 individual methods. Generally, any method with a name starting with `run_` is meant to be called by PyGAD from inside the `run()` method. Supported in [PyGAD 3.3.1](https://pygad.readthedocs.io/en/latest/releases.html#pygad-3-3-1). + 1. `run_loop_head()`: The code before the loop starts. + 2. `run_select_parents(call_on_parents=True)`: Select the parents and call the callable `on_parents()` if defined. If `call_on_parents` is `True`, then the callable `on_parents()` is called. It must be `False` when the `run_select_parents()` method is called to update the parents at the end of the `run()` method. + 3. `run_crossover()`: Apply crossover and call the callable `on_crossover()` if defined. + 4. `run_mutation()`: Apply mutation and call the callable `on_mutation()` if defined. + 5. `run_update_population()`: Update the `population` attribute after completing the processes of crossover and mutation. There are many methods that are not designed for user usage. Some of them are listed above but this is not a comprehensive list. The [release history](https://pygad.readthedocs.io/en/latest/releases.html) section usually covers them. Moreover, you can check the [PyGAD GitHub repository](https://github.com/ahmedfgad/GeneticAlgorithmPython) to find more. The next sections discuss the methods available in the `pygad.GA` class. -## `initialize_population()` - -It creates an initial population randomly as a NumPy array. The array is saved in the instance attribute named `population`. - -Accepts the following parameters: - -- `low`: The lower value of the random range from which the gene values in the initial population are selected. It defaults to -4. Available in PyGAD 1.0.20 and higher. -- `high`: The upper value of the random range from which the gene values in the initial population are selected. It defaults to -4. Available in PyGAD 1.0.20. - -This method assigns the values of the following 3 instance attributes: - -1. `pop_size`: Size of the population. -2. `population`: Initially, it holds the initial population and later updated after each generation. -3. `initial_population`: Keeping the initial population. - -## `cal_pop_fitness()` - -The `cal_pop_fitness()` method calculates and returns the fitness values of the solutions in the current population. - -This function is optimized to save time by making fewer calls the fitness function. It follows this process: - -1. If the `save_solutions` parameter is set to `True`, then it checks if the solution is already explored and saved in the `solutions` instance attribute. If so, then it just retrieves its fitness from the `solutions_fitness` instance attribute without calling the fitness function. -2. If `save_solutions` is set to `False` or if it is `True` but the solution was not explored yet, then the `cal_pop_fitness()` method checks if the `keep_elitism` parameter is set to a positive integer. If so, then it checks if the solution is saved into the `last_generation_elitism` instance attribute. If so, then it retrieves its fitness from the `previous_generation_fitness` instance attribute. -3. If neither of the above 3 conditions apply (1. `save_solutions` is set to `False` or 2. if it is `True` but the solution was not explored yet or 3. `keep_elitism` is set to zero), then the `cal_pop_fitness()` method checks if the `keep_parents` parameter is set to `-1` or a positive integer. If so, then it checks if the solution is saved into the `last_generation_parents` instance attribute. If so, then it retrieves its fitness from the `previous_generation_fitness` instance attribute. -4. If neither of the above 4 conditions apply, then we have to call the fitness function to calculate the fitness for the solution. This is by calling the function assigned to the `fitness_func` parameter. - -This function takes into consideration: - -1. The `parallel_processing` parameter to check whether parallel processing is in effect. -2. The `fitness_batch_size` parameter to check if the fitness should be calculated in batches of solutions. - -It returns a vector of the solutions' fitness values. - -## `run()` - -Runs the genetic algorithm. This is the main method in which the genetic algorithm is evolved through some generations. It accepts no parameters as it uses the instance to access all of its requirements. - -For each generation, the fitness values of all solutions within the population are calculated according to the `cal_pop_fitness()` method which internally just calls the function assigned to the `fitness_func` parameter in the `pygad.GA` class constructor for each solution. - -According to the fitness values of all solutions, the parents are selected using the `select_parents()` method. This method behaviour is determined according to the parent selection type in the `parent_selection_type` parameter in the `pygad.GA` class constructor - -Based on the selected parents, offspring are generated by applying the crossover and mutation operations using the `crossover()` and `mutation()` methods. The behaviour of such 2 methods is defined according to the `crossover_type` and `mutation_type` parameters in the `pygad.GA` class constructor. - -After the generation completes, the following takes place: - -- The `population` attribute is updated by the new population. -- The `generations_completed` attribute is assigned by the number of the last completed generation. -- If there is a callback function assigned to the `on_generation` attribute, then it will be called. - -After the `run()` method completes, the following takes place: - -- The `best_solution_generation` is assigned the generation number at which the best fitness value is reached. -- The `run_completed` attribute is set to `True`. - -## Parent Selection Methods - -The `ParentSelection` class in the `pygad.utils.parent_selection` module has several methods for selecting the parents that will mate to produce the offspring. All of such methods accept the same parameters which are: - -* `fitness`: The fitness values of the solutions in the current population. -* `num_parents`: The number of parents to be selected. - -All of such methods return an array of the selected parents. - -The next subsections list the supported methods for parent selection. - -### `steady_state_selection()` - -Selects the parents using the steady-state selection technique. - -### `rank_selection()` - -Selects the parents using the rank selection technique. - -### `random_selection()` - -Selects the parents randomly. - -### `tournament_selection()` - -Selects the parents using the tournament selection technique. - -### `roulette_wheel_selection()` - -Selects the parents using the roulette wheel selection technique. - -### `stochastic_universal_selection()` - -Selects the parents using the stochastic universal selection technique. - -### `nsga2_selection()` - -Selects the parents for the NSGA-II algorithm to solve multi-objective optimization problems. It selects the parents by ranking them based on non-dominated sorting and crowding distance. - -### `tournament_selection_nsga2()` - -Selects the parents for the NSGA-II algorithm to solve multi-objective optimization problems. It selects the parents using the tournament selection technique applied based on non-dominated sorting and crowding distance. - -## Crossover Methods - -The `Crossover` class in the `pygad.utils.crossover` module supports several methods for applying crossover between the selected parents. All of these methods accept the same parameters which are: - -* `parents`: The parents to mate for producing the offspring. -* `offspring_size`: The size of the offspring to produce. - -All of such methods return an array of the produced offspring. - -The next subsections list the supported methods for crossover. - -### `single_point_crossover()` - -Applies the single-point crossover. It selects a point randomly at which crossover takes place between the pairs of parents. - -### `two_points_crossover()` - -Applies the 2 points crossover. It selects the 2 points randomly at which crossover takes place between the pairs of parents. - -### `uniform_crossover()` - -Applies the uniform crossover. For each gene, a parent out of the 2 mating parents is selected randomly and the gene is copied from it. - -### `scattered_crossover()` - -Applies the scattered crossover. It randomly selects the gene from one of the 2 parents. - -## Mutation Methods - -The `Mutation` class in the `pygad.utils.mutation` module supports several methods for applying mutation. All of these methods accept the same parameter which is: - -* `offspring`: The offspring to mutate. - -All of such methods return an array of the mutated offspring. - -The next subsections list the supported methods for mutation. - -### `random_mutation()` - -Applies the random mutation which changes the values of some genes randomly. The number of genes is specified according to either the `mutation_num_genes` or the `mutation_percent_genes` attributes. - -For each gene, a random value is selected according to the range specified by the 2 attributes `random_mutation_min_val` and `random_mutation_max_val`. The random value is added to the selected gene. - -### `swap_mutation()` - -Applies the swap mutation which interchanges the values of 2 randomly selected genes. - -### `inversion_mutation()` - -Applies the inversion mutation which selects a subset of genes and inverts them. - -### `scramble_mutation()` - -Applies the scramble mutation which selects a subset of genes and shuffles their order randomly. - -### `adaptive_mutation()` - -Applies the adaptive mutation which selects the number/percentage of genes to mutate based on the solution's fitness. If the fitness is high (i.e. solution quality is high), then small number/percentage of genes is mutated compared to a solution with a low fitness. - -## `best_solution()` - -Returns information about the best solution found by the genetic algorithm. - -It accepts the following parameters: - -* `pop_fitness=None`: An optional parameter that accepts a list of the fitness values of the solutions in the population. If `None`, then the `cal_pop_fitness()` method is called to calculate the fitness values of the population. - -It returns the following: - -* `best_solution`: Best solution in the current population. - -* `best_solution_fitness`: Fitness value of the best solution. - -* `best_match_idx`: Index of the best solution in the current population. - -## `plot_fitness()` - -Previously named `plot_result()`, this method creates, shows, and returns a figure that summarizes how the fitness value evolves by generation. - -It works only after completing at least 1 generation. If no generation is completed (at least 1), an exception is raised. - -## `plot_new_solution_rate()` - -The `plot_new_solution_rate()` method creates, shows, and returns a figure that shows the number of new solutions explored in each generation. This method works only when `save_solutions=True` in the constructor of the `pygad.GA` class. - -It works only after completing at least 1 generation. If no generation is completed (at least 1), an exception is raised. - -## `plot_genes()` - -The `plot_genes()` method creates, shows, and returns a figure that describes each gene. It has different options to create the figures which helps to: - -1. Explore the gene value for each generation by creating a normal plot. -2. Create a histogram for each gene. -3. Create a boxplot. - -This is controlled by the `graph_type` parameter. - -It works only after completing at least 1 generation. If no generation is completed (at least 1), an exception is raised. - ## `save()` -Saves the genetic algorithm instance +The `save()` method in the `pygad.GA` class saves the genetic algorithm instance as a pickled object. Accepts the following parameter: @@ -863,13 +691,13 @@ import functools import operator def img2chromosome(img_arr): - return numpy.reshape(a=img_arr, newshape=(functools.reduce(operator.mul, img_arr.shape))) + return numpy.reshape(img_arr, (functools.reduce(operator.mul, img_arr.shape))) def chromosome2img(vector, shape): if len(vector) != functools.reduce(operator.mul, shape): raise ValueError(f"A vector of length {len(vector)} into an array of shape {shape}.") - return numpy.reshape(a=vector, newshape=shape) + return numpy.reshape(vector, shape) ``` ### Create an Instance of the `pygad.GA` Class diff --git a/docs/md/releases.md b/docs/md/releases.md index 9fce35fe..7b0f12a3 100644 --- a/docs/md/releases.md +++ b/docs/md/releases.md @@ -552,7 +552,13 @@ Release Date 29 January 2024 Release Date 17 February 2024 1. After the last generation and before the `run()` method completes, update the 2 instance attributes: 1) `last_generation_parents` 2) `last_generation_parents_indices`. This is to keep the list of parents up-to-date with the latest population fitness `last_generation_fitness`. https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/275 -2. 4 methods with names starting with `run_`. Their purpose is to keep the main loop inside the `run()` method clean. Check the [Other Methods](https://pygad.readthedocs.io/en/latest/pygad.html#other-methods) section for more information. +2. 5 methods with names starting with `run_`. Their purpose is to keep the main loop inside the `run()` method clean. Check the [Other Methods](https://pygad.readthedocs.io/en/latest/pygad.html#other-methods) section for more information. + 1. `run_loop_head()`: The code before the loop starts. + 2. `run_select_parents()`: The parent selection-related code. + 3. `run_crossover()`: The crossover-related code. + 4. `run_mutation()`: The mutation-related code. + 5. `run_update_population()`: Update the `population` instance attribute after completing the processes of crossover and mutation. + ## PyGAD 3.4.0 @@ -608,6 +614,48 @@ Release Date 08 July 2025 17. Fixed a bug while applying the non-dominated sorting in the `get_non_dominated_set()` method inside the `pygad/utils/nsga2.py` script. It was swapping the non-dominated and dominated sets. In other words, it used the non-dominated set as if it is the dominated set and vice versa. All the calls to this method were edited accordingly. https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/320. 18. Fix a bug retrieving in the `best_solution()` method when retrieving the best solution for multi-objective problems. https://github.com/ahmedfgad/GeneticAlgorithmPython/pull/331 +## PyGAD 3.6.0 + +1. Support passing a class to the fitness, crossover, and mutation. https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/342 +2. A new class called `Validation` is created in the new `pygad/utils/validation.py` script. It has a method called `validate_parameters()` to validate all the parameters passed while instantiating the `pygad.GA` class. +3. Refactoring the `pygad.py` script by moving a lot of functions and methods to other classes in other scripts. + 4. The `summary()` method was moved to `Helper` class in the `pygad/helper/misc.py` script. + 5. The validation code in the `__init__()` method of the `pygad.GA` class is moved to the new `validate_parameters()` method in the new `Validation` class in the new `pygad/utils/validation.py` script. Moreover, the `validate_multi_stop_criteria()` method is also moved to the same class. + 6. The GA main workflow is moved into the new `GAEngine` class in the new `pygad/utils/engine.py` script. Specifically, these methods are moved from the `pygad.GA` class to the new `GAEngine` class: + 1. `run()` + 1. `run_loop_head()` + 2. `run_select_parents()` + 3. `run_crossover()` + 4. `run_mutation()` + 5. `run_update_population()` + 2. `initialize_population()` + 3. `cal_pop_fitness()` + 4. `best_solution()` + 5. `round_genes()` +7. The `pygad.GA` class now extends the two new classes `utils.validation.Validation` and `utils.engine.GAEngine`. +8. The version of the `pygad.utils` submodule is upgraded from `1.3.0` to `1.4.0`. +9. The version of the `pygad.helper` submodule is upgraded from `1.2.0` to `1.3.0`. +10. The version of the `pygad.visualize` submodule is upgraded from `1.1.0` to `1.1.1`. +11. The version of the `pygad.nn` submodule is upgraded from `1.2.1` to `1.2.2`. +12. The version of the `pygad.cnn` submodule is upgraded from `1.1.0` to `1.1.1`. +13. The version of the `pygad.torchga` submodule is upgraded from `1.4.0` to `1.4.1`. +14. The version of the `pygad.kerasga` submodule is upgraded from `1.3.0` to `1.3.1`. +15. Update the elitism after the evolution ends to fix issue where the best solution returned by the `best_solution()` method is not correct. https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/337 +16. Fix a bug in calling the `numpy.reshape()` function. The parameter `newshape` is removed since it is no longer supported started from NumPy `2.4.0`. https://numpy.org/doc/stable/release/2.4.0-notes.html#removed-newshape-parameter-from-numpy-reshape +17. A minor change in the documentation is made to replace the `newshape` parameter when calling `numpy.reshape()`. +18. Fix a bug in the `visualize/plot.py` script that causes a warning to be given when the plot leged is used with single-objective problems. +19. A new method called `initialize_parents_array()` is added to the `Helper` class in the `pygad/helper/misc.py` script. It is usually called from the methods in the `ParentSelection` class in the `pygad/utils/parent_selection.py` script to initialize the parents array. +20. Add more tests about: + 1. Operators (crossover, mutation, and parent selection). + 2. The `best_solution()` method. + 3. Parallel processing. + 4. The `GANN` module. + 5. The plots created by the `visualize`. + +21. Instead of using repeated code for converting the data type and rounding the genes during crossover and mutation, the `change_gene_dtype_and_round()` method is called from the `pygad.helper.misc.Helper` class. +22. Fix some documentation issues. https://github.com/ahmedfgad/GeneticAlgorithmPython/pull/336 +23. Update the documentation to reflect the recent additions and changes to the library structure. + # PyGAD Projects at GitHub The PyGAD library is available at PyPI at this page https://pypi.org/project/pygad. PyGAD is built out of a number of open-source GitHub projects. A brief note about these projects is given in the next subsections. diff --git a/docs/md/utils.md b/docs/md/utils.md index bf6873f0..b80d1d6c 100644 --- a/docs/md/utils.md +++ b/docs/md/utils.md @@ -6,15 +6,121 @@ PyGAD supports different types of operators for selecting the parents, applying The submodules in the `pygad.utils` module are: -1. `crossover`: Has the `Crossover` class that implements the crossover operators. -2. `mutation`: Has the `Mutation` class that implements the mutation operators. -3. `parent_selection`: Has the `ParentSelection` class that implements the parent selection operators. -4. `nsga2`: Has the `NSGA2` class that implements the Non-Dominated Sorting Genetic Algorithm II (NSGA-II). +1. `engine`: The core engine of the library. It has the `GAEngine` class implementing the main loop and related functions. +2. `crossover`: Has the `Crossover` class that implements the crossover operators. +3. `mutation`: Has the `Mutation` class that implements the mutation operators. +4. `parent_selection`: Has the `ParentSelection` class that implements the parent selection operators. +5. `nsga2`: Has the `NSGA2` class that implements the Non-Dominated Sorting Genetic Algorithm II (NSGA-II). Note that the `pygad.GA` class extends all of these classes. So, the user can access any of the methods in such classes directly by the instance/object of the `pygad.GA` class. The next sections discuss each submodule. +# `pygad.utils.engine` Submodule + +The `pygad.utils.engine` module has the `GAEngine` class that implements the engine of the library. The methods in this class are: + +1. `initialize_population()` +2. `cal_pop_fitness()` +3. `run()` + 1. `run_loop_head()` + 2. `run_select_parents()` + 3. `run_crossover()` + 4. `run_mutation()` + 5. `run_update_population()` +4. `best_solution()` +5. `round_genes()` + +## `initialize_population()` + +It creates an initial population randomly as a NumPy array. The array is saved in the instance attribute named `population`. + +Accepts the following parameters: + +- `low`: The lower value of the random range from which the gene values in the initial population are selected. It defaults to -4. Available in PyGAD 1.0.20 and higher. +- `high`: The upper value of the random range from which the gene values in the initial population are selected. It defaults to -4. Available in PyGAD 1.0.20. + +This method assigns the values of the following 3 instance attributes: + +1. `pop_size`: Size of the population. +2. `population`: Initially, it holds the initial population and later updated after each generation. +3. `initial_population`: Keeping the initial population. + +## `cal_pop_fitness()` + +The `cal_pop_fitness()` method calculates and returns the fitness values of the solutions in the current population. + +This function is optimized to save time by making fewer calls the fitness function. It follows this process: + +1. If the `save_solutions` parameter is set to `True`, then it checks if the solution is already explored and saved in the `solutions` instance attribute. If so, then it just retrieves its fitness from the `solutions_fitness` instance attribute without calling the fitness function. +2. If `save_solutions` is set to `False` or if it is `True` but the solution was not explored yet, then the `cal_pop_fitness()` method checks if the `keep_elitism` parameter is set to a positive integer. If so, then it checks if the solution is saved into the `last_generation_elitism` instance attribute. If so, then it retrieves its fitness from the `previous_generation_fitness` instance attribute. +3. If neither of the above 3 conditions apply (1. `save_solutions` is set to `False` or 2. if it is `True` but the solution was not explored yet or 3. `keep_elitism` is set to zero), then the `cal_pop_fitness()` method checks if the `keep_parents` parameter is set to `-1` or a positive integer. If so, then it checks if the solution is saved into the `last_generation_parents` instance attribute. If so, then it retrieves its fitness from the `previous_generation_fitness` instance attribute. +4. If neither of the above 4 conditions apply, then we have to call the fitness function to calculate the fitness for the solution. This is by calling the function assigned to the `fitness_func` parameter. + +This function takes into consideration: + +1. The `parallel_processing` parameter to check whether parallel processing is in effect. +2. The `fitness_batch_size` parameter to check if the fitness should be calculated in batches of solutions. + +It returns a vector of the solutions' fitness values. + +## `run()` + +Runs the genetic algorithm. This is the main method in which the genetic algorithm is evolved through some generations. It accepts no parameters as it uses the instance to access all of its requirements. + +For each generation, the fitness values of all solutions within the population are calculated according to the `cal_pop_fitness()` method which internally just calls the function assigned to the `fitness_func` parameter in the `pygad.GA` class constructor for each solution. + +According to the fitness values of all solutions, the parents are selected using the `select_parents()` method. This method behavior is determined according to the parent selection type in the `parent_selection_type` parameter in the `pygad.GA` class constructor + +Based on the selected parents, offspring are generated by applying the crossover and mutation operations using the `crossover()` and `mutation()` methods. The behavior of such 2 methods is defined according to the `crossover_type` and `mutation_type` parameters in the `pygad.GA` class constructor. + +After the generation completes, the following takes place: + +- The `population` attribute is updated by the new population. +- The `generations_completed` attribute is assigned by the number of the last completed generation. +- If there is a callback function assigned to the `on_generation` attribute, then it will be called. + +After the `run()` method completes, the following takes place: + +- The `best_solution_generation` is assigned the generation number at which the best fitness value is reached. +- The `run_completed` attribute is set to `True`. + +Note that the `run()` method is calling 5 different methods during the loop: + +1. `run_loop_head()` +2. `run_select_parents()` +3. `run_crossover()` +4. `run_mutation()` +5. `run_update_population()` + +## `best_solution()` + +Returns information about the best solution found by the genetic algorithm. + +It accepts the following parameters: + +* `pop_fitness=None`: An optional parameter that accepts a list of the fitness values of the solutions in the population. If `None`, then the `cal_pop_fitness()` method is called to calculate the fitness values of the population. + +It returns the following: + +* `best_solution`: Best solution in the current population. + +* `best_solution_fitness`: Fitness value of the best solution. + +* `best_match_idx`: Index of the best solution in the current population. + +## `round_genes()` + +A method to round the genes in the passed solutions. It loops through each gene across all the passed solutions and rounds their values if applicable. + +# `pygad.utils.validation` Submodule + +The `pygad.utils.validation` module has the `Validation` class that validates the arguments passed while instantiating the `pygad.GA` class. The methods in this class are: + +1. `validate_parameters()`: A method that accepts the same list of arguments accepted by the constructor of the `pygad.GA` class. It validates all the parameters. If everything is validated, the instance attribute `valid_parameters` will be set to `True`. Otherwise, it will be `False` and an exception is raised indicating the invalid criteria. + +An inner method called `validate_multi_stop_criteria()` exists to validate the `stop_criteria` argument. + # `pygad.utils.crossover` Submodule The `pygad.utils.crossover` module has a class named `Crossover` with the supported crossover operations which are: @@ -29,6 +135,33 @@ All crossover methods accept this parameter: 1. `parents`: The parents to mate for producing the offspring. 2. `offspring_size`: The size of the offspring to produce. +## Crossover Methods + +The `Crossover` class in the `pygad.utils.crossover` module supports several methods for applying crossover between the selected parents. All of these methods accept the same parameters which are: + +* `parents`: The parents to mate for producing the offspring. +* `offspring_size`: The size of the offspring to produce. + +All of such methods return an array of the produced offspring. + +The next subsections list the supported methods for crossover. + +### `single_point_crossover()` + +Applies the single-point crossover. It selects a point randomly at which crossover takes place between the pairs of parents. + +### `two_points_crossover()` + +Applies the 2 points crossover. It selects the 2 points randomly at which crossover takes place between the pairs of parents. + +### `uniform_crossover()` + +Applies the uniform crossover. For each gene, a parent out of the 2 mating parents is selected randomly and the gene is copied from it. + +### `scattered_crossover()` + +Applies the scattered crossover. It randomly selects the gene from one of the 2 parents. + # `pygad.utils.mutation` Submodule The `pygad.utils.mutation` module has a class named `Mutation` with the supported mutation operations which are: @@ -43,6 +176,40 @@ All mutation methods accept this parameter: 1. `offspring`: The offspring to mutate. +## Mutation Methods + +The `Mutation` class in the `pygad.utils.mutation` module supports several methods for applying mutation. All of these methods accept the same parameter which is: + +* `offspring`: The offspring to mutate. + +All of such methods return an array of the mutated offspring. + +The next subsections list the supported methods for mutation. + +### `random_mutation()` + +Applies the random mutation which changes the values of some genes randomly. The number of genes is specified according to either the `mutation_num_genes` or the `mutation_percent_genes` attributes. + +For each gene, a random value is selected according to the range specified by the 2 attributes `random_mutation_min_val` and `random_mutation_max_val`. The random value is added to the selected gene. + +### `swap_mutation()` + +Applies the swap mutation which interchanges the values of 2 randomly selected genes. + +### `inversion_mutation()` + +Applies the inversion mutation which selects a subset of genes and inverts them. + +### `scramble_mutation()` + +Applies the scramble mutation which selects a subset of genes and shuffles their order randomly. + +### `adaptive_mutation()` + +Applies the adaptive mutation which selects the number/percentage of genes to mutate based on the solution's fitness. If the fitness is high (i.e. solution quality is high), then small number/percentage of genes is mutated compared to a solution with a low fitness. + +## Mutation Helper Methods + The `pygad.utils.mutation` module has some helper methods to assist applying the mutation operation: 1. `mutation_by_space()`: Applies the mutation using the `gene_space` parameter. @@ -185,6 +352,49 @@ It has the following helper methods: 1. `wheel_cumulative_probs()`: A helper function to calculate the wheel probabilities for these 2 methods: 1) `roulette_wheel_selection()` 2) `rank_selection()` +## Parent Selection Methods + +The `ParentSelection` class in the `pygad.utils.parent_selection` module has several methods for selecting the parents that will mate to produce the offspring. All of such methods accept the same parameters which are: + +* `fitness`: The fitness values of the solutions in the current population. +* `num_parents`: The number of parents to be selected. + +All of such methods return an array of the selected parents. + +The next subsections list the supported methods for parent selection. + +### `steady_state_selection()` + +Selects the parents using the steady-state selection technique. + +### `rank_selection()` + +Selects the parents using the rank selection technique. + +### `random_selection()` + +Selects the parents randomly. + +### `tournament_selection()` + +Selects the parents using the tournament selection technique. + +### `roulette_wheel_selection()` + +Selects the parents using the roulette wheel selection technique. + +### `stochastic_universal_selection()` + +Selects the parents using the stochastic universal selection technique. + +### `nsga2_selection()` + +Selects the parents for the NSGA-II algorithm to solve multi-objective optimization problems. It selects the parents by ranking them based on non-dominated sorting and crowding distance. + +### `tournament_selection_nsga2()` + +Selects the parents for the NSGA-II algorithm to solve multi-objective optimization problems. It selects the parents using the tournament selection technique applied based on non-dominated sorting and crowding distance. + # `pygad.utils.nsga2` Submodule The `pygad.utils.nsga2` module has a class named `NSGA2` that implements NSGA-II. The methods inside this class are: diff --git a/docs/md/visualize.md b/docs/md/visualize.md index 74b0934c..82ee74d6 100644 --- a/docs/md/visualize.md +++ b/docs/md/visualize.md @@ -4,9 +4,9 @@ This section of the PyGAD's library documentation discusses the **pygad.visualiz This section discusses the different options to visualize the results in PyGAD through these methods: -1. `plot_fitness()`: Creates plots for the fitness. -2. `plot_genes()`: Creates plots for the genes. -3. `plot_new_solution_rate()`: Creates plots for the new solution rate. +1. `plot_fitness()`: Creates plots for the fitness to show how the fitness evolves by generation. . +2. `plot_genes()`: Creates plots for the genes to show how the gene value changes for each generation. +3. `plot_new_solution_rate()`: Creates plots for the new solution rate to show how the number of new solutions explored in each solution. 4. `plot_pareto_front_curve()`: Creates plots for the pareto front for multi-objective problems. In the following code, the `save_solutions` flag is set to `True` which means all solutions are saved in the `solutions` attribute. The code runs for only 10 generations. @@ -41,7 +41,7 @@ Let's explore how to visualize the results by the above mentioned methods. ## `plot_fitness()` -The `plot_fitness()` method shows the fitness value for each generation. It creates, shows, and returns a figure that summarizes how the fitness value(s) evolve(s) by generation. +The `plot_fitness()` method shows the fitness value for each generation. It creates, shows, and returns a figure that summarizes how the fitness value(s) evolve(s) by generation. It was previously named `plot_result()`. It works only after completing at least 1 generation. If no generation is completed (at least 1), an exception is raised. diff --git a/docs/source/conf.py b/docs/source/conf.py index 30554786..1dabc364 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -18,11 +18,11 @@ # -- Project information ----------------------------------------------------- project = 'PyGAD' -copyright = '2025, Ahmed Fawzy Gad' +copyright = '2026, Ahmed Fawzy Gad' author = 'Ahmed Fawzy Gad' # The full version, including alpha/beta/rc tags -release = '3.5.0' +release = '3.6.0' master_doc = 'index' diff --git a/docs/source/pygad.rst b/docs/source/pygad.rst index 7160c2a7..a3906ec1 100644 --- a/docs/source/pygad.rst +++ b/docs/source/pygad.rst @@ -450,21 +450,60 @@ If the 2 parameters ``mutation_type`` and ``crossover_type`` are make. As a result, the genetic algorithm cannot find a better solution that the best solution in the initial population. -The parameters are validated within the constructor. If at least a -parameter is not correct, an exception is thrown. +The parameters are validated by calling the ``validate_parameters()`` +method of the ``utils.validation.Validation`` class within the +constructor. If at least a parameter is not correct, an exception is +thrown and the ``valid_parameters`` attribute is set to ``False``. -.. _plotting-methods-in-pygadga-class: +Extended Classes +================ -Plotting Methods in ``pygad.GA`` Class --------------------------------------- +To make the library modular and structured, different scripts are +created where each script has one or more classes. Each class has its +own objective. -- ``plot_fitness()``: Shows how the fitness evolves by generation. +This is the list of scripts and classes within them where the +``pygad.GA`` class extends: -- ``plot_genes()``: Shows how the gene value changes for each - generation. +1. ``utils/engine.py``: + + 1. ``utils.engine.GAEngine``: + +2. ``utils/validation.py`` + + 1. ``utils.validation.Validation`` + +3. ``utils/parent_selection.py`` + + 1. ``utils.parent_selection.ParentSelection`` + +4. ``utils/crossover.py`` + + 1. ``utils.crossover.Crossover`` + +5. ``utils/mutation.py`` + + 1. ``utils.mutation.Mutation`` -- ``plot_new_solution_rate()``: Shows the number of new solutions - explored in each solution. +6. ``utils/nsga2.py`` + + 1. ``utils.nsga2.NSGA2`` + +7. ``helper/unique.py`` + + 1. ``helper.unique.Unique`` + +8. ``helper/misc.py`` + + 1. ``helper.misc.Helper`` + +9. ``visualize/plot.py`` + + 1. ``visualize.plot.Plot`` + +Since the ``pygad.GA`` class extends such classes, the attributes and +methods inside them can be retrieved by instances of the ``pygad.GA`` +class. Class Attributes ---------------- @@ -490,13 +529,18 @@ attributes and methods added to the instances of the ``pygad.GA`` class: The next 2 subsections list such attributes and methods. + The ``GA`` class gains the attributes of its parent classes via + inheritance, making them accessible through the ``GA`` object even if + they are defined externally to its specific class body. + Other Attributes ~~~~~~~~~~~~~~~~ - ``generations_completed``: Holds the number of the last completed generation. -- ``population``: A NumPy array holding the initial population. +- ``population``: A NumPy array that initially holds the initial + population and is later updated after each generation. - ``valid_parameters``: Set to ``True`` when all the parameters passed in the ``GA`` class constructor are valid. @@ -525,7 +569,7 @@ Other Attributes fitness of the most recent population is saved in the ``last_generation_fitness`` attribute. The fitness of the population exactly preceding this most recent population is saved in the - ``last_generation_fitness`` attribute. This + ``previous_generation_fitness`` attribute. This ``previous_generation_fitness`` attribute is used to fetch the pre-calculated fitness instead of calling the fitness function for already explored solutions. `Added in PyGAD @@ -613,27 +657,29 @@ Other Methods Summary `__ section for more details and examples. -- 4 methods with names starting with ``run_``. Their purpose is to keep +- 5 methods with names starting with ``run_``. Their purpose is to keep the main loop inside the ``run()`` method clean. The details inside the loop are moved to 4 individual methods. Generally, any method with a name starting with ``run_`` is meant to be called by PyGAD from inside the ``run()`` method. Supported in `PyGAD 3.3.1 `__. - 1. ``run_select_parents(call_on_parents=True)``: Select the parents + 1. ``run_loop_head()``: The code before the loop starts. + + 2. ``run_select_parents(call_on_parents=True)``: Select the parents and call the callable ``on_parents()`` if defined. If ``call_on_parents`` is ``True``, then the callable ``on_parents()`` is called. It must be ``False`` when the ``run_select_parents()`` method is called to update the parents at the end of the ``run()`` method. - 2. ``run_crossover()``: Apply crossover and call the callable + 3. ``run_crossover()``: Apply crossover and call the callable ``on_crossover()`` if defined. - 3. ``run_mutation()``: Apply mutation and call the callable + 4. ``run_mutation()``: Apply mutation and call the callable ``on_mutation()`` if defined. - 4. ``run_update_population()``: Update the ``population`` attribute + 5. ``run_update_population()``: Update the ``population`` attribute after completing the processes of crossover and mutation. There are many methods that are not designed for user usage. Some of @@ -646,382 +692,11 @@ find more. The next sections discuss the methods available in the ``pygad.GA`` class. -.. _initializepopulation: - -``initialize_population()`` ---------------------------- - -It creates an initial population randomly as a NumPy array. The array is -saved in the instance attribute named ``population``. - -Accepts the following parameters: - -- ``low``: The lower value of the random range from which the gene - values in the initial population are selected. It defaults to -4. - Available in PyGAD 1.0.20 and higher. - -- ``high``: The upper value of the random range from which the gene - values in the initial population are selected. It defaults to -4. - Available in PyGAD 1.0.20. - -This method assigns the values of the following 3 instance attributes: - -1. ``pop_size``: Size of the population. - -2. ``population``: Initially, it holds the initial population and later - updated after each generation. - -3. ``initial_population``: Keeping the initial population. - -.. _calpopfitness: - -``cal_pop_fitness()`` ---------------------- - -The ``cal_pop_fitness()`` method calculates and returns the fitness -values of the solutions in the current population. - -This function is optimized to save time by making fewer calls the -fitness function. It follows this process: - -1. If the ``save_solutions`` parameter is set to ``True``, then it - checks if the solution is already explored and saved in the - ``solutions`` instance attribute. If so, then it just retrieves its - fitness from the ``solutions_fitness`` instance attribute without - calling the fitness function. - -2. If ``save_solutions`` is set to ``False`` or if it is ``True`` but - the solution was not explored yet, then the ``cal_pop_fitness()`` - method checks if the ``keep_elitism`` parameter is set to a positive - integer. If so, then it checks if the solution is saved into the - ``last_generation_elitism`` instance attribute. If so, then it - retrieves its fitness from the ``previous_generation_fitness`` - instance attribute. - -3. If neither of the above 3 conditions apply (1. ``save_solutions`` is - set to ``False`` or 2. if it is ``True`` but the solution was not - explored yet or 3. ``keep_elitism`` is set to zero), then the - ``cal_pop_fitness()`` method checks if the ``keep_parents`` parameter - is set to ``-1`` or a positive integer. If so, then it checks if the - solution is saved into the ``last_generation_parents`` instance - attribute. If so, then it retrieves its fitness from the - ``previous_generation_fitness`` instance attribute. - -4. If neither of the above 4 conditions apply, then we have to call the - fitness function to calculate the fitness for the solution. This is - by calling the function assigned to the ``fitness_func`` parameter. - -This function takes into consideration: - -1. The ``parallel_processing`` parameter to check whether parallel - processing is in effect. - -2. The ``fitness_batch_size`` parameter to check if the fitness should - be calculated in batches of solutions. - -It returns a vector of the solutions' fitness values. - -``run()`` ---------- - -Runs the genetic algorithm. This is the main method in which the genetic -algorithm is evolved through some generations. It accepts no parameters -as it uses the instance to access all of its requirements. - -For each generation, the fitness values of all solutions within the -population are calculated according to the ``cal_pop_fitness()`` method -which internally just calls the function assigned to the -``fitness_func`` parameter in the ``pygad.GA`` class constructor for -each solution. - -According to the fitness values of all solutions, the parents are -selected using the ``select_parents()`` method. This method behaviour is -determined according to the parent selection type in the -``parent_selection_type`` parameter in the ``pygad.GA`` class -constructor - -Based on the selected parents, offspring are generated by applying the -crossover and mutation operations using the ``crossover()`` and -``mutation()`` methods. The behaviour of such 2 methods is defined -according to the ``crossover_type`` and ``mutation_type`` parameters in -the ``pygad.GA`` class constructor. - -After the generation completes, the following takes place: - -- The ``population`` attribute is updated by the new population. - -- The ``generations_completed`` attribute is assigned by the number of - the last completed generation. - -- If there is a callback function assigned to the ``on_generation`` - attribute, then it will be called. - -After the ``run()`` method completes, the following takes place: - -- The ``best_solution_generation`` is assigned the generation number at - which the best fitness value is reached. - -- The ``run_completed`` attribute is set to ``True``. - -Parent Selection Methods ------------------------- - -The ``ParentSelection`` class in the ``pygad.utils.parent_selection`` -module has several methods for selecting the parents that will mate to -produce the offspring. All of such methods accept the same parameters -which are: - -- ``fitness``: The fitness values of the solutions in the current - population. - -- ``num_parents``: The number of parents to be selected. - -All of such methods return an array of the selected parents. - -The next subsections list the supported methods for parent selection. - -.. _steadystateselection: - -``steady_state_selection()`` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Selects the parents using the steady-state selection technique. - -.. _rankselection: - -``rank_selection()`` -~~~~~~~~~~~~~~~~~~~~ - -Selects the parents using the rank selection technique. - -.. _randomselection: - -``random_selection()`` -~~~~~~~~~~~~~~~~~~~~~~ - -Selects the parents randomly. - -.. _tournamentselection: - -``tournament_selection()`` -~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Selects the parents using the tournament selection technique. - -.. _roulettewheelselection: - -``roulette_wheel_selection()`` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Selects the parents using the roulette wheel selection technique. - -.. _stochasticuniversalselection: - -``stochastic_universal_selection()`` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Selects the parents using the stochastic universal selection technique. - -.. _nsga2selection: - -``nsga2_selection()`` -~~~~~~~~~~~~~~~~~~~~~ - -Selects the parents for the NSGA-II algorithm to solve multi-objective -optimization problems. It selects the parents by ranking them based on -non-dominated sorting and crowding distance. - -.. _tournamentselectionnsga2: - -``tournament_selection_nsga2()`` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Selects the parents for the NSGA-II algorithm to solve multi-objective -optimization problems. It selects the parents using the tournament -selection technique applied based on non-dominated sorting and crowding -distance. - -Crossover Methods ------------------ - -The ``Crossover`` class in the ``pygad.utils.crossover`` module supports -several methods for applying crossover between the selected parents. All -of these methods accept the same parameters which are: - -- ``parents``: The parents to mate for producing the offspring. - -- ``offspring_size``: The size of the offspring to produce. - -All of such methods return an array of the produced offspring. - -The next subsections list the supported methods for crossover. - -.. _singlepointcrossover: - -``single_point_crossover()`` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Applies the single-point crossover. It selects a point randomly at which -crossover takes place between the pairs of parents. - -.. _twopointscrossover: - -``two_points_crossover()`` -~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Applies the 2 points crossover. It selects the 2 points randomly at -which crossover takes place between the pairs of parents. - -.. _uniformcrossover: - -``uniform_crossover()`` -~~~~~~~~~~~~~~~~~~~~~~~ - -Applies the uniform crossover. For each gene, a parent out of the 2 -mating parents is selected randomly and the gene is copied from it. - -.. _scatteredcrossover: - -``scattered_crossover()`` -~~~~~~~~~~~~~~~~~~~~~~~~~ - -Applies the scattered crossover. It randomly selects the gene from one -of the 2 parents. - -Mutation Methods ----------------- - -The ``Mutation`` class in the ``pygad.utils.mutation`` module supports -several methods for applying mutation. All of these methods accept the -same parameter which is: - -- ``offspring``: The offspring to mutate. - -All of such methods return an array of the mutated offspring. - -The next subsections list the supported methods for mutation. - -.. _randommutation: - -``random_mutation()`` -~~~~~~~~~~~~~~~~~~~~~ - -Applies the random mutation which changes the values of some genes -randomly. The number of genes is specified according to either the -``mutation_num_genes`` or the ``mutation_percent_genes`` attributes. - -For each gene, a random value is selected according to the range -specified by the 2 attributes ``random_mutation_min_val`` and -``random_mutation_max_val``. The random value is added to the selected -gene. - -.. _swapmutation: - -``swap_mutation()`` -~~~~~~~~~~~~~~~~~~~ - -Applies the swap mutation which interchanges the values of 2 randomly -selected genes. - -.. _inversionmutation: - -``inversion_mutation()`` -~~~~~~~~~~~~~~~~~~~~~~~~ - -Applies the inversion mutation which selects a subset of genes and -inverts them. - -.. _scramblemutation: - -``scramble_mutation()`` -~~~~~~~~~~~~~~~~~~~~~~~ - -Applies the scramble mutation which selects a subset of genes and -shuffles their order randomly. - -.. _adaptivemutation: - -``adaptive_mutation()`` -~~~~~~~~~~~~~~~~~~~~~~~ - -Applies the adaptive mutation which selects the number/percentage of -genes to mutate based on the solution's fitness. If the fitness is high -(i.e. solution quality is high), then small number/percentage of genes -is mutated compared to a solution with a low fitness. - -.. _bestsolution: - -``best_solution()`` -------------------- - -Returns information about the best solution found by the genetic -algorithm. - -It accepts the following parameters: - -- ``pop_fitness=None``: An optional parameter that accepts a list of the - fitness values of the solutions in the population. If ``None``, then - the ``cal_pop_fitness()`` method is called to calculate the fitness - values of the population. - -It returns the following: - -- ``best_solution``: Best solution in the current population. - -- ``best_solution_fitness``: Fitness value of the best solution. - -- ``best_match_idx``: Index of the best solution in the current - population. - -.. _plotfitness: - -``plot_fitness()`` ------------------- - -Previously named ``plot_result()``, this method creates, shows, and -returns a figure that summarizes how the fitness value evolves by -generation. - -It works only after completing at least 1 generation. If no generation -is completed (at least 1), an exception is raised. - -.. _plotnewsolutionrate: - -``plot_new_solution_rate()`` ----------------------------- - -The ``plot_new_solution_rate()`` method creates, shows, and returns a -figure that shows the number of new solutions explored in each -generation. This method works only when ``save_solutions=True`` in the -constructor of the ``pygad.GA`` class. - -It works only after completing at least 1 generation. If no generation -is completed (at least 1), an exception is raised. - -.. _plotgenes: - -``plot_genes()`` ----------------- - -The ``plot_genes()`` method creates, shows, and returns a figure that -describes each gene. It has different options to create the figures -which helps to: - -1. Explore the gene value for each generation by creating a normal plot. - -2. Create a histogram for each gene. - -3. Create a boxplot. - -This is controlled by the ``graph_type`` parameter. - -It works only after completing at least 1 generation. If no generation -is completed (at least 1), an exception is raised. - ``save()`` ---------- -Saves the genetic algorithm instance +The ``save()`` method in the ``pygad.GA`` class saves the genetic +algorithm instance as a pickled object. Accepts the following parameter: @@ -1727,13 +1402,13 @@ its code is listed below. import operator def img2chromosome(img_arr): - return numpy.reshape(a=img_arr, newshape=(functools.reduce(operator.mul, img_arr.shape))) + return numpy.reshape(img_arr, (functools.reduce(operator.mul, img_arr.shape))) def chromosome2img(vector, shape): if len(vector) != functools.reduce(operator.mul, shape): raise ValueError(f"A vector of length {len(vector)} into an array of shape {shape}.") - return numpy.reshape(a=vector, newshape=shape) + return numpy.reshape(vector, shape) .. _create-an-instance-of-the-pygadga-class-2: diff --git a/docs/source/releases.rst b/docs/source/releases.rst index 8611130e..def4d0ac 100644 --- a/docs/source/releases.rst +++ b/docs/source/releases.rst @@ -1545,11 +1545,23 @@ Release Date 17 February 2024 ``last_generation_fitness``. https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/275 -2. 4 methods with names starting with ``run_``. Their purpose is to keep +2. 5 methods with names starting with ``run_``. Their purpose is to keep the main loop inside the ``run()`` method clean. Check the `Other Methods `__ section for more information. + 1. ``run_loop_head()``: The code before the loop starts. + + 2. ``run_select_parents()``: The parent selection-related code. + + 3. ``run_crossover()``: The crossover-related code. + + 4. ``run_mutation()``: The mutation-related code. + + 5. ``run_update_population()``: Update the ``population`` instance + attribute after completing the processes of crossover and + mutation. + .. _pygad-340: PyGAD 3.4.0 @@ -1774,6 +1786,125 @@ Release Date 08 July 2025 retrieving the best solution for multi-objective problems. https://github.com/ahmedfgad/GeneticAlgorithmPython/pull/331 +.. _pygad-360: + +PyGAD 3.6.0 +----------- + +1. Support passing a class to the fitness, crossover, and mutation. + https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/342 + +2. A new class called ``Validation`` is created in the new + ``pygad/utils/validation.py`` script. It has a method called + ``validate_parameters()`` to validate all the parameters passed + while instantiating the ``pygad.GA`` class. + +3. Refactoring the ``pygad.py`` script by moving a lot of functions and + methods to other classes in other scripts. + +4. The ``summary()`` method was moved to ``Helper`` class in the + ``pygad/helper/misc.py`` script. + +5. The validation code in the ``__init__()`` method of the ``pygad.GA`` + class is moved to the new ``validate_parameters()`` method in the + new ``Validation`` class in the new ``pygad/utils/validation.py`` + script. Moreover, the ``validate_multi_stop_criteria()`` method is + also moved to the same class. + +6. The GA main workflow is moved into the new ``GAEngine`` class in the + new ``pygad/utils/engine.py`` script. Specifically, these methods + are moved from the ``pygad.GA`` class to the new ``GAEngine`` class: + + 1. ``run()`` + + 1. ``run_loop_head()`` + + 2. ``run_select_parents()`` + + 3. ``run_crossover()`` + + 4. ``run_mutation()`` + + 5. ``run_update_population()`` + + 2. ``initialize_population()`` + + 3. ``cal_pop_fitness()`` + + 4. ``best_solution()`` + + 5. ``round_genes()`` + +7. The ``pygad.GA`` class now extends the two new classes + ``utils.validation.Validation`` and ``utils.engine.GAEngine``. + +8. The version of the ``pygad.utils`` submodule is upgraded from + ``1.3.0`` to ``1.4.0``. + +9. The version of the ``pygad.helper`` submodule is upgraded from + ``1.2.0`` to ``1.3.0``. + +10. The version of the ``pygad.visualize`` submodule is upgraded from + ``1.1.0`` to ``1.1.1``. + +11. The version of the ``pygad.nn`` submodule is upgraded from ``1.2.1`` + to ``1.2.2``. + +12. The version of the ``pygad.cnn`` submodule is upgraded from + ``1.1.0`` to ``1.1.1``. + +13. The version of the ``pygad.torchga`` submodule is upgraded from + ``1.4.0`` to ``1.4.1``. + +14. The version of the ``pygad.kerasga`` submodule is upgraded from + ``1.3.0`` to ``1.3.1``. + +15. Update the elitism after the evolution ends to fix issue where the + best solution returned by the ``best_solution()`` method is not + correct. + https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/337 + +16. Fix a bug in calling the ``numpy.reshape()`` function. The parameter + ``newshape`` is removed since it is no longer supported started from + NumPy ``2.4.0``. + https://numpy.org/doc/stable/release/2.4.0-notes.html#removed-newshape-parameter-from-numpy-reshape + +17. A minor change in the documentation is made to replace the + ``newshape`` parameter when calling ``numpy.reshape()``. + +18. Fix a bug in the ``visualize/plot.py`` script that causes a warning + to be given when the plot leged is used with single-objective + problems. + +19. A new method called ``initialize_parents_array()`` is added to the + ``Helper`` class in the ``pygad/helper/misc.py`` script. It is + usually called from the methods in the ``ParentSelection`` class in + the ``pygad/utils/parent_selection.py`` script to initialize the + parents array. + +20. Add more tests about: + + 1. Operators (crossover, mutation, and parent selection). + + 2. The ``best_solution()`` method. + + 3. Parallel processing. + + 4. The ``GANN`` module. + + 5. The plots created by the ``visualize``. + +21. Instead of using repeated code for converting the data type and + rounding the genes during crossover and mutation, the + ``change_gene_dtype_and_round()`` method is called from the + ``pygad.helper.misc.Helper`` class. + +22. Fix some documentation issues. + https://github.com/ahmedfgad/GeneticAlgorithmPython/pull/336 + +23. Update the documentation to reflect the recent additions and changes + to the library structure. + PyGAD Projects at GitHub ======================== diff --git a/docs/source/utils.rst b/docs/source/utils.rst index 15dcfb34..ef81bf60 100644 --- a/docs/source/utils.rst +++ b/docs/source/utils.rst @@ -14,16 +14,19 @@ section. The submodules in the ``pygad.utils`` module are: -1. ``crossover``: Has the ``Crossover`` class that implements the +1. ``engine``: The core engine of the library. It has the ``GAEngine`` + class implementing the main loop and related functions. + +2. ``crossover``: Has the ``Crossover`` class that implements the crossover operators. -2. ``mutation``: Has the ``Mutation`` class that implements the mutation +3. ``mutation``: Has the ``Mutation`` class that implements the mutation operators. -3. ``parent_selection``: Has the ``ParentSelection`` class that +4. ``parent_selection``: Has the ``ParentSelection`` class that implements the parent selection operators. -4. ``nsga2``: Has the ``NSGA2`` class that implements the Non-Dominated +5. ``nsga2``: Has the ``NSGA2`` class that implements the Non-Dominated Sorting Genetic Algorithm II (NSGA-II). Note that the ``pygad.GA`` class extends all of these classes. So, the @@ -32,6 +35,193 @@ instance/object of the ``pygad.GA`` class. The next sections discuss each submodule. +.. _pygadutilsengine-submodule: + +``pygad.utils.engine`` Submodule +================================ + +The ``pygad.utils.engine`` module has the ``GAEngine`` class that +implements the engine of the library. The methods in this class are: + +1. ``initialize_population()`` + +2. ``cal_pop_fitness()`` + +3. ``run()`` + + 1. ``run_loop_head()`` + + 2. ``run_select_parents()`` + + 3. ``run_crossover()`` + + 4. ``run_mutation()`` + + 5. ``run_update_population()`` + +4. ``best_solution()`` + +5. ``round_genes()`` + +.. _initializepopulation: + +``initialize_population()`` +--------------------------- + +It creates an initial population randomly as a NumPy array. The array is +saved in the instance attribute named ``population``. + +Accepts the following parameters: + +- ``low``: The lower value of the random range from which the gene + values in the initial population are selected. It defaults to -4. + Available in PyGAD 1.0.20 and higher. + +- ``high``: The upper value of the random range from which the gene + values in the initial population are selected. It defaults to -4. + Available in PyGAD 1.0.20. + +This method assigns the values of the following 3 instance attributes: + +1. ``pop_size``: Size of the population. + +2. ``population``: Initially, it holds the initial population and later + updated after each generation. + +3. ``initial_population``: Keeping the initial population. + +.. _calpopfitness: + +``cal_pop_fitness()`` +--------------------- + +The ``cal_pop_fitness()`` method calculates and returns the fitness +values of the solutions in the current population. + +This function is optimized to save time by making fewer calls the +fitness function. It follows this process: + +1. If the ``save_solutions`` parameter is set to ``True``, then it + checks if the solution is already explored and saved in the + ``solutions`` instance attribute. If so, then it just retrieves its + fitness from the ``solutions_fitness`` instance attribute without + calling the fitness function. + +2. If ``save_solutions`` is set to ``False`` or if it is ``True`` but + the solution was not explored yet, then the ``cal_pop_fitness()`` + method checks if the ``keep_elitism`` parameter is set to a positive + integer. If so, then it checks if the solution is saved into the + ``last_generation_elitism`` instance attribute. If so, then it + retrieves its fitness from the ``previous_generation_fitness`` + instance attribute. + +3. If neither of the above 3 conditions apply (1. ``save_solutions`` is + set to ``False`` or 2. if it is ``True`` but the solution was not + explored yet or 3. ``keep_elitism`` is set to zero), then the + ``cal_pop_fitness()`` method checks if the ``keep_parents`` parameter + is set to ``-1`` or a positive integer. If so, then it checks if the + solution is saved into the ``last_generation_parents`` instance + attribute. If so, then it retrieves its fitness from the + ``previous_generation_fitness`` instance attribute. + +4. If neither of the above 4 conditions apply, then we have to call the + fitness function to calculate the fitness for the solution. This is + by calling the function assigned to the ``fitness_func`` parameter. + +This function takes into consideration: + +1. The ``parallel_processing`` parameter to check whether parallel + processing is in effect. + +2. The ``fitness_batch_size`` parameter to check if the fitness should + be calculated in batches of solutions. + +It returns a vector of the solutions' fitness values. + +``run()`` +--------- + +Runs the genetic algorithm. This is the main method in which the genetic +algorithm is evolved through some generations. It accepts no parameters +as it uses the instance to access all of its requirements. + +For each generation, the fitness values of all solutions within the +population are calculated according to the ``cal_pop_fitness()`` method +which internally just calls the function assigned to the +``fitness_func`` parameter in the ``pygad.GA`` class constructor for +each solution. + +According to the fitness values of all solutions, the parents are +selected using the ``select_parents()`` method. This method behavior is +determined according to the parent selection type in the +``parent_selection_type`` parameter in the ``pygad.GA`` class +constructor + +Based on the selected parents, offspring are generated by applying the +crossover and mutation operations using the ``crossover()`` and +``mutation()`` methods. The behavior of such 2 methods is defined +according to the ``crossover_type`` and ``mutation_type`` parameters in +the ``pygad.GA`` class constructor. + +After the generation completes, the following takes place: + +- The ``population`` attribute is updated by the new population. + +- The ``generations_completed`` attribute is assigned by the number of + the last completed generation. + +- If there is a callback function assigned to the ``on_generation`` + attribute, then it will be called. + +After the ``run()`` method completes, the following takes place: + +- The ``best_solution_generation`` is assigned the generation number at + which the best fitness value is reached. + +- The ``run_completed`` attribute is set to ``True``. + +Note that the ``run()`` method is calling 5 different methods during the +loop: + +1. ``run_loop_head()`` + +2. ``run_select_parents()`` + +3. ``run_crossover()`` + +4. ``run_mutation()`` + +5. ``run_update_population()`` + +.. _bestsolution: + +``best_solution()`` +------------------- + +Returns the following information about the best solution in the latest +population: + +1. Solution + +2. Fitness + +3. Index within the population + +The best solution is determined based on the fitness values. To save +time calling the fitness function, the user is allowed to pass the +fitness based on which the best solution is determined. If not passed, +it will call the fitness function to calculate the fitness of all +solutions within the latest population. + +.. _roundgenes: + +``round_genes()`` +----------------- + +A method to round the genes in the passed solutions. It loops through +each gene across all the passed solutions and rounds their values if +applicable. + .. _pygadutilscrossover-submodule: ``pygad.utils.crossover`` Submodule @@ -339,8 +529,10 @@ implements NSGA-II. The methods inside this class are: 1. ``non_dominated_sorting()``: Returns all the pareto fronts by applying non-dominated sorting over the solutions. -2. ``get_non_dominated_set()``: Returns the set of non-dominated - solutions from the passed solutions. +2. ``get_non_dominated_set()``: Returns the 2 sets of non-dominated + solutions and dominated solutions from the passed solutions. Note + that the Pareto front consists of the solutions in the non-dominated + set. 3. ``crowding_distance()``: Calculates the crowding distance for all solutions in the current pareto front. diff --git a/docs/source/visualize.rst b/docs/source/visualize.rst index 629d4dfa..0f7e0c1b 100644 --- a/docs/source/visualize.rst +++ b/docs/source/visualize.rst @@ -10,12 +10,14 @@ visualization in PyGAD. This section discusses the different options to visualize the results in PyGAD through these methods: -1. ``plot_fitness()``: Creates plots for the fitness. +1. ``plot_fitness()``: Creates plots for the fitness to show how the + fitness evolves by generation. . -2. ``plot_genes()``: Creates plots for the genes. +2. ``plot_genes()``: Creates plots for the genes to show how the gene + value changes for each generation. -3. ``plot_new_solution_rate()``: Creates plots for the new solution - rate. +3. ``plot_new_solution_rate()``: Creates plots for the new solution rate + to show how the number of new solutions explored in each solution. 4. ``plot_pareto_front_curve()``: Creates plots for the pareto front for multi-objective problems. @@ -91,7 +93,7 @@ This method accepts the following parameters: 9. ``save_dir``: Directory to save the figure. -.. _plottype=plot: +.. _plottypeplot: ``plot_type="plot"`` ~~~~~~~~~~~~~~~~~~~~ @@ -107,7 +109,7 @@ line connecting the fitness values across all generations: |image1| -.. _plottype=scatter: +.. _plottypescatter: ``plot_type="scatter"`` ~~~~~~~~~~~~~~~~~~~~~~~ @@ -122,7 +124,7 @@ these dots can be changed using the ``linewidth`` parameter. |image2| -.. _plottype=bar: +.. _plottypebar: ``plot_type="bar"`` ~~~~~~~~~~~~~~~~~~~ @@ -175,7 +177,7 @@ in the ``plot_fitness()`` method (it also have 3 possible values for 8. ``save_dir``: Directory to save the figure. -.. _plottype=plot-2: +.. _plottypeplot-2: ``plot_type="plot"`` ~~~~~~~~~~~~~~~~~~~~ @@ -195,7 +197,7 @@ the constructor of the ``pygad.GA`` class) which is 10 in this example. |image4| -.. _plottype=scatter-2: +.. _plottypescatter-2: ``plot_type="scatter"`` ~~~~~~~~~~~~~~~~~~~~~~~ @@ -209,7 +211,7 @@ The previous graph can be represented as scattered points by setting |image5| -.. _plottype=bar-2: +.. _plottypebar-2: ``plot_type="bar"`` ~~~~~~~~~~~~~~~~~~~ @@ -311,7 +313,7 @@ An exception is raised if: - ``solutions="best"`` while ``save_best_solutions=False`` in the constructor of the ``pygad.GA`` class. . -.. _graphtype=plot: +.. _graphtypeplot: ``graph_type="plot"`` ~~~~~~~~~~~~~~~~~~~~~ @@ -320,7 +322,7 @@ When ``graph_type="plot"``, then the figure creates a normal graph where the relationship between the gene values and the generation numbers is represented as a continuous plot, scattered points, or bars. -.. _plottype=plot-3: +.. _plottypeplot-3: ``plot_type="plot"`` ^^^^^^^^^^^^^^^^^^^^ @@ -362,7 +364,7 @@ the following method calls generate the same plot. plot_type="plot", solutions="all") -.. _plottype=scatter-3: +.. _plottypescatter-3: ``plot_type="scatter"`` ^^^^^^^^^^^^^^^^^^^^^^^ @@ -380,7 +382,7 @@ scatter plot. |image8| -.. _plottype=bar-3: +.. _plottypebar-3: ``plot_type="bar"`` ^^^^^^^^^^^^^^^^^^^ @@ -395,7 +397,7 @@ scatter plot. |image9| -.. _graphtype=boxplot: +.. _graphtypeboxplot: ``graph_type="boxplot"`` ~~~~~~~~~~~~~~~~~~~~~~~~ @@ -416,7 +418,7 @@ figure as the default value for the ``solutions`` parameter is |image10| -.. _graphtype=histogram: +.. _graphtypehistogram: ``graph_type="histogram"`` ~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/examples/example_lifecycle_classes.py b/examples/example_lifecycle_classes.py new file mode 100644 index 00000000..07fffe33 --- /dev/null +++ b/examples/example_lifecycle_classes.py @@ -0,0 +1,78 @@ +import pygad +import numpy + +""" +Use a method to build the lifecycle. +""" + +class Fitness: + def __call__(self, ga_instance, solution, solution_idx): + fitness = numpy.sum(solution) + return fitness + +class Crossover: + def __call__(self, parents, offspring_size, ga_instance): + return numpy.random.rand(offspring_size[0], offspring_size[1]) + +class Mutation: + def __call__(self, offspring, ga_instance): + return offspring + +class OnStart: + def __call__(self, ga_instance): + print("on_start") + +class OnFitness: + def __call__(self, ga_instance, fitness): + print("on_fitness") + +class OnCrossover: + def __call__(self, ga_instance, offspring): + print("on_crossover") + +class OnMutation: + def __call__(self, ga_instance, offspring): + print("on_mutation") + +class OnParents: + def __call__(self, ga_instance, parents): + print("on_parents") + +class OnGeneration: + def __call__(self, ga_instance): + print("on_generation") + +class OnStop: + def __call__(self, ga_instance, fitness): + print("on_stop") + +num_generations = 10 # Number of generations. +num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool. + +sol_per_pop = 10 # Number of solutions in the population. +num_genes = 5 + +ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + + fitness_func=Fitness(), + + crossover_type=Crossover(), + mutation_type=Mutation(), + + on_start=OnStart(), + on_fitness=OnFitness(), + on_crossover=OnCrossover(), + on_mutation=OnMutation(), + on_parents=OnParents(), + on_generation=OnGeneration(), + on_stop=OnStop(), + + suppress_warnings=True) + +# Running the GA to optimize the parameters of the function. +ga_instance.run() + +ga_instance.plot_fitness() diff --git a/examples/example_lifecycle_methods.py b/examples/example_lifecycle_methods.py new file mode 100644 index 00000000..15a76aef --- /dev/null +++ b/examples/example_lifecycle_methods.py @@ -0,0 +1,73 @@ +import pygad +import numpy + +""" +Use a method to build the lifecycle. +""" + +class GAOperations: + def fitness_func(self, ga_instance, solution, solution_idx): + fitness = numpy.sum(solution) + return fitness + + def crossover(self, parents, offspring_size, ga_instance): + return numpy.random.rand(offspring_size[0], offspring_size[1]) + + def mutation(self, offspring, ga_instance): + return offspring + +class Lifecycle: + def on_start(self, ga_instance): + print("on_start") + + def on_fitness(self, ga_instance, fitness): + print("on_fitness") + + def on_crossover(self, ga_instance, offspring): + print("on_crossover") + + def on_mutation(self, ga_instance, offspring): + print("on_mutation") + + def on_parents(self, ga_instance, parents): + print("on_parents") + + def on_generation(self, ga_instance): + print("on_generation") + + def on_stop(self, ga_instance, fitness): + print("on_stop") + +ga_obj = GAOperations() +lifecycle_obj = Lifecycle() + +num_generations = 10 # Number of generations. +num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool. + +sol_per_pop = 10 # Number of solutions in the population. +num_genes = 5 + +ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + + fitness_func=ga_obj.fitness_func, + + crossover_type=ga_obj.crossover, + mutation_type=ga_obj.mutation, + + on_start=lifecycle_obj.on_start, + on_fitness=lifecycle_obj.on_fitness, + on_crossover=lifecycle_obj.on_crossover, + on_mutation=lifecycle_obj.on_mutation, + on_parents=lifecycle_obj.on_parents, + on_generation=lifecycle_obj.on_generation, + on_stop=lifecycle_obj.on_stop, + + suppress_warnings=True) + +# Running the GA to optimize the parameters of the function. +ga_instance.run() + +ga_instance.plot_fitness() diff --git a/examples/example_summary.py b/examples/example_summary.py new file mode 100644 index 00000000..c1bc6d3a --- /dev/null +++ b/examples/example_summary.py @@ -0,0 +1,30 @@ +import pygad +import numpy + +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): + output = numpy.sum(solution*function_inputs) + fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) + return fitness + +num_generations = 100 # Number of generations. +num_parents_mating = 10 # Number of solutions to be selected as parents in the mating pool. + +sol_per_pop = 20 # Number of solutions in the population. +num_genes = len(function_inputs) + +ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + fitness_func=fitness_func) + +# Running the GA to optimize the parameters of the function. +ga_instance.run() + +ga_instance.plot_fitness() + +ga_instance.summary() + diff --git a/examples/pygad_lifecycle.py b/examples/pygad_lifecycle.py index 8eeae5b6..829fa060 100644 --- a/examples/pygad_lifecycle.py +++ b/examples/pygad_lifecycle.py @@ -37,12 +37,15 @@ def on_stop(ga_instance, last_population_fitness): 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) + on_stop=on_stop, + + suppress_warnings=True) ga_instance.run() diff --git a/pygad/__init__.py b/pygad/__init__.py index 53ccec2f..d7dca9d5 100644 --- a/pygad/__init__.py +++ b/pygad/__init__.py @@ -1,3 +1,3 @@ from .pygad import * # Relative import. -__version__ = "3.5.0" +__version__ = "3.6.0" diff --git a/pygad/cnn/__init__.py b/pygad/cnn/__init__.py index da317b5b..7db2b71c 100644 --- a/pygad/cnn/__init__.py +++ b/pygad/cnn/__init__.py @@ -1,4 +1,4 @@ from .cnn import * -__version__ = "1.1.0" +__version__ = "1.1.1" diff --git a/pygad/cnn/cnn.py b/pygad/cnn/cnn.py index 82dc8de1..0e425bb0 100644 --- a/pygad/cnn/cnn.py +++ b/pygad/cnn/cnn.py @@ -126,7 +126,7 @@ def layers_weights_as_matrix(model, vector_weights): weights_vector=vector_weights[start:start + layer_weights_size] # matrix = pygad.nn.DenseLayer.to_array(vector=weights_vector, shape=layer_weights_shape) - matrix = numpy.reshape(weights_vector, newshape=(layer_weights_shape)) + matrix = numpy.reshape(weights_vector, (layer_weights_shape)) network_weights.append(matrix) start = start + layer_weights_size @@ -163,11 +163,11 @@ def layers_weights_as_vector(model, initial=True): if type(layer) in [Conv2D, Dense]: # If the 'initial' parameter is True, append the initial weights. Otherwise, append the trained weights. if initial == True: - vector = numpy.reshape(layer.initial_weights, newshape=(layer.initial_weights.size)) + vector = numpy.reshape(layer.initial_weights, (layer.initial_weights.size)) # vector = pygad.nn.DenseLayer.to_vector(matrix=layer.initial_weights) network_weights.extend(vector) elif initial == False: - vector = numpy.reshape(layer.trained_weights, newshape=(layer.trained_weights.size)) + vector = numpy.reshape(layer.trained_weights, (layer.trained_weights.size)) # vector = pygad.nn.DenseLayer.to_vector(array=layer.trained_weights) network_weights.extend(vector) else: diff --git a/pygad/gann/__init__.py b/pygad/gann/__init__.py index ff458d41..7465513a 100644 --- a/pygad/gann/__init__.py +++ b/pygad/gann/__init__.py @@ -1,4 +1,4 @@ from .gann import * -__version__ = "1.0.1" +__version__ = "1.0.0" diff --git a/pygad/helper/__init__.py b/pygad/helper/__init__.py index 49c736ce..4e1472a6 100644 --- a/pygad/helper/__init__.py +++ b/pygad/helper/__init__.py @@ -1,4 +1,4 @@ from pygad.helper import unique from pygad.helper import misc -__version__ = "1.2.0" \ No newline at end of file +__version__ = "1.3.0" \ No newline at end of file diff --git a/pygad/helper/misc.py b/pygad/helper/misc.py index 43c063ec..49157d11 100644 --- a/pygad/helper/misc.py +++ b/pygad/helper/misc.py @@ -9,6 +9,244 @@ class Helper: + def summary(self, + line_length=70, + fill_character=" ", + line_character="-", + line_character2="=", + columns_equal_len=False, + print_step_parameters=True, + print_parameters_summary=True): + """ + The summary() method prints a summary of the PyGAD lifecycle in a Keras style. + The parameters are: + line_length: An integer representing the length of the single line in characters. + fill_character: A character to fill the lines. + line_character: A character for creating a line separator. + line_character2: A secondary character to create a line separator. + columns_equal_len: The table rows are split into equal-sized columns or split subjective to the width needed. + print_step_parameters: Whether to print extra parameters about each step inside the step. If print_step_parameters=False and print_parameters_summary=True, then the parameters of each step are printed at the end of the table. + print_parameters_summary: Whether to print parameters summary at the end of the table. If print_step_parameters=False, then the parameters of each step are printed at the end of the table too. + """ + + summary_output = "" + + def fill_message(msg, line_length=line_length, fill_character=fill_character): + num_spaces = int((line_length - len(msg))/2) + num_spaces = int(num_spaces / len(fill_character)) + msg = "{spaces}{msg}{spaces}".format( + msg=msg, spaces=fill_character * num_spaces) + return msg + + def line_separator(line_length=line_length, line_character=line_character): + num_characters = int(line_length / len(line_character)) + return line_character * num_characters + + def create_row(columns, line_length=line_length, fill_character=fill_character, split_percentages=None): + filled_columns = [] + if split_percentages is None: + split_percentages = [int(100/len(columns))] * 3 + columns_lengths = [int((split_percentages[idx] * line_length) / 100) + for idx in range(len(split_percentages))] + for column_idx, column in enumerate(columns): + current_column_length = len(column) + extra_characters = columns_lengths[column_idx] - \ + current_column_length + filled_column = column + fill_character * extra_characters + filled_columns.append(filled_column) + + return "".join(filled_columns) + + def print_parent_selection_params(): + nonlocal summary_output + m = f"Number of Parents: {self.num_parents_mating}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + if self.parent_selection_type == "tournament": + m = f"K Tournament: {self.K_tournament}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + + def print_fitness_params(): + nonlocal summary_output + if not self.fitness_batch_size is None: + m = f"Fitness batch size: {self.fitness_batch_size}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + + def print_crossover_params(): + nonlocal summary_output + if not self.crossover_probability is None: + m = f"Crossover probability: {self.crossover_probability}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + + def print_mutation_params(): + nonlocal summary_output + if not self.mutation_probability is None: + m = f"Mutation Probability: {self.mutation_probability}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + if self.mutation_percent_genes == "default": + m = f"Mutation Percentage: {self.mutation_percent_genes}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + # Number of mutation genes is already showed above. + m = f"Mutation Genes: {self.mutation_num_genes}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = f"Random Mutation Range: ({self.random_mutation_min_val}, {self.random_mutation_max_val})" + self.logger.info(m) + summary_output = summary_output + m + "\n" + if not self.gene_space is None: + m = f"Gene Space: {self.gene_space}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = f"Mutation by Replacement: {self.mutation_by_replacement}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = f"Allow Duplicated Genes: {self.allow_duplicate_genes}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + + def print_on_generation_params(): + nonlocal summary_output + if not self.stop_criteria is None: + m = f"Stop Criteria: {self.stop_criteria}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + + def print_params_summary(): + nonlocal summary_output + m = f"Population Size: ({self.sol_per_pop}, {self.num_genes})" + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = f"Number of Generations: {self.num_generations}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = f"Initial Population Range: ({self.init_range_low}, {self.init_range_high})" + self.logger.info(m) + summary_output = summary_output + m + "\n" + + if not print_step_parameters: + print_fitness_params() + + if not print_step_parameters: + print_parent_selection_params() + + if self.keep_elitism != 0: + m = f"Keep Elitism: {self.keep_elitism}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + else: + m = f"Keep Parents: {self.keep_parents}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = f"Gene DType: {self.gene_type}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + + if not print_step_parameters: + print_crossover_params() + + if not print_step_parameters: + print_mutation_params() + + if not print_step_parameters: + print_on_generation_params() + + if not self.parallel_processing is None: + m = f"Parallel Processing: {self.parallel_processing}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + if not self.random_seed is None: + m = f"Random Seed: {self.random_seed}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = f"Save Best Solutions: {self.save_best_solutions}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = f"Save Solutions: {self.save_solutions}" + self.logger.info(m) + summary_output = summary_output + m + "\n" + + m = line_separator(line_character=line_character) + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = fill_message("PyGAD Lifecycle") + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = line_separator(line_character=line_character2) + self.logger.info(m) + summary_output = summary_output + m + "\n" + + lifecycle_steps = ["on_start()", "Fitness Function", "On Fitness", "Parent Selection", "On Parents", + "Crossover", "On Crossover", "Mutation", "On Mutation", "On Generation", "On Stop"] + lifecycle_functions = [self.on_start, self.fitness_func, self.on_fitness, self.select_parents, self.on_parents, + self.crossover, self.on_crossover, self.mutation, self.on_mutation, self.on_generation, self.on_stop] + lifecycle_functions = [getattr( + lifecycle_func, '__name__', "None") for lifecycle_func in lifecycle_functions] + lifecycle_functions = [lifecycle_func + "()" if lifecycle_func != + "None" else "None" for lifecycle_func in lifecycle_functions] + lifecycle_output = ["None", "(1)", "None", f"({self.num_parents_mating}, {self.num_genes})", "None", + f"({self.num_parents_mating}, {self.num_genes})", "None", f"({self.num_parents_mating}, {self.num_genes})", "None", "None", "None"] + lifecycle_step_parameters = [None, print_fitness_params, None, print_parent_selection_params, None, + print_crossover_params, None, print_mutation_params, None, print_on_generation_params, None] + + if not columns_equal_len: + max_lengthes = [max(list(map(len, lifecycle_steps))), max( + list(map(len, lifecycle_functions))), max(list(map(len, lifecycle_output)))] + split_percentages = [ + int((column_len / sum(max_lengthes)) * 100) for column_len in max_lengthes] + else: + split_percentages = None + + header_columns = ["Step", "Handler", "Output Shape"] + header_row = create_row( + header_columns, split_percentages=split_percentages) + m = header_row + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = line_separator(line_character=line_character2) + self.logger.info(m) + summary_output = summary_output + m + "\n" + + for lifecycle_idx in range(len(lifecycle_steps)): + lifecycle_column = [lifecycle_steps[lifecycle_idx], + lifecycle_functions[lifecycle_idx], lifecycle_output[lifecycle_idx]] + if lifecycle_column[1] == "None": + continue + lifecycle_row = create_row( + lifecycle_column, split_percentages=split_percentages) + m = lifecycle_row + self.logger.info(m) + summary_output = summary_output + m + "\n" + if print_step_parameters: + if not lifecycle_step_parameters[lifecycle_idx] is None: + lifecycle_step_parameters[lifecycle_idx]() + m = line_separator(line_character=line_character) + self.logger.info(m) + summary_output = summary_output + m + "\n" + + m = line_separator(line_character=line_character2) + self.logger.info(m) + summary_output = summary_output + m + "\n" + if print_parameters_summary: + print_params_summary() + m = line_separator(line_character=line_character2) + self.logger.info(m) + summary_output = summary_output + m + "\n" + return summary_output + + def initialize_parents_array(self, shape): + """ + Standardize array initialization for parents and offspring. + """ + if self.gene_type_single: + return numpy.empty(shape, dtype=self.gene_type[0]) + else: + return numpy.empty(shape, dtype=object) + def change_population_dtype_and_round(self, population): """ diff --git a/pygad/kerasga/__init__.py b/pygad/kerasga/__init__.py index 735d549c..8f5b353a 100644 --- a/pygad/kerasga/__init__.py +++ b/pygad/kerasga/__init__.py @@ -1,3 +1,3 @@ from .kerasga import * -__version__ = "1.3.0" +__version__ = "1.3.1" diff --git a/pygad/kerasga/kerasga.py b/pygad/kerasga/kerasga.py index cda2c4b9..738e9717 100644 --- a/pygad/kerasga/kerasga.py +++ b/pygad/kerasga/kerasga.py @@ -23,7 +23,7 @@ def model_weights_as_vector(model): if layer.trainable: layer_weights = layer.get_weights() for l_weights in layer_weights: - vector = numpy.reshape(l_weights, newshape=(l_weights.size)) + vector = numpy.reshape(l_weights, (l_weights.size)) weights_vector.extend(vector) return numpy.array(weights_vector) @@ -57,7 +57,7 @@ def model_weights_as_matrix(model, weights_vector): layer_weights_size = l_weights.size layer_weights_vector = weights_vector[start:start + layer_weights_size] - layer_weights_matrix = numpy.reshape(layer_weights_vector, newshape=(layer_weights_shape)) + layer_weights_matrix = numpy.reshape(layer_weights_vector, (layer_weights_shape)) weights_matrix.append(layer_weights_matrix) start = start + layer_weights_size diff --git a/pygad/nn/__init__.py b/pygad/nn/__init__.py index 224d9843..8a7632b9 100644 --- a/pygad/nn/__init__.py +++ b/pygad/nn/__init__.py @@ -1,4 +1,4 @@ from .nn import * -__version__ = "1.2.1" +__version__ = "1.2.2" diff --git a/pygad/nn/nn.py b/pygad/nn/nn.py index 023c4e28..d14d0393 100644 --- a/pygad/nn/nn.py +++ b/pygad/nn/nn.py @@ -56,11 +56,11 @@ def layers_weights_as_vector(last_layer, initial=True): while "previous_layer" in layer.__init__.__code__.co_varnames: # If the 'initial' parameter is True, append the initial weights. Otherwise, append the trained weights. if initial == True: - vector = numpy.reshape(layer.initial_weights, newshape=(layer.initial_weights.size)) + vector = numpy.reshape(layer.initial_weights, (layer.initial_weights.size)) # vector = DenseLayer.to_vector(matrix=layer.initial_weights) network_weights.extend(vector) elif initial == False: - vector = numpy.reshape(layer.trained_weights, newshape=(layer.trained_weights.size)) + vector = numpy.reshape(layer.trained_weights, (layer.trained_weights.size)) # vector = DenseLayer.to_vector(array=layer.trained_weights) network_weights.extend(vector) else: @@ -98,7 +98,7 @@ def layers_weights_as_matrix(last_layer, vector_weights): weights_vector=vector_weights[start:start + layer_weights_size] # matrix = DenseLayer.to_array(vector=weights_vector, shape=layer_weights_shape) - matrix = numpy.reshape(weights_vector, newshape=(layer_weights_shape)) + matrix = numpy.reshape(weights_vector, (layer_weights_shape)) network_weights.append(matrix) start = start + layer_weights_size @@ -338,7 +338,7 @@ def to_vector(array): """ if not (type(array) is numpy.ndarray): raise TypeError(f"An input of type numpy.ndarray is expected but an input of type {type(array)} found.") - return numpy.reshape(array, newshape=(array.size)) + return numpy.reshape(array, (array.size)) def to_array(vector, shape): """ @@ -357,7 +357,7 @@ def to_array(vector, shape): raise ValueError(f"A 1D NumPy array is expected but an array of {vector.ndim} dimensions found.") if vector.size != functools.reduce(lambda x,y:x*y, shape, 1): # (operator.mul == lambda x,y:x*y raise ValueError(f"Mismatch between the vector length and the array shape. A vector of length {vector.size} cannot be converted into a array of shape ({shape}).") - return numpy.reshape(vector, newshape=shape) + return numpy.reshape(vector, shape) class InputLayer: """ diff --git a/pygad/pygad.py b/pygad/pygad.py index 6eca219f..f17bd99b 100644 --- a/pygad/pygad.py +++ b/pygad/pygad.py @@ -1,10 +1,5 @@ import numpy -import random import cloudpickle -import warnings -import concurrent.futures -import inspect -import logging from pygad import utils from pygad import helper from pygad import visualize @@ -14,6 +9,8 @@ class GA(utils.parent_selection.ParentSelection, utils.crossover.Crossover, utils.mutation.Mutation, utils.nsga2.NSGA2, + utils.validation.Validation, + utils.engine.GAEngine, helper.unique.Unique, helper.misc.Helper, visualize.plot.Plot): @@ -73,12 +70,12 @@ def __init__(self, num_generations: Number of generations. num_parents_mating: Number of solutions to be selected as parents in the mating pool. - fitness_func: Accepts a function/method and returns the fitness value of the solution. In PyGAD 2.20.0, a third parameter is passed referring to the 'pygad.GA' instance. If method, then it must accept 4 parameters where the fourth one refers to the method's object. + fitness_func: Accepts a function/method and returns the fitness value of the solution. In PyGAD 2.20.0, a third parameter is passed referring to the 'pygad.GA' instance. fitness_batch_size: Added in PyGAD 2.19.0. Supports calculating the fitness in batches. If the value is 1 or None, then the fitness function is called for each individual solution. If given another value X where X is neither 1 nor None (e.g. X=3), then the fitness function is called once for each X (3) solutions. initial_population: A user-defined initial population. It is useful when the user wants to start the generations with a custom initial population. It defaults to None which means no initial population is specified by the user. In this case, PyGAD creates an initial population using the 'sol_per_pop' and 'num_genes' parameters. An exception is raised if the 'initial_population' is None while any of the 2 parameters ('sol_per_pop' or 'num_genes') is also None. sol_per_pop: Number of solutions in the population. - num_genes: Number of parameters in the function. + num_genes: Number of genes in the solution. init_range_low: The lower value of the random range from which the gene values in the initial population are selected. It defaults to -4. Available in PyGAD 1.0.20 and higher. init_range_high: The upper value of the random range from which the gene values in the initial population are selected. It defaults to -4. Available in PyGAD 1.0.20. @@ -87,7 +84,7 @@ def __init__(self, gene_type: The type of the gene. It is assigned to any of these types (int, numpy.int8, numpy.int16, numpy.int32, numpy.int64, numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, float, numpy.float16, numpy.float32, numpy.float64) and forces all the genes to be of that type. parent_selection_type: Type of parent selection. - keep_parents: If 0, this means no parent in the current population will be used in the next population. If -1, this means all parents in the current population will be used in the next population. If set to a value > 0, then the specified value refers to the number of parents in the current population to be used in the next population. Some parent selection operators such as rank selection, favor population diversity and therefore keeping the parents in the next generation can be beneficial. However, some other parent selection operators, such as roulette wheel selection (RWS), have higher selection pressure and keeping more than one parent in the next generation can seriously harm population diversity. This parameter have an effect only when the keep_elitism parameter is 0. Thanks to Prof. Fernando Jiménez (http://webs.um.es/fernan) for editing this sentence. + keep_parents: If 0, this means no parent in the current population will be used in the next population. If -1, this means all parents in the current population will be used in the next population. If set to a value > 0, then the specified value refers to the number of parents in the current population to be used in the next population. Some parent selection operators such as rank selection, favor population diversity and therefore keeping the parents in the next generation can be beneficial. However, some other parent selection operators, such as roulette wheel selection (RWS), have higher selection pressure and keeping more than one parent in the next generation can seriously harm population diversity. This parameter has an effect only when the keep_elitism parameter is 0. Thanks to Prof. Fernando Jiménez (http://webs.um.es/fernan) for editing this sentence. K_tournament: When the value of 'parent_selection_type' is 'tournament', the 'K_tournament' parameter specifies the number of solutions from which a parent is selected randomly. keep_elitism: Added in PyGAD 2.18.0. It can take the value 0 or a positive integer that satisfies (0 <= keep_elitism <= sol_per_pop). It defaults to 1 which means only the best solution in the current generation is kept in the next generation. If assigned 0, this means it has no effect. If assigned a positive integer K, then the best K solutions are kept in the next generation. It cannot be assigned a value greater than the value assigned to the sol_per_pop parameter. If this parameter has a value different from 0, then the keep_parents parameter will have no effect. @@ -110,13 +107,13 @@ def __init__(self, gene_constraint: It accepts a list of constraints for the genes. Each constraint is a Python function. Added in PyGAD 3.5.0. sample_size: To select a gene value that respects a constraint, this variable defines the size of the sample from which a value is selected randomly. Useful if either allow_duplicate_genes or gene_constraint is used. Added in PyGAD 3.5.0. - on_start: Accepts a function/method to be called only once before the genetic algorithm starts its evolution. If functioned, then it must accept a single parameter representing the instance of the genetic algorithm. If method, then it must accept 2 parameters where the second one refers to the method's object. Added in PyGAD 2.6.0. - on_fitness: Accepts a function/method to be called after calculating the fitness values of all solutions in the population. If functioned, then it must accept 2 parameters: 1) a list of all solutions' fitness values 2) the instance of the genetic algorithm. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0. - on_parents: Accepts a function/method to be called after selecting the parents that mates. If functioned, then it must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one represents the selected parents. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0. - on_crossover: Accepts a function/method to be called each time the crossover operation is applied. If functioned, then it must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one represents the offspring generated using crossover. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0. - on_mutation: Accepts a function/method to be called each time the mutation operation is applied. If functioned, then it must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one represents the offspring after applying the mutation. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0. - on_generation: Accepts a function/method to be called after each generation. If functioned, then it must accept a single parameter representing the instance of the genetic algorithm. If the function returned "stop", then the run() method stops without completing the other generations. If method, then it must accept 2 parameters where the second one refers to the method's object. Added in PyGAD 2.6.0. - on_stop: Accepts a function/method to be called only once exactly before the genetic algorithm stops or when it completes all the generations. If functioned, then it must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one is a list of fitness values of the last population's solutions. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0. + on_start: Accepts a function/method to be called only once before the genetic algorithm starts its evolution. If function, then it must accept a single parameter representing the instance of the genetic algorithm. If method, then it must accept 2 parameters where the second one refers to the method's object. Added in PyGAD 2.6.0. + on_fitness: Accepts a function/method to be called after calculating the fitness values of all solutions in the population. If function, then it must accept 2 parameters: 1) a list of all solutions' fitness values 2) the instance of the genetic algorithm. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0. + on_parents: Accepts a function/method to be called after selecting the parents that mate. If function, then it must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one represents the selected parents. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0. + on_crossover: Accepts a function/method to be called each time the crossover operation is applied. If function, then it must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one represents the offspring generated using crossover. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0. + on_mutation: Accepts a function/method to be called each time the mutation operation is applied. If function, then it must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one represents the offspring after applying the mutation. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0. + on_generation: Accepts a function/method to be called after each generation. If function, then it must accept a single parameter representing the instance of the genetic algorithm. If the function returned "stop", then the run() method stops without completing the other generations. If method, then it must accept 2 parameters where the second one refers to the method's object. Added in PyGAD 2.6.0. + on_stop: Accepts a function/method to be called only once exactly before the genetic algorithm stops or when it completes all the generations. If function, then it must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one is a list of fitness values of the last population's solutions. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0. save_best_solutions: Added in PyGAD 2.9.0 and its type is bool. If True, then the best solution in each generation is saved into the 'best_solutions' attribute. Use this parameter with caution as it may cause memory overflow when either the number of generations or the number of genes is large. save_solutions: Added in PyGAD 2.15.0 and its type is bool. If True, then all solutions in each generation are saved into the 'solutions' attribute. Use this parameter with caution as it may cause memory overflow when either the number of generations, number of genes, or number of solutions in population is large. @@ -134,2126 +131,52 @@ def __init__(self, logger: Added in PyGAD 2.20.0. It accepts a logger object of the 'logging.Logger' class to log the messages. If no logger is passed, then a default logger is created to log/print the messages to the console exactly like using the 'print()' function. """ try: - # If no logger is passed, then create a logger that logs only the messages to the console. - if logger is None: - # Create a logger named with the module name. - logger = logging.getLogger(__name__) - # Set the logger log level to 'DEBUG' to log all kinds of messages. - logger.setLevel(logging.DEBUG) - - # Clear any attached handlers to the logger from the previous runs. - # If the handlers are not cleared, then the new handler will be appended to the list of handlers. - # This makes the single log message be repeated according to the length of the list of handlers. - logger.handlers.clear() - - # Create the handlers. - stream_handler = logging.StreamHandler() - # Set the handler log level to 'DEBUG' to log all kinds of messages received from the logger. - stream_handler.setLevel(logging.DEBUG) - - # Create the formatter that just includes the log message. - formatter = logging.Formatter('%(message)s') - - # Add the formatter to the handler. - stream_handler.setFormatter(formatter) - - # Add the handler to the logger. - logger.addHandler(stream_handler) - else: - # Validate that the passed logger is of type 'logging.Logger'. - if isinstance(logger, logging.Logger): - pass - else: - raise TypeError(f"The expected type of the 'logger' parameter is 'logging.Logger' but {type(logger)} found.") - - # Create the 'self.logger' attribute to hold the logger. - # Instead of using 'print()', use 'self.logger.info()' - self.logger = logger - - self.random_seed = random_seed - if random_seed is None: - pass - else: - numpy.random.seed(self.random_seed) - random.seed(self.random_seed) - - # If suppress_warnings is bool and its value is False, then print warning messages. - if type(suppress_warnings) is bool: - self.suppress_warnings = suppress_warnings - else: - self.valid_parameters = False - raise TypeError(f"The expected type of the 'suppress_warnings' parameter is bool but {type(suppress_warnings)} found.") - - # Validating mutation_by_replacement - if not (type(mutation_by_replacement) is bool): - self.valid_parameters = False - raise TypeError(f"The expected type of the 'mutation_by_replacement' parameter is bool but {type(mutation_by_replacement)} found.") - - self.mutation_by_replacement = mutation_by_replacement - - # Validate the sample_size parameter. - if type(sample_size) in GA.supported_int_types: - if sample_size > 0: - pass - else: - self.valid_parameters = False - raise ValueError(f"The value of the sample_size parameter must be > 0 but the value ({sample_size}) found.") - else: - self.valid_parameters = False - raise TypeError(f"The type of the sample_size parameter must be integer but the value ({sample_size}) of type ({type(sample_size)}) found.") - - self.sample_size = sample_size - - # Validate allow_duplicate_genes - if not (type(allow_duplicate_genes) is bool): - self.valid_parameters = False - raise TypeError(f"The expected type of the 'allow_duplicate_genes' parameter is bool but {type(allow_duplicate_genes)} found.") - - self.allow_duplicate_genes = allow_duplicate_genes - - # Validate gene_space - self.gene_space_nested = False - if type(gene_space) is type(None): - pass - elif type(gene_space) is range: - if len(gene_space) == 0: - self.valid_parameters = False - raise ValueError("'gene_space' cannot be empty (i.e. its length must be >= 0).") - elif type(gene_space) in [list, numpy.ndarray]: - if len(gene_space) == 0: - self.valid_parameters = False - raise ValueError("'gene_space' cannot be empty (i.e. its length must be >= 0).") - else: - for index, el in enumerate(gene_space): - if type(el) in [numpy.ndarray, list, tuple, range]: - if len(el) == 0: - self.valid_parameters = False - raise ValueError(f"The element indexed {index} of 'gene_space' with type {type(el)} cannot be empty (i.e. its length must be >= 0).") - else: - for val in el: - if not (type(val) in [type(None)] + GA.supported_int_float_types): - raise TypeError(f"All values in the sublists inside the 'gene_space' attribute must be numeric of type int/float/None but ({val}) of type {type(val)} found.") - self.gene_space_nested = True - elif type(el) == type(None): - pass - # self.gene_space_nested = True - elif type(el) is dict: - if len(el.items()) == 2: - if ('low' in el.keys()) and ('high' in el.keys()): - pass - else: - self.valid_parameters = False - raise ValueError(f"When an element in the 'gene_space' parameter is of type dict, then it can have the keys 'low', 'high', and 'step' (optional) but the following keys found: {el.keys()}") - elif len(el.items()) == 3: - if ('low' in el.keys()) and ('high' in el.keys()) and ('step' in el.keys()): - pass - else: - self.valid_parameters = False - raise ValueError(f"When an element in the 'gene_space' parameter is of type dict, then it can have the keys 'low', 'high', and 'step' (optional) but the following keys found: {el.keys()}") - else: - self.valid_parameters = False - raise ValueError(f"When an element in the 'gene_space' parameter is of type dict, then it must have only 2 items but ({len(el.items())}) items found.") - self.gene_space_nested = True - elif not (type(el) in GA.supported_int_float_types): - self.valid_parameters = False - raise TypeError(f"Unexpected type {type(el)} for the element indexed {index} of 'gene_space'. The accepted types are list/tuple/range/numpy.ndarray of numbers, a single number (int/float), or None.") - - elif type(gene_space) is dict: - if len(gene_space.items()) == 2: - if ('low' in gene_space.keys()) and ('high' in gene_space.keys()): - pass - else: - self.valid_parameters = False - raise ValueError(f"When the 'gene_space' parameter is of type dict, then it can have only the keys 'low', 'high', and 'step' (optional) but the following keys found: {gene_space.keys()}") - elif len(gene_space.items()) == 3: - if ('low' in gene_space.keys()) and ('high' in gene_space.keys()) and ('step' in gene_space.keys()): - pass - else: - self.valid_parameters = False - raise ValueError(f"When the 'gene_space' parameter is of type dict, then it can have only the keys 'low', 'high', and 'step' (optional) but the following keys found: {gene_space.keys()}") - else: - self.valid_parameters = False - raise ValueError(f"When the 'gene_space' parameter is of type dict, then it must have only 2 items but ({len(gene_space.items())}) items found.") - - else: - self.valid_parameters = False - raise TypeError(f"The expected type of 'gene_space' is list, range, or numpy.ndarray but {type(gene_space)} found.") - - self.gene_space = gene_space - - # Validate init_range_low and init_range_high - if type(init_range_low) in GA.supported_int_float_types: - if type(init_range_high) in GA.supported_int_float_types: - if init_range_low == init_range_high: - if not self.suppress_warnings: - warnings.warn("The values of the 2 parameters 'init_range_low' and 'init_range_high' are equal and this might return the same value for some genes in the initial population.") - else: - self.valid_parameters = False - raise TypeError(f"Type mismatch between the 2 parameters 'init_range_low' {type(init_range_low)} and 'init_range_high' {type(init_range_high)}.") - elif type(init_range_low) in [list, tuple, numpy.ndarray]: - # Get the number of genes before validating the num_genes parameter. - if num_genes is None: - if initial_population is None: - self.valid_parameters = False - raise TypeError("When the parameter 'initial_population' is None, then the 2 parameters 'sol_per_pop' and 'num_genes' cannot be None too.") - elif not len(init_range_low) == len(initial_population[0]): - self.valid_parameters = False - raise ValueError(f"The length of the 'init_range_low' parameter is {len(init_range_low)} which is different from the number of genes {len(initial_population[0])}.") - elif not len(init_range_low) == num_genes: - self.valid_parameters = False - raise ValueError(f"The length of the 'init_range_low' parameter is {len(init_range_low)} which is different from the number of genes {num_genes}.") - - if type(init_range_high) in [list, tuple, numpy.ndarray]: - if len(init_range_low) == len(init_range_high): - pass - else: - self.valid_parameters = False - raise ValueError(f"Size mismatch between the 2 parameters 'init_range_low' {len(init_range_low)} and 'init_range_high' {len(init_range_high)}.") - - # Validate the values in init_range_low - for val in init_range_low: - if type(val) in GA.supported_int_float_types: - pass - else: - self.valid_parameters = False - raise TypeError(f"When an iterable (list/tuple/numpy.ndarray) is assigned to the 'init_range_low' parameter, its elements must be numeric but the value {val} of type {type(val)} found.") - - # Validate the values in init_range_high - for val in init_range_high: - if type(val) in GA.supported_int_float_types: - pass - else: - self.valid_parameters = False - raise TypeError(f"When an iterable (list/tuple/numpy.ndarray) is assigned to the 'init_range_high' parameter, its elements must be numeric but the value {val} of type {type(val)} found.") - else: - self.valid_parameters = False - raise TypeError(f"Type mismatch between the 2 parameters 'init_range_low' {type(init_range_low)} and 'init_range_high' {type(init_range_high)}. Both of them can be either numeric or iterable (list/tuple/numpy.ndarray).") - else: - self.valid_parameters = False - raise TypeError(f"The expected type of the 'init_range_low' parameter is numeric or list/tuple/numpy.ndarray but {type(init_range_low)} found.") - - self.init_range_low = init_range_low - self.init_range_high = init_range_high - - # Validate gene_type - if gene_type in GA.supported_int_float_types: - self.gene_type = [gene_type, None] - self.gene_type_single = True - # A single data type of float with precision. - elif len(gene_type) == 2 and gene_type[0] in GA.supported_float_types and (type(gene_type[1]) in GA.supported_int_types or gene_type[1] is None): - self.gene_type = gene_type - self.gene_type_single = True - # A single data type of integer with precision None ([int, None]). - elif len(gene_type) == 2 and gene_type[0] in GA.supported_int_types and gene_type[1] is None: - self.gene_type = gene_type - self.gene_type_single = True - # Raise an exception for a single data type of int with integer precision. - elif len(gene_type) == 2 and gene_type[0] in GA.supported_int_types and (type(gene_type[1]) in GA.supported_int_types or gene_type[1] is None): - self.gene_type_single = False - raise ValueError(f"Integers cannot have precision. Please use the integer data type directly instead of {gene_type}.") - elif type(gene_type) in [list, tuple, numpy.ndarray]: - # Get the number of genes before validating the num_genes parameter. - if num_genes is None: - if initial_population is None: - self.valid_parameters = False - raise TypeError("When the parameter 'initial_population' is None, then the 2 parameters 'sol_per_pop' and 'num_genes' cannot be None too.") - elif not len(gene_type) == len(initial_population[0]): - self.valid_parameters = False - raise ValueError(f"When the parameter 'gene_type' is nested, then it can be either [float, int] or with length equal to the number of genes parameter. Instead, value {gene_type} with len(gene_type) ({len(gene_type)}) != number of genes ({len(initial_population[0])}) found.") - elif not len(gene_type) == num_genes: - self.valid_parameters = False - raise ValueError(f"When the parameter 'gene_type' is nested, then it can be either [float, int] or with length equal to the value passed to the 'num_genes' parameter. Instead, value {gene_type} with len(gene_type) ({len(gene_type)}) != len(num_genes) ({num_genes}) found.") - for gene_type_idx, gene_type_val in enumerate(gene_type): - if gene_type_val in GA.supported_int_float_types: - # If the gene type is float and no precision is passed or an integer, set its precision to None. - gene_type[gene_type_idx] = [gene_type_val, None] - elif type(gene_type_val) in [list, tuple, numpy.ndarray]: - # A float type is expected in a list/tuple/numpy.ndarray of length 2. - if len(gene_type_val) == 2: - if gene_type_val[0] in GA.supported_float_types: - if type(gene_type_val[1]) in GA.supported_int_types: - pass - else: - self.valid_parameters = False - raise TypeError(f"In the 'gene_type' parameter, the precision for float gene data types must be an integer but the element {gene_type_val} at index {gene_type_idx} has a precision of {gene_type_val[1]} with type {gene_type_val[0]}.") - elif gene_type_val[0] in GA.supported_int_types: - if gene_type_val[1] is None: - pass - else: - self.valid_parameters = False - raise TypeError(f"In the 'gene_type' parameter, either do not set a precision for integer data types or set it to None. But the element {gene_type_val} at index {gene_type_idx} has a precision of {gene_type_val[1]} with type {gene_type_val[0]}.") - else: - self.valid_parameters = False - raise TypeError( - f"In the 'gene_type' parameter, a precision is expected only for float gene data types but the element {gene_type_val} found at index {gene_type_idx}.\nNote that the data type must be at index 0 of the item followed by precision at index 1.") - else: - self.valid_parameters = False - raise ValueError(f"In the 'gene_type' parameter, a precision is specified in a list/tuple/numpy.ndarray of length 2 but value ({gene_type_val}) of type {type(gene_type_val)} with length {len(gene_type_val)} found at index {gene_type_idx}.") - else: - self.valid_parameters = False - raise ValueError(f"When a list/tuple/numpy.ndarray is assigned to the 'gene_type' parameter, then its elements must be of integer, floating-point, list, tuple, or numpy.ndarray data types but the value ({gene_type_val}) of type {type(gene_type_val)} found at index {gene_type_idx}.") - self.gene_type = gene_type - self.gene_type_single = False - else: - self.valid_parameters = False - raise ValueError(f"The value passed to the 'gene_type' parameter must be either a single integer, floating-point, list, tuple, or numpy.ndarray but ({gene_type}) of type {type(gene_type)} found.") - - # Call the unpack_gene_space() method in the pygad.helper.unique.Unique class. - self.gene_space_unpacked = self.unpack_gene_space(range_min=self.init_range_low, - range_max=self.init_range_high) - - # Build the initial population - if initial_population is None: - if (sol_per_pop is None) or (num_genes is None): - self.valid_parameters = False - raise TypeError("Error creating the initial population:\n\nWhen the parameter 'initial_population' is None, then the 2 parameters 'sol_per_pop' and 'num_genes' cannot be None too.\nThere are 2 options to prepare the initial population:\n1) Assigning the initial population to the 'initial_population' parameter. In this case, the values of the 2 parameters sol_per_pop and num_genes will be deduced.\n2) Assign integer values to the 'sol_per_pop' and 'num_genes' parameters so that PyGAD can create the initial population automatically.") - elif (type(sol_per_pop) is int) and (type(num_genes) is int): - # Validating the number of solutions in the population (sol_per_pop) - if sol_per_pop <= 0: - self.valid_parameters = False - raise ValueError(f"The number of solutions in the population (sol_per_pop) must be > 0 but ({sol_per_pop}) found. \nThe following parameters must be > 0: \n1) Population size (i.e. number of solutions per population) (sol_per_pop).\n2) Number of selected parents in the mating pool (num_parents_mating).\n") - # Validating the number of gene. - if (num_genes <= 0): - self.valid_parameters = False - raise ValueError(f"The number of genes cannot be <= 0 but ({num_genes}) found.\n") - # When initial_population=None and the 2 parameters sol_per_pop and num_genes have valid integer values, then the initial population is created. - # Inside the initialize_population() method, the initial_population attribute is assigned to keep the initial population accessible. - self.num_genes = num_genes # Number of genes in the solution. - - # In case the 'gene_space' parameter is nested, then make sure the number of its elements equals to the number of genes. - if self.gene_space_nested: - if len(gene_space) != self.num_genes: - self.valid_parameters = False - raise ValueError(f"When the parameter 'gene_space' is nested, then its length must be equal to the value passed to the 'num_genes' parameter. Instead, length of gene_space ({len(gene_space)}) != num_genes ({self.num_genes})") - - # Number of solutions in the population. - self.sol_per_pop = sol_per_pop - self.initialize_population(allow_duplicate_genes=allow_duplicate_genes, - gene_type=self.gene_type, - gene_constraint=gene_constraint) - else: - self.valid_parameters = False - raise TypeError(f"The expected type of both the sol_per_pop and num_genes parameters is int but {type(sol_per_pop)} and {type(num_genes)} found.") - elif not type(initial_population) in [list, tuple, numpy.ndarray]: - self.valid_parameters = False - raise TypeError(f"The value assigned to the 'initial_population' parameter is expected to by of type list, tuple, or ndarray but {type(initial_population)} found.") - elif numpy.array(initial_population).ndim != 2: - self.valid_parameters = False - raise ValueError(f"A 2D list is expected to the initial_population parameter but a ({numpy.array(initial_population).ndim}-D) list found.") - else: - # Validate the type of each value in the 'initial_population' parameter. - for row_idx in range(len(initial_population)): - for col_idx in range(len(initial_population[0])): - if type(initial_population[row_idx][col_idx]) in GA.supported_int_float_types: - pass - else: - self.valid_parameters = False - raise TypeError(f"The values in the initial population can be integers or floats but the value ({initial_population[row_idx][col_idx]}) of type {type(initial_population[row_idx][col_idx])} found.") - - # Change the data type and round all genes within the initial population. - self.initial_population = self.change_population_dtype_and_round(initial_population) - - # Check if duplicates are allowed. If not, then solve any existing duplicates in the passed initial population. - if self.allow_duplicate_genes == False: - for initial_solution_idx, initial_solution in enumerate(self.initial_population): - if self.gene_space is None: - self.initial_population[initial_solution_idx], _, _ = self.solve_duplicate_genes_randomly(solution=initial_solution, - min_val=self.init_range_low, - max_val=self.init_range_high, - mutation_by_replacement=True, - gene_type=self.gene_type, - sample_size=self.sample_size) - else: - self.initial_population[initial_solution_idx], _, _ = self.solve_duplicate_genes_by_space(solution=initial_solution, - gene_type=self.gene_type, - sample_size=self.sample_size, - mutation_by_replacement=True, - build_initial_pop=True) - - # A NumPy array holding the initial population. - self.population = self.initial_population.copy() - # Number of genes in the solution. - self.num_genes = self.initial_population.shape[1] - # Number of solutions in the population. - self.sol_per_pop = self.initial_population.shape[0] - # The population size. - self.pop_size = (self.sol_per_pop, self.num_genes) - - # Change the data type and round all genes within the initial population. - self.initial_population = self.change_population_dtype_and_round(self.initial_population) - self.population = self.initial_population.copy() - - # In case the 'gene_space' parameter is nested, then make sure the number of its elements equals to the number of genes. - if self.gene_space_nested: - if len(gene_space) != self.num_genes: - self.valid_parameters = False - raise ValueError(f"When the parameter 'gene_space' is nested, then its length must be equal to the value passed to the 'num_genes' parameter. Instead, length of gene_space ({len(gene_space)}) != num_genes ({self.num_genes})") - - # Validate random_mutation_min_val and random_mutation_max_val - if type(random_mutation_min_val) in GA.supported_int_float_types: - if type(random_mutation_max_val) in GA.supported_int_float_types: - if random_mutation_min_val == random_mutation_max_val: - if not self.suppress_warnings: - warnings.warn("The values of the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val' are equal and this might cause a fixed mutation to some genes.") - else: - self.valid_parameters = False - raise TypeError(f"Type mismatch between the 2 parameters 'random_mutation_min_val' {type(random_mutation_min_val)} and 'random_mutation_max_val' {type(random_mutation_max_val)}.") - elif type(random_mutation_min_val) in [list, tuple, numpy.ndarray]: - if len(random_mutation_min_val) == self.num_genes: - pass - else: - self.valid_parameters = False - raise ValueError(f"The length of the 'random_mutation_min_val' parameter is {len(random_mutation_min_val)} which is different from the number of genes {self.num_genes}.") - if type(random_mutation_max_val) in [list, tuple, numpy.ndarray]: - if len(random_mutation_min_val) == len(random_mutation_max_val): - pass - else: - self.valid_parameters = False - raise ValueError(f"Size mismatch between the 2 parameters 'random_mutation_min_val' {len(random_mutation_min_val)} and 'random_mutation_max_val' {len(random_mutation_max_val)}.") - - # Validate the values in random_mutation_min_val - for val in random_mutation_min_val: - if type(val) in GA.supported_int_float_types: - pass - else: - self.valid_parameters = False - raise TypeError(f"When an iterable (list/tuple/numpy.ndarray) is assigned to the 'random_mutation_min_val' parameter, its elements must be numeric but the value {val} of type {type(val)} found.") - - # Validate the values in random_mutation_max_val - for val in random_mutation_max_val: - if type(val) in GA.supported_int_float_types: - pass - else: - self.valid_parameters = False - raise TypeError(f"When an iterable (list/tuple/numpy.ndarray) is assigned to the 'random_mutation_max_val' parameter, its elements must be numeric but the value {val} of type {type(val)} found.") - else: - self.valid_parameters = False - raise TypeError(f"Type mismatch between the 2 parameters 'random_mutation_min_val' {type(random_mutation_min_val)} and 'random_mutation_max_val' {type(random_mutation_max_val)}.") - else: - self.valid_parameters = False - raise TypeError(f"The expected type of the 'random_mutation_min_val' parameter is numeric or list/tuple/numpy.ndarray but {type(random_mutation_min_val)} found.") - - self.random_mutation_min_val = random_mutation_min_val - self.random_mutation_max_val = random_mutation_max_val - - # Validate that gene_constraint is a list or tuple and every element inside it is either None or callable. - if gene_constraint: - if type(gene_constraint) in [list, tuple]: - if len(gene_constraint) == self.num_genes: - for constraint_idx, item in enumerate(gene_constraint): - # Check whether the element is None or a callable. - if item is None: - pass - elif item and callable(item): - if item.__code__.co_argcount == 2: - # Every callable is valid if it receives 2 arguments. - # The 2 arguments: 1) solution 2) A list or numpy.ndarray of values to check if they meet the constraint. - pass - else: - self.valid_parameters = False - raise ValueError(f"Every callable inside the gene_constraint parameter must accept 2 arguments representing 1) The solution/chromosome where the gene exists 2) A list of NumPy array of values to check if they meet the constraint. But the callable at index {constraint_idx} named '{item.__code__.co_name}' accepts {item.__code__.co_argcount} argument(s).") - else: - self.valid_parameters = False - raise TypeError(f"The expected type of an element in the 'gene_constraint' parameter is None or a callable (e.g. function). But {item} at index {constraint_idx} of type {type(item)} found.") - else: - self.valid_parameters = False - raise ValueError(f"The number of constrains ({len(gene_constraint)}) in the 'gene_constraint' parameter must be equal to the number of genes ({self.num_genes}).") - else: - self.valid_parameters = False - raise TypeError(f"The expected type of the 'gene_constraint' parameter is either a list or tuple. But the value {gene_constraint} of type {type(gene_constraint)} found.") - else: - # gene_constraint is None and not used. - pass - - self.gene_constraint = gene_constraint - - # Validating the number of parents to be selected for mating (num_parents_mating) - if num_parents_mating <= 0: - self.valid_parameters = False - raise ValueError(f"The number of parents mating (num_parents_mating) parameter must be > 0 but ({num_parents_mating}) found. \nThe following parameters must be > 0: \n1) Population size (i.e. number of solutions per population) (sol_per_pop).\n2) Number of selected parents in the mating pool (num_parents_mating).\n") - - # Validating the number of parents to be selected for mating: num_parents_mating - if num_parents_mating > self.sol_per_pop: - self.valid_parameters = False - raise ValueError(f"The number of parents to select for mating ({num_parents_mating}) cannot be greater than the number of solutions in the population ({self.sol_per_pop}) (i.e., num_parents_mating must always be <= sol_per_pop).\n") - - self.num_parents_mating = num_parents_mating - - # crossover: Refers to the method that applies the crossover operator based on the selected type of crossover in the crossover_type property. - # Validating the crossover type: crossover_type - if crossover_type is None: - self.crossover = None - elif inspect.ismethod(crossover_type): - # Check if the crossover_type is a method that accepts 4 parameters. - if crossover_type.__code__.co_argcount == 4: - # The crossover method assigned to the crossover_type parameter is validated. - self.crossover = crossover_type - else: - self.valid_parameters = False - raise ValueError(f"When 'crossover_type' is assigned to a method, then this crossover method must accept 4 parameters:\n1) Expected to be the 'self' object.\n2) The selected parents.\n3) The size of the offspring to be produced.\n4) The instance from the pygad.GA class.\n\nThe passed crossover method named '{crossover_type.__code__.co_name}' accepts {crossover_type.__code__.co_argcount} parameter(s).") - elif callable(crossover_type): - # Check if the crossover_type is a function that accepts 2 parameters. - if crossover_type.__code__.co_argcount == 3: - # The crossover function assigned to the crossover_type parameter is validated. - self.crossover = crossover_type - else: - self.valid_parameters = False - raise ValueError(f"When 'crossover_type' is assigned to a function, then this crossover function must accept 3 parameters:\n1) The selected parents.\n2) The size of the offspring to be produced.3) The instance from the pygad.GA class to retrieve any property like population, gene data type, gene space, etc.\n\nThe passed crossover function named '{crossover_type.__code__.co_name}' accepts {crossover_type.__code__.co_argcount} parameter(s).") - elif not (type(crossover_type) is str): - self.valid_parameters = False - raise TypeError(f"The expected type of the 'crossover_type' parameter is either callable or str but {type(crossover_type)} found.") - else: # type crossover_type is str - crossover_type = crossover_type.lower() - if crossover_type == "single_point": - self.crossover = self.single_point_crossover - elif crossover_type == "two_points": - self.crossover = self.two_points_crossover - elif crossover_type == "uniform": - self.crossover = self.uniform_crossover - elif crossover_type == "scattered": - self.crossover = self.scattered_crossover - else: - self.valid_parameters = False - raise TypeError(f"Undefined crossover type. \nThe assigned value to the crossover_type ({crossover_type}) parameter does not refer to one of the supported crossover types which are: \n-single_point (for single point crossover)\n-two_points (for two points crossover)\n-uniform (for uniform crossover)\n-scattered (for scattered crossover).\n") - - self.crossover_type = crossover_type - - # Calculate the value of crossover_probability - if crossover_probability is None: - self.crossover_probability = None - elif type(crossover_probability) in GA.supported_int_float_types: - if 0 <= crossover_probability <= 1: - self.crossover_probability = crossover_probability - else: - self.valid_parameters = False - raise ValueError(f"The value assigned to the 'crossover_probability' parameter must be between 0 and 1 inclusive but ({crossover_probability}) found.") - else: - self.valid_parameters = False - raise TypeError(f"Unexpected type for the 'crossover_probability' parameter. Float is expected but ({crossover_probability}) of type {type(crossover_probability)} found.") - - # mutation: Refers to the method that applies the mutation operator based on the selected type of mutation in the mutation_type property. - # Validating the mutation type: mutation_type - # "adaptive" mutation is supported starting from PyGAD 2.10.0 - if mutation_type is None: - self.mutation = None - elif inspect.ismethod(mutation_type): - # Check if the mutation_type is a method that accepts 3 parameters. - if (mutation_type.__code__.co_argcount == 3): - # The mutation method assigned to the mutation_type parameter is validated. - self.mutation = mutation_type - else: - self.valid_parameters = False - raise ValueError(f"When 'mutation_type' is assigned to a method, then it must accept 3 parameters:\n1) Expected to be the 'self' object.\n2) The offspring to be mutated.\n3) The instance from the pygad.GA class.\n\nThe passed mutation method named '{mutation_type.__code__.co_name}' accepts {mutation_type.__code__.co_argcount} parameter(s).") - elif callable(mutation_type): - # Check if the mutation_type is a function that accepts 2 parameters. - if (mutation_type.__code__.co_argcount == 2): - # The mutation function assigned to the mutation_type parameter is validated. - self.mutation = mutation_type - else: - self.valid_parameters = False - raise ValueError(f"When 'mutation_type' is assigned to a function, then this mutation function must accept 2 parameters:\n1) The offspring to be mutated.\n2) The instance from the pygad.GA class to retrieve any property like population, gene data type, gene space, etc.\n\nThe passed mutation function named '{mutation_type.__code__.co_name}' accepts {mutation_type.__code__.co_argcount} parameter(s).") - elif not (type(mutation_type) is str): - self.valid_parameters = False - raise TypeError(f"The expected type of the 'mutation_type' parameter is either callable or str but {type(mutation_type)} found.") - else: # type mutation_type is str - mutation_type = mutation_type.lower() - if mutation_type == "random": - self.mutation = self.random_mutation - elif mutation_type == "swap": - self.mutation = self.swap_mutation - elif mutation_type == "scramble": - self.mutation = self.scramble_mutation - elif mutation_type == "inversion": - self.mutation = self.inversion_mutation - elif mutation_type == "adaptive": - self.mutation = self.adaptive_mutation - else: - self.valid_parameters = False - raise TypeError(f"Undefined mutation type. \nThe assigned string value to the 'mutation_type' parameter ({mutation_type}) does not refer to one of the supported mutation types which are: \n-random (for random mutation)\n-swap (for swap mutation)\n-inversion (for inversion mutation)\n-scramble (for scramble mutation)\n-adaptive (for adaptive mutation).\n") - - self.mutation_type = mutation_type - - # Calculate the value of mutation_probability - if not (self.mutation_type is None): - if mutation_probability is None: - self.mutation_probability = None - elif mutation_type != "adaptive": - # Mutation probability is fixed not adaptive. - if type(mutation_probability) in GA.supported_int_float_types: - if 0 <= mutation_probability <= 1: - self.mutation_probability = mutation_probability - else: - self.valid_parameters = False - raise ValueError(f"The value assigned to the 'mutation_probability' parameter must be between 0 and 1 inclusive but ({mutation_probability}) found.") - else: - self.valid_parameters = False - raise TypeError(f"Unexpected type for the 'mutation_probability' parameter. A numeric value is expected but ({mutation_probability}) of type {type(mutation_probability)} found.") - else: - # Mutation probability is adaptive not fixed. - if type(mutation_probability) in [list, tuple, numpy.ndarray]: - if len(mutation_probability) == 2: - for el in mutation_probability: - if type(el) in GA.supported_int_float_types: - if 0 <= el <= 1: - pass - else: - self.valid_parameters = False - raise ValueError(f"The values assigned to the 'mutation_probability' parameter must be between 0 and 1 inclusive but ({el}) found.") - else: - self.valid_parameters = False - raise TypeError(f"Unexpected type for a value assigned to the 'mutation_probability' parameter. A numeric value is expected but ({el}) of type {type(el)} found.") - if mutation_probability[0] < mutation_probability[1]: - if not self.suppress_warnings: - warnings.warn(f"The first element in the 'mutation_probability' parameter is {mutation_probability[0]} which is smaller than the second element {mutation_probability[1]}. This means the mutation rate for the high-quality solutions is higher than the mutation rate of the low-quality ones. This causes high disruption in the high quality solutions while making little changes in the low quality solutions. Please make the first element higher than the second element.") - self.mutation_probability = mutation_probability - else: - self.valid_parameters = False - raise ValueError(f"When mutation_type='adaptive', then the 'mutation_probability' parameter must have only 2 elements but ({len(mutation_probability)}) element(s) found.") - else: - self.valid_parameters = False - raise TypeError(f"Unexpected type for the 'mutation_probability' parameter. When mutation_type='adaptive', then list/tuple/numpy.ndarray is expected but ({mutation_probability}) of type {type(mutation_probability)} found.") - else: - pass - - # Calculate the value of mutation_num_genes - if not (self.mutation_type is None): - if mutation_num_genes is None: - # The mutation_num_genes parameter does not exist. Checking whether adaptive mutation is used. - if mutation_type != "adaptive": - # The percent of genes to mutate is fixed not adaptive. - if mutation_percent_genes == 'default'.lower(): - mutation_percent_genes = 10 - # Based on the mutation percentage in the 'mutation_percent_genes' parameter, the number of genes to mutate is calculated. - mutation_num_genes = numpy.uint32( - (mutation_percent_genes*self.num_genes)/100) - # Based on the mutation percentage of genes, if the number of selected genes for mutation is less than the least possible value which is 1, then the number will be set to 1. - if mutation_num_genes == 0: - if self.mutation_probability is None: - if not self.suppress_warnings: - warnings.warn( - f"The percentage of genes to mutate (mutation_percent_genes={mutation_percent_genes}) resulted in selecting ({mutation_num_genes}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.") - mutation_num_genes = 1 - - elif type(mutation_percent_genes) in GA.supported_int_float_types: - if mutation_percent_genes <= 0 or mutation_percent_genes > 100: - self.valid_parameters = False - raise ValueError(f"The percentage of selected genes for mutation (mutation_percent_genes) must be > 0 and <= 100 but ({mutation_percent_genes}) found.\n") - else: - # If mutation_percent_genes equals the string "default", then it is replaced by the numeric value 10. - if mutation_percent_genes == 'default'.lower(): - mutation_percent_genes = 10 - - # Based on the mutation percentage in the 'mutation_percent_genes' parameter, the number of genes to mutate is calculated. - mutation_num_genes = numpy.uint32( - (mutation_percent_genes*self.num_genes)/100) - # Based on the mutation percentage of genes, if the number of selected genes for mutation is less than the least possible value which is 1, then the number will be set to 1. - if mutation_num_genes == 0: - if self.mutation_probability is None: - if not self.suppress_warnings: - warnings.warn(f"The percentage of genes to mutate (mutation_percent_genes={mutation_percent_genes}) resulted in selecting ({mutation_num_genes}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.") - mutation_num_genes = 1 - else: - self.valid_parameters = False - raise TypeError(f"Unexpected value or type of the 'mutation_percent_genes' parameter. It only accepts the string 'default' or a numeric value but ({mutation_percent_genes}) of type {type(mutation_percent_genes)} found.") - else: - # The percent of genes to mutate is adaptive not fixed. - if type(mutation_percent_genes) in [list, tuple, numpy.ndarray]: - if len(mutation_percent_genes) == 2: - mutation_num_genes = numpy.zeros_like( - mutation_percent_genes, dtype=numpy.uint32) - for idx, el in enumerate(mutation_percent_genes): - if type(el) in GA.supported_int_float_types: - if el <= 0 or el > 100: - self.valid_parameters = False - raise ValueError(f"The values assigned to the 'mutation_percent_genes' must be > 0 and <= 100 but ({mutation_percent_genes}) found.\n") - else: - self.valid_parameters = False - raise TypeError(f"Unexpected type for a value assigned to the 'mutation_percent_genes' parameter. An integer value is expected but ({el}) of type {type(el)} found.") - # At this point of the loop, the current value assigned to the parameter 'mutation_percent_genes' is validated. - # Based on the mutation percentage in the 'mutation_percent_genes' parameter, the number of genes to mutate is calculated. - mutation_num_genes[idx] = numpy.uint32( - (mutation_percent_genes[idx]*self.num_genes)/100) - # Based on the mutation percentage of genes, if the number of selected genes for mutation is less than the least possible value which is 1, then the number will be set to 1. - if mutation_num_genes[idx] == 0: - if not self.suppress_warnings: - warnings.warn(f"The percentage of genes to mutate ({mutation_percent_genes[idx]}) resulted in selecting ({mutation_num_genes[idx]}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.") - mutation_num_genes[idx] = 1 - if mutation_percent_genes[0] < mutation_percent_genes[1]: - if not self.suppress_warnings: - warnings.warn(f"The first element in the 'mutation_percent_genes' parameter is ({mutation_percent_genes[0]}) which is smaller than the second element ({mutation_percent_genes[1]}).\nThis means the mutation rate for the high-quality solutions is higher than the mutation rate of the low-quality ones. This causes high disruption in the high quality solutions while making little changes in the low quality solutions.\nPlease make the first element higher than the second element.") - # At this point outside the loop, all values of the parameter 'mutation_percent_genes' are validated. Everything is OK. - else: - self.valid_parameters = False - raise ValueError(f"When mutation_type='adaptive', then the 'mutation_percent_genes' parameter must have only 2 elements but ({len(mutation_percent_genes)}) element(s) found.") - else: - if self.mutation_probability is None: - self.valid_parameters = False - raise TypeError(f"Unexpected type of the 'mutation_percent_genes' parameter. When mutation_type='adaptive', then the 'mutation_percent_genes' parameter should exist and assigned a list/tuple/numpy.ndarray with 2 values but ({mutation_percent_genes}) found.") - # The mutation_num_genes parameter exists. Checking whether adaptive mutation is used. - elif mutation_type != "adaptive": - # Number of genes to mutate is fixed not adaptive. - if type(mutation_num_genes) in GA.supported_int_types: - if mutation_num_genes <= 0: - self.valid_parameters = False - raise ValueError(f"The number of selected genes for mutation (mutation_num_genes) cannot be <= 0 but ({mutation_num_genes}) found. If you do not want to use mutation, please set mutation_type=None\n") - elif mutation_num_genes > self.num_genes: - self.valid_parameters = False - raise ValueError(f"The number of selected genes for mutation (mutation_num_genes), which is ({mutation_num_genes}), cannot be greater than the number of genes ({self.num_genes}).\n") - else: - self.valid_parameters = False - raise TypeError(f"The 'mutation_num_genes' parameter is expected to be a positive integer but the value ({mutation_num_genes}) of type {type(mutation_num_genes)} found.\n") - else: - # Number of genes to mutate is adaptive not fixed. - if type(mutation_num_genes) in [list, tuple, numpy.ndarray]: - if len(mutation_num_genes) == 2: - for el in mutation_num_genes: - if type(el) in GA.supported_int_types: - if el <= 0: - self.valid_parameters = False - raise ValueError(f"The values assigned to the 'mutation_num_genes' cannot be <= 0 but ({el}) found. If you do not want to use mutation, please set mutation_type=None\n") - elif el > self.num_genes: - self.valid_parameters = False - raise ValueError(f"The values assigned to the 'mutation_num_genes' cannot be greater than the number of genes ({self.num_genes}) but ({el}) found.\n") - else: - self.valid_parameters = False - raise TypeError(f"Unexpected type for a value assigned to the 'mutation_num_genes' parameter. An integer value is expected but ({el}) of type {type(el)} found.") - # At this point of the loop, the current value assigned to the parameter 'mutation_num_genes' is validated. - if mutation_num_genes[0] < mutation_num_genes[1]: - if not self.suppress_warnings: - warnings.warn(f"The first element in the 'mutation_num_genes' parameter is {mutation_num_genes[0]} which is smaller than the second element {mutation_num_genes[1]}. This means the mutation rate for the high-quality solutions is higher than the mutation rate of the low-quality ones. This causes high disruption in the high quality solutions while making little changes in the low quality solutions. Please make the first element higher than the second element.") - # At this point outside the loop, all values of the parameter 'mutation_num_genes' are validated. Everything is OK. - else: - self.valid_parameters = False - raise ValueError(f"When mutation_type='adaptive', then the 'mutation_num_genes' parameter must have only 2 elements but ({len(mutation_num_genes)}) element(s) found.") - else: - self.valid_parameters = False - raise TypeError(f"Unexpected type for the 'mutation_num_genes' parameter. When mutation_type='adaptive', then list/tuple/numpy.ndarray is expected but ({mutation_num_genes}) of type {type(mutation_num_genes)} found.") - else: - pass - - # Validating mutation_by_replacement and mutation_type - if self.mutation_type != "random" and self.mutation_by_replacement: - if not self.suppress_warnings: - warnings.warn(f"The mutation_by_replacement parameter is set to True while the mutation_type parameter is not set to random but ({mutation_type}). Note that the mutation_by_replacement parameter has an effect only when mutation_type='random'.") - - # Check if crossover and mutation are both disabled. - if (self.mutation_type is None) and (self.crossover_type is None): - if not self.suppress_warnings: - warnings.warn("The 2 parameters mutation_type and crossover_type are None. This disables any type of evolution the genetic algorithm can make. As a result, the genetic algorithm cannot find a better solution that the best solution in the initial population.") - - # select_parents: Refers to a method that selects the parents based on the parent selection type specified in the parent_selection_type attribute. - # Validating the selected type of parent selection: parent_selection_type - if inspect.ismethod(parent_selection_type): - # Check if the parent_selection_type is a method that accepts 4 parameters. - if parent_selection_type.__code__.co_argcount == 4: - # population: Added in PyGAD 2.16.0. It should use only to support custom parent selection functions. Otherwise, it should be left to None to retrieve the population by self.population. - # The parent selection method assigned to the parent_selection_type parameter is validated. - self.select_parents = parent_selection_type - else: - self.valid_parameters = False - raise ValueError(f"When 'parent_selection_type' is assigned to a method, then it must accept 4 parameters:\n1) Expected to be the 'self' object.\n2) The fitness values of the current population.\n3) The number of parents needed.\n4) The instance from the pygad.GA class.\n\nThe passed parent selection method named '{parent_selection_type.__code__.co_name}' accepts {parent_selection_type.__code__.co_argcount} parameter(s).") - elif callable(parent_selection_type): - # Check if the parent_selection_type is a function that accepts 3 parameters. - if parent_selection_type.__code__.co_argcount == 3: - # population: Added in PyGAD 2.16.0. It should use only to support custom parent selection functions. Otherwise, it should be left to None to retrieve the population by self.population. - # The parent selection function assigned to the parent_selection_type parameter is validated. - self.select_parents = parent_selection_type - else: - self.valid_parameters = False - raise ValueError(f"When 'parent_selection_type' is assigned to a user-defined function, then this parent selection function must accept 3 parameters:\n1) The fitness values of the current population.\n2) The number of parents needed.\n3) The instance from the pygad.GA class to retrieve any property like population, gene data type, gene space, etc.\n\nThe passed parent selection function named '{parent_selection_type.__code__.co_name}' accepts {parent_selection_type.__code__.co_argcount} parameter(s).") - elif not (type(parent_selection_type) is str): - self.valid_parameters = False - - raise TypeError(f"The expected type of the 'parent_selection_type' parameter is either callable or str but {type(parent_selection_type)} found.") - else: - parent_selection_type = parent_selection_type.lower() - if parent_selection_type == "sss": - self.select_parents = self.steady_state_selection - elif parent_selection_type == "rws": - self.select_parents = self.roulette_wheel_selection - elif parent_selection_type == "sus": - self.select_parents = self.stochastic_universal_selection - elif parent_selection_type == "random": - self.select_parents = self.random_selection - elif parent_selection_type == "tournament": - self.select_parents = self.tournament_selection - elif parent_selection_type == "tournament_nsga2": # Supported in PyGAD >= 3.2 - self.select_parents = self.tournament_selection_nsga2 - elif parent_selection_type == "nsga2": # Supported in PyGAD >= 3.2 - self.select_parents = self.nsga2_selection - elif parent_selection_type == "rank": - self.select_parents = self.rank_selection - else: - self.valid_parameters = False - raise TypeError(f"Undefined parent selection type: {parent_selection_type}. \nThe assigned value to the 'parent_selection_type' parameter does not refer to one of the supported parent selection techniques which are: \n-sss (steady state selection)\n-rws (roulette wheel selection)\n-sus (stochastic universal selection)\n-rank (rank selection)\n-random (random selection)\n-tournament (tournament selection)\n-tournament_nsga2: (Tournament selection for NSGA-II)\n-nsga2: (NSGA-II parent selection).\n") - - # For tournament selection, validate the K value. - if parent_selection_type == "tournament": - if type(K_tournament) in GA.supported_int_types: - if K_tournament > self.sol_per_pop: - K_tournament = self.sol_per_pop - if not self.suppress_warnings: - warnings.warn(f"K of the tournament selection ({K_tournament}) should not be greater than the number of solutions within the population ({self.sol_per_pop}).\nK will be clipped to be equal to the number of solutions in the population (sol_per_pop).\n") - elif K_tournament <= 0: - self.valid_parameters = False - raise ValueError(f"K of the tournament selection cannot be <=0 but ({K_tournament}) found.\n") - else: - self.valid_parameters = False - raise ValueError(f"The type of K of the tournament selection must be integer but the value ({K_tournament}) of type ({type(K_tournament)}) found.") - - self.K_tournament = K_tournament - - # Validating the number of parents to keep in the next population: keep_parents - if not (type(keep_parents) in GA.supported_int_types): - self.valid_parameters = False - raise TypeError(f"Incorrect type of the value assigned to the keep_parents parameter. The value ({keep_parents}) of type {type(keep_parents)} found but an integer is expected.") - elif keep_parents > self.sol_per_pop or keep_parents > self.num_parents_mating or keep_parents < -1: - self.valid_parameters = False - raise ValueError(f"Incorrect value to the keep_parents parameter: {keep_parents}. \nThe assigned value to the keep_parent parameter must satisfy the following conditions: \n1) Less than or equal to sol_per_pop\n2) Less than or equal to num_parents_mating\n3) Greater than or equal to -1.") - - self.keep_parents = keep_parents - - if parent_selection_type == "sss" and self.keep_parents == 0: - if not self.suppress_warnings: - warnings.warn("The steady-state parent (sss) selection operator is used despite that no parents are kept in the next generation.") - - # Validating the number of elitism to keep in the next population: keep_elitism - if not (type(keep_elitism) in GA.supported_int_types): - self.valid_parameters = False - raise TypeError(f"Incorrect type of the value assigned to the keep_elitism parameter. The value ({keep_elitism}) of type {type(keep_elitism)} found but an integer is expected.") - elif keep_elitism > self.sol_per_pop or keep_elitism < 0: - self.valid_parameters = False - raise ValueError(f"Incorrect value to the keep_elitism parameter: {keep_elitism}. \nThe assigned value to the keep_elitism parameter must satisfy the following conditions: \n1) Less than or equal to sol_per_pop\n2) Greater than or equal to 0.") - - self.keep_elitism = keep_elitism - - # Validate keep_parents. - if self.keep_elitism == 0: - # Keep all parents in the next population. - if self.keep_parents == -1: - self.num_offspring = self.sol_per_pop - self.num_parents_mating - # Keep no parents in the next population. - elif self.keep_parents == 0: - self.num_offspring = self.sol_per_pop - # Keep the specified number of parents in the next population. - elif self.keep_parents > 0: - self.num_offspring = self.sol_per_pop - self.keep_parents - else: - self.num_offspring = self.sol_per_pop - self.keep_elitism - - # Check if the fitness_func is a method. - # In PyGAD 2.19.0, a method can be passed to the fitness function. If function is passed, then it accepts 2 parameters. If method, then it accepts 3 parameters. - # In PyGAD 2.20.0, a new parameter is passed referring to the instance of the `pygad.GA` class. So, the function accepts 3 parameters and the method accepts 4 parameters. - if inspect.ismethod(fitness_func): - # If the fitness is calculated through a method, not a function, then there is a fourth 'self` parameters. - if fitness_func.__code__.co_argcount == 4: - self.fitness_func = fitness_func - else: - self.valid_parameters = False - raise ValueError(f"In PyGAD 2.20.0, if a method is used to calculate the fitness value, then it must accept 4 parameters\n1) Expected to be the 'self' object.\n2) The instance of the 'pygad.GA' class.\n3) A solution to calculate its fitness value.\n4) The solution's index within the population.\n\nThe passed fitness method named '{fitness_func.__code__.co_name}' accepts {fitness_func.__code__.co_argcount} parameter(s).") - elif callable(fitness_func): - # Check if the fitness function accepts 2 parameters. - if fitness_func.__code__.co_argcount == 3: - self.fitness_func = fitness_func - else: - self.valid_parameters = False - raise ValueError(f"In PyGAD 2.20.0, the fitness function must accept 3 parameters:\n1) The instance of the 'pygad.GA' class.\n2) A solution to calculate its fitness value.\n3) The solution's index within the population.\n\nThe passed fitness function named '{fitness_func.__code__.co_name}' accepts {fitness_func.__code__.co_argcount} parameter(s).") - else: - self.valid_parameters = False - - raise TypeError(f"The value assigned to the fitness_func parameter is expected to be of type function but {type(fitness_func)} found.") - - if fitness_batch_size is None: - pass - elif not (type(fitness_batch_size) in GA.supported_int_types): - self.valid_parameters = False - raise TypeError(f"The value assigned to the fitness_batch_size parameter is expected to be integer but the value ({fitness_batch_size}) of type {type(fitness_batch_size)} found.") - elif fitness_batch_size <= 0 or fitness_batch_size > self.sol_per_pop: - self.valid_parameters = False - raise ValueError(f"The value assigned to the fitness_batch_size parameter must be:\n1) Greater than 0.\n2) Less than or equal to sol_per_pop ({self.sol_per_pop}).\nBut the value ({fitness_batch_size}) found.") - - self.fitness_batch_size = fitness_batch_size - - # Check if the on_start exists. - if not (on_start is None): - if inspect.ismethod(on_start): - # Check if the on_start method accepts 2 parameters. - if on_start.__code__.co_argcount == 2: - self.on_start = on_start - else: - self.valid_parameters = False - raise ValueError(f"The method assigned to the on_start parameter must accept only 2 parameters:\n1) Expected to be the 'self' object.\n2) The instance of the genetic algorithm.\nThe passed method named '{on_start.__code__.co_name}' accepts {on_start.__code__.co_argcount} parameter(s).") - # Check if the on_start is a function. - elif callable(on_start): - # Check if the on_start function accepts only a single parameter. - if on_start.__code__.co_argcount == 1: - self.on_start = on_start - else: - self.valid_parameters = False - raise ValueError(f"The function assigned to the on_start parameter must accept only 1 parameter representing the instance of the genetic algorithm.\nThe passed function named '{on_start.__code__.co_name}' accepts {on_start.__code__.co_argcount} parameter(s).") - else: - self.valid_parameters = False - - raise TypeError(f"The value assigned to the on_start parameter is expected to be of type function but {type(on_start)} found.") - else: - self.on_start = None - - # Check if the on_fitness exists. - if not (on_fitness is None): - # Check if the on_fitness is a method. - if inspect.ismethod(on_fitness): - # Check if the on_fitness method accepts 3 parameters. - if on_fitness.__code__.co_argcount == 3: - self.on_fitness = on_fitness - else: - self.valid_parameters = False - raise ValueError(f"The method assigned to the on_fitness parameter must accept 3 parameters:\n1) Expected to be the 'self' object.\n2) The instance of the genetic algorithm.3) The fitness values of all solutions.\nThe passed method named '{on_fitness.__code__.co_name}' accepts {on_fitness.__code__.co_argcount} parameter(s).") - # Check if the on_fitness is a function. - elif callable(on_fitness): - # Check if the on_fitness function accepts 2 parameters. - if on_fitness.__code__.co_argcount == 2: - self.on_fitness = on_fitness - else: - self.valid_parameters = False - raise ValueError(f"The function assigned to the on_fitness parameter must accept 2 parameters representing the instance of the genetic algorithm and the fitness values of all solutions.\nThe passed function named '{on_fitness.__code__.co_name}' accepts {on_fitness.__code__.co_argcount} parameter(s).") - else: - self.valid_parameters = False - raise TypeError(f"The value assigned to the on_fitness parameter is expected to be of type function but {type(on_fitness)} found.") - else: - self.on_fitness = None - - # Check if the on_parents exists. - if not (on_parents is None): - # Check if the on_parents is a method. - if inspect.ismethod(on_parents): - # Check if the on_parents method accepts 3 parameters. - if on_parents.__code__.co_argcount == 3: - self.on_parents = on_parents - else: - self.valid_parameters = False - raise ValueError(f"The method assigned to the on_parents parameter must accept 3 parameters:\n1) Expected to be the 'self' object.\n2) The instance of the genetic algorithm.\n3) The fitness values of all solutions.\nThe passed method named '{on_parents.__code__.co_name}' accepts {on_parents.__code__.co_argcount} parameter(s).") - # Check if the on_parents is a function. - elif callable(on_parents): - # Check if the on_parents function accepts 2 parameters. - if on_parents.__code__.co_argcount == 2: - self.on_parents = on_parents - else: - self.valid_parameters = False - raise ValueError(f"The function assigned to the on_parents parameter must accept 2 parameters representing the instance of the genetic algorithm and the fitness values of all solutions.\nThe passed function named '{on_parents.__code__.co_name}' accepts {on_parents.__code__.co_argcount} parameter(s).") - else: - self.valid_parameters = False - raise TypeError(f"The value assigned to the on_parents parameter is expected to be of type function but {type(on_parents)} found.") - else: - self.on_parents = None - - # Check if the on_crossover exists. - if not (on_crossover is None): - # Check if the on_crossover is a method. - if inspect.ismethod(on_crossover): - # Check if the on_crossover method accepts 3 parameters. - if on_crossover.__code__.co_argcount == 3: - self.on_crossover = on_crossover - else: - self.valid_parameters = False - raise ValueError(f"The method assigned to the on_crossover parameter must accept 3 parameters:\n1) Expected to be the 'self' object.\n2) The instance of the genetic algorithm.\n2) The offspring generated using crossover.\nThe passed method named '{on_crossover.__code__.co_name}' accepts {on_crossover.__code__.co_argcount} parameter(s).") - # Check if the on_crossover is a function. - elif callable(on_crossover): - # Check if the on_crossover function accepts 2 parameters. - if on_crossover.__code__.co_argcount == 2: - self.on_crossover = on_crossover - else: - self.valid_parameters = False - raise ValueError(f"The function assigned to the on_crossover parameter must accept 2 parameters representing the instance of the genetic algorithm and the offspring generated using crossover.\nThe passed function named '{on_crossover.__code__.co_name}' accepts {on_crossover.__code__.co_argcount} parameter(s).") - else: - self.valid_parameters = False - raise TypeError(f"The value assigned to the on_crossover parameter is expected to be of type function but {type(on_crossover)} found.") - else: - self.on_crossover = None - - # Check if the on_mutation exists. - if not (on_mutation is None): - # Check if the on_mutation is a method. - if inspect.ismethod(on_mutation): - # Check if the on_mutation method accepts 3 parameters. - if on_mutation.__code__.co_argcount == 3: - self.on_mutation = on_mutation - else: - self.valid_parameters = False - raise ValueError(f"The method assigned to the on_mutation parameter must accept 3 parameters:\n1) Expected to be the 'self' object.\n2) The instance of the genetic algorithm.\n2) The offspring after applying the mutation operation.\nThe passed method named '{on_mutation.__code__.co_name}' accepts {on_mutation.__code__.co_argcount} parameter(s).") - # Check if the on_mutation is a function. - elif callable(on_mutation): - # Check if the on_mutation function accepts 2 parameters. - if on_mutation.__code__.co_argcount == 2: - self.on_mutation = on_mutation - else: - self.valid_parameters = False - raise ValueError(f"The function assigned to the on_mutation parameter must accept 2 parameters representing the instance of the genetic algorithm and the offspring after applying the mutation operation.\nThe passed function named '{on_mutation.__code__.co_name}' accepts {on_mutation.__code__.co_argcount} parameter(s).") - else: - self.valid_parameters = False - raise TypeError(f"The value assigned to the on_mutation parameter is expected to be of type function but {type(on_mutation)} found.") - else: - self.on_mutation = None - - # Check if the on_generation exists. - if not (on_generation is None): - # Check if the on_generation is a method. - if inspect.ismethod(on_generation): - # Check if the on_generation method accepts 2 parameters. - if on_generation.__code__.co_argcount == 2: - self.on_generation = on_generation - else: - self.valid_parameters = False - raise ValueError(f"The method assigned to the on_generation parameter must accept 2 parameters:\n1) Expected to be the 'self' object.\n2) The instance of the genetic algorithm.\nThe passed method named '{on_generation.__code__.co_name}' accepts {on_generation.__code__.co_argcount} parameter(s).") - # Check if the on_generation is a function. - elif callable(on_generation): - # Check if the on_generation function accepts only a single parameter. - if on_generation.__code__.co_argcount == 1: - self.on_generation = on_generation - else: - self.valid_parameters = False - raise ValueError(f"The function assigned to the on_generation parameter must accept only 1 parameter representing the instance of the genetic algorithm.\nThe passed function named '{on_generation.__code__.co_name}' accepts {on_generation.__code__.co_argcount} parameter(s).") - else: - self.valid_parameters = False - raise TypeError(f"The value assigned to the on_generation parameter is expected to be of type function but {type(on_generation)} found.") - else: - self.on_generation = None - - # Check if the on_stop exists. - if not (on_stop is None): - # Check if the on_stop is a method. - if inspect.ismethod(on_stop): - # Check if the on_stop method accepts 3 parameters. - if on_stop.__code__.co_argcount == 3: - self.on_stop = on_stop - else: - self.valid_parameters = False - raise ValueError(f"The method assigned to the on_stop parameter must accept 3 parameters:\n1) Expected to be the 'self' object.\n2) The instance of the genetic algorithm.\n2) A list of the fitness values of the solutions in the last population.\nThe passed method named '{on_stop.__code__.co_name}' accepts {on_stop.__code__.co_argcount} parameter(s).") - # Check if the on_stop is a function. - elif callable(on_stop): - # Check if the on_stop function accepts 2 parameters. - if on_stop.__code__.co_argcount == 2: - self.on_stop = on_stop - else: - self.valid_parameters = False - raise ValueError(f"The function assigned to the on_stop parameter must accept 2 parameters representing the instance of the genetic algorithm and a list of the fitness values of the solutions in the last population.\nThe passed function named '{on_stop.__code__.co_name}' accepts {on_stop.__code__.co_argcount} parameter(s).") - else: - self.valid_parameters = False - raise TypeError(f"The value assigned to the 'on_stop' parameter is expected to be of type function but {type(on_stop)} found.") - else: - self.on_stop = None - - # Validate save_best_solutions - if type(save_best_solutions) is bool: - if save_best_solutions == True: - if not self.suppress_warnings: - warnings.warn("Use the 'save_best_solutions' parameter with caution as it may cause memory overflow when either the number of generations or number of genes is large.") - else: - self.valid_parameters = False - raise TypeError(f"The value passed to the 'save_best_solutions' parameter must be of type bool but {type(save_best_solutions)} found.") - - # Validate save_solutions - if type(save_solutions) is bool: - if save_solutions == True: - if not self.suppress_warnings: - warnings.warn("Use the 'save_solutions' parameter with caution as it may cause memory overflow when either the number of generations, number of genes, or number of solutions in population is large.") - else: - self.valid_parameters = False - raise TypeError(f"The value passed to the 'save_solutions' parameter must be of type bool but {type(save_solutions)} found.") - - def validate_multi_stop_criteria(self, stop_word, number): - if stop_word == 'reach': - pass - else: - self.valid_parameters = False - raise ValueError(f"Passing multiple numbers following the keyword in the 'stop_criteria' parameter is expected only with the 'reach' keyword but the keyword ({stop_word}) found.") - - for idx, num in enumerate(number): - if num.replace(".", "").replace("-", "").isnumeric(): - number[idx] = float(num) - else: - self.valid_parameters = False - raise ValueError(f"The value(s) following the stop word in the 'stop_criteria' parameter must be numeric but the value ({num}) of type {type(num)} found.") - return number - - self.stop_criteria = [] - self.supported_stop_words = ["reach", "saturate"] - if stop_criteria is None: - # None: Stop after passing through all generations. - self.stop_criteria = None - elif type(stop_criteria) is str: - # reach_{target_fitness}: Stop if the target fitness value is reached. - # saturate_{num_generations}: Stop if the fitness value does not change (saturates) for the given number of generations. - criterion = stop_criteria.split("_") - stop_word = criterion[0] - # criterion[1] might be a single or multiple numbers. - number = criterion[1:] - if stop_word in self.supported_stop_words: - pass - else: - self.valid_parameters = False - raise ValueError(f"In the 'stop_criteria' parameter, the supported stop words are '{self.supported_stop_words}' but '{stop_word}' found.") - - if len(criterion) == 2: - # There is only a single number. - number = number[0] - if number.replace(".", "").replace("-", "").isnumeric(): - number = float(number) - else: - self.valid_parameters = False - raise ValueError(f"The value following the stop word in the 'stop_criteria' parameter must be a number but the value ({number}) of type {type(number)} found.") - - self.stop_criteria.append([stop_word, number]) - elif len(criterion) > 2: - number = validate_multi_stop_criteria(self, stop_word, number) - self.stop_criteria.append([stop_word] + number) - else: - self.valid_parameters = False - raise ValueError(f"For format of a single criterion in the 'stop_criteria' parameter is 'word_number' but '{stop_criteria}' found.") - - elif type(stop_criteria) in [list, tuple, numpy.ndarray]: - # Remove duplicate criteria by converting the list to a set then back to a list. - stop_criteria = list(set(stop_criteria)) - for idx, val in enumerate(stop_criteria): - if type(val) is str: - criterion = val.split("_") - stop_word = criterion[0] - number = criterion[1:] - if len(criterion) == 2: - # There is only a single number. - number = number[0] - if stop_word in self.supported_stop_words: - pass - else: - self.valid_parameters = False - raise ValueError(f"In the 'stop_criteria' parameter, the supported stop words are {self.supported_stop_words} but '{stop_word}' found.") - - if number.replace(".", "").replace("-", "").isnumeric(): - number = float(number) - else: - self.valid_parameters = False - raise ValueError(f"The value following the stop word in the 'stop_criteria' parameter must be a number but the value ({number}) of type {type(number)} found.") - - self.stop_criteria.append([stop_word, number]) - elif len(criterion) > 2: - number = validate_multi_stop_criteria(self, stop_word, number) - self.stop_criteria.append([stop_word] + number) - else: - self.valid_parameters = False - raise ValueError(f"The format of a single criterion in the 'stop_criteria' parameter is 'word_number' but {criterion} found.") - else: - self.valid_parameters = False - raise TypeError(f"When the 'stop_criteria' parameter is assigned a tuple/list/numpy.ndarray, then its elements must be strings but the value ({val}) of type {type(val)} found at index {idx}.") - else: - self.valid_parameters = False - raise TypeError(f"The expected value of the 'stop_criteria' is a single string or a list/tuple/numpy.ndarray of strings but the value ({stop_criteria}) of type {type(stop_criteria)} found.") - - if parallel_processing is None: - self.parallel_processing = None - elif type(parallel_processing) in GA.supported_int_types: - if parallel_processing > 0: - self.parallel_processing = ["thread", parallel_processing] - else: - self.valid_parameters = False - raise ValueError(f"When the 'parallel_processing' parameter is assigned an integer, then the integer must be positive but the value ({parallel_processing}) found.") - elif type(parallel_processing) in [list, tuple]: - if len(parallel_processing) == 2: - if type(parallel_processing[0]) is str: - if parallel_processing[0] in ["process", "thread"]: - if (type(parallel_processing[1]) in GA.supported_int_types and parallel_processing[1] > 0) or (parallel_processing[1] == 0) or (parallel_processing[1] is None): - if parallel_processing[1] == 0: - # If the number of processes/threads is 0, this means no parallel processing is used. It is equivalent to setting parallel_processing=None. - self.parallel_processing = None - else: - # Whether the second value is None or a positive integer. - self.parallel_processing = parallel_processing - else: - self.valid_parameters = False - raise TypeError(f"When a list or tuple is assigned to the 'parallel_processing' parameter, then the second element must be an integer but the value ({parallel_processing[1]}) of type {type(parallel_processing[1])} found.") - else: - self.valid_parameters = False - raise ValueError(f"When a list or tuple is assigned to the 'parallel_processing' parameter, then the value of the first element must be either 'process' or 'thread' but the value ({parallel_processing[0]}) found.") - else: - self.valid_parameters = False - raise TypeError(f"When a list or tuple is assigned to the 'parallel_processing' parameter, then the first element must be of type 'str' but the value ({parallel_processing[0]}) of type {type(parallel_processing[0])} found.") - else: - self.valid_parameters = False - raise ValueError(f"When a list or tuple is assigned to the 'parallel_processing' parameter, then it must have 2 elements but ({len(parallel_processing)}) found.") - else: - self.valid_parameters = False - raise ValueError(f"Unexpected value ({parallel_processing}) of type ({type(parallel_processing)}) assigned to the 'parallel_processing' parameter. The accepted values for this parameter are:\n1) None: (Default) It means no parallel processing is used.\n2) A positive integer referring to the number of threads to be used (i.e. threads, not processes, are used.\n3) list/tuple: If a list or a tuple of exactly 2 elements is assigned, then:\n\t*1) The first element can be either 'process' or 'thread' to specify whether processes or threads are used, respectively.\n\t*2) The second element can be:\n\t\t**1) A positive integer to select the maximum number of processes or threads to be used.\n\t\t**2) 0 to indicate that parallel processing is not used. This is identical to setting 'parallel_processing=None'.\n\t\t**3) None to use the default value as calculated by the concurrent.futures module.") - - # Set the `run_completed` property to False. It is set to `True` only after the `run()` method is complete. - self.run_completed = False - - # The number of completed generations. - self.generations_completed = 0 - - # At this point, all necessary parameters validation is done successfully, and we are sure that the parameters are valid. - # Set to True when all the parameters passed in the GA class constructor are valid. - self.valid_parameters = True - - # Parameters of the genetic algorithm. - self.num_generations = abs(num_generations) - self.parent_selection_type = parent_selection_type - - # Parameters of the mutation operation. - self.mutation_percent_genes = mutation_percent_genes - self.mutation_num_genes = mutation_num_genes - - # Even such this parameter is declared in the class header, it is assigned to the object here to access it after saving the object. - # A list holding the fitness value of the best solution for each generation. - self.best_solutions_fitness = [] - - # The generation number at which the best fitness value is reached. It is only assigned the generation number after the `run()` method completes. Otherwise, its value is -1. - self.best_solution_generation = -1 - - self.save_best_solutions = save_best_solutions - self.best_solutions = [] # Holds the best solution in each generation. - - self.save_solutions = save_solutions - self.solutions = [] # Holds the solutions in each generation. - # Holds the fitness of the solutions in each generation. - self.solutions_fitness = [] - - # A list holding the fitness values of all solutions in the last generation. - self.last_generation_fitness = None - # A list holding the parents of the last generation. - self.last_generation_parents = None - # A list holding the offspring after applying crossover in the last generation. - self.last_generation_offspring_crossover = None - # A list holding the offspring after applying mutation in the last generation. - self.last_generation_offspring_mutation = None - # Holds the fitness values of one generation before the fitness values saved in the last_generation_fitness attribute. Added in PyGAD 2.16.2. - # They are used inside the cal_pop_fitness() method to fetch the fitness of the parents in one generation before the latest generation. - # This is to avoid re-calculating the fitness for such parents again. - self.previous_generation_fitness = None - # Added in PyGAD 2.18.0. A NumPy array holding the elitism of the current generation according to the value passed in the 'keep_elitism' parameter. It works only if the 'keep_elitism' parameter has a non-zero value. - self.last_generation_elitism = None - # Added in PyGAD 2.19.0. A NumPy array holding the indices of the elitism of the current generation. It works only if the 'keep_elitism' parameter has a non-zero value. - self.last_generation_elitism_indices = None - # Supported in PyGAD 3.2.0. It holds the pareto fronts when solving a multi-objective problem. - self.pareto_fronts = None + self.validate_parameters(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + fitness_batch_size=fitness_batch_size, + initial_population=initial_population, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + init_range_low=init_range_low, + init_range_high=init_range_high, + gene_type=gene_type, + parent_selection_type=parent_selection_type, + keep_parents=keep_parents, + keep_elitism=keep_elitism, + K_tournament=K_tournament, + crossover_type=crossover_type, + crossover_probability=crossover_probability, + mutation_type=mutation_type, + mutation_probability=mutation_probability, + mutation_by_replacement=mutation_by_replacement, + mutation_percent_genes=mutation_percent_genes, + mutation_num_genes=mutation_num_genes, + random_mutation_min_val=random_mutation_min_val, + random_mutation_max_val=random_mutation_max_val, + gene_space=gene_space, + gene_constraint=gene_constraint, + sample_size=sample_size, + allow_duplicate_genes=allow_duplicate_genes, + 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, + save_best_solutions=save_best_solutions, + save_solutions=save_solutions, + suppress_warnings=suppress_warnings, + stop_criteria=stop_criteria, + parallel_processing=parallel_processing, + random_seed=random_seed, + logger=logger) except Exception as e: self.logger.exception(e) # sys.exit(-1) raise e - def round_genes(self, solutions): - for gene_idx in range(self.num_genes): - if self.gene_type_single: - if not self.gene_type[1] is None: - solutions[:, gene_idx] = numpy.round(solutions[:, gene_idx], - self.gene_type[1]) - else: - if not self.gene_type[gene_idx][1] is None: - solutions[:, gene_idx] = numpy.round(numpy.asarray(solutions[:, gene_idx], - dtype=self.gene_type[gene_idx][0]), - self.gene_type[gene_idx][1]) - return solutions - - def initialize_population(self, - allow_duplicate_genes, - gene_type, - gene_constraint): - """ - Creates an initial population randomly as a NumPy array. The array is saved in the instance attribute named 'population'. - - It accepts: - -allow_duplicate_genes: Whether duplicate genes are allowed or not. - -gene_type: The data type of the genes. - -gene_constraint: The constraints of the genes. - - This method assigns the values of the following 3 instance attributes: - 1. pop_size: Size of the population. - 2. population: Initially, holds the initial population and later updated after each generation. - 3. init_population: Keeping the initial population. - """ - - # Population size = (number of chromosomes, number of genes per chromosome) - # The population will have sol_per_pop chromosome where each chromosome has num_genes genes. - self.pop_size = (self.sol_per_pop, self.num_genes) - - # There are 4 steps to build the initial population: - # 1) Generate the population. - # 2) Change the data type and round the values. - # 3) Check for the constraints. - # 4) Solve duplicates if not allowed. - - # Create an empty population. - self.population = numpy.empty(shape=self.pop_size, dtype=object) - - # 1) Create the initial population either randomly or using the gene space. - if self.gene_space is None: - # Create the initial population randomly. - - # Set gene_value=None to consider generating values for the initial population instead of generating values for mutation. - # Loop through the genes, randomly generate the values of a single gene at a time, and insert the values of each gene to the population. - for sol_idx in range(self.sol_per_pop): - for gene_idx in range(self.num_genes): - range_min, range_max = self.get_initial_population_range(gene_index=gene_idx) - self.population[sol_idx, gene_idx] = self.generate_gene_value_randomly(range_min=range_min, - range_max=range_max, - gene_idx=gene_idx, - mutation_by_replacement=True, - gene_value=None, - sample_size=1, - step=1) - - else: - # Generate the initial population using the gene_space. - for sol_idx in range(self.sol_per_pop): - for gene_idx in range(self.num_genes): - self.population[sol_idx, gene_idx] = self.generate_gene_value_from_space(gene_idx=gene_idx, - mutation_by_replacement=True, - gene_value=None, - solution=self.population[sol_idx], - sample_size=1) - - # 2) Change the data type and round all genes within the initial population. - self.population = self.change_population_dtype_and_round(self.population) - - # Note that gene_constraint is not validated yet. - # We have to set it as a property of the pygad.GA instance to retrieve without passing it as an additional parameter. - self.gene_constraint = gene_constraint - - # 3) Enforce the gene constraints as much as possible. - if self.gene_constraint is None: - pass - else: - for sol_idx, solution in enumerate(self.population): - for gene_idx in range(self.num_genes): - # Check that a constraint is available for the gene and that the current value does not satisfy that constraint - if self.gene_constraint[gene_idx]: - # Remember that the second argument to the gene constraint callable is a list/numpy.ndarray of the values to check if they meet the gene constraint. - values = [solution[gene_idx]] - filtered_values = self.gene_constraint[gene_idx](solution, values) - result = self.validate_gene_constraint_callable_output(selected_values=filtered_values, - values=values) - if result: - pass - else: - raise Exception("The output from the gene_constraint callable/function must be a list or NumPy array that is subset of the passed values (second argument).") - - if len(filtered_values) ==1 and filtered_values[0] != solution[gene_idx]: - # Error by the user's defined gene constraint callable. - raise Exception(f"It is expected to receive a list/numpy.ndarray from the gene_constraint callable with a single value equal to {values[0]}, but the value {filtered_values[0]} found.") - - # Check if the gene value does not satisfy the gene constraint. - # Note that we already passed a list of a single value. - # It is expected to receive a list of either a single value or an empty list. - if len(filtered_values) < 1: - # Search for a value that satisfies the gene constraint. - range_min, range_max = self.get_initial_population_range(gene_index=gene_idx) - # While initializing the population, we follow a mutation by replacement approach. So, the original gene value is not needed. - values_filtered = self.get_valid_gene_constraint_values(range_min=range_min, - range_max=range_max, - gene_value=None, - gene_idx=gene_idx, - mutation_by_replacement=True, - solution=solution, - sample_size=self.sample_size) - if values_filtered is None: - if not self.suppress_warnings: - warnings.warn(f"No value satisfied the constraint for the gene at index {gene_idx} with value {solution[gene_idx]} while creating the initial population.") - else: - self.population[sol_idx, gene_idx] = random.choice(values_filtered) - elif len(filtered_values) == 1: - # The value already satisfied the gene constraint. - pass - else: - # Error by the user's defined gene constraint callable. - raise Exception(f"It is expected to receive a list/numpy.ndarray from the gene_constraint callable that is either empty or has a single value equal, but received a list/numpy.ndarray of length {len(filtered_values)}.") - - # 4) Solve duplicate genes. - if allow_duplicate_genes == False: - for solution_idx in range(self.population.shape[0]): - if self.gene_space is None: - self.population[solution_idx], _, _ = self.solve_duplicate_genes_randomly(solution=self.population[solution_idx], - min_val=self.init_range_low, - max_val=self.init_range_high, - gene_type=gene_type, - mutation_by_replacement=True, - sample_size=self.sample_size) - else: - self.population[solution_idx], _, _ = self.solve_duplicate_genes_by_space(solution=self.population[solution_idx].copy(), - gene_type=self.gene_type, - mutation_by_replacement=True, - sample_size=self.sample_size, - build_initial_pop=True) - - # Change the data type and round all genes within the initial population. - self.population = self.change_population_dtype_and_round(self.population) - - # Keeping the initial population in the initial_population attribute. - self.initial_population = self.population.copy() - - def cal_pop_fitness(self): - """ - Calculating the fitness values of batches of solutions in the current population. - It returns: - -fitness: An array of the calculated fitness values. - """ - try: - if self.valid_parameters == False: - raise Exception("ERROR calling the cal_pop_fitness() method: \nPlease check the parameters passed while creating an instance of the GA class.\n") - - # 'last_generation_parents_as_list' is the list version of 'self.last_generation_parents' - # It is used to return the parent index using the 'in' membership operator of Python lists. This is much faster than using 'numpy.where()'. - if self.last_generation_parents is not None: - last_generation_parents_as_list = self.last_generation_parents.tolist() - else: - last_generation_parents_as_list = [] - - # 'last_generation_elitism_as_list' is the list version of 'self.last_generation_elitism' - # It is used to return the elitism index using the 'in' membership operator of Python lists. This is much faster than using 'numpy.where()'. - if self.last_generation_elitism is not None: - last_generation_elitism_as_list = self.last_generation_elitism.tolist() - else: - last_generation_elitism_as_list = [] - - pop_fitness = ["undefined"] * len(self.population) - if self.parallel_processing is None: - # Calculating the fitness value of each solution in the current population. - for sol_idx, sol in enumerate(self.population): - # Check if the `save_solutions` parameter is `True` and whether the solution already exists in the `solutions` list. If so, use its fitness rather than calculating it again. - # The functions numpy.any()/numpy.all()/numpy.where()/numpy.equal() are very slow. - # So, list membership operator 'in' is used to check if the solution exists in the 'self.solutions' list. - # Make sure that both the solution and 'self.solutions' are of type 'list' not 'numpy.ndarray'. - # if (self.save_solutions) and (len(self.solutions) > 0) and (numpy.any(numpy.all(self.solutions == numpy.array(sol), axis=1))) - # if (self.save_solutions) and (len(self.solutions) > 0) and (numpy.any(numpy.all(numpy.equal(self.solutions, numpy.array(sol)), axis=1))) - - # Make sure self.best_solutions is a list of lists before proceeding. - # Because the second condition expects that best_solutions is a list of lists. - if type(self.best_solutions) is numpy.ndarray: - self.best_solutions = self.best_solutions.tolist() - - if (self.save_solutions) and (len(self.solutions) > 0) and (list(sol) in self.solutions): - solution_idx = self.solutions.index(list(sol)) - fitness = self.solutions_fitness[solution_idx] - elif (self.save_best_solutions) and (len(self.best_solutions) > 0) and (list(sol) in self.best_solutions): - solution_idx = self.best_solutions.index(list(sol)) - fitness = self.best_solutions_fitness[solution_idx] - elif (self.keep_elitism > 0) and (self.last_generation_elitism is not None) and (len(self.last_generation_elitism) > 0) and (list(sol) in last_generation_elitism_as_list): - # Return the index of the elitism from the elitism array 'self.last_generation_elitism'. - # This is not its index within the population. It is just its index in the 'self.last_generation_elitism' array. - elitism_idx = last_generation_elitism_as_list.index(list(sol)) - # Use the returned elitism index to return its index in the last population. - elitism_idx = self.last_generation_elitism_indices[elitism_idx] - # Use the elitism's index to return its pre-calculated fitness value. - fitness = self.previous_generation_fitness[elitism_idx] - # If the solutions are not saved (i.e. `save_solutions=False`), check if this solution is a parent from the previous generation and its fitness value is already calculated. If so, use the fitness value instead of calling the fitness function. - # We cannot use the `numpy.where()` function directly because it does not support the `axis` parameter. This is why the `numpy.all()` function is used to match the solutions on axis=1. - # elif (self.last_generation_parents is not None) and len(numpy.where(numpy.all(self.last_generation_parents == sol, axis=1))[0] > 0): - elif ((self.keep_parents == -1) or (self.keep_parents > 0)) and (self.last_generation_parents is not None) and (len(self.last_generation_parents) > 0) and (list(sol) in last_generation_parents_as_list): - # Index of the parent in the 'self.last_generation_parents' array. - # This is not its index within the population. It is just its index in the 'self.last_generation_parents' array. - # parent_idx = numpy.where(numpy.all(self.last_generation_parents == sol, axis=1))[0][0] - parent_idx = last_generation_parents_as_list.index(list(sol)) - # Use the returned parent index to return its index in the last population. - parent_idx = self.last_generation_parents_indices[parent_idx] - # Use the parent's index to return its pre-calculated fitness value. - fitness = self.previous_generation_fitness[parent_idx] - else: - # Check if batch processing is used. If not, then calculate this missing fitness value. - if self.fitness_batch_size in [1, None]: - fitness = self.fitness_func(self, sol, sol_idx) - if type(fitness) in GA.supported_int_float_types: - # The fitness function returns a single numeric value. - # This is a single-objective optimization problem. - pass - elif type(fitness) in [list, tuple, numpy.ndarray]: - # The fitness function returns a list/tuple/numpy.ndarray. - # This is a multi-objective optimization problem. - pass - else: - raise ValueError(f"The fitness function should return a number or an iterable (list, tuple, or numpy.ndarray) but the value {fitness} of type {type(fitness)} found.") - else: - # Reaching this point means that batch processing is in effect to calculate the fitness values. - # Do not continue the loop as no fitness is calculated. The fitness will be calculated later in batch mode. - continue - - # This is only executed if the fitness value was already calculated. - pop_fitness[sol_idx] = fitness - - if self.fitness_batch_size not in [1, None]: - # Reaching this block means that batch fitness calculation is used. - - # Indices of the solutions to calculate their fitness. - solutions_indices = [idx for idx, fit in enumerate(pop_fitness) if type(fit) is str and fit == "undefined"] - # Number of batches. - num_batches = int(numpy.ceil(len(solutions_indices) / self.fitness_batch_size)) - # For each batch, get its indices and call the fitness function. - for batch_idx in range(num_batches): - batch_first_index = batch_idx * self.fitness_batch_size - batch_last_index = (batch_idx + 1) * self.fitness_batch_size - batch_indices = solutions_indices[batch_first_index:batch_last_index] - batch_solutions = self.population[batch_indices, :] - - batch_fitness = self.fitness_func( - self, batch_solutions, batch_indices) - if type(batch_fitness) not in [list, tuple, numpy.ndarray]: - raise TypeError(f"Expected to receive a list, tuple, or numpy.ndarray from the fitness function but the value ({batch_fitness}) of type {type(batch_fitness)}.") - elif len(numpy.array(batch_fitness)) != len(batch_indices): - raise ValueError(f"There is a mismatch between the number of solutions passed to the fitness function ({len(batch_indices)}) and the number of fitness values returned ({len(batch_fitness)}). They must match.") - - for index, fitness in zip(batch_indices, batch_fitness): - if type(fitness) in GA.supported_int_float_types: - # The fitness function returns a single numeric value. - # This is a single-objective optimization problem. - pop_fitness[index] = fitness - elif type(fitness) in [list, tuple, numpy.ndarray]: - # The fitness function returns a list/tuple/numpy.ndarray. - # This is a multi-objective optimization problem. - pop_fitness[index] = fitness - else: - raise ValueError(f"The fitness function should return a number or an iterable (list, tuple, or numpy.ndarray) but the value {fitness} of type {type(fitness)} found.") - else: - # Calculating the fitness value of each solution in the current population. - for sol_idx, sol in enumerate(self.population): - # Check if the `save_solutions` parameter is `True` and whether the solution already exists in the `solutions` list. If so, use its fitness rather than calculating it again. - # The functions numpy.any()/numpy.all()/numpy.where()/numpy.equal() are very slow. - # So, list membership operator 'in' is used to check if the solution exists in the 'self.solutions' list. - # Make sure that both the solution and 'self.solutions' are of type 'list' not 'numpy.ndarray'. - if (self.save_solutions) and (len(self.solutions) > 0) and (list(sol) in self.solutions): - solution_idx = self.solutions.index(list(sol)) - fitness = self.solutions_fitness[solution_idx] - pop_fitness[sol_idx] = fitness - elif (self.keep_elitism > 0) and (self.last_generation_elitism is not None) and (len(self.last_generation_elitism) > 0) and (list(sol) in last_generation_elitism_as_list): - # Return the index of the elitism from the elitism array 'self.last_generation_elitism'. - # This is not its index within the population. It is just its index in the 'self.last_generation_elitism' array. - elitism_idx = last_generation_elitism_as_list.index( - list(sol)) - # Use the returned elitism index to return its index in the last population. - elitism_idx = self.last_generation_elitism_indices[elitism_idx] - # Use the elitism's index to return its pre-calculated fitness value. - fitness = self.previous_generation_fitness[elitism_idx] - - pop_fitness[sol_idx] = fitness - # If the solutions are not saved (i.e. `save_solutions=False`), check if this solution is a parent from the previous generation and its fitness value is already calculated. If so, use the fitness value instead of calling the fitness function. - # We cannot use the `numpy.where()` function directly because it does not support the `axis` parameter. This is why the `numpy.all()` function is used to match the solutions on axis=1. - # elif (self.last_generation_parents is not None) and len(numpy.where(numpy.all(self.last_generation_parents == sol, axis=1))[0] > 0): - elif ((self.keep_parents == -1) or (self.keep_parents > 0)) and (self.last_generation_parents is not None) and (len(self.last_generation_parents) > 0) and (list(sol) in last_generation_parents_as_list): - # Index of the parent in the 'self.last_generation_parents' array. - # This is not its index within the population. It is just its index in the 'self.last_generation_parents' array. - # parent_idx = numpy.where(numpy.all(self.last_generation_parents == sol, axis=1))[0][0] - parent_idx = last_generation_parents_as_list.index( - list(sol)) - # Use the returned parent index to return its index in the last population. - parent_idx = self.last_generation_parents_indices[parent_idx] - # Use the parent's index to return its pre-calculated fitness value. - fitness = self.previous_generation_fitness[parent_idx] - - pop_fitness[sol_idx] = fitness - - # Decide which class to use based on whether the user selected "process" or "thread" - if self.parallel_processing[0] == "process": - ExecutorClass = concurrent.futures.ProcessPoolExecutor - else: - ExecutorClass = concurrent.futures.ThreadPoolExecutor - - # We can use a with statement to ensure threads are cleaned up promptly (https://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor-example) - with ExecutorClass(max_workers=self.parallel_processing[1]) as executor: - solutions_to_submit_indices = [] - solutions_to_submit = [] - for sol_idx, sol in enumerate(self.population): - # The "undefined" value means that the fitness of this solution must be calculated. - if type(pop_fitness[sol_idx]) is str: - if pop_fitness[sol_idx] == "undefined": - solutions_to_submit.append(sol.copy()) - solutions_to_submit_indices.append(sol_idx) - elif type(pop_fitness[sol_idx]) in [list, tuple, numpy.ndarray]: - # This is a multi-objective problem. The fitness is already calculated. Nothing to do. - pass - - # Check if batch processing is used. If not, then calculate the fitness value for individual solutions. - if self.fitness_batch_size in [1, None]: - for index, fitness in zip(solutions_to_submit_indices, executor.map(self.fitness_func, [self]*len(solutions_to_submit_indices), solutions_to_submit, solutions_to_submit_indices)): - if type(fitness) in GA.supported_int_float_types: - # The fitness function returns a single numeric value. - # This is a single-objective optimization problem. - pop_fitness[index] = fitness - elif type(fitness) in [list, tuple, numpy.ndarray]: - # The fitness function returns a list/tuple/numpy.ndarray. - # This is a multi-objective optimization problem. - pop_fitness[index] = fitness - else: - raise ValueError(f"The fitness function should return a number or an iterable (list, tuple, or numpy.ndarray) but the value {fitness} of type {type(fitness)} found.") - else: - # Reaching this block means that batch processing is used. The fitness values are calculated in batches. - - # Number of batches. - num_batches = int(numpy.ceil(len(solutions_to_submit_indices) / self.fitness_batch_size)) - # Each element of the `batches_solutions` list represents the solutions in one batch. - batches_solutions = [] - # Each element of the `batches_indices` list represents the solutions' indices in one batch. - batches_indices = [] - # For each batch, get its indices and call the fitness function. - for batch_idx in range(num_batches): - batch_first_index = batch_idx * self.fitness_batch_size - batch_last_index = (batch_idx + 1) * self.fitness_batch_size - batch_indices = solutions_to_submit_indices[batch_first_index:batch_last_index] - batch_solutions = self.population[batch_indices, :] - - batches_solutions.append(batch_solutions) - batches_indices.append(batch_indices) - - for batch_indices, batch_fitness in zip(batches_indices, executor.map(self.fitness_func, [self]*len(solutions_to_submit_indices), batches_solutions, batches_indices)): - if type(batch_fitness) not in [list, tuple, numpy.ndarray]: - raise TypeError(f"Expected to receive a list, tuple, or numpy.ndarray from the fitness function but the value ({batch_fitness}) of type {type(batch_fitness)}.") - elif len(numpy.array(batch_fitness)) != len(batch_indices): - raise ValueError(f"There is a mismatch between the number of solutions passed to the fitness function ({len(batch_indices)}) and the number of fitness values returned ({len(batch_fitness)}). They must match.") - - for index, fitness in zip(batch_indices, batch_fitness): - if type(fitness) in GA.supported_int_float_types: - # The fitness function returns a single numeric value. - # This is a single-objective optimization problem. - pop_fitness[index] = fitness - elif type(fitness) in [list, tuple, numpy.ndarray]: - # The fitness function returns a list/tuple/numpy.ndarray. - # This is a multi-objective optimization problem. - pop_fitness[index] = fitness - else: - raise ValueError(f"The fitness function should return a number or an iterable (list, tuple, or numpy.ndarray) but the value ({fitness}) of type {type(fitness)} found.") - - pop_fitness = numpy.array(pop_fitness) - except Exception as ex: - self.logger.exception(ex) - # sys.exit(-1) - raise ex - return pop_fitness - - def run(self): - """ - Runs the genetic algorithm. This is the main method in which the genetic algorithm is evolved through a number of generations. - """ - try: - if self.valid_parameters == False: - raise Exception("Error calling the run() method: \nThe run() method cannot be executed with invalid parameters. Please check the parameters passed while creating an instance of the GA class.\n") - - # Starting from PyGAD 2.18.0, the 4 properties (best_solutions, best_solutions_fitness, solutions, and solutions_fitness) are no longer reset with each call to the run() method. Instead, they are extended. - # For example, if there are 50 generations and the user set save_best_solutions=True, then the length of the 2 properties best_solutions and best_solutions_fitness will be 50 after the first call to the run() method, then 100 after the second call, 150 after the third, and so on. - - # self.best_solutions: Holds the best solution in each generation. - if type(self.best_solutions) is numpy.ndarray: - self.best_solutions = self.best_solutions.tolist() - # self.best_solutions_fitness: A list holding the fitness value of the best solution for each generation. - if type(self.best_solutions_fitness) is numpy.ndarray: - self.best_solutions_fitness = list(self.best_solutions_fitness) - # self.solutions: Holds the solutions in each generation. - if type(self.solutions) is numpy.ndarray: - self.solutions = self.solutions.tolist() - # self.solutions_fitness: Holds the fitness of the solutions in each generation. - if type(self.solutions_fitness) is numpy.ndarray: - self.solutions_fitness = list(self.solutions_fitness) - - if not (self.on_start is None): - self.on_start(self) - - stop_run = False - - # To continue from where we stopped, the first generation index should start from the value of the 'self.generations_completed' parameter. - if self.generations_completed != 0 and type(self.generations_completed) in GA.supported_int_types: - # If the 'self.generations_completed' parameter is not '0', then this means we continue execution. - generation_first_idx = self.generations_completed - generation_last_idx = self.num_generations + self.generations_completed - else: - # If the 'self.generations_completed' parameter is '0', then stat from scratch. - generation_first_idx = 0 - generation_last_idx = self.num_generations - - # Measuring the fitness of each chromosome in the population. Save the fitness in the last_generation_fitness attribute. - self.last_generation_fitness = self.cal_pop_fitness() - - # Know whether the problem is SOO or MOO. - if type(self.last_generation_fitness[0]) in GA.supported_int_float_types: - # Single-objective problem. - # If the problem is SOO, the parent selection type cannot be nsga2 or tournament_nsga2. - if self.parent_selection_type in ['nsga2', 'tournament_nsga2']: - raise TypeError(f"Incorrect parent selection type. The fitness function returned a single numeric fitness value which means the problem is single-objective. But the parent selection type {self.parent_selection_type} is used which only works for multi-objective optimization problems.") - elif type(self.last_generation_fitness[0]) in [list, tuple, numpy.ndarray]: - # Multi-objective problem. - pass - - best_solution, best_solution_fitness, best_match_idx = self.best_solution(pop_fitness=self.last_generation_fitness) - - # Appending the best solution in the initial population to the best_solutions list. - if self.save_best_solutions: - self.best_solutions.append(list(best_solution)) - - for generation in range(generation_first_idx, generation_last_idx): - - self.run_loop_head(best_solution_fitness) - - # Call the 'run_select_parents()' method to select the parents. - # It edits these 2 instance attributes: - # 1) last_generation_parents: A NumPy array of the selected parents. - # 2) last_generation_parents_indices: A 1D NumPy array of the indices of the selected parents. - self.run_select_parents() - - # Call the 'run_crossover()' method to select the offspring. - # It edits these 2 instance attributes: - # 1) last_generation_offspring_crossover: A NumPy array of the selected offspring. - # 2) last_generation_elitism: A NumPy array of the current generation elitism. Applicable only if the 'keep_elitism' parameter > 0. - self.run_crossover() - - # Call the 'run_mutation()' method to mutate the selected offspring. - # It edits this instance attribute: - # 1) last_generation_offspring_mutation: A NumPy array of the mutated offspring. - self.run_mutation() - - # Call the 'run_update_population()' method to update the population after both crossover and mutation operations complete. - # It edits this instance attribute: - # 1) population: A NumPy array of the population of solutions/chromosomes. - self.run_update_population() - - # The generations_completed attribute holds the number of the last completed generation. - self.generations_completed = generation + 1 - - self.previous_generation_fitness = self.last_generation_fitness.copy() - # Measuring the fitness of each chromosome in the population. Save the fitness in the last_generation_fitness attribute. - self.last_generation_fitness = self.cal_pop_fitness() - - best_solution, best_solution_fitness, best_match_idx = self.best_solution( - pop_fitness=self.last_generation_fitness) - - # Appending the best solution in the current generation to the best_solutions list. - if self.save_best_solutions: - self.best_solutions.append(list(best_solution)) - - # Note: Any code that has loop-dependant statements (e.g. continue, break, etc.) must be kept inside the loop of the 'run()' method. It can be moved to another method to clean the run() method. - # If the on_generation attribute is not None, then cal the callback function after the generation. - if not (self.on_generation is None): - r = self.on_generation(self) - if type(r) is str and r.lower() == "stop": - # Before aborting the loop, save the fitness value of the best solution. - # _, best_solution_fitness, _ = self.best_solution() - self.best_solutions_fitness.append(best_solution_fitness) - break - - if not self.stop_criteria is None: - for criterion in self.stop_criteria: - if criterion[0] == "reach": - # Single-objective problem. - if type(self.last_generation_fitness[0]) in GA.supported_int_float_types: - if max(self.last_generation_fitness) >= criterion[1]: - stop_run = True - break - # Multi-objective problem. - elif type(self.last_generation_fitness[0]) in [list, tuple, numpy.ndarray]: - # Validate the value passed to the criterion. - if len(criterion[1:]) == 1: - # There is a single value used across all the objectives. - pass - elif len(criterion[1:]) > 1: - # There are multiple values. The number of values must be equal to the number of objectives. - if len(criterion[1:]) == len(self.last_generation_fitness[0]): - pass - else: - self.valid_parameters = False - raise ValueError(f"When the the 'reach' keyword is used with the 'stop_criteria' parameter for solving a multi-objective problem, then the number of numeric values following the keyword can be:\n1) A single numeric value to be used across all the objective functions.\n2) A number of numeric values equal to the number of objective functions.\nBut the value {criterion} found with {len(criterion)-1} numeric values which is not equal to the number of objective functions {len(self.last_generation_fitness[0])}.") - - stop_run = True - for obj_idx in range(len(self.last_generation_fitness[0])): - # Use the objective index to return the proper value for the criterion. - - if len(criterion[1:]) == len(self.last_generation_fitness[0]): - reach_fitness_value = criterion[obj_idx + 1] - elif len(criterion[1:]) == 1: - reach_fitness_value = criterion[1] - else: - # Unexpected to be reached, but it is safer to handle it. - self.valid_parameters = False - raise ValueError(f"The number of values does not equal the number of objectives.") - - if max(self.last_generation_fitness[:, obj_idx]) >= reach_fitness_value: - pass - else: - stop_run = False - break - elif criterion[0] == "saturate": - criterion[1] = int(criterion[1]) - if self.generations_completed >= criterion[1]: - # Single-objective problem. - if type(self.last_generation_fitness[0]) in GA.supported_int_float_types: - if (self.best_solutions_fitness[self.generations_completed - criterion[1]] - self.best_solutions_fitness[self.generations_completed - 1]) == 0: - stop_run = True - break - # Multi-objective problem. - elif type(self.last_generation_fitness[0]) in [list, tuple, numpy.ndarray]: - stop_run = True - for obj_idx in range(len(self.last_generation_fitness[0])): - if (self.best_solutions_fitness[self.generations_completed - criterion[1]][obj_idx] - self.best_solutions_fitness[self.generations_completed - 1][obj_idx]) == 0: - pass - else: - stop_run = False - break - - if stop_run: - break - - # Save the fitness of the last generation. - if self.save_solutions: - # self.solutions.extend(self.population.copy()) - population_as_list = self.population.copy() - population_as_list = [list(item) for item in population_as_list] - self.solutions.extend(population_as_list) - - self.solutions_fitness.extend(self.last_generation_fitness) - - # Call the run_select_parents() method to update these 2 attributes according to the 'last_generation_fitness' attribute: - # 1) last_generation_parents 2) last_generation_parents_indices - # Set 'call_on_parents=False' to avoid calling the callable 'on_parents' because this step is not part of the cycle. - self.run_select_parents(call_on_parents=False) - - # Save the fitness value of the best solution. - _, best_solution_fitness, _ = self.best_solution( - pop_fitness=self.last_generation_fitness) - self.best_solutions_fitness.append(best_solution_fitness) - - self.best_solution_generation = numpy.where(numpy.array( - self.best_solutions_fitness) == numpy.max(numpy.array(self.best_solutions_fitness)))[0][0] - # After the run() method completes, the run_completed flag is changed from False to True. - # Set to True only after the run() method completes gracefully. - self.run_completed = True - - if not (self.on_stop is None): - self.on_stop(self, self.last_generation_fitness) - - # Converting the 'best_solutions' list into a NumPy array. - self.best_solutions = numpy.array(self.best_solutions) - - # Update previous_generation_fitness because it is used to get the fitness of the parents. - self.previous_generation_fitness = self.last_generation_fitness.copy() - - # Converting the 'solutions' list into a NumPy array. - # self.solutions = numpy.array(self.solutions) - except Exception as ex: - self.logger.exception(ex) - # sys.exit(-1) - raise ex - - def run_loop_head(self, best_solution_fitness): - if not (self.on_fitness is None): - on_fitness_output = self.on_fitness(self, - self.last_generation_fitness) - - if on_fitness_output is None: - pass - else: - if type(on_fitness_output) in [tuple, list, numpy.ndarray, range]: - on_fitness_output = numpy.array(on_fitness_output) - if on_fitness_output.shape == self.last_generation_fitness.shape: - self.last_generation_fitness = on_fitness_output - else: - raise ValueError(f"Size mismatch between the output of on_fitness() {on_fitness_output.shape} and the expected fitness output {self.last_generation_fitness.shape}.") - else: - raise ValueError(f"The output of on_fitness() is expected to be tuple/list/range/numpy.ndarray but {type(on_fitness_output)} found.") - - # Appending the fitness value of the best solution in the current generation to the best_solutions_fitness attribute. - self.best_solutions_fitness.append(best_solution_fitness) - - # Appending the solutions in the current generation to the solutions list. - if self.save_solutions: - # self.solutions.extend(self.population.copy()) - population_as_list = self.population.copy() - population_as_list = [list(item) for item in population_as_list] - self.solutions.extend(population_as_list) - - self.solutions_fitness.extend(self.last_generation_fitness) - - def run_select_parents(self, call_on_parents=True): - """ - This method must be only called from inside the run() method. It is not meant for use by the user. - Generally, any method with a name starting with 'run_' is meant to be only called by PyGAD from inside the 'run()' method. - - The objective of the 'run_select_parents()' method is to select the parents and call the callable on_parents() if defined. - It does not return any variables. However, it changes these 2 attributes of the pygad.GA class instances: - 1) last_generation_parents: A NumPy array of the selected parents. - 2) last_generation_parents_indices: A 1D NumPy array of the indices of the selected parents. - - Parameters - ---------- - call_on_parents : bool, optional - If True, then the callable 'on_parents()' is called. The default is True. - - Returns - ------- - None. - """ - - # Selecting the best parents in the population for mating. - if callable(self.parent_selection_type): - self.last_generation_parents, self.last_generation_parents_indices = self.select_parents(self.last_generation_fitness, - self.num_parents_mating, - self) - if not type(self.last_generation_parents) is numpy.ndarray: - raise TypeError(f"The type of the iterable holding the selected parents is expected to be (numpy.ndarray) but {type(self.last_generation_parents)} found.") - if not type(self.last_generation_parents_indices) is numpy.ndarray: - raise TypeError(f"The type of the iterable holding the selected parents' indices is expected to be (numpy.ndarray) but {type(self.last_generation_parents_indices)} found.") - else: - self.last_generation_parents, self.last_generation_parents_indices = self.select_parents(self.last_generation_fitness, - num_parents=self.num_parents_mating) - - # Validate the output of the parent selection step: self.select_parents() - if self.last_generation_parents.shape != (self.num_parents_mating, self.num_genes): - if self.last_generation_parents.shape[0] != self.num_parents_mating: - raise ValueError(f"Size mismatch between the size of the selected parents {self.last_generation_parents.shape} and the expected size {(self.num_parents_mating, self.num_genes)}. It is expected to select ({self.num_parents_mating}) parents but ({self.last_generation_parents.shape[0]}) selected.") - elif self.last_generation_parents.shape[1] != self.num_genes: - raise ValueError(f"Size mismatch between the size of the selected parents {self.last_generation_parents.shape} and the expected size {(self.num_parents_mating, self.num_genes)}. Parents are expected to have ({self.num_genes}) genes but ({self.last_generation_parents.shape[1]}) produced.") - - if self.last_generation_parents_indices.ndim != 1: - raise ValueError(f"The iterable holding the selected parents indices is expected to have 1 dimension but ({len(self.last_generation_parents_indices)}) found.") - elif len(self.last_generation_parents_indices) != self.num_parents_mating: - raise ValueError(f"The iterable holding the selected parents indices is expected to have ({self.num_parents_mating}) values but ({len(self.last_generation_parents_indices)}) found.") - - if call_on_parents: - if not (self.on_parents is None): - on_parents_output = self.on_parents(self, - self.last_generation_parents) - - if on_parents_output is None: - pass - elif type(on_parents_output) in [list, tuple, numpy.ndarray]: - if len(on_parents_output) == 2: - on_parents_selected_parents, on_parents_selected_parents_indices = on_parents_output - else: - raise ValueError(f"The output of on_parents() is expected to be tuple/list/numpy.ndarray of length 2 but {type(on_parents_output)} of length {len(on_parents_output)} found.") - - # Validate the parents. - if on_parents_selected_parents is None: - raise ValueError("The returned outputs of on_parents() cannot be None but the first output is None.") - else: - if type(on_parents_selected_parents) in [tuple, list, numpy.ndarray]: - on_parents_selected_parents = numpy.array(on_parents_selected_parents) - if on_parents_selected_parents.shape == self.last_generation_parents.shape: - self.last_generation_parents = on_parents_selected_parents - else: - raise ValueError(f"Size mismatch between the parents returned by on_parents() {on_parents_selected_parents.shape} and the expected parents shape {self.last_generation_parents.shape}.") - else: - raise ValueError(f"The output of on_parents() is expected to be tuple/list/numpy.ndarray but the first output type is {type(on_parents_selected_parents)}.") - - # Validate the parents indices. - if on_parents_selected_parents_indices is None: - raise ValueError("The returned outputs of on_parents() cannot be None but the second output is None.") - else: - if type(on_parents_selected_parents_indices) in [tuple, list, numpy.ndarray, range]: - on_parents_selected_parents_indices = numpy.array(on_parents_selected_parents_indices) - if on_parents_selected_parents_indices.shape == self.last_generation_parents_indices.shape: - # Add this new instance attribute. - self.last_generation_parents_indices = on_parents_selected_parents_indices - else: - raise ValueError(f"Size mismatch between the parents indices returned by on_parents() {on_parents_selected_parents_indices.shape} and the expected crossover output {self.last_generation_parents_indices.shape}.") - else: - raise ValueError(f"The output of on_parents() is expected to be tuple/list/range/numpy.ndarray but the second output type is {type(on_parents_selected_parents_indices)}.") - - else: - raise TypeError(f"The output of on_parents() is expected to be tuple/list/numpy.ndarray but {type(on_parents_output)} found.") - - def run_crossover(self): - """ - This method must be only called from inside the run() method. It is not meant for use by the user. - Generally, any method with a name starting with 'run_' is meant to be only called by PyGAD from inside the 'run()' method. - - The objective of the 'run_crossover()' method is to apply crossover and call the callable on_crossover() if defined. - It does not return any variables. However, it changes these 2 attributes of the pygad.GA class instances: - 1) last_generation_offspring_crossover: A NumPy array of the selected offspring. - 2) last_generation_elitism: A NumPy array of the current generation elitism. Applicable only if the 'keep_elitism' parameter > 0. - - Returns - ------- - None. - """ - - # If self.crossover_type=None, then no crossover is applied and thus no offspring will be created in the next generations. The next generation will use the solutions in the current population. - if self.crossover_type is None: - if self.keep_elitism == 0: - num_parents_to_keep = self.num_parents_mating if self.keep_parents == - 1 else self.keep_parents - if self.num_offspring <= num_parents_to_keep: - self.last_generation_offspring_crossover = self.last_generation_parents[0:self.num_offspring] - else: - self.last_generation_offspring_crossover = numpy.concatenate( - (self.last_generation_parents, self.population[0:(self.num_offspring - self.last_generation_parents.shape[0])])) - else: - # The steady_state_selection() function is called to select the best solutions (i.e. elitism). The keep_elitism parameter defines the number of these solutions. - # The steady_state_selection() function is still called here even if its output may not be used given that the condition of the next if statement is True. The reason is that it will be used later. - self.last_generation_elitism, _ = self.steady_state_selection(self.last_generation_fitness, - num_parents=self.keep_elitism) - if self.num_offspring <= self.keep_elitism: - self.last_generation_offspring_crossover = self.last_generation_parents[0:self.num_offspring] - else: - self.last_generation_offspring_crossover = numpy.concatenate( - (self.last_generation_elitism, self.population[0:(self.num_offspring - self.last_generation_elitism.shape[0])])) - else: - # Generating offspring using crossover. - if callable(self.crossover_type): - self.last_generation_offspring_crossover = self.crossover(self.last_generation_parents, - (self.num_offspring, self.num_genes), - self) - if not type(self.last_generation_offspring_crossover) is numpy.ndarray: - raise TypeError(f"The output of the crossover step is expected to be of type (numpy.ndarray) but {type(self.last_generation_offspring_crossover)} found.") - else: - self.last_generation_offspring_crossover = self.crossover(self.last_generation_parents, - offspring_size=(self.num_offspring, self.num_genes)) - if self.last_generation_offspring_crossover.shape != (self.num_offspring, self.num_genes): - if self.last_generation_offspring_crossover.shape[0] != self.num_offspring: - raise ValueError(f"Size mismatch between the crossover output {self.last_generation_offspring_crossover.shape} and the expected crossover output {(self.num_offspring, self.num_genes)}. It is expected to produce ({self.num_offspring}) offspring but ({self.last_generation_offspring_crossover.shape[0]}) produced.") - elif self.last_generation_offspring_crossover.shape[1] != self.num_genes: - raise ValueError(f"Size mismatch between the crossover output {self.last_generation_offspring_crossover.shape} and the expected crossover output {(self.num_offspring, self.num_genes)}. It is expected that the offspring has ({self.num_genes}) genes but ({self.last_generation_offspring_crossover.shape[1]}) produced.") - - # PyGAD 2.18.2 // The on_crossover() callback function is called even if crossover_type is None. - if not (self.on_crossover is None): - on_crossover_output = self.on_crossover(self, - self.last_generation_offspring_crossover) - if on_crossover_output is None: - pass - else: - if type(on_crossover_output) in [tuple, list, numpy.ndarray]: - on_crossover_output = numpy.array(on_crossover_output) - if on_crossover_output.shape == self.last_generation_offspring_crossover.shape: - self.last_generation_offspring_crossover = on_crossover_output - else: - raise ValueError(f"Size mismatch between the output of on_crossover() {on_crossover_output.shape} and the expected crossover output {self.last_generation_offspring_crossover.shape}.") - else: - raise ValueError(f"The output of on_crossover() is expected to be tuple/list/numpy.ndarray but {type(on_crossover_output)} found.") - - def run_mutation(self): - """ - This method must be only called from inside the run() method. It is not meant for use by the user. - Generally, any method with a name starting with 'run_' is meant to be only called by PyGAD from inside the 'run()' method. - - The objective of the 'run_mutation()' method is to apply mutation and call the callable on_mutation() if defined. - It does not return any variables. However, it changes this attribute of the pygad.GA class instances: - 1) last_generation_offspring_mutation: A NumPy array of the mutated offspring. - - Returns - ------- - None. - """ - - # If self.mutation_type=None, then no mutation is applied and thus no changes are applied to the offspring created using the crossover operation. The offspring will be used unchanged in the next generation. - if self.mutation_type is None: - self.last_generation_offspring_mutation = self.last_generation_offspring_crossover - else: - # Adding some variations to the offspring using mutation. - if callable(self.mutation_type): - self.last_generation_offspring_mutation = self.mutation(self.last_generation_offspring_crossover, - self) - if not type(self.last_generation_offspring_mutation) is numpy.ndarray: - raise TypeError(f"The output of the mutation step is expected to be of type (numpy.ndarray) but {type(self.last_generation_offspring_mutation)} found.") - else: - self.last_generation_offspring_mutation = self.mutation(self.last_generation_offspring_crossover) - - if self.last_generation_offspring_mutation.shape != (self.num_offspring, self.num_genes): - if self.last_generation_offspring_mutation.shape[0] != self.num_offspring: - raise ValueError(f"Size mismatch between the mutation output {self.last_generation_offspring_mutation.shape} and the expected mutation output {(self.num_offspring, self.num_genes)}. It is expected to produce ({self.num_offspring}) offspring but ({self.last_generation_offspring_mutation.shape[0]}) produced.") - elif self.last_generation_offspring_mutation.shape[1] != self.num_genes: - raise ValueError(f"Size mismatch between the mutation output {self.last_generation_offspring_mutation.shape} and the expected mutation output {(self.num_offspring, self.num_genes)}. It is expected that the offspring has ({self.num_genes}) genes but ({self.last_generation_offspring_mutation.shape[1]}) produced.") - - # PyGAD 2.18.2 // The on_mutation() callback function is called even if mutation_type is None. - if not (self.on_mutation is None): - on_mutation_output = self.on_mutation(self, - self.last_generation_offspring_mutation) - - if on_mutation_output is None: - pass - else: - if type(on_mutation_output) in [tuple, list, numpy.ndarray]: - on_mutation_output = numpy.array(on_mutation_output) - if on_mutation_output.shape == self.last_generation_offspring_mutation.shape: - self.last_generation_offspring_mutation = on_mutation_output - else: - raise ValueError(f"Size mismatch between the output of on_mutation() {on_mutation_output.shape} and the expected mutation output {self.last_generation_offspring_mutation.shape}.") - else: - raise ValueError(f"The output of on_mutation() is expected to be tuple/list/numpy.ndarray but {type(on_mutation_output)} found.") - - def run_update_population(self): - """ - This method must be only called from inside the run() method. It is not meant for use by the user. - Generally, any method with a name starting with 'run_' is meant to be only called by PyGAD from inside the 'run()' method. - - The objective of the 'run_update_population()' method is to update the 'population' attribute after completing the processes of crossover and mutation. - It does not return any variables. However, it changes this attribute of the pygad.GA class instances: - 1) population: A NumPy array of the population of solutions/chromosomes. - - Returns - ------- - None. - """ - - # Update the population attribute according to the offspring generated. - if self.keep_elitism == 0: - # If the keep_elitism parameter is 0, then the keep_parents parameter will be used to decide if the parents are kept in the next generation. - if self.keep_parents == 0: - self.population = self.last_generation_offspring_mutation - elif self.keep_parents == -1: - # Creating the new population based on the parents and offspring. - self.population[0:self.last_generation_parents.shape[0],:] = self.last_generation_parents - self.population[self.last_generation_parents.shape[0]:, :] = self.last_generation_offspring_mutation - elif self.keep_parents > 0: - parents_to_keep, _ = self.steady_state_selection(self.last_generation_fitness, - num_parents=self.keep_parents) - self.population[0:parents_to_keep.shape[0],:] = parents_to_keep - self.population[parents_to_keep.shape[0]:,:] = self.last_generation_offspring_mutation - else: - self.last_generation_elitism, self.last_generation_elitism_indices = self.steady_state_selection(self.last_generation_fitness, - num_parents=self.keep_elitism) - self.population[0:self.last_generation_elitism.shape[0],:] = self.last_generation_elitism - self.population[self.last_generation_elitism.shape[0]:, :] = self.last_generation_offspring_mutation - - def best_solution(self, pop_fitness=None): - """ - Returns information about the best solution found by the genetic algorithm. - Accepts the following parameters: - pop_fitness: An optional parameter holding the fitness values of the solutions in the latest population. If passed, then it save time calculating the fitness. If None, then the 'cal_pop_fitness()' method is called to calculate the fitness of the latest population. - The following are returned: - -best_solution: Best solution in the current population. - -best_solution_fitness: Fitness value of the best solution. - -best_match_idx: Index of the best solution in the current population. - """ - - try: - if pop_fitness is None: - # If the 'pop_fitness' parameter is not passed, then we have to call the 'cal_pop_fitness()' method to calculate the fitness of all solutions in the lastest population. - pop_fitness = self.cal_pop_fitness() - # Verify the type of the 'pop_fitness' parameter. - elif type(pop_fitness) in [tuple, list, numpy.ndarray]: - # Verify that the length of the passed population fitness matches the length of the 'self.population' attribute. - if len(pop_fitness) == len(self.population): - # This successfully verifies the 'pop_fitness' parameter. - pass - else: - raise ValueError(f"The length of the list/tuple/numpy.ndarray passed to the 'pop_fitness' parameter ({len(pop_fitness)}) must match the length of the 'self.population' attribute ({len(self.population)}).") - else: - raise ValueError(f"The type of the 'pop_fitness' parameter is expected to be list, tuple, or numpy.ndarray but ({type(pop_fitness)}) found.") - - # Return the index of the best solution that has the best fitness value. - # For multi-objective optimization: find the index of the solution with the maximum fitness in the first objective, - # break ties using the second objective, then third, etc. - pop_fitness_arr = numpy.array(pop_fitness) - # Get the indices that would sort by all objectives in descending order - if pop_fitness_arr.ndim == 1: - # Single-objective optimization. - best_match_idx = numpy.where( - pop_fitness == numpy.max(pop_fitness))[0][0] - elif pop_fitness_arr.ndim == 2: - # Multi-objective optimization. - # Use NSGA-2 to sort the solutions using the fitness. - # Set find_best_solution=True to avoid overriding the pareto_fronts instance attribute. - best_match_list = self.sort_solutions_nsga2(fitness=pop_fitness, - find_best_solution=True) - - # Get the first index of the best match. - best_match_idx = best_match_list[0] - - best_solution = self.population[best_match_idx, :].copy() - best_solution_fitness = pop_fitness[best_match_idx] - except Exception as ex: - self.logger.exception(ex) - # sys.exit(-1) - raise ex - - return best_solution, best_solution_fitness, best_match_idx - def save(self, filename): """ Saves the genetic algorithm instance: @@ -2265,235 +188,6 @@ def save(self, filename): file.write(cloudpickle_serialized_object) cloudpickle.dump(self, file) - def summary(self, - line_length=70, - fill_character=" ", - line_character="-", - line_character2="=", - columns_equal_len=False, - print_step_parameters=True, - print_parameters_summary=True): - """ - The summary() method prints a summary of the PyGAD lifecycle in a Keras style. - The parameters are: - line_length: An integer representing the length of the single line in characters. - fill_character: A character to fill the lines. - line_character: A character for creating a line separator. - line_character2: A secondary character to create a line separator. - columns_equal_len: The table rows are split into equal-sized columns or split subjective to the width needed. - print_step_parameters: Whether to print extra parameters about each step inside the step. If print_step_parameters=False and print_parameters_summary=True, then the parameters of each step are printed at the end of the table. - print_parameters_summary: Whether to print parameters summary at the end of the table. If print_step_parameters=False, then the parameters of each step are printed at the end of the table too. - """ - - summary_output = "" - - def fill_message(msg, line_length=line_length, fill_character=fill_character): - num_spaces = int((line_length - len(msg))/2) - num_spaces = int(num_spaces / len(fill_character)) - msg = "{spaces}{msg}{spaces}".format( - msg=msg, spaces=fill_character * num_spaces) - return msg - - def line_separator(line_length=line_length, line_character=line_character): - num_characters = int(line_length / len(line_character)) - return line_character * num_characters - - def create_row(columns, line_length=line_length, fill_character=fill_character, split_percentages=None): - filled_columns = [] - if split_percentages is None: - split_percentages = [int(100/len(columns))] * 3 - columns_lengths = [int((split_percentages[idx] * line_length) / 100) - for idx in range(len(split_percentages))] - for column_idx, column in enumerate(columns): - current_column_length = len(column) - extra_characters = columns_lengths[column_idx] - \ - current_column_length - filled_column = column + fill_character * extra_characters - filled_columns.append(filled_column) - - return "".join(filled_columns) - - def print_parent_selection_params(): - nonlocal summary_output - m = f"Number of Parents: {self.num_parents_mating}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - if self.parent_selection_type == "tournament": - m = f"K Tournament: {self.K_tournament}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - - def print_fitness_params(): - nonlocal summary_output - if not self.fitness_batch_size is None: - m = f"Fitness batch size: {self.fitness_batch_size}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - - def print_crossover_params(): - nonlocal summary_output - if not self.crossover_probability is None: - m = f"Crossover probability: {self.crossover_probability}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - - def print_mutation_params(): - nonlocal summary_output - if not self.mutation_probability is None: - m = f"Mutation Probability: {self.mutation_probability}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - if self.mutation_percent_genes == "default": - m = f"Mutation Percentage: {self.mutation_percent_genes}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - # Number of mutation genes is already showed above. - m = f"Mutation Genes: {self.mutation_num_genes}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - m = f"Random Mutation Range: ({self.random_mutation_min_val}, {self.random_mutation_max_val})" - self.logger.info(m) - summary_output = summary_output + m + "\n" - if not self.gene_space is None: - m = f"Gene Space: {self.gene_space}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - m = f"Mutation by Replacement: {self.mutation_by_replacement}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - m = f"Allow Duplicated Genes: {self.allow_duplicate_genes}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - - def print_on_generation_params(): - nonlocal summary_output - if not self.stop_criteria is None: - m = f"Stop Criteria: {self.stop_criteria}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - - def print_params_summary(): - nonlocal summary_output - m = f"Population Size: ({self.sol_per_pop}, {self.num_genes})" - self.logger.info(m) - summary_output = summary_output + m + "\n" - m = f"Number of Generations: {self.num_generations}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - m = f"Initial Population Range: ({self.init_range_low}, {self.init_range_high})" - self.logger.info(m) - summary_output = summary_output + m + "\n" - - if not print_step_parameters: - print_fitness_params() - - if not print_step_parameters: - print_parent_selection_params() - - if self.keep_elitism != 0: - m = f"Keep Elitism: {self.keep_elitism}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - else: - m = f"Keep Parents: {self.keep_parents}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - m = f"Gene DType: {self.gene_type}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - - if not print_step_parameters: - print_crossover_params() - - if not print_step_parameters: - print_mutation_params() - - if not print_step_parameters: - print_on_generation_params() - - if not self.parallel_processing is None: - m = f"Parallel Processing: {self.parallel_processing}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - if not self.random_seed is None: - m = f"Random Seed: {self.random_seed}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - m = f"Save Best Solutions: {self.save_best_solutions}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - m = f"Save Solutions: {self.save_solutions}" - self.logger.info(m) - summary_output = summary_output + m + "\n" - - m = line_separator(line_character=line_character) - self.logger.info(m) - summary_output = summary_output + m + "\n" - m = fill_message("PyGAD Lifecycle") - self.logger.info(m) - summary_output = summary_output + m + "\n" - m = line_separator(line_character=line_character2) - self.logger.info(m) - summary_output = summary_output + m + "\n" - - lifecycle_steps = ["on_start()", "Fitness Function", "On Fitness", "Parent Selection", "On Parents", - "Crossover", "On Crossover", "Mutation", "On Mutation", "On Generation", "On Stop"] - lifecycle_functions = [self.on_start, self.fitness_func, self.on_fitness, self.select_parents, self.on_parents, - self.crossover, self.on_crossover, self.mutation, self.on_mutation, self.on_generation, self.on_stop] - lifecycle_functions = [getattr( - lifecycle_func, '__name__', "None") for lifecycle_func in lifecycle_functions] - lifecycle_functions = [lifecycle_func + "()" if lifecycle_func != - "None" else "None" for lifecycle_func in lifecycle_functions] - lifecycle_output = ["None", "(1)", "None", f"({self.num_parents_mating}, {self.num_genes})", "None", - f"({self.num_parents_mating}, {self.num_genes})", "None", f"({self.num_parents_mating}, {self.num_genes})", "None", "None", "None"] - lifecycle_step_parameters = [None, print_fitness_params, None, print_parent_selection_params, None, - print_crossover_params, None, print_mutation_params, None, print_on_generation_params, None] - - if not columns_equal_len: - max_lengthes = [max(list(map(len, lifecycle_steps))), max( - list(map(len, lifecycle_functions))), max(list(map(len, lifecycle_output)))] - split_percentages = [ - int((column_len / sum(max_lengthes)) * 100) for column_len in max_lengthes] - else: - split_percentages = None - - header_columns = ["Step", "Handler", "Output Shape"] - header_row = create_row( - header_columns, split_percentages=split_percentages) - m = header_row - self.logger.info(m) - summary_output = summary_output + m + "\n" - m = line_separator(line_character=line_character2) - self.logger.info(m) - summary_output = summary_output + m + "\n" - - for lifecycle_idx in range(len(lifecycle_steps)): - lifecycle_column = [lifecycle_steps[lifecycle_idx], - lifecycle_functions[lifecycle_idx], lifecycle_output[lifecycle_idx]] - if lifecycle_column[1] == "None": - continue - lifecycle_row = create_row( - lifecycle_column, split_percentages=split_percentages) - m = lifecycle_row - self.logger.info(m) - summary_output = summary_output + m + "\n" - if print_step_parameters: - if not lifecycle_step_parameters[lifecycle_idx] is None: - lifecycle_step_parameters[lifecycle_idx]() - m = line_separator(line_character=line_character) - self.logger.info(m) - summary_output = summary_output + m + "\n" - - m = line_separator(line_character=line_character2) - self.logger.info(m) - summary_output = summary_output + m + "\n" - if print_parameters_summary: - print_params_summary() - m = line_separator(line_character=line_character2) - self.logger.info(m) - summary_output = summary_output + m + "\n" - return summary_output - def load(filename): """ Reads a saved instance of the genetic algorithm: diff --git a/pygad/torchga/__init__.py b/pygad/torchga/__init__.py index b7b98f58..b6cae746 100644 --- a/pygad/torchga/__init__.py +++ b/pygad/torchga/__init__.py @@ -1,3 +1,3 @@ from .torchga import * -__version__ = "1.4.0" +__version__ = "1.4.1" diff --git a/pygad/torchga/torchga.py b/pygad/torchga/torchga.py index e552d3dd..2682cec4 100644 --- a/pygad/torchga/torchga.py +++ b/pygad/torchga/torchga.py @@ -10,7 +10,7 @@ def model_weights_as_vector(model): # cpu() is called for making shore the data is moved from GPU to cpu # numpy() is called for converting the tensor into a NumPy array. curr_weights = curr_weights.cpu().detach().numpy() - vector = numpy.reshape(curr_weights, newshape=(curr_weights.size)) + vector = numpy.reshape(curr_weights, (curr_weights.size)) weights_vector.extend(vector) return numpy.array(weights_vector) @@ -28,7 +28,7 @@ def model_weights_as_dict(model, weights_vector): layer_weights_size = w_matrix.size layer_weights_vector = weights_vector[start:start + layer_weights_size] - layer_weights_matrix = numpy.reshape(layer_weights_vector, newshape=(layer_weights_shape)) + layer_weights_matrix = numpy.reshape(layer_weights_vector, (layer_weights_shape)) weights_dict[key] = torch.from_numpy(layer_weights_matrix) start = start + layer_weights_size diff --git a/pygad/utils/__init__.py b/pygad/utils/__init__.py index 7d30650f..16d0a101 100644 --- a/pygad/utils/__init__.py +++ b/pygad/utils/__init__.py @@ -2,5 +2,7 @@ from pygad.utils import crossover from pygad.utils import mutation from pygad.utils import nsga2 +from pygad.utils import validation +from pygad.utils import engine -__version__ = "1.3.0" \ No newline at end of file +__version__ = "1.4.0" \ No newline at end of file diff --git a/pygad/utils/engine.py b/pygad/utils/engine.py new file mode 100644 index 00000000..91d9ae8b --- /dev/null +++ b/pygad/utils/engine.py @@ -0,0 +1,927 @@ +import numpy +import random +import warnings +import concurrent.futures + +class GAEngine: + + def round_genes(self, solutions): + for gene_idx in range(self.num_genes): + if self.gene_type_single: + if not self.gene_type[1] is None: + solutions[:, gene_idx] = numpy.round(solutions[:, gene_idx], + self.gene_type[1]) + else: + if not self.gene_type[gene_idx][1] is None: + solutions[:, gene_idx] = numpy.round(numpy.asarray(solutions[:, gene_idx], + dtype=self.gene_type[gene_idx][0]), + self.gene_type[gene_idx][1]) + return solutions + + def initialize_population(self, + allow_duplicate_genes, + gene_type, + gene_constraint): + """ + Creates an initial population randomly as a NumPy array. The array is saved in the instance attribute named 'population'. + + It accepts: + -allow_duplicate_genes: Whether duplicate genes are allowed or not. + -gene_type: The data type of the genes. + -gene_constraint: The constraints of the genes. + + This method assigns the values of the following 3 instance attributes: + 1. pop_size: Size of the population. + 2. population: Initially, holds the initial population and later updated after each generation. + 3. init_population: Keeping the initial population. + """ + + # Population size = (number of chromosomes, number of genes per chromosome) + # The population will have sol_per_pop chromosome where each chromosome has num_genes genes. + self.pop_size = (self.sol_per_pop, self.num_genes) + + # There are 4 steps to build the initial population: + # 1) Generate the population. + # 2) Change the data type and round the values. + # 3) Check for the constraints. + # 4) Solve duplicates if not allowed. + + # Create an empty population. + self.population = numpy.empty(shape=self.pop_size, dtype=object) + + # 1) Create the initial population either randomly or using the gene space. + if self.gene_space is None: + # Create the initial population randomly. + + # Set gene_value=None to consider generating values for the initial population instead of generating values for mutation. + # Loop through the genes, randomly generate the values of a single gene at a time, and insert the values of each gene to the population. + for sol_idx in range(self.sol_per_pop): + for gene_idx in range(self.num_genes): + range_min, range_max = self.get_initial_population_range(gene_index=gene_idx) + self.population[sol_idx, gene_idx] = self.generate_gene_value_randomly(range_min=range_min, + range_max=range_max, + gene_idx=gene_idx, + mutation_by_replacement=True, + gene_value=None, + sample_size=1, + step=1) + + else: + # Generate the initial population using the gene_space. + for sol_idx in range(self.sol_per_pop): + for gene_idx in range(self.num_genes): + self.population[sol_idx, gene_idx] = self.generate_gene_value_from_space(gene_idx=gene_idx, + mutation_by_replacement=True, + gene_value=None, + solution=self.population[sol_idx], + sample_size=1) + + # 2) Change the data type and round all genes within the initial population. + self.population = self.change_population_dtype_and_round(self.population) + + # Note that gene_constraint is not validated yet. + # We have to set it as a property of the pygad.GA instance to retrieve without passing it as an additional parameter. + self.gene_constraint = gene_constraint + + # 3) Enforce the gene constraints as much as possible. + if self.gene_constraint is None: + pass + else: + for sol_idx, solution in enumerate(self.population): + for gene_idx in range(self.num_genes): + # Check that a constraint is available for the gene and that the current value does not satisfy that constraint + if self.gene_constraint[gene_idx]: + # Remember that the second argument to the gene constraint callable is a list/numpy.ndarray of the values to check if they meet the gene constraint. + values = [solution[gene_idx]] + filtered_values = self.gene_constraint[gene_idx](solution, values) + result = self.validate_gene_constraint_callable_output(selected_values=filtered_values, + values=values) + if result: + pass + else: + raise Exception("The output from the gene_constraint callable/function must be a list or NumPy array that is subset of the passed values (second argument).") + + if len(filtered_values) ==1 and filtered_values[0] != solution[gene_idx]: + # Error by the user's defined gene constraint callable. + raise Exception(f"It is expected to receive a list/numpy.ndarray from the gene_constraint callable with a single value equal to {values[0]}, but the value {filtered_values[0]} found.") + + # Check if the gene value does not satisfy the gene constraint. + # Note that we already passed a list of a single value. + # It is expected to receive a list of either a single value or an empty list. + if len(filtered_values) < 1: + # Search for a value that satisfies the gene constraint. + range_min, range_max = self.get_initial_population_range(gene_index=gene_idx) + # While initializing the population, we follow a mutation by replacement approach. So, the original gene value is not needed. + values_filtered = self.get_valid_gene_constraint_values(range_min=range_min, + range_max=range_max, + gene_value=None, + gene_idx=gene_idx, + mutation_by_replacement=True, + solution=solution, + sample_size=self.sample_size) + if values_filtered is None: + if not self.suppress_warnings: + warnings.warn(f"No value satisfied the constraint for the gene at index {gene_idx} with value {solution[gene_idx]} while creating the initial population.") + else: + self.population[sol_idx, gene_idx] = random.choice(values_filtered) + elif len(filtered_values) == 1: + # The value already satisfied the gene constraint. + pass + else: + # Error by the user's defined gene constraint callable. + raise Exception(f"It is expected to receive a list/numpy.ndarray from the gene_constraint callable that is either empty or has a single value equal, but received a list/numpy.ndarray of length {len(filtered_values)}.") + + # 4) Solve duplicate genes. + if allow_duplicate_genes == False: + for solution_idx in range(self.population.shape[0]): + if self.gene_space is None: + self.population[solution_idx], _, _ = self.solve_duplicate_genes_randomly(solution=self.population[solution_idx], + min_val=self.init_range_low, + max_val=self.init_range_high, + gene_type=gene_type, + mutation_by_replacement=True, + sample_size=self.sample_size) + else: + self.population[solution_idx], _, _ = self.solve_duplicate_genes_by_space(solution=self.population[solution_idx].copy(), + gene_type=self.gene_type, + mutation_by_replacement=True, + sample_size=self.sample_size, + build_initial_pop=True) + + # Change the data type and round all genes within the initial population. + self.population = self.change_population_dtype_and_round(self.population) + + # Keeping the initial population in the initial_population attribute. + self.initial_population = self.population.copy() + + def cal_pop_fitness(self): + """ + Calculating the fitness values of batches of solutions in the current population. + It returns: + -fitness: An array of the calculated fitness values. + """ + try: + if self.valid_parameters == False: + raise Exception("ERROR calling the cal_pop_fitness() method: \nPlease check the parameters passed while creating an instance of the GA class.\n") + + # 'last_generation_parents_as_list' is the list version of 'self.last_generation_parents' + # It is used to return the parent index using the 'in' membership operator of Python lists. This is much faster than using 'numpy.where()'. + if self.last_generation_parents is not None: + last_generation_parents_as_list = self.last_generation_parents.tolist() + else: + last_generation_parents_as_list = [] + + # 'last_generation_elitism_as_list' is the list version of 'self.last_generation_elitism' + # It is used to return the elitism index using the 'in' membership operator of Python lists. This is much faster than using 'numpy.where()'. + if self.last_generation_elitism is not None: + last_generation_elitism_as_list = self.last_generation_elitism.tolist() + else: + last_generation_elitism_as_list = [] + + pop_fitness = ["undefined"] * len(self.population) + if self.parallel_processing is None: + # Calculating the fitness value of each solution in the current population. + for sol_idx, sol in enumerate(self.population): + # Check if the `save_solutions` parameter is `True` and whether the solution already exists in the `solutions` list. If so, use its fitness rather than calculating it again. + # The functions numpy.any()/numpy.all()/numpy.where()/numpy.equal() are very slow. + # So, list membership operator 'in' is used to check if the solution exists in the 'self.solutions' list. + # Make sure that both the solution and 'self.solutions' are of type 'list' not 'numpy.ndarray'. + # if (self.save_solutions) and (len(self.solutions) > 0) and (numpy.any(numpy.all(self.solutions == numpy.array(sol), axis=1))) + # if (self.save_solutions) and (len(self.solutions) > 0) and (numpy.any(numpy.all(numpy.equal(self.solutions, numpy.array(sol)), axis=1))) + + # Make sure self.best_solutions is a list of lists before proceeding. + # Because the second condition expects that best_solutions is a list of lists. + if type(self.best_solutions) is numpy.ndarray: + self.best_solutions = self.best_solutions.tolist() + + if (self.save_solutions) and (len(self.solutions) > 0) and (list(sol) in self.solutions): + solution_idx = self.solutions.index(list(sol)) + fitness = self.solutions_fitness[solution_idx] + elif (self.save_best_solutions) and (len(self.best_solutions) > 0) and (list(sol) in self.best_solutions): + solution_idx = self.best_solutions.index(list(sol)) + fitness = self.best_solutions_fitness[solution_idx] + elif (self.keep_elitism > 0) and (self.last_generation_elitism is not None) and (len(self.last_generation_elitism) > 0) and (list(sol) in last_generation_elitism_as_list): + # Return the index of the elitism from the elitism array 'self.last_generation_elitism'. + # This is not its index within the population. It is just its index in the 'self.last_generation_elitism' array. + elitism_idx = last_generation_elitism_as_list.index(list(sol)) + # Use the returned elitism index to return its index in the last population. + elitism_idx = self.last_generation_elitism_indices[elitism_idx] + # Use the elitism's index to return its pre-calculated fitness value. + fitness = self.previous_generation_fitness[elitism_idx] + # If the solutions are not saved (i.e. `save_solutions=False`), check if this solution is a parent from the previous generation and its fitness value is already calculated. If so, use the fitness value instead of calling the fitness function. + # We cannot use the `numpy.where()` function directly because it does not support the `axis` parameter. This is why the `numpy.all()` function is used to match the solutions on axis=1. + # elif (self.last_generation_parents is not None) and len(numpy.where(numpy.all(self.last_generation_parents == sol, axis=1))[0] > 0): + elif ((self.keep_parents == -1) or (self.keep_parents > 0)) and (self.last_generation_parents is not None) and (len(self.last_generation_parents) > 0) and (list(sol) in last_generation_parents_as_list): + # Index of the parent in the 'self.last_generation_parents' array. + # This is not its index within the population. It is just its index in the 'self.last_generation_parents' array. + # parent_idx = numpy.where(numpy.all(self.last_generation_parents == sol, axis=1))[0][0] + parent_idx = last_generation_parents_as_list.index(list(sol)) + # Use the returned parent index to return its index in the last population. + parent_idx = self.last_generation_parents_indices[parent_idx] + # Use the parent's index to return its pre-calculated fitness value. + fitness = self.previous_generation_fitness[parent_idx] + else: + # Check if batch processing is used. If not, then calculate this missing fitness value. + if self.fitness_batch_size in [1, None]: + fitness = self.fitness_func(self, sol, sol_idx) + if type(fitness) in self.supported_int_float_types: + # The fitness function returns a single numeric value. + # This is a single-objective optimization problem. + pass + elif type(fitness) in [list, tuple, numpy.ndarray]: + # The fitness function returns a list/tuple/numpy.ndarray. + # This is a multi-objective optimization problem. + pass + else: + raise ValueError(f"The fitness function should return a number or an iterable (list, tuple, or numpy.ndarray) but the value {fitness} of type {type(fitness)} found.") + else: + # Reaching this point means that batch processing is in effect to calculate the fitness values. + # Do not continue the loop as no fitness is calculated. The fitness will be calculated later in batch mode. + continue + + # This is only executed if the fitness value was already calculated. + pop_fitness[sol_idx] = fitness + + if self.fitness_batch_size not in [1, None]: + # Reaching this block means that batch fitness calculation is used. + + # Indices of the solutions to calculate their fitness. + solutions_indices = [idx for idx, fit in enumerate(pop_fitness) if type(fit) is str and fit == "undefined"] + # Number of batches. + num_batches = int(numpy.ceil(len(solutions_indices) / self.fitness_batch_size)) + # For each batch, get its indices and call the fitness function. + for batch_idx in range(num_batches): + batch_first_index = batch_idx * self.fitness_batch_size + batch_last_index = (batch_idx + 1) * self.fitness_batch_size + batch_indices = solutions_indices[batch_first_index:batch_last_index] + batch_solutions = self.population[batch_indices, :] + + batch_fitness = self.fitness_func( + self, batch_solutions, batch_indices) + if type(batch_fitness) not in [list, tuple, numpy.ndarray]: + raise TypeError(f"Expected to receive a list, tuple, or numpy.ndarray from the fitness function but the value ({batch_fitness}) of type {type(batch_fitness)}.") + elif len(numpy.array(batch_fitness)) != len(batch_indices): + raise ValueError(f"There is a mismatch between the number of solutions passed to the fitness function ({len(batch_indices)}) and the number of fitness values returned ({len(batch_fitness)}). They must match.") + + for index, fitness in zip(batch_indices, batch_fitness): + if type(fitness) in self.supported_int_float_types: + # The fitness function returns a single numeric value. + # This is a single-objective optimization problem. + pop_fitness[index] = fitness + elif type(fitness) in [list, tuple, numpy.ndarray]: + # The fitness function returns a list/tuple/numpy.ndarray. + # This is a multi-objective optimization problem. + pop_fitness[index] = fitness + else: + raise ValueError(f"The fitness function should return a number or an iterable (list, tuple, or numpy.ndarray) but the value {fitness} of type {type(fitness)} found.") + else: + # Calculating the fitness value of each solution in the current population. + for sol_idx, sol in enumerate(self.population): + # Check if the `save_solutions` parameter is `True` and whether the solution already exists in the `solutions` list. If so, use its fitness rather than calculating it again. + # The functions numpy.any()/numpy.all()/numpy.where()/numpy.equal() are very slow. + # So, list membership operator 'in' is used to check if the solution exists in the 'self.solutions' list. + # Make sure that both the solution and 'self.solutions' are of type 'list' not 'numpy.ndarray'. + if (self.save_solutions) and (len(self.solutions) > 0) and (list(sol) in self.solutions): + solution_idx = self.solutions.index(list(sol)) + fitness = self.solutions_fitness[solution_idx] + pop_fitness[sol_idx] = fitness + elif (self.keep_elitism > 0) and (self.last_generation_elitism is not None) and (len(self.last_generation_elitism) > 0) and (list(sol) in last_generation_elitism_as_list): + # Return the index of the elitism from the elitism array 'self.last_generation_elitism'. + # This is not its index within the population. It is just its index in the 'self.last_generation_elitism' array. + elitism_idx = last_generation_elitism_as_list.index( + list(sol)) + # Use the returned elitism index to return its index in the last population. + elitism_idx = self.last_generation_elitism_indices[elitism_idx] + # Use the elitism's index to return its pre-calculated fitness value. + fitness = self.previous_generation_fitness[elitism_idx] + + pop_fitness[sol_idx] = fitness + # If the solutions are not saved (i.e. `save_solutions=False`), check if this solution is a parent from the previous generation and its fitness value is already calculated. If so, use the fitness value instead of calling the fitness function. + # We cannot use the `numpy.where()` function directly because it does not support the `axis` parameter. This is why the `numpy.all()` function is used to match the solutions on axis=1. + # elif (self.last_generation_parents is not None) and len(numpy.where(numpy.all(self.last_generation_parents == sol, axis=1))[0] > 0): + elif ((self.keep_parents == -1) or (self.keep_parents > 0)) and (self.last_generation_parents is not None) and (len(self.last_generation_parents) > 0) and (list(sol) in last_generation_parents_as_list): + # Index of the parent in the 'self.last_generation_parents' array. + # This is not its index within the population. It is just its index in the 'self.last_generation_parents' array. + # parent_idx = numpy.where(numpy.all(self.last_generation_parents == sol, axis=1))[0][0] + parent_idx = last_generation_parents_as_list.index( + list(sol)) + # Use the returned parent index to return its index in the last population. + parent_idx = self.last_generation_parents_indices[parent_idx] + # Use the parent's index to return its pre-calculated fitness value. + fitness = self.previous_generation_fitness[parent_idx] + + pop_fitness[sol_idx] = fitness + + # Decide which class to use based on whether the user selected "process" or "thread" + if self.parallel_processing[0] == "process": + ExecutorClass = concurrent.futures.ProcessPoolExecutor + else: + ExecutorClass = concurrent.futures.ThreadPoolExecutor + + # We can use a with statement to ensure threads are cleaned up promptly (https://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor-example) + with ExecutorClass(max_workers=self.parallel_processing[1]) as executor: + solutions_to_submit_indices = [] + solutions_to_submit = [] + for sol_idx, sol in enumerate(self.population): + # The "undefined" value means that the fitness of this solution must be calculated. + if type(pop_fitness[sol_idx]) is str: + if pop_fitness[sol_idx] == "undefined": + solutions_to_submit.append(sol.copy()) + solutions_to_submit_indices.append(sol_idx) + elif type(pop_fitness[sol_idx]) in [list, tuple, numpy.ndarray]: + # This is a multi-objective problem. The fitness is already calculated. Nothing to do. + pass + + # Check if batch processing is used. If not, then calculate the fitness value for individual solutions. + if self.fitness_batch_size in [1, None]: + for index, fitness in zip(solutions_to_submit_indices, executor.map(self.fitness_func, [self]*len(solutions_to_submit_indices), solutions_to_submit, solutions_to_submit_indices)): + if type(fitness) in self.supported_int_float_types: + # The fitness function returns a single numeric value. + # This is a single-objective optimization problem. + pop_fitness[index] = fitness + elif type(fitness) in [list, tuple, numpy.ndarray]: + # The fitness function returns a list/tuple/numpy.ndarray. + # This is a multi-objective optimization problem. + pop_fitness[index] = fitness + else: + raise ValueError(f"The fitness function should return a number or an iterable (list, tuple, or numpy.ndarray) but the value {fitness} of type {type(fitness)} found.") + else: + # Reaching this block means that batch processing is used. The fitness values are calculated in batches. + + # Number of batches. + num_batches = int(numpy.ceil(len(solutions_to_submit_indices) / self.fitness_batch_size)) + # Each element of the `batches_solutions` list represents the solutions in one batch. + batches_solutions = [] + # Each element of the `batches_indices` list represents the solutions' indices in one batch. + batches_indices = [] + # For each batch, get its indices and call the fitness function. + for batch_idx in range(num_batches): + batch_first_index = batch_idx * self.fitness_batch_size + batch_last_index = (batch_idx + 1) * self.fitness_batch_size + batch_indices = solutions_to_submit_indices[batch_first_index:batch_last_index] + batch_solutions = self.population[batch_indices, :] + + batches_solutions.append(batch_solutions) + batches_indices.append(batch_indices) + + for batch_indices, batch_fitness in zip(batches_indices, executor.map(self.fitness_func, [self]*len(solutions_to_submit_indices), batches_solutions, batches_indices)): + if type(batch_fitness) not in [list, tuple, numpy.ndarray]: + raise TypeError(f"Expected to receive a list, tuple, or numpy.ndarray from the fitness function but the value ({batch_fitness}) of type {type(batch_fitness)}.") + elif len(numpy.array(batch_fitness)) != len(batch_indices): + raise ValueError(f"There is a mismatch between the number of solutions passed to the fitness function ({len(batch_indices)}) and the number of fitness values returned ({len(batch_fitness)}). They must match.") + + for index, fitness in zip(batch_indices, batch_fitness): + if type(fitness) in self.supported_int_float_types: + # The fitness function returns a single numeric value. + # This is a single-objective optimization problem. + pop_fitness[index] = fitness + elif type(fitness) in [list, tuple, numpy.ndarray]: + # The fitness function returns a list/tuple/numpy.ndarray. + # This is a multi-objective optimization problem. + pop_fitness[index] = fitness + else: + raise ValueError(f"The fitness function should return a number or an iterable (list, tuple, or numpy.ndarray) but the value {fitness} of type {type(fitness)} found.") + + pop_fitness = numpy.array(pop_fitness) + except Exception as ex: + self.logger.exception(ex) + # sys.exit(-1) + raise ex + return pop_fitness + + def run(self): + """ + Runs the genetic algorithm. This is the main method in which the genetic algorithm is evolved through a number of generations. + """ + try: + if self.valid_parameters == False: + raise Exception("Error calling the run() method: \nThe run() method cannot be executed with invalid parameters. Please check the parameters passed while creating an instance of the GA class.\n") + + # Starting from PyGAD 2.18.0, the 4 properties (best_solutions, best_solutions_fitness, solutions, and solutions_fitness) are no longer reset with each call to the run() method. Instead, they are extended. + # For example, if there are 50 generations and the user set save_best_solutions=True, then the length of the 2 properties best_solutions and best_solutions_fitness will be 50 after the first call to the run() method, then 100 after the second call, 150 after the third, and so on. + + # self.best_solutions: Holds the best solution in each generation. + if type(self.best_solutions) is numpy.ndarray: + self.best_solutions = self.best_solutions.tolist() + # self.best_solutions_fitness: A list holding the fitness value of the best solution for each generation. + if type(self.best_solutions_fitness) is numpy.ndarray: + self.best_solutions_fitness = list(self.best_solutions_fitness) + # self.solutions: Holds the solutions in each generation. + if type(self.solutions) is numpy.ndarray: + self.solutions = self.solutions.tolist() + # self.solutions_fitness: Holds the fitness of the solutions in each generation. + if type(self.solutions_fitness) is numpy.ndarray: + self.solutions_fitness = list(self.solutions_fitness) + + if not (self.on_start is None): + self.on_start(self) + + stop_run = False + + # To continue from where we stopped, the first generation index should start from the value of the 'self.generations_completed' parameter. + if self.generations_completed != 0 and type(self.generations_completed) in self.supported_int_types: + # If the 'self.generations_completed' parameter is not '0', then this means we continue execution. + generation_first_idx = self.generations_completed + generation_last_idx = self.num_generations + self.generations_completed + else: + # If the 'self.generations_completed' parameter is '0', then stat from scratch. + generation_first_idx = 0 + generation_last_idx = self.num_generations + + # Measuring the fitness of each chromosome in the population. Save the fitness in the last_generation_fitness attribute. + self.last_generation_fitness = self.cal_pop_fitness() + + # Know whether the problem is SOO or MOO. + if type(self.last_generation_fitness[0]) in self.supported_int_float_types: + # Single-objective problem. + # If the problem is SOO, the parent selection type cannot be nsga2 or tournament_nsga2. + if self.parent_selection_type in ['nsga2', 'tournament_nsga2']: + raise TypeError(f"Incorrect parent selection type. The fitness function returned a single numeric fitness value which means the problem is single-objective. But the parent selection type {self.parent_selection_type} is used which only works for multi-objective optimization problems.") + elif type(self.last_generation_fitness[0]) in [list, tuple, numpy.ndarray]: + # Multi-objective problem. + pass + + best_solution, best_solution_fitness, best_match_idx = self.best_solution(pop_fitness=self.last_generation_fitness) + + # Appending the best solution in the initial population to the best_solutions list. + if self.save_best_solutions: + self.best_solutions.append(list(best_solution)) + + for generation in range(generation_first_idx, generation_last_idx): + + self.run_loop_head(best_solution_fitness) + + # Call the 'run_select_parents()' method to select the parents. + # It edits these 2 instance attributes: + # 1) last_generation_parents: A NumPy array of the selected parents. + # 2) last_generation_parents_indices: A 1D NumPy array of the indices of the selected parents. + self.run_select_parents() + + # Call the 'run_crossover()' method to select the offspring. + # It edits these 2 instance attributes: + # 1) last_generation_offspring_crossover: A NumPy array of the selected offspring. + # 2) last_generation_elitism: A NumPy array of the current generation elitism. Applicable only if the 'keep_elitism' parameter > 0. + self.run_crossover() + + # Call the 'run_mutation()' method to mutate the selected offspring. + # It edits this instance attribute: + # 1) last_generation_offspring_mutation: A NumPy array of the mutated offspring. + self.run_mutation() + + # Call the 'run_update_population()' method to update the population after both crossover and mutation operations complete. + # It edits this instance attribute: + # 1) population: A NumPy array of the population of solutions/chromosomes. + self.run_update_population() + + # The generations_completed attribute holds the number of the last completed generation. + self.generations_completed = generation + 1 + + self.previous_generation_fitness = self.last_generation_fitness.copy() + # Measuring the fitness of each chromosome in the population. Save the fitness in the last_generation_fitness attribute. + self.last_generation_fitness = self.cal_pop_fitness() + + best_solution, best_solution_fitness, best_match_idx = self.best_solution( + pop_fitness=self.last_generation_fitness) + + # Appending the best solution in the current generation to the best_solutions list. + if self.save_best_solutions: + self.best_solutions.append(list(best_solution)) + + # Note: Any code that has loop-dependant statements (e.g. continue, break, etc.) must be kept inside the loop of the 'run()' method. It can be moved to another method to clean the run() method. + # If the on_generation attribute is not None, then cal the callback function after the generation. + if not (self.on_generation is None): + r = self.on_generation(self) + if type(r) is str and r.lower() == "stop": + # Before aborting the loop, save the fitness value of the best solution. + # _, best_solution_fitness, _ = self.best_solution() + self.best_solutions_fitness.append(best_solution_fitness) + break + + if not self.stop_criteria is None: + for criterion in self.stop_criteria: + if criterion[0] == "reach": + # Single-objective problem. + if type(self.last_generation_fitness[0]) in self.supported_int_float_types: + if max(self.last_generation_fitness) >= criterion[1]: + stop_run = True + break + # Multi-objective problem. + elif type(self.last_generation_fitness[0]) in [list, tuple, numpy.ndarray]: + # Validate the value passed to the criterion. + if len(criterion[1:]) == 1: + # There is a single value used across all the objectives. + pass + elif len(criterion[1:]) > 1: + # There are multiple values. The number of values must be equal to the number of objectives. + if len(criterion[1:]) == len(self.last_generation_fitness[0]): + pass + else: + self.valid_parameters = False + raise ValueError(f"When the the 'reach' keyword is used with the 'stop_criteria' parameter for solving a multi-objective problem, then the number of numeric values following the keyword can be:\n1) A single numeric value to be used across all the objective functions.\n2) A number of numeric values equal to the number of objective functions.\nBut the value {criterion} found with {len(criterion)-1} numeric values which is not equal to the number of objective functions {len(self.last_generation_fitness[0])}.") + + stop_run = True + for obj_idx in range(len(self.last_generation_fitness[0])): + # Use the objective index to return the proper value for the criterion. + + if len(criterion[1:]) == len(self.last_generation_fitness[0]): + reach_fitness_value = criterion[obj_idx + 1] + elif len(criterion[1:]) == 1: + reach_fitness_value = criterion[1] + else: + # Unexpected to be reached, but it is safer to handle it. + self.valid_parameters = False + raise ValueError(f"The number of values {len(criterion[1:])} does not equal the number of objectives {len(self.last_generation_fitness[0])}.") + + if max(self.last_generation_fitness[:, obj_idx]) >= reach_fitness_value: + pass + else: + stop_run = False + break + elif criterion[0] == "saturate": + criterion[1] = int(criterion[1]) + if self.generations_completed >= criterion[1]: + # Single-objective problem. + if type(self.last_generation_fitness[0]) in self.supported_int_float_types: + if (self.best_solutions_fitness[self.generations_completed - criterion[1]] - self.best_solutions_fitness[self.generations_completed - 1]) == 0: + stop_run = True + break + # Multi-objective problem. + elif type(self.last_generation_fitness[0]) in [list, tuple, numpy.ndarray]: + stop_run = True + for obj_idx in range(len(self.last_generation_fitness[0])): + if (self.best_solutions_fitness[self.generations_completed - criterion[1]][obj_idx] - self.best_solutions_fitness[self.generations_completed - 1][obj_idx]) == 0: + pass + else: + stop_run = False + break + + if stop_run: + break + + # Save the fitness of the last generation. + if self.save_solutions: + # self.solutions.extend(self.population.copy()) + population_as_list = self.population.copy() + population_as_list = [list(item) for item in population_as_list] + self.solutions.extend(population_as_list) + + self.solutions_fitness.extend(self.last_generation_fitness) + + # Call the run_select_parents() method to update these 2 attributes according to the 'last_generation_fitness' attribute: + # 1) last_generation_parents 2) last_generation_parents_indices + # Set 'call_on_parents=False' to avoid calling the callable 'on_parents' because this step is not part of the cycle. + self.run_select_parents(call_on_parents=False) + + # Update the elitism according to the 'last_generation_fitness' attribute. + if self.keep_elitism > 0: + self.last_generation_elitism, self.last_generation_elitism_indices = self.steady_state_selection(self.last_generation_fitness, + num_parents=self.keep_elitism) + + # Save the fitness value of the best solution. + _, best_solution_fitness, _ = self.best_solution( + pop_fitness=self.last_generation_fitness) + self.best_solutions_fitness.append(best_solution_fitness) + + self.best_solution_generation = numpy.where(numpy.array( + self.best_solutions_fitness) == numpy.max(numpy.array(self.best_solutions_fitness)))[0][0] + # After the run() method completes, the run_completed flag is changed from False to True. + # Set to True only after the run() method completes gracefully. + self.run_completed = True + + if not (self.on_stop is None): + self.on_stop(self, self.last_generation_fitness) + + # Converting the 'best_solutions' list into a NumPy array. + self.best_solutions = numpy.array(self.best_solutions) + + # Update previous_generation_fitness because it is used to get the fitness of the parents. + self.previous_generation_fitness = self.last_generation_fitness.copy() + + # Converting the 'solutions' list into a NumPy array. + # self.solutions = numpy.array(self.solutions) + except Exception as ex: + self.logger.exception(ex) + # sys.exit(-1) + raise ex + + def run_loop_head(self, best_solution_fitness): + if not (self.on_fitness is None): + on_fitness_output = self.on_fitness(self, + self.last_generation_fitness) + + if on_fitness_output is None: + pass + else: + if type(on_fitness_output) in [tuple, list, numpy.ndarray, range]: + on_fitness_output = numpy.array(on_fitness_output) + if on_fitness_output.shape == self.last_generation_fitness.shape: + self.last_generation_fitness = on_fitness_output + else: + raise ValueError(f"Size mismatch between the output of on_fitness() {on_fitness_output.shape} and the expected fitness output {self.last_generation_fitness.shape}.") + else: + raise ValueError(f"The output of on_fitness() is expected to be tuple/list/range/numpy.ndarray but {type(on_fitness_output)} found.") + + # Appending the fitness value of the best solution in the current generation to the best_solutions_fitness attribute. + self.best_solutions_fitness.append(best_solution_fitness) + + # Appending the solutions in the current generation to the solutions list. + if self.save_solutions: + # self.solutions.extend(self.population.copy()) + population_as_list = self.population.copy() + population_as_list = [list(item) for item in population_as_list] + self.solutions.extend(population_as_list) + + self.solutions_fitness.extend(self.last_generation_fitness) + + def run_select_parents(self, call_on_parents=True): + """ + This method must be only called from inside the run() method. It is not meant for use by the user. + Generally, any method with a name starting with 'run_' is meant to be only called by PyGAD from inside the 'run()' method. + + The objective of the 'run_select_parents()' method is to select the parents and call the callable on_parents() if defined. + It does not return any variables. However, it changes these 2 attributes of the pygad.GA class instances: + 1) last_generation_parents: A NumPy array of the selected parents. + 2) last_generation_parents_indices: A 1D NumPy array of the indices of the selected parents. + + Parameters + ---------- + call_on_parents : bool, optional + If True, then the callable 'on_parents()' is called. The default is True. + + Returns + ------- + None. + """ + + # Selecting the best parents in the population for mating. + if callable(self.parent_selection_type): + self.last_generation_parents, self.last_generation_parents_indices = self.select_parents(self.last_generation_fitness, + self.num_parents_mating, + self) + if not type(self.last_generation_parents) is numpy.ndarray: + raise TypeError(f"The type of the iterable holding the selected parents is expected to be (numpy.ndarray) but {type(self.last_generation_parents)} found.") + if not type(self.last_generation_parents_indices) is numpy.ndarray: + raise TypeError(f"The type of the iterable holding the selected parents' indices is expected to be (numpy.ndarray) but {type(self.last_generation_parents_indices)} found.") + else: + self.last_generation_parents, self.last_generation_parents_indices = self.select_parents(self.last_generation_fitness, + num_parents=self.num_parents_mating) + + # Validate the output of the parent selection step: self.select_parents() + if self.last_generation_parents.shape != (self.num_parents_mating, self.num_genes): + if self.last_generation_parents.shape[0] != self.num_parents_mating: + raise ValueError(f"Size mismatch between the size of the selected parents {self.last_generation_parents.shape} and the expected size {(self.num_parents_mating, self.num_genes)}. It is expected to select ({self.num_parents_mating}) parents but ({self.last_generation_parents.shape[0]}) selected.") + elif self.last_generation_parents.shape[1] != self.num_genes: + raise ValueError(f"Size mismatch between the size of the selected parents {self.last_generation_parents.shape} and the expected size {(self.num_parents_mating, self.num_genes)}. Parents are expected to have ({self.num_genes}) genes but ({self.last_generation_parents.shape[1]}) produced.") + + if self.last_generation_parents_indices.ndim != 1: + raise ValueError(f"The iterable holding the selected parents indices is expected to have 1 dimension but ({len(self.last_generation_parents_indices)}) found.") + elif len(self.last_generation_parents_indices) != self.num_parents_mating: + raise ValueError(f"The iterable holding the selected parents indices is expected to have ({self.num_parents_mating}) values but ({len(self.last_generation_parents_indices)}) found.") + + if call_on_parents: + if not (self.on_parents is None): + on_parents_output = self.on_parents(self, + self.last_generation_parents) + + if on_parents_output is None: + pass + elif type(on_parents_output) in [list, tuple, numpy.ndarray]: + if len(on_parents_output) == 2: + on_parents_selected_parents, on_parents_selected_parents_indices = on_parents_output + else: + raise ValueError(f"The output of on_parents() is expected to be tuple/list/numpy.ndarray of length 2 but {type(on_parents_output)} of length {len(on_parents_output)} found.") + + # Validate the parents. + if on_parents_selected_parents is None: + raise ValueError("The returned outputs of on_parents() cannot be None but the first output is None.") + else: + if type(on_parents_selected_parents) in [tuple, list, numpy.ndarray]: + on_parents_selected_parents = numpy.array(on_parents_selected_parents) + if on_parents_selected_parents.shape == self.last_generation_parents.shape: + self.last_generation_parents = on_parents_selected_parents + else: + raise ValueError(f"Size mismatch between the parents returned by on_parents() {on_parents_selected_parents.shape} and the expected parents shape {self.last_generation_parents.shape}.") + else: + raise ValueError(f"The output of on_parents() is expected to be tuple/list/numpy.ndarray but the first output type is {type(on_parents_selected_parents)}.") + + # Validate the parents indices. + if on_parents_selected_parents_indices is None: + raise ValueError("The returned outputs of on_parents() cannot be None but the second output is None.") + else: + if type(on_parents_selected_parents_indices) in [tuple, list, numpy.ndarray, range]: + on_parents_selected_parents_indices = numpy.array(on_parents_selected_parents_indices) + if on_parents_selected_parents_indices.shape == self.last_generation_parents_indices.shape: + # Add this new instance attribute. + self.last_generation_parents_indices = on_parents_selected_parents_indices + else: + raise ValueError(f"Size mismatch between the parents indices returned by on_parents() {on_parents_selected_parents_indices.shape} and the expected crossover output {self.last_generation_parents_indices.shape}.") + else: + raise ValueError(f"The output of on_parents() is expected to be tuple/list/range/numpy.ndarray but the second output type is {type(on_parents_selected_parents_indices)}.") + + else: + raise TypeError(f"The output of on_parents() is expected to be tuple/list/numpy.ndarray but {type(on_parents_output)} found.") + + def run_crossover(self): + """ + This method must be only called from inside the run() method. It is not meant for use by the user. + Generally, any method with a name starting with 'run_' is meant to be only called by PyGAD from inside the 'run()' method. + + The objective of the 'run_crossover()' method is to apply crossover and call the callable on_crossover() if defined. + It does not return any variables. However, it changes these 2 attributes of the pygad.GA class instances: + 1) last_generation_offspring_crossover: A NumPy array of the selected offspring. + 2) last_generation_elitism: A NumPy array of the current generation elitism. Applicable only if the 'keep_elitism' parameter > 0. + + Returns + ------- + None. + """ + + # If self.crossover_type=None, then no crossover is applied and thus no offspring will be created in the next generations. The next generation will use the solutions in the current population. + if self.crossover_type is None: + if self.keep_elitism == 0: + num_parents_to_keep = self.num_parents_mating if self.keep_parents == - 1 else self.keep_parents + if self.num_offspring <= num_parents_to_keep: + self.last_generation_offspring_crossover = self.last_generation_parents[0:self.num_offspring] + else: + self.last_generation_offspring_crossover = numpy.concatenate( + (self.last_generation_parents, self.population[0:(self.num_offspring - self.last_generation_parents.shape[0])])) + else: + # The steady_state_selection() function is called to select the best solutions (i.e. elitism). The keep_elitism parameter defines the number of these solutions. + # The steady_state_selection() function is still called here even if its output may not be used given that the condition of the next if statement is True. The reason is that it will be used later. + self.last_generation_elitism, _ = self.steady_state_selection(self.last_generation_fitness, + num_parents=self.keep_elitism) + if self.num_offspring <= self.keep_elitism: + self.last_generation_offspring_crossover = self.last_generation_parents[0:self.num_offspring] + else: + self.last_generation_offspring_crossover = numpy.concatenate( + (self.last_generation_elitism, self.population[0:(self.num_offspring - self.last_generation_elitism.shape[0])])) + else: + # Generating offspring using crossover. + if callable(self.crossover_type): + self.last_generation_offspring_crossover = self.crossover(self.last_generation_parents, + (self.num_offspring, self.num_genes), + self) + if not type(self.last_generation_offspring_crossover) is numpy.ndarray: + raise TypeError(f"The output of the crossover step is expected to be of type (numpy.ndarray) but {type(self.last_generation_offspring_crossover)} found.") + else: + self.last_generation_offspring_crossover = self.crossover(self.last_generation_parents, + offspring_size=(self.num_offspring, self.num_genes)) + if self.last_generation_offspring_crossover.shape != (self.num_offspring, self.num_genes): + if self.last_generation_offspring_crossover.shape[0] != self.num_offspring: + raise ValueError(f"Size mismatch between the crossover output {self.last_generation_offspring_crossover.shape} and the expected crossover output {(self.num_offspring, self.num_genes)}. It is expected to produce ({self.num_offspring}) offspring but ({self.last_generation_offspring_crossover.shape[0]}) produced.") + elif self.last_generation_offspring_crossover.shape[1] != self.num_genes: + raise ValueError(f"Size mismatch between the crossover output {self.last_generation_offspring_crossover.shape} and the expected crossover output {(self.num_offspring, self.num_genes)}. It is expected that the offspring has ({self.num_genes}) genes but ({self.last_generation_offspring_crossover.shape[1]}) produced.") + + # PyGAD 2.18.2 // The on_crossover() callback function is called even if crossover_type is None. + if not (self.on_crossover is None): + on_crossover_output = self.on_crossover(self, + self.last_generation_offspring_crossover) + if on_crossover_output is None: + pass + else: + if type(on_crossover_output) in [tuple, list, numpy.ndarray]: + on_crossover_output = numpy.array(on_crossover_output) + if on_crossover_output.shape == self.last_generation_offspring_crossover.shape: + self.last_generation_offspring_crossover = on_crossover_output + else: + raise ValueError(f"Size mismatch between the output of on_crossover() {on_crossover_output.shape} and the expected crossover output {self.last_generation_offspring_crossover.shape}.") + else: + raise ValueError(f"The output of on_crossover() is expected to be tuple/list/numpy.ndarray but {type(on_crossover_output)} found.") + + def run_mutation(self): + """ + This method must be only called from inside the run() method. It is not meant for use by the user. + Generally, any method with a name starting with 'run_' is meant to be only called by PyGAD from inside the 'run()' method. + + The objective of the 'run_mutation()' method is to apply mutation and call the callable on_mutation() if defined. + It does not return any variables. However, it changes this attribute of the pygad.GA class instances: + 1) last_generation_offspring_mutation: A NumPy array of the mutated offspring. + + Returns + ------- + None. + """ + + # If self.mutation_type=None, then no mutation is applied and thus no changes are applied to the offspring created using the crossover operation. The offspring will be used unchanged in the next generation. + if self.mutation_type is None: + self.last_generation_offspring_mutation = self.last_generation_offspring_crossover + else: + # Adding some variations to the offspring using mutation. + if callable(self.mutation_type): + self.last_generation_offspring_mutation = self.mutation(self.last_generation_offspring_crossover, + self) + if not type(self.last_generation_offspring_mutation) is numpy.ndarray: + raise TypeError(f"The output of the mutation step is expected to be of type (numpy.ndarray) but {type(self.last_generation_offspring_mutation)} found.") + else: + self.last_generation_offspring_mutation = self.mutation(self.last_generation_offspring_crossover) + + if self.last_generation_offspring_mutation.shape != (self.num_offspring, self.num_genes): + if self.last_generation_offspring_mutation.shape[0] != self.num_offspring: + raise ValueError(f"Size mismatch between the mutation output {self.last_generation_offspring_mutation.shape} and the expected mutation output {(self.num_offspring, self.num_genes)}. It is expected to produce ({self.num_offspring}) offspring but ({self.last_generation_offspring_mutation.shape[0]}) produced.") + elif self.last_generation_offspring_mutation.shape[1] != self.num_genes: + raise ValueError(f"Size mismatch between the mutation output {self.last_generation_offspring_mutation.shape} and the expected mutation output {(self.num_offspring, self.num_genes)}. It is expected that the offspring has ({self.num_genes}) genes but ({self.last_generation_offspring_mutation.shape[1]}) produced.") + + # PyGAD 2.18.2 // The on_mutation() callback function is called even if mutation_type is None. + if not (self.on_mutation is None): + on_mutation_output = self.on_mutation(self, + self.last_generation_offspring_mutation) + + if on_mutation_output is None: + pass + else: + if type(on_mutation_output) in [tuple, list, numpy.ndarray]: + on_mutation_output = numpy.array(on_mutation_output) + if on_mutation_output.shape == self.last_generation_offspring_mutation.shape: + self.last_generation_offspring_mutation = on_mutation_output + else: + raise ValueError(f"Size mismatch between the output of on_mutation() {on_mutation_output.shape} and the expected mutation output {self.last_generation_offspring_mutation.shape}.") + else: + raise ValueError(f"The output of on_mutation() is expected to be tuple/list/numpy.ndarray but {type(on_mutation_output)} found.") + + def run_update_population(self): + """ + This method must be only called from inside the run() method. It is not meant for use by the user. + Generally, any method with a name starting with 'run_' is meant to be only called by PyGAD from inside the 'run()' method. + + The objective of the 'run_update_population()' method is to update the 'population' attribute after completing the processes of crossover and mutation. + It does not return any variables. However, it changes this attribute of the pygad.GA class instances: + 1) population: A NumPy array of the population of solutions/chromosomes. + + Returns + ------- + None. + """ + + # Update the population attribute according to the offspring generated. + if self.keep_elitism == 0: + # If the keep_elitism parameter is 0, then the keep_parents parameter will be used to decide if the parents are kept in the next generation. + if self.keep_parents == 0: + self.population = self.last_generation_offspring_mutation + elif self.keep_parents == -1: + # Creating the new population based on the parents and offspring. + self.population[0:self.last_generation_parents.shape[0],:] = self.last_generation_parents + self.population[self.last_generation_parents.shape[0]:, :] = self.last_generation_offspring_mutation + elif self.keep_parents > 0: + parents_to_keep, _ = self.steady_state_selection(self.last_generation_fitness, + num_parents=self.keep_parents) + self.population[0:parents_to_keep.shape[0],:] = parents_to_keep + self.population[parents_to_keep.shape[0]:,:] = self.last_generation_offspring_mutation + else: + self.last_generation_elitism, self.last_generation_elitism_indices = self.steady_state_selection(self.last_generation_fitness, + num_parents=self.keep_elitism) + self.population[0:self.last_generation_elitism.shape[0],:] = self.last_generation_elitism + self.population[self.last_generation_elitism.shape[0]:, :] = self.last_generation_offspring_mutation + + def best_solution(self, pop_fitness=None): + """ + Returns information about the best solution found by the genetic algorithm. + Accepts the following parameters: + pop_fitness: An optional parameter holding the fitness values of the solutions in the latest population. If passed, then it save time calculating the fitness. If None, then the 'cal_pop_fitness()' method is called to calculate the fitness of the latest population. + The following are returned: + -best_solution: Best solution in the current population. + -best_solution_fitness: Fitness value of the best solution. + -best_match_idx: Index of the best solution in the current population. + """ + + try: + if pop_fitness is None: + # If the 'pop_fitness' parameter is not passed, then we have to call the 'cal_pop_fitness()' method to calculate the fitness of all solutions in the lastest population. + pop_fitness = self.cal_pop_fitness() + # Verify the type of the 'pop_fitness' parameter. + elif type(pop_fitness) in [tuple, list, numpy.ndarray]: + # Verify that the length of the passed population fitness matches the length of the 'self.population' attribute. + if len(pop_fitness) == len(self.population): + # This successfully verifies the 'pop_fitness' parameter. + pass + else: + raise ValueError(f"The length of the list/tuple/numpy.ndarray passed to the 'pop_fitness' parameter ({len(pop_fitness)}) must match the length of the 'self.population' attribute ({len(self.population)}).") + else: + raise ValueError(f"The type of the 'pop_fitness' parameter is expected to be list, tuple, or numpy.ndarray but ({type(pop_fitness)}) found.") + + # Return the index of the best solution that has the best fitness value. + # For multi-objective optimization: find the index of the solution with the maximum fitness in the first objective, + # break ties using the second objective, then third, etc. + pop_fitness_arr = numpy.array(pop_fitness) + # Get the indices that would sort by all objectives in descending order + if pop_fitness_arr.ndim == 1: + # Single-objective optimization. + best_match_idx = numpy.where( + pop_fitness == numpy.max(pop_fitness))[0][0] + elif pop_fitness_arr.ndim == 2: + # Multi-objective optimization. + # Use NSGA-2 to sort the solutions using the fitness. + # Set find_best_solution=True to avoid overriding the pareto_fronts instance attribute. + best_match_list = self.sort_solutions_nsga2(fitness=pop_fitness, + find_best_solution=True) + + # Get the first index of the best match. + best_match_idx = best_match_list[0] + + best_solution = self.population[best_match_idx, :].copy() + best_solution_fitness = pop_fitness[best_match_idx] + except Exception as ex: + self.logger.exception(ex) + # sys.exit(-1) + raise ex + + return best_solution, best_solution_fitness, best_match_idx diff --git a/pygad/utils/mutation.py b/pygad/utils/mutation.py index d51b7edc..a9ad0513 100644 --- a/pygad/utils/mutation.py +++ b/pygad/utils/mutation.py @@ -66,18 +66,7 @@ def mutation_by_space(self, offspring): sample_size=self.sample_size) # Before assigning the selected value from the space to the gene, change its data type and round it. - if self.gene_type_single == True: - if not self.gene_type[1] is None: - offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[0](value_from_space), - self.gene_type[1]) - else: - offspring[offspring_idx, gene_idx] = self.gene_type[0](value_from_space) - else: - if not self.gene_type[gene_idx][1] is None: - offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[gene_idx][0](value_from_space), - self.gene_type[gene_idx][1]) - else: - offspring[offspring_idx, gene_idx] = self.gene_type[gene_idx][0](value_from_space) + offspring[offspring_idx, gene_idx] = self.change_gene_dtype_and_round(gene_idx, value_from_space) if self.allow_duplicate_genes == False: offspring[offspring_idx], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[offspring_idx], @@ -107,18 +96,7 @@ def mutation_probs_by_space(self, offspring): sample_size=self.sample_size) # Assigning the selected value from the space to the gene. - if self.gene_type_single == True: - if not self.gene_type[1] is None: - offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[0](value_from_space), - self.gene_type[1]) - else: - offspring[offspring_idx, gene_idx] = self.gene_type[0](value_from_space) - else: - if not self.gene_type[gene_idx][1] is None: - offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[gene_idx][0](value_from_space), - self.gene_type[gene_idx][1]) - else: - offspring[offspring_idx, gene_idx] = self.gene_type[gene_idx][0](value_from_space) + offspring[offspring_idx, gene_idx] = self.change_gene_dtype_and_round(gene_idx, value_from_space) if self.allow_duplicate_genes == False: offspring[offspring_idx], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[offspring_idx], @@ -540,18 +518,7 @@ def adaptive_mutation_by_space(self, offspring): sample_size=self.sample_size) # Assigning the selected value from the space to the gene. - if self.gene_type_single == True: - if not self.gene_type[1] is None: - offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[0](value_from_space), - self.gene_type[1]) - else: - offspring[offspring_idx, gene_idx] = self.gene_type[0](value_from_space) - else: - if not self.gene_type[gene_idx][1] is None: - offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[gene_idx][0](value_from_space), - self.gene_type[gene_idx][1]) - else: - offspring[offspring_idx, gene_idx] = self.gene_type[gene_idx][0](value_from_space) + offspring[offspring_idx, gene_idx] = self.change_gene_dtype_and_round(gene_idx, value_from_space) if self.allow_duplicate_genes == False: offspring[offspring_idx], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[offspring_idx], @@ -678,18 +645,7 @@ def adaptive_mutation_probs_by_space(self, offspring): sample_size=self.sample_size) # Assigning the selected value from the space to the gene. - if self.gene_type_single == True: - if not self.gene_type[1] is None: - offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[0](value_from_space), - self.gene_type[1]) - else: - offspring[offspring_idx, gene_idx] = self.gene_type[0](value_from_space) - else: - if not self.gene_type[gene_idx][1] is None: - offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[gene_idx][0](value_from_space), - self.gene_type[gene_idx][1]) - else: - offspring[offspring_idx, gene_idx] = self.gene_type[gene_idx][0](value_from_space) + offspring[offspring_idx, gene_idx] = self.change_gene_dtype_and_round(gene_idx, value_from_space) if self.allow_duplicate_genes == False: offspring[offspring_idx], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[offspring_idx], diff --git a/pygad/utils/parent_selection.py b/pygad/utils/parent_selection.py index 3ea4577d..bd04b306 100644 --- a/pygad/utils/parent_selection.py +++ b/pygad/utils/parent_selection.py @@ -29,15 +29,11 @@ def steady_state_selection(self, fitness, num_parents): fitness_sorted = self.sort_solutions_nsga2(fitness=fitness) # Selecting the best individuals in the current generation as parents for producing the offspring of the next generation. - if self.gene_type_single == True: - parents = numpy.empty((num_parents, self.population.shape[1]), dtype=self.gene_type[0]) - else: - parents = numpy.empty((num_parents, self.population.shape[1]), dtype=object) + parents = self.initialize_parents_array((num_parents, self.population.shape[1])) + parents_indices = numpy.array(fitness_sorted[:num_parents]) + parents[:, :] = self.population[parents_indices, :].copy() - for parent_num in range(num_parents): - parents[parent_num, :] = self.population[fitness_sorted[parent_num], :].copy() - - return parents, numpy.array(fitness_sorted[:num_parents]) + return parents, parents_indices def rank_selection(self, fitness, num_parents): @@ -91,15 +87,9 @@ def random_selection(self, fitness, num_parents): -The indices of the selected solutions. """ - if self.gene_type_single == True: - parents = numpy.empty((num_parents, self.population.shape[1]), dtype=self.gene_type[0]) - else: - parents = numpy.empty((num_parents, self.population.shape[1]), dtype=object) - + parents = self.initialize_parents_array((num_parents, self.population.shape[1])) rand_indices = numpy.random.randint(low=0.0, high=fitness.shape[0], size=num_parents) - - for parent_num in range(num_parents): - parents[parent_num, :] = self.population[rand_indices[parent_num], :].copy() + parents[:, :] = self.population[rand_indices, :].copy() return parents, rand_indices @@ -119,11 +109,7 @@ def tournament_selection(self, fitness, num_parents): # This function works with both single- and multi-objective optimization problems. fitness_sorted = self.sort_solutions_nsga2(fitness=fitness) - if self.gene_type_single == True: - parents = numpy.empty((num_parents, self.population.shape[1]), dtype=self.gene_type[0]) - else: - parents = numpy.empty((num_parents, self.population.shape[1]), dtype=object) - + parents = self.initialize_parents_array((num_parents, self.population.shape[1])) parents_indices = [] for parent_num in range(num_parents): @@ -137,10 +123,11 @@ def tournament_selection(self, fitness, num_parents): # Append the index of the selected parent. parents_indices.append(rand_indices[selected_parent_idx]) - # Insert the selected parent. - parents[parent_num, :] = self.population[rand_indices[selected_parent_idx], :].copy() - return parents, numpy.array(parents_indices) + parents_indices = numpy.array(parents_indices) + parents[:, :] = self.population[parents_indices, :].copy() + + return parents, parents_indices def roulette_wheel_selection(self, fitness, num_parents): @@ -186,11 +173,13 @@ def roulette_wheel_selection(self, fitness, num_parents): rand_prob = numpy.random.rand() for idx in range(probs.shape[0]): if (rand_prob >= probs_start[idx] and rand_prob < probs_end[idx]): - parents[parent_num, :] = self.population[idx, :].copy() parents_indices.append(idx) break - return parents, numpy.array(parents_indices) + parents_indices = numpy.array(parents_indices) + parents[:, :] = self.population[parents_indices, :].copy() + + return parents, parents_indices def wheel_cumulative_probs(self, probs, num_parents): """ @@ -220,10 +209,7 @@ def wheel_cumulative_probs(self, probs, num_parents): probs[min_probs_idx] = float('inf') # Selecting the best individuals in the current generation as parents for producing the offspring of the next generation. - if self.gene_type_single == True: - parents = numpy.empty((num_parents, self.population.shape[1]), dtype=self.gene_type[0]) - else: - parents = numpy.empty((num_parents, self.population.shape[1]), dtype=object) + parents = self.initialize_parents_array((num_parents, self.population.shape[1])) return probs_start, probs_end, parents @@ -281,10 +267,7 @@ def stochastic_universal_selection(self, fitness, num_parents): size=1)[0] # Location of the first pointer. # Selecting the best individuals in the current generation as parents for producing the offspring of the next generation. - if self.gene_type_single == True: - parents = numpy.empty((num_parents, self.population.shape[1]), dtype=self.gene_type[0]) - else: - parents = numpy.empty((num_parents, self.population.shape[1]), dtype=object) + parents = self.initialize_parents_array((num_parents, self.population.shape[1])) parents_indices = [] @@ -292,11 +275,13 @@ def stochastic_universal_selection(self, fitness, num_parents): rand_pointer = first_pointer + parent_num*pointers_distance for idx in range(probs.shape[0]): if (rand_pointer >= probs_start[idx] and rand_pointer < probs_end[idx]): - parents[parent_num, :] = self.population[idx, :].copy() parents_indices.append(idx) break - return parents, numpy.array(parents_indices) + parents_indices = numpy.array(parents_indices) + parents[:, :] = self.population[parents_indices, :].copy() + + return parents, parents_indices def tournament_selection_nsga2(self, fitness, diff --git a/pygad/utils/validation.py b/pygad/utils/validation.py new file mode 100644 index 00000000..6c61bea2 --- /dev/null +++ b/pygad/utils/validation.py @@ -0,0 +1,1383 @@ +import numpy +import random +import warnings +import inspect +import logging + +class Validation: + def validate_parameters(self, + num_generations, + num_parents_mating, + fitness_func, + fitness_batch_size, + initial_population, + sol_per_pop, + num_genes, + init_range_low, + init_range_high, + gene_type, + parent_selection_type, + keep_parents, + keep_elitism, + K_tournament, + crossover_type, + crossover_probability, + mutation_type, + mutation_probability, + mutation_by_replacement, + mutation_percent_genes, + mutation_num_genes, + random_mutation_min_val, + random_mutation_max_val, + gene_space, + gene_constraint, + sample_size, + allow_duplicate_genes, + on_start, + on_fitness, + on_parents, + on_crossover, + on_mutation, + on_generation, + on_stop, + save_best_solutions, + save_solutions, + suppress_warnings, + stop_criteria, + parallel_processing, + random_seed, + logger): + + # If no logger is passed, then create a logger that logs only the messages to the console. + if logger is None: + # Create a logger named with the module name. + logger = logging.getLogger(__name__) + # Set the logger log level to 'DEBUG' to log all kinds of messages. + logger.setLevel(logging.DEBUG) + + # Clear any attached handlers to the logger from the previous runs. + logger.handlers.clear() + + # Create the handlers. + stream_handler = logging.StreamHandler() + # Set the handler log level to 'DEBUG' to log all kinds of messages received from the logger. + stream_handler.setLevel(logging.DEBUG) + + # Create the formatter that just includes the log message. + formatter = logging.Formatter('%(message)s') + + # Add the formatter to the handler. + stream_handler.setFormatter(formatter) + + # Add the handler to the logger. + logger.addHandler(stream_handler) + else: + # Validate that the passed logger is of type 'logging.Logger'. + if isinstance(logger, logging.Logger): + pass + else: + self.valid_parameters = False + raise TypeError(f"The expected type of the 'logger' parameter is 'logging.Logger' but {type(logger)} found.") + + # Create the 'self.logger' attribute to hold the logger. + self.logger = logger + + self.random_seed = random_seed + if random_seed is None: + pass + else: + numpy.random.seed(self.random_seed) + random.seed(self.random_seed) + + # If suppress_warnings is bool and its value is False, then print warning messages. + if type(suppress_warnings) is bool: + self.suppress_warnings = suppress_warnings + else: + self.valid_parameters = False + raise TypeError(f"The expected type of the 'suppress_warnings' parameter is bool but {type(suppress_warnings)} found.") + + # Validating mutation_by_replacement + if not (type(mutation_by_replacement) is bool): + self.valid_parameters = False + raise TypeError(f"The expected type of the 'mutation_by_replacement' parameter is bool but {type(mutation_by_replacement)} found.") + + self.mutation_by_replacement = mutation_by_replacement + + # Validate the sample_size parameter. + if type(sample_size) in self.supported_int_types: + if sample_size > 0: + pass + else: + self.valid_parameters = False + raise ValueError(f"The value of the sample_size parameter must be > 0 but the value ({sample_size}) found.") + else: + self.valid_parameters = False + raise TypeError(f"The type of the sample_size parameter must be integer but the value ({sample_size}) of type {type(sample_size)} found.") + + self.sample_size = sample_size + + # Validate allow_duplicate_genes + if not (type(allow_duplicate_genes) is bool): + self.valid_parameters = False + raise TypeError(f"The expected type of the 'allow_duplicate_genes' parameter is bool but {type(allow_duplicate_genes)} found.") + + self.allow_duplicate_genes = allow_duplicate_genes + + # Validate gene_space + self.gene_space_nested = False + if type(gene_space) is type(None): + pass + elif type(gene_space) is range: + if len(gene_space) == 0: + self.valid_parameters = False + raise ValueError("'gene_space' cannot be empty (i.e. its length must be >= 0).") + elif type(gene_space) in [list, numpy.ndarray]: + if len(gene_space) == 0: + self.valid_parameters = False + raise ValueError("'gene_space' cannot be empty (i.e. its length must be >= 0).") + else: + for index, el in enumerate(gene_space): + if type(el) in [numpy.ndarray, list, tuple, range]: + if len(el) == 0: + self.valid_parameters = False + raise ValueError(f"The element indexed {index} of 'gene_space' with type {type(el)} cannot be empty (i.e. its length must be >= 0).") + else: + for val in el: + if not (type(val) in [type(None)] + self.supported_int_float_types): + raise TypeError(f"All values in the sublists inside the 'gene_space' attribute must be numeric of type int/float/None but ({val}) of type {type(val)} found.") + self.gene_space_nested = True + elif type(el) == type(None): + pass + elif type(el) is dict: + if len(el.items()) == 2: + if ('low' in el.keys()) and ('high' in el.keys()): + pass + else: + self.valid_parameters = False + raise ValueError(f"When an element in the 'gene_space' parameter is of type dict, then it can have the keys 'low', 'high', and 'step' (optional) but the following keys found: {el.keys()}") + elif len(el.items()) == 3: + if ('low' in el.keys()) and ('high' in el.keys()) and ('step' in el.keys()): + pass + else: + self.valid_parameters = False + raise ValueError(f"When an element in the 'gene_space' parameter is of type dict, then it can have the keys 'low', 'high', and 'step' (optional) but the following keys found: {el.keys()}") + else: + self.valid_parameters = False + raise ValueError(f"When an element in the 'gene_space' parameter is of type dict, then it must have only 2 items but ({len(el.items())}) items found.") + self.gene_space_nested = True + elif not (type(el) in self.supported_int_float_types): + self.valid_parameters = False + raise TypeError(f"Unexpected type {type(el)} for the element indexed {index} of 'gene_space'. The accepted types are list/tuple/range/numpy.ndarray of numbers, a single number (int/float), or None.") + + elif type(gene_space) is dict: + if len(gene_space.items()) == 2: + if ('low' in gene_space.keys()) and ('high' in gene_space.keys()): + pass + else: + self.valid_parameters = False + raise ValueError(f"When the 'gene_space' parameter is of type dict, then it can have only the keys 'low', 'high', and 'step' (optional) but the following keys found: {gene_space.keys()}") + elif len(gene_space.items()) == 3: + if ('low' in gene_space.keys()) and ('high' in gene_space.keys()) and ('step' in gene_space.keys()): + pass + else: + self.valid_parameters = False + raise ValueError(f"When the 'gene_space' parameter is of type dict, then it can have only the keys 'low', 'high', and 'step' (optional) but the following keys found: {gene_space.keys()}") + else: + self.valid_parameters = False + raise ValueError(f"When the 'gene_space' parameter is of type dict, then it must have only 2 items but ({len(gene_space.items())}) items found.") + + else: + self.valid_parameters = False + raise TypeError(f"The expected type of 'gene_space' is list, range, or numpy.ndarray but {type(gene_space)} found.") + + self.gene_space = gene_space + + # Validate init_range_low and init_range_high + if type(init_range_low) in self.supported_int_float_types: + if type(init_range_high) in self.supported_int_float_types: + if init_range_low == init_range_high: + if not self.suppress_warnings: + warnings.warn("The values of the 2 parameters 'init_range_low' and 'init_range_high' are equal and this might return the same value for some genes in the initial population.") + else: + self.valid_parameters = False + raise TypeError(f"Type mismatch between the 2 parameters 'init_range_low' {type(init_range_low)} and 'init_range_high' {type(init_range_high)}.") + elif type(init_range_low) in [list, tuple, numpy.ndarray]: + # Get the number of genes before validating the num_genes parameter. + if num_genes is None: + if initial_population is None: + self.valid_parameters = False + raise TypeError("When the parameter 'initial_population' is None, then the 2 parameters 'sol_per_pop' and 'num_genes' cannot be None too.") + elif not len(init_range_low) == len(initial_population[0]): + self.valid_parameters = False + raise ValueError(f"The length of the 'init_range_low' parameter is {len(init_range_low)} which is different from the number of genes {len(initial_population[0])}.") + elif not len(init_range_low) == num_genes: + self.valid_parameters = False + raise ValueError(f"The length of the 'init_range_low' parameter is {len(init_range_low)} which is different from the number of genes {num_genes}.") + + if type(init_range_high) in [list, tuple, numpy.ndarray]: + if len(init_range_low) == len(init_range_high): + pass + else: + self.valid_parameters = False + raise ValueError(f"Size mismatch between the 2 parameters 'init_range_low' {len(init_range_low)} and 'init_range_high' {len(init_range_high)}.") + + # Validate the values in init_range_low + for val in init_range_low: + if type(val) in self.supported_int_float_types: + pass + else: + self.valid_parameters = False + raise TypeError(f"When an iterable (list/tuple/numpy.ndarray) is assigned to the 'init_range_low' parameter, its elements must be numeric but the value {val} of type {type(val)} found.") + + # Validate the values in init_range_high + for val in init_range_high: + if type(val) in self.supported_int_float_types: + pass + else: + self.valid_parameters = False + raise TypeError(f"When an iterable (list/tuple/numpy.ndarray) is assigned to the 'init_range_high' parameter, its elements must be numeric but the value {val} of type {type(val)} found.") + else: + self.valid_parameters = False + raise TypeError(f"Type mismatch between the 2 parameters 'init_range_low' {type(init_range_low)} and 'init_range_high' {type(init_range_high)}. Both of them can be either numeric or iterable (list/tuple/numpy.ndarray).") + else: + self.valid_parameters = False + raise TypeError(f"The expected type of the 'init_range_low' parameter is numeric or list/tuple/numpy.ndarray but {type(init_range_low)} found.") + + self.init_range_low = init_range_low + self.init_range_high = init_range_high + + # Validate gene_type + if gene_type in self.supported_int_float_types: + self.gene_type = [gene_type, None] + self.gene_type_single = True + # A single data type of float with precision. + elif len(gene_type) == 2 and gene_type[0] in self.supported_float_types and (type(gene_type[1]) in self.supported_int_types or gene_type[1] is None): + self.gene_type = gene_type + self.gene_type_single = True + # A single data type of integer with precision None ([int, None]). + elif len(gene_type) == 2 and gene_type[0] in self.supported_int_types and gene_type[1] is None: + self.gene_type = gene_type + self.gene_type_single = True + # Raise an exception for a single data type of int with integer precision. + elif len(gene_type) == 2 and gene_type[0] in self.supported_int_types and (type(gene_type[1]) in self.supported_int_types or gene_type[1] is None): + self.gene_type_single = False + raise ValueError(f"Integers cannot have precision. Please use the integer data type directly instead of {gene_type}.") + elif type(gene_type) in [list, tuple, numpy.ndarray]: + # Get the number of genes before validating the num_genes parameter. + if num_genes is None: + if initial_population is None: + self.valid_parameters = False + raise TypeError("When the parameter 'initial_population' is None, then the 2 parameters 'sol_per_pop' and 'num_genes' cannot be None too.") + elif not len(gene_type) == len(initial_population[0]): + self.valid_parameters = False + raise ValueError(f"When the parameter 'gene_type' is nested, then it can be either [float, int] or with length equal to the number of genes parameter. Instead, value {gene_type} with len(gene_type) ({len(gene_type)}) != number of genes ({len(initial_population[0])}) found.") + elif not len(gene_type) == num_genes: + self.valid_parameters = False + raise ValueError(f"When the parameter 'gene_type' is nested, then it can be either [float, int] or with length equal to the value passed to the 'num_genes' parameter. Instead, value {gene_type} with len(gene_type) ({len(gene_type)}) != len(num_genes) ({num_genes}) found.") + for gene_type_idx, gene_type_val in enumerate(gene_type): + if gene_type_val in self.supported_int_float_types: + # If the gene type is float and no precision is passed or an integer, set its precision to None. + gene_type[gene_type_idx] = [gene_type_val, None] + elif type(gene_type_val) in [list, tuple, numpy.ndarray]: + # A float type is expected in a list/tuple/numpy.ndarray of length 2. + if len(gene_type_val) == 2: + if gene_type_val[0] in self.supported_float_types: + if type(gene_type_val[1]) in self.supported_int_types: + pass + else: + self.valid_parameters = False + raise TypeError(f"In the 'gene_type' parameter, the precision for float gene data types must be an integer but the element {gene_type_val} at index {gene_type_idx} has a precision of {gene_type_val[1]} with type {gene_type_val[0]}.") + elif gene_type_val[0] in self.supported_int_types: + if gene_type_val[1] is None: + pass + else: + self.valid_parameters = False + raise TypeError(f"In the 'gene_type' parameter, either do not set a precision for integer data types or set it to None. But the element {gene_type_val} at index {gene_type_idx} has a precision of {gene_type_val[1]} with type {gene_type_val[0]}.") + else: + self.valid_parameters = False + raise TypeError( + f"In the 'gene_type' parameter, a precision is expected only for float gene data types but the element {gene_type_val} found at index {gene_type_idx}.\nNote that the data type must be at index 0 of the item followed by precision at index 1.") + else: + self.valid_parameters = False + raise ValueError(f"In the 'gene_type' parameter, a precision is specified in a list/tuple/numpy.ndarray of length 2 but value ({gene_type_val}) of type {type(gene_type_val)} with length {len(gene_type_val)} found at index {gene_type_idx}.") + else: + self.valid_parameters = False + raise ValueError(f"When a list/tuple/numpy.ndarray is assigned to the 'gene_type' parameter, then its elements must be of integer, floating-point, list, tuple, or numpy.ndarray data types but the value ({gene_type_val}) of type {type(gene_type_val)} found at index {gene_type_idx}.") + self.gene_type = gene_type + self.gene_type_single = False + else: + self.valid_parameters = False + raise ValueError(f"The value passed to the 'gene_type' parameter must be either a single integer, floating-point, list, tuple, or numpy.ndarray but ({gene_type}) of type {type(gene_type)} found.") + + # Call the unpack_gene_space() method in the pygad.helper.unique.Unique class. + self.gene_space_unpacked = self.unpack_gene_space(range_min=self.init_range_low, + range_max=self.init_range_high) + + # Build the initial population + if initial_population is None: + if (sol_per_pop is None) or (num_genes is None): + self.valid_parameters = False + raise TypeError("Error creating the initial population:\n\nWhen the parameter 'initial_population' is None, then the 2 parameters 'sol_per_pop' and 'num_genes' cannot be None too.\nThere are 2 options to prepare the initial population:\n1) Assigning the initial population to the 'initial_population' parameter. In this case, the values of the 2 parameters sol_per_pop and num_genes will be deduced.\n2) Assign integer values to the 'sol_per_pop' and 'num_genes' parameters so that PyGAD can create the initial population automatically.") + elif (type(sol_per_pop) is int) and (type(num_genes) is int): + # Validating the number of solutions in the population (sol_per_pop) + if sol_per_pop <= 0: + self.valid_parameters = False + raise ValueError(f"The number of solutions in the population (sol_per_pop) must be > 0 but ({sol_per_pop}) found. \nThe following parameters must be > 0: \n1) Population size (i.e. number of solutions per population) (sol_per_pop).\n2) Number of selected parents in the mating pool (num_parents_mating).\n") + # Validating the number of gene. + if (num_genes <= 0): + self.valid_parameters = False + raise ValueError(f"The number of genes cannot be <= 0 but ({num_genes}) found.\n") + # When initial_population=None and the 2 parameters sol_per_pop and num_genes have valid integer values, then the initial population is created. + # Inside the initialize_population() method, the initial_population attribute is assigned to keep the initial population accessible. + self.num_genes = num_genes # Number of genes in the solution. + + # In case the 'gene_space' parameter is nested, then make sure the number of its elements equals to the number of genes. + if self.gene_space_nested: + if len(gene_space) != self.num_genes: + self.valid_parameters = False + raise ValueError(f"When the parameter 'gene_space' is nested, then its length must be equal to the value passed to the 'num_genes' parameter. Instead, length of gene_space ({len(gene_space)}) != num_genes ({self.num_genes})") + + # Number of solutions in the population. + self.sol_per_pop = sol_per_pop + self.initialize_population(allow_duplicate_genes=allow_duplicate_genes, + gene_type=self.gene_type, + gene_constraint=gene_constraint) + else: + self.valid_parameters = False + raise TypeError(f"The expected type of both the sol_per_pop and num_genes parameters is int but {type(sol_per_pop)} and {type(num_genes)} found.") + elif not type(initial_population) in [list, tuple, numpy.ndarray]: + self.valid_parameters = False + raise TypeError(f"The value assigned to the 'initial_population' parameter is expected to be of type list, tuple, or ndarray but {type(initial_population)} found.") + elif numpy.array(initial_population).ndim != 2: + self.valid_parameters = False + raise ValueError(f"A 2D list is expected to the initial_population parameter but a ({numpy.array(initial_population).ndim}-D) list found.") + else: + # Validate the type of each value in the 'initial_population' parameter. + for row_idx in range(len(initial_population)): + for col_idx in range(len(initial_population[0])): + if type(initial_population[row_idx][col_idx]) in self.supported_int_float_types: + pass + else: + self.valid_parameters = False + raise TypeError(f"The values in the initial population can be integers or floats but the value ({initial_population[row_idx][col_idx]}) of type {type(initial_population[row_idx][col_idx])} found.") + + # Change the data type and round all genes within the initial population. + self.initial_population = self.change_population_dtype_and_round(initial_population) + + # Check if duplicates are allowed. If not, then solve any existing duplicates in the passed initial population. + if self.allow_duplicate_genes == False: + for initial_solution_idx, initial_solution in enumerate(self.initial_population): + if self.gene_space is None: + self.initial_population[initial_solution_idx], _, _ = self.solve_duplicate_genes_randomly(solution=initial_solution, + min_val=self.init_range_low, + max_val=self.init_range_high, + mutation_by_replacement=True, + gene_type=self.gene_type, + sample_size=self.sample_size) + else: + self.initial_population[initial_solution_idx], _, _ = self.solve_duplicate_genes_by_space(solution=initial_solution, + gene_type=self.gene_type, + sample_size=self.sample_size, + mutation_by_replacement=True, + build_initial_pop=True) + + # A NumPy array holding the initial population. + self.population = self.initial_population.copy() + # Number of genes in the solution. + self.num_genes = self.initial_population.shape[1] + # Number of solutions in the population. + self.sol_per_pop = self.initial_population.shape[0] + # The population size. + self.pop_size = (self.sol_per_pop, self.num_genes) + + # Change the data type and round all genes within the initial population. + self.initial_population = self.change_population_dtype_and_round(self.initial_population) + self.population = self.initial_population.copy() + + # In case the 'gene_space' parameter is nested, then make sure the number of its elements equals to the number of genes. + if self.gene_space_nested: + if len(gene_space) != self.num_genes: + self.valid_parameters = False + raise ValueError(f"When the parameter 'gene_space' is nested, then its length must be equal to the value passed to the 'num_genes' parameter. Instead, length of gene_space ({len(gene_space)}) != num_genes ({self.num_genes})") + + # Validate random_mutation_min_val and random_mutation_max_val + if type(random_mutation_min_val) in self.supported_int_float_types: + if type(random_mutation_max_val) in self.supported_int_float_types: + if random_mutation_min_val == random_mutation_max_val: + if not self.suppress_warnings: + warnings.warn("The values of the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val' are equal and this might cause a fixed mutation to some genes.") + else: + self.valid_parameters = False + raise TypeError(f"Type mismatch between the 2 parameters 'random_mutation_min_val' {type(random_mutation_min_val)} and 'random_mutation_max_val' {type(random_mutation_max_val)}.") + elif type(random_mutation_min_val) in [list, tuple, numpy.ndarray]: + if len(random_mutation_min_val) == self.num_genes: + pass + else: + self.valid_parameters = False + raise ValueError(f"The length of the 'random_mutation_min_val' parameter is {len(random_mutation_min_val)} which is different from the number of genes {self.num_genes}.") + if type(random_mutation_max_val) in [list, tuple, numpy.ndarray]: + if len(random_mutation_min_val) == len(random_mutation_max_val): + pass + else: + self.valid_parameters = False + raise ValueError(f"Size mismatch between the 2 parameters 'random_mutation_min_val' {len(random_mutation_min_val)} and 'random_mutation_max_val' {len(random_mutation_max_val)}.") + + # Validate the values in random_mutation_min_val + for val in random_mutation_min_val: + if type(val) in self.supported_int_float_types: + pass + else: + self.valid_parameters = False + raise TypeError(f"When an iterable (list/tuple/numpy.ndarray) is assigned to the 'random_mutation_min_val' parameter, its elements must be numeric but the value {val} of type {type(val)} found.") + + # Validate the values in random_mutation_max_val + for val in random_mutation_max_val: + if type(val) in self.supported_int_float_types: + pass + else: + self.valid_parameters = False + raise TypeError(f"When an iterable (list/tuple/numpy.ndarray) is assigned to the 'random_mutation_max_val' parameter, its elements must be numeric but the value {val} of type {type(val)} found.") + else: + self.valid_parameters = False + raise TypeError(f"Type mismatch between the 2 parameters 'random_mutation_min_val' {type(random_mutation_min_val)} and 'random_mutation_max_val' {type(random_mutation_max_val)}.") + else: + self.valid_parameters = False + raise TypeError(f"The expected type of the 'random_mutation_min_val' parameter is numeric or list/tuple/numpy.ndarray but {type(random_mutation_min_val)} found.") + + self.random_mutation_min_val = random_mutation_min_val + self.random_mutation_max_val = random_mutation_max_val + + # Validate that gene_constraint is a list or tuple and every element inside it is either None or callable. + if gene_constraint: + if type(gene_constraint) in [list, tuple]: + if len(gene_constraint) == self.num_genes: + for constraint_idx, item in enumerate(gene_constraint): + # Check whether the element is None or a callable. + if item is None: + pass + elif item and callable(item): + if item.__code__.co_argcount == 2: + # Every callable is valid if it receives 2 arguments. + # The 2 arguments: 1) solution 2) A list or numpy.ndarray of values to check if they meet the constraint. + pass + else: + self.valid_parameters = False + raise ValueError(f"Every callable inside the gene_constraint parameter must accept 2 arguments representing 1) The solution/chromosome where the gene exists 2) A list of NumPy array of values to check if they meet the constraint. But the callable at index {constraint_idx} named '{item.__code__.co_name}' accepts {item.__code__.co_argcount} argument(s).") + else: + self.valid_parameters = False + raise TypeError(f"The expected type of an element in the 'gene_constraint' parameter is None or a callable (e.g. function). But {item} at index {constraint_idx} of type {type(item)} found.") + else: + self.valid_parameters = False + raise ValueError(f"The number of constrains ({len(gene_constraint)}) in the 'gene_constraint' parameter must be equal to the number of genes ({self.num_genes}).") + else: + self.valid_parameters = False + raise TypeError(f"The expected type of the 'gene_constraint' parameter is either a list or tuple. But the value {gene_constraint} of type {type(gene_constraint)} found.") + else: + # gene_constraint is None and not used. + pass + + self.gene_constraint = gene_constraint + + # Validating the number of parents to be selected for mating (num_parents_mating) + if num_parents_mating <= 0: + self.valid_parameters = False + raise ValueError(f"The number of parents mating (num_parents_mating) parameter must be > 0 but ({num_parents_mating}) found. \nThe following parameters must be > 0: \n1) Population size (i.e. number of solutions per population) (sol_per_pop).\n2) Number of selected parents in the mating pool (num_parents_mating).\n") + + # Validating the number of parents to be selected for mating: num_parents_mating + if num_parents_mating > self.sol_per_pop: + self.valid_parameters = False + raise ValueError(f"The number of parents to select for mating ({num_parents_mating}) cannot be greater than the number of solutions in the population ({self.sol_per_pop}) (i.e., num_parents_mating must always be <= sol_per_pop).\n") + + self.num_parents_mating = num_parents_mating + + # crossover: Refers to the method that applies the crossover operator based on the selected type of crossover in the crossover_type property. + # Validating the crossover type: crossover_type + if crossover_type is None: + self.crossover = None + elif inspect.ismethod(crossover_type): + # Check if the crossover_type is a method that accepts 3 parameters. + if len(inspect.signature(crossover_type).parameters) == 3: + # The crossover method assigned to the crossover_type parameter is validated. + self.crossover = crossover_type + else: + self.valid_parameters = False + raise ValueError(f"When 'crossover_type' is assigned to a method, then this crossover method must accept 3 parameters:\n1) The selected parents.\n2) The size of the offspring to be produced.\n3) The instance from the pygad.GA class.\n\nThe passed crossover method named '{crossover_type.__code__.co_name}' accepts {len(inspect.signature(crossover_type).parameters)} parameter(s).") + elif inspect.isfunction(crossover_type): + # Check if the crossover_type is a function that accepts 3 parameters. + if len(inspect.signature(crossover_type).parameters) == 3: + # The crossover function assigned to the crossover_type parameter is validated. + self.crossover = crossover_type + else: + self.valid_parameters = False + raise ValueError(f"When 'crossover_type' is assigned to a function, then this crossover function must accept 3 parameters:\n1) The selected parents.\n2) The size of the offspring to be produced.3) The instance from the pygad.GA class to retrieve any property like population, gene data type, gene space, etc.\n\nThe passed crossover function named '{crossover_type.__code__.co_name}' accepts {len(inspect.signature(crossover_type).parameters)} parameter(s).") + elif callable(crossover_type) and not inspect.isclass(crossover_type): + # The object must have the __call__() method. + if hasattr(crossover_type, '__call__'): + # Check if the __call__() method accepts 3 parameters. + if len(inspect.signature(crossover_type).parameters) == 3: + # The crossover class instance assigned to the crossover_type parameter is validated. + self.crossover = crossover_type + else: + self.valid_parameters = False + raise ValueError(f"When 'crossover_type' is assigned a class instance, then its __call__ method must accept 3 parameters:\n1) The selected parents.\n2) The size of the offspring to be produced.\n3) The instance from the pygad.GA class.\n\nThe passed instance of the class named '{crossover_type.__class__.__name__}' accepts {len(inspect.signature(crossover_type).parameters)} parameter(s).") + else: + self.valid_parameters = False + raise ValueError("When 'crossover_type' is assigned a class instance, then its __call__ method must be implemented and accept 3 parameters.") + elif not (type(crossover_type) is str): + self.valid_parameters = False + raise TypeError(f"The expected type of the 'crossover_type' parameter is either callable or str but {type(crossover_type)} found.") + else: # type crossover_type is str + crossover_type = crossover_type.lower() + if crossover_type == "single_point": + self.crossover = self.single_point_crossover + elif crossover_type == "two_points": + self.crossover = self.two_points_crossover + elif crossover_type == "uniform": + self.crossover = self.uniform_crossover + elif crossover_type == "scattered": + self.crossover = self.scattered_crossover + else: + self.valid_parameters = False + raise TypeError(f"Undefined crossover type. \nThe assigned value to the crossover_type ({crossover_type}) parameter does not refer to one of the supported crossover types which are: \n-single_point (for single point crossover)\n-two_points (for two points crossover)\n-uniform (for uniform crossover)\n-scattered (for scattered crossover).\n") + + self.crossover_type = crossover_type + + # Calculate the value of crossover_probability + if crossover_probability is None: + self.crossover_probability = None + elif type(crossover_probability) in self.supported_int_float_types: + if 0 <= crossover_probability <= 1: + self.crossover_probability = crossover_probability + else: + self.valid_parameters = False + raise ValueError(f"The value assigned to the 'crossover_probability' parameter must be between 0 and 1 inclusive but ({crossover_probability}) found.") + else: + self.valid_parameters = False + raise TypeError(f"Unexpected type for the 'crossover_probability' parameter. Float is expected but ({crossover_probability}) of type {type(crossover_probability)} found.") + + # mutation: Refers to the method that applies the mutation operator based on the selected type of mutation in the mutation_type property. + # Validating the mutation type: mutation_type + # "adaptive" mutation is supported starting from PyGAD 2.10.0 + if mutation_type is None: + self.mutation = None + elif inspect.ismethod(mutation_type): + # Check if the mutation_type is a method that accepts 2 parameters. + if (len(inspect.signature(mutation_type).parameters) == 2): + # The mutation method assigned to the mutation_type parameter is validated. + self.mutation = mutation_type + else: + self.valid_parameters = False + raise ValueError(f"When 'mutation_type' is assigned to a method, then it must accept 2 parameters:\n1) The offspring to be mutated.\n2) The instance from the pygad.GA class.\n\nThe passed mutation method named '{mutation_type.__code__.co_name}' accepts {len(inspect.signature(mutation_type).parameters)} parameter(s).") + elif inspect.isfunction(mutation_type): + # Check if the mutation_type is a function that accepts 2 parameters. + if (len(inspect.signature(mutation_type).parameters) == 2): + # The mutation function assigned to the mutation_type parameter is validated. + self.mutation = mutation_type + else: + self.valid_parameters = False + raise ValueError(f"When 'mutation_type' is assigned to a function, then this mutation function must accept 2 parameters:\n1) The offspring to be mutated.\n2) The instance from the pygad.GA class to retrieve any property like population, gene data type, gene space, etc.\n\nThe passed mutation function named '{mutation_type.__code__.co_name}' accepts {len(inspect.signature(mutation_type).parameters)} parameter(s).") + elif callable(mutation_type) and not inspect.isclass(mutation_type): + # The object must have the __call__() method. + if hasattr(mutation_type, '__call__'): + # Check if the __call__() method accepts 2 parameters. + if len(inspect.signature(mutation_type).parameters) == 2: + # The mutation class instance assigned to the mutation_type parameter is validated. + self.mutation = mutation_type + else: + self.valid_parameters = False + raise ValueError(f"When 'mutation_type' is assigned a class instance, then its __call__ method must accept 2 parameters:\n1) The offspring to be mutated.\n2) The instance from the pygad.GA class to retrieve any property like population, gene data type, gene space, etc.\n\nThe passed instance of the class named '{mutation_type.__class__.__name__}' accepts {len(inspect.signature(mutation_type).parameters)} parameter(s).") + else: + self.valid_parameters = False + raise ValueError("When 'mutation_type' is assigned a class instance, then its __call__ method must be implemented and accept 2 parameters.") + elif not (type(mutation_type) is str): + self.valid_parameters = False + raise TypeError(f"The expected type of the 'mutation_type' parameter is either callable or str but {type(mutation_type)} found.") + else: # type mutation_type is str + mutation_type = mutation_type.lower() + if mutation_type == "random": + self.mutation = self.random_mutation + elif mutation_type == "swap": + self.mutation = self.swap_mutation + elif mutation_type == "scramble": + self.mutation = self.scramble_mutation + elif mutation_type == "inversion": + self.mutation = self.inversion_mutation + elif mutation_type == "adaptive": + self.mutation = self.adaptive_mutation + else: + self.valid_parameters = False + raise TypeError(f"Undefined mutation type. \nThe assigned string value to the 'mutation_type' parameter ({mutation_type}) does not refer to one of the supported mutation types which are: \n-random (for random mutation)\n-swap (for swap mutation)\n-inversion (for inversion mutation)\n-scramble (for scramble mutation)\n-adaptive (for adaptive mutation).\n") + + self.mutation_type = mutation_type + + # Calculate the value of mutation_probability + if not (self.mutation_type is None): + if mutation_probability is None: + self.mutation_probability = None + elif mutation_type != "adaptive": + # Mutation probability is fixed not adaptive. + if type(mutation_probability) in self.supported_int_float_types: + if 0 <= mutation_probability <= 1: + self.mutation_probability = mutation_probability + else: + self.valid_parameters = False + raise ValueError(f"The value assigned to the 'mutation_probability' parameter must be between 0 and 1 inclusive but ({mutation_probability}) found.") + else: + self.valid_parameters = False + raise TypeError(f"Unexpected type for the 'mutation_probability' parameter. A numeric value is expected but ({mutation_probability}) of type {type(mutation_probability)} found.") + else: + # Mutation probability is adaptive not fixed. + if type(mutation_probability) in [list, tuple, numpy.ndarray]: + if len(mutation_probability) == 2: + for el in mutation_probability: + if type(el) in self.supported_int_float_types: + if 0 <= el <= 1: + pass + else: + self.valid_parameters = False + raise ValueError(f"The values assigned to the 'mutation_probability' parameter must be between 0 and 1 inclusive but ({el}) found.") + else: + self.valid_parameters = False + raise TypeError(f"Unexpected type for a value assigned to the 'mutation_probability' parameter. A numeric value is expected but ({el}) of type {type(el)} found.") + if mutation_probability[0] < mutation_probability[1]: + if not self.suppress_warnings: + warnings.warn(f"The first element in the 'mutation_probability' parameter is {mutation_probability[0]} which is smaller than the second element {mutation_probability[1]}. This means the mutation rate for the high-quality solutions is higher than the mutation rate of the low-quality ones. This causes high disruption in the high quality solutions while making little changes in the low quality solutions. Please make the first element higher than the second element.") + self.mutation_probability = mutation_probability + else: + self.valid_parameters = False + raise ValueError(f"When mutation_type='adaptive', then the 'mutation_probability' parameter must have only 2 elements but ({len(mutation_probability)}) element(s) found.") + else: + self.valid_parameters = False + raise TypeError(f"Unexpected type for the 'mutation_probability' parameter. When mutation_type='adaptive', then list/tuple/numpy.ndarray is expected but ({mutation_probability}) of type {type(mutation_probability)} found.") + else: + pass + + # Calculate the value of mutation_num_genes + if not (self.mutation_type is None): + if mutation_num_genes is None: + # The mutation_num_genes parameter does not exist. Checking whether adaptive mutation is used. + if mutation_type != "adaptive": + # The percent of genes to mutate is fixed not adaptive. + if mutation_percent_genes == 'default'.lower(): + mutation_percent_genes = 10 + # Based on the mutation percentage in the 'mutation_percent_genes' parameter, the number of genes to mutate is calculated. + mutation_num_genes = numpy.uint32( + (mutation_percent_genes*self.num_genes)/100) + # Based on the mutation percentage of genes, if the number of selected genes for mutation is less than the least possible value which is 1, then the number will be set to 1. + if mutation_num_genes == 0: + if self.mutation_probability is None: + if not self.suppress_warnings: + warnings.warn( + f"The percentage of genes to mutate (mutation_percent_genes={mutation_percent_genes}) resulted in selecting ({mutation_num_genes}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.") + mutation_num_genes = 1 + + elif type(mutation_percent_genes) in self.supported_int_float_types: + if mutation_percent_genes <= 0 or mutation_percent_genes > 100: + self.valid_parameters = False + raise ValueError(f"The percentage of selected genes for mutation (mutation_percent_genes) must be > 0 and <= 100 but ({mutation_percent_genes}) found.\n") + else: + # If mutation_percent_genes equals the string "default", then it is replaced by the numeric value 10. + if mutation_percent_genes == 'default'.lower(): + mutation_percent_genes = 10 + + # Based on the mutation percentage in the 'mutation_percent_genes' parameter, the number of genes to mutate is calculated. + mutation_num_genes = numpy.uint32( + (mutation_percent_genes*self.num_genes)/100) + # Based on the mutation percentage of genes, if the number of selected genes for mutation is less than the least possible value which is 1, then the number will be set to 1. + if mutation_num_genes == 0: + if self.mutation_probability is None: + if not self.suppress_warnings: + warnings.warn(f"The percentage of genes to mutate (mutation_percent_genes={mutation_percent_genes}) resulted in selecting ({mutation_num_genes}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.") + mutation_num_genes = 1 + else: + self.valid_parameters = False + raise TypeError(f"Unexpected value or type of the 'mutation_percent_genes' parameter. It only accepts the string 'default' or a numeric value but ({mutation_percent_genes}) of type {type(mutation_percent_genes)} found.") + else: + # The percent of genes to mutate is adaptive not fixed. + if type(mutation_percent_genes) in [list, tuple, numpy.ndarray]: + if len(mutation_percent_genes) == 2: + mutation_num_genes = numpy.zeros_like( + mutation_percent_genes, dtype=numpy.uint32) + for idx, el in enumerate(mutation_percent_genes): + if type(el) in self.supported_int_float_types: + if el <= 0 or el > 100: + self.valid_parameters = False + raise ValueError(f"The values assigned to the 'mutation_percent_genes' must be > 0 and <= 100 but ({mutation_percent_genes}) found.\n") + else: + self.valid_parameters = False + raise TypeError(f"Unexpected type for a value assigned to the 'mutation_percent_genes' parameter. An integer value is expected but ({el}) of type {type(el)} found.") + # At this point of the loop, the current value assigned to the parameter 'mutation_percent_genes' is validated. + # Based on the mutation percentage in the 'mutation_percent_genes' parameter, the number of genes to mutate is calculated. + mutation_num_genes[idx] = numpy.uint32( + (mutation_percent_genes[idx]*self.num_genes)/100) + # Based on the mutation percentage of genes, if the number of selected genes for mutation is less than the least possible value which is 1, then the number will be set to 1. + if mutation_num_genes[idx] == 0: + if not self.suppress_warnings: + warnings.warn(f"The percentage of genes to mutate ({mutation_percent_genes[idx]}) resulted in selecting ({mutation_num_genes[idx]}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.") + mutation_num_genes[idx] = 1 + if mutation_percent_genes[0] < mutation_percent_genes[1]: + if not self.suppress_warnings: + warnings.warn(f"The first element in the 'mutation_percent_genes' parameter is ({mutation_percent_genes[0]}) which is smaller than the second element ({mutation_percent_genes[1]}).\nThis means the mutation rate for the high-quality solutions is higher than the mutation rate of the low-quality ones. This causes high disruption in the high quality solutions while making little changes in the low quality solutions.\nPlease make the first element higher than the second element.") + # At this point outside the loop, all values of the parameter 'mutation_percent_genes' are validated. Everything is OK. + else: + self.valid_parameters = False + raise ValueError(f"When mutation_type='adaptive', then the 'mutation_percent_genes' parameter must have only 2 elements but ({len(mutation_percent_genes)}) element(s) found.") + else: + if self.mutation_probability is None: + self.valid_parameters = False + raise TypeError(f"Unexpected type of the 'mutation_percent_genes' parameter. When mutation_type='adaptive', then the 'mutation_percent_genes' parameter should exist and assigned a list/tuple/numpy.ndarray with 2 values but ({mutation_percent_genes}) found.") + # The mutation_num_genes parameter exists. Checking whether adaptive mutation is used. + elif mutation_type != "adaptive": + # Number of genes to mutate is fixed not adaptive. + if type(mutation_num_genes) in self.supported_int_types: + if mutation_num_genes <= 0: + self.valid_parameters = False + raise ValueError(f"The number of selected genes for mutation (mutation_num_genes) cannot be <= 0 but ({mutation_num_genes}) found. If you do not want to use mutation, please set mutation_type=None\n") + elif mutation_num_genes > self.num_genes: + self.valid_parameters = False + raise ValueError(f"The number of selected genes for mutation (mutation_num_genes), which is ({mutation_num_genes}), cannot be greater than the number of genes ({self.num_genes}).\n") + else: + self.valid_parameters = False + raise TypeError(f"The 'mutation_num_genes' parameter is expected to be a positive integer but the value ({mutation_num_genes}) of type {type(mutation_num_genes)} found.\n") + else: + # Number of genes to mutate is adaptive not fixed. + if type(mutation_num_genes) in [list, tuple, numpy.ndarray]: + if len(mutation_num_genes) == 2: + for el in mutation_num_genes: + if type(el) in self.supported_int_types: + if el <= 0: + self.valid_parameters = False + raise ValueError(f"The values assigned to the 'mutation_num_genes' cannot be <= 0 but ({el}) found. If you do not want to use mutation, please set mutation_type=None\n") + elif el > self.num_genes: + self.valid_parameters = False + raise ValueError(f"The values assigned to the 'mutation_num_genes' cannot be greater than the number of genes ({self.num_genes}) but ({el}) found.\n") + else: + self.valid_parameters = False + raise TypeError(f"Unexpected type for a value assigned to the 'mutation_num_genes' parameter. An integer value is expected but ({el}) of type {type(el)} found.") + # At this point of the loop, the current value assigned to the parameter 'mutation_num_genes' is validated. + if mutation_num_genes[0] < mutation_num_genes[1]: + if not self.suppress_warnings: + warnings.warn(f"The first element in the 'mutation_num_genes' parameter is {mutation_num_genes[0]} which is smaller than the second element {mutation_num_genes[1]}. This means the mutation rate for the high-quality solutions is higher than the mutation rate of the low-quality ones. This causes high disruption in the high quality solutions while making little changes in the low quality solutions. Please make the first element higher than the second element.") + # At this point outside the loop, all values of the parameter 'mutation_num_genes' are validated. Everything is OK. + else: + self.valid_parameters = False + raise ValueError(f"When mutation_type='adaptive', then the 'mutation_num_genes' parameter must have only 2 elements but ({len(mutation_num_genes)}) element(s) found.") + else: + self.valid_parameters = False + raise TypeError(f"Unexpected type for the 'mutation_num_genes' parameter. When mutation_type='adaptive', then list/tuple/numpy.ndarray is expected but ({mutation_num_genes}) of type {type(mutation_num_genes)} found.") + else: + pass + + # Validating mutation_by_replacement and mutation_type + if self.mutation_type != "random" and self.mutation_by_replacement: + if not self.suppress_warnings: + warnings.warn(f"The mutation_by_replacement parameter is set to True while the mutation_type parameter is not set to random but ({mutation_type}). Note that the mutation_by_replacement parameter has an effect only when mutation_type='random'.") + + # Check if crossover and mutation are both disabled. + if (self.mutation_type is None) and (self.crossover_type is None): + if not self.suppress_warnings: + warnings.warn("The 2 parameters mutation_type and crossover_type are None. This disables any type of evolution the genetic algorithm can make. As a result, the genetic algorithm cannot find a better solution that the best solution in the initial population.") + + # select_parents: Refers to a method that selects the parents based on the parent selection type specified in the parent_selection_type attribute. + # Validating the selected type of parent selection: parent_selection_type + if inspect.ismethod(parent_selection_type): + # Check if the parent_selection_type is a method that accepts 3 parameters. + if len(inspect.signature(parent_selection_type).parameters) == 3: + # The parent selection method assigned to the parent_selection_type parameter is validated. + self.select_parents = parent_selection_type + else: + self.valid_parameters = False + raise ValueError(f"When 'parent_selection_type' is assigned to a method, then it must accept 3 parameters:\n1) The fitness values of the current population.\n2) The number of parents needed.\n3) The instance from the pygad.GA class.\n\nThe passed parent selection method named '{parent_selection_type.__code__.co_name}' accepts {len(inspect.signature(parent_selection_type).parameters)} parameter(s).") + elif inspect.isfunction(parent_selection_type): + # Check if the parent_selection_type is a function that accepts 2 parameters. + if len(inspect.signature(parent_selection_type).parameters) == 3: + # The parent selection function assigned to the parent_selection_type parameter is validated. + self.select_parents = parent_selection_type + else: + self.valid_parameters = False + raise ValueError(f"When 'parent_selection_type' is assigned to a user-defined function, then this parent selection function must accept 3 parameters:\n1) The fitness values of the current population.\n2) The number of parents needed.\n3) The instance from the pygad.GA class to retrieve any property like population, gene data type, gene space, etc.\n\nThe passed parent selection function named '{parent_selection_type.__code__.co_name}' accepts {len(inspect.signature(parent_selection_type).parameters)} parameter(s).") + elif callable(parent_selection_type) and not inspect.isclass(parent_selection_type): + # The object must have the __call__() method. + if hasattr(parent_selection_type, '__call__'): + # Check if the __call__() method accepts 3 parameters. + if len(inspect.signature(parent_selection_type).parameters) == 3: + # The parent selection class instance assigned to the parent_selection_type parameter is validated. + self.select_parents = parent_selection_type + else: + self.valid_parameters = False + raise ValueError(f"When 'parent_selection_type' is assigned a class instance, then its __call__ method must accept 3 parameters:\n1) The fitness values of the current population.\n2) The number of parents needed.\n3) The instance from the pygad.GA class to retrieve any property like population, gene data type, gene space, etc.\n\nThe passed instance of the class named '{parent_selection_type.__class__.__name__}' accepts {len(inspect.signature(parent_selection_type).parameters)} parameter(s).") + else: + self.valid_parameters = False + raise ValueError("When 'parent_selection_type' is assigned a class instance, then its __call__ method must be implemented and accept 3 parameters.") + elif not (type(parent_selection_type) is str): + self.valid_parameters = False + + raise TypeError(f"The expected type of the 'parent_selection_type' parameter is either callable or str but {type(parent_selection_type)} found.") + else: + parent_selection_type = parent_selection_type.lower() + if parent_selection_type == "sss": + self.select_parents = self.steady_state_selection + elif parent_selection_type == "rws": + self.select_parents = self.roulette_wheel_selection + elif parent_selection_type == "sus": + self.select_parents = self.stochastic_universal_selection + elif parent_selection_type == "random": + self.select_parents = self.random_selection + elif parent_selection_type == "tournament": + self.select_parents = self.tournament_selection + elif parent_selection_type == "tournament_nsga2": # Supported in PyGAD >= 3.2 + self.select_parents = self.tournament_selection_nsga2 + elif parent_selection_type == "nsga2": # Supported in PyGAD >= 3.2 + self.select_parents = self.nsga2_selection + elif parent_selection_type == "rank": + self.select_parents = self.rank_selection + else: + self.valid_parameters = False + raise TypeError(f"Undefined parent selection type: {parent_selection_type}. \nThe assigned value to the 'parent_selection_type' parameter does not refer to one of the supported parent selection techniques which are: \n-sss (steady state selection)\n-rws (roulette wheel selection)\n-sus (stochastic universal selection)\n-rank (rank selection)\n-random (random selection)\n-tournament (tournament selection)\n-tournament_nsga2: (Tournament selection for NSGA-II)\n-nsga2: (NSGA-II parent selection).\n") + + # For tournament selection, validate the K value. + if parent_selection_type == "tournament": + if type(K_tournament) in self.supported_int_types: + if K_tournament > self.sol_per_pop: + K_tournament = self.sol_per_pop + if not self.suppress_warnings: + warnings.warn(f"K of the tournament selection ({K_tournament}) should not be greater than the number of solutions within the population ({self.sol_per_pop}).\nK will be clipped to be equal to the number of solutions in the population (sol_per_pop).\n") + elif K_tournament <= 0: + self.valid_parameters = False + raise ValueError(f"K of the tournament selection cannot be <=0 but ({K_tournament}) found.\n") + else: + self.valid_parameters = False + raise ValueError(f"The type of K of the tournament selection must be integer but the value ({K_tournament}) of type ({type(K_tournament)}) found.") + + self.K_tournament = K_tournament + + # Validating the number of parents to keep in the next population: keep_parents + if not (type(keep_parents) in self.supported_int_types): + self.valid_parameters = False + raise TypeError(f"Incorrect type of the value assigned to the keep_parents parameter. The value ({keep_parents}) of type {type(keep_parents)} found but an integer is expected.") + elif keep_parents > self.sol_per_pop or keep_parents > self.num_parents_mating or keep_parents < -1: + self.valid_parameters = False + raise ValueError(f"Incorrect value to the keep_parents parameter: {keep_parents}. \nThe assigned value to the keep_parent parameter must satisfy the following conditions: \n1) Less than or equal to sol_per_pop\n2) Less than or equal to num_parents_mating\n3) Greater than or equal to -1.") + + self.keep_parents = keep_parents + + if parent_selection_type == "sss" and self.keep_parents == 0: + if not self.suppress_warnings: + warnings.warn("The steady-state parent (sss) selection operator is used despite that no parents are kept in the next generation.") + + # Validating the number of elitism to keep in the next population: keep_elitism + if not (type(keep_elitism) in self.supported_int_types): + self.valid_parameters = False + raise TypeError(f"Incorrect type of the value assigned to the keep_elitism parameter. The value ({keep_elitism}) of type {type(keep_elitism)} found but an integer is expected.") + elif keep_elitism > self.sol_per_pop or keep_elitism < 0: + self.valid_parameters = False + raise ValueError(f"Incorrect value to the keep_elitism parameter: {keep_elitism}. \nThe assigned value to the keep_elitism parameter must satisfy the following conditions: \n1) Less than or equal to sol_per_pop\n2) Greater than or equal to 0.") + + self.keep_elitism = keep_elitism + + # Validate keep_parents. + if self.keep_elitism == 0: + # Keep all parents in the next population. + if self.keep_parents == -1: + self.num_offspring = self.sol_per_pop - self.num_parents_mating + # Keep no parents in the next population. + elif self.keep_parents == 0: + self.num_offspring = self.sol_per_pop + # Keep the specified number of parents in the next population. + elif self.keep_parents > 0: + self.num_offspring = self.sol_per_pop - self.keep_parents + else: + self.num_offspring = self.sol_per_pop - self.keep_elitism + + # Check if the fitness_func is a method. + if inspect.ismethod(fitness_func): + # Check if the fitness method accepts 3 parameters. + if len(inspect.signature(fitness_func).parameters) == 3: + self.fitness_func = fitness_func + else: + self.valid_parameters = False + raise ValueError(f"In PyGAD 2.20.0, if a method is used to calculate the fitness value, then it must accept 3 parameters\n1) The instance of the 'pygad.GA' class.\n2) A solution to calculate its fitness value.\n3) The solution's index within the population.\n\nThe passed fitness method named '{fitness_func.__code__.co_name}' accepts {len(inspect.signature(fitness_func).parameters)} parameter(s).") + elif inspect.isfunction(fitness_func): + # Check if the fitness function accepts 3 parameters. + if len(inspect.signature(fitness_func).parameters) == 3: + self.fitness_func = fitness_func + else: + self.valid_parameters = False + raise ValueError(f"In PyGAD 2.20.0, the fitness function must accept 3 parameters:\n1) The instance of the 'pygad.GA' class.\n2) A solution to calculate its fitness value.\n3) The solution's index within the population.\n\nThe passed fitness function named '{fitness_func.__code__.co_name}' accepts {len(inspect.signature(fitness_func).parameters)} parameter(s).") + elif callable(fitness_func) and not inspect.isclass(fitness_func): + # The object must have the __call__() method. + if hasattr(fitness_func, '__call__'): + # Check if the __call__() method accepts 3 parameters. + if len(inspect.signature(fitness_func).parameters) == 3: + # The fitness class instance assigned to the fitness_func parameter is validated. + self.fitness_func = fitness_func + else: + self.valid_parameters = False + raise ValueError(f"When 'fitness_func' is assigned a class instance, then its __call__ method must accept 3 parameters:\n1) The instance of the 'pygad.GA' class.\n2) A solution to calculate its fitness value.\n3) The solution's index within the population.\n\nThe passed instance of the class named '{fitness_func.__class__.__name__}' accepts {len(inspect.signature(fitness_func).parameters)} parameter(s).") + else: + self.valid_parameters = False + raise ValueError("When 'fitness_func' is assigned a class instance, then its __call__ method must be implemented and accept 3 parameters.") + else: + self.valid_parameters = False + + raise TypeError(f"The value assigned to the fitness_func parameter is expected to be a function or a method but {type(fitness_func)} found.") + + if fitness_batch_size is None: + pass + elif not (type(fitness_batch_size) in self.supported_int_types): + self.valid_parameters = False + raise TypeError(f"The value assigned to the fitness_batch_size parameter is expected to be integer but the value ({fitness_batch_size}) of type {type(fitness_batch_size)} found.") + elif fitness_batch_size <= 0 or fitness_batch_size > self.sol_per_pop: + self.valid_parameters = False + raise ValueError(f"The value assigned to the fitness_batch_size parameter must be:\n1) Greater than 0.\n2) Less than or equal to sol_per_pop ({self.sol_per_pop}).\nBut the value ({fitness_batch_size}) found.") + + self.fitness_batch_size = fitness_batch_size + + # Check if the on_start exists. + if not (on_start is None): + if inspect.ismethod(on_start): + # Check if the on_start method accepts 1 parameter. + if len(inspect.signature(on_start).parameters) == 1: + self.on_start = on_start + else: + self.valid_parameters = False + raise ValueError(f"The method assigned to the on_start parameter must accept only 2 parameters:\n1) The instance of the genetic algorithm.\nThe passed method named '{on_start.__code__.co_name}' accepts {len(inspect.signature(on_start).parameters)} parameter(s).") + # Check if the on_start is a function. + elif inspect.isfunction(on_start): + # Check if the on_start function accepts only a single parameter. + if len(inspect.signature(on_start).parameters) == 1: + self.on_start = on_start + else: + self.valid_parameters = False + raise ValueError(f"The function assigned to the on_start parameter must accept only 1 parameter representing the instance of the genetic algorithm.\nThe passed function named '{on_start.__code__.co_name}' accepts {len(inspect.signature(on_start).parameters)} parameter(s).") + elif callable(on_start) and not inspect.isclass(on_start): + # The object must have the __call__() method. + if hasattr(on_start, '__call__'): + # Check if the __call__() method accepts 1 parameter. + if len(inspect.signature(on_start).parameters) == 1: + # The on_start class instance assigned to the on_start parameter is validated. + self.on_start = on_start + else: + self.valid_parameters = False + raise ValueError(f"When 'on_start' is assigned a class instance, then its __call__ method must accept only 1 parameter representing the instance of the genetic algorithm.\n\nThe passed instance of the class named '{on_start.__class__.__name__}' accepts {len(inspect.signature(on_start).parameters)} parameter(s).") + else: + self.valid_parameters = False + raise ValueError("When 'on_start' is assigned a class instance, then its __call__ method must be implemented and accept 1 parameter.") + else: + self.valid_parameters = False + + raise TypeError(f"The value assigned to the on_start parameter is expected to be of type function but {type(on_start)} found.") + else: + self.on_start = None + + # Check if the on_fitness exists. + if not (on_fitness is None): + # Check if the on_fitness is a method. + if inspect.ismethod(on_fitness): + # Check if the on_fitness method accepts 2 parameters. + if len(inspect.signature(on_fitness).parameters) == 2: + self.on_fitness = on_fitness + else: + self.valid_parameters = False + raise ValueError(f"The method assigned to the on_fitness parameter must accept 2 parameters:\n1) The instance of the genetic algorithm.\n2) The fitness values of all solutions.\nThe passed method named '{on_fitness.__code__.co_name}' accepts {len(inspect.signature(on_fitness).parameters)} parameter(s).") + # Check if the on_fitness is a function. + elif inspect.isfunction(on_fitness): + # Check if the on_fitness function accepts 2 parameters. + if len(inspect.signature(on_fitness).parameters) == 2: + self.on_fitness = on_fitness + else: + self.valid_parameters = False + raise ValueError(f"The function assigned to the on_fitness parameter must accept 2 parameters representing the instance of the genetic algorithm and the fitness values of all solutions.\nThe passed function named '{on_fitness.__code__.co_name}' accepts {on_fitness.__code__.co_argcount} parameter(s).") + elif callable(on_fitness) and not inspect.isclass(on_fitness): + # The object must have the __call__() method. + if hasattr(on_fitness, '__call__'): + # Check if the __call__() method accepts 2 parameters. + if len(inspect.signature(on_fitness).parameters) == 2: + # The on_fitness class instance assigned to the on_fitness parameter is validated. + self.on_fitness = on_fitness + else: + self.valid_parameters = False + raise ValueError(f"When 'on_fitness' is assigned a class instance, then its __call__ method must accept 2 parameters:\n1) The instance of the genetic algorithm.\n2) The fitness values of all solutions.\n\nThe passed instance of the class named '{on_fitness.__class__.__name__}' accepts {len(inspect.signature(on_fitness).parameters)} parameter(s).") + else: + self.valid_parameters = False + raise ValueError("When 'on_fitness' is assigned a class instance, then its __call__ method must be implemented and accept 2 parameters.") + else: + self.valid_parameters = False + raise TypeError(f"The value assigned to the on_fitness parameter is expected to be of type function but {type(on_fitness)} found.") + else: + self.on_fitness = None + + # Check if the on_parents exists. + if not (on_parents is None): + # Check if the on_parents is a method. + if inspect.ismethod(on_parents): + # Check if the on_parents method accepts 2 parameters. + if len(inspect.signature(on_parents).parameters) == 2: + self.on_parents = on_parents + else: + self.valid_parameters = False + raise ValueError(f"The method assigned to the on_parents parameter must accept 2 parameters:\n1) The instance of the genetic algorithm.\n2) The fitness values of all solutions.\nThe passed method named '{on_parents.__code__.co_name}' accepts {len(inspect.signature(on_parents).parameters)} parameter(s).") + # Check if the on_parents is a function. + elif inspect.isfunction(on_parents): + # Check if the on_parents function accepts 2 parameters. + if len(inspect.signature(on_parents).parameters) == 2: + self.on_parents = on_parents + else: + self.valid_parameters = False + raise ValueError(f"The function assigned to the on_parents parameter must accept 2 parameters:\n1) The instance of the genetic algorithm.\n2) The fitness values of all solutions.\nThe passed function named '{on_parents.__code__.co_name}' accepts {len(inspect.signature(on_parents).parameters)} parameter(s).") + elif callable(on_parents) and not inspect.isclass(on_parents): + # The object must have the __call__() method. + if hasattr(on_parents, '__call__'): + # Check if the __call__() method accepts 2 parameters. + if len(inspect.signature(on_parents).parameters) == 2: + # The on_parents class instance assigned to the on_parents parameter is validated. + self.on_parents = on_parents + else: + self.valid_parameters = False + raise ValueError(f"When 'on_parents' is assigned a class instance, then its __call__ method must accept 2 parameters:\n1) The instance of the genetic algorithm.\n2) The fitness values of all solutions.\n\nThe passed instance of the class named '{on_parents.__class__.__name__}' accepts {len(inspect.signature(on_parents).parameters)} parameter(s).") + else: + self.valid_parameters = False + raise ValueError("When 'on_parents' is assigned a class instance, then its __call__ method must be implemented and accept 2 parameters.") + else: + self.valid_parameters = False + raise TypeError(f"The value assigned to the on_parents parameter is expected to be of type function but {type(on_parents)} found.") + else: + self.on_parents = None + + # Check if the on_crossover exists. + if not (on_crossover is None): + # Check if the on_crossover is a method. + if inspect.ismethod(on_crossover): + # Check if the on_crossover method accepts 2 parameters. + if len(inspect.signature(on_crossover).parameters) == 2: + self.on_crossover = on_crossover + else: + self.valid_parameters = False + raise ValueError(f"The method assigned to the on_crossover parameter must accept 2 parameters:\n1) The instance of the genetic algorithm.\n2) The offspring generated using crossover.\nThe passed method named '{on_crossover.__code__.co_name}' accepts {len(inspect.signature(on_crossover).parameters)} parameter(s).") + # Check if the on_crossover is a function. + elif inspect.isfunction(on_crossover): + # Check if the on_crossover function accepts 2 parameters. + if len(inspect.signature(on_crossover).parameters) == 2: + self.on_crossover = on_crossover + else: + self.valid_parameters = False + raise ValueError(f"The function assigned to the on_crossover parameter must accept 2 parameters representing the instance of the genetic algorithm and the offspring generated using crossover.\nThe passed function named '{on_crossover.__code__.co_name}' accepts {len(inspect.signature(on_crossover).parameters)} parameter(s).") + elif callable(on_crossover) and not inspect.isclass(on_crossover): + # The object must have the __call__() method. + if hasattr(on_crossover, '__call__'): + # Check if the __call__() method accepts 2 parameters. + if len(inspect.signature(on_crossover).parameters) == 2: + # The on_crossover class instance assigned to the on_crossover parameter is validated. + self.on_crossover = on_crossover + else: + self.valid_parameters = False + raise ValueError(f"When 'on_crossover' is assigned a class instance, then its __call__ method must accept 2 parameters:\n1) The instance of the genetic algorithm.\n2) The offspring generated using crossover.\n\nThe passed instance of the class named '{on_crossover.__class__.__name__}' accepts {len(inspect.signature(on_crossover).parameters)} parameter(s).") + else: + self.valid_parameters = False + raise ValueError("When 'on_crossover' is assigned a class instance, then its __call__ method must be implemented and accept 2 parameters.") + else: + self.valid_parameters = False + raise TypeError(f"The value assigned to the on_crossover parameter is expected to be of type function but {type(on_crossover)} found.") + else: + self.on_crossover = None + + # Check if the on_mutation exists. + if not (on_mutation is None): + # Check if the on_mutation is a method. + if inspect.ismethod(on_mutation): + # Check if the on_mutation method accepts 2 parameters. + if len(inspect.signature(on_mutation).parameters) == 2: + self.on_mutation = on_mutation + else: + self.valid_parameters = False + raise ValueError(f"The method assigned to the on_mutation parameter must accept 2 parameters:\n1) The instance of the genetic algorithm.\n2) The offspring after applying the mutation operation.\nThe passed method named '{on_mutation.__code__.co_name}' accepts {len(inspect.signature(on_mutation).parameters)} parameter(s).") + # Check if the on_mutation is a function. + elif inspect.isfunction(on_mutation): + # Check if the on_mutation function accepts 2 parameters. + if len(inspect.signature(on_mutation).parameters) == 2: + self.on_mutation = on_mutation + else: + self.valid_parameters = False + raise ValueError(f"The function assigned to the on_mutation parameter must accept 2 parameters representing the instance of the genetic algorithm and the offspring after applying the mutation operation.\nThe passed function named '{on_mutation.__code__.co_name}' accepts {len(inspect.signature(on_mutation).parameters)} parameter(s).") + elif callable(on_mutation) and not inspect.isclass(on_mutation): + # The object must have the __call__() method. + if hasattr(on_mutation, '__call__'): + # Check if the __call__() method accepts 2 parameters. + if len(inspect.signature(on_mutation).parameters) == 2: + # The on_mutation class instance assigned to the on_mutation parameter is validated. + self.on_mutation = on_mutation + else: + self.valid_parameters = False + raise ValueError(f"When 'on_mutation' is assigned a class instance, then its __call__ method must accept 2 parameters:\n1) The instance of the genetic algorithm.\n2) The offspring after applying the mutation operation.\n\nThe passed instance of the class named '{on_mutation.__class__.__name__}' accepts {len(inspect.signature(on_mutation).parameters)} parameter(s).") + else: + self.valid_parameters = False + raise ValueError("When 'on_mutation' is assigned a class instance, then its __call__ method must be implemented and accept 2 parameters.") + else: + self.valid_parameters = False + raise TypeError(f"The value assigned to the on_mutation parameter is expected to be of type function but {type(on_mutation)} found.") + else: + self.on_mutation = None + + # Check if the on_generation exists. + if not (on_generation is None): + # Check if the on_generation is a method. + if inspect.ismethod(on_generation): + # Check if the on_generation method accepts 1 parameter. + if len(inspect.signature(on_generation).parameters) == 1: + self.on_generation = on_generation + else: + self.valid_parameters = False + raise ValueError(f"The method assigned to the on_generation parameter must accept only 1 parameter representing the instance of the genetic algorithm.\nThe passed method named '{on_generation.__code__.co_name}' accepts {len(inspect.signature(on_generation).parameters)} parameter(s).") + # Check if the on_generation is a function. + elif inspect.isfunction(on_generation): + # Check if the on_generation function accepts only a single parameter. + if len(inspect.signature(on_generation).parameters) == 1: + self.on_generation = on_generation + else: + self.valid_parameters = False + raise ValueError(f"The function assigned to the on_generation parameter must accept only 1 parameter representing the instance of the genetic algorithm.\nThe passed function named '{on_generation.__code__.co_name}' accepts {len(inspect.signature(on_generation).parameters)} parameter(s).") + elif callable(on_generation) and not inspect.isclass(on_generation): + # The object must have the __call__() method. + if hasattr(on_generation, '__call__'): + # Check if the __call__() method accepts 1 parameter. + if len(inspect.signature(on_generation).parameters) == 1: + # The on_generation class instance assigned to the on_generation parameter is validated. + self.on_generation = on_generation + else: + self.valid_parameters = False + raise ValueError(f"When 'on_generation' is assigned a class instance, then its __call__ method must accept only 1 parameter representing the instance of the genetic algorithm.\n\nThe passed instance of the class named '{on_generation.__class__.__name__}' accepts {len(inspect.signature(on_generation).parameters)} parameter(s).") + else: + self.valid_parameters = False + raise ValueError("When 'on_generation' is assigned a class instance, then its __call__ method must be implemented and accept 1 parameter.") + else: + self.valid_parameters = False + raise TypeError(f"The value assigned to the on_generation parameter is expected to be of type function but {type(on_generation)} found.") + else: + self.on_generation = None + + # Check if the on_stop exists. + if not (on_stop is None): + # Check if the on_stop is a method. + if inspect.ismethod(on_stop): + # Check if the on_stop method accepts 2 parameters. + if len(inspect.signature(on_stop).parameters) == 2: + self.on_stop = on_stop + else: + self.valid_parameters = False + raise ValueError(f"The method assigned to the on_stop parameter must accept 2 parameters:\n1) The instance of the genetic algorithm.\n2) A list of the fitness values of the solutions in the last population.\n\nThe passed method named '{on_stop.__code__.co_name}' accepts {len(inspect.signature(on_stop).parameters)} parameter(s).") + # Check if the on_stop is a function. + elif inspect.isfunction(on_stop): + # Check if the on_stop function accepts 2 parameters. + if len(inspect.signature(on_stop).parameters) == 2: + self.on_stop = on_stop + else: + self.valid_parameters = False + raise ValueError(f"The function assigned to the on_stop parameter must accept 2 parameters representing the instance of the genetic algorithm and a list of the fitness values of the solutions in the last population.\nThe passed function named '{on_stop.__code__.co_name}' accepts {len(inspect.signature(on_stop).parameters)} parameter(s).") + elif callable(on_stop) and not inspect.isclass(on_stop): + # The object must have the __call__() method. + if hasattr(on_stop, '__call__'): + # Check if the __call__() method accepts 2 parameters. + if len(inspect.signature(on_stop).parameters) == 2: + # The on_stop class instance assigned to the on_stop parameter is validated. + self.on_stop = on_stop + else: + self.valid_parameters = False + raise ValueError(f"When 'on_stop' is assigned a class instance, then its __call__ method must accept 2 parameters: \n1) The instance of the genetic algorithm.\n2) A list of the fitness values of the solutions in the last population.\n\nThe passed instance of the class named '{on_stop.__class__.__name__}' accepts {len(inspect.signature(on_stop).parameters)} parameter(s).") + else: + self.valid_parameters = False + raise ValueError("When 'on_stop' is assigned a class instance, then its __call__ method must be implemented and accept 2 parameters.") + else: + self.valid_parameters = False + raise TypeError(f"The value assigned to the 'on_stop' parameter is expected to be of type function but {type(on_stop)} found.") + else: + self.on_stop = None + + # Validate save_best_solutions + if type(save_best_solutions) is bool: + if save_best_solutions == True: + if not self.suppress_warnings: + warnings.warn("Use the 'save_best_solutions' parameter with caution as it may cause memory overflow when either the number of generations or number of genes is large.") + else: + self.valid_parameters = False + raise TypeError(f"The value passed to the 'save_best_solutions' parameter must be of type bool but {type(save_best_solutions)} found.") + + # Validate save_solutions + if type(save_solutions) is bool: + if save_solutions == True: + if not self.suppress_warnings: + warnings.warn("Use the 'save_solutions' parameter with caution as it may cause memory overflow when either the number of generations, number of genes, or number of solutions in population is large.") + else: + self.valid_parameters = False + raise TypeError(f"The value passed to the 'save_solutions' parameter must be of type bool but {type(save_solutions)} found.") + + self.stop_criteria = [] + self.supported_stop_words = ["reach", "saturate"] + if stop_criteria is None: + # None: Stop after passing through all generations. + self.stop_criteria = None + elif type(stop_criteria) is str: + # reach_{target_fitness}: Stop if the target fitness value is reached. + # saturate_{num_generations}: Stop if the fitness value does not change (saturates) for the given number of generations. + criterion = stop_criteria.split("_") + stop_word = criterion[0] + # criterion[1] might be a single or multiple numbers. + number = criterion[1:] + if stop_word in self.supported_stop_words: + pass + else: + self.valid_parameters = False + raise ValueError(f"In the 'stop_criteria' parameter, the supported stop words are '{self.supported_stop_words}' but '{stop_word}' found.") + + if len(criterion) == 2: + # There is only a single number. + number = number[0] + if number.replace(".", "").replace("-", "").isnumeric(): + number = float(number) + else: + self.valid_parameters = False + raise ValueError(f"The value following the stop word in the 'stop_criteria' parameter must be a number but the value ({number}) of type {type(number)} found.") + + self.stop_criteria.append([stop_word, number]) + elif len(criterion) > 2: + number = self.validate_multi_stop_criteria(stop_word, number) + self.stop_criteria.append([stop_word] + number) + else: + self.valid_parameters = False + raise ValueError(f"For format of a single criterion in the 'stop_criteria' parameter is 'word_number' but '{stop_criteria}' found.") + + elif type(stop_criteria) in [list, tuple, numpy.ndarray]: + # Remove duplicate criteria by converting the list to a set then back to a list. + stop_criteria = list(set(stop_criteria)) + for idx, val in enumerate(stop_criteria): + if type(val) is str: + criterion = val.split("_") + stop_word = criterion[0] + number = criterion[1:] + if len(criterion) == 2: + # There is only a single number. + number = number[0] + if stop_word in self.supported_stop_words: + pass + else: + self.valid_parameters = False + raise ValueError(f"In the 'stop_criteria' parameter, the supported stop words are {self.supported_stop_words} but '{stop_word}' found.") + + if number.replace(".", "").replace("-", "").isnumeric(): + number = float(number) + else: + self.valid_parameters = False + raise ValueError(f"The value following the stop word in the 'stop_criteria' parameter must be a number but the value ({number}) of type {type(number)} found.") + + self.stop_criteria.append([stop_word, number]) + elif len(criterion) > 2: + number = self.validate_multi_stop_criteria(stop_word, number) + self.stop_criteria.append([stop_word] + number) + else: + self.valid_parameters = False + raise ValueError(f"The format of a single criterion in the 'stop_criteria' parameter is 'word_number' but {criterion} found.") + else: + self.valid_parameters = False + raise TypeError(f"When the 'stop_criteria' parameter is assigned a tuple/list/numpy.ndarray, then its elements must be strings but the value ({val}) of type {type(val)} found at index {idx}.") + else: + self.valid_parameters = False + raise TypeError(f"The expected value of the 'stop_criteria' is a single string or a list/tuple/numpy.ndarray of strings but the value ({stop_criteria}) of type {type(stop_criteria)} found.") + + if parallel_processing is None: + self.parallel_processing = None + elif type(parallel_processing) in self.supported_int_types: + if parallel_processing > 0: + self.parallel_processing = ["thread", parallel_processing] + else: + self.valid_parameters = False + raise ValueError(f"When the 'parallel_processing' parameter is assigned an integer, then the integer must be positive but the value ({parallel_processing}) found.") + elif type(parallel_processing) in [list, tuple]: + if len(parallel_processing) == 2: + if type(parallel_processing[0]) is str: + if parallel_processing[0] in ["process", "thread"]: + if (type(parallel_processing[1]) in self.supported_int_types and parallel_processing[1] > 0) or (parallel_processing[1] == 0) or (parallel_processing[1] is None): + if parallel_processing[1] == 0: + # If the number of processes/threads is 0, this means no parallel processing is used. It is equivalent to setting parallel_processing=None. + self.parallel_processing = None + else: + # Whether the second value is None or a positive integer. + self.parallel_processing = parallel_processing + else: + self.valid_parameters = False + raise TypeError(f"When a list or tuple is assigned to the 'parallel_processing' parameter, then the second element must be an integer but the value ({parallel_processing[1]}) of type {type(parallel_processing[1])} found.") + else: + self.valid_parameters = False + raise ValueError(f"When a list or tuple is assigned to the 'parallel_processing' parameter, then the value of the first element must be either 'process' or 'thread' but the value ({parallel_processing[0]}) found.") + else: + self.valid_parameters = False + raise TypeError(f"When a list or tuple is assigned to the 'parallel_processing' parameter, then the first element must be of type 'str' but the value ({parallel_processing[0]}) of type {type(parallel_processing[0])} found.") + else: + self.valid_parameters = False + raise ValueError(f"When a list or tuple is assigned to the 'parallel_processing' parameter, then it must have 2 elements but ({len(parallel_processing)}) found.") + else: + self.valid_parameters = False + raise ValueError(f"Unexpected value ({parallel_processing}) of type ({type(parallel_processing)}) assigned to the 'parallel_processing' parameter. The accepted values for this parameter are:\n1) None: (Default) It means no parallel processing is used.\n2) A positive integer referring to the number of threads to be used (i.e. threads, not processes, are used.\n3) list/tuple: If a list or a tuple of exactly 2 elements is assigned, then:\n\t*1) The first element can be either 'process' or 'thread' to specify whether processes or threads are used, respectively.\n\t*2) The second element can be:\n\t\t**1) A positive integer to select the maximum number of processes or threads to be used.\n\t\t**2) 0 to indicate that parallel processing is not used. This is identical to setting 'parallel_processing=None'.\n\t\t**3) None to use the default value as calculated by the concurrent.futures module.") + + # Set the `run_completed` property to False. It is set to `True` only after the `run()` method is complete. + self.run_completed = False + + # The number of completed generations. + self.generations_completed = 0 + + # At this point, all necessary parameters validation is done successfully, and we are sure that the parameters are valid. + # Set to True when all the parameters passed in the GA class constructor are valid. + self.valid_parameters = True + + # Parameters of the genetic algorithm. + self.num_generations = abs(num_generations) + self.parent_selection_type = parent_selection_type + + # Parameters of the mutation operation. + self.mutation_percent_genes = mutation_percent_genes + self.mutation_num_genes = mutation_num_genes + + # Even though this parameter is declared in the class header, it is assigned to the object here to access it after saving the object. + # A list holding the fitness value of the best solution for each generation. + self.best_solutions_fitness = [] + + # The generation number at which the best fitness value is reached. It is only assigned the generation number after the `run()` method completes. Otherwise, its value is -1. + self.best_solution_generation = -1 + + self.save_best_solutions = save_best_solutions + self.best_solutions = [] # Holds the best solution in each generation. + + self.save_solutions = save_solutions + self.solutions = [] # Holds the solutions in each generation. + # Holds the fitness of the solutions in each generation. + self.solutions_fitness = [] + + # A list holding the fitness values of all solutions in the last generation. + self.last_generation_fitness = None + # A list holding the parents of the last generation. + self.last_generation_parents = None + # A list holding the offspring after applying crossover in the last generation. + self.last_generation_offspring_crossover = None + # A list holding the offspring after applying mutation in the last generation. + self.last_generation_offspring_mutation = None + # Holds the fitness values of one generation before the fitness values saved in the last_generation_fitness attribute. Added in PyGAD 2.16.2. + self.previous_generation_fitness = None + # Added in PyGAD 2.18.0. A NumPy array holding the elitism of the current generation according to the value passed in the 'keep_elitism' parameter. It works only if the 'keep_elitism' parameter has a non-zero value. + self.last_generation_elitism = None + # Added in PyGAD 2.19.0. A NumPy array holding the indices of the elitism of the current generation. It works only if the 'keep_elitism' parameter has a non-zero value. + self.last_generation_elitism_indices = None + # Supported in PyGAD 3.2.0. It holds the pareto fronts when solving a multi-objective problem. + self.pareto_fronts = None + + def validate_multi_stop_criteria(self, stop_word, number): + if stop_word == 'reach': + pass + else: + self.valid_parameters = False + raise ValueError(f"Passing multiple numbers following the keyword in the 'stop_criteria' parameter is expected only with the 'reach' keyword but the keyword ({stop_word}) found.") + + for idx, num in enumerate(number): + if num.replace(".", "").replace("-", "").isnumeric(): + number[idx] = float(num) + else: + self.valid_parameters = False + raise ValueError(f"The value(s) following the stop word in the 'stop_criteria' parameter must be numeric but the value ({num}) of type {type(num)} found.") + return number diff --git a/pygad/visualize/__init__.py b/pygad/visualize/__init__.py index 056dc670..8eb293f8 100644 --- a/pygad/visualize/__init__.py +++ b/pygad/visualize/__init__.py @@ -1,3 +1,3 @@ from pygad.visualize import plot -__version__ = "1.1.0" \ No newline at end of file +__version__ = "1.1.1" \ No newline at end of file diff --git a/pygad/visualize/plot.py b/pygad/visualize/plot.py index 623c0d9c..b1aa9aa5 100644 --- a/pygad/visualize/plot.py +++ b/pygad/visualize/plot.py @@ -117,7 +117,10 @@ def plot_fitness(self, matplt.xlabel(xlabel, fontsize=font_size) matplt.ylabel(ylabel, fontsize=font_size) # Create a legend out of the labels. - matplt.legend() + # Check if there is at least 1 labeled artist. + # If not, the matplt.legend() method will raise a warning. + if not (matplt.gca().get_legend_handles_labels()[0] == []): + matplt.legend() if not save_dir is None: matplt.savefig(fname=save_dir, diff --git a/pyproject.toml b/pyproject.toml index 34ae65c6..42d372da 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ build-backend = "setuptools.build_meta" [project] name = "pygad" -version = "3.5.0" +version = "3.6.0" description = "PyGAD: A Python Library for Building the Genetic Algorithm and Training Machine Learning Algoithms (Keras & PyTorch)." readme = {file = "README.md", content-type = "text/markdown"} requires-python = ">=3" diff --git a/setup.py b/setup.py index 2d9478c9..8fe7dc9b 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="pygad", - version="3.5.0", + version="3.6.0", author="Ahmed Fawzy Gad", install_requires=["numpy", "matplotlib", "cloudpickle",], author_email="ahmed.f.gad@gmail.com", diff --git a/tests/test_best_solution.py b/tests/test_best_solution.py new file mode 100644 index 00000000..16da2d46 --- /dev/null +++ b/tests/test_best_solution.py @@ -0,0 +1,374 @@ +import numpy +import pygad +import random + +# Global constants for testing +num_generations = 100 +num_parents_mating = 5 +sol_per_pop = 10 +num_genes = 3 +random_seed = 42 + +def fitness_func(ga_instance, solution, solution_idx): + """Single-objective fitness function.""" + return numpy.sum(solution**2) + +def fitness_func_multi(ga_instance, solution, solution_idx): + """Multi-objective fitness function.""" + return [numpy.sum(solution**2), numpy.sum(solution)] + +def test_best_solution_consistency_single_objective(): + """ + Test best_solution() consistency for single-objective optimization. + """ + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + random_seed=random_seed, + suppress_warnings=True + ) + ga_instance.run() + + # Call with last_generation_fitness + sol1, fitness1, idx1 = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness) + + # Call without pop_fitness + sol2, fitness2, idx2 = ga_instance.best_solution() + + assert numpy.array_equal(sol1, sol2) + assert fitness1 == fitness2 + assert idx1 == idx2 + print("test_best_solution_consistency_single_objective passed.") + +def test_best_solution_consistency_multi_objective(): + """ + Test best_solution() consistency for multi-objective optimization. + """ + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func_multi, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + random_seed=random_seed, + parent_selection_type="nsga2", + suppress_warnings=True + ) + ga_instance.run() + + # Call with last_generation_fitness + sol1, fitness1, idx1 = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness) + + # Call without pop_fitness + sol2, fitness2, idx2 = ga_instance.best_solution() + + assert numpy.array_equal(sol1, sol2) + assert numpy.array_equal(fitness1, fitness2) + assert idx1 == idx2 + print("test_best_solution_consistency_multi_objective passed.") + +def test_best_solution_before_run(): + """ + Test best_solution() consistency before run() is called. + """ + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + random_seed=random_seed, + suppress_warnings=True + ) + + # Before run(), last_generation_fitness is None + # We can still call best_solution(), it should call cal_pop_fitness() + sol2, fitness2, idx2 = ga_instance.best_solution() + + # Now cal_pop_fitness() should match ga_instance.best_solution() output if we pass it + pop_fitness = ga_instance.cal_pop_fitness() + sol1, fitness1, idx1 = ga_instance.best_solution(pop_fitness=pop_fitness) + + assert numpy.array_equal(sol1, sol2) + assert fitness1 == fitness2 + assert idx1 == idx2 + print("test_best_solution_before_run passed.") + +def test_best_solution_with_save_solutions(): + """ + Test best_solution() consistency when save_solutions=True. + This tests the caching mechanism in cal_pop_fitness(). + """ + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + random_seed=random_seed, + save_solutions=True, + suppress_warnings=True + ) + ga_instance.run() + + # Call with last_generation_fitness + sol1, fitness1, idx1 = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness) + + # Call without pop_fitness (this will trigger cal_pop_fitness which uses saved solutions) + sol2, fitness2, idx2 = ga_instance.best_solution() + + assert numpy.array_equal(sol1, sol2) + assert fitness1 == fitness2 + assert idx1 == idx2 + print("test_best_solution_with_save_solutions passed.") + +def test_best_solution_with_save_best_solutions(): + """ + Test best_solution() consistency when save_best_solutions=True. + """ + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + random_seed=random_seed, + save_best_solutions=True, + suppress_warnings=True + ) + ga_instance.run() + + # Call with last_generation_fitness + sol1, fitness1, idx1 = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness) + + # Call without pop_fitness + sol2, fitness2, idx2 = ga_instance.best_solution() + + assert numpy.array_equal(sol1, sol2) + assert fitness1 == fitness2 + assert idx1 == idx2 + print("test_best_solution_with_save_best_solutions passed.") + +def test_best_solution_with_keep_elitism(): + """ + Test best_solution() consistency when keep_elitism > 0. + """ + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + random_seed=random_seed, + keep_elitism=2, + suppress_warnings=True + ) + ga_instance.run() + + # Call with last_generation_fitness + sol1, fitness1, idx1 = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness) + + # Call without pop_fitness + sol2, fitness2, idx2 = ga_instance.best_solution() + + assert numpy.array_equal(sol1, sol2) + assert fitness1 == fitness2 + assert idx1 == idx2 + print("test_best_solution_with_keep_elitism passed.") + +def test_best_solution_with_keep_parents(): + """ + Test best_solution() consistency when keep_parents > 0. + Note: keep_parents is ignored if keep_elitism > 0 (default is 1). + So this tests the case where keep_parents is passed but effectively ignored by population update, + yet we check if best_solution() still works consistently. + """ + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + random_seed=random_seed, + keep_parents=2, + suppress_warnings=True + ) + ga_instance.run() + + # Call with last_generation_fitness + sol1, fitness1, idx1 = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness) + + # Call without pop_fitness + sol2, fitness2, idx2 = ga_instance.best_solution() + + assert numpy.array_equal(sol1, sol2) + assert fitness1 == fitness2 + assert idx1 == idx2 + print("test_best_solution_with_keep_parents passed.") + +def test_best_solution_with_keep_parents_elitism_0(): + """ + Test best_solution() consistency when keep_parents > 0 and keep_elitism = 0. + This ensures the 'keep_parents' logic in cal_pop_fitness is exercised. + """ + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + random_seed=random_seed, + keep_elitism=0, + keep_parents=2, + suppress_warnings=True + ) + ga_instance.run() + + # Call with last_generation_fitness + sol1, fitness1, idx1 = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness) + + # Call without pop_fitness + sol2, fitness2, idx2 = ga_instance.best_solution() + + assert numpy.array_equal(sol1, sol2) + assert fitness1 == fitness2 + assert idx1 == idx2 + print("test_best_solution_with_keep_parents_elitism_0 passed.") + +def test_best_solution_pop_fitness_validation(): + """ + Test validation of the pop_fitness parameter in best_solution(). + + Note: num_generations=1 is used for speed as evolution is not needed. + sol_per_pop=5 is used to provide a small population for testing invalid lengths. + """ + ga_instance = pygad.GA(num_generations=1, + num_parents_mating=1, + fitness_func=fitness_func, + sol_per_pop=5, + num_genes=3, + suppress_warnings=True + ) + + # Test invalid type + try: + ga_instance.best_solution(pop_fitness="invalid") + except ValueError as e: + assert "expected to be list, tuple, or numpy.ndarray" in str(e) + print("Validation: Invalid type caught.") + + # Test invalid length + try: + ga_instance.best_solution(pop_fitness=[1, 2, 3]) # Length 3, but sol_per_pop is 5 + except ValueError as e: + assert "must match the length of the 'self.population' attribute" in str(e) + print("Validation: Invalid length caught.") + +def test_best_solution_single_objective_tie(): + """ + Test best_solution() when there is a tie in fitness values. + It should return the first solution with the maximum fitness. + + Note: sol_per_pop=5 must match the length of the manual pop_fitness array below. + num_generations=1 is sufficient for testing selection logic. + """ + ga_instance = pygad.GA(num_generations=1, + num_parents_mating=1, + fitness_func=fitness_func, + sol_per_pop=5, + num_genes=3, + suppress_warnings=True + ) + + # Mock fitness with a tie at index 1 and 3 + pop_fitness = numpy.array([10, 50, 20, 50, 5]) + + sol, fitness, idx = ga_instance.best_solution(pop_fitness=pop_fitness) + + assert fitness == 50 + assert idx == 1 # First occurrence + print("test_best_solution_single_objective_tie passed.") + +def test_best_solution_with_parallel_processing(): + """ + Test best_solution() with parallel_processing enabled. + + Note: num_generations=5 is used to ensure the initial population and first generation + trigger parallel fitness calculation. + """ + ga_instance = pygad.GA(num_generations=5, + num_parents_mating=2, + fitness_func=fitness_func, + sol_per_pop=10, + num_genes=3, + random_seed=random_seed, + parallel_processing=["thread", 2], + suppress_warnings=True + ) + # best_solution() should work and trigger cal_pop_fitness() internally + sol, fitness, idx = ga_instance.best_solution() + assert sol is not None + assert fitness is not None + print("test_best_solution_with_parallel_processing passed.") + +def test_best_solution_with_fitness_batch_size(): + """ + Test best_solution() with fitness_batch_size > 1. + + Note: num_generations=5 and sol_per_pop=10 provide enough work for batch processing. + """ + def fitness_func_batch(ga_instance, solutions, indices): + return [numpy.sum(s**2) for s in solutions] + + ga_instance = pygad.GA(num_generations=5, + num_parents_mating=2, + fitness_func=fitness_func_batch, + sol_per_pop=10, + num_genes=3, + random_seed=random_seed, + fitness_batch_size=2, + suppress_warnings=True + ) + + sol, fitness, idx = ga_instance.best_solution() + assert sol is not None + assert fitness is not None + print("test_best_solution_with_fitness_batch_size passed.") + +def test_best_solution_pop_fitness_types(): + """ + Test best_solution() with different types for the pop_fitness parameter. + + Note: sol_per_pop=3 must match the length of fitness_vals below. + num_generations=1 is sufficient for this type-check test. + """ + ga_instance = pygad.GA(num_generations=1, + num_parents_mating=1, + fitness_func=fitness_func, + sol_per_pop=3, + num_genes=3, + suppress_warnings=True + ) + + fitness_vals = [1.0, 5.0, 2.0] + + # Test list + _, _, idx_list = ga_instance.best_solution(pop_fitness=fitness_vals) + # Test tuple + _, _, idx_tuple = ga_instance.best_solution(pop_fitness=tuple(fitness_vals)) + # Test numpy array + _, _, idx_ndarray = ga_instance.best_solution(pop_fitness=numpy.array(fitness_vals)) + + assert idx_list == idx_tuple == idx_ndarray == 1 + print("test_best_solution_pop_fitness_types passed.") + +if __name__ == "__main__": + test_best_solution_consistency_single_objective() + test_best_solution_consistency_multi_objective() + test_best_solution_before_run() + test_best_solution_with_save_solutions() + test_best_solution_with_save_best_solutions() + test_best_solution_with_keep_elitism() + test_best_solution_with_keep_parents() + test_best_solution_with_keep_parents_elitism_0() + test_best_solution_pop_fitness_validation() + test_best_solution_single_objective_tie() + test_best_solution_with_parallel_processing() + test_best_solution_with_fitness_batch_size() + test_best_solution_pop_fitness_types() + print("\nAll tests passed!") diff --git a/tests/test_gann.py b/tests/test_gann.py new file mode 100644 index 00000000..1d4c7af5 --- /dev/null +++ b/tests/test_gann.py @@ -0,0 +1,75 @@ +import pygad.gann +import pygad.nn +import numpy + +def test_gann_regression(): + """Test GANN for a simple regression problem.""" + # Data + data_inputs = numpy.array([[0.02, 0.1, 0.15], + [0.7, 0.6, 0.8], + [1.5, 1.2, 1.7], + [3.2, 2.9, 3.1]]) + data_outputs = numpy.array([0.1, 0.6, 1.3, 2.5]) + + # GANN architecture + num_inputs = data_inputs.shape[1] + num_classes = 1 # Regression + + gann_instance = pygad.gann.GANN(num_solutions=10, + num_neurons_input=num_inputs, + num_neurons_hidden_layers=[5], + num_neurons_output=num_classes, + hidden_activations="relu", + output_activation="None") + + # The number of genes is the total number of weights in the network. + # We can get it by converting the weights of any network in the population into a vector. + num_genes = len(pygad.nn.layers_weights_as_vector(last_layer=gann_instance.population_networks[0])) + + def fitness_func(ga_instance, solution, solution_idx): + # Update the weights of the network associated with the current solution. + # GANN.update_population_trained_weights expects weights for ALL solutions. + # To avoid updating all, we can update just the one we need. + + # However, for simplicity and to test GANN's intended flow: + population_matrices = pygad.gann.population_as_matrices(population_networks=gann_instance.population_networks, + population_vectors=ga_instance.population) + gann_instance.update_population_trained_weights(population_trained_weights=population_matrices) + + predictions = pygad.nn.predict(last_layer=gann_instance.population_networks[solution_idx], + data_inputs=data_inputs) + + # Mean Absolute Error + abs_error = numpy.mean(numpy.abs(predictions - data_outputs)) + 0.00000001 + fitness = 1.0 / abs_error + return fitness + + ga_instance = pygad.GA(num_generations=5, + num_parents_mating=4, + fitness_func=fitness_func, + sol_per_pop=10, + num_genes=num_genes, + random_seed=42, + suppress_warnings=True) + + ga_instance.run() + assert ga_instance.run_completed + print("test_gann_regression passed.") + +def test_nn_direct_usage(): + """Test pygad.nn layers directly.""" + input_layer = pygad.nn.InputLayer(num_inputs=3) + dense_layer = pygad.nn.DenseLayer(num_neurons=2, previous_layer=input_layer, activation_function="relu") + output_layer = pygad.nn.DenseLayer(num_neurons=1, previous_layer=dense_layer, activation_function="sigmoid") + + data_inputs = numpy.array([[0.1, 0.2, 0.3]]) + predictions = pygad.nn.predict(last_layer=output_layer, data_inputs=data_inputs) + + assert len(predictions) == 1 + assert 0 <= predictions[0] <= 1 + print("test_nn_direct_usage passed.") + +if __name__ == "__main__": + test_gann_regression() + test_nn_direct_usage() + print("\nAll GANN/NN tests passed!") diff --git a/tests/test_operators.py b/tests/test_operators.py new file mode 100644 index 00000000..e56800ae --- /dev/null +++ b/tests/test_operators.py @@ -0,0 +1,77 @@ +import pygad +import numpy +import random + +# Global constants for testing +num_generations = 5 +num_parents_mating = 4 +sol_per_pop = 10 +num_genes = 10 +random_seed = 42 + +def fitness_func(ga_instance, solution, solution_idx): + return numpy.sum(solution) + +def fitness_func_multi(ga_instance, solution, solution_idx): + return [numpy.sum(solution), numpy.sum(solution**2)] + +def run_ga_with_params(parent_selection_type='sss', crossover_type='single_point', mutation_type='random', multi_objective=False): + if multi_objective: + f = fitness_func_multi + else: + f = fitness_func + + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=f, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + parent_selection_type=parent_selection_type, + crossover_type=crossover_type, + mutation_type=mutation_type, + random_seed=random_seed, + suppress_warnings=True) + ga_instance.run() + return ga_instance + +def test_selection_operators(): + operators = ['sss', 'rws', 'sus', 'rank', 'random', 'tournament'] + for op in operators: + ga = run_ga_with_params(parent_selection_type=op) + # Verify parents were selected + assert ga.last_generation_parents.shape == (num_parents_mating, num_genes) + print(f"Selection operator '{op}' passed.") + +def test_crossover_operators(): + operators = ['single_point', 'two_points', 'uniform', 'scattered'] + for op in operators: + ga = run_ga_with_params(crossover_type=op) + # Verify population shape + assert ga.population.shape == (sol_per_pop, num_genes) + print(f"Crossover operator '{op}' passed.") + +def test_mutation_operators(): + operators = ['random', 'swap', 'inversion', 'scramble'] + for op in operators: + ga = run_ga_with_params(mutation_type=op) + # Verify population shape + assert ga.population.shape == (sol_per_pop, num_genes) + print(f"Mutation operator '{op}' passed.") + +def test_multi_objective_selection(): + # NSGA-II is usually used for multi-objective + ga = run_ga_with_params(parent_selection_type='nsga2', multi_objective=True) + assert ga.last_generation_parents.shape == (num_parents_mating, num_genes) + print("Multi-objective selection (nsga2) passed.") + + # Tournament NSGA-II + ga = run_ga_with_params(parent_selection_type='tournament_nsga2', multi_objective=True) + assert ga.last_generation_parents.shape == (num_parents_mating, num_genes) + print("Multi-objective selection (tournament_nsga2) passed.") + +if __name__ == "__main__": + test_selection_operators() + test_crossover_operators() + test_mutation_operators() + test_multi_objective_selection() + print("\nAll operator tests passed!") diff --git a/tests/test_parallel.py b/tests/test_parallel.py new file mode 100644 index 00000000..ee6c2ad5 --- /dev/null +++ b/tests/test_parallel.py @@ -0,0 +1,73 @@ +import pygad +import numpy +import time + +# Global constants for testing +num_generations = 5 +num_parents_mating = 4 +sol_per_pop = 10 +num_genes = 3 +random_seed = 42 + +def fitness_func(ga_instance, solution, solution_idx): + # Simulate some work + # time.sleep(0.01) + return numpy.sum(solution**2) + +def fitness_func_batch(ga_instance, solutions, indices): + return [numpy.sum(s**2) for s in solutions] + +def test_parallel_thread(): + """Test parallel_processing with 'thread' mode.""" + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + parallel_processing=["thread", 2], + random_seed=random_seed, + suppress_warnings=True + ) + ga_instance.run() + assert ga_instance.run_completed + print("test_parallel_thread passed.") + +def test_parallel_process(): + """Test parallel_processing with 'process' mode.""" + # Note: 'process' mode might be tricky in some environments (e.g. Windows without if __name__ == '__main__':) + # But for a CI environment it should be tested. + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + parallel_processing=["process", 2], + random_seed=random_seed, + suppress_warnings=True + ) + ga_instance.run() + assert ga_instance.run_completed + print("test_parallel_process passed.") + +def test_parallel_thread_batch(): + """Test parallel_processing with 'thread' mode and batch fitness.""" + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func_batch, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + parallel_processing=["thread", 2], + fitness_batch_size=2, + random_seed=random_seed, + suppress_warnings=True + ) + ga_instance.run() + assert ga_instance.run_completed + print("test_parallel_thread_batch passed.") + +if __name__ == "__main__": + # For 'process' mode to work on Windows/macOS, we need this guard + test_parallel_thread() + test_parallel_process() + test_parallel_thread_batch() + print("\nAll parallel tests passed!") diff --git a/tests/test_summary.py b/tests/test_summary.py new file mode 100644 index 00000000..4830e8bc --- /dev/null +++ b/tests/test_summary.py @@ -0,0 +1,123 @@ +import pygad +import random + +num_generations = 100 +sol_per_pop = 10 +num_parents_mating = 5 + +def number_saved_solutions(keep_elitism=1, + keep_parents=-1, + mutation_type="random", + mutation_percent_genes="default", + parent_selection_type='sss', + multi_objective=False, + fitness_batch_size=None, + save_solutions=False, + save_best_solutions=False): + + def fitness_func_no_batch_single(ga, solution, idx): + return random.random() + + def fitness_func_no_batch_multi(ga_instance, solution, solution_idx): + return [random.random(), random.random()] + + def fitness_func_batch_single(ga_instance, solution, solution_idx): + f = [] + for sol in solution: + f.append(random.random()) + return f + + def fitness_func_batch_multi(ga_instance, solution, solution_idx): + f = [] + for sol in solution: + f.append([random.random(), random.random()]) + return f + + if fitness_batch_size is None or (type(fitness_batch_size) in pygad.GA.supported_int_types and fitness_batch_size == 1): + if multi_objective == True: + fitness_func = fitness_func_no_batch_multi + else: + fitness_func = fitness_func_no_batch_single + elif (type(fitness_batch_size) in pygad.GA.supported_int_types and fitness_batch_size > 1): + if multi_objective == True: + fitness_func = fitness_func_batch_multi + else: + fitness_func = fitness_func_batch_single + + ga_optimizer = pygad.GA(num_generations=num_generations, + sol_per_pop=sol_per_pop, + num_genes=6, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + mutation_type=mutation_type, + parent_selection_type=parent_selection_type, + mutation_percent_genes=mutation_percent_genes, + keep_elitism=keep_elitism, + keep_parents=keep_parents, + suppress_warnings=True, + fitness_batch_size=fitness_batch_size, + save_best_solutions=save_best_solutions, + save_solutions=save_solutions) + + ga_optimizer.run() + + ga_optimizer.summary() + +#### Single Objective +def test_save_solutions_default_keep(): + number_saved_solutions() + + +######## Batch Fitness +#### Single Objective +def test_save_solutions_no_keep_batch_1(): + number_saved_solutions(fitness_batch_size=1) + + +#### Multi Objective +def test_save_solutions_default_keep_multi_objective(): + number_saved_solutions(multi_objective=True) + + +#### Multi Objective +def test_save_solutions_no_keep_multi_objective_batch_1(): + number_saved_solutions(multi_objective=True, + fitness_batch_size=1) + +#### Multi Objective NSGA-II Parent Selection +def test_save_solutions_default_keep_multi_objective_nsga2(): + number_saved_solutions(multi_objective=True, + parent_selection_type='nsga2') + +#### Multi Objective NSGA-II Parent Selection +def test_save_solutions_no_keep_multi_objective_nsga2_batch_1(): + number_saved_solutions(multi_objective=True, + parent_selection_type='nsga2', + fitness_batch_size=1) + +if __name__ == "__main__": + #### Single Objective + print() + test_save_solutions_default_keep() + print() + + #### Multi-Objective + print() + test_save_solutions_default_keep_multi_objective() + + #### Multi-Objective NSGA-II Parent Selection + print() + test_save_solutions_default_keep_multi_objective_nsga2() + + ######## Batch Fitness Calculation + #### Single Objective + print() + test_save_solutions_no_keep_batch_1() + + #### Multi-Objective + print() + test_save_solutions_no_keep_multi_objective_batch_1() + + #### Multi-Objective NSGA-II Parent Selection + print() + test_save_solutions_no_keep_multi_objective_nsga2_batch_1() diff --git a/tests/test_visualize.py b/tests/test_visualize.py new file mode 100644 index 00000000..a2900d04 --- /dev/null +++ b/tests/test_visualize.py @@ -0,0 +1,196 @@ +import pygad +import numpy +import os +import matplotlib +# Use Agg backend for headless testing (no GUI needed) +matplotlib.use('Agg') +import matplotlib.pyplot as plt + +# Global constants for testing +num_generations = 5 +num_parents_mating = 4 +sol_per_pop = 10 +num_genes = 3 +random_seed = 42 + +def fitness_func(ga_instance, solution, solution_idx): + return numpy.sum(solution**2) + +def fitness_func_multi(ga_instance, solution, solution_idx): + return [numpy.sum(solution**2), numpy.sum(solution)] + +def test_plot_fitness_parameters(): + """Test all parameters of plot_fitness().""" + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + random_seed=random_seed, + suppress_warnings=True + ) + ga_instance.run() + + # Test different plot types + for p_type in ["plot", "scatter", "bar"]: + fig = ga_instance.plot_fitness(plot_type=p_type, + title=f"Title {p_type}", + xlabel="X", ylabel="Y", + linewidth=2, font_size=12, color="blue") + assert isinstance(fig, matplotlib.figure.Figure) + plt.close(fig) + + # Test multi-objective specific parameters + ga_multi = pygad.GA(num_generations=2, + num_parents_mating=2, + fitness_func=fitness_func_multi, + sol_per_pop=5, + num_genes=3, + parent_selection_type="nsga2", + suppress_warnings=True) + ga_multi.run() + + fig = ga_multi.plot_fitness(linewidth=[2, 4], + color=["blue", "green"], + label=["Obj A", "Obj B"]) + assert isinstance(fig, matplotlib.figure.Figure) + plt.close(fig) + print("test_plot_fitness_parameters passed.") + +def test_plot_new_solution_rate_parameters(): + """Test all parameters of plot_new_solution_rate() and its validation.""" + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + random_seed=random_seed, + save_solutions=True, + suppress_warnings=True + ) + ga_instance.run() + + # Test different plot types and parameters + for p_type in ["plot", "scatter", "bar"]: + fig = ga_instance.plot_new_solution_rate(title=f"Rate {p_type}", + plot_type=p_type, + linewidth=2, color="purple") + assert isinstance(fig, matplotlib.figure.Figure) + plt.close(fig) + + # Validation: Test error when save_solutions=False + ga_instance_no_save = pygad.GA(num_generations=1, + num_parents_mating=1, + fitness_func=fitness_func, + sol_per_pop=5, + num_genes=2, + save_solutions=False, + suppress_warnings=True) + ga_instance_no_save.run() + try: + ga_instance_no_save.plot_new_solution_rate() + except RuntimeError: + print("plot_new_solution_rate validation caught.") + + print("test_plot_new_solution_rate_parameters passed.") + +def test_plot_genes_parameters(): + """Test all parameters of plot_genes().""" + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + random_seed=random_seed, + save_solutions=True, + save_best_solutions=True, + suppress_warnings=True + ) + ga_instance.run() + + # Test different graph types and parameters + for g_type in ["plot", "boxplot", "histogram"]: + fig = ga_instance.plot_genes(graph_type=g_type, fill_color="yellow", color="black") + assert isinstance(fig, matplotlib.figure.Figure) + plt.close(fig) + + # Test solutions="best" + fig = ga_instance.plot_genes(solutions="best") + assert isinstance(fig, matplotlib.figure.Figure) + plt.close(fig) + + print("test_plot_genes_parameters passed.") + +def test_plot_pareto_front_curve_parameters(): + """Test all parameters of plot_pareto_front_curve() and its validation.""" + ga_instance = pygad.GA(num_generations=num_generations, + num_parents_mating=num_parents_mating, + fitness_func=fitness_func_multi, + sol_per_pop=sol_per_pop, + num_genes=num_genes, + random_seed=random_seed, + parent_selection_type="nsga2", + suppress_warnings=True + ) + ga_instance.run() + + fig = ga_instance.plot_pareto_front_curve(title="Pareto", + linewidth=4, + label="Frontier", + color="red", + color_fitness="black", + grid=False, + alpha=0.5, + marker="x") + assert isinstance(fig, matplotlib.figure.Figure) + plt.close(fig) + + # Validation: Test error for single-objective + ga_instance_single = pygad.GA(num_generations=1, + num_parents_mating=1, + fitness_func=fitness_func, + sol_per_pop=5, + num_genes=2, + suppress_warnings=True) + ga_instance_single.run() + try: + ga_instance_single.plot_pareto_front_curve() + except RuntimeError: + print("plot_pareto_front_curve validation (multi-objective required) caught.") + + print("test_plot_pareto_front_curve_parameters passed.") + +def test_visualize_save_dir(): + """Test save_dir parameter for all methods.""" + ga_instance = pygad.GA(num_generations=2, + num_parents_mating=2, + fitness_func=fitness_func, + sol_per_pop=5, + num_genes=2, + save_solutions=True, + suppress_warnings=True + ) + ga_instance.run() + + methods = [ + (ga_instance.plot_fitness, {}), + (ga_instance.plot_new_solution_rate, {}), + (ga_instance.plot_genes, {"graph_type": "plot"}) + ] + + for method, kwargs in methods: + filename = f"test_{method.__name__}.png" + if os.path.exists(filename): os.remove(filename) + method(save_dir=filename, **kwargs) + assert os.path.exists(filename) + os.remove(filename) + + print("test_visualize_save_dir passed.") + +if __name__ == "__main__": + test_plot_fitness_parameters() + test_plot_new_solution_rate_parameters() + test_plot_genes_parameters() + test_plot_pareto_front_curve_parameters() + test_visualize_save_dir() + print("\nAll visualization tests passed!")