Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ask for new features about fitness_func #163

Closed
whubaichuan opened this issue Feb 24, 2023 · 7 comments
Closed

ask for new features about fitness_func #163

whubaichuan opened this issue Feb 24, 2023 · 7 comments
Labels
enhancement New feature or request

Comments

@whubaichuan
Copy link

Hi, for function fitness_func(solution,solution_idx):, can you add a new input variable such as fitness_func(solution,solution_idx, ga_instance): or fitness_func(solution,solution_idx, generations_completed):? Because I want to do some different calculate of fitness for different generations.

@ahmedfgad
Copy link
Owner

ahmedfgad commented Feb 24, 2023

Can you use the on_fitness() callback to do your calculations? There the ga_instance is passed.

The fitness of the population will be passed to on_fitness().

@whubaichuan
Copy link
Author

@ahmedfgad Hej, thanks for your quick reply. Actually, I want to change my fitness_func according to the ga_instance.generations_completed. For example, for the last generation, I want to change some hyperparameters in the fitness_func. Although this could be achieved by calculating the fitness of ga_instance.population manually after ga_instance.run() by adjusting a new fitness_function, how can I do it for example, the 2nd or 4th generation in general?

@whubaichuan
Copy link
Author

whubaichuan commented Feb 25, 2023

@ahmedfgad or could you slightly modify the capsulated Pygad-multiprocess function to make the change of generation_idx can be passed to the fitness_func()?

import signal
import time
import sys
import multiprocessing

generation_idx = 0

def change_idx():
    global generation_idx
    generation_idx =generation_idx+ 1
    print(generation_idx)

def fitness_func():
   
    print("generation_idx = {0}\n".format(generation_idx))
    time.sleep(1)

def main():
    for i in range(3):
        change_idx()
        worker = multiprocessing.Process(target=fitness_func, args=())
        worker.start()
        worker.join()


if __name__ == "__main__":
    main()


output:

1
generation_idx = 0

2
generation_idx = 0

3
generation_idx = 0

@ahmedfgad ahmedfgad added the enhancement New feature or request label Feb 25, 2023
@ahmedfgad
Copy link
Owner

@whubaichuan,

The ga_instance will be passed to the fitness function in the next release. Thank you!

@whubaichuan
Copy link
Author

@ahmedfgad Thanks, looking forward to that.

ahmedfgad added a commit that referenced this issue Apr 8, 2023
PyGAD 3.0.0 Release Notes
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. The `pygad.GA` class extends this class.
  3. The `pygad.utils.crossover` module has a class named `Crossover` where all the crossover operators exist. The `pygad.GA` class extends this class.
  4. The `pygad.utils.mutation` module has a class named `Mutation` where all the mutation operators exist. The `pygad.GA` class extends this class.
  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. The `pygad.GA` class extends this class.
  6. The `pygad.visualize.plot` module has a class named `Plot` where all the methods that create plots exist. The `pygad.GA` class extends this class.

```python
...
class GA(utils.parent_selection.ParentSelection,
         utils.crossover.Crossover,
         utils.mutation.Mutation,
         helper.unique.Unique,
         visualize.plot.Plot):
...
```

2. 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](https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#logging-outputs) for more information.
3. A new instance attribute called `logger` to save the logger.
4. 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://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#use-functions-and-methods-to-build-fitness-and-callbacks). #163
5. 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://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#use-functions-and-methods-to-build-fitness-and-callbacks). #92 (comment)
6. Validate the value passed to the `initial_population` parameter.
7. Validate the type and length of the `pop_fitness` parameter of the `best_solution()` method.
8. Some edits in the documentation. #106
9. 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`.
10. The `summary()` method returns the summary as a single-line string. Just log/print the returned string it to see it properly.
11. The `callback_generation` parameter is removed. Use the `on_generation` parameter instead.
12. 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. #145 ahmedfgad/TorchGA#5 ahmedfgad/KerasGA#6. Thanks to this [StackOverflow answer](https://stackoverflow.com/a/75606666/5426539).
13. Replace `numpy.float` by `float` in the 2 parent selection operators roulette wheel and stochastic universal. #168
@ahmedfgad
Copy link
Owner

@whubaichuan, PyGAD 3.0.0 is released and the GA instance is passed to the fitness function/method.

@whubaichuan
Copy link
Author

@ahmedfgad Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants