| Total Complexity | 3 |
| Total Lines | 36 |
| Duplicated Lines | 27.78 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 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): |
|
|
|
|||
| 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 |