DEAP / deap

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

Inaccuracy in PSO example? #685

Open mariosky opened 1 year ago

mariosky commented 1 year ago

There is an inaccuracy on the PSO example on the smin and smax parameters. When the parameters are used in the constructor, they are similar to pmin and pmax:

toolbox.register("particle", generate, size=2, pmin=-6, pmax=6, smin=-3, smax=3)

They seem to be a minimum and maximum speed range. But when a particle is updated, they seem to have a different meaning:

for i, speed in enumerate(part.speed):
        if abs(speed) < part.smin:
            part.speed[i] = math.copysign(part.smin, speed)
        elif abs(speed) > part.smax:
            part.speed[i] = math.copysign(part.smax, speed)

In this case, with smin having a negative value, the first conditional will never be true. I think the code has a different meaning: smin is the minimum absolute value of the speed regardless of direction. So, a particle could have a speed from 0.3 to 3 negative or positive. I think this more useful. The case above works because it is symmetric around zero. Maybe the parameter should be:

toolbox.register("particle", generate, size=2, pmin=-6, pmax=6, smin=0, smax=3)

I think if someone wants different smin it will not work:

toolbox.register("particle", generate, size=2, pmin=-6, pmax=6, smin=-1, smax=3)