diff --git a/README.md b/README.md index d1e8e4d..015c020 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,10 @@ The library is under active development and more features are added regularly. I # Donation -For donattion: -* By Card (recommended): https://donate.stripe.com/eVa5kO866elKgM0144 -* [Open Collective](https://opencollective.com/pygad): [opencollective.com/pygad](https://opencollective.com/pygad). -* PayPal: Either this link: [paypal.me/ahmedfgad](https://paypal.me/ahmedfgad) or the e-mail address ahmed.f.gad@gmail.com. +* [Credit/Debit Card](https://donate.stripe.com/eVa5kO866elKgM0144): https://donate.stripe.com/eVa5kO866elKgM0144 +* [Open Collective](https://opencollective.com/pygad): [opencollective.com/pygad](https://opencollective.com/pygad) +* PayPal: Use either this link: [paypal.me/ahmedfgad](https://paypal.me/ahmedfgad) or the e-mail address ahmed.f.gad@gmail.com +* Interac e-Transfer: Use e-mail address ahmed.f.gad@gmail.com # Installation @@ -76,7 +76,7 @@ import numpy function_inputs = [4,-2,3.5,5,-11,-4.7] desired_output = 44 -def fitness_func(solution, solution_idx): +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 @@ -164,7 +164,7 @@ What are the best values for the 6 weights (w1 to w6)? We are going to use the g function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs. desired_output = 44 # Function output. -def fitness_func(solution, solution_idx): +def fitness_func(ga_instance, solution, solution_idx): # Calculating the fitness value of each solution in the current population. # The fitness function calulates the sum of products between each input and its corresponding weight. output = numpy.sum(solution*function_inputs) @@ -280,7 +280,7 @@ To start with coding the genetic algorithm, you can check the tutorial titled [* - [KDnuggets](https://www.kdnuggets.com/2018/04/building-convolutional-neural-network-numpy-scratch.html) - [Chinese Translation](http://m.aliyun.com/yunqi/articles/585741) -[This tutorial](https://www.linkedin.com/pulse/building-convolutional-neural-network-using-numpy-from-ahmed-gad) is prepared based on a previous version of the project but it still a good resource to start with coding CNNs. +[This tutorial](https://www.linkedin.com/pulse/building-convolutional-neural-network-using-numpy-from-ahmed-gad)) is prepared based on a previous version of the project but it still a good resource to start with coding CNNs. [![Building CNN in Python](https://user-images.githubusercontent.com/16560492/82431022-6c3a1200-9a8e-11ea-8f1b-b055196d76e3.png)](https://www.linkedin.com/pulse/building-convolutional-neural-network-using-numpy-from-ahmed-gad) @@ -331,4 +331,3 @@ If you used PyGAD, please consider adding a citation to the following paper abou * [KDnuggets](https://kdnuggets.com/author/ahmed-gad) * [TowardsDataScience](https://towardsdatascience.com/@ahmedfgad) * [GitHub](https://github.com/ahmedfgad) - diff --git a/__init__.py b/__init__.py index f5556d9..71f207b 100644 --- a/__init__.py +++ b/__init__.py @@ -1,3 +1,3 @@ from .pygad import * # Relative import. -__version__ = "2.19.2" +__version__ = "3.0.0" diff --git a/docs/source/Footer.rst b/docs/source/Footer.rst index 361bf62..5efa117 100644 --- a/docs/source/Footer.rst +++ b/docs/source/Footer.rst @@ -852,7 +852,7 @@ Release Date: 28 September 2021 equation_inputs = [4,-2,3.5] desired_output = 44 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness @@ -891,7 +891,7 @@ progress bar. equation_inputs = [4,-2,3.5] desired_output = 44 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness @@ -1119,7 +1119,7 @@ Release Date: 22 February 2023 (https://github.com/cloudpipe/cloudpickle) is used instead of the ``pickle`` library to pickle the ``pygad.GA`` objects. This solves the issue of having to redefine the functions (e.g. fitness - function). The ``cloudpickle`` library is added as a dependancy in + function). The ``cloudpickle`` library is added as a dependency in the ``requirements.txt`` file. https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/159 @@ -1185,12 +1185,107 @@ Release Date: 22 February 2023 PyGAD 2.19.2 ------------ -Release Data 23 February 2023 +Release Date 23 February 2023 1. Fix an issue when parallel processing was used where the elitism solutions' fitness values are not re-used. https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/160#issuecomment-1441718184 +.. _pygad-300: + +PyGAD 3.0.0 +----------- + +Release Date ... 2023 + +1. The structure of the library is changed and some methods defined in + the ``pygad.py`` module are moved to the ``pygad.utils``, + ``pygad.helper``, and ``pygad.visualize`` submodules. + +2. The ``pygad.utils.parent_selection`` module has a class named + ``ParentSelection`` where all the parent selection operators exist. + +3. The ``pygad.utils.crossover`` module has a class named ``Crossover`` + where all the crossover operators exist. + +4. The ``pygad.utils.mutation`` module has a class named ``Mutation`` + where all the mutation operators exist. + +5. The ``pygad.helper.unique`` module has a class named ``Unique`` some + helper methods exist to solve duplicate genes and make sure every + gene is unique. + +6. | The ``pygad.visualize.plot`` module has a class named ``Plot`` + where all the methods that create plots exist. + | The ``pygad.GA`` class extends all of these classes. + +.. code:: python + + ... + class GA(utils.parent_selection.ParentSelection, + utils.crossover.Crossover, + utils.mutation.Mutation, + helper.unique.Unique, + visualize.plot.Plot): + ... + +1. Support of using the ``logging`` module to log the outputs to both + the console and text file instead of using the ``print()`` function. + This is by assigning the ``logging.Logger`` to the new ``logger`` + parameter. Check the `Logging + Outputs `__ + for more information. + +2. A new instance attribute called ``logger`` to save the logger. + +3. The function/method passed to the ``fitness_func`` parameter accepts + a new parameter that refers to the instance of the ``pygad.GA`` + class. Check this for an example: `Use Functions and Methods to + Build Fitness Function and + Callbacks `__. + https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/163 + +4. Update the documentation to include an example of using functions + and methods to calculate the fitness and build callbacks. Check this + for more details: `Use Functions and Methods to Build Fitness + Function and + Callbacks `__. + https://github.com/ahmedfgad/GeneticAlgorithmPython/pull/92#issuecomment-1443635003 + +5. Validate the value passed to the ``initial_population`` parameter. + +6. Validate the type and length of the ``pop_fitness`` parameter of the + ``best_solution()`` method. + +7. Some edits in the documentation. + https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/106 + +8. Fix an issue when building the initial population as (some) genes + have their value taken from the mutation range (defined by the + parameters ``random_mutation_min_val`` and + ``random_mutation_max_val``) instead of using the parameters + ``init_range_low`` and ``init_range_high``. + +9. The ``summary()`` method returns the summary as a single-line + string. Just log/print the returned string it to see it properly. + +10. The ``callback_generation`` parameter is removed. Use the + ``on_generation`` parameter instead. + +11. There was an issue when using the ``parallel_processing`` parameter + with Keras and PyTorch. As Keras/PyTorch are not thread-safe, the + ``predict()`` method gives incorrect and weird results when more + than 1 thread is used. + https://github.com/ahmedfgad/GeneticAlgorithmPython/issues/145 + https://github.com/ahmedfgad/TorchGA/issues/5 + https://github.com/ahmedfgad/KerasGA/issues/6. Thanks to this + `StackOverflow + answer `__. + +12. Replace ``numpy.float`` by ``float`` in the 2 parent selection + operators roulette wheel and stochastic universal. + https://github.com/ahmedfgad/GeneticAlgorithmPython/pull/168 + PyGAD Projects at GitHub ======================== diff --git a/docs/source/README_pygad_ReadTheDocs.rst b/docs/source/README_pygad_ReadTheDocs.rst index 2b25a38..1ab5505 100644 --- a/docs/source/README_pygad_ReadTheDocs.rst +++ b/docs/source/README_pygad_ReadTheDocs.rst @@ -33,16 +33,12 @@ The ``pygad.GA`` class constructor supports the following parameters: - ``num_parents_mating``: Number of solutions to be selected as parents. -- ``fitness_func``: Accepts a function that must accept 2 parameters (a - single solution and its index in the population) and return the - fitness value of the solution. Available starting from `PyGAD - 1.0.17 `__ - until - `1.0.20 `__ - with a single parameter representing the solution. Changed in `PyGAD - 2.0.0 `__ - and higher to include a second parameter representing the solution - index. Check the `Preparing the fitness_func +- ``fitness_func``: Accepts a function/method and returns the fitness + value of the solution. If a function is passed, then it must accept 3 + parameters (1. the instance of the ``pygad.GA`` class, 2. a single + solution, and 3. its index in the population). If method, then it + accepts a fourth parameter representing the method's class instance. + Check the `Preparing the fitness_func Parameter `__ section for information about creating such a function. @@ -131,7 +127,10 @@ The ``pygad.GA`` class constructor supports the following parameters: solutions within the population ``sol_per_pop``. Starting from `PyGAD 2.18.0 `__, this parameter have an effect only when the ``keep_elitism`` - parameter is ``0``. + parameter is ``0``. Starting from `PyGAD + 2.20.0 `__, + the parents' fitness from the last generation will not be re-used if + ``keep_parents=0``. - ``keep_elitism=1``: Added in `PyGAD 2.18.0 `__. @@ -295,23 +294,26 @@ The ``pygad.GA`` class constructor supports the following parameters: moving from the start to the end of the range specified by the 2 existing keys ``"low"`` and ``"high"``. -- ``on_start=None``: Accepts a function to be called only once before - the genetic algorithm starts its evolution. This function must accept - a single parameter representing the instance of the genetic - algorithm. Added in `PyGAD +- ``on_start=None``: 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=None``: Accepts a function to be called after - calculating the fitness values of all solutions in the population. - This function must accept 2 parameters: the first one represents the - instance of the genetic algorithm and the second one is a list of all - solutions' fitness values. Added in `PyGAD +- ``on_fitness=None``: 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=None``: Accepts a function to be called after selecting - the parents that mates. This function must accept 2 parameters: the - first one represents the instance of the genetic algorithm and the - second one represents the selected parents. Added in `PyGAD +- ``on_parents=None``: Accepts a function/method to be called after + selecting the parents that mates. If function, then it must accept 2 + parameters: 1) the selected parents 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_crossover=None``: Accepts a function to be called each time the @@ -328,23 +330,6 @@ The ``pygad.GA`` class constructor supports the following parameters: the mutation. Added in `PyGAD 2.6.0 `__. -- ``callback_generation=None``: Accepts a function to be called after - each generation. This function must accept a single parameter - representing the instance of the genetic algorithm. Supported in - `PyGAD - 2.0.0 `__ - and higher. In `PyGAD - 2.4.0 `__, - if this function returned the string ``stop``, then the ``run()`` - method stops at the current generation without completing the - remaining generations. Check the `Release - History `__ - section of the documentation for an example. Starting from `PyGAD - 2.6.0 `__, - the ``callback_generation`` parameter is deprecated and should be - replaced by the ``on_generation`` parameter. The - ``callback_generation`` parameter will be removed in a later version. - - ``on_generation=None``: Accepts a function to be called after each generation. This function must accept a single parameter representing the instance of the genetic algorithm. If the function returned the @@ -425,6 +410,16 @@ The ``pygad.GA`` class constructor supports the following parameters: seed (e.g. ``random_seed=2``). If given the value ``None``, then it has no effect. +- ``logger=None``: Accepts an instance of the ``logging.Logger`` class + to log the outputs. Any message is no longer printed using + ``print()`` but logged. If ``logger=None``, then a logger is created + that uses ``StreamHandler`` to logs the messages to the console. + Added in `PyGAD + 3.0.0 `__. + Check the `Logging + Outputs `__ + for more information. + The user doesn't have to specify all of such parameters while creating an instance of the GA class. A very important parameter you must care about is ``fitness_func`` which defines the fitness function. @@ -554,6 +549,10 @@ Other Attributes ``keep_elitism`` parameter has a non-zero value. Supported in `PyGAD 2.19.0 `__. +- ``logger``: This attribute holds the logger from the ``logging`` + module. Supported in `PyGAD + 3.0.0 `__. + Note that the attributes with its name start with ``last_generation_`` are updated after each generation. @@ -715,8 +714,8 @@ After the generation completes, the following takes place: - The ``generations_completed`` attribute is assigned by the number of the last completed generation. -- If there is a callback function assigned to the - ``callback_generation`` attribute, then it will be called. +- 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: @@ -728,9 +727,10 @@ After the ``run()`` method completes, the following takes place: Parent Selection Methods ------------------------ -The ``pygad.GA`` class has several methods for selecting the parents -that will mate to produce the offspring. All of such methods accept the -same parameters which are: +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. @@ -786,9 +786,9 @@ Selects the parents using the stochastic universal selection technique. Crossover Methods ----------------- -The ``pygad.GA`` class supports several methods for applying crossover -between the selected parents. All of these methods accept the same -parameters which are: +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. @@ -833,8 +833,9 @@ of the 2 parents. Mutation Methods ---------------- -The ``pygad.GA`` class supports several methods for applying mutation. -All of these methods accept the same parameter which is: +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. @@ -1103,14 +1104,14 @@ is the calculation of the fitness value. There is no unique way of calculating the fitness value and it changes from one problem to another. -On ``15 April 2020``, a new argument named ``fitness_func`` is added to -PyGAD 1.0.17 that allows the user to specify a custom function to be -used as a fitness function. This function must be a maximization -function so that a solution with a high fitness value returned is -selected compared to a solution with a low value. Doing that allows the -user to freely use PyGAD to solve any problem by passing the appropriate -fitness function. It is very important to understand the problem well -for creating this function. +PyGAD has a parameter called ``fitness_func`` that allows the user to +specify a custom function/method to use when calculating the fitness. +This function/method must be a maximization function/method so that a +solution with a high fitness value returned is selected compared to a +solution with a low value. Doing that allows the user to freely use +PyGAD to solve any problem by passing the appropriate fitness +function/method. It is very important to understand the problem well for +creating it. Let's discuss an example: @@ -1123,34 +1124,40 @@ Let's discuss an example: So, the task is about using the genetic algorithm to find the best values for the 6 weight ``W1`` to ``W6``. Thinking of the problem, it is clear that the best solution is that returning an output that is close -to the desired output ``y=44``. So, the fitness function should return a -value that gets higher when the solution's output is closer to ``y=44``. -Here is a function that does that: +to the desired output ``y=44``. So, the fitness function/method should +return a value that gets higher when the solution's output is closer to +``y=44``. Here is a function that does that: .. code:: python function_inputs = [4, -2, 3.5, 5, -11, -4.7] # Function inputs. desired_output = 44 # Function output. - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution*function_inputs) fitness = 1.0 / numpy.abs(output - desired_output) return fitness -Such a user-defined function must accept 2 parameters: +Such a user-defined function must accept 3 parameters: -1. 1D vector representing a single solution. Introduced in `PyGAD - 1.0.17 `__. +1. The instance of the ``pygad.GA`` class. This helps the user to fetch + any property that helps when calculating the fitness. -2. Solution index within the population. Introduced in `PyGAD - 2.0.0 `__ - and higher. +2. The solution(s) to calculate the fitness value(s). Note that the + fitness function can accept multiple solutions only if the + ``fitness_batch_size`` is given a value greater than 1. + +3. The indices of the solutions in the population. The number of indices + also depends on the ``fitness_batch_size`` parameter. + +If a method is passed to the ``fitness_func`` parameter, then it accepts +a fourth parameter representing the method's instance. The ``__code__`` object is used to check if this function accepts the required number of parameters. If more or fewer parameters are passed, an exception is thrown. -By creating this function, you almost did an awesome step towards using +By creating this function, you did a very important step towards using PyGAD. Preparing Other Parameters @@ -1179,47 +1186,39 @@ Here is an example for preparing the other parameters: mutation_type = "random" mutation_percent_genes = 10 -.. _the-callbackgeneration-parameter: +.. _the-ongeneration-parameter: -The ``callback_generation`` Parameter -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +The ``on_generation`` Parameter +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -==This parameter should be replaced by ``on_generation``. The -``callback_generation`` parameter will be removed in a later release of -PyGAD.== - -In `PyGAD -2.0.0 `__ -and higher, an optional parameter named ``callback_generation`` is -supported which allows the user to call a function (with a single -parameter) after each generation. Here is a simple function that just -prints the current generation number and the fitness value of the best -solution in the current generation. The ``generations_completed`` -attribute of the GA class returns the number of the last completed -generation. +An optional parameter named ``on_generation`` is supported which allows +the user to call a function (with a single parameter) after each +generation. Here is a simple function that just prints the current +generation number and the fitness value of the best solution in the +current generation. The ``generations_completed`` attribute of the GA +class returns the number of the last completed generation. .. code:: python - def callback_gen(ga_instance): + def on_gen(ga_instance): print("Generation : ", ga_instance.generations_completed) print("Fitness of the best solution :", ga_instance.best_solution()[1]) -After being defined, the function is assigned to the -``callback_generation`` parameter of the GA class constructor. By doing -that, the ``callback_gen()`` function will be called after each -generation. +After being defined, the function is assigned to the ``on_generation`` +parameter of the GA class constructor. By doing that, the ``on_gen()`` +function will be called after each generation. .. code:: python ga_instance = pygad.GA(..., - callback_generation=callback_gen, + on_generation=on_gen, ...) After the parameters are prepared, we can import PyGAD and build an instance of the ``pygad.GA`` class. -Import the ``pygad`` --------------------- +Import ``pygad`` +---------------- The next step is to import PyGAD as follows: @@ -1427,7 +1426,7 @@ name. function_inputs = [4,-2,3.5,5,-11,-4.7] desired_output = 44 - def fitness_func(solution, solution_idx): + 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 @@ -1590,7 +1589,7 @@ values: 1. The first value is the mutation rate for the low-quality solutions. -2. The second value is the mutation rate for the low-quality solutions. +2. The second value is the mutation rate for the high-quality solutions. PyGAD expects that the first value is higher than the second value and thus a warning is printed in case the first value is lower than the @@ -1638,7 +1637,7 @@ Here is an example that uses adaptive mutation. function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs. desired_output = 44 # Function output. - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): # The fitness function calulates the sum of products between each input and its corresponding weight. output = numpy.sum(solution*function_inputs) # The value 0.000001 is used to avoid the Inf value when the denominator numpy.abs(output - desired_output) is 0.0. @@ -1743,10 +1742,10 @@ In `PyGAD 2.4.0 `__, it is possible to stop the genetic algorithm after any generation. All you need to do it to return the string ``"stop"`` in the callback -function ``callback_generation``. When this callback function is -implemented and assigned to the ``callback_generation`` parameter in the -constructor of the ``pygad.GA`` class, then the algorithm immediately -stops after completing its current generation. Let's discuss an example. +function ``on_generation``. When this callback function is implemented +and assigned to the ``on_generation`` parameter in the constructor of +the ``pygad.GA`` class, then the algorithm immediately stops after +completing its current generation. Let's discuss an example. Assume that the user wants to stop algorithm either after the 100 generations or if a condition is met. The user may assign a value of 100 @@ -1806,7 +1805,7 @@ reached ``127.4`` or if the fitness saturates for ``15`` generations. equation_inputs = [4, -2, 3.5, 8, 9, 4] desired_output = 44 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) @@ -1845,7 +1844,7 @@ each generation are kept in the next generation. function_inputs = [4,-2,3.5,5,-11,-4.7] desired_output = 44 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution*function_inputs) fitness = 1.0 / numpy.abs(output - desired_output) return fitness @@ -1923,7 +1922,7 @@ seed. function_inputs = [4,-2,3.5,5,-11,-4.7] desired_output = 44 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution*function_inputs) fitness = 1.0 / numpy.abs(output - desired_output) return fitness @@ -1982,7 +1981,7 @@ Now, the user can save the model by calling the ``save()`` method. import pygad - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): ... return fitness @@ -2003,7 +2002,7 @@ data. import pygad - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): ... return fitness @@ -2044,7 +2043,7 @@ population after each generation. import pygad - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): return 0 def on_generation(ga): @@ -2108,7 +2107,7 @@ has the same space of values that consists of 4 values (1, 2, 3, and 4). import pygad - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): return 0 def on_generation(ga): @@ -2213,7 +2212,7 @@ This is a sample code that does not use any custom function. equation_inputs = [4,-2,3.5] desired_output = 44 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness @@ -2458,7 +2457,7 @@ previous 3 user-defined functions instead of the built-in functions. equation_inputs = [4,-2,3.5] desired_output = 44 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) @@ -2516,6 +2515,75 @@ previous 3 user-defined functions instead of the built-in functions. ga_instance.run() ga_instance.plot_fitness() +This is the same example but using methods instead of functions. + +.. code:: python + + import pygad + import numpy + + equation_inputs = [4,-2,3.5] + desired_output = 44 + + class Test: + def fitness_func(self, ga_instance, solution, solution_idx): + output = numpy.sum(solution * equation_inputs) + + fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) + + return fitness + + def parent_selection_func(self, fitness, num_parents, ga_instance): + + fitness_sorted = sorted(range(len(fitness)), key=lambda k: fitness[k]) + fitness_sorted.reverse() + + parents = numpy.empty((num_parents, ga_instance.population.shape[1])) + + for parent_num in range(num_parents): + parents[parent_num, :] = ga_instance.population[fitness_sorted[parent_num], :].copy() + + return parents, numpy.array(fitness_sorted[:num_parents]) + + def crossover_func(self, parents, offspring_size, ga_instance): + + offspring = [] + idx = 0 + while len(offspring) != offspring_size[0]: + parent1 = parents[idx % parents.shape[0], :].copy() + parent2 = parents[(idx + 1) % parents.shape[0], :].copy() + + random_split_point = numpy.random.choice(range(offspring_size[0])) + + parent1[random_split_point:] = parent2[random_split_point:] + + offspring.append(parent1) + + idx += 1 + + return numpy.array(offspring) + + def mutation_func(self, offspring, ga_instance): + + for chromosome_idx in range(offspring.shape[0]): + random_gene_idx = numpy.random.choice(range(offspring.shape[1])) + + offspring[chromosome_idx, random_gene_idx] += numpy.random.random() + + return offspring + + ga_instance = pygad.GA(num_generations=10, + sol_per_pop=5, + num_parents_mating=2, + num_genes=len(equation_inputs), + fitness_func=Test().fitness_func, + parent_selection_type=Test().parent_selection_func, + crossover_type=Test().crossover_func, + mutation_type=Test().mutation_func) + + ga_instance.run() + ga_instance.plot_fitness() + .. _more-about-the-genespace-parameter: More about the ``gene_space`` Parameter @@ -2666,7 +2734,7 @@ the initial and final population are only integers. equation_inputs = [4, -2, 3.5, 8, -2] desired_output = 2671.1234 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness @@ -2707,12 +2775,12 @@ Data Type for All Genes with Precision A precision can only be specified for a ``float`` data type and cannot be specified for integers. Here is an example to use a precision of 3 -for the ``numpy.float`` data type. In this case, all genes are of type -``numpy.float`` and their maximum precision is 3. +for the ``float`` data type. In this case, all genes are of type +``float`` and their maximum precision is 3. .. code:: python - gene_type=[numpy.float, 3] + gene_type=[float, 3] The next code uses prints the initial and final population where the genes are of type ``float`` with precision 3. @@ -2725,7 +2793,7 @@ genes are of type ``float`` with precision 3. equation_inputs = [4, -2, 3.5, 8, -2] desired_output = 2671.1234 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) @@ -2777,7 +2845,7 @@ assigned to the genes. .. code:: python - gene_type=[int, float, numpy.float16, numpy.int8, numpy.float] + gene_type=[int, float, numpy.float16, numpy.int8, float] This is a complete code that prints the initial and final population for a custom-gene data type. @@ -2790,7 +2858,7 @@ a custom-gene data type. equation_inputs = [4, -2, 3.5, 8, -2] desired_output = 2671.1234 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness @@ -2800,7 +2868,7 @@ a custom-gene data type. num_parents_mating=2, num_genes=len(equation_inputs), fitness_func=fitness_func, - gene_type=[int, float, numpy.float16, numpy.int8, numpy.float]) + gene_type=[int, float, numpy.float16, numpy.int8, float]) print("Initial Population") print(ga_instance.initial_population) @@ -2835,7 +2903,7 @@ precision is 1. .. code:: python - gene_type=[int, [float, 2], numpy.float16, numpy.int8, [numpy.float, 1]] + gene_type=[int, [float, 2], numpy.float16, numpy.int8, [float, 1]] This is a complete example where the initial and final populations are printed where the genes comply with the data types and precisions @@ -2849,7 +2917,7 @@ specified. equation_inputs = [4, -2, 3.5, 8, -2] desired_output = 2671.1234 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness @@ -2859,7 +2927,7 @@ specified. num_parents_mating=2, num_genes=len(equation_inputs), fitness_func=fitness_func, - gene_type=[int, [float, 2], numpy.float16, numpy.int8, [numpy.float, 1]]) + gene_type=[int, [float, 2], numpy.float16, numpy.int8, [float, 1]]) print("Initial Population") print(ga_instance.initial_population) @@ -2909,7 +2977,7 @@ code runs for only 10 generations. equation_inputs = [4, -2, 3.5, 8, -2, 3.5, 8] desired_output = 2671.1234 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness @@ -3346,7 +3414,7 @@ means no parallel processing is used. import pygad import time - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): for _ in range(99): pass return 0 @@ -3414,7 +3482,7 @@ generations are used. With no parallelization, it takes 22 seconds. import pygad import time - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): for _ in range(99999999): pass return 0 @@ -3609,6 +3677,296 @@ parameters. ---------------------------------------------------------------------- ====================================================================== +Logging Outputs +=============== + +In `PyGAD +3.0.0 `__, +the ``print()`` statement is no longer used and the outputs are printed +using the `logging `__ +module. A a new parameter called ``logger`` is supported to accept the +user-defined logger. + +.. code:: python + + import logging + + logger = ... + + ga_instance = pygad.GA(..., + logger=logger, + ...) + +The default value for this parameter is ``None``. If there is no logger +passed (i.e. ``logger=None``), then a default logger is created to log +the messages to the console exactly like how the ``print()`` statement +works. + +Some advantages of using the the +`logging `__ module +instead of the ``print()`` statement are: + +1. The user has more control over the printed messages specially if + there is a project that uses multiple modules where each module + prints its messages. A logger can organize the outputs. + +2. Using the proper ``Handler``, the user can log the output messages to + files and not only restricted to printing it to the console. So, it + is much easier to record the outputs. + +3. The format of the printed messages can be changed by customizing the + ``Formatter`` assigned to the Logger. + +This section gives some quick examples to use the ``logging`` module and +then gives an example to use the logger with PyGAD. + +Logging to the Console +---------------------- + +This is an example to create a logger to log the messages to the +console. + +.. code:: python + + import logging + + # Create a logger + logger = logging.getLogger(__name__) + + # Set the logger level to debug so that all the messages are printed. + logger.setLevel(logging.DEBUG) + + # Create a stream handler to log the messages to the console. + stream_handler = logging.StreamHandler() + + # Set the handler level to debug. + stream_handler.setLevel(logging.DEBUG) + + # Create a formatter + formatter = logging.Formatter('%(message)s') + + # Add the formatter to handler. + stream_handler.setFormatter(formatter) + + # Add the stream handler to the logger + logger.addHandler(stream_handler) + +Now, we can log messages to the console with the format specified in the +``Formatter``. + +.. code:: python + + logger.debug('Debug message.') + logger.info('Info message.') + logger.warning('Warn message.') + logger.error('Error message.') + logger.critical('Critical message.') + +The outputs are identical to those returned using the ``print()`` +statement. + +.. code:: + + Debug message. + Info message. + Warn message. + Error message. + Critical message. + +By changing the format of the output messages, we can have more +information about each message. + +.. code:: python + + formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') + +This is a sample output. + +.. code:: python + + 2023-04-03 18:46:27 DEBUG: Debug message. + 2023-04-03 18:46:27 INFO: Info message. + 2023-04-03 18:46:27 WARNING: Warn message. + 2023-04-03 18:46:27 ERROR: Error message. + 2023-04-03 18:46:27 CRITICAL: Critical message. + +Note that you may need to clear the handlers after finishing the +execution. This is to make sure no cached handlers are used in the next +run. If the cached handlers are not cleared, then the single output +message may be repeated. + +.. code:: python + + logger.handlers.clear() + +Logging to a File +----------------- + +This is another example to log the messages to a file named +``logfile.txt``. The formatter prints the following about each message: + +1. The date and time at which the message is logged. + +2. The log level. + +3. The message. + +4. The path of the file. + +5. The lone number of the log message. + +.. code:: python + + import logging + + level = logging.DEBUG + name = 'logfile.txt' + + logger = logging.getLogger(name) + logger.setLevel(level) + + file_handler = logging.FileHandler(name, 'a+', 'utf-8') + file_handler.setLevel(logging.DEBUG) + file_format = logging.Formatter('%(asctime)s %(levelname)s: %(message)s - %(pathname)s:%(lineno)d', datefmt='%Y-%m-%d %H:%M:%S') + file_handler.setFormatter(file_format) + logger.addHandler(file_handler) + +This is how the outputs look like. + +.. code:: python + + 2023-04-03 18:54:03 DEBUG: Debug message. - c:\users\agad069\desktop\logger\example2.py:46 + 2023-04-03 18:54:03 INFO: Info message. - c:\users\agad069\desktop\logger\example2.py:47 + 2023-04-03 18:54:03 WARNING: Warn message. - c:\users\agad069\desktop\logger\example2.py:48 + 2023-04-03 18:54:03 ERROR: Error message. - c:\users\agad069\desktop\logger\example2.py:49 + 2023-04-03 18:54:03 CRITICAL: Critical message. - c:\users\agad069\desktop\logger\example2.py:50 + +Consider clearing the handlers if necessary. + +.. code:: python + + logger.handlers.clear() + +Log to Both the Console and a File +---------------------------------- + +This is an example to create a single Logger associated with 2 handlers: + +1. A file handler. + +2. A stream handler. + +.. code:: python + + import logging + + level = logging.DEBUG + name = 'logfile.txt' + + logger = logging.getLogger(name) + logger.setLevel(level) + + file_handler = logging.FileHandler(name,'a+','utf-8') + file_handler.setLevel(logging.DEBUG) + file_format = logging.Formatter('%(asctime)s %(levelname)s: %(message)s - %(pathname)s:%(lineno)d', datefmt='%Y-%m-%d %H:%M:%S') + file_handler.setFormatter(file_format) + logger.addHandler(file_handler) + + console_handler = logging.StreamHandler() + console_handler.setLevel(logging.INFO) + console_format = logging.Formatter('%(message)s') + console_handler.setFormatter(console_format) + logger.addHandler(console_handler) + +When a log message is executed, then it is both printed to the console +and saved in the ``logfile.txt``. + +Consider clearing the handlers if necessary. + +.. code:: python + + logger.handlers.clear() + +PyGAD Example +------------- + +To use the logger in PyGAD, just create your custom logger and pass it +to the ``logger`` parameter. + +.. code:: python + + import logging + import pygad + import numpy + + level = logging.DEBUG + name = 'logfile.txt' + + logger = logging.getLogger(name) + logger.setLevel(level) + + file_handler = logging.FileHandler(name,'a+','utf-8') + file_handler.setLevel(logging.DEBUG) + file_format = logging.Formatter('%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') + file_handler.setFormatter(file_format) + logger.addHandler(file_handler) + + console_handler = logging.StreamHandler() + console_handler.setLevel(logging.INFO) + console_format = logging.Formatter('%(message)s') + console_handler.setFormatter(console_format) + logger.addHandler(console_handler) + + equation_inputs = [4, -2, 8] + desired_output = 2671.1234 + + def fitness_func(ga_instance, solution, solution_idx): + output = numpy.sum(solution * equation_inputs) + fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) + return fitness + + def on_generation(ga_instance): + ga_instance.logger.info("Generation = {generation}".format(generation=ga_instance.generations_completed)) + ga_instance.logger.info("Fitness = {fitness}".format(fitness=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1])) + + ga_instance = pygad.GA(num_generations=10, + sol_per_pop=40, + num_parents_mating=2, + keep_parents=2, + num_genes=len(equation_inputs), + fitness_func=fitness_func, + on_generation=on_generation, + logger=logger) + ga_instance.run() + + logger.handlers.clear() + +By executing this code, the logged messages are printed to the console +and also saved in the text file. + +.. code:: python + + 2023-04-03 19:04:27 INFO: Generation = 1 + 2023-04-03 19:04:27 INFO: Fitness = 0.00038086960368076276 + 2023-04-03 19:04:27 INFO: Generation = 2 + 2023-04-03 19:04:27 INFO: Fitness = 0.00038214871408010853 + 2023-04-03 19:04:27 INFO: Generation = 3 + 2023-04-03 19:04:27 INFO: Fitness = 0.0003832795907974678 + 2023-04-03 19:04:27 INFO: Generation = 4 + 2023-04-03 19:04:27 INFO: Fitness = 0.00038398612055017196 + 2023-04-03 19:04:27 INFO: Generation = 5 + 2023-04-03 19:04:27 INFO: Fitness = 0.00038442348890867516 + 2023-04-03 19:04:27 INFO: Generation = 6 + 2023-04-03 19:04:27 INFO: Fitness = 0.0003854406039137763 + 2023-04-03 19:04:27 INFO: Generation = 7 + 2023-04-03 19:04:27 INFO: Fitness = 0.00038646083174063284 + 2023-04-03 19:04:27 INFO: Generation = 8 + 2023-04-03 19:04:27 INFO: Fitness = 0.0003875169193024936 + 2023-04-03 19:04:27 INFO: Generation = 9 + 2023-04-03 19:04:27 INFO: Fitness = 0.0003888816727311021 + 2023-04-03 19:04:27 INFO: Generation = 10 + 2023-04-03 19:04:27 INFO: Fitness = 0.000389832593101348 + Batch Fitness Calculation ========================= @@ -3677,7 +4035,7 @@ the fitness function for each individual solution. number_of_calls = 0 - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): global number_of_calls number_of_calls = number_of_calls + 1 output = numpy.sum(solution*function_inputs) @@ -3742,7 +4100,7 @@ then the total number of calls is ``5*5 + 5 = 30``. number_of_calls = 0 - def fitness_func_batch(solutions, solutions_indices): + def fitness_func_batch(ga_instance, solutions, solutions_indices): global number_of_calls number_of_calls = number_of_calls + 1 batch_fitness = [] @@ -3771,6 +4129,140 @@ then the total number of calls is ``5*5 + 5 = 30``. When batch fitness calculation is used, then we saved ``120 - 30 = 90`` calls to the fitness function. +Use Functions and Methods to Build Fitness and Callbacks +======================================================== + +In PyGAD 2.19.0, it is possible to pass user-defined functions or +methods to the following parameters: + +1. ``fitness_func`` + +2. ``on_start`` + +3. ``on_fitness`` + +4. ``on_parents`` + +5. ``on_crossover`` + +6. ``on_mutation`` + +7. ``on_generation`` + +8. ``on_stop`` + +This section gives 2 examples to assign these parameters user-defined: + +1. Functions. + +2. Methods. + +Assign Functions +---------------- + +This is a dummy example where the fitness function returns a random +value. Note that the instance of the ``pygad.GA`` class is passed as the +last parameter of all functions. + +.. code:: python + + import pygad + import numpy + + def fitness_func(ga_instanse, solution, solution_idx): + return numpy.random.rand() + + def on_start(ga_instanse): + print("on_start") + + def on_fitness(ga_instanse, last_gen_fitness): + print("on_fitness") + + def on_parents(ga_instanse, last_gen_parents): + print("on_parents") + + def on_crossover(ga_instanse, last_gen_offspring): + print("on_crossover") + + def on_mutation(ga_instanse, last_gen_offspring): + print("on_mutation") + + def on_generation(ga_instanse): + print("on_generation\n") + + def on_stop(ga_instanse, last_gen_fitness): + print("on_stop") + + ga_instance = pygad.GA(num_generations=5, + num_parents_mating=4, + sol_per_pop=10, + num_genes=2, + 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, + fitness_func=fitness_func) + + ga_instance.run() + +Assign Methods +-------------- + +The next example has all the method defined inside the class ``Test``. +All of the methods accept an additional parameter representing the +method's object of the class ``Test``. + +All methods accept ``self`` as the first parameter and the instance of +the ``pygad.GA`` class as the last parameter. + +.. code:: python + + import pygad + import numpy + + class Test: + def fitness_func(self, ga_instanse, solution, solution_idx): + return numpy.random.rand() + + def on_start(self, ga_instanse): + print("on_start") + + def on_fitness(self, ga_instanse, last_gen_fitness): + print("on_fitness") + + def on_parents(self, ga_instanse, last_gen_parents): + print("on_parents") + + def on_crossover(self, ga_instanse, last_gen_offspring): + print("on_crossover") + + def on_mutation(self, ga_instanse, last_gen_offspring): + print("on_mutation") + + def on_generation(self, ga_instanse): + print("on_generation\n") + + def on_stop(self, ga_instanse, last_gen_fitness): + print("on_stop") + + ga_instance = pygad.GA(num_generations=5, + num_parents_mating=4, + sol_per_pop=10, + num_genes=2, + on_start=Test().on_start, + on_fitness=Test().on_fitness, + on_parents=Test().on_parents, + on_crossover=Test().on_crossover, + on_mutation=Test().on_mutation, + on_generation=Test().on_generation, + on_stop=Test().on_stop, + fitness_func=Test().fitness_func) + + ga_instance.run() + .. _examples-2: Examples @@ -3802,7 +4294,7 @@ below. function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs. desired_output = 44 # Function output. - def fitness_func(solution, solution_idx): + 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 @@ -3905,7 +4397,7 @@ to the next code. import numpy target_im = imageio.imread('fruit.jpg') - target_im = numpy.asarray(target_im/255, dtype=numpy.float) + target_im = numpy.asarray(target_im/255, dtype=float) Here is the read image. @@ -3925,9 +4417,9 @@ Prepare the Fitness Function The next code creates a function that will be used as a fitness function for calculating the fitness value for each solution in the population. -This function must be a maximization function that accepts 2 parameters -representing a solution and its index. It returns a value representing -the fitness value. +This function must be a maximization function that accepts 3 parameters +representing the instance of the ``pygad.GA`` class, a solution, and its +index. It returns a value representing the fitness value. .. code:: python @@ -3935,7 +4427,7 @@ the fitness value. target_chromosome = gari.img2chromosome(target_im) - def fitness_fun(solution, solution_idx): + def fitness_fun(ga_instance, solution, solution_idx): fitness = numpy.sum(numpy.abs(target_chromosome-solution)) # Negating the fitness value to make it increasing rather than decreasing. diff --git a/docs/source/README_pygad_gacnn_ReadTheDocs.rst b/docs/source/README_pygad_gacnn_ReadTheDocs.rst index ea3282d..39bbc6e 100644 --- a/docs/source/README_pygad_gacnn_ReadTheDocs.rst +++ b/docs/source/README_pygad_gacnn_ReadTheDocs.rst @@ -95,7 +95,7 @@ This section discusses the functions in the ``pygad.gacnn`` module. .. _pygadgacnnpopulationasvectors: ``pygad.gacnn.population_as_vectors()`` ---------------------------------------- +---------------------------------------- Accepts the population as a list of references to the ``pygad.cnn.Model`` class and returns a list holding all weights of the @@ -356,7 +356,7 @@ fitness value is returned. .. code:: python - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global GACNN_instance, data_inputs, data_outputs predictions = GACNN_instance.population_networks[sol_idx].predict(data_inputs=data_inputs) @@ -558,7 +558,7 @@ complete code is listed below. It is also translated into Chinese: http://m.aliyun.com/yunqi/articles/585741 """ - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global GACNN_instance, data_inputs, data_outputs predictions = GACNN_instance.population_networks[sol_idx].predict(data_inputs=data_inputs) diff --git a/docs/source/README_pygad_gann_ReadTheDocs.rst b/docs/source/README_pygad_gann_ReadTheDocs.rst index fe95b1e..ac95e61 100644 --- a/docs/source/README_pygad_gann_ReadTheDocs.rst +++ b/docs/source/README_pygad_gann_ReadTheDocs.rst @@ -408,7 +408,7 @@ fitness value is returned. .. code:: python - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global GANN_instance, data_inputs, data_outputs predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx], @@ -646,7 +646,7 @@ its complete code is listed below. import pygad.nn import pygad.gann - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global GANN_instance, data_inputs, data_outputs predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx], @@ -803,7 +803,7 @@ according to the ``num_neurons_output`` parameter of the import pygad.nn import pygad.gann - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global GANN_instance, data_inputs, data_outputs predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx], @@ -960,7 +960,7 @@ To train a neural network for regression, follow these instructions: .. code:: python - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): ... predictions = pygad.nn.predict(..., @@ -980,7 +980,7 @@ for regression. import pygad.nn import pygad.gann - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global GANN_instance, data_inputs, data_outputs predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx], @@ -1146,7 +1146,7 @@ Here is the complete code. import pygad.gann import pandas - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global GANN_instance, data_inputs, data_outputs predictions = pygad.nn.predict(last_layer=GANN_instance.population_networks[sol_idx], diff --git a/docs/source/README_pygad_kerasga_ReadTheDocs.rst b/docs/source/README_pygad_kerasga_ReadTheDocs.rst index 0ae8db2..9b82467 100644 --- a/docs/source/README_pygad_kerasga_ReadTheDocs.rst +++ b/docs/source/README_pygad_kerasga_ReadTheDocs.rst @@ -158,7 +158,7 @@ This section discusses the functions in the ``pygad.kerasga`` module. .. _pygadkerasgamodelweightsasvector: ``pygad.kerasga.model_weights_as_vector()`` -------------------------------------------- +-------------------------------------------- The ``model_weights_as_vector()`` function accepts a single parameter named ``model`` representing the Keras model. It returns a vector @@ -228,7 +228,7 @@ subsections discuss each part in the code. import numpy import pygad - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, keras_ga, model predictions = pygad.kerasga.predict(model=model, @@ -383,7 +383,7 @@ Feel free to use any other loss function to calculate the fitness value. .. code:: python - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, keras_ga, model predictions = pygad.kerasga.predict(model=model, @@ -504,7 +504,7 @@ previous example. import numpy import pygad - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, keras_ga, model predictions = pygad.kerasga.predict(model=model, @@ -657,7 +657,7 @@ Here is the code. import numpy import pygad - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, keras_ga, model predictions = pygad.kerasga.predict(model=model, @@ -804,7 +804,7 @@ Here is the complete code. import numpy import pygad - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, keras_ga, model predictions = pygad.kerasga.predict(model=model, diff --git a/docs/source/README_pygad_torchga_ReadTheDocs.rst b/docs/source/README_pygad_torchga_ReadTheDocs.rst index fd032a1..66a554e 100644 --- a/docs/source/README_pygad_torchga_ReadTheDocs.rst +++ b/docs/source/README_pygad_torchga_ReadTheDocs.rst @@ -132,7 +132,7 @@ This section discusses the functions in the ``pygad.torchga`` module. .. _pygadtorchgamodelweightsasvector: ``pygad.torchga.model_weights_as_vector()`` -------------------------------------------- +-------------------------------------------- The ``model_weights_as_vector()`` function accepts a single parameter named ``model`` representing the PyTorch model. It returns a vector @@ -198,7 +198,7 @@ subsections discuss each part in the code. import torchga import pygad - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, torch_ga, model, loss_function predictions = pygad.torchga.predict(model=model, @@ -349,7 +349,7 @@ other loss function to calculate the fitness value. loss_function = torch.nn.L1Loss() - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, torch_ga, model, loss_function predictions = pygad.torchga.predict(model=model, @@ -468,7 +468,7 @@ previous example. import torchga import pygad - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, torch_ga, model, loss_function predictions = pygad.torchga.predict(model=model, @@ -629,7 +629,7 @@ Here is the code. import pygad import numpy - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, torch_ga, model, loss_function predictions = pygad.torchga.predict(model=model, @@ -774,7 +774,7 @@ Here is the complete code. import pygad import numpy - def fitness_func(solution, sol_idx): + def fitness_func(ga_instance, solution, sol_idx): global data_inputs, data_outputs, torch_ga, model, loss_function predictions = pygad.torchga.predict(model=model, diff --git a/docs/source/conf.py b/docs/source/conf.py index 352ad80..0799f5e 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -22,7 +22,7 @@ author = 'Ahmed Fawzy Gad' # The full version, including alpha/beta/rc tags -release = '2.19.2' +release = '3.0.0' master_doc = 'index' diff --git a/docs/source/index.rst b/docs/source/index.rst index 2628ffc..b346229 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -43,6 +43,9 @@ Donation & Support You can donate to PyGAD via: +- `Credit/Debit Card `__: + https://donate.stripe.com/eVa5kO866elKgM0144 + - `Open Collective `__: `opencollective.com/pygad `__ @@ -107,7 +110,7 @@ used for calculating the fitness value for each solution. Here is one. .. code:: python - def fitness_func(solution, solution_idx): + def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution*function_inputs) fitness = 1.0 / numpy.abs(output - desired_output) return fitness @@ -188,26 +191,33 @@ PyGAD's Modules `PyGAD `__ has the following modules: -1. The main module has the same name as the library which is ``pygad`` - that builds the genetic algorithm. +1. The main module has the same name as the library ``pygad`` which is + the main interface to build the genetic algorithm. + +2. The ``nn`` module builds artificial neural networks. + +3. The ``gann`` module optimizes neural networks (for classification + and regression) using the genetic algorithm. + +4. The ``cnn`` module builds convolutional neural networks. -2. The ``nn`` module builds artificial neural networks. +5. The ``gacnn`` module optimizes convolutional neural networks using + the genetic algorithm. -3. The ``gann`` module optimizes neural networks (for classification and - regression) using the genetic algorithm. +6. The ``kerasga`` module to train `Keras `__ models + using the genetic algorithm. -4. The ``cnn`` module builds convolutional neural networks. +7. The ``torchga`` module to train `PyTorch `__ + models using the genetic algorithm. -5. The ``gacnn`` module optimizes convolutional neural networks using - the genetic algorithm. +8. The ``visualize`` module to visualize the results. -6. The ``kerasga`` module to train `Keras `__ models - using the genetic algorithm. +9. The ``utils`` module contains the operators (crossover, mutation, + and parent selection). -7. The ``torchga`` module to train `PyTorch `__ - models using the genetic algorithm. +10. The ``helper`` module has some helper functions. -The documentation discusses each of these modules. +The documentation discusses these modules. PyGAD Citation - Bibtex Formatted ================================= diff --git a/example.py b/example.py index 3ecd8e6..d73dac1 100644 --- a/example.py +++ b/example.py @@ -11,7 +11,7 @@ function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs. desired_output = 44 # Function output. -def fitness_func(solution, solution_idx): +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 diff --git a/example_clustering_2.py b/example_clustering_2.py index c30e0c9..43c99df 100644 --- a/example_clustering_2.py +++ b/example_clustering_2.py @@ -83,7 +83,7 @@ def cluster_data(solution, solution_idx): return cluster_centers, all_clusters_dists, cluster_indices, clusters, clusters_sum_dist -def fitness_func(solution, solution_idx): +def fitness_func(ga_instance, solution, solution_idx): _, _, _, _, clusters_sum_dist = cluster_data(solution, solution_idx) # The tiny value 0.00000001 is added to the denominator in case the average distance is 0. @@ -119,4 +119,4 @@ def fitness_func(solution, solution_idx): matplotlib.pyplot.scatter(cluster_x, cluster_y) matplotlib.pyplot.scatter(cluster_centers[cluster_idx, 0], cluster_centers[cluster_idx, 1], linewidths=5) matplotlib.pyplot.title("Clustering using PyGAD") -matplotlib.pyplot.show() +matplotlib.pyplot.show() \ No newline at end of file diff --git a/example_clustering_3.py b/example_clustering_3.py index bfec5ef..08e3dd7 100644 --- a/example_clustering_3.py +++ b/example_clustering_3.py @@ -94,7 +94,7 @@ def cluster_data(solution, solution_idx): return cluster_centers, all_clusters_dists, cluster_indices, clusters, clusters_sum_dist -def fitness_func(solution, solution_idx): +def fitness_func(ga_instance, solution, solution_idx): _, _, _, _, clusters_sum_dist = cluster_data(solution, solution_idx) # The tiny value 0.00000001 is added to the denominator in case the average distance is 0. diff --git a/example_custom_operators.py b/example_custom_operators.py index c01a1da..0acd5a5 100644 --- a/example_custom_operators.py +++ b/example_custom_operators.py @@ -13,7 +13,7 @@ equation_inputs = [4,-2,3.5] desired_output = 44 -def fitness_func(solution, solution_idx): +def fitness_func(ga_instance, solution, solution_idx): output = numpy.sum(solution * equation_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) @@ -31,7 +31,7 @@ def parent_selection_func(fitness, num_parents, ga_instance): for parent_num in range(num_parents): parents[parent_num, :] = ga_instance.population[fitness_sorted[parent_num], :].copy() - return parents, fitness_sorted[:num_parents] + return parents, numpy.array(fitness_sorted[:num_parents]) def crossover_func(parents, offspring_size, ga_instance): # This is single-point crossover. @@ -71,4 +71,4 @@ def mutation_func(offspring, ga_instance): mutation_type=mutation_func) ga_instance.run() -ga_instance.plot_fitness() +ga_instance.plot_fitness() \ No newline at end of file diff --git a/example_logger.py b/example_logger.py new file mode 100644 index 0000000..d38a179 --- /dev/null +++ b/example_logger.py @@ -0,0 +1,45 @@ +import logging +import pygad +import numpy + +level = logging.DEBUG +name = 'logfile.txt' + +logger = logging.getLogger(name) +logger.setLevel(level) + +file_handler = logging.FileHandler(name,'a+','utf-8') +file_handler.setLevel(logging.DEBUG) +file_format = logging.Formatter('%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') +file_handler.setFormatter(file_format) +logger.addHandler(file_handler) + +console_handler = logging.StreamHandler() +console_handler.setLevel(logging.INFO) +console_format = logging.Formatter('%(message)s') +console_handler.setFormatter(console_format) +logger.addHandler(console_handler) + +equation_inputs = [4, -2, 8] +desired_output = 2671.1234 + +def fitness_func(ga_instance, solution, solution_idx): + output = numpy.sum(solution * equation_inputs) + fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) + return fitness + +def on_generation(ga_instance): + ga_instance.logger.info("Generation = {generation}".format(generation=ga_instance.generations_completed)) + ga_instance.logger.info("Fitness = {fitness}".format(fitness=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1])) + +ga_instance = pygad.GA(num_generations=10, + sol_per_pop=40, + num_parents_mating=2, + keep_parents=2, + num_genes=len(equation_inputs), + fitness_func=fitness_func, + on_generation=on_generation, + logger=logger) +ga_instance.run() + +logger.handlers.clear() diff --git a/lifecycle.py b/lifecycle.py index 9c5c078..67ba128 100644 --- a/lifecycle.py +++ b/lifecycle.py @@ -4,7 +4,7 @@ function_inputs = [4,-2,3.5,5,-11,-4.7] desired_output = 44 -def fitness_func(solution, solution_idx): +def fitness_func(ga_instanse, solution, solution_idx): output = numpy.sum(solution*function_inputs) fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness diff --git a/pygad.py b/pygad.py index 83ca630..9916a15 100644 --- a/pygad.py +++ b/pygad.py @@ -1,13 +1,20 @@ import numpy import random -import matplotlib.pyplot import cloudpickle import time import warnings import concurrent.futures import inspect +import logging +from pygad import utils +from pygad import helper +from pygad import visualize -class GA: +class GA(utils.parent_selection.ParentSelection, + utils.crossover.Crossover, + utils.mutation.Mutation, + helper.unique.Unique, + visualize.plot.Plot): supported_int_types = [int, numpy.int8, numpy.int16, numpy.int32, numpy.int64, numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64] supported_float_types = [float, numpy.float16, numpy.float32, numpy.float64] @@ -44,7 +51,6 @@ def __init__(self, on_parents=None, on_crossover=None, on_mutation=None, - callback_generation=None, on_generation=None, on_stop=None, delay_after_gen=0.0, @@ -53,7 +59,8 @@ def __init__(self, suppress_warnings=False, stop_criteria=None, parallel_processing=None, - random_seed=None): + random_seed=None, + logger=None): """ The constructor of the GA class accepts all parameters required to create an instance of the GA class. It validates such parameters. @@ -61,7 +68,7 @@ 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 that must accept 2 parameters (a single solution and its index in the population) and return the fitness value of the solution. Available starting from PyGAD 1.0.17 until 1.0.20 with a single parameter representing the solution. Changed in PyGAD 2.0.0 and higher to include the second parameter representing the solution index. + 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_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 invidiaul 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. @@ -72,7 +79,7 @@ def __init__(self, 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. # It is OK to set the value of any of the 2 parameters ('init_range_low' and 'init_range_high') to be equal, higher or lower than the other parameter (i.e. init_range_low is not needed to be lower than init_range_high). - gene_type: The type of the gene. It is assigned to any of these types (int, float, numpy.int, numpy.int8, numpy.int16, numpy.int32, numpy.int64, numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, numpy.float, numpy.float16, numpy.float32, numpy.float64) and forces all the genes to be of that type. + gene_type: The type of the gene. It is assigned to any of these types (int, float, numpy.int8, numpy.int16, numpy.int32, numpy.int64, numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, 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 Barrionuevo (http://webs.um.es/fernan) for editing this sentence. @@ -95,14 +102,13 @@ def __init__(self, gene_space: It accepts a list of all possible values of the gene. This list is used in the mutation step. Should be used only if the gene space is a set of discrete values. No need for the 2 parameters (random_mutation_min_val and random_mutation_max_val) if the parameter gene_space exists. Added in PyGAD 2.5.0. In PyGAD 2.11.0, the gene_space can be assigned a dict. - on_start: Accepts a function to be called only once before the genetic algorithm starts its evolution. This function must accept a single parameter representing the instance of the genetic algorithm. Added in PyGAD 2.6.0. - on_fitness: Accepts a function to be called after calculating the fitness values of all solutions in the population. This function must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one is a list of all solutions' fitness values. Added in PyGAD 2.6.0. - on_parents: Accepts a function to be called after selecting the parents that mates. This function must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one represents the selected parents. Added in PyGAD 2.6.0. - on_crossover: Accepts a function to be called each time the crossover operation is applied. This function must accept 2 parameters: the first one represents the instance of the genetic algorithm and the second one represents the offspring generated using crossover. Added in PyGAD 2.6.0. - on_mutation: Accepts a function to be called each time the mutation operation is applied. This function 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. Added in PyGAD 2.6.0. - callback_generation: Accepts a function to be called after each generation. This function 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. Starting from PyGAD 2.6.0, the callback_generation parameter is deprecated and should be replaced by the on_generation parameter. - on_generation: Accepts a function to be called after each generation. This function 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. Added in PyGAD 2.6.0. - on_stop: Accepts a function to be called only once exactly before the genetic algorithm stops or when it completes all the generations. This function 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. 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 mates. 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. delay_after_gen: Added in PyGAD 2.4.0. It accepts a non-negative number specifying the number of seconds to wait after a generation completes and before going to the next generation. It defaults to 0.0 which means no delay after the generation. @@ -116,10 +122,48 @@ def __init__(self, stop_criteria: Added in PyGAD 2.15.0. It is assigned to some criteria to stop the evolution if at least one criterion holds. parallel_processing: Added in PyGAD 2.17.0. Defaults to `None` which means no parallel processing is used. If a positive integer is assigned, it specifies the number of threads to be used. If a list or a tuple of exactly 2 elements is assigned, then: 1) The first element can be either "process" or "thread" to specify whether processes or threads are used, respectively. 2) The second element can be: 1) A positive integer to select the maximum number of processes or threads to be used. 2) 0 to indicate that parallel processing is not used. This is identical to setting 'parallel_processing=None'. 3) None to use the default value as calculated by the concurrent.futures module. - + random_seed: Added in PyGAD 2.18.0. It defines the random seed to be used by the random function generators (we use random functions in the NumPy and random modules). This helps to reproduce the same results by setting the same random seed. + + 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. """ + # 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("The expected type of the 'logger' parameter is 'logging.Logger' but {logger_type} found.".format(logger_type=type(logger))) + + # 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 @@ -132,12 +176,14 @@ def __init__(self, self.suppress_warnings = suppress_warnings else: self.valid_parameters = False + self.logger.error("The expected type of the 'suppress_warnings' parameter is bool but {suppress_warnings_type} found.".format(suppress_warnings_type=type(suppress_warnings))) raise TypeError("The expected type of the 'suppress_warnings' parameter is bool but {suppress_warnings_type} found.".format(suppress_warnings_type=type(suppress_warnings))) # Validating mutation_by_replacement if not (type(mutation_by_replacement) is bool): self.valid_parameters = False - raise TypeError("The expected type of the 'mutation_by_replacement' parameter is bool but ({mutation_by_replacement_type}) found.".format(mutation_by_replacement_type=type(mutation_by_replacement))) + self.logger.error("The expected type of the 'mutation_by_replacement' parameter is bool but {mutation_by_replacement_type} found.".format(mutation_by_replacement_type=type(mutation_by_replacement))) + raise TypeError("The expected type of the 'mutation_by_replacement' parameter is bool but {mutation_by_replacement_type} found.".format(mutation_by_replacement_type=type(mutation_by_replacement))) self.mutation_by_replacement = mutation_by_replacement @@ -148,16 +194,19 @@ def __init__(self, elif type(gene_space) in [list, tuple, range, numpy.ndarray]: if len(gene_space) == 0: self.valid_parameters = False + self.logger.error("'gene_space' cannot be empty (i.e. its length must be >= 0).") 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 [list, tuple, range, numpy.ndarray]: if len(el) == 0: self.valid_parameters = False + self.logger.error("The element indexed {index} of 'gene_space' with type {el_type} cannot be empty (i.e. its length must be >= 0).".format(index=index, el_type=type(el))) raise ValueError("The element indexed {index} of 'gene_space' with type {el_type} cannot be empty (i.e. its length must be >= 0).".format(index=index, el_type=type(el))) else: for val in el: if not (type(val) in [type(None)] + GA.supported_int_float_types): + self.logger.error("All values in the sublists inside the 'gene_space' attribute must be numeric of type int/float/None but ({val}) of type {typ} found.".format(val=val, typ=type(val))) raise TypeError("All values in the sublists inside the 'gene_space' attribute must be numeric of type int/float/None but ({val}) of type {typ} found.".format(val=val, typ=type(val))) self.gene_space_nested = True elif type(el) == type(None): @@ -169,19 +218,23 @@ def __init__(self, pass else: self.valid_parameters = False + self.logger.error("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: {gene_space_dict_keys}".format(gene_space_dict_keys=el.keys())) raise ValueError("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: {gene_space_dict_keys}".format(gene_space_dict_keys=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 + self.logger.error("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: {gene_space_dict_keys}".format(gene_space_dict_keys=el.keys())) raise ValueError("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: {gene_space_dict_keys}".format(gene_space_dict_keys=el.keys())) else: self.valid_parameters = False + self.logger.error("When an element in the 'gene_space' parameter is of type dict, then it must have only 2 items but ({num_items}) items found.".format(num_items=len(el.items()))) raise ValueError("When an element in the 'gene_space' parameter is of type dict, then it must have only 2 items but ({num_items}) items found.".format(num_items=len(el.items()))) self.gene_space_nested = True elif not (type(el) in GA.supported_int_float_types): self.valid_parameters = False + self.logger.error("Unexpected type {el_type} 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.".format(index=index, el_type=type(el))) raise TypeError("Unexpected type {el_type} 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.".format(index=index, el_type=type(el))) elif type(gene_space) is dict: @@ -190,21 +243,25 @@ def __init__(self, pass else: self.valid_parameters = False + self.logger.error("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_dict_keys}".format(gene_space_dict_keys=gene_space.keys())) raise ValueError("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_dict_keys}".format(gene_space_dict_keys=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 + self.logger.error("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_dict_keys}".format(gene_space_dict_keys=gene_space.keys())) raise ValueError("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_dict_keys}".format(gene_space_dict_keys=gene_space.keys())) else: self.valid_parameters = False + self.logger.error("When the 'gene_space' parameter is of type dict, then it must have only 2 items but ({num_items}) items found.".format(num_items=len(gene_space.items()))) raise ValueError("When the 'gene_space' parameter is of type dict, then it must have only 2 items but ({num_items}) items found.".format(num_items=len(gene_space.items()))) else: self.valid_parameters = False - raise TypeError("The expected type of 'gene_space' is list, tuple, range, or numpy.ndarray but ({gene_space_type}) found.".format(gene_space_type=type(gene_space))) - + self.logger.error("The expected type of 'gene_space' is list, tuple, range, or numpy.ndarray but {gene_space_type} found.".format(gene_space_type=type(gene_space))) + raise TypeError("The expected type of 'gene_space' is list, tuple, range, or numpy.ndarray but {gene_space_type} found.".format(gene_space_type=type(gene_space))) + self.gene_space = gene_space # Validate init_range_low and init_range_high @@ -214,12 +271,13 @@ def __init__(self, self.init_range_high = init_range_high else: self.valid_parameters = False + self.logger.error("The value passed to the 'init_range_high' parameter must be either integer or floating-point number but the value ({init_range_high_value}) of type {init_range_high_type} found.".format(init_range_high_value=init_range_high, init_range_high_type=type(init_range_high))) raise ValueError("The value passed to the 'init_range_high' parameter must be either integer or floating-point number but the value ({init_range_high_value}) of type {init_range_high_type} found.".format(init_range_high_value=init_range_high, init_range_high_type=type(init_range_high))) else: self.valid_parameters = False + self.logger.error("The value passed to the 'init_range_low' parameter must be either integer or floating-point number but the value ({init_range_low_value}) of type {init_range_low_type} found.".format(init_range_low_value=init_range_low, init_range_low_type=type(init_range_low))) raise ValueError("The value passed to the 'init_range_low' parameter must be either integer or floating-point number but the value ({init_range_low_value}) of type {init_range_low_type} found.".format(init_range_low_value=init_range_low, init_range_low_type=type(init_range_low))) - # 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: @@ -227,10 +285,12 @@ def __init__(self, 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 causes a fixed change to all genes.") else: self.valid_parameters = False - raise TypeError("The expected type of the 'random_mutation_max_val' parameter is numeric but ({random_mutation_max_val_type}) found.".format(random_mutation_max_val_type=type(random_mutation_max_val))) + self.logger.error("The expected type of the 'random_mutation_max_val' parameter is numeric but {random_mutation_max_val_type} found.".format(random_mutation_max_val_type=type(random_mutation_max_val))) + raise TypeError("The expected type of the 'random_mutation_max_val' parameter is numeric but {random_mutation_max_val_type} found.".format(random_mutation_max_val_type=type(random_mutation_max_val))) else: self.valid_parameters = False - raise TypeError("The expected type of the 'random_mutation_min_val' parameter is numeric but ({random_mutation_min_val_type}) found.".format(random_mutation_min_val_type=type(random_mutation_min_val))) + self.logger.error("The expected type of the 'random_mutation_min_val' parameter is numeric but {random_mutation_min_val_type} found.".format(random_mutation_min_val_type=type(random_mutation_min_val))) + raise TypeError("The expected type of the 'random_mutation_min_val' parameter is numeric but {random_mutation_min_val_type} found.".format(random_mutation_min_val_type=type(random_mutation_min_val))) self.random_mutation_min_val = random_mutation_min_val self.random_mutation_max_val = random_mutation_max_val @@ -243,8 +303,18 @@ def __init__(self, self.gene_type = gene_type self.gene_type_single = True elif type(gene_type) in [list, tuple, numpy.ndarray]: - if not len(gene_type) == num_genes: + if num_genes is None: + if initial_population is None: + self.valid_parameters = False + self.logger.error("When the parameter 'initial_population' is None, then the 2 parameters 'sol_per_pop' and 'num_genes' cannot be None too.") + 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 + self.logger.error("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_val} with len(gene_type) ({len_gene_type}) != number of genes ({num_genes}) found.".format(gene_type_val=gene_type, len_gene_type=len(gene_type), num_genes=len(initial_population[0]))) + raise ValueError("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_val} with len(gene_type) ({len_gene_type}) != number of genes ({num_genes}) found.".format(gene_type_val=gene_type, len_gene_type=len(gene_type), num_genes=len(initial_population[0]))) + elif not len(gene_type) == num_genes: self.valid_parameters = False + self.logger.error("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_val} with len(gene_type) ({len_gene_type}) != len(num_genes) ({num_genes}) found.".format(gene_type_val=gene_type, len_gene_type=len(gene_type), num_genes=num_genes)) raise ValueError("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_val} with len(gene_type) ({len_gene_type}) != len(num_genes) ({num_genes}) found.".format(gene_type_val=gene_type, len_gene_type=len(gene_type), num_genes=num_genes)) for gene_type_idx, gene_type_val in enumerate(gene_type): if gene_type_val in GA.supported_float_types: @@ -260,35 +330,43 @@ def __init__(self, pass else: self.valid_parameters = False - raise TypeError("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_precision_val} with type {gene_type_type} .".format(gene_type_val=gene_type_val, gene_type_precision_val=gene_type_val[1], gene_type_type=gene_type_val[0], gene_type_idx=gene_type_idx)) + self.logger.error("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_precision_val} with type {gene_type_type}.".format(gene_type_val=gene_type_val, gene_type_precision_val=gene_type_val[1], gene_type_type=gene_type_val[0], gene_type_idx=gene_type_idx)) + raise TypeError("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_precision_val} with type {gene_type_type}.".format(gene_type_val=gene_type_val, gene_type_precision_val=gene_type_val[1], gene_type_type=gene_type_val[0], gene_type_idx=gene_type_idx)) else: self.valid_parameters = False - raise TypeError("In the 'gene_type' parameter, a precision is expected only for float gene data types but the element {gene_type} found at index {gene_type_idx}. Note that the data type must be at index 0 followed by precision at index 1.".format(gene_type=gene_type_val, gene_type_idx=gene_type_idx)) + self.logger.error("In the 'gene_type' parameter, a precision is expected only for float gene data types but the element {gene_type} found at index {gene_type_idx}.\nNote that the data type must be at index 0 followed by precision at index 1.".format(gene_type=gene_type_val, gene_type_idx=gene_type_idx)) + raise TypeError("In the 'gene_type' parameter, a precision is expected only for float gene data types but the element {gene_type} found at index {gene_type_idx}.\nNote that the data type must be at index 0 followed by precision at index 1.".format(gene_type=gene_type_val, gene_type_idx=gene_type_idx)) else: self.valid_parameters = False + self.logger.error("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 {gene_type_type} with length {gene_type_length} found at index {gene_type_idx}.".format(gene_type_val=gene_type_val, gene_type_type=type(gene_type_val), gene_type_idx=gene_type_idx, gene_type_length=len(gene_type_val))) raise ValueError("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 {gene_type_type} with length {gene_type_length} found at index {gene_type_idx}.".format(gene_type_val=gene_type_val, gene_type_type=type(gene_type_val), gene_type_idx=gene_type_idx, gene_type_length=len(gene_type_val))) else: self.valid_parameters = False + self.logger.error("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 {gene_type_type} found at index {gene_type_idx}.".format(gene_type_val=gene_type_val, gene_type_type=type(gene_type_val), gene_type_idx=gene_type_idx)) raise ValueError("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 {gene_type_type} found at index {gene_type_idx}.".format(gene_type_val=gene_type_val, gene_type_type=type(gene_type_val), gene_type_idx=gene_type_idx)) self.gene_type = gene_type self.gene_type_single = False else: self.valid_parameters = False + self.logger.error("The value passed to the 'gene_type' parameter must be either a single integer, floating-point, list, tuple, or numpy.ndarray but ({gene_type_val}) of type {gene_type_type} found.".format(gene_type_val=gene_type, gene_type_type=type(gene_type))) raise ValueError("The value passed to the 'gene_type' parameter must be either a single integer, floating-point, list, tuple, or numpy.ndarray but ({gene_type_val}) of type {gene_type_type} found.".format(gene_type_val=gene_type, gene_type_type=type(gene_type))) # 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.\nThere are 2 options to prepare the initial population:\n1) Assinging 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.") + self.logger.error("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) Assinging 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.") + 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) Assinging 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 + self.logger.error("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".format(sol_per_pop=sol_per_pop)) raise ValueError("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".format(sol_per_pop=sol_per_pop)) # Validating the number of gene. if (num_genes <= 0): self.valid_parameters = False + self.logger.error("The number of genes cannot be <= 0 but ({num_genes}) found.\n".format(num_genes=num_genes)) raise ValueError("The number of genes cannot be <= 0 but ({num_genes}) found.\n".format(num_genes=num_genes)) # 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. @@ -298,17 +376,38 @@ def __init__(self, if self.gene_space_nested: if len(gene_space) != self.num_genes: self.valid_parameters = False + self.logger.error("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 ({num_genes})".format(len_gene_space=len(gene_space), num_genes=self.num_genes)) raise ValueError("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 ({num_genes})".format(len_gene_space=len(gene_space), num_genes=self.num_genes)) self.sol_per_pop = sol_per_pop # Number of solutions in the population. - self.initialize_population(self.init_range_low, self.init_range_high, allow_duplicate_genes, True, self.gene_type) + self.initialize_population(self.init_range_low, + self.init_range_high, + allow_duplicate_genes, + True, + self.gene_type) else: self.valid_parameters = False - raise TypeError("The expected type of both the sol_per_pop and num_genes parameters is int but ({sol_per_pop_type}) and {num_genes_type} found.".format(sol_per_pop_type=type(sol_per_pop), num_genes_type=type(num_genes))) + self.logger.error("The expected type of both the sol_per_pop and num_genes parameters is int but {sol_per_pop_type} and {num_genes_type} found.".format(sol_per_pop_type=type(sol_per_pop), num_genes_type=type(num_genes))) + raise TypeError("The expected type of both the sol_per_pop and num_genes parameters is int but {sol_per_pop_type} and {num_genes_type} found.".format(sol_per_pop_type=type(sol_per_pop), num_genes_type=type(num_genes))) + elif not type(initial_population) in [list, tuple, numpy.ndarray]: + self.valid_parameters = False + self.logger.error("The value assigned to the 'initial_population' parameter is expected to by of type list, tuple, or ndarray but {initial_population_type} found.".format(initial_population_type=type(initial_population))) + raise TypeError("The value assigned to the 'initial_population' parameter is expected to by of type list, tuple, or ndarray but {initial_population_type} found.".format(initial_population_type=type(initial_population))) elif numpy.array(initial_population).ndim != 2: self.valid_parameters = False + self.logger.error("A 2D list is expected to the initail_population parameter but a ({initial_population_ndim}-D) list found.".format(initial_population_ndim=numpy.array(initial_population).ndim)) raise ValueError("A 2D list is expected to the initail_population parameter but a ({initial_population_ndim}-D) list found.".format(initial_population_ndim=numpy.array(initial_population).ndim)) 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 + self.logger.error("The values in the initial population can be integers or floats but the value ({value}) of type {value_type} found.".format(value=initial_population[row_idx][col_idx], value_type=type(initial_population[row_idx][col_idx]))) + raise TypeError("The values in the initial population can be integers or floats but the value ({value}) of type {value_type} found.".format(value=initial_population[row_idx][col_idx], value_type=type(initial_population[row_idx][col_idx]))) + # Forcing the initial_population array to have the data type assigned to the gene_type parameter. if self.gene_type_single == True: if self.gene_type[1] == None: @@ -340,16 +439,19 @@ def __init__(self, if self.gene_space_nested: if len(gene_space) != self.num_genes: self.valid_parameters = False + self.logger.error("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 ({len_num_genes})".format(len_gene_space=len(gene_space), len_num_genes=self.num_genes)) raise ValueError("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 ({len_num_genes})".format(len_gene_space=len(gene_space), len_num_genes=self.num_genes)) # Validating the number of parents to be selected for mating (num_parents_mating) if num_parents_mating <= 0: self.valid_parameters = False + self.logger.error("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".format(num_parents_mating=num_parents_mating)) raise ValueError("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".format(num_parents_mating=num_parents_mating)) # 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 + self.logger.error("The number of parents to select for mating ({num_parents_mating}) cannot be greater than the number of solutions in the population ({sol_per_pop}) (i.e., num_parents_mating must always be <= sol_per_pop).\n".format(num_parents_mating=num_parents_mating, sol_per_pop=self.sol_per_pop)) raise ValueError("The number of parents to select for mating ({num_parents_mating}) cannot be greater than the number of solutions in the population ({sol_per_pop}) (i.e., num_parents_mating must always be <= sol_per_pop).\n".format(num_parents_mating=num_parents_mating, sol_per_pop=self.sol_per_pop)) self.num_parents_mating = num_parents_mating @@ -365,6 +467,7 @@ def __init__(self, self.crossover = crossover_type else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=crossover_type.__code__.co_name, argcount=crossover_type.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=crossover_type.__code__.co_name, argcount=crossover_type.__code__.co_argcount)) elif callable(crossover_type): # Check if the crossover_type is a function that accepts 2 paramaters. @@ -373,10 +476,12 @@ def __init__(self, self.crossover = crossover_type else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=crossover_type.__code__.co_name, argcount=crossover_type.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=crossover_type.__code__.co_name, argcount=crossover_type.__code__.co_argcount)) elif not (type(crossover_type) is str): self.valid_parameters = False - raise TypeError("The expected type of the 'crossover_type' parameter is either callable or str but ({crossover_type}) found.".format(crossover_type=type(crossover_type))) + self.logger.error("The expected type of the 'crossover_type' parameter is either callable or str but {crossover_type} found.".format(crossover_type=type(crossover_type))) + raise TypeError("The expected type of the 'crossover_type' parameter is either callable or str but {crossover_type} found.".format(crossover_type=type(crossover_type))) else: # type crossover_type is str crossover_type = crossover_type.lower() if (crossover_type == "single_point"): @@ -389,6 +494,7 @@ def __init__(self, self.crossover = self.scattered_crossover else: self.valid_parameters = False + self.logger.error("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".format(crossover_type=crossover_type)) raise TypeError("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".format(crossover_type=crossover_type)) self.crossover_type = crossover_type @@ -401,9 +507,11 @@ def __init__(self, self.crossover_probability = crossover_probability else: self.valid_parameters = False + self.logger.error("The value assigned to the 'crossover_probability' parameter must be between 0 and 1 inclusive but ({crossover_probability_value}) found.".format(crossover_probability_value=crossover_probability)) raise ValueError("The value assigned to the 'crossover_probability' parameter must be between 0 and 1 inclusive but ({crossover_probability_value}) found.".format(crossover_probability_value=crossover_probability)) else: self.valid_parameters = False + self.logger.error("Unexpected type for the 'crossover_probability' parameter. Float is expected but ({crossover_probability_value}) of type {crossover_probability_type} found.".format(crossover_probability_value=crossover_probability, crossover_probability_type=type(crossover_probability))) raise TypeError("Unexpected type for the 'crossover_probability' parameter. Float is expected but ({crossover_probability_value}) of type {crossover_probability_type} found.".format(crossover_probability_value=crossover_probability, crossover_probability_type=type(crossover_probability))) # mutation: Refers to the method that applies the mutation operator based on the selected type of mutation in the mutation_type property. @@ -418,6 +526,7 @@ def __init__(self, self.mutation = mutation_type else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=mutation_type.__code__.co_name, argcount=mutation_type.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=mutation_type.__code__.co_name, argcount=mutation_type.__code__.co_argcount)) elif callable(mutation_type): # Check if the mutation_type is a function that accepts 2 paramater. @@ -426,10 +535,12 @@ def __init__(self, self.mutation = mutation_type else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=mutation_type.__code__.co_name, argcount=mutation_type.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=mutation_type.__code__.co_name, argcount=mutation_type.__code__.co_argcount)) elif not (type(mutation_type) is str): self.valid_parameters = False - raise TypeError("The expected type of the 'mutation_type' parameter is either callable or str but ({mutation_type}) found.".format(mutation_type=type(mutation_type))) + self.logger.error("The expected type of the 'mutation_type' parameter is either callable or str but {mutation_type} found.".format(mutation_type=type(mutation_type))) + raise TypeError("The expected type of the 'mutation_type' parameter is either callable or str but {mutation_type} found.".format(mutation_type=type(mutation_type))) else: # type mutation_type is str mutation_type = mutation_type.lower() if (mutation_type == "random"): @@ -444,6 +555,7 @@ def __init__(self, self.mutation = self.adaptive_mutation else: self.valid_parameters = False + self.logger.error("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".format(mutation_type=mutation_type)) raise TypeError("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".format(mutation_type=mutation_type)) self.mutation_type = mutation_type @@ -459,9 +571,11 @@ def __init__(self, self.mutation_probability = mutation_probability else: self.valid_parameters = False + self.logger.error("The value assigned to the 'mutation_probability' parameter must be between 0 and 1 inclusive but ({mutation_probability_value}) found.".format(mutation_probability_value=mutation_probability)) raise ValueError("The value assigned to the 'mutation_probability' parameter must be between 0 and 1 inclusive but ({mutation_probability_value}) found.".format(mutation_probability_value=mutation_probability)) else: self.valid_parameters = False + self.logger.error("Unexpected type for the 'mutation_probability' parameter. A numeric value is expected but ({mutation_probability_value}) of type {mutation_probability_type} found.".format(mutation_probability_value=mutation_probability, mutation_probability_type=type(mutation_probability))) raise TypeError("Unexpected type for the 'mutation_probability' parameter. A numeric value is expected but ({mutation_probability_value}) of type {mutation_probability_type} found.".format(mutation_probability_value=mutation_probability, mutation_probability_type=type(mutation_probability))) else: # Mutation probability is adaptive not fixed. @@ -473,18 +587,22 @@ def __init__(self, pass else: self.valid_parameters = False + self.logger.error("The values assigned to the 'mutation_probability' parameter must be between 0 and 1 inclusive but ({mutation_probability_value}) found.".format(mutation_probability_value=el)) raise ValueError("The values assigned to the 'mutation_probability' parameter must be between 0 and 1 inclusive but ({mutation_probability_value}) found.".format(mutation_probability_value=el)) else: self.valid_parameters = False + self.logger.error("Unexpected type for a value assigned to the 'mutation_probability' parameter. A numeric value is expected but ({mutation_probability_value}) of type {mutation_probability_type} found.".format(mutation_probability_value=el, mutation_probability_type=type(el))) raise TypeError("Unexpected type for a value assigned to the 'mutation_probability' parameter. A numeric value is expected but ({mutation_probability_value}) of type {mutation_probability_type} found.".format(mutation_probability_value=el, mutation_probability_type=type(el))) if mutation_probability[0] < mutation_probability[1]: if not self.suppress_warnings: warnings.warn("The first element in the 'mutation_probability' parameter is {first_el} which is smaller than the second element {second_el}. 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 qualitiy solutions while making little changes in the low quality solutions. Please make the first element higher than the second element.".format(first_el=mutation_probability[0], second_el=mutation_probability[1])) self.mutation_probability = mutation_probability else: self.valid_parameters = False + self.logger.error("When mutation_type='adaptive', then the 'mutation_probability' parameter must have only 2 elements but ({mutation_probability_length}) element(s) found.".format(mutation_probability_length=len(mutation_probability))) raise ValueError("When mutation_type='adaptive', then the 'mutation_probability' parameter must have only 2 elements but ({mutation_probability_length}) element(s) found.".format(mutation_probability_length=len(mutation_probability))) else: self.valid_parameters = False + self.logger.error("Unexpected type for the 'mutation_probability' parameter. When mutation_type='adaptive', then list/tuple/numpy.ndarray is expected but ({mutation_probability_value}) of type {mutation_probability_type} found.".format(mutation_probability_value=mutation_probability, mutation_probability_type=type(mutation_probability))) raise TypeError("Unexpected type for the 'mutation_probability' parameter. When mutation_type='adaptive', then list/tuple/numpy.ndarray is expected but ({mutation_probability_value}) of type {mutation_probability_type} found.".format(mutation_probability_value=mutation_probability, mutation_probability_type=type(mutation_probability))) else: pass @@ -508,6 +626,7 @@ def __init__(self, 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 + self.logger.error("The percentage of selected genes for mutation (mutation_percent_genes) must be > 0 and <= 100 but ({mutation_percent_genes}) found.\n".format(mutation_percent_genes=mutation_percent_genes)) raise ValueError("The percentage of selected genes for mutation (mutation_percent_genes) must be > 0 and <= 100 but ({mutation_percent_genes}) found.\n".format(mutation_percent_genes=mutation_percent_genes)) else: # If mutation_percent_genes equals the string "default", then it is replaced by the numeric value 10. @@ -523,6 +642,7 @@ def __init__(self, mutation_num_genes = 1 else: self.valid_parameters = False + self.logger.error("Unexpected value or type of the 'mutation_percent_genes' parameter. It only accepts the string 'default' or a numeric value but ({mutation_percent_genes_value}) of type {mutation_percent_genes_type} found.".format(mutation_percent_genes_value=mutation_percent_genes, mutation_percent_genes_type=type(mutation_percent_genes))) raise TypeError("Unexpected value or type of the 'mutation_percent_genes' parameter. It only accepts the string 'default' or a numeric value but ({mutation_percent_genes_value}) of type {mutation_percent_genes_type} found.".format(mutation_percent_genes_value=mutation_percent_genes, mutation_percent_genes_type=type(mutation_percent_genes))) else: # The percent of genes to mutate is adaptive not fixed. @@ -533,9 +653,11 @@ def __init__(self, if type(el) in GA.supported_int_float_types: if (el <= 0 or el > 100): self.valid_parameters = False + self.logger.error("The values assigned to the 'mutation_percent_genes' must be > 0 and <= 100 but ({mutation_percent_genes}) found.\n".format(mutation_percent_genes=mutation_percent_genes)) raise ValueError("The values assigned to the 'mutation_percent_genes' must be > 0 and <= 100 but ({mutation_percent_genes}) found.\n".format(mutation_percent_genes=mutation_percent_genes)) else: self.valid_parameters = False + self.logger.error("Unexpected type for a value assigned to the 'mutation_percent_genes' parameter. An integer value is expected but ({mutation_percent_genes_value}) of type {mutation_percent_genes_type} found.".format(mutation_percent_genes_value=el, mutation_percent_genes_type=type(el))) raise TypeError("Unexpected type for a value assigned to the 'mutation_percent_genes' parameter. An integer value is expected but ({mutation_percent_genes_value}) of type {mutation_percent_genes_type} found.".format(mutation_percent_genes_value=el, mutation_percent_genes_type=type(el))) # 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. @@ -549,23 +671,28 @@ def __init__(self, # At this point outside the loop, all values of the parameter 'mutation_percent_genes' are validated. Eveyrthing is OK. else: self.valid_parameters = False + self.logger.error("When mutation_type='adaptive', then the 'mutation_percent_genes' parameter must have only 2 elements but ({mutation_percent_genes_length}) element(s) found.".format(mutation_percent_genes_length=len(mutation_percent_genes))) raise ValueError("When mutation_type='adaptive', then the 'mutation_percent_genes' parameter must have only 2 elements but ({mutation_percent_genes_length}) element(s) found.".format(mutation_percent_genes_length=len(mutation_percent_genes))) else: if self.mutation_probability is None: self.valid_parameters = False - raise TypeError("Unexpected type for 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_value}) found.".format(mutation_percent_genes_value=mutation_percent_genes)) + self.logger.error("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_value}) found.".format(mutation_percent_genes_value=mutation_percent_genes)) + raise TypeError("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_value}) found.".format(mutation_percent_genes_value=mutation_percent_genes)) # 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 + self.logger.error("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".format(mutation_num_genes=mutation_num_genes)) raise ValueError("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".format(mutation_num_genes=mutation_num_genes)) elif (mutation_num_genes > self.num_genes): self.valid_parameters = False + self.logger.error("The number of selected genes for mutation (mutation_num_genes), which is ({mutation_num_genes}), cannot be greater than the number of genes ({num_genes}).\n".format(mutation_num_genes=mutation_num_genes, num_genes=self.num_genes)) raise ValueError("The number of selected genes for mutation (mutation_num_genes), which is ({mutation_num_genes}), cannot be greater than the number of genes ({num_genes}).\n".format(mutation_num_genes=mutation_num_genes, num_genes=self.num_genes)) else: self.valid_parameters = False + self.logger.error("The 'mutation_num_genes' parameter is expected to be a positive integer but the value ({mutation_num_genes_value}) of type {mutation_num_genes_type} found.\n".format(mutation_num_genes_value=mutation_num_genes, mutation_num_genes_type=type(mutation_num_genes))) raise TypeError("The 'mutation_num_genes' parameter is expected to be a positive integer but the value ({mutation_num_genes_value}) of type {mutation_num_genes_type} found.\n".format(mutation_num_genes_value=mutation_num_genes, mutation_num_genes_type=type(mutation_num_genes))) else: # Number of genes to mutate is adaptive not fixed. @@ -575,12 +702,15 @@ def __init__(self, if type(el) in GA.supported_int_types: if (el <= 0): self.valid_parameters = False + self.logger.error("The values assigned to the 'mutation_num_genes' cannot be <= 0 but ({mutation_num_genes_value}) found. If you do not want to use mutation, please set mutation_type=None\n".format(mutation_num_genes_value=el)) raise ValueError("The values assigned to the 'mutation_num_genes' cannot be <= 0 but ({mutation_num_genes_value}) found. If you do not want to use mutation, please set mutation_type=None\n".format(mutation_num_genes_value=el)) elif (el > self.num_genes): self.valid_parameters = False + self.logger.error("The values assigned to the 'mutation_num_genes' cannot be greater than the number of genes ({num_genes}) but ({mutation_num_genes_value}) found.\n".format(mutation_num_genes_value=el, num_genes=self.num_genes)) raise ValueError("The values assigned to the 'mutation_num_genes' cannot be greater than the number of genes ({num_genes}) but ({mutation_num_genes_value}) found.\n".format(mutation_num_genes_value=el, num_genes=self.num_genes)) else: self.valid_parameters = False + self.logger.error("Unexpected type for a value assigned to the 'mutation_num_genes' parameter. An integer value is expected but ({mutation_num_genes_value}) of type {mutation_num_genes_type} found.".format(mutation_num_genes_value=el, mutation_num_genes_type=type(el))) raise TypeError("Unexpected type for a value assigned to the 'mutation_num_genes' parameter. An integer value is expected but ({mutation_num_genes_value}) of type {mutation_num_genes_type} found.".format(mutation_num_genes_value=el, mutation_num_genes_type=type(el))) # 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]: @@ -588,9 +718,11 @@ def __init__(self, # At this point outside the loop, all values of the parameter 'mutation_num_genes' are validated. Eveyrthing is OK. else: self.valid_parameters = False + self.logger.error("When mutation_type='adaptive', then the 'mutation_num_genes' parameter must have only 2 elements but ({mutation_num_genes_length}) element(s) found.".format(mutation_num_genes_length=len(mutation_num_genes))) raise ValueError("When mutation_type='adaptive', then the 'mutation_num_genes' parameter must have only 2 elements but ({mutation_num_genes_length}) element(s) found.".format(mutation_num_genes_length=len(mutation_num_genes))) else: self.valid_parameters = False + self.logger.error("Unexpected type for the 'mutation_num_genes' parameter. When mutation_type='adaptive', then list/tuple/numpy.ndarray is expected but ({mutation_num_genes_value}) of type {mutation_num_genes_type} found.".format(mutation_num_genes_value=mutation_num_genes, mutation_num_genes_type=type(mutation_num_genes))) raise TypeError("Unexpected type for the 'mutation_num_genes' parameter. When mutation_type='adaptive', then list/tuple/numpy.ndarray is expected but ({mutation_num_genes_value}) of type {mutation_num_genes_type} found.".format(mutation_num_genes_value=mutation_num_genes, mutation_num_genes_type=type(mutation_num_genes))) else: pass @@ -613,6 +745,7 @@ def __init__(self, self.select_parents = parent_selection_type else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=parent_selection_type.__code__.co_name, argcount=parent_selection_type.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=parent_selection_type.__code__.co_name, argcount=parent_selection_type.__code__.co_argcount)) elif callable(parent_selection_type): # Check if the parent_selection_type is a function that accepts 3 paramaters. @@ -622,10 +755,12 @@ def __init__(self, self.select_parents = parent_selection_type else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=parent_selection_type.__code__.co_name, argcount=parent_selection_type.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=parent_selection_type.__code__.co_name, argcount=parent_selection_type.__code__.co_argcount)) elif not (type(parent_selection_type) is str): self.valid_parameters = False - raise TypeError("The expected type of the 'parent_selection_type' parameter is either callable or str but ({parent_selection_type}) found.".format(parent_selection_type=type(parent_selection_type))) + self.logger.error("The expected type of the 'parent_selection_type' parameter is either callable or str but {parent_selection_type} found.".format(parent_selection_type=type(parent_selection_type))) + raise TypeError("The expected type of the 'parent_selection_type' parameter is either callable or str but {parent_selection_type} found.".format(parent_selection_type=type(parent_selection_type))) else: parent_selection_type = parent_selection_type.lower() if (parent_selection_type == "sss"): @@ -642,6 +777,7 @@ def __init__(self, self.select_parents = self.rank_selection else: self.valid_parameters = False + self.logger.error("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 (for steady state selection)\n-rws (for roulette wheel selection)\n-sus (for stochastic universal selection)\n-rank (for rank selection)\n-random (for random selection)\n-tournament (for tournament selection).\n".format(parent_selection_type=parent_selection_type)) raise TypeError("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 (for steady state selection)\n-rws (for roulette wheel selection)\n-sus (for stochastic universal selection)\n-rank (for rank selection)\n-random (for random selection)\n-tournament (for tournament selection).\n".format(parent_selection_type=parent_selection_type)) # For tournament selection, validate the K value. @@ -651,6 +787,7 @@ def __init__(self, if not self.suppress_warnings: warnings.warn("K of the tournament selection ({K_tournament}) should not be greater than the number of solutions within the population ({sol_per_pop}).\nK will be clipped to be equal to the number of solutions in the population (sol_per_pop).\n".format(K_tournament=K_tournament, sol_per_pop=self.sol_per_pop)) elif (K_tournament <= 0): self.valid_parameters = False + self.logger.error("K of the tournament selection cannot be <=0 but ({K_tournament}) found.\n".format(K_tournament=K_tournament)) raise ValueError("K of the tournament selection cannot be <=0 but ({K_tournament}) found.\n".format(K_tournament=K_tournament)) self.K_tournament = K_tournament @@ -658,9 +795,11 @@ def __init__(self, # 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("Incorrect type of the value assigned to the keep_parents parameter. The value {keep_parents} of type {keep_parents_type} found but an integer is expected.".format(keep_parents=keep_parents, keep_parents_type=type(keep_parents))) + self.logger.error("Incorrect type of the value assigned to the keep_parents parameter. The value ({keep_parents}) of type {keep_parents_type} found but an integer is expected.".format(keep_parents=keep_parents, keep_parents_type=type(keep_parents))) + raise TypeError("Incorrect type of the value assigned to the keep_parents parameter. The value ({keep_parents}) of type {keep_parents_type} found but an integer is expected.".format(keep_parents=keep_parents, keep_parents_type=type(keep_parents))) elif (keep_parents > self.sol_per_pop or keep_parents > self.num_parents_mating or keep_parents < -1): self.valid_parameters = False + self.logger.error("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.".format(keep_parents=keep_parents)) raise ValueError("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.".format(keep_parents=keep_parents)) self.keep_parents = keep_parents @@ -671,9 +810,11 @@ def __init__(self, # 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("Incorrect type of the value assigned to the keep_elitism parameter. The value {keep_elitism} of type {keep_elitism_type} found but an integer is expected.".format(keep_elitism=keep_elitism, keep_elitism_type=type(keep_elitism))) + self.logger.error("Incorrect type of the value assigned to the keep_elitism parameter. The value ({keep_elitism}) of type {keep_elitism_type} found but an integer is expected.".format(keep_elitism=keep_elitism, keep_elitism_type=type(keep_elitism))) + raise TypeError("Incorrect type of the value assigned to the keep_elitism parameter. The value ({keep_elitism}) of type {keep_elitism_type} found but an integer is expected.".format(keep_elitism=keep_elitism, keep_elitism_type=type(keep_elitism))) elif (keep_elitism > self.sol_per_pop or keep_elitism < 0): self.valid_parameters = False + self.logger.error("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.".format(keep_elitism=keep_elitism)) raise ValueError("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.".format(keep_elitism=keep_elitism)) self.keep_elitism = keep_elitism @@ -690,34 +831,40 @@ def __init__(self, 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 third 'self` paramaters. - # Check if the method has 3 parameters. - if (fitness_func.__code__.co_argcount == 3): + # If the fitness is calculated through a method, not a function, then there is a fourth 'self` paramaters. + if (fitness_func.__code__.co_argcount == 4): self.fitness_func = fitness_func else: self.valid_parameters = False - raise ValueError("If a method is used to calculate the fitness value, then it must accept 3 parameters\n1) Expected to be the 'self' object.\n2) A solution to calculate its fitness value.\n3) The solution's index within the population.\n\nThe passed fitness method named '{funcname}' accepts {argcount} parameter(s).".format(funcname=fitness_func.__code__.co_name, argcount=fitness_func.__code__.co_argcount)) + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=fitness_func.__code__.co_name, argcount=fitness_func.__code__.co_argcount)) + raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=fitness_func.__code__.co_name, argcount=fitness_func.__code__.co_argcount)) elif callable(fitness_func): # Check if the fitness function accepts 2 paramaters. - if (fitness_func.__code__.co_argcount == 2): + if (fitness_func.__code__.co_argcount == 3): self.fitness_func = fitness_func else: self.valid_parameters = False - raise ValueError("The fitness function must accept 2 parameters:\n1) A solution to calculate its fitness value.\n2) The solution's index within the population.\n\nThe passed fitness function named '{funcname}' accepts {argcount} parameter(s).".format(funcname=fitness_func.__code__.co_name, argcount=fitness_func.__code__.co_argcount)) + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=fitness_func.__code__.co_name, argcount=fitness_func.__code__.co_argcount)) + raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=fitness_func.__code__.co_name, argcount=fitness_func.__code__.co_argcount)) else: self.valid_parameters = False - raise TypeError("The value assigned to the fitness_func parameter is expected to be of type function but ({fitness_func_type}) found.".format(fitness_func_type=type(fitness_func))) + self.logger.error("The value assigned to the fitness_func parameter is expected to be of type function but {fitness_func_type} found.".format(fitness_func_type=type(fitness_func))) + raise TypeError("The value assigned to the fitness_func parameter is expected to be of type function but {fitness_func_type} found.".format(fitness_func_type=type(fitness_func))) if fitness_batch_size is None: pass elif not (type(fitness_batch_size) in GA.supported_int_types): self.valid_parameters = False - raise TypeError("The value assigned to the fitness_batch_size parameter is expected to be integer but the value ({fitness_batch_size}) of type ({fitness_batch_size_type}) found.".format(fitness_batch_size=fitness_batch_size, fitness_batch_size_type=type(fitness_batch_size))) + self.logger.error("The value assigned to the fitness_batch_size parameter is expected to be integer but the value ({fitness_batch_size}) of type {fitness_batch_size_type} found.".format(fitness_batch_size=fitness_batch_size, fitness_batch_size_type=type(fitness_batch_size))) + raise TypeError("The value assigned to the fitness_batch_size parameter is expected to be integer but the value ({fitness_batch_size}) of type {fitness_batch_size_type} found.".format(fitness_batch_size=fitness_batch_size, fitness_batch_size_type=type(fitness_batch_size))) elif fitness_batch_size <= 0 or fitness_batch_size > self.sol_per_pop: self.valid_parameters = False + self.logger.error("The value assigned to the fitness_batch_size parameter must be:\n1) Greater than 0.\n2) Less than or equal to sol_per_pop ({sol_per_pop}).\nBut the value ({fitness_batch_size}) found.".format(fitness_batch_size=fitness_batch_size, sol_per_pop=self.sol_per_pop)) raise ValueError("The value assigned to the fitness_batch_size parameter must be:\n1) Greater than 0.\n2) Less than or equal to sol_per_pop ({sol_per_pop}).\nBut the value ({fitness_batch_size}) found.".format(fitness_batch_size=fitness_batch_size, sol_per_pop=self.sol_per_pop)) - + self.fitness_batch_size = fitness_batch_size # Check if the on_start exists. @@ -728,6 +875,7 @@ def __init__(self, self.on_start = on_start else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_start.__code__.co_name, argcount=on_start.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_start.__code__.co_name, argcount=on_start.__code__.co_argcount)) # Check if the on_start is a function. elif callable(on_start): @@ -736,10 +884,12 @@ def __init__(self, self.on_start = on_start else: self.valid_parameters = False + self.logger.error("The function assigned to the on_start parameter must accept only 1 parameter representing the instance of the genetic algorithm.\nThe passed function named '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_start.__code__.co_name, argcount=on_start.__code__.co_argcount)) raise ValueError("The function assigned to the on_start parameter must accept only 1 parameter representing the instance of the genetic algorithm.\nThe passed function named '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_start.__code__.co_name, argcount=on_start.__code__.co_argcount)) else: self.valid_parameters = False - raise TypeError("The value assigned to the on_start parameter is expected to be of type function but ({on_start_type}) found.".format(on_start_type=type(on_start))) + self.logger.error("The value assigned to the on_start parameter is expected to be of type function but {on_start_type} found.".format(on_start_type=type(on_start))) + raise TypeError("The value assigned to the on_start parameter is expected to be of type function but {on_start_type} found.".format(on_start_type=type(on_start))) else: self.on_start = None @@ -752,6 +902,7 @@ def __init__(self, self.on_fitness = on_fitness else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_fitness.__code__.co_name, argcount=on_fitness.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_fitness.__code__.co_name, argcount=on_fitness.__code__.co_argcount)) # Check if the on_fitness is a function. elif callable(on_fitness): @@ -760,10 +911,12 @@ def __init__(self, self.on_fitness = on_fitness else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_fitness.__code__.co_name, argcount=on_fitness.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_fitness.__code__.co_name, argcount=on_fitness.__code__.co_argcount)) else: self.valid_parameters = False - raise TypeError("The value assigned to the on_fitness parameter is expected to be of type function but ({on_fitness_type}) found.".format(on_fitness_type=type(on_fitness))) + self.logger.error("The value assigned to the on_fitness parameter is expected to be of type function but {on_fitness_type} found.".format(on_fitness_type=type(on_fitness))) + raise TypeError("The value assigned to the on_fitness parameter is expected to be of type function but {on_fitness_type} found.".format(on_fitness_type=type(on_fitness))) else: self.on_fitness = None @@ -776,6 +929,7 @@ def __init__(self, self.on_parents = on_parents else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_parents.__code__.co_name, argcount=on_parents.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_parents.__code__.co_name, argcount=on_parents.__code__.co_argcount)) # Check if the on_parents is a function. elif callable(on_parents): @@ -784,10 +938,12 @@ def __init__(self, self.on_parents = on_parents else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_parents.__code__.co_name, argcount=on_parents.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_parents.__code__.co_name, argcount=on_parents.__code__.co_argcount)) else: self.valid_parameters = False - raise TypeError("The value assigned to the on_parents parameter is expected to be of type function but ({on_parents_type}) found.".format(on_parents_type=type(on_parents))) + self.logger.error("The value assigned to the on_parents parameter is expected to be of type function but {on_parents_type} found.".format(on_parents_type=type(on_parents))) + raise TypeError("The value assigned to the on_parents parameter is expected to be of type function but {on_parents_type} found.".format(on_parents_type=type(on_parents))) else: self.on_parents = None @@ -800,6 +956,7 @@ def __init__(self, self.on_crossover = on_crossover else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_crossover.__code__.co_name, argcount=on_crossover.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_crossover.__code__.co_name, argcount=on_crossover.__code__.co_argcount)) # Check if the on_crossover is a function. elif callable(on_crossover): @@ -808,10 +965,12 @@ def __init__(self, self.on_crossover = on_crossover else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_crossover.__code__.co_name, argcount=on_crossover.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_crossover.__code__.co_name, argcount=on_crossover.__code__.co_argcount)) else: self.valid_parameters = False - raise TypeError("The value assigned to the on_crossover parameter is expected to be of type function but ({on_crossover_type}) found.".format(on_crossover_type=type(on_crossover))) + self.logger.error("The value assigned to the on_crossover parameter is expected to be of type function but {on_crossover_type} found.".format(on_crossover_type=type(on_crossover))) + raise TypeError("The value assigned to the on_crossover parameter is expected to be of type function but {on_crossover_type} found.".format(on_crossover_type=type(on_crossover))) else: self.on_crossover = None @@ -824,6 +983,7 @@ def __init__(self, self.on_mutation = on_mutation else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_mutation.__code__.co_name, argcount=on_mutation.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_mutation.__code__.co_name, argcount=on_mutation.__code__.co_argcount)) # Check if the on_mutation is a function. elif callable(on_mutation): @@ -832,41 +992,15 @@ def __init__(self, self.on_mutation = on_mutation else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_mutation.__code__.co_name, argcount=on_mutation.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_mutation.__code__.co_name, argcount=on_mutation.__code__.co_argcount)) else: self.valid_parameters = False - raise TypeError("The value assigned to the on_mutation parameter is expected to be of type function but ({on_mutation_type}) found.".format(on_mutation_type=type(on_mutation))) + self.logger.error("The value assigned to the on_mutation parameter is expected to be of type function but {on_mutation_type} found.".format(on_mutation_type=type(on_mutation))) + raise TypeError("The value assigned to the on_mutation parameter is expected to be of type function but {on_mutation_type} found.".format(on_mutation_type=type(on_mutation))) else: self.on_mutation = None - # Check if the callback_generation exists. - if not (callback_generation is None): - # Check if the callback_generation is a method. - if inspect.ismethod(callback_generation): - # Check if the callback_generation method accepts 2 paramaters. - if (callback_generation.__code__.co_argcount == 2): - self.callback_generation = callback_generation - on_generation = callback_generation - if not self.suppress_warnings: warnings.warn("Starting from PyGAD 2.6.0, the callback_generation parameter is deprecated and will be removed in a later release of PyGAD. Please use the on_generation parameter instead.") - else: - self.valid_parameters = False - raise ValueError("The method assigned to the callback_generation parameter must accept 2 parameters:\n1) Expected to be the 'self' object.\n2) The instance of the genetic algorithm.\nThe passed method named '{funcname}' accepts {argcount} parameter(s).".format(funcname=callback_generation.__code__.co_name, argcount=callback_generation.__code__.co_argcount)) - # Check if the callback_generation is a function. - elif callable(callback_generation): - # Check if the callback_generation function accepts only a single paramater. - if (callback_generation.__code__.co_argcount == 1): - self.callback_generation = callback_generation - on_generation = callback_generation - if not self.suppress_warnings: warnings.warn("Starting from PyGAD 2.6.0, the callback_generation parameter is deprecated and will be removed in a later release of PyGAD. Please use the on_generation parameter instead.") - else: - self.valid_parameters = False - raise ValueError("The function assigned to the callback_generation parameter must accept only 1 parameter representing the instance of the genetic algorithm.\nThe passed function named '{funcname}' accepts {argcount} parameter(s).".format(funcname=callback_generation.__code__.co_name, argcount=callback_generation.__code__.co_argcount)) - else: - self.valid_parameters = False - raise TypeError("The value assigned to the callback_generation parameter is expected to be of type function but ({callback_generation_type}) found.".format(callback_generation_type=type(callback_generation))) - else: - self.callback_generation = None - # Check if the on_generation exists. if not (on_generation is None): # Check if the on_generation is a method. @@ -876,6 +1010,7 @@ def __init__(self, self.on_generation = on_generation else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_generation.__code__.co_name, argcount=on_generation.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_generation.__code__.co_name, argcount=on_generation.__code__.co_argcount)) # Check if the on_generation is a function. elif callable(on_generation): @@ -884,10 +1019,12 @@ def __init__(self, self.on_generation = on_generation else: self.valid_parameters = False + self.logger.error("The function assigned to the on_generation parameter must accept only 1 parameter representing the instance of the genetic algorithm.\nThe passed function named '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_generation.__code__.co_name, argcount=on_generation.__code__.co_argcount)) raise ValueError("The function assigned to the on_generation parameter must accept only 1 parameter representing the instance of the genetic algorithm.\nThe passed function named '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_generation.__code__.co_name, argcount=on_generation.__code__.co_argcount)) else: self.valid_parameters = False - raise TypeError("The value assigned to the on_generation parameter is expected to be of type function but ({on_generation_type}) found.".format(on_generation_type=type(on_generation))) + self.logger.error("The value assigned to the on_generation parameter is expected to be of type function but {on_generation_type} found.".format(on_generation_type=type(on_generation))) + raise TypeError("The value assigned to the on_generation parameter is expected to be of type function but {on_generation_type} found.".format(on_generation_type=type(on_generation))) else: self.on_generation = None @@ -900,6 +1037,7 @@ def __init__(self, self.on_stop = on_stop else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_stop.__code__.co_name, argcount=on_stop.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_stop.__code__.co_name, argcount=on_stop.__code__.co_argcount)) # Check if the on_stop is a function. elif callable(on_stop): @@ -908,10 +1046,12 @@ def __init__(self, self.on_stop = on_stop else: self.valid_parameters = False + self.logger.error("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_stop.__code__.co_name, argcount=on_stop.__code__.co_argcount)) raise ValueError("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 '{funcname}' accepts {argcount} parameter(s).".format(funcname=on_stop.__code__.co_name, argcount=on_stop.__code__.co_argcount)) else: self.valid_parameters = False - raise TypeError("The value assigned to the 'on_stop' parameter is expected to be of type function but ({on_stop_type}) found.".format(on_stop_type=type(on_stop))) + self.logger.error("The value assigned to the 'on_stop' parameter is expected to be of type function but {on_stop_type} found.".format(on_stop_type=type(on_stop))) + raise TypeError("The value assigned to the 'on_stop' parameter is expected to be of type function but {on_stop_type} found.".format(on_stop_type=type(on_stop))) else: self.on_stop = None @@ -921,10 +1061,12 @@ def __init__(self, self.delay_after_gen = delay_after_gen else: self.valid_parameters = False - raise ValueError("The value passed to the 'delay_after_gen' parameter must be a non-negative number. The value passed is {delay_after_gen} of type {delay_after_gen_type}.".format(delay_after_gen=delay_after_gen, delay_after_gen_type=type(delay_after_gen))) + self.logger.error("The value passed to the 'delay_after_gen' parameter must be a non-negative number. The value passed is ({delay_after_gen}) of type {delay_after_gen_type}.".format(delay_after_gen=delay_after_gen, delay_after_gen_type=type(delay_after_gen))) + raise ValueError("The value passed to the 'delay_after_gen' parameter must be a non-negative number. The value passed is ({delay_after_gen}) of type {delay_after_gen_type}.".format(delay_after_gen=delay_after_gen, delay_after_gen_type=type(delay_after_gen))) else: self.valid_parameters = False - raise TypeError("The value passed to the 'delay_after_gen' parameter must be of type int or float but ({delay_after_gen_type}) found.".format(delay_after_gen_type=type(delay_after_gen))) + self.logger.error("The value passed to the 'delay_after_gen' parameter must be of type int or float but {delay_after_gen_type} found.".format(delay_after_gen_type=type(delay_after_gen))) + raise TypeError("The value passed to the 'delay_after_gen' parameter must be of type int or float but {delay_after_gen_type} found.".format(delay_after_gen_type=type(delay_after_gen))) # Validate save_best_solutions if type(save_best_solutions) is bool: @@ -932,7 +1074,8 @@ def __init__(self, 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("The value passed to the 'save_best_solutions' parameter must be of type bool but ({save_best_solutions_type}) found.".format(save_best_solutions_type=type(save_best_solutions))) + self.logger.error("The value passed to the 'save_best_solutions' parameter must be of type bool but {save_best_solutions_type} found.".format(save_best_solutions_type=type(save_best_solutions))) + raise TypeError("The value passed to the 'save_best_solutions' parameter must be of type bool but {save_best_solutions_type} found.".format(save_best_solutions_type=type(save_best_solutions))) # Validate save_solutions if type(save_solutions) is bool: @@ -940,12 +1083,14 @@ def __init__(self, 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("The value passed to the 'save_solutions' parameter must be of type bool but ({save_solutions_type}) found.".format(save_solutions_type=type(save_solutions))) + self.logger.error("The value passed to the 'save_solutions' parameter must be of type bool but {save_solutions_type} found.".format(save_solutions_type=type(save_solutions))) + raise TypeError("The value passed to the 'save_solutions' parameter must be of type bool but {save_solutions_type} found.".format(save_solutions_type=type(save_solutions))) # Validate allow_duplicate_genes if not (type(allow_duplicate_genes) is bool): self.valid_parameters = False - raise TypeError("The expected type of the 'allow_duplicate_genes' parameter is bool but ({allow_duplicate_genes_type}) found.".format(allow_duplicate_genes_type=type(allow_duplicate_genes))) + self.logger.error("The expected type of the 'allow_duplicate_genes' parameter is bool but {allow_duplicate_genes_type} found.".format(allow_duplicate_genes_type=type(allow_duplicate_genes))) + raise TypeError("The expected type of the 'allow_duplicate_genes' parameter is bool but {allow_duplicate_genes_type} found.".format(allow_duplicate_genes_type=type(allow_duplicate_genes))) self.allow_duplicate_genes = allow_duplicate_genes @@ -966,18 +1111,21 @@ def __init__(self, pass else: self.valid_parameters = False + self.logger.error("In the 'stop_criteria' parameter, the supported stop words are '{supported_stop_words}' but '{stop_word}' found.".format(supported_stop_words=self.supported_stop_words, stop_word=stop_word)) raise ValueError("In the 'stop_criteria' parameter, the supported stop words are '{supported_stop_words}' but '{stop_word}' found.".format(supported_stop_words=self.supported_stop_words, stop_word=stop_word)) if number.replace(".", "").isnumeric(): number = float(number) else: self.valid_parameters = False - raise ValueError("The value following the stop word in the 'stop_criteria' parameter must be a number but the value '{stop_val}' of type {stop_val_type} found.".format(stop_val=number, stop_val_type=type(number))) + self.logger.error("The value following the stop word in the 'stop_criteria' parameter must be a number but the value ({stop_val}) of type {stop_val_type} found.".format(stop_val=number, stop_val_type=type(number))) + raise ValueError("The value following the stop word in the 'stop_criteria' parameter must be a number but the value ({stop_val}) of type {stop_val_type} found.".format(stop_val=number, stop_val_type=type(number))) self.stop_criteria.append([stop_word, number]) else: self.valid_parameters = False + self.logger.error("For format of a single criterion in the 'stop_criteria' parameter is 'word_number' but '{stop_criteria}' found.".format(stop_criteria=stop_criteria)) raise ValueError("For format of a single criterion in the 'stop_criteria' parameter is 'word_number' but '{stop_criteria}' found.".format(stop_criteria=stop_criteria)) elif type(stop_criteria) in [list, tuple, numpy.ndarray]: @@ -994,25 +1142,30 @@ def __init__(self, pass else: self.valid_parameters = False + self.logger.error("In the 'stop_criteria' parameter, the supported stop words are {supported_stop_words} but '{stop_word}' found.".format(supported_stop_words=self.supported_stop_words, stop_word=stop_word)) raise ValueError("In the 'stop_criteria' parameter, the supported stop words are {supported_stop_words} but '{stop_word}' found.".format(supported_stop_words=self.supported_stop_words, stop_word=stop_word)) if number.replace(".", "").isnumeric(): number = float(number) else: self.valid_parameters = False - raise ValueError("The value following the stop word in the 'stop_criteria' parameter must be a number but the value '{stop_val}' of type {stop_val_type} found.".format(stop_val=number, stop_val_type=type(number))) + self.logger.error("The value following the stop word in the 'stop_criteria' parameter must be a number but the value ({stop_val}) of type {stop_val_type} found.".format(stop_val=number, stop_val_type=type(number))) + raise ValueError("The value following the stop word in the 'stop_criteria' parameter must be a number but the value ({stop_val}) of type {stop_val_type} found.".format(stop_val=number, stop_val_type=type(number))) self.stop_criteria.append([stop_word, number]) else: self.valid_parameters = False + self.logger.error("The format of a single criterion in the 'stop_criteria' parameter is 'word_number' but {stop_criteria} found.".format(stop_criteria=criterion)) raise ValueError("The format of a single criterion in the 'stop_criteria' parameter is 'word_number' but {stop_criteria} found.".format(stop_criteria=criterion)) else: self.valid_parameters = False - raise TypeError("When the 'stop_criteria' parameter is assigned a tuple/list/numpy.ndarray, then its elements must be strings but the value '{stop_criteria_val}' of type {stop_criteria_val_type} found at index {stop_criteria_val_idx}.".format(stop_criteria_val=val, stop_criteria_val_type=type(val), stop_criteria_val_idx=idx)) + self.logger.error("When the 'stop_criteria' parameter is assigned a tuple/list/numpy.ndarray, then its elements must be strings but the value ({stop_criteria_val}) of type {stop_criteria_val_type} found at index {stop_criteria_val_idx}.".format(stop_criteria_val=val, stop_criteria_val_type=type(val), stop_criteria_val_idx=idx)) + raise TypeError("When the 'stop_criteria' parameter is assigned a tuple/list/numpy.ndarray, then its elements must be strings but the value ({stop_criteria_val}) of type {stop_criteria_val_type} found at index {stop_criteria_val_idx}.".format(stop_criteria_val=val, stop_criteria_val_type=type(val), stop_criteria_val_idx=idx)) else: self.valid_parameters = False - raise TypeError("The expected value of the 'stop_criteria' is a single string or a list/tuple/numpy.ndarray of strings but the value {stop_criteria_val} of type {stop_criteria_type} found.".format(stop_criteria_val=stop_criteria, stop_criteria_type=type(stop_criteria))) + self.logger.error("The expected value of the 'stop_criteria' is a single string or a list/tuple/numpy.ndarray of strings but the value ({stop_criteria_val}) of type {stop_criteria_type} found.".format(stop_criteria_val=stop_criteria, stop_criteria_type=type(stop_criteria))) + raise TypeError("The expected value of the 'stop_criteria' is a single string or a list/tuple/numpy.ndarray of strings but the value ({stop_criteria_val}) of type {stop_criteria_type} found.".format(stop_criteria_val=stop_criteria, stop_criteria_type=type(stop_criteria))) if parallel_processing is None: self.parallel_processing = None @@ -1021,6 +1174,7 @@ def __init__(self, self.parallel_processing = ["thread", parallel_processing] else: self.valid_parameters = False + self.logger.error("When the 'parallel_processing' parameter is assigned an integer, then the integer must be positive but the value ({parallel_processing_value}) found.".format(parallel_processing_value=parallel_processing)) raise ValueError("When the 'parallel_processing' parameter is assigned an integer, then the integer must be positive but the value ({parallel_processing_value}) found.".format(parallel_processing_value=parallel_processing)) elif type(parallel_processing) in [list, tuple]: if len(parallel_processing) == 2: @@ -1035,18 +1189,23 @@ def __init__(self, self.parallel_processing = parallel_processing else: self.valid_parameters = False - raise TypeError("When a list or tuple is assigned to the 'parallel_processing' parameter, then the second element must be an integer but the value ({second_value}) of type ({second_value_type}) found.".format(second_value=parallel_processing[1], second_value_type=type(parallel_processing[1]))) + self.logger.error("When a list or tuple is assigned to the 'parallel_processing' parameter, then the second element must be an integer but the value ({second_value}) of type {second_value_type} found.".format(second_value=parallel_processing[1], second_value_type=type(parallel_processing[1]))) + raise TypeError("When a list or tuple is assigned to the 'parallel_processing' parameter, then the second element must be an integer but the value ({second_value}) of type {second_value_type} found.".format(second_value=parallel_processing[1], second_value_type=type(parallel_processing[1]))) else: self.valid_parameters = False + self.logger.error("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 ({first_value}) found.".format(first_value=parallel_processing[0])) raise ValueError("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 ({first_value}) found.".format(first_value=parallel_processing[0])) else: self.valid_parameters = False - raise TypeError("When a list or tuple is assigned to the 'parallel_processing' parameter, then the first element must be of type 'str' but the value ({first_value}) of type ({first_value_type}) found.".format(first_value=parallel_processing[0], first_value_type=type(parallel_processing[0]))) + self.logger.error("When a list or tuple is assigned to the 'parallel_processing' parameter, then the first element must be of type 'str' but the value ({first_value}) of type {first_value_type} found.".format(first_value=parallel_processing[0], first_value_type=type(parallel_processing[0]))) + raise TypeError("When a list or tuple is assigned to the 'parallel_processing' parameter, then the first element must be of type 'str' but the value ({first_value}) of type {first_value_type} found.".format(first_value=parallel_processing[0], first_value_type=type(parallel_processing[0]))) else: self.valid_parameters = False + self.logger.error("When a list or tuple is assigned to the 'parallel_processing' parameter, then it must have 2 elements but ({num_elements}) found.".format(num_elements=len(parallel_processing))) raise ValueError("When a list or tuple is assigned to the 'parallel_processing' parameter, then it must have 2 elements but ({num_elements}) found.".format(num_elements=len(parallel_processing))) else: self.valid_parameters = False + self.logger.error("Unexpected value ({parallel_processing_value}) of type ({parallel_processing_type}) 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.".format(parallel_processing_value=parallel_processing, parallel_processing_type=type(parallel_processing))) raise ValueError("Unexpected value ({parallel_processing_value}) of type ({parallel_processing_type}) 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.".format(parallel_processing_value=parallel_processing, parallel_processing_type=type(parallel_processing))) # Set the `run_completed` property to False. It is set to `True` only after the `run()` method is complete. @@ -1098,7 +1257,12 @@ def round_genes(self, solutions): self.gene_type[gene_idx][1]) return solutions - def initialize_population(self, low, high, allow_duplicate_genes, mutation_by_replacement, gene_type): + def initialize_population(self, + low, + high, + allow_duplicate_genes, + mutation_by_replacement, + gene_type): """ Creates an initial population randomly as a NumPy array. The array is saved in the instance attribute named 'population'. @@ -1137,14 +1301,14 @@ def initialize_population(self, low, high, allow_duplicate_genes, mutation_by_re if allow_duplicate_genes == False: for solution_idx in range(self.population.shape[0]): - # print("Before", self.population[solution_idx]) + # self.logger.info("Before", self.population[solution_idx]) self.population[solution_idx], _, _ = self.solve_duplicate_genes_randomly(solution=self.population[solution_idx], min_val=low, max_val=high, mutation_by_replacement=True, gene_type=gene_type, num_trials=10) - # print("After", self.population[solution_idx]) + # self.logger.info("After", self.population[solution_idx]) elif self.gene_space_nested: if self.gene_type_single == True: @@ -1326,6 +1490,7 @@ def cal_pop_fitness(self): """ if self.valid_parameters == False: + self.logger.error("ERROR calling the cal_pop_fitness() method: \nPlease check the parameters passed while creating an instance of the GA class.\n") 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' @@ -1374,10 +1539,11 @@ def cal_pop_fitness(self): 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(sol, sol_idx) + fitness = self.fitness_func(self, sol, sol_idx) if type(fitness) in GA.supported_int_float_types: pass else: + self.logger.error("The fitness function should return a number but the value {fit_val} of type {fit_type} found.".format(fit_val=fitness, fit_type=type(fitness))) raise ValueError("The fitness function should return a number but the value {fit_val} of type {fit_type} found.".format(fit_val=fitness, fit_type=type(fitness))) else: # Reaching this point means that batch processing is in effect to calculate the fitness values. @@ -1393,7 +1559,7 @@ def cal_pop_fitness(self): # Indices of the solutions to calculate their fitness. solutions_indices = numpy.where(numpy.array(pop_fitness) == "undefined")[0] # Number of batches. - num_batches = numpy.int(numpy.ceil(len(solutions_indices) / self.fitness_batch_size)) + 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 @@ -1401,16 +1567,19 @@ def cal_pop_fitness(self): batch_indices = solutions_indices[batch_first_index:batch_last_index] batch_solutions = self.population[batch_indices, :] - batch_fitness = self.fitness_func(batch_solutions, batch_indices) + batch_fitness = self.fitness_func(self, batch_solutions, batch_indices) if type(batch_fitness) not in [list, tuple, numpy.ndarray]: - raise TypeError("Expected to receive a list, tuple, or numpy.ndarray from the fitness function but the value ({batch_fitness}) of type ({batch_fitness_type}).".format(batch_fitness=batch_fitness, batch_fitness_type=type(batch_fitness))) + self.logger.error("Expected to receive a list, tuple, or numpy.ndarray from the fitness function but the value ({batch_fitness}) of type {batch_fitness_type}.".format(batch_fitness=batch_fitness, batch_fitness_type=type(batch_fitness))) + raise TypeError("Expected to receive a list, tuple, or numpy.ndarray from the fitness function but the value ({batch_fitness}) of type {batch_fitness_type}.".format(batch_fitness=batch_fitness, batch_fitness_type=type(batch_fitness))) elif len(numpy.array(batch_fitness)) != len(batch_indices): + self.logger.error("There is a mismatch between the number of solutions passed to the fitness function ({batch_indices_len}) and the number of fitness values returned ({batch_fitness_len}). They must match.".format(batch_fitness_len=len(batch_fitness), batch_indices_len=len(batch_indices))) raise ValueError("There is a mismatch between the number of solutions passed to the fitness function ({batch_indices_len}) and the number of fitness values returned ({batch_fitness_len}). They must match.".format(batch_fitness_len=len(batch_fitness), batch_indices_len=len(batch_indices))) for index, fitness in zip(batch_indices, batch_fitness): if type(fitness) in GA.supported_int_float_types: pop_fitness[index] = fitness else: + self.logger.error("The fitness function should return a number but the value {fit_val} of type {fit_type} found.".format(fit_val=fitness, fit_type=type(fitness))) raise ValueError("The fitness function should return a number but the value {fit_val} of type {fit_type} found.".format(fit_val=fitness, fit_type=type(fitness))) else: # Calculating the fitness value of each solution in the current population. @@ -1436,7 +1605,7 @@ def cal_pop_fitness(self): # 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.last_generation_parents is not None) and (len(self.last_generation_parents) > 0) and (list(sol) in last_generation_parents_as_list): + 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] @@ -1466,16 +1635,17 @@ def cal_pop_fitness(self): # 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, solutions_to_submit, solutions_to_submit_indices)): + 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: pop_fitness[index] = fitness else: + self.logger.error("The fitness function should return a number but the value {fit_val} of type {fit_type} found.".format(fit_val=fitness, fit_type=type(fitness))) raise ValueError("The fitness function should return a number but the value {fit_val} of type {fit_type} found.".format(fit_val=fitness, fit_type=type(fitness))) else: # Reaching this block means that batch processing is used. The fitness values are calculated in batches. # Number of batches. - num_batches = numpy.int(numpy.ceil(len(solutions_to_submit_indices) / self.fitness_batch_size)) + 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. @@ -1490,17 +1660,20 @@ def cal_pop_fitness(self): batches_solutions.append(batch_solutions) batches_indices.append(batch_indices) - for batch_indices, batch_fitness in zip(batches_indices, executor.map(self.fitness_func, batches_solutions, batches_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("Expected to receive a list, tuple, or numpy.ndarray from the fitness function but the value ({batch_fitness}) of type ({batch_fitness_type}).".format(batch_fitness=batch_fitness, batch_fitness_type=type(batch_fitness))) + self.logger.error("Expected to receive a list, tuple, or numpy.ndarray from the fitness function but the value ({batch_fitness}) of type {batch_fitness_type}.".format(batch_fitness=batch_fitness, batch_fitness_type=type(batch_fitness))) + raise TypeError("Expected to receive a list, tuple, or numpy.ndarray from the fitness function but the value ({batch_fitness}) of type {batch_fitness_type}.".format(batch_fitness=batch_fitness, batch_fitness_type=type(batch_fitness))) elif len(numpy.array(batch_fitness)) != len(batch_indices): + self.logger.error("There is a mismatch between the number of solutions passed to the fitness function ({batch_indices_len}) and the number of fitness values returned ({batch_fitness_len}). They must match.".format(batch_fitness_len=len(batch_fitness), batch_indices_len=len(batch_indices))) raise ValueError("There is a mismatch between the number of solutions passed to the fitness function ({batch_indices_len}) and the number of fitness values returned ({batch_fitness_len}). They must match.".format(batch_fitness_len=len(batch_fitness), batch_indices_len=len(batch_indices))) for index, fitness in zip(batch_indices, batch_fitness): if type(fitness) in GA.supported_int_float_types: pop_fitness[index] = fitness else: - raise ValueError("The fitness function should return a number but the value {fit_val} of type {fit_type} found.".format(fit_val=fitness, fit_type=type(fitness))) + self.logger.error("The fitness function should return a number but the value ({fit_val}) of type {fit_type} found.".format(fit_val=fitness, fit_type=type(fitness))) + raise ValueError("The fitness function should return a number but the value ({fit_val}) of type {fit_type} found.".format(fit_val=fitness, fit_type=type(fitness))) pop_fitness = numpy.array(pop_fitness) @@ -1513,6 +1686,7 @@ def run(self): """ if self.valid_parameters == False: + self.logger.error("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") 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. @@ -1573,24 +1747,33 @@ def run(self): # 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) + self.last_generation_parents, self.last_generation_parents_indices = self.select_parents(self, + self.last_generation_fitness, + self.num_parents_mating, self) if not type(self.last_generation_parents) is numpy.ndarray: + self.logger.error("The type of the iterable holding the selected parents is expected to be (numpy.ndarray) but {last_generation_parents_type} found.".format(last_generation_parents_type=type(self.last_generation_parents))) raise TypeError("The type of the iterable holding the selected parents is expected to be (numpy.ndarray) but {last_generation_parents_type} found.".format(last_generation_parents_type=type(self.last_generation_parents))) if not type(self.last_generation_parents_indices) is numpy.ndarray: + self.logger.error("The type of the iterable holding the selected parents' indices is expected to be (numpy.ndarray) but {last_generation_parents_indices_type} found.".format(last_generation_parents_indices_type=type(self.last_generation_parents_indices))) raise TypeError("The type of the iterable holding the selected parents' indices is expected to be (numpy.ndarray) but {last_generation_parents_indices_type} found.".format(last_generation_parents_indices_type=type(self.last_generation_parents_indices))) else: - self.last_generation_parents, self.last_generation_parents_indices = self.select_parents(self.last_generation_fitness, num_parents=self.num_parents_mating) + 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: + self.logger.error("Size mismatch between the size of the selected parents {parents_size_actual} and the expected size {parents_size_expected}. It is expected to select ({num_parents_mating}) parents but ({num_parents_mating_selected}) selected.".format(parents_size_actual=self.last_generation_parents.shape, parents_size_expected=(self.num_parents_mating, self.num_genes), num_parents_mating=self.num_parents_mating, num_parents_mating_selected=self.last_generation_parents.shape[0])) raise ValueError("Size mismatch between the size of the selected parents {parents_size_actual} and the expected size {parents_size_expected}. It is expected to select ({num_parents_mating}) parents but ({num_parents_mating_selected}) selected.".format(parents_size_actual=self.last_generation_parents.shape, parents_size_expected=(self.num_parents_mating, self.num_genes), num_parents_mating=self.num_parents_mating, num_parents_mating_selected=self.last_generation_parents.shape[0])) elif self.last_generation_parents.shape[1] != self.num_genes: + self.logger.error("Size mismatch between the size of the selected parents {parents_size_actual} and the expected size {parents_size_expected}. Parents are expected to have ({num_genes}) genes but ({num_genes_selected}) produced.".format(parents_size_actual=self.last_generation_parents.shape, parents_size_expected=(self.num_parents_mating, self.num_genes), num_genes=self.num_genes, num_genes_selected=self.last_generation_parents.shape[1])) raise ValueError("Size mismatch between the size of the selected parents {parents_size_actual} and the expected size {parents_size_expected}. Parents are expected to have ({num_genes}) genes but ({num_genes_selected}) produced.".format(parents_size_actual=self.last_generation_parents.shape, parents_size_expected=(self.num_parents_mating, self.num_genes), num_genes=self.num_genes, num_genes_selected=self.last_generation_parents.shape[1])) if self.last_generation_parents_indices.ndim != 1: + self.logger.error("The iterable holding the selected parents indices is expected to have 1 dimension but ({parents_indices_ndim}) found.".format(parents_indices_ndim=len(self.last_generation_parents_indices))) raise ValueError("The iterable holding the selected parents indices is expected to have 1 dimension but ({parents_indices_ndim}) found.".format(parents_indices_ndim=len(self.last_generation_parents_indices))) elif len(self.last_generation_parents_indices) != self.num_parents_mating: + self.logger.error("The iterable holding the selected parents indices is expected to have ({num_parents_mating}) values but ({num_parents_mating_selected}) found.".format(num_parents_mating=self.num_parents_mating, num_parents_mating_selected=len(self.last_generation_parents_indices))) raise ValueError("The iterable holding the selected parents indices is expected to have ({num_parents_mating}) values but ({num_parents_mating_selected}) found.".format(num_parents_mating=self.num_parents_mating, num_parents_mating_selected=len(self.last_generation_parents_indices))) if not (self.on_parents is None): @@ -1605,9 +1788,10 @@ def run(self): 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() method is called to select the best solutions (i.e. elitism). The keep_elitism parameter defines the number of these solutions. - # The steady_state_selection() method 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) + # 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: @@ -1619,14 +1803,17 @@ def run(self): (self.num_offspring, self.num_genes), self) if not type(self.last_generation_offspring_crossover) is numpy.ndarray: + self.logger.error("The output of the crossover step is expected to be of type (numpy.ndarray) but {last_generation_offspring_crossover_type} found.".format(last_generation_offspring_crossover_type=type(self.last_generation_offspring_crossover))) raise TypeError("The output of the crossover step is expected to be of type (numpy.ndarray) but {last_generation_offspring_crossover_type} found.".format(last_generation_offspring_crossover_type=type(self.last_generation_offspring_crossover))) 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: + self.logger.error("Size mismatch between the crossover output {crossover_actual_size} and the expected crossover output {crossover_expected_size}. It is expected to produce ({num_offspring}) offspring but ({num_offspring_produced}) produced.".format(crossover_actual_size=self.last_generation_offspring_crossover.shape, crossover_expected_size=(self.num_offspring, self.num_genes), num_offspring=self.num_offspring, num_offspring_produced=self.last_generation_offspring_crossover.shape[0])) raise ValueError("Size mismatch between the crossover output {crossover_actual_size} and the expected crossover output {crossover_expected_size}. It is expected to produce ({num_offspring}) offspring but ({num_offspring_produced}) produced.".format(crossover_actual_size=self.last_generation_offspring_crossover.shape, crossover_expected_size=(self.num_offspring, self.num_genes), num_offspring=self.num_offspring, num_offspring_produced=self.last_generation_offspring_crossover.shape[0])) elif self.last_generation_offspring_crossover.shape[1] != self.num_genes: + self.logger.error("Size mismatch between the crossover output {crossover_actual_size} and the expected crossover output {crossover_expected_size}. It is expected that the offspring has ({num_genes}) genes but ({num_genes_produced}) produced.".format(crossover_actual_size=self.last_generation_offspring_crossover.shape, crossover_expected_size=(self.num_offspring, self.num_genes), num_genes=self.num_genes, num_genes_produced=self.last_generation_offspring_crossover.shape[1])) raise ValueError("Size mismatch between the crossover output {crossover_actual_size} and the expected crossover output {crossover_expected_size}. It is expected that the offspring has ({num_genes}) genes but ({num_genes_produced}) produced.".format(crossover_actual_size=self.last_generation_offspring_crossover.shape, crossover_expected_size=(self.num_offspring, self.num_genes), num_genes=self.num_genes, num_genes_produced=self.last_generation_offspring_crossover.shape[1])) # PyGAD 2.18.2 // The on_crossover() callback function is called even if crossover_type is None. @@ -1639,16 +1826,20 @@ def run(self): 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) + self.last_generation_offspring_mutation = self.mutation(self.last_generation_offspring_crossover, + self) if not type(self.last_generation_offspring_mutation) is numpy.ndarray: + self.logger.error("The output of the mutation step is expected to be of type (numpy.ndarray) but {last_generation_offspring_mutation_type} found.".format(last_generation_offspring_mutation_type=type(self.last_generation_offspring_mutation))) raise TypeError("The output of the mutation step is expected to be of type (numpy.ndarray) but {last_generation_offspring_mutation_type} found.".format(last_generation_offspring_mutation_type=type(self.last_generation_offspring_mutation))) 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: + self.logger.error("Size mismatch between the mutation output {mutation_actual_size} and the expected mutation output {mutation_expected_size}. It is expected to produce ({num_offspring}) offspring but ({num_offspring_produced}) produced.".format(mutation_actual_size=self.last_generation_offspring_mutation.shape, mutation_expected_size=(self.num_offspring, self.num_genes), num_offspring=self.num_offspring, num_offspring_produced=self.last_generation_offspring_mutation.shape[0])) raise ValueError("Size mismatch between the mutation output {mutation_actual_size} and the expected mutation output {mutation_expected_size}. It is expected to produce ({num_offspring}) offspring but ({num_offspring_produced}) produced.".format(mutation_actual_size=self.last_generation_offspring_mutation.shape, mutation_expected_size=(self.num_offspring, self.num_genes), num_offspring=self.num_offspring, num_offspring_produced=self.last_generation_offspring_mutation.shape[0])) elif self.last_generation_offspring_mutation.shape[1] != self.num_genes: + self.logger.error("Size mismatch between the mutation output {mutation_actual_size} and the expected mutation output {mutation_expected_size}. It is expected that the offspring has ({num_genes}) genes but ({num_genes_produced}) produced.".format(mutation_actual_size=self.last_generation_offspring_mutation.shape, mutation_expected_size=(self.num_offspring, self.num_genes), num_genes=self.num_genes, num_genes_produced=self.last_generation_offspring_mutation.shape[1])) raise ValueError("Size mismatch between the mutation output {mutation_actual_size} and the expected mutation output {mutation_expected_size}. It is expected that the offspring has ({num_genes}) genes but ({num_genes_produced}) produced.".format(mutation_actual_size=self.last_generation_offspring_mutation.shape, mutation_expected_size=(self.num_offspring, self.num_genes), num_genes=self.num_genes, num_genes_produced=self.last_generation_offspring_mutation.shape[1])) # PyGAD 2.18.2 // The on_mutation() callback function is called even if mutation_type is None. @@ -1665,11 +1856,13 @@ def run(self): 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) + 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.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 @@ -1685,7 +1878,7 @@ def run(self): if self.save_best_solutions: self.best_solutions.append(best_solution) - # If the callback_generation attribute is not None, then cal the callback function after the generation. + # 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": @@ -1738,2339 +1931,274 @@ def run(self): # Converting the 'solutions' list into a NumPy array. # self.solutions = numpy.array(self.solutions) - def steady_state_selection(self, fitness, num_parents): - - """ - Selects the parents using the steady-state selection technique. Later, these parents will mate to produce the offspring. - It accepts 2 parameters: - -fitness: The fitness values of the solutions in the current population. - -num_parents: The number of parents to be selected. - It returns an array of the selected parents. - """ - - fitness_sorted = sorted(range(len(fitness)), key=lambda k: fitness[k]) - fitness_sorted.reverse() - # 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) - 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]) - - def rank_selection(self, fitness, num_parents): - - """ - Selects the parents using the rank selection technique. Later, these parents will mate to produce the offspring. - It accepts 2 parameters: - -fitness: The fitness values of the solutions in the current population. - -num_parents: The number of parents to be selected. - It returns an array of the selected parents. - """ - - fitness_sorted = sorted(range(len(fitness)), key=lambda k: fitness[k]) - fitness_sorted.reverse() - # 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) - 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]) - - def random_selection(self, fitness, num_parents): + def best_solution(self, pop_fitness=None): """ - Selects the parents randomly. Later, these parents will mate to produce the offspring. - It accepts 2 parameters: - -fitness: The fitness values of the solutions in the current population. - -num_parents: The number of parents to be selected. - It returns an array of the selected parents. + 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. """ - if self.gene_type_single == True: - parents = numpy.empty((num_parents, self.population.shape[1]), dtype=self.gene_type[0]) + 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: + self.logger.error("The length of the list/tuple/numpy.ndarray passed to the 'pop_fitness' parameter ({pop_fitness_length}) must match the length of the 'self.population' attribute ({population_length}).".format(pop_fitness_length=len(pop_fitness), population_length=len(self.population))) + raise ValueError("The length of the list/tuple/numpy.ndarray passed to the 'pop_fitness' parameter ({pop_fitness_length}) must match the length of the 'self.population' attribute ({population_length}).".format(pop_fitness_length=len(pop_fitness), population_length=len(self.population))) else: - parents = numpy.empty((num_parents, self.population.shape[1]), dtype=object) + self.logger.error("The type of the 'pop_fitness' parameter is expected to be list, tuple, or numpy.ndarray but ({pop_fitness_type}) found.".format(pop_fitness_type=type(pop_fitness))) + raise ValueError("The type of the 'pop_fitness' parameter is expected to be list, tuple, or numpy.ndarray but ({pop_fitness_type}) found.".format(pop_fitness_type=type(pop_fitness))) - rand_indices = numpy.random.randint(low=0.0, high=fitness.shape[0], size=num_parents) + # Return the index of the best solution that has the best fitness value. + best_match_idx = numpy.where(pop_fitness == numpy.max(pop_fitness))[0][0] - for parent_num in range(num_parents): - parents[parent_num, :] = self.population[rand_indices[parent_num], :].copy() + best_solution = self.population[best_match_idx, :].copy() + best_solution_fitness = pop_fitness[best_match_idx] - return parents, rand_indices + return best_solution, best_solution_fitness, best_match_idx - def tournament_selection(self, fitness, num_parents): + def save(self, filename): """ - Selects the parents using the tournament selection technique. Later, these parents will mate to produce the offspring. - It accepts 2 parameters: - -fitness: The fitness values of the solutions in the current population. - -num_parents: The number of parents to be selected. - It returns an array of the selected parents. + Saves the genetic algorithm instance: + -filename: Name of the file to save the instance. No extension is needed. """ - 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_indices = [] - - for parent_num in range(num_parents): - rand_indices = numpy.random.randint(low=0.0, high=len(fitness), size=self.K_tournament) - K_fitnesses = fitness[rand_indices] - selected_parent_idx = numpy.where(K_fitnesses == numpy.max(K_fitnesses))[0][0] - parents_indices.append(rand_indices[selected_parent_idx]) - parents[parent_num, :] = self.population[rand_indices[selected_parent_idx], :].copy() - - return parents, numpy.array(parents_indices) - - def roulette_wheel_selection(self, fitness, num_parents): + cloudpickle_serialized_object = cloudpickle.dumps(self) + with open(filename + ".pkl", 'wb') as file: + 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): """ - Selects the parents using the roulette wheel selection technique. Later, these parents will mate to produce the offspring. - It accepts 2 parameters: - -fitness: The fitness values of the solutions in the current population. - -num_parents: The number of parents to be selected. - It returns an array of the selected parents. + 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. """ - fitness_sum = numpy.sum(fitness) - if fitness_sum == 0: - raise ZeroDivisionError("Cannot proceed because the sum of fitness values is zero. Cannot divide by zero.") - probs = fitness / fitness_sum - probs_start = numpy.zeros(probs.shape, dtype=float) # An array holding the start values of the ranges of probabilities. - probs_end = numpy.zeros(probs.shape, dtype=float) # An array holding the end values of the ranges of probabilities. - - curr = 0.0 - - # Calculating the probabilities of the solutions to form a roulette wheel. - for _ in range(probs.shape[0]): - min_probs_idx = numpy.where(probs == numpy.min(probs))[0][0] - probs_start[min_probs_idx] = curr - curr = curr + probs[min_probs_idx] - probs_end[min_probs_idx] = curr - probs[min_probs_idx] = 99999999999 - - # 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_indices = [] - - for parent_num in range(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) - - def stochastic_universal_selection(self, fitness, num_parents): + summary_output = "" - """ - Selects the parents using the stochastic universal selection technique. Later, these parents will mate to produce the offspring. - It accepts 2 parameters: - -fitness: The fitness values of the solutions in the current population. - -num_parents: The number of parents to be selected. - It returns an array of the selected parents. - """ + 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 - fitness_sum = numpy.sum(fitness) - if fitness_sum == 0: - raise ZeroDivisionError("Cannot proceed because the sum of fitness values is zero. Cannot divide by zero.") - probs = fitness / fitness_sum - probs_start = numpy.zeros(probs.shape, dtype=float) # An array holding the start values of the ranges of probabilities. - probs_end = numpy.zeros(probs.shape, dtype=float) # An array holding the end values of the ranges of probabilities. - - curr = 0.0 - - # Calculating the probabilities of the solutions to form a roulette wheel. - for _ in range(probs.shape[0]): - min_probs_idx = numpy.where(probs == numpy.min(probs))[0][0] - probs_start[min_probs_idx] = curr - curr = curr + probs[min_probs_idx] - probs_end[min_probs_idx] = curr - probs[min_probs_idx] = 99999999999 - - pointers_distance = 1.0 / self.num_parents_mating # Distance between different pointers. - first_pointer = numpy.random.uniform(low=0.0, high=pointers_distance, size=1) # 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_indices = [] - - for parent_num in range(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) + 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 single_point_crossover(self, parents, offspring_size): + def create_row(columns, line_length=line_length, fill_character=fill_character, split_percentages=None): + filled_columns = [] + if split_percentages == 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_column = column + fill_character * extra_characters + filled_columns.append(filled_column) - """ - Applies the single-point crossover. It selects a point randomly at which crossover takes place between the pairs of parents. - It accepts 2 parameters: - -parents: The parents to mate for producing the offspring. - -offspring_size: The size of the offspring to produce. - It returns an array the produced offspring. - """ + return "".join(filled_columns) - if self.gene_type_single == True: - offspring = numpy.empty(offspring_size, dtype=self.gene_type[0]) - else: - offspring = numpy.empty(offspring_size, dtype=object) - - for k in range(offspring_size[0]): - # The point at which crossover takes place between two parents. Usually, it is at the center. - crossover_point = numpy.random.randint(low=0, high=parents.shape[1], size=1)[0] - - if not (self.crossover_probability is None): - probs = numpy.random.random(size=parents.shape[0]) - indices = numpy.where(probs <= self.crossover_probability)[0] - - # If no parent satisfied the probability, no crossover is applied and a parent is selected. - if len(indices) == 0: - offspring[k, :] = parents[k % parents.shape[0], :] - continue - elif len(indices) == 1: - parent1_idx = indices[0] - parent2_idx = parent1_idx - else: - indices = random.sample(list(set(indices)), 2) - parent1_idx = indices[0] - parent2_idx = indices[1] - else: - # Index of the first parent to mate. - parent1_idx = k % parents.shape[0] - # Index of the second parent to mate. - parent2_idx = (k+1) % parents.shape[0] - - # The new offspring has its first half of its genes from the first parent. - offspring[k, 0:crossover_point] = parents[parent1_idx, 0:crossover_point] - # The new offspring has its second half of its genes from the second parent. - offspring[k, crossover_point:] = parents[parent2_idx, crossover_point:] - - if self.allow_duplicate_genes == False: - if self.gene_space is None: - offspring[k], _, _ = self.solve_duplicate_genes_randomly(solution=offspring[k], - min_val=self.random_mutation_min_val, - max_val=self.random_mutation_max_val, - mutation_by_replacement=self.mutation_by_replacement, - gene_type=self.gene_type, - num_trials=10) - else: - offspring[k], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[k], - gene_type=self.gene_type, - num_trials=10) + def print_parent_selection_params(): + nonlocal summary_output + m = "Number of Parents: {num_parents_mating}".format(num_parents_mating=self.num_parents_mating) + self.logger.info(m) + summary_output = summary_output + m + "\n" + if self.parent_selection_type == "tournament": + m = "K Tournament: {K_tournament}".format(K_tournament=self.K_tournament) + self.logger.info(m) + summary_output = summary_output + m + "\n" - return offspring + def print_fitness_params(): + nonlocal summary_output + if not self.fitness_batch_size is None: + m = "Fitness batch size: {fitness_batch_size}".format(fitness_batch_size=self.fitness_batch_size) + self.logger.info(m) + summary_output = summary_output + m + "\n" - def two_points_crossover(self, parents, offspring_size): + def print_crossover_params(): + nonlocal summary_output + if not self.crossover_probability is None: + m = "Crossover probability: {crossover_probability}".format(crossover_probability=self.crossover_probability) + self.logger.info(m) + summary_output = summary_output + m + "\n" - """ - Applies the 2 points crossover. It selects the 2 points randomly at which crossover takes place between the pairs of parents. - It accepts 2 parameters: - -parents: The parents to mate for producing the offspring. - -offspring_size: The size of the offspring to produce. - It returns an array the produced offspring. - """ + def print_mutation_params(): + nonlocal summary_output + if not self.mutation_probability is None: + m = "Mutation Probability: {mutation_probability}".format(mutation_probability=self.mutation_probability) + self.logger.info(m) + summary_output = summary_output + m + "\n" + if self.mutation_percent_genes == "default": + m = "Mutation Percentage: {mutation_percent_genes}".format(mutation_percent_genes=self.mutation_percent_genes) + self.logger.info(m) + summary_output = summary_output + m + "\n" + # Number of mutation genes is already showed above. + m = "Mutation Genes: {mutation_num_genes}".format(mutation_num_genes=self.mutation_num_genes) + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = "Random Mutation Range: ({random_mutation_min_val}, {random_mutation_max_val})".format(random_mutation_min_val=self.random_mutation_min_val, random_mutation_max_val=self.random_mutation_max_val) + self.logger.info(m) + summary_output = summary_output + m + "\n" + if not self.gene_space is None: + m = "Gene Space: {gene_space}".format(gene_space=self.gene_space) + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = "Mutation by Replacement: {mutation_by_replacement}".format(mutation_by_replacement=self.mutation_by_replacement) + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = "Allow Duplicated Genes: {allow_duplicate_genes}".format(allow_duplicate_genes=self.allow_duplicate_genes) + self.logger.info(m) + summary_output = summary_output + m + "\n" - if self.gene_type_single == True: - offspring = numpy.empty(offspring_size, dtype=self.gene_type[0]) - else: - offspring = numpy.empty(offspring_size, dtype=object) + def print_on_generation_params(): + nonlocal summary_output + if not self.stop_criteria is None: + m = "Stop Criteria: {stop_criteria}".format(stop_criteria=self.stop_criteria) + self.logger.info(m) + summary_output = summary_output + m + "\n" - for k in range(offspring_size[0]): - if (parents.shape[1] == 1): # If the chromosome has only a single gene. In this case, this gene is copied from the second parent. - crossover_point1 = 0 - else: - crossover_point1 = numpy.random.randint(low=0, high=numpy.ceil(parents.shape[1]/2 + 1), size=1)[0] - - crossover_point2 = crossover_point1 + int(parents.shape[1]/2) # The second point must always be greater than the first point. - - if not (self.crossover_probability is None): - probs = numpy.random.random(size=parents.shape[0]) - indices = numpy.where(probs <= self.crossover_probability)[0] - - # If no parent satisfied the probability, no crossover is applied and a parent is selected. - if len(indices) == 0: - offspring[k, :] = parents[k % parents.shape[0], :] - continue - elif len(indices) == 1: - parent1_idx = indices[0] - parent2_idx = parent1_idx - else: - indices = random.sample(list(set(indices)), 2) - parent1_idx = indices[0] - parent2_idx = indices[1] - else: - # Index of the first parent to mate. - parent1_idx = k % parents.shape[0] - # Index of the second parent to mate. - parent2_idx = (k+1) % parents.shape[0] - - # The genes from the beginning of the chromosome up to the first point are copied from the first parent. - offspring[k, 0:crossover_point1] = parents[parent1_idx, 0:crossover_point1] - # The genes from the second point up to the end of the chromosome are copied from the first parent. - offspring[k, crossover_point2:] = parents[parent1_idx, crossover_point2:] - # The genes between the 2 points are copied from the second parent. - offspring[k, crossover_point1:crossover_point2] = parents[parent2_idx, crossover_point1:crossover_point2] - - if self.allow_duplicate_genes == False: - if self.gene_space is None: - offspring[k], _, _ = self.solve_duplicate_genes_randomly(solution=offspring[k], - min_val=self.random_mutation_min_val, - max_val=self.random_mutation_max_val, - mutation_by_replacement=self.mutation_by_replacement, - gene_type=self.gene_type, - num_trials=10) - else: - offspring[k], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[k], - gene_type=self.gene_type, - num_trials=10) - return offspring + def print_params_summary(): + nonlocal summary_output + m = "Population Size: ({sol_per_pop}, {num_genes})".format(sol_per_pop=self.sol_per_pop, num_genes=self.num_genes) + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = "Number of Generations: {num_generations}".format(num_generations=self.num_generations) + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = "Initial Population Range: ({init_range_low}, {init_range_high})".format(init_range_low=self.init_range_low, init_range_high=self.init_range_high) + self.logger.info(m) + summary_output = summary_output + m + "\n" - def uniform_crossover(self, parents, offspring_size): + if not print_step_parameters: + print_fitness_params() - """ - 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. - It accepts 2 parameters: - -parents: The parents to mate for producing the offspring. - -offspring_size: The size of the offspring to produce. - It returns an array the produced offspring. - """ + if not print_step_parameters: + print_parent_selection_params() - if self.gene_type_single == True: - offspring = numpy.empty(offspring_size, dtype=self.gene_type[0]) - else: - offspring = numpy.empty(offspring_size, dtype=object) - - for k in range(offspring_size[0]): - if not (self.crossover_probability is None): - probs = numpy.random.random(size=parents.shape[0]) - indices = numpy.where(probs <= self.crossover_probability)[0] - - # If no parent satisfied the probability, no crossover is applied and a parent is selected. - if len(indices) == 0: - offspring[k, :] = parents[k % parents.shape[0], :] - continue - elif len(indices) == 1: - parent1_idx = indices[0] - parent2_idx = parent1_idx - else: - indices = random.sample(list(set(indices)), 2) - parent1_idx = indices[0] - parent2_idx = indices[1] + if self.keep_elitism != 0: + m = "Keep Elitism: {keep_elitism}".format(keep_elitism=self.keep_elitism) + self.logger.info(m) + summary_output = summary_output + m + "\n" else: - # Index of the first parent to mate. - parent1_idx = k % parents.shape[0] - # Index of the second parent to mate. - parent2_idx = (k+1) % parents.shape[0] - - genes_source = numpy.random.randint(low=0, high=2, size=offspring_size[1]) - for gene_idx in range(offspring_size[1]): - if (genes_source[gene_idx] == 0): - # The gene will be copied from the first parent if the current gene index is 0. - offspring[k, gene_idx] = parents[parent1_idx, gene_idx] - elif (genes_source[gene_idx] == 1): - # The gene will be copied from the second parent if the current gene index is 1. - offspring[k, gene_idx] = parents[parent2_idx, gene_idx] - - if self.allow_duplicate_genes == False: - if self.gene_space is None: - offspring[k], _, _ = self.solve_duplicate_genes_randomly(solution=offspring[k], - min_val=self.random_mutation_min_val, - max_val=self.random_mutation_max_val, - mutation_by_replacement=self.mutation_by_replacement, - gene_type=self.gene_type, - num_trials=10) - else: - offspring[k], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[k], - gene_type=self.gene_type, - num_trials=10) + m = "Keep Parents: {keep_parents}".format(keep_parents=self.keep_parents) + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = "Gene DType: {gene_type}".format(gene_type=self.gene_type) + self.logger.info(m) + summary_output = summary_output + m + "\n" - return offspring - - def scattered_crossover(self, parents, offspring_size): + if not print_step_parameters: + print_crossover_params() - """ - Applies the scattered crossover. It randomly selects the gene from one of the 2 parents. - It accepts 2 parameters: - -parents: The parents to mate for producing the offspring. - -offspring_size: The size of the offspring to produce. - It returns an array the produced offspring. - """ + if not print_step_parameters: + print_mutation_params() - if self.gene_type_single == True: - offspring = numpy.empty(offspring_size, dtype=self.gene_type[0]) - else: - offspring = numpy.empty(offspring_size, dtype=object) - - for k in range(offspring_size[0]): - if not (self.crossover_probability is None): - probs = numpy.random.random(size=parents.shape[0]) - indices = numpy.where(probs <= self.crossover_probability)[0] - - # If no parent satisfied the probability, no crossover is applied and a parent is selected. - if len(indices) == 0: - offspring[k, :] = parents[k % parents.shape[0], :] - continue - elif len(indices) == 1: - parent1_idx = indices[0] - parent2_idx = parent1_idx - else: - indices = random.sample(list(set(indices)), 2) - parent1_idx = indices[0] - parent2_idx = indices[1] - else: - # Index of the first parent to mate. - parent1_idx = k % parents.shape[0] - # Index of the second parent to mate. - parent2_idx = (k+1) % parents.shape[0] - - # A 0/1 vector where 0 means the gene is taken from the first parent and 1 means the gene is taken from the second parent. - gene_sources = numpy.random.randint(0, 2, size=self.num_genes) - offspring[k, :] = numpy.where(gene_sources == 0, parents[parent1_idx, :], parents[parent2_idx, :]) - - if self.allow_duplicate_genes == False: - if self.gene_space is None: - offspring[k], _, _ = self.solve_duplicate_genes_randomly(solution=offspring[k], - min_val=self.random_mutation_min_val, - max_val=self.random_mutation_max_val, - mutation_by_replacement=self.mutation_by_replacement, - gene_type=self.gene_type, - num_trials=10) - else: - offspring[k], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[k], - gene_type=self.gene_type, - num_trials=10) - return offspring + if self.delay_after_gen != 0: + m = "Post-Generation Delay: {delay_after_gen}".format(delay_after_gen=self.delay_after_gen) + self.logger.info(m) + summary_output = summary_output + m + "\n" - def random_mutation(self, offspring): + if not print_step_parameters: + print_on_generation_params() - """ - Applies the random mutation which changes the values of a number of genes randomly. - The random value is selected either using the 'gene_space' parameter or the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring. - """ + if not self.parallel_processing is None: + m = "Parallel Processing: {parallel_processing}".format(parallel_processing=self.parallel_processing) + self.logger.info(m) + summary_output = summary_output + m + "\n" + if not self.random_seed is None: + m = "Random Seed: {random_seed}".format(random_seed=self.random_seed) + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = "Save Best Solutions: {save_best_solutions}".format(save_best_solutions=self.save_best_solutions) + self.logger.info(m) + summary_output = summary_output + m + "\n" + m = "Save Solutions: {save_solutions}".format(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" - # If the mutation values are selected from the mutation space, the attribute 'gene_space' is not None. Otherwise, it is None. - # When the 'mutation_probability' parameter exists (i.e. not None), then it is used in the mutation. Otherwise, the 'mutation_num_genes' parameter is used. + 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", "({num_parents_mating}, {num_genes})".format(num_parents_mating=self.num_parents_mating, num_genes=self.num_genes), "None", "({num_parents_mating}, {num_genes})".format(num_parents_mating=self.num_parents_mating, num_genes=self.num_genes), "None", "({num_parents_mating}, {num_genes})".format(num_parents_mating=self.num_parents_mating, num_genes=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 self.mutation_probability is None: - # When the 'mutation_probability' parameter does not exist (i.e. None), then the parameter 'mutation_num_genes' is used in the mutation. - if not (self.gene_space is None): - # When the attribute 'gene_space' exists (i.e. not None), the mutation values are selected randomly from the space of values of each gene. - offspring = self.mutation_by_space(offspring) - else: - offspring = self.mutation_randomly(offspring) + 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: - # When the 'mutation_probability' parameter exists (i.e. not None), then it is used in the mutation. - if not (self.gene_space is None): - # When the attribute 'gene_space' does not exist (i.e. None), the mutation values are selected randomly based on the continuous range specified by the 2 attributes 'random_mutation_min_val' and 'random_mutation_max_val'. - offspring = self.mutation_probs_by_space(offspring) - else: - offspring = self.mutation_probs_randomly(offspring) - - return offspring - - def mutation_by_space(self, offspring): - - """ - Applies the random mutation using the mutation values' space. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring using the mutation space. - """ - - # For each offspring, a value from the gene space is selected randomly and assigned to the selected mutated gene. - for offspring_idx in range(offspring.shape[0]): - mutation_indices = numpy.array(random.sample(range(0, self.num_genes), self.mutation_num_genes)) - for gene_idx in mutation_indices: - - if self.gene_space_nested: - # Returning the current gene space from the 'gene_space' attribute. - if type(self.gene_space[gene_idx]) in [numpy.ndarray, list]: - curr_gene_space = self.gene_space[gene_idx].copy() - else: - curr_gene_space = self.gene_space[gene_idx] - - # If the gene space has only a single value, use it as the new gene value. - if type(curr_gene_space) in GA.supported_int_float_types: - value_from_space = curr_gene_space - # If the gene space is None, apply mutation by adding a random value between the range defined by the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'. - elif curr_gene_space is None: - rand_val = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - if self.mutation_by_replacement: - value_from_space = rand_val - else: - value_from_space = offspring[offspring_idx, gene_idx] + rand_val - elif type(curr_gene_space) is dict: - # The gene's space of type dict specifies the lower and upper limits of a gene. - if 'step' in curr_gene_space.keys(): - value_from_space = numpy.random.choice(numpy.arange(start=curr_gene_space['low'], - stop=curr_gene_space['high'], - step=curr_gene_space['step']), - size=1) - else: - value_from_space = numpy.random.uniform(low=curr_gene_space['low'], - high=curr_gene_space['high'], - size=1) - else: - # Selecting a value randomly based on the current gene's space in the 'gene_space' attribute. - # If the gene space has only 1 value, then select it. The old and new values of the gene are identical. - if len(curr_gene_space) == 1: - value_from_space = curr_gene_space[0] - # If the gene space has more than 1 value, then select a new one that is different from the current value. - else: - values_to_select_from = list(set(curr_gene_space) - set([offspring[offspring_idx, gene_idx]])) - - if len(values_to_select_from) == 0: - value_from_space = offspring[offspring_idx, gene_idx] - else: - value_from_space = random.choice(values_to_select_from) - else: - # Selecting a value randomly from the global gene space in the 'gene_space' attribute. - if type(self.gene_space) is dict: - # When the gene_space is assigned a dict object, then it specifies the lower and upper limits of all genes in the space. - if 'step' in self.gene_space.keys(): - value_from_space = numpy.random.choice(numpy.arange(start=self.gene_space['low'], - stop=self.gene_space['high'], - step=self.gene_space['step']), - size=1) - else: - value_from_space = numpy.random.uniform(low=self.gene_space['low'], - high=self.gene_space['high'], - size=1) - else: - # If the space type is not of type dict, then a value is randomly selected from the gene_space attribute. - values_to_select_from = list(set(self.gene_space) - set([offspring[offspring_idx, gene_idx]])) - - if len(values_to_select_from) == 0: - value_from_space = offspring[offspring_idx, gene_idx] - else: - value_from_space = random.choice(values_to_select_from) - # value_from_space = random.choice(self.gene_space) - - if value_from_space is None: - value_from_space = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - - # Assinging 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) - - if self.allow_duplicate_genes == False: - offspring[offspring_idx], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[offspring_idx], - gene_type=self.gene_type, - num_trials=10) - return offspring - - def mutation_probs_by_space(self, offspring): - - """ - Applies the random mutation using the mutation values' space and the mutation probability. For each gene, if its probability is <= that mutation probability, then it will be mutated based on the mutation space. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring using the mutation space. - """ - - # For each offspring, a value from the gene space is selected randomly and assigned to the selected mutated gene. - for offspring_idx in range(offspring.shape[0]): - probs = numpy.random.random(size=offspring.shape[1]) - for gene_idx in range(offspring.shape[1]): - if probs[gene_idx] <= self.mutation_probability: - if self.gene_space_nested: - # Returning the current gene space from the 'gene_space' attribute. - if type(self.gene_space[gene_idx]) in [numpy.ndarray, list]: - curr_gene_space = self.gene_space[gene_idx].copy() - else: - curr_gene_space = self.gene_space[gene_idx] - - # If the gene space has only a single value, use it as the new gene value. - if type(curr_gene_space) in GA.supported_int_float_types: - value_from_space = curr_gene_space - # If the gene space is None, apply mutation by adding a random value between the range defined by the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'. - elif curr_gene_space is None: - rand_val = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - if self.mutation_by_replacement: - value_from_space = rand_val - else: - value_from_space = offspring[offspring_idx, gene_idx] + rand_val - elif type(curr_gene_space) is dict: - # Selecting a value randomly from the current gene's space in the 'gene_space' attribute. - if 'step' in curr_gene_space.keys(): - value_from_space = numpy.random.choice(numpy.arange(start=curr_gene_space['low'], - stop=curr_gene_space['high'], - step=curr_gene_space['step']), - size=1) - else: - value_from_space = numpy.random.uniform(low=curr_gene_space['low'], - high=curr_gene_space['high'], - size=1) - else: - # Selecting a value randomly from the current gene's space in the 'gene_space' attribute. - # If the gene space has only 1 value, then select it. The old and new values of the gene are identical. - if len(curr_gene_space) == 1: - value_from_space = curr_gene_space[0] - # If the gene space has more than 1 value, then select a new one that is different from the current value. - else: - values_to_select_from = list(set(curr_gene_space) - set([offspring[offspring_idx, gene_idx]])) - - if len(values_to_select_from) == 0: - value_from_space = offspring[offspring_idx, gene_idx] - else: - value_from_space = random.choice(values_to_select_from) - else: - # Selecting a value randomly from the global gene space in the 'gene_space' attribute. - if type(self.gene_space) is dict: - if 'step' in self.gene_space.keys(): - value_from_space = numpy.random.choice(numpy.arange(start=self.gene_space['low'], - stop=self.gene_space['high'], - step=self.gene_space['step']), - size=1) - else: - value_from_space = numpy.random.uniform(low=self.gene_space['low'], - high=self.gene_space['high'], - size=1) - else: - values_to_select_from = list(set(self.gene_space) - set([offspring[offspring_idx, gene_idx]])) - - if len(values_to_select_from) == 0: - value_from_space = offspring[offspring_idx, gene_idx] - else: - value_from_space = random.choice(values_to_select_from) - - # 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) - - if self.allow_duplicate_genes == False: - offspring[offspring_idx], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[offspring_idx], - gene_type=self.gene_type, - num_trials=10) - return offspring + split_percentages = None - def mutation_randomly(self, offspring): + 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" - """ - Applies the random mutation the mutation probability. For each gene, if its probability is <= that mutation probability, then it will be mutated randomly. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring. - """ + 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" - # Random mutation changes one or more genes in each offspring randomly. - for offspring_idx in range(offspring.shape[0]): - mutation_indices = numpy.array(random.sample(range(0, self.num_genes), self.mutation_num_genes)) - for gene_idx in mutation_indices: - # Generating a random value. - random_value = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - # If the mutation_by_replacement attribute is True, then the random value replaces the current gene value. - if self.mutation_by_replacement: - if self.gene_type_single == True: - random_value = self.gene_type[0](random_value) - else: - random_value = self.gene_type[gene_idx][0](random_value) - if type(random_value) is numpy.ndarray: - random_value = random_value[0] - # If the mutation_by_replacement attribute is False, then the random value is added to the gene value. - else: - if self.gene_type_single == True: - random_value = self.gene_type[0](offspring[offspring_idx, gene_idx] + random_value) - else: - random_value = self.gene_type[gene_idx][0](offspring[offspring_idx, gene_idx] + random_value) - if type(random_value) is numpy.ndarray: - random_value = random_value[0] - - # Round the gene - if self.gene_type_single == True: - if not self.gene_type[1] is None: - random_value = numpy.round(random_value, self.gene_type[1]) - else: - if not self.gene_type[gene_idx][1] is None: - random_value = numpy.round(random_value, self.gene_type[gene_idx][1]) - - offspring[offspring_idx, gene_idx] = random_value - - if self.allow_duplicate_genes == False: - offspring[offspring_idx], _, _ = self.solve_duplicate_genes_randomly(solution=offspring[offspring_idx], - min_val=self.random_mutation_min_val, - max_val=self.random_mutation_max_val, - mutation_by_replacement=self.mutation_by_replacement, - gene_type=self.gene_type, - num_trials=10) - - return offspring - - def mutation_probs_randomly(self, offspring): - - """ - Applies the random mutation using the mutation probability. For each gene, if its probability is <= that mutation probability, then it will be mutated randomly. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring. - """ - - # Random mutation changes one or more gene in each offspring randomly. - for offspring_idx in range(offspring.shape[0]): - probs = numpy.random.random(size=offspring.shape[1]) - for gene_idx in range(offspring.shape[1]): - if probs[gene_idx] <= self.mutation_probability: - # Generating a random value. - random_value = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - # If the mutation_by_replacement attribute is True, then the random value replaces the current gene value. - if self.mutation_by_replacement: - if self.gene_type_single == True: - random_value = self.gene_type[0](random_value) - else: - random_value = self.gene_type[gene_idx][0](random_value) - if type(random_value) is numpy.ndarray: - random_value = random_value[0] - # If the mutation_by_replacement attribute is False, then the random value is added to the gene value. - else: - if self.gene_type_single == True: - random_value = self.gene_type[0](offspring[offspring_idx, gene_idx] + random_value) - else: - random_value = self.gene_type[gene_idx][0](offspring[offspring_idx, gene_idx] + random_value) - if type(random_value) is numpy.ndarray: - random_value = random_value[0] - - # Round the gene - if self.gene_type_single == True: - if not self.gene_type[1] is None: - random_value = numpy.round(random_value, self.gene_type[1]) - else: - if not self.gene_type[gene_idx][1] is None: - random_value = numpy.round(random_value, self.gene_type[gene_idx][1]) - - offspring[offspring_idx, gene_idx] = random_value - - if self.allow_duplicate_genes == False: - offspring[offspring_idx], _, _ = self.solve_duplicate_genes_randomly(solution=offspring[offspring_idx], - min_val=self.random_mutation_min_val, - max_val=self.random_mutation_max_val, - mutation_by_replacement=self.mutation_by_replacement, - gene_type=self.gene_type, - num_trials=10) - return offspring - - def swap_mutation(self, offspring): - - """ - Applies the swap mutation which interchanges the values of 2 randomly selected genes. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring. - """ - - for idx in range(offspring.shape[0]): - mutation_gene1 = numpy.random.randint(low=0, high=offspring.shape[1]/2, size=1)[0] - mutation_gene2 = mutation_gene1 + int(offspring.shape[1]/2) - - temp = offspring[idx, mutation_gene1] - offspring[idx, mutation_gene1] = offspring[idx, mutation_gene2] - offspring[idx, mutation_gene2] = temp - return offspring - - def inversion_mutation(self, offspring): - - """ - Applies the inversion mutation which selects a subset of genes and inverts them (in order). - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring. - """ - - for idx in range(offspring.shape[0]): - mutation_gene1 = numpy.random.randint(low=0, high=numpy.ceil(offspring.shape[1]/2 + 1), size=1)[0] - mutation_gene2 = mutation_gene1 + int(offspring.shape[1]/2) - - genes_to_scramble = numpy.flip(offspring[idx, mutation_gene1:mutation_gene2]) - offspring[idx, mutation_gene1:mutation_gene2] = genes_to_scramble - return offspring - - def scramble_mutation(self, offspring): - - """ - Applies the scramble mutation which selects a subset of genes and shuffles their order randomly. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring. - """ - - for idx in range(offspring.shape[0]): - mutation_gene1 = numpy.random.randint(low=0, high=numpy.ceil(offspring.shape[1]/2 + 1), size=1)[0] - mutation_gene2 = mutation_gene1 + int(offspring.shape[1]/2) - genes_range = numpy.arange(start=mutation_gene1, stop=mutation_gene2) - numpy.random.shuffle(genes_range) - - genes_to_scramble = numpy.flip(offspring[idx, genes_range]) - offspring[idx, genes_range] = genes_to_scramble - return offspring - - def adaptive_mutation_population_fitness(self, offspring): - - """ - A helper method to calculate the average fitness of the solutions before applying the adaptive mutation. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns the average fitness to be used in adaptive mutation. - """ - - fitness = self.last_generation_fitness.copy() - temp_population = numpy.zeros_like(self.population) - - if (self.keep_elitism == 0): - if (self.keep_parents == 0): - parents_to_keep = [] - elif (self.keep_parents == -1): - parents_to_keep = self.last_generation_parents.copy() - temp_population[0:len(parents_to_keep), :] = parents_to_keep - elif (self.keep_parents > 0): - parents_to_keep, _ = self.steady_state_selection(self.last_generation_fitness, num_parents=self.keep_parents) - temp_population[0:len(parents_to_keep), :] = parents_to_keep - else: - parents_to_keep, _ = self.steady_state_selection(self.last_generation_fitness, num_parents=self.keep_elitism) - temp_population[0:len(parents_to_keep), :] = parents_to_keep - - temp_population[len(parents_to_keep):, :] = offspring - - fitness[:self.last_generation_parents.shape[0]] = self.last_generation_fitness[self.last_generation_parents_indices] - - for idx in range(len(parents_to_keep), fitness.shape[0]): - fitness[idx] = self.fitness_func(temp_population[idx], None) - average_fitness = numpy.mean(fitness) - - return average_fitness, fitness[len(parents_to_keep):] - - def adaptive_mutation(self, offspring): - - """ - Applies the adaptive mutation which changes the values of a number of genes randomly. In adaptive mutation, the number of genes to mutate differs based on the fitness value of the solution. - The random value is selected either using the 'gene_space' parameter or the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring. - """ - - # If the attribute 'gene_space' exists (i.e. not None), then the mutation values are selected from the 'gene_space' parameter according to the space of values of each gene. Otherwise, it is selected randomly based on the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'. - # When the 'mutation_probability' parameter exists (i.e. not None), then it is used in the mutation. Otherwise, the 'mutation_num_genes' parameter is used. - - if self.mutation_probability is None: - # When the 'mutation_probability' parameter does not exist (i.e. None), then the parameter 'mutation_num_genes' is used in the mutation. - if not (self.gene_space is None): - # When the attribute 'gene_space' exists (i.e. not None), the mutation values are selected randomly from the space of values of each gene. - offspring = self.adaptive_mutation_by_space(offspring) - else: - # When the attribute 'gene_space' does not exist (i.e. None), the mutation values are selected randomly based on the continuous range specified by the 2 attributes 'random_mutation_min_val' and 'random_mutation_max_val'. - offspring = self.adaptive_mutation_randomly(offspring) - else: - # When the 'mutation_probability' parameter exists (i.e. not None), then it is used in the mutation. - if not (self.gene_space is None): - # When the attribute 'gene_space' exists (i.e. not None), the mutation values are selected randomly from the space of values of each gene. - offspring = self.adaptive_mutation_probs_by_space(offspring) - else: - # When the attribute 'gene_space' does not exist (i.e. None), the mutation values are selected randomly based on the continuous range specified by the 2 attributes 'random_mutation_min_val' and 'random_mutation_max_val'. - offspring = self.adaptive_mutation_probs_randomly(offspring) - - return offspring - - def adaptive_mutation_by_space(self, offspring): - - """ - Applies the adaptive mutation based on the 2 parameters 'mutation_num_genes' and 'gene_space'. - A number of genes equal are selected randomly for mutation. This number depends on the fitness of the solution. - The random values are selected from the 'gene_space' parameter. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring. - """ - - # For each offspring, a value from the gene space is selected randomly and assigned to the selected gene for mutation. - - average_fitness, offspring_fitness = self.adaptive_mutation_population_fitness(offspring) - - # Adaptive mutation changes one or more genes in each offspring randomly. - # The number of genes to mutate depends on the solution's fitness value. - for offspring_idx in range(offspring.shape[0]): - if offspring_fitness[offspring_idx] < average_fitness: - adaptive_mutation_num_genes = self.mutation_num_genes[0] - else: - adaptive_mutation_num_genes = self.mutation_num_genes[1] - mutation_indices = numpy.array(random.sample(range(0, self.num_genes), adaptive_mutation_num_genes)) - for gene_idx in mutation_indices: - - if self.gene_space_nested: - # Returning the current gene space from the 'gene_space' attribute. - if type(self.gene_space[gene_idx]) in [numpy.ndarray, list]: - curr_gene_space = self.gene_space[gene_idx].copy() - else: - curr_gene_space = self.gene_space[gene_idx] - - # If the gene space has only a single value, use it as the new gene value. - if type(curr_gene_space) in GA.supported_int_float_types: - value_from_space = curr_gene_space - # If the gene space is None, apply mutation by adding a random value between the range defined by the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'. - elif curr_gene_space is None: - rand_val = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - if self.mutation_by_replacement: - value_from_space = rand_val - else: - value_from_space = offspring[offspring_idx, gene_idx] + rand_val - elif type(curr_gene_space) is dict: - # Selecting a value randomly from the current gene's space in the 'gene_space' attribute. - if 'step' in curr_gene_space.keys(): - value_from_space = numpy.random.choice(numpy.arange(start=curr_gene_space['low'], - stop=curr_gene_space['high'], - step=curr_gene_space['step']), - size=1) - else: - value_from_space = numpy.random.uniform(low=curr_gene_space['low'], - high=curr_gene_space['high'], - size=1) - else: - # Selecting a value randomly from the current gene's space in the 'gene_space' attribute. - # If the gene space has only 1 value, then select it. The old and new values of the gene are identical. - if len(curr_gene_space) == 1: - value_from_space = curr_gene_space[0] - # If the gene space has more than 1 value, then select a new one that is different from the current value. - else: - values_to_select_from = list(set(curr_gene_space) - set([offspring[offspring_idx, gene_idx]])) - - if len(values_to_select_from) == 0: - value_from_space = offspring[offspring_idx, gene_idx] - else: - value_from_space = random.choice(values_to_select_from) - else: - # Selecting a value randomly from the global gene space in the 'gene_space' attribute. - if type(self.gene_space) is dict: - if 'step' in self.gene_space.keys(): - value_from_space = numpy.random.choice(numpy.arange(start=self.gene_space['low'], - stop=self.gene_space['high'], - step=self.gene_space['step']), - size=1) - else: - value_from_space = numpy.random.uniform(low=self.gene_space['low'], - high=self.gene_space['high'], - size=1) - else: - values_to_select_from = list(set(self.gene_space) - set([offspring[offspring_idx, gene_idx]])) - - if len(values_to_select_from) == 0: - value_from_space = offspring[offspring_idx, gene_idx] - else: - value_from_space = random.choice(values_to_select_from) - - - if value_from_space is None: - value_from_space = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - - # Assinging 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) - - if self.allow_duplicate_genes == False: - offspring[offspring_idx], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[offspring_idx], - gene_type=self.gene_type, - num_trials=10) - return offspring - - def adaptive_mutation_randomly(self, offspring): - - """ - Applies the adaptive mutation based on the 'mutation_num_genes' parameter. - A number of genes equal are selected randomly for mutation. This number depends on the fitness of the solution. - The random values are selected based on the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring. - """ - - average_fitness, offspring_fitness = self.adaptive_mutation_population_fitness(offspring) - - # Adaptive random mutation changes one or more genes in each offspring randomly. - # The number of genes to mutate depends on the solution's fitness value. - for offspring_idx in range(offspring.shape[0]): - if offspring_fitness[offspring_idx] < average_fitness: - adaptive_mutation_num_genes = self.mutation_num_genes[0] - else: - adaptive_mutation_num_genes = self.mutation_num_genes[1] - mutation_indices = numpy.array(random.sample(range(0, self.num_genes), adaptive_mutation_num_genes)) - for gene_idx in mutation_indices: - # Generating a random value. - random_value = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - # If the mutation_by_replacement attribute is True, then the random value replaces the current gene value. - if self.mutation_by_replacement: - if self.gene_type_single == True: - random_value = self.gene_type[0](random_value) - else: - random_value = self.gene_type[gene_idx][0](random_value) - if type(random_value) is numpy.ndarray: - random_value = random_value[0] - # If the mutation_by_replacement attribute is False, then the random value is added to the gene value. - else: - if self.gene_type_single == True: - random_value = self.gene_type[0](offspring[offspring_idx, gene_idx] + random_value) - else: - random_value = self.gene_type[gene_idx][0](offspring[offspring_idx, gene_idx] + random_value) - if type(random_value) is numpy.ndarray: - random_value = random_value[0] - - if self.gene_type_single == True: - if not self.gene_type[1] is None: - random_value = numpy.round(random_value, self.gene_type[1]) - else: - if not self.gene_type[gene_idx][1] is None: - random_value = numpy.round(random_value, self.gene_type[gene_idx][1]) - - offspring[offspring_idx, gene_idx] = random_value - - if self.allow_duplicate_genes == False: - offspring[offspring_idx], _, _ = self.solve_duplicate_genes_randomly(solution=offspring[offspring_idx], - min_val=self.random_mutation_min_val, - max_val=self.random_mutation_max_val, - mutation_by_replacement=self.mutation_by_replacement, - gene_type=self.gene_type, - num_trials=10) - return offspring - - def adaptive_mutation_probs_by_space(self, offspring): - - """ - Applies the adaptive mutation based on the 2 parameters 'mutation_probability' and 'gene_space'. - Based on whether the solution fitness is above or below a threshold, the mutation is applied diffrently by mutating high or low number of genes. - The random values are selected based on space of values for each gene. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring. - """ - - # For each offspring, a value from the gene space is selected randomly and assigned to the selected gene for mutation. - - average_fitness, offspring_fitness = self.adaptive_mutation_population_fitness(offspring) - - # Adaptive random mutation changes one or more genes in each offspring randomly. - # The probability of mutating a gene depends on the solution's fitness value. - for offspring_idx in range(offspring.shape[0]): - if offspring_fitness[offspring_idx] < average_fitness: - adaptive_mutation_probability = self.mutation_probability[0] - else: - adaptive_mutation_probability = self.mutation_probability[1] - - probs = numpy.random.random(size=offspring.shape[1]) - for gene_idx in range(offspring.shape[1]): - if probs[gene_idx] <= adaptive_mutation_probability: - if self.gene_space_nested: - # Returning the current gene space from the 'gene_space' attribute. - if type(self.gene_space[gene_idx]) in [numpy.ndarray, list]: - curr_gene_space = self.gene_space[gene_idx].copy() - else: - curr_gene_space = self.gene_space[gene_idx] - - # If the gene space has only a single value, use it as the new gene value. - if type(curr_gene_space) in GA.supported_int_float_types: - value_from_space = curr_gene_space - # If the gene space is None, apply mutation by adding a random value between the range defined by the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'. - elif curr_gene_space is None: - rand_val = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - if self.mutation_by_replacement: - value_from_space = rand_val - else: - value_from_space = offspring[offspring_idx, gene_idx] + rand_val - elif type(curr_gene_space) is dict: - # Selecting a value randomly from the current gene's space in the 'gene_space' attribute. - if 'step' in curr_gene_space.keys(): - value_from_space = numpy.random.choice(numpy.arange(start=curr_gene_space['low'], - stop=curr_gene_space['high'], - step=curr_gene_space['step']), - size=1) - else: - value_from_space = numpy.random.uniform(low=curr_gene_space['low'], - high=curr_gene_space['high'], - size=1) - else: - # Selecting a value randomly from the current gene's space in the 'gene_space' attribute. - # If the gene space has only 1 value, then select it. The old and new values of the gene are identical. - if len(curr_gene_space) == 1: - value_from_space = curr_gene_space[0] - # If the gene space has more than 1 value, then select a new one that is different from the current value. - else: - values_to_select_from = list(set(curr_gene_space) - set([offspring[offspring_idx, gene_idx]])) - - if len(values_to_select_from) == 0: - value_from_space = offspring[offspring_idx, gene_idx] - else: - value_from_space = random.choice(values_to_select_from) - else: - # Selecting a value randomly from the global gene space in the 'gene_space' attribute. - if type(self.gene_space) is dict: - if 'step' in self.gene_space.keys(): - value_from_space = numpy.random.choice(numpy.arange(start=self.gene_space['low'], - stop=self.gene_space['high'], - step=self.gene_space['step']), - size=1) - else: - value_from_space = numpy.random.uniform(low=self.gene_space['low'], - high=self.gene_space['high'], - size=1) - else: - values_to_select_from = list(set(self.gene_space) - set([offspring[offspring_idx, gene_idx]])) - - if len(values_to_select_from) == 0: - value_from_space = offspring[offspring_idx, gene_idx] - else: - value_from_space = random.choice(values_to_select_from) - - if value_from_space is None: - value_from_space = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - - # Assinging 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) - - if self.allow_duplicate_genes == False: - offspring[offspring_idx], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[offspring_idx], - gene_type=self.gene_type, - num_trials=10) - return offspring - - def adaptive_mutation_probs_randomly(self, offspring): - - """ - Applies the adaptive mutation based on the 'mutation_probability' parameter. - Based on whether the solution fitness is above or below a threshold, the mutation is applied diffrently by mutating high or low number of genes. - The random values are selected based on the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'. - It accepts a single parameter: - -offspring: The offspring to mutate. - It returns an array of the mutated offspring. - """ - - average_fitness, offspring_fitness = self.adaptive_mutation_population_fitness(offspring) - - # Adaptive random mutation changes one or more genes in each offspring randomly. - # The probability of mutating a gene depends on the solution's fitness value. - for offspring_idx in range(offspring.shape[0]): - if offspring_fitness[offspring_idx] < average_fitness: - adaptive_mutation_probability = self.mutation_probability[0] - else: - adaptive_mutation_probability = self.mutation_probability[1] - - probs = numpy.random.random(size=offspring.shape[1]) - for gene_idx in range(offspring.shape[1]): - if probs[gene_idx] <= adaptive_mutation_probability: - # Generating a random value. - random_value = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - # If the mutation_by_replacement attribute is True, then the random value replaces the current gene value. - if self.mutation_by_replacement: - if self.gene_type_single == True: - random_value = self.gene_type[0](random_value) - else: - random_value = self.gene_type[gene_idx][0](random_value) - if type(random_value) is numpy.ndarray: - random_value = random_value[0] - # If the mutation_by_replacement attribute is False, then the random value is added to the gene value. - else: - if self.gene_type_single == True: - random_value = self.gene_type[0](offspring[offspring_idx, gene_idx] + random_value) - else: - random_value = self.gene_type[gene_idx][0](offspring[offspring_idx, gene_idx] + random_value) - if type(random_value) is numpy.ndarray: - random_value = random_value[0] - - if self.gene_type_single == True: - if not self.gene_type[1] is None: - random_value = numpy.round(random_value, self.gene_type[1]) - else: - if not self.gene_type[gene_idx][1] is None: - random_value = numpy.round(random_value, self.gene_type[gene_idx][1]) - - offspring[offspring_idx, gene_idx] = random_value - - if self.allow_duplicate_genes == False: - offspring[offspring_idx], _, _ = self.solve_duplicate_genes_randomly(solution=offspring[offspring_idx], - min_val=self.random_mutation_min_val, - max_val=self.random_mutation_max_val, - mutation_by_replacement=self.mutation_by_replacement, - gene_type=self.gene_type, - num_trials=10) - return offspring - - def solve_duplicate_genes_randomly(self, solution, min_val, max_val, mutation_by_replacement, gene_type, num_trials=10): - - """ - Solves the duplicates in a solution by randomly selecting new values for the duplicating genes. - - solution: A solution with duplicate values. - min_val: Minimum value of the range to sample a number randomly. - max_val: Maximum value of the range to sample a number randomly. - mutation_by_replacement: Identical to the self.mutation_by_replacement attribute. - gene_type: Exactly the same as the self.gene_type attribute. - num_trials: Maximum number of trials to change the gene value to solve the duplicates. - - Returns: - new_solution: Solution after trying to solve its duplicates. If no duplicates solved, then it is identical to the passed solution parameter. - not_unique_indices: Indices of the genes with duplicate values. - num_unsolved_duplicates: Number of unsolved duplicates. - """ - - new_solution = solution.copy() - - _, unique_gene_indices = numpy.unique(solution, return_index=True) - not_unique_indices = set(range(len(solution))) - set(unique_gene_indices) - - num_unsolved_duplicates = 0 - if len(not_unique_indices) > 0: - for duplicate_index in not_unique_indices: - for trial_index in range(num_trials): - if self.gene_type_single == True: - if gene_type[0] in GA.supported_int_types: - temp_val = self.unique_int_gene_from_range(solution=new_solution, - gene_index=duplicate_index, - min_val=min_val, - max_val=max_val, - mutation_by_replacement=mutation_by_replacement, - gene_type=gene_type) - else: - temp_val = numpy.random.uniform(low=min_val, - high=max_val, - size=1) - if mutation_by_replacement: - pass - else: - temp_val = new_solution[duplicate_index] + temp_val - else: - if gene_type[duplicate_index] in GA.supported_int_types: - temp_val = self.unique_int_gene_from_range(solution=new_solution, - gene_index=duplicate_index, - min_val=min_val, - max_val=max_val, - mutation_by_replacement=mutation_by_replacement, - gene_type=gene_type) - else: - temp_val = numpy.random.uniform(low=min_val, - high=max_val, - size=1) - if mutation_by_replacement: - pass - else: - temp_val = new_solution[duplicate_index] + temp_val - - if self.gene_type_single == True: - if not gene_type[1] is None: - temp_val = numpy.round(gene_type[0](temp_val), - gene_type[1]) - else: - temp_val = gene_type[0](temp_val) - else: - if not gene_type[duplicate_index][1] is None: - temp_val = numpy.round(gene_type[duplicate_index][0](temp_val), - gene_type[duplicate_index][1]) - else: - temp_val = gene_type[duplicate_index][0](temp_val) - - if temp_val in new_solution and trial_index == (num_trials - 1): - num_unsolved_duplicates = num_unsolved_duplicates + 1 - if not self.suppress_warnings: warnings.warn("Failed to find a unique value for gene with index {gene_idx} whose value is {gene_value}. Consider adding more values in the gene space or use a wider range for initial population or random mutation.".format(gene_idx=duplicate_index, gene_value=solution[duplicate_index])) - elif temp_val in new_solution: - continue - else: - new_solution[duplicate_index] = temp_val - break - - # Update the list of duplicate indices after each iteration. - _, unique_gene_indices = numpy.unique(new_solution, return_index=True) - not_unique_indices = set(range(len(solution))) - set(unique_gene_indices) - # print("not_unique_indices INSIDE", not_unique_indices) - - return new_solution, not_unique_indices, num_unsolved_duplicates - - def solve_duplicate_genes_by_space(self, solution, gene_type, num_trials=10, build_initial_pop=False): - - """ - Solves the duplicates in a solution by selecting values for the duplicating genes from the gene space. - - solution: A solution with duplicate values. - gene_type: Exactly the same as the self.gene_type attribute. - num_trials: Maximum number of trials to change the gene value to solve the duplicates. - - Returns: - new_solution: Solution after trying to solve its duplicates. If no duplicates solved, then it is identical to the passed solution parameter. - not_unique_indices: Indices of the genes with duplicate values. - num_unsolved_duplicates: Number of unsolved duplicates. - """ - - new_solution = solution.copy() - - _, unique_gene_indices = numpy.unique(solution, return_index=True) - not_unique_indices = set(range(len(solution))) - set(unique_gene_indices) - # print("not_unique_indices OUTSIDE", not_unique_indices) - - # First try to solve the duplicates. - # For a solution like [3 2 0 0], the indices of the 2 duplicating genes are 2 and 3. - # The next call to the find_unique_value() method tries to change the value of the gene with index 3 to solve the duplicate. - if len(not_unique_indices) > 0: - new_solution, not_unique_indices, num_unsolved_duplicates = self.unique_genes_by_space(new_solution=new_solution, - gene_type=gene_type, - not_unique_indices=not_unique_indices, - num_trials=10, - build_initial_pop=build_initial_pop) - else: - return new_solution, not_unique_indices, len(not_unique_indices) - - # Do another try if there exist duplicate genes. - # If there are no possible values for the gene 3 with index 3 to solve the duplicate, try to change the value of the other gene with index 2. - if len(not_unique_indices) > 0: - not_unique_indices = set(numpy.where(new_solution == new_solution[list(not_unique_indices)[0]])[0]) - set([list(not_unique_indices)[0]]) - new_solution, not_unique_indices, num_unsolved_duplicates = self.unique_genes_by_space(new_solution=new_solution, - gene_type=gene_type, - not_unique_indices=not_unique_indices, - num_trials=10, - build_initial_pop=build_initial_pop) - else: - # If there exist duplicate genes, then changing either of the 2 duplicating genes (with indices 2 and 3) will not solve the problem. - # This problem can be solved by randomly changing one of the non-duplicating genes that may make a room for a unique value in one the 2 duplicating genes. - # For example, if gene_space=[[3, 0, 1], [4, 1, 2], [0, 2], [3, 2, 0]] and the solution is [3 2 0 0], then the values of the last 2 genes duplicate. - # There are no possible changes in the last 2 genes to solve the problem. But it could be solved by changing the second gene from 2 to 4. - # As a result, any of the last 2 genes can take the value 2 and solve the duplicates. - return new_solution, not_unique_indices, len(not_unique_indices) - - return new_solution, not_unique_indices, num_unsolved_duplicates - - def unique_int_gene_from_range(self, solution, gene_index, min_val, max_val, mutation_by_replacement, gene_type, step=None): - - """ - Finds a unique integer value for the gene. - - solution: A solution with duplicate values. - gene_index: Index of the gene to find a unique value. - min_val: Minimum value of the range to sample a number randomly. - max_val: Maximum value of the range to sample a number randomly. - mutation_by_replacement: Identical to the self.mutation_by_replacement attribute. - gene_type: Exactly the same as the self.gene_type attribute. - - Returns: - selected_value: The new value of the gene. It may be identical to the original gene value in case there are no possible unique values for the gene. - """ - - if self.gene_type_single == True: - if step is None: - all_gene_values = numpy.arange(min_val, max_val, dtype=gene_type[0]) - else: - # For non-integer steps, the numpy.arange() function returns zeros id the dtype parameter is set to an integer data type. So, this returns zeros if step is non-integer and dtype is set to an int data type: numpy.arange(min_val, max_val, step, dtype=gene_type[0]) - # To solve this issue, the data type casting will not be handled inside numpy.arange(). The range is generated by numpy.arange() and then the data type is converted using the numpy.asarray() function. - all_gene_values = numpy.asarray(numpy.arange(min_val, max_val, step), dtype=gene_type[0]) - else: - if step is None: - all_gene_values = numpy.arange(min_val, max_val, dtype=gene_type[gene_index][0]) - else: - all_gene_values = numpy.asarray(numpy.arange(min_val, max_val, step), dtype=gene_type[gene_index][0]) - - if mutation_by_replacement: - pass - else: - all_gene_values = all_gene_values + solution[gene_index] - - if self.gene_type_single == True: - if not gene_type[1] is None: - all_gene_values = numpy.round(gene_type[0](all_gene_values), - gene_type[1]) - else: - if type(all_gene_values) is numpy.ndarray: - all_gene_values = numpy.asarray(all_gene_values, dtype=gene_type[0]) - else: - all_gene_values = gene_type[0](all_gene_values) - else: - if not gene_type[gene_index][1] is None: - all_gene_values = numpy.round(gene_type[gene_index][0](all_gene_values), - gene_type[gene_index][1]) - else: - all_gene_values = gene_type[gene_index][0](all_gene_values) - - values_to_select_from = list(set(all_gene_values) - set(solution)) - - if len(values_to_select_from) == 0: - if not self.suppress_warnings: warnings.warn("You set 'allow_duplicate_genes=False' but there is no enough values to prevent duplicates.") - selected_value = solution[gene_index] - else: - selected_value = random.choice(values_to_select_from) - - #if self.gene_type_single == True: - # selected_value = gene_type[0](selected_value) - #else: - # selected_value = gene_type[gene_index][0](selected_value) - - return selected_value - - def unique_genes_by_space(self, new_solution, gene_type, not_unique_indices, num_trials=10, build_initial_pop=False): - - """ - Loops through all the duplicating genes to find unique values that from their gene spaces to solve the duplicates. - For each duplicating gene, a call to the unique_gene_by_space() is made. - - new_solution: A solution with duplicate values. - gene_type: Exactly the same as the self.gene_type attribute. - not_unique_indices: Indices with duplicating values. - num_trials: Maximum number of trials to change the gene value to solve the duplicates. - - Returns: - new_solution: Solution after trying to solve all of its duplicates. If no duplicates solved, then it is identical to the passed solution parameter. - not_unique_indices: Indices of the genes with duplicate values. - num_unsolved_duplicates: Number of unsolved duplicates. - """ - - num_unsolved_duplicates = 0 - for duplicate_index in not_unique_indices: - for trial_index in range(num_trials): - temp_val = self.unique_gene_by_space(solution=new_solution, - gene_idx=duplicate_index, - gene_type=gene_type, - build_initial_pop=build_initial_pop) - - if temp_val in new_solution and trial_index == (num_trials - 1): - # print("temp_val, duplicate_index", temp_val, duplicate_index, new_solution) - num_unsolved_duplicates = num_unsolved_duplicates + 1 - if not self.suppress_warnings: warnings.warn("Failed to find a unique value for gene with index {gene_idx} whose value is {gene_value}. Consider adding more values in the gene space or use a wider range for initial population or random mutation.".format(gene_idx=duplicate_index, gene_value=new_solution[duplicate_index])) - elif temp_val in new_solution: - continue - else: - new_solution[duplicate_index] = temp_val - # print("SOLVED", duplicate_index) - break - - # Update the list of duplicate indices after each iteration. - _, unique_gene_indices = numpy.unique(new_solution, return_index=True) - not_unique_indices = set(range(len(new_solution))) - set(unique_gene_indices) - # print("not_unique_indices INSIDE", not_unique_indices) - - return new_solution, not_unique_indices, num_unsolved_duplicates - - def unique_gene_by_space(self, solution, gene_idx, gene_type, build_initial_pop=False): - - """ - Returns a unique gene value for a single gene based on its value space to solve the duplicates. - - solution: A solution with duplicate values. - gene_idx: The index of the gene that duplicates its value with another gene. - gene_type: Exactly the same as the self.gene_type attribute. - - Returns: - A unique value, if exists, for the gene. - """ - - if self.gene_space_nested: - # Returning the current gene space from the 'gene_space' attribute. - if type(self.gene_space[gene_idx]) in [numpy.ndarray, list]: - curr_gene_space = self.gene_space[gene_idx].copy() - else: - curr_gene_space = self.gene_space[gene_idx] - - # If the gene space has only a single value, use it as the new gene value. - if type(curr_gene_space) in GA.supported_int_float_types: - value_from_space = curr_gene_space - # If the gene space is None, apply mutation by adding a random value between the range defined by the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'. - elif curr_gene_space is None: - if self.gene_type_single == True: - if gene_type[0] in GA.supported_int_types: - if build_initial_pop == True: - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=self.random_mutation_min_val, - max_val=self.random_mutation_max_val, - mutation_by_replacement=True, - gene_type=gene_type) - else: - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=self.random_mutation_min_val, - max_val=self.random_mutation_max_val, - mutation_by_replacement=True, #self.mutation_by_replacement, - gene_type=gene_type) - else: - value_from_space = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - if self.mutation_by_replacement: - pass - else: - value_from_space = solution[gene_idx] + value_from_space - else: - if gene_type[gene_idx] in GA.supported_int_types: - if build_initial_pop == True: - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=self.random_mutation_min_val, - max_val=self.random_mutation_max_val, - mutation_by_replacement=True, - gene_type=gene_type) - else: - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=self.random_mutation_min_val, - max_val=self.random_mutation_max_val, - mutation_by_replacement=True, #self.mutation_by_replacement, - gene_type=gene_type) - else: - value_from_space = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - if self.mutation_by_replacement: - pass - else: - value_from_space = solution[gene_idx] + value_from_space - - elif type(curr_gene_space) is dict: - if self.gene_type_single == True: - if gene_type[0] in GA.supported_int_types: - if build_initial_pop == True: - if 'step' in curr_gene_space.keys(): - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=curr_gene_space['low'], - max_val=curr_gene_space['high'], - step=curr_gene_space['step'], - mutation_by_replacement=True, - gene_type=gene_type) - else: - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=curr_gene_space['low'], - max_val=curr_gene_space['high'], - step=None, - mutation_by_replacement=True, - gene_type=gene_type) - else: - if 'step' in curr_gene_space.keys(): - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=curr_gene_space['low'], - max_val=curr_gene_space['high'], - step=curr_gene_space['step'], - mutation_by_replacement=True, - gene_type=gene_type) - else: - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=curr_gene_space['low'], - max_val=curr_gene_space['high'], - step=None, - mutation_by_replacement=True, - gene_type=gene_type) - else: - if 'step' in curr_gene_space.keys(): - value_from_space = numpy.random.choice(numpy.arange(start=curr_gene_space['low'], - stop=curr_gene_space['high'], - step=curr_gene_space['step']), - size=1) - else: - value_from_space = numpy.random.uniform(low=curr_gene_space['low'], - high=curr_gene_space['high'], - size=1) - if self.mutation_by_replacement: - pass - else: - value_from_space = solution[gene_idx] + value_from_space - else: - if gene_type[gene_idx] in GA.supported_int_types: - if build_initial_pop == True: - if 'step' in curr_gene_space.keys(): - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=curr_gene_space['low'], - max_val=curr_gene_space['high'], - step=curr_gene_space['step'], - mutation_by_replacement=True, - gene_type=gene_type) - else: - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=curr_gene_space['low'], - max_val=curr_gene_space['high'], - step=None, - mutation_by_replacement=True, - gene_type=gene_type) - else: - if 'step' in curr_gene_space.keys(): - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=curr_gene_space['low'], - max_val=curr_gene_space['high'], - step=curr_gene_space['step'], - mutation_by_replacement=True, - gene_type=gene_type) - else: - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=curr_gene_space['low'], - max_val=curr_gene_space['high'], - step=None, - mutation_by_replacement=True, - gene_type=gene_type) - else: - if 'step' in curr_gene_space.keys(): - value_from_space = numpy.random.choice(numpy.arange(start=curr_gene_space['low'], - stop=curr_gene_space['high'], - step=curr_gene_space['step']), - size=1) - else: - value_from_space = numpy.random.uniform(low=curr_gene_space['low'], - high=curr_gene_space['high'], - size=1) - if self.mutation_by_replacement: - pass - else: - value_from_space = solution[gene_idx] + value_from_space - - else: - # Selecting a value randomly based on the current gene's space in the 'gene_space' attribute. - # If the gene space has only 1 value, then select it. The old and new values of the gene are identical. - if len(curr_gene_space) == 1: - value_from_space = curr_gene_space[0] - if not self.suppress_warnings: warnings.warn("You set 'allow_duplicate_genes=False' but the space of the gene with index {gene_idx} has only a single value. Thus, duplicates are possible.".format(gene_idx=gene_idx)) - # If the gene space has more than 1 value, then select a new one that is different from the current value. - else: - values_to_select_from = list(set(curr_gene_space) - set(solution)) - - if len(values_to_select_from) == 0: - if not self.suppress_warnings: warnings.warn("You set 'allow_duplicate_genes=False' but the gene space does not have enough values to prevent duplicates.") - value_from_space = solution[gene_idx] - else: - value_from_space = random.choice(values_to_select_from) - else: - # Selecting a value randomly from the global gene space in the 'gene_space' attribute. - if type(self.gene_space) is dict: - if self.gene_type_single == True: - if gene_type[0] in GA.supported_int_types: - if build_initial_pop == True: - if 'step' in self.gene_space.keys(): - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=self.gene_space['low'], - max_val=self.gene_space['high'], - step=self.gene_space['step'], - mutation_by_replacement=True, - gene_type=gene_type) - else: - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=self.gene_space['low'], - max_val=self.gene_space['high'], - step=None, - mutation_by_replacement=True, - gene_type=gene_type) - else: - if 'step' in self.gene_space.keys(): - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=self.gene_space['low'], - max_val=self.gene_space['high'], - step=self.gene_space['step'], - mutation_by_replacement=True, - gene_type=gene_type) - else: - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=self.gene_space['low'], - max_val=self.gene_space['high'], - step=None, - mutation_by_replacement=True, - gene_type=gene_type) - else: - # When the gene_space is assigned a dict object, then it specifies the lower and upper limits of all genes in the space. - if 'step' in self.gene_space.keys(): - value_from_space = numpy.random.choice(numpy.arange(start=self.gene_space['low'], - stop=self.gene_space['high'], - step=self.gene_space['step']), - size=1) - else: - value_from_space = numpy.random.uniform(low=self.gene_space['low'], - high=self.gene_space['high'], - size=1) - if self.mutation_by_replacement: - pass - else: - value_from_space = solution[gene_idx] + value_from_space - else: - if gene_type[gene_idx] in GA.supported_int_types: - if build_initial_pop == True: - if 'step' in self.gene_space.keys(): - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=self.gene_space['low'], - max_val=self.gene_space['high'], - step=self.gene_space['step'], - mutation_by_replacement=True, - gene_type=gene_type) - else: - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=self.gene_space['low'], - max_val=self.gene_space['high'], - step=None, - mutation_by_replacement=True, - gene_type=gene_type) - else: - if 'step' in self.gene_space.keys(): - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=self.gene_space['low'], - max_val=self.gene_space['high'], - step=self.gene_space['step'], - mutation_by_replacement=True, - gene_type=gene_type) - else: - value_from_space = self.unique_int_gene_from_range(solution=solution, - gene_index=gene_idx, - min_val=self.gene_space['low'], - max_val=self.gene_space['high'], - step=None, - mutation_by_replacement=True, - gene_type=gene_type) - else: - # When the gene_space is assigned a dict object, then it specifies the lower and upper limits of all genes in the space. - if 'step' in self.gene_space.keys(): - value_from_space = numpy.random.choice(numpy.arange(start=self.gene_space['low'], - stop=self.gene_space['high'], - step=self.gene_space['step']), - size=1) - else: - value_from_space = numpy.random.uniform(low=self.gene_space['low'], - high=self.gene_space['high'], - size=1) - if self.mutation_by_replacement: - pass - else: - value_from_space = solution[gene_idx] + value_from_space - - else: - # If the space type is not of type dict, then a value is randomly selected from the gene_space attribute. - # Remove all the genes in the current solution from the gene_space. - # This only leaves the unique values that could be selected for the gene. - values_to_select_from = list(set(self.gene_space) - set(solution)) - - if len(values_to_select_from) == 0: - if not self.suppress_warnings: warnings.warn("You set 'allow_duplicate_genes=False' but the gene space does not have enough values to prevent duplicates.") - value_from_space = solution[gene_idx] - else: - value_from_space = random.choice(values_to_select_from) - - if value_from_space is None: - value_from_space = numpy.random.uniform(low=self.random_mutation_min_val, - high=self.random_mutation_max_val, - size=1) - - if self.gene_type_single == True: - if not gene_type[1] is None: - value_from_space = numpy.round(gene_type[0](value_from_space), - gene_type[1]) - else: - value_from_space = gene_type[0](value_from_space) - else: - if not gene_type[gene_idx][1] is None: - value_from_space = numpy.round(gene_type[gene_idx][0](value_from_space), - gene_type[gene_idx][1]) - else: - value_from_space = gene_type[gene_idx][0](value_from_space) - - return value_from_space - - 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 current population. If None, then the cal_pop_fitness() method is called to calculate the fitness of the 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. - """ - - # Getting the best solution after finishing all generations. - # At first, the fitness is calculated for each solution in the final generation. - if pop_fitness is None: - pop_fitness = self.cal_pop_fitness() - # Then return the index of that solution corresponding to the best fitness. - best_match_idx = numpy.where(pop_fitness == numpy.max(pop_fitness))[0][0] - - best_solution = self.population[best_match_idx, :].copy() - best_solution_fitness = pop_fitness[best_match_idx] - - return best_solution, best_solution_fitness, best_match_idx - - def plot_result(self, - title="PyGAD - Generation vs. Fitness", - xlabel="Generation", - ylabel="Fitness", - linewidth=3, - font_size=14, - plot_type="plot", - color="#3870FF", - save_dir=None): - - if not self.suppress_warnings: - warnings.warn("Please use the plot_fitness() method instead of plot_result(). The plot_result() method will be removed in the future.") - - return self.plot_fitness(title=title, - xlabel=xlabel, - ylabel=ylabel, - linewidth=linewidth, - font_size=font_size, - plot_type=plot_type, - color=color, - save_dir=save_dir) - - def plot_fitness(self, - title="PyGAD - Generation vs. Fitness", - xlabel="Generation", - ylabel="Fitness", - linewidth=3, - font_size=14, - plot_type="plot", - color="#3870FF", - save_dir=None): - - """ - Creates, shows, and returns a figure that summarizes how the fitness value evolved by generation. Can only be called after completing at least 1 generation. If no generation is completed, an exception is raised. - - Accepts the following: - title: Figure title. - xlabel: Label on the X-axis. - ylabel: Label on the Y-axis. - linewidth: Line width of the plot. Defaults to 3. - font_size: Font size for the labels and title. Defaults to 14. - plot_type: Type of the plot which can be either "plot" (default), "scatter", or "bar". - color: Color of the plot which defaults to "#3870FF". - save_dir: Directory to save the figure. - - Returns the figure. - """ - - if self.generations_completed < 1: - raise RuntimeError("The plot_fitness() (i.e. plot_result()) method can only be called after completing at least 1 generation but ({generations_completed}) is completed.".format(generations_completed=self.generations_completed)) - -# if self.run_completed == False: -# if not self.suppress_warnings: warnings.warn("Warning calling the plot_result() method: \nGA is not executed yet and there are no results to display. Please call the run() method before calling the plot_result() method.\n") - - fig = matplotlib.pyplot.figure() - if plot_type == "plot": - matplotlib.pyplot.plot(self.best_solutions_fitness, linewidth=linewidth, color=color) - elif plot_type == "scatter": - matplotlib.pyplot.scatter(range(len(self.best_solutions_fitness)), self.best_solutions_fitness, linewidth=linewidth, color=color) - elif plot_type == "bar": - matplotlib.pyplot.bar(range(len(self.best_solutions_fitness)), self.best_solutions_fitness, linewidth=linewidth, color=color) - matplotlib.pyplot.title(title, fontsize=font_size) - matplotlib.pyplot.xlabel(xlabel, fontsize=font_size) - matplotlib.pyplot.ylabel(ylabel, fontsize=font_size) - - if not save_dir is None: - matplotlib.pyplot.savefig(fname=save_dir, - bbox_inches='tight') - matplotlib.pyplot.show() - - return fig - - def plot_new_solution_rate(self, - title="PyGAD - Generation vs. New Solution Rate", - xlabel="Generation", - ylabel="New Solution Rate", - linewidth=3, - font_size=14, - plot_type="plot", - color="#3870FF", - save_dir=None): - - """ - Creates, shows, and returns a figure that summarizes the rate of exploring new solutions. This method works only when save_solutions=True in the constructor of the pygad.GA class. - - Accepts the following: - title: Figure title. - xlabel: Label on the X-axis. - ylabel: Label on the Y-axis. - linewidth: Line width of the plot. Defaults to 3. - font_size: Font size for the labels and title. Defaults to 14. - plot_type: Type of the plot which can be either "plot" (default), "scatter", or "bar". - color: Color of the plot which defaults to "#3870FF". - save_dir: Directory to save the figure. - - Returns the figure. - """ - - if self.generations_completed < 1: - raise RuntimeError("The plot_new_solution_rate() method can only be called after completing at least 1 generation but ({generations_completed}) is completed.".format(generations_completed=self.generations_completed)) - - if self.save_solutions == False: - raise RuntimeError("The plot_new_solution_rate() method works only when save_solutions=True in the constructor of the pygad.GA class.") - - unique_solutions = set() - num_unique_solutions_per_generation = [] - for generation_idx in range(self.generations_completed): - - len_before = len(unique_solutions) - - start = generation_idx * self.sol_per_pop - end = start + self.sol_per_pop - - for sol in self.solutions[start:end]: - unique_solutions.add(tuple(sol)) - - len_after = len(unique_solutions) - - generation_num_unique_solutions = len_after - len_before - num_unique_solutions_per_generation.append(generation_num_unique_solutions) - - fig = matplotlib.pyplot.figure() - if plot_type == "plot": - matplotlib.pyplot.plot(num_unique_solutions_per_generation, linewidth=linewidth, color=color) - elif plot_type == "scatter": - matplotlib.pyplot.scatter(range(self.generations_completed), num_unique_solutions_per_generation, linewidth=linewidth, color=color) - elif plot_type == "bar": - matplotlib.pyplot.bar(range(self.generations_completed), num_unique_solutions_per_generation, linewidth=linewidth, color=color) - matplotlib.pyplot.title(title, fontsize=font_size) - matplotlib.pyplot.xlabel(xlabel, fontsize=font_size) - matplotlib.pyplot.ylabel(ylabel, fontsize=font_size) - - if not save_dir is None: - matplotlib.pyplot.savefig(fname=save_dir, - bbox_inches='tight') - matplotlib.pyplot.show() - - return fig - - def plot_genes(self, - title="PyGAD - Gene", - xlabel="Gene", - ylabel="Value", - linewidth=3, - font_size=14, - plot_type="plot", - graph_type="plot", - fill_color="#3870FF", - color="black", - solutions="all", - save_dir=None): - - """ - Creates, shows, and returns a figure with number of subplots equal to the number of genes. Each subplot shows the gene value for each generation. - This method works only when save_solutions=True in the constructor of the pygad.GA class. - It also works only after completing at least 1 generation. If no generation is completed, an exception is raised. - - Accepts the following: - title: Figure title. - xlabel: Label on the X-axis. - ylabel: Label on the Y-axis. - linewidth: Line width of the plot. Defaults to 3. - font_size: Font size for the labels and title. Defaults to 14. - plot_type: Type of the plot which can be either "plot" (default), "scatter", or "bar". - graph_type: Type of the graph which can be either "plot" (default), "boxplot", or "histogram". - fill_color: Fill color of the graph which defaults to "#3870FF". This has no effect if graph_type="plot". - color: Color of the plot which defaults to "black". - solutions: Defaults to "all" which means use all solutions. If "best" then only the best solutions are used. - save_dir: Directory to save the figure. - - Returns the figure. - """ - - if self.generations_completed < 1: - raise RuntimeError("The plot_genes() method can only be called after completing at least 1 generation but ({generations_completed}) is completed.".format(generations_completed=self.generations_completed)) - - if type(solutions) is str: - if solutions == 'all': - if self.save_solutions: - solutions_to_plot = numpy.array(self.solutions) - else: - raise RuntimeError("The plot_genes() method with solutions='all' can only be called if 'save_solutions=True' in the pygad.GA class constructor.") - elif solutions == 'best': - if self.save_best_solutions: - solutions_to_plot = self.best_solutions - else: - raise RuntimeError("The plot_genes() method with solutions='best' can only be called if 'save_best_solutions=True' in the pygad.GA class constructor.") - else: - raise RuntimeError("The solutions parameter can be either 'all' or 'best' but {solutions} found.".format(solutions=solutions)) - else: - raise RuntimeError("The solutions parameter must be a string but {solutions_type} found.".format(solutions_type=type(solutions))) - - if graph_type == "plot": - # num_rows will be always be >= 1 - # num_cols can only be 0 if num_genes=1 - num_rows = int(numpy.ceil(self.num_genes/5.0)) - num_cols = int(numpy.ceil(self.num_genes/num_rows)) - - if num_cols == 0: - figsize = (10, 8) - # There is only a single gene - fig, ax = matplotlib.pyplot.subplots(num_rows, figsize=figsize) - if plot_type == "plot": - ax.plot(solutions_to_plot[:, 0], linewidth=linewidth, color=fill_color) - elif plot_type == "scatter": - ax.scatter(range(self.generations_completed + 1), solutions_to_plot[:, 0], linewidth=linewidth, color=fill_color) - elif plot_type == "bar": - ax.bar(range(self.generations_completed + 1), solutions_to_plot[:, 0], linewidth=linewidth, color=fill_color) - ax.set_xlabel(0, fontsize=font_size) - else: - fig, axs = matplotlib.pyplot.subplots(num_rows, num_cols) - - if num_cols == 1 and num_rows == 1: - fig.set_figwidth(5 * num_cols) - fig.set_figheight(4) - axs.plot(solutions_to_plot[:, 0], linewidth=linewidth, color=fill_color) - axs.set_xlabel("Gene " + str(0), fontsize=font_size) - elif num_cols == 1 or num_rows == 1: - fig.set_figwidth(5 * num_cols) - fig.set_figheight(4) - for gene_idx in range(len(axs)): - if plot_type == "plot": - axs[gene_idx].plot(solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color) - elif plot_type == "scatter": - axs[gene_idx].scatter(range(solutions_to_plot.shape[0]), solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color) - elif plot_type == "bar": - axs[gene_idx].bar(range(solutions_to_plot.shape[0]), solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color) - axs[gene_idx].set_xlabel("Gene " + str(gene_idx), fontsize=font_size) - else: - gene_idx = 0 - fig.set_figwidth(25) - fig.set_figheight(4*num_rows) - for row_idx in range(num_rows): - for col_idx in range(num_cols): - if gene_idx >= self.num_genes: - # axs[row_idx, col_idx].remove() - break - if plot_type == "plot": - axs[row_idx, col_idx].plot(solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color) - elif plot_type == "scatter": - axs[row_idx, col_idx].scatter(range(solutions_to_plot.shape[0]), solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color) - elif plot_type == "bar": - axs[row_idx, col_idx].bar(range(solutions_to_plot.shape[0]), solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color) - axs[row_idx, col_idx].set_xlabel("Gene " + str(gene_idx), fontsize=font_size) - gene_idx += 1 - - fig.suptitle(title, fontsize=font_size, y=1.001) - matplotlib.pyplot.tight_layout() - - elif graph_type == "boxplot": - fig = matplotlib.pyplot.figure(1, figsize=(0.7*self.num_genes, 6)) - - # Create an axes instance - ax = fig.add_subplot(111) - boxeplots = ax.boxplot(solutions_to_plot, - labels=range(self.num_genes), - patch_artist=True) - # adding horizontal grid lines - ax.yaxis.grid(True) - - for box in boxeplots['boxes']: - # change outline color - box.set(color='black', linewidth=linewidth) - # change fill color https://color.adobe.com/create/color-wheel - box.set_facecolor(fill_color) - - for whisker in boxeplots['whiskers']: - whisker.set(color=color, linewidth=linewidth) - for median in boxeplots['medians']: - median.set(color=color, linewidth=linewidth) - for cap in boxeplots['caps']: - cap.set(color=color, linewidth=linewidth) - - matplotlib.pyplot.title(title, fontsize=font_size) - matplotlib.pyplot.xlabel(xlabel, fontsize=font_size) - matplotlib.pyplot.ylabel(ylabel, fontsize=font_size) - matplotlib.pyplot.tight_layout() - - elif graph_type == "histogram": - # num_rows will be always be >= 1 - # num_cols can only be 0 if num_genes=1 - num_rows = int(numpy.ceil(self.num_genes/5.0)) - num_cols = int(numpy.ceil(self.num_genes/num_rows)) - - if num_cols == 0: - figsize = (10, 8) - # There is only a single gene - fig, ax = matplotlib.pyplot.subplots(num_rows, - figsize=figsize) - ax.hist(solutions_to_plot[:, 0], color=fill_color) - ax.set_xlabel(0, fontsize=font_size) - else: - fig, axs = matplotlib.pyplot.subplots(num_rows, num_cols) - - if num_cols == 1 and num_rows == 1: - fig.set_figwidth(4 * num_cols) - fig.set_figheight(3) - axs.hist(solutions_to_plot[:, 0], - color=fill_color, - rwidth=0.95) - axs.set_xlabel("Gene " + str(0), fontsize=font_size) - elif num_cols == 1 or num_rows == 1: - fig.set_figwidth(4 * num_cols) - fig.set_figheight(3) - for gene_idx in range(len(axs)): - axs[gene_idx].hist(solutions_to_plot[:, gene_idx], - color=fill_color, - rwidth=0.95) - axs[gene_idx].set_xlabel("Gene " + str(gene_idx), fontsize=font_size) - else: - gene_idx = 0 - fig.set_figwidth(20) - fig.set_figheight(3*num_rows) - for row_idx in range(num_rows): - for col_idx in range(num_cols): - if gene_idx >= self.num_genes: - # axs[row_idx, col_idx].remove() - break - axs[row_idx, col_idx].hist(solutions_to_plot[:, gene_idx], - color=fill_color, - rwidth=0.95) - axs[row_idx, col_idx].set_xlabel("Gene " + str(gene_idx), fontsize=font_size) - gene_idx += 1 - - fig.suptitle(title, fontsize=font_size, y=1.001) - matplotlib.pyplot.tight_layout() - - if not save_dir is None: - matplotlib.pyplot.savefig(fname=save_dir, - bbox_inches='tight') - - matplotlib.pyplot.show() - - return fig - - def save(self, filename): - - """ - Saves the genetic algorithm instance: - -filename: Name of the file to save the instance. No extension is needed. - """ - - cloudpickle_serialized_object = cloudpickle.dumps(self) - with open(filename + ".pkl", 'wb') as file: - 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. - """ - - 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 == 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_column = column + fill_character * extra_characters - filled_columns.append(filled_column) - - return "".join(filled_columns) - - def print_parent_selection_params(): - print("Number of Parents: {num_parents_mating}".format(num_parents_mating=self.num_parents_mating)) - if self.parent_selection_type == "tournament": - print("K Tournament: {K_tournament}".format(K_tournament=self.K_tournament)) - - def print_fitness_params(): - if not self.fitness_batch_size is None: - print("Fitness batch size: {fitness_batch_size}".format(fitness_batch_size=self.fitness_batch_size)) - - def print_crossover_params(): - if not self.crossover_probability is None: - print("Crossover probability: {crossover_probability}".format(crossover_probability=self.crossover_probability)) - - def print_mutation_params(): - if not self.mutation_probability is None: - print("Mutation Probability: {mutation_probability}".format(mutation_probability=self.mutation_probability)) - if self.mutation_percent_genes == "default": - print("Mutation Percentage: {mutation_percent_genes}".format(mutation_percent_genes=self.mutation_percent_genes)) - # Number of mutation genes is already showed above. - print("Mutation Genes: {mutation_num_genes}".format(mutation_num_genes=self.mutation_num_genes)) - print("Random Mutation Range: ({random_mutation_min_val}, {random_mutation_max_val})".format(random_mutation_min_val=self.random_mutation_min_val, random_mutation_max_val=self.random_mutation_max_val)) - if not self.gene_space is None: - print("Gene Space: {gene_space}".format(gene_space=self.gene_space)) - print("Mutation by Replacement: {mutation_by_replacement}".format(mutation_by_replacement=self.mutation_by_replacement)) - print("Allow Duplicated Genes: {allow_duplicate_genes}".format(allow_duplicate_genes=self.allow_duplicate_genes)) - - def print_on_generation_params(): - if not self.stop_criteria is None: - print("Stop Criteria: {stop_criteria}".format(stop_criteria=self.stop_criteria)) - - def print_params_summary(): - print("Population Size: ({sol_per_pop}, {num_genes})".format(sol_per_pop=self.sol_per_pop, num_genes=self.num_genes)) - print("Number of Generations: {num_generations}".format(num_generations=self.num_generations)) - print("Initial Population Range: ({init_range_low}, {init_range_high})".format(init_range_low=self.init_range_low, init_range_high=self.init_range_high)) - - if not print_step_parameters: - print_fitness_params() - - if not print_step_parameters: - print_parent_selection_params() - - if self.keep_elitism != 0: - print("Keep Elitism: {keep_elitism}".format(keep_elitism=self.keep_elitism)) - else: - print("Keep Parents: {keep_parents}".format(keep_parents=self.keep_parents)) - print("Gene DType: {gene_type}".format(gene_type=self.gene_type)) - - if not print_step_parameters: - print_crossover_params() - - if not print_step_parameters: - print_mutation_params() - - if self.delay_after_gen != 0: - print("Post-Generation Delay: {delay_after_gen}".format(delay_after_gen=self.delay_after_gen)) - - if not print_step_parameters: - print_on_generation_params() - - if not self.parallel_processing is None: - print("Parallel Processing: {parallel_processing}".format(parallel_processing=self.parallel_processing)) - if not self.random_seed is None: - print("Random Seed: {random_seed}".format(random_seed=self.random_seed)) - print("Save Best Solutions: {save_best_solutions}".format(save_best_solutions=self.save_best_solutions)) - print("Save Solutions: {save_solutions}".format(save_solutions=self.save_solutions)) - - print(line_separator(line_character=line_character)) - print(fill_message("PyGAD Lifecycle")) - print(line_separator(line_character=line_character2)) - - 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", "({num_parents_mating}, {num_genes})".format(num_parents_mating=self.num_parents_mating, num_genes=self.num_genes), "None", "({num_parents_mating}, {num_genes})".format(num_parents_mating=self.num_parents_mating, num_genes=self.num_genes), "None", "({num_parents_mating}, {num_genes})".format(num_parents_mating=self.num_parents_mating, num_genes=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) - print(header_row) - print(line_separator(line_character=line_character2)) - - 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) - print(lifecycle_row) - if print_step_parameters: - if not lifecycle_step_parameters[lifecycle_idx] is None: - lifecycle_step_parameters[lifecycle_idx]() - print(line_separator(line_character=line_character)) - - print(line_separator(line_character=line_character2)) + m = line_separator(line_character=line_character2) + self.logger.info(m) + summary_output = summary_output + m + "\n" if print_parameters_summary: print_params_summary() - print(line_separator(line_character=line_character2)) + m = line_separator(line_character=line_character2) + self.logger.info(m) + summary_output = summary_output + m + "\n" + return summary_output def load(filename): @@ -4088,4 +2216,4 @@ def load(filename): except: # raise BaseException("Error loading the file. If the file already exists, please reload all the functions previously used (e.g. fitness function).") raise BaseException("Error loading the file.") - return ga_in \ No newline at end of file + return ga_in diff --git a/setup.py b/setup.py index 1801818..f5bab50 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="pygad", - version="2.19.2", + version="3.0.0", author="Ahmed Fawzy Gad", install_requires=["numpy", "matplotlib", "cloudpickle",], author_email="ahmed.f.gad@gmail.com",