You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 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?
@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
Activity
ahmedfgad commentedon 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 commentedon Feb 24, 2023
@ahmedfgad Hej, thanks for your quick reply. Actually, I want to change my
fitness_funcaccording to thega_instance.generations_completed. For example, for the last generation, I want to change some hyperparameters in thefitness_func. Although this could be achieved by calculating the fitness ofga_instance.populationmanually afterga_instance.run()by adjusting a newfitness_function, how can I do it for example, the 2nd or 4th generation in general?whubaichuan commentedon Feb 25, 2023
@ahmedfgad or could you slightly modify the capsulated Pygad-multiprocess function to make the change of
generation_idxcan be passed to thefitness_func()?ahmedfgad commentedon Feb 25, 2023
@whubaichuan,
The
ga_instancewill be passed to the fitness function in the next release. Thank you!whubaichuan commentedon Feb 27, 2023
@ahmedfgad Thanks, looking forward to that.
PyGAD 3.0.0 Release
ahmedfgad commentedon Apr 9, 2023
@whubaichuan, PyGAD 3.0.0 is released and the GA instance is passed to the fitness function/method.
whubaichuan commentedon Apr 10, 2023
@ahmedfgad Thanks a lot.