| Total Complexity | 1 |
| Total Lines | 25 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # encoding=utf8 |
||
| 2 | # This is temporary fix to import module from parent folder |
||
| 3 | # It will be removed when package is published on PyPI |
||
| 4 | import sys |
||
| 5 | sys.path.append('../') |
||
| 6 | # End of fix |
||
| 7 | |||
| 8 | from NiaPy.algorithms.basic import ParticleSwarmAlgorithm |
||
| 9 | from NiaPy.task.task import StoppingTask, OptimizationType |
||
| 10 | from NiaPy.benchmarks import Sphere |
||
| 11 | from numpy import random as rand, apply_along_axis |
||
| 12 | |||
| 13 | def MyInit(task, NP, rnd=rand, **kwargs): |
||
| 14 | pop = 0.2 + rnd.rand(NP, task.D) * task.bRange |
||
| 15 | fpop = apply_along_axis(task.eval, 1, pop) |
||
| 16 | return pop, fpop |
||
| 17 | |||
| 18 | |||
| 19 | # we will run Particle Swarm Algorithm with custom Init function for 5 independent runs |
||
| 20 | for i in range(5): |
||
| 21 | task = StoppingTask(D=10, nFES=1000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) |
||
| 22 | algo = ParticleSwarmAlgorithm(NP=10, C1=2.0, C2=2.0, w=0.7, vMin=-4, vMax=4, InitPopFunc=MyInit) |
||
| 23 | best = algo.run(task=task) |
||
| 24 | print(best) |
||
| 25 |