1
|
|
|
# encoding=utf8 |
2
|
|
|
|
3
|
|
|
"""Implementation of benchmarks utility function.""" |
4
|
|
|
|
5
|
|
|
from NiaPy import benchmarks |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class Utility: |
9
|
|
|
r"""Base class with string mappings to benchmarks. |
10
|
|
|
|
11
|
|
|
Attributes: |
12
|
|
|
classes (Dict[str, Benchmark]): Mapping from stings to benchmark. |
13
|
|
|
|
14
|
|
|
""" |
15
|
|
|
|
16
|
|
|
def __init__(self): |
17
|
|
|
r"""Initialize benchmarks.""" |
18
|
|
|
|
19
|
|
|
self.benchmark_classes = { |
20
|
|
|
"ackley": benchmarks.Ackley, |
21
|
|
|
"alpine1": benchmarks.Alpine1, |
22
|
|
|
"alpine2": benchmarks.Alpine2, |
23
|
|
|
"bentcigar": benchmarks.BentCigar, |
24
|
|
|
"chungReynolds": benchmarks.ChungReynolds, |
25
|
|
|
"cosinemixture": benchmarks.CosineMixture, |
26
|
|
|
"csendes": benchmarks.Csendes, |
27
|
|
|
"discus": benchmarks.Discus, |
28
|
|
|
"dixonprice": benchmarks.DixonPrice, |
29
|
|
|
"conditionedellptic": benchmarks.Elliptic, |
30
|
|
|
"elliptic": benchmarks.Elliptic, |
31
|
|
|
"expandedgriewankplusrosenbrock": benchmarks.ExpandedGriewankPlusRosenbrock, |
32
|
|
|
"expandedschaffer": benchmarks.ExpandedSchaffer, |
33
|
|
|
"griewank": benchmarks.Griewank, |
34
|
|
|
"happyCat": benchmarks.HappyCat, |
35
|
|
|
"hgbat": benchmarks.HGBat, |
36
|
|
|
"infinity": benchmarks.Infinity, |
37
|
|
|
"katsuura": benchmarks.Katsuura, |
38
|
|
|
"levy": benchmarks.Levy, |
39
|
|
|
"michalewicz": benchmarks.Michalewichz, |
40
|
|
|
"modifiedscwefel": benchmarks.ModifiedSchwefel, |
41
|
|
|
"perm": benchmarks.Perm, |
42
|
|
|
"pinter": benchmarks.Pinter, |
43
|
|
|
"powell": benchmarks.Powell, |
44
|
|
|
"qing": benchmarks.Qing, |
45
|
|
|
"quintic": benchmarks.Quintic, |
46
|
|
|
"rastrigin": benchmarks.Rastrigin, |
47
|
|
|
"ridge": benchmarks.Ridge, |
48
|
|
|
"rosenbrock": benchmarks.Rosenbrock, |
49
|
|
|
"salomon": benchmarks.Salomon, |
50
|
|
|
"schaffer2": benchmarks.SchafferN2, |
51
|
|
|
"schaffer4": benchmarks.SchafferN4, |
52
|
|
|
"schumerSteiglitz": benchmarks.SchumerSteiglitz, |
53
|
|
|
"schwefel": benchmarks.Schwefel, |
54
|
|
|
"schwefel221": benchmarks.Schwefel221, |
55
|
|
|
"schwefel222": benchmarks.Schwefel222, |
56
|
|
|
"sphere": benchmarks.Sphere, |
57
|
|
|
"sphere2": benchmarks.Sphere2, |
58
|
|
|
"sphere3": benchmarks.Sphere3, |
59
|
|
|
"step": benchmarks.Step, |
60
|
|
|
"step2": benchmarks.Step2, |
61
|
|
|
"step3": benchmarks.Step3, |
62
|
|
|
"stepint": benchmarks.Stepint, |
63
|
|
|
"styblinskiTang": benchmarks.StyblinskiTang, |
64
|
|
|
"sumSquares": benchmarks.SumSquares, |
65
|
|
|
"trid": benchmarks.Trid, |
66
|
|
|
"weierstrass": benchmarks.Weierstrass, |
67
|
|
|
"whitley": benchmarks.Whitley, |
68
|
|
|
"zakharov": benchmarks.Zakharov |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
self.algorithm_classes = {} |
72
|
|
|
|
73
|
|
|
def get_benchmark(self, benchmark): |
74
|
|
|
r"""Get the optimization problem. |
75
|
|
|
|
76
|
|
|
Arguments: |
77
|
|
|
benchmark (Union[str, Benchmark]): String or class that represents the optimization problem. |
78
|
|
|
|
79
|
|
|
Returns: |
80
|
|
|
Benchmark: Optimization function with limits. |
81
|
|
|
|
82
|
|
|
""" |
83
|
|
|
|
84
|
|
|
if issubclass(type(benchmark), benchmarks.Benchmark) or isinstance(benchmark, benchmarks.Benchmark): |
85
|
|
|
return benchmark |
86
|
|
|
elif benchmark in self.benchmark_classes.keys(): |
87
|
|
|
return self.benchmark_classes[benchmark]() |
88
|
|
|
else: |
89
|
|
|
raise TypeError("Passed benchmark is not defined!") |
90
|
|
|
|
91
|
|
|
@classmethod |
92
|
|
|
def __raiseLowerAndUpperNotDefined(cls): |
93
|
|
|
r"""Trow exception if lower and upper bounds are not defined in benchmark. |
94
|
|
|
|
95
|
|
|
Raises: |
96
|
|
|
TypeError: Type error. |
97
|
|
|
|
98
|
|
|
""" |
99
|
|
|
|
100
|
|
|
raise TypeError("Upper and Lower value must be defined!") |
101
|
|
|
|