1. import package>>> from slapy.swarm.package_name import engine_name2. define the fitness functionexample:
note: arg need to be 1 X n vector3. run the model>>> engine = engine_name(your_args)>>> engine.run()4. show result>>> x, y = engine.gbest.chromosome>>> print('max value', fun(engine.gbest.chromosome))>>> print('x:', x, 'y:', y)There is a example for PSO.
>>> def fun(vars):>>> # fitness function>>> x, y = vars>>> if 1 <= x <= 2 * np.pi and 1 <= y <= np.pi:>>> return np.cos(x) + np.sin(x) - x * y>>> else:>>> return -2 - 4 * np.pi ** 2 # return a small float number can not reach
>>> if __name__ == '__main__':>>> engine = PSOEngine(vmax=0.01, bound=[[1, 2 * np.pi]], min_fitness_value=-1, dim=2, fitness_function=fun, steps=100)>>> engine.run()>>> x, y = engine.gbest.chromosome>>> print('max value', fun(engine.gbest.chromosome))>>> print('x:', x, 'y:', y)
请发表评论