guofei9987 / scikit-opt

Genetic Algorithm, Particle Swarm Optimization, Simulated Annealing, Ant Colony Optimization Algorithm,Immune Algorithm, Artificial Fish Swarm Algorithm, Differential Evolution and TSP(Traveling salesman)
https://scikit-opt.github.io/scikit-opt/#/en/
MIT License
5.27k stars 991 forks source link

Crossover Probability #127

Closed andrecasotti closed 3 years ago

andrecasotti commented 3 years ago

is possible to setup the crossover rate probability in GA?

guofei9987 commented 3 years ago

Now we can setup the crossover probability in GA since version 0.6.5

from sko.GA import GA
from sko.operators import crossover

ga = GA(func=lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + (x[2] - 0.5) ** 2
        , n_dim=3, size_pop=100, max_iter=500, lb=[-1, -10, -5], ub=[2, 10, 2],
        precision=[1e-7, 1e-7, 1])

ga.register(operator_name='crossover', operator=crossover.crossover_2point_prob, crossover_prob=0.5)

best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)

related: #79

andrecasotti commented 3 years ago

ok, tks! and about mutation probability?

guofei9987 commented 3 years ago

ok, tks! and about mutation probability?

ga = GA(***,prob_mut=0.001)

Like this: https://github.com/guofei9987/scikit-opt/blob/b0b7168e9d7aebb2c2998274f314f2bafb942362/examples/demo_ga.py#L17

abc-hy commented 3 years ago

@guofei9987 I tried the codes you provided for crossover probability setting but it cannot work. I got the error message: module sko.operators.crossover has not attribute 'crossover_2point_prob' Why is this? Do I need to update my sko version? Or is there any typo in the codes you provided? How can I make the code work? @andrecasotti Have you tried the codes? Can you set the crossover rate now?

guofei9987 commented 3 years ago

@guofei9987 I tried the codes you provided for crossover probability setting but it cannot work. I got the error message: module sko.operators.crossover has not attribute 'crossover_2point_prob' Why is this? Do I need to update my sko version? Or is there any typo in the codes you provided? How can I make the code work? @andrecasotti Have you tried the codes? Can you set the crossover rate now?

Try this:

pip install --upgrade scikit-opt