forked from NiaOrg/NiaPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_ba.py
76 lines (56 loc) · 1.84 KB
/
run_ba.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# This is temporary fix to import module from parent folder
# It will be removed when package is published on PyPI
import sys
sys.path.append('../')
# End of fix
import random
import logging
from NiaPy.algorithms.basic import BatAlgorithm
from NiaPy.benchmarks import Griewank
logging.basicConfig()
logger = logging.getLogger('examples')
logger.setLevel('INFO')
# For reproducive results
random.seed(1234)
class MyBenchmark(object):
def __init__(self):
self.Lower = -5.12
self.Upper = 5.12
def function(self):
def evaluate(D, sol):
val = 0.0
for i in range(D):
val = val + sol[i] * sol[i]
return val
return evaluate
# example using custom benchmark "MyBenchmark"
logger.info('Running with custom MyBenchmark...')
for i in range(10):
Algorithm = BatAlgorithm(10, 40, 10000, 0.5, 0.5, 0.0, 2.0, MyBenchmark())
Best = Algorithm.run()
logger.info(Best)
# example using predifined benchmark function
# available benchmarks are:
# - griewank
# - rastrigin
# - rosenbrock
# - sphere
logger.info('Running with default Griewank benchmark...')
griewank = Griewank()
for i in range(10):
Algorithm = BatAlgorithm(10, 40, 10000, 0.5, 0.5, 0.0, 2.0, griewank)
Best = Algorithm.run()
logger.info(Best)
logger.info(
'Running with default Griewank benchmark - should be the same as previous implementataion...')
for i in range(10):
Algorithm = BatAlgorithm(10, 40, 10000, 0.5, 0.5, 0.0, 2.0, 'griewank')
Best = Algorithm.run()
logger.info(Best)
# example with changed griewank's lower and upper bounds
logger.info('Running with Griewank with changed Upper and Lower bounds...')
griewank = Griewank(-50, 50)
for i in range(10):
Algorithm = BatAlgorithm(10, 40, 10000, 0.5, 0.5, 0.0, 2.0, griewank)
Best = Algorithm.run()
logger.info(Best)