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 GreyWolfOptimizer |
||
10 | |||
11 | # our custom benchmark class |
||
12 | View Code Duplication | class MyBenchmark(Benchmark): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
13 | def __init__(self): |
||
14 | Benchmark.__init__(self, -10, 10) |
||
15 | |||
16 | def function(self): |
||
17 | def evaluate(D, sol): |
||
18 | val = 0.0 |
||
19 | for i in range(D): val += sol[i] ** 2 |
||
20 | return val |
||
21 | return evaluate |
||
22 | |||
23 | |||
24 | # we will run 10 repetitions of Grey Wolf Optimizer against our custom MyBenchmark benchmark function |
||
25 | for i in range(10): |
||
26 | task = StoppingTask(D=20, nGEN=100, optType=OptimizationType.MINIMIZATION, benchmark=MyBenchmark()) |
||
27 | |||
28 | # parameter is population size |
||
29 | algo = GreyWolfOptimizer(NP=20) |
||
30 | |||
31 | # running algorithm returns best found minimum |
||
32 | best = algo.run(task) |
||
33 | |||
34 | # printing best minimum |
||
35 | print(best[-1]) |
||
36 |