Completed
Push — master ( cc7745...279fb5 )
by Grega
19s queued 17s
created

run_gsov1   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 15.94 %

Importance

Changes 0
Metric Value
eloc 53
dl 11
loc 69
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A MinMB.function() 6 6 2
A MinMB.__init__() 3 3 1
A MaxMB.function() 0 4 1

4 Functions

Rating   Name   Duplication   Size   Complexity  
A logging_example() 0 5 1
A plot_example() 0 6 1
A simple_example() 0 6 2
A getOptType() 0 4 3

How to fix   Duplicated Code   

Duplicated Code

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
import random
9
import logging
10
from NiaPy.algorithms.basic import GlowwormSwarmOptimizationV1
11
from NiaPy.util import Task, TaskConvPrint, TaskConvPlot, OptimizationType, getDictArgs
12
13
logging.basicConfig()
14
logger = logging.getLogger('examples')
15
logger.setLevel('INFO')
16
17
# For reproducive results
18
random.seed(1234)
19
20 View Code Duplication
class MinMB(object):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
21
	def __init__(self):
22
		self.Lower = -11
23
		self.Upper = 11
24
25
	def function(self):
26
		def evaluate(D, sol):
27
			val = 0.0
28
			for i in range(D): val = val + sol[i] * sol[i]
29
			return val
30
		return evaluate
31
32
class MaxMB(MinMB):
33
	def function(self):
34
		f = MinMB.function(self)
35
		def e(D, sol): return -f(D, sol)
36
		return e
37
38
def simple_example(alg, runs=10, D=10, nFES=50000, nGEN=10000, seed=[None], optType=OptimizationType.MINIMIZATION, optFunc=MinMB, **kn):
39
	for i in range(runs):
40
		task = Task(D=D, nFES=nFES, nGEN=nGEN, optType=optType, benchmark=optFunc())
41
		algo = alg(seed=seed[i % len(seed)], task=task)
42
		best = algo.run()
43
		logger.info('%s %s' % (best[0], best[1]))
44
45
def logging_example(alg, D=10, nFES=50000, nGEN=100000, seed=[None], optType=OptimizationType.MINIMIZATION, optFunc=MinMB, **kn):
46
	task = TaskConvPrint(D=D, nFES=nFES, nGEN=nGEN, optType=optType, benchmark=optFunc())
47
	algo = alg(seed=seed[0], task=task)
48
	best = algo.run()
49
	logger.info('%s %s' % (best[0], best[1]))
50
51
def plot_example(alg, D=10, nFES=50000, nGEN=100000, seed=[None], optType=OptimizationType.MINIMIZATION, optFunc=MinMB, **kn):
52
	task = TaskConvPlot(D=D, nFES=nFES, nGEN=nGEN, optType=optType, benchmark=optFunc())
53
	algo = alg(seed=seed[0], task=task)
54
	best = algo.run()
55
	logger.info('%s %s' % (best[0], best[1]))
56
	input('Press [enter] to continue')
57
58
def getOptType(otype):
59
	if otype == OptimizationType.MINIMIZATION: return MinMB
60
	elif otype == OptimizationType.MAXIMIZATION: return MaxMB
61
	else: return None
62
63
if __name__ == '__main__':
64
	pargs, algo = getDictArgs(sys.argv[1:]), GlowwormSwarmOptimizationV1
65
	optFunc = getOptType(pargs['optType'])
66
	if not pargs['runType']: simple_example(algo, optFunc=optFunc, **pargs)
67
	elif pargs['runType'] == 'log': logging_example(algo, optFunc=optFunc, **pargs)
68
	elif pargs['runType'] == 'plot': plot_example(algo, optFunc=optFunc, **pargs)
69
70
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
71