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 | |||
7 | from NiaPy.task import StoppingTask, OptimizationType |
||
8 | from NiaPy.benchmarks import Benchmark |
||
9 | from NiaPy.algorithms.basic import ParticleSwarmAlgorithm |
||
10 | |||
11 | View Code Duplication | class MyBenchmark(Benchmark): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
12 | def __init__(self): |
||
13 | Benchmark.__init__(self, -10, 10) |
||
14 | |||
15 | def function(self): |
||
16 | def evaluate(D, sol): |
||
17 | val = 0.0 |
||
18 | for i in range(D): val += sol[i] ** 2 |
||
19 | return val |
||
20 | return evaluate |
||
21 | |||
22 | |||
23 | # we will run Particle Swarm Algorithm with on custom benchmark |
||
24 | for i in range(1): |
||
25 | task = StoppingTask(D=10, nGEN=1000, optType=OptimizationType.MINIMIZATION, benchmark=MyBenchmark()) |
||
26 | algo = ParticleSwarmAlgorithm(NP=40, C1=2.0, C2=2.0, w=0.7, vMin=-4, vMax=4) |
||
27 | best = algo.run(task=task) |
||
28 | print('%s -> %s ' % (best[0], best[1])) |
||
29 |