MaxHalford / eaopt

:four_leaf_clover: Evolutionary optimization library for Go (genetic algorithm, partical swarm optimization, differential evolution)
https://pkg.go.dev/github.com/MaxHalford/eaopt
MIT License
888 stars 96 forks source link

[tanghaibao] Add support for CrossRate in models #27

Closed tanghaibao closed 6 years ago

tanghaibao commented 6 years ago

@MaxHalford Thanks for the awesome package - I'm adding support to specify crossover rate. Naturally, the MutRate controls how fast one explore new solutions; CrossRate controls how much mixing amongst good solutions.

The old behavior was essentially CrossRate = 1, which, for most applications is too much. One could see this pathological behavior in the example of grid_tsp.go, where it seems to lack the ability to mutate (since crossover is dominating).

This PR allows one to specify CrossRate inside the model.

Haibao

MaxHalford commented 6 years ago

Please correct your code so that the Travis build doesn't fail :)

coveralls commented 6 years ago

Coverage Status

Coverage decreased (-0.4%) to 98.775% when pulling 3120a02c8d773e4cf1cea51566aac5c983c5f518 on tanghaibao:master into cd089b93f0aa84244fe635bb294240a7d6ff904b on MaxHalford:master.

coveralls commented 6 years ago

Coverage Status

Coverage increased (+0.007%) to 99.207% when pulling 47df520fa55fb664d2003ae6137f12d013fc322b on tanghaibao:master into cd089b93f0aa84244fe635bb294240a7d6ff904b on MaxHalford:master.

tanghaibao commented 6 years ago

@MaxHalford While all the tests now pass, I should add that now the default CrossRate (when you don't specify it) is 0. There is a potential risk to break someone else's existing code - when they just initialize the struct without CrossRate. However, this is the same expected as MutRate, when nothing is specified, no mutations. Here when nothing is specified, no crossovers.

Or perhaps you could come up with a cleaner way to deal with defaults here?

Haibao

MaxHalford commented 6 years ago

Okay I'm going to merge this PR. As for the value of the crossover rate that has to be comprised between 0 and 1, that's why I impose each model has a Validate() bool method to ensure the parameters are correct. Go doesn't supports default values!