DEAP / deap

Distributed Evolutionary Algorithms in Python
http://deap.readthedocs.org/
GNU Lesser General Public License v3.0
5.66k stars 1.11k forks source link

Callback Function in Deap #748

Closed PauloDoMonte closed 1 week ago

PauloDoMonte commented 2 weeks ago

Does anyone know of a way to add a callback function within eaSimple. This function would be used to check whether at one time the pop had a certain value and if so, it could terminate the program. It would be another early_stop that is available in keras.

`def def_callback(pop): ...... #What my function does

resultado, log = algorithms.eaSimple(pop, toolbox, cxpb=cxpb, mutpb=mutpb_, halloffame=hof, stats=estatistica, ngen=1000, verbose=True, callback=def_callback)`

fmder commented 2 weeks ago

The recommended way would be to copy the whole eaSimple function in your code and add the part you need. We intentionally avoided callbacks to encourage users to understand entirely the algorithms.

Le sam. 15 juin 2024, 11 h 55, Paulo Do Monte @.***> a écrit :

Does anyone know of a way to add a callback function within eaSimple. This function would be used to check whether at one time the pop had a certain value and if so, it could terminate the program. It would be another early_stop that is available in keras.

`def def_callback(pop): ...... #What my function does

resultado, log = algorithms.eaSimple(pop, toolbox, cxpb=cxpb, mutpb=mutpb_, halloffame=hof, stats=estatistica, ngen=1000, verbose=True, callback=def_callback)`

— Reply to this email directly, view it on GitHub https://github.com/DEAP/deap/issues/748, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHKXQTEB6WNJM3W473MEELZHRPYBAVCNFSM6AAAAABJLZLFYSVHI2DSMVQWIX3LMV43ASLTON2WKOZSGM2TIOJWG44DKMQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

Lucandia commented 2 weeks ago

I understand the philosophy of avoiding callbacks, but in my case, I'd like to do a simple thing: at the end of each generation send the statistics to another function (to write the results on a Streamlit WebApp). In this case, it doesn't really have to do anything to do with understanding the algorithms, it's just to show the results in real-time. Copy/pasting the code is messy, especially if it is the exact same function as the eaSimple, but with a silly callback. I don't think there is a big problem with adding a simple callback option or that would be tricky to implement. I'd like it as a feature

PauloDoMonte commented 2 weeks ago

I wasn't familiar with the philosophy of avoiding callbacks, but I find it interesting. I wish there was a way to still use callbacks, especially for people in the physics field who may not be very familiar with programming. It would make the research process easier since they could spend more time being a physicist and thinking about an analytical or numerical solution, rather than figuring out a solution in terms of algorithms. But I understand your point. I'll look into this algorithm more and work on my own implementation.