Completed
Push — master ( 9a7980...2248e9 )
by Grega
19s queued 14s
created

advanced_example.MyBenchmark.__init__()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 2
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 2
loc 2
rs 10
c 0
b 0
f 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
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
This code seems to be duplicated in your project.
Loading history...
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