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

run_gsov1.simple_example()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 9
dl 0
loc 6
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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