| Total Complexity | 3 | 
| Total Lines | 57 | 
| Duplicated Lines | 17.54 % | 
| 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 | # End of fix | ||
| 7 | |||
| 8 | from NiaPy import Runner | ||
| 9 | from NiaPy.algorithms.basic import ( | ||
| 10 | GreyWolfOptimizer, | ||
| 11 | ParticleSwarmAlgorithm | ||
| 12 | ) | ||
| 13 | from NiaPy.benchmarks import ( | ||
| 14 | Benchmark, | ||
| 15 | Ackley, | ||
| 16 | Griewank, | ||
| 17 | Sphere, | ||
| 18 | HappyCat | ||
| 19 | ) | ||
| 20 | |||
| 21 | |||
| 22 | """Example demonstrating the use of NiaPy Runner.""" | ||
| 23 | |||
| 24 | View Code Duplication | class MyBenchmark(Benchmark): | |
|  | |||
| 25 | def __init__(self): | ||
| 26 | Benchmark.__init__(self, -10, 10) | ||
| 27 | |||
| 28 | def function(self): | ||
| 29 | def evaluate(D, sol): | ||
| 30 | val = 0.0 | ||
| 31 | for i in range(D): val += sol[i] ** 2 | ||
| 32 | return val | ||
| 33 | return evaluate | ||
| 34 | |||
| 35 | runner = Runner( | ||
| 36 | D=40, | ||
| 37 | nFES=100, | ||
| 38 | nRuns=2, | ||
| 39 | useAlgorithms=[ | ||
| 40 | GreyWolfOptimizer(), | ||
| 41 | "FlowerPollinationAlgorithm", | ||
| 42 | ParticleSwarmAlgorithm(), | ||
| 43 | "HybridBatAlgorithm", | ||
| 44 | "SimulatedAnnealing", | ||
| 45 | "CuckooSearch"], | ||
| 46 | useBenchmarks=[ | ||
| 47 | Ackley(), | ||
| 48 | Griewank(), | ||
| 49 | Sphere(), | ||
| 50 | HappyCat(), | ||
| 51 | "rastrigin", | ||
| 52 | MyBenchmark() | ||
| 53 | ] | ||
| 54 | ) | ||
| 55 | |||
| 56 | runner.run(export='dataframe', verbose=True) | ||
| 57 |