|
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 BareBonesFireworksAlgorithm |
|
11
|
|
|
from NiaPy.benchmarks import Benchmark, Katsuura, Elliptic |
|
12
|
|
|
from NiaPy.util import TaskConvPrint, TaskConvPlot |
|
13
|
|
|
|
|
14
|
|
|
logging.basicConfig() |
|
15
|
|
|
logger = logging.getLogger('examples') |
|
16
|
|
|
logger.setLevel('INFO') |
|
17
|
|
|
|
|
18
|
|
|
# For reproducive results |
|
19
|
|
|
random.seed(1234) |
|
20
|
|
|
|
|
21
|
|
View Code Duplication |
class MyBenchmark(Benchmark): |
|
|
|
|
|
|
22
|
|
|
def __init__(self): |
|
23
|
|
|
self.Lower = -11 |
|
24
|
|
|
self.Upper = 11 |
|
25
|
|
|
|
|
26
|
|
|
def function(self): |
|
27
|
|
|
def evaluate(D, sol): |
|
28
|
|
|
val = 0.0 |
|
29
|
|
|
for i in range(D): val += sol[i] ** 2 |
|
30
|
|
|
return val |
|
31
|
|
|
return evaluate |
|
32
|
|
|
|
|
33
|
|
|
def simple_example(runs=10): |
|
34
|
|
|
for i in range(runs): |
|
35
|
|
|
algo = BareBonesFireworksAlgorithm(D=50, nFES=50000, n=15, C_a=1, C_r=0.5, benchmark=MyBenchmark()) |
|
36
|
|
|
best = algo.run() |
|
37
|
|
|
logger.info('%s %s' % (best[0], best[1])) |
|
38
|
|
|
|
|
39
|
|
|
def logging_example(): |
|
40
|
|
|
task = TaskConvPrint(D=50, nFES=50000, nGEN=10000, benchmark=MyBenchmark()) |
|
41
|
|
|
algo = BareBonesFireworksAlgorithm(task=task, n=15, C_a=1, C_r=0.5) |
|
42
|
|
|
best = algo.run() |
|
43
|
|
|
logger.info('%s %s' % (best[0], best[1])) |
|
44
|
|
|
|
|
45
|
|
|
def plot_example(): |
|
46
|
|
|
task = TaskConvPlot(D=50, nFES=50000, nGEN=10000, benchmark=MyBenchmark()) |
|
47
|
|
|
algo = BareBonesFireworksAlgorithm(task=task, n=15, C_a=1, C_r=0.5) |
|
48
|
|
|
best = algo.run() |
|
49
|
|
|
logger.info('%s %s' % (best[0], best[1])) |
|
50
|
|
|
input('Press [enter] to continue') |
|
51
|
|
|
|
|
52
|
|
|
# benc = MyBenchmark() |
|
53
|
|
|
# benc.plot3d() |
|
54
|
|
|
|
|
55
|
|
|
benc = Katsuura(-1, 1) |
|
56
|
|
|
benc.plot3d(0.06) |
|
57
|
|
|
|
|
58
|
|
|
benc = Elliptic() |
|
59
|
|
|
benc.plot3d(0.65) |
|
60
|
|
|
# simple_example() |
|
61
|
|
|
# logging_example() |
|
62
|
|
|
# plot_example() |
|
63
|
|
|
|
|
64
|
|
|
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3 |
|
65
|
|
|
|