-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestfunctions.py
187 lines (131 loc) · 4.92 KB
/
testfunctions.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from abc import ABC, abstractmethod
import numpy as np
from numpy.typing import NDArray
import matplotlib as mat
import matplotlib.pyplot as plt
import os
import typing as t
def suppress_qt_warnings() -> None:
os.environ["QT_DEVICE_PIXEL_RATIO"] = "0"
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
os.environ["QT_SCREEN_SCALE_FACTORS"] = "1"
os.environ["QT_SCALE_FACTOR"] = "1"
class ValueOutOFRange(Exception):
def __init__(self, axis: str) -> None:
super().__init__(f'Values given for {axis}-axis are out of range.')
class Function2D(ABC):
_xmin = np.float32(-100.0)
_xmax = np.float32(+100.0)
_ymin = np.float32(-100.0)
_ymax = np.float32(+100.0)
_graphtitle = '-x-'
def __init__(self,
xshift: np.float32 = np.float32(0.0),
yshift: np.float32 = np.float32(0.0),
xiter: np.int32 = np.int32(100),
yiter: np.int32 = np.int32(100)) -> None:
self.xshift = xshift
self.yshift = yshift
self.xiter = xiter
self.yiter = yiter
def _checkBounds(self, x: NDArray[np.float32], y: NDArray[np.float32]) -> t.Union[None, str]:
if(np.any((self.xmin > x) | (x > self.xmax))):
return 'x'
if(np.any((self.ymin > y) | (y > self.ymax))):
return 'y'
return None
def evaluateSingle(self, v: NDArray[np.float32]) -> np.float32:
axis: t.Union[None, str] = self._checkBounds(v[0], v[1])
if(axis is not None):
raise ValueOutOFRange(axis)
return self._func(v[0], v[1])
def evaluate(self, x: NDArray[np.float32], y: NDArray[np.float32]) -> NDArray[np.float32]:
axis: t.Union[None, str] = self._checkBounds(x, y)
if(axis is not None):
raise ValueOutOFRange(axis)
return self._func(x, y)
def show(self, ax = None) -> None:
x: NDArray[np.float32] = np.linspace(self.xmin, self.xmax, self.xiter)
y: NDArray[np.float32] = np.linspace(self.ymin, self.ymax, self.yiter)
x, y = np.meshgrid(x, y)
z: NDArray[np.float32] = self._func(x, y)
ax.imshow(z, interpolation='bilinear',
extent = [self.xmin, self.xmax, self.ymin, self.ymax],
origin = 'lower',
cmap = 'YlOrBr')
ax.title.set_text(self.graphtitle)
@abstractmethod
def _func(self, x: NDArray[np.float32], y: NDArray[np.float32]) -> NDArray[np.float32]:
pass
@property
def xmin(self) -> np.float32:
return self._xmin
@property
def xmax(self) -> np.float32:
return self._xmax
@property
def ymin(self) -> np.float32:
return self._ymin
@property
def ymax(self) -> np.float32:
return self._ymax
@property
def xshift(self) -> np.float32:
return self._xshift
@xshift.setter
def xshift(self, val: np.float32) -> None:
self._xshift = val
@property
def yshift(self) -> np.float32:
return self._yshift
@yshift.setter
def yshift(self, val: np.float32) -> None:
self._yshift = val
@property
def xiter(self) -> np.int32:
return self._xiter
@xiter.setter
def xiter(self, val: np.int32) -> None:
self._xiter = val
@property
def yiter(self) -> np.int32:
return self._yiter
@yiter.setter
def yiter(self, val: np.int32) -> None:
self._yiter = val
@property
def graphtitle(self) -> str:
return self._graphtitle
class ShafferF62D(Function2D):
xmin = np.float32(-100.0)
xmax = np.float32(+100.0)
ymin = np.float32(-100.0)
ymax = np.float32(+100.0)
graphtitle = 'ShafferF6 - 2D'
def __init__(self,
xshift: np.float32 = np.float32(0.0),
yshift: np.float32 = np.float32(0.0),
xiter: np.int32 = np.int32(500),
yiter: np.int32 = np.int32(500)) -> None:
super().__init__(xshift, yshift, xiter, yiter)
def _func(self, x: NDArray[np.float32], y: NDArray[np.float32]) -> NDArray[np.float32]:
x += self.xshift
y += self.yshift
return 0.5 + ((np.sin(np.sqrt(x**2 + y**2)))**2 - 0.5)/((1 + 0.001 * (x**2 + y**2))**2)
class Rastrigin2D(Function2D):
xmin = np.float32(-5.12)
xmax = np.float32(+5.12)
ymin = np.float32(-5.12)
ymax = np.float32(+5.12)
graphtitle = 'Rastrigin - 2D'
def _func(self, x: NDArray[np.float32], y: NDArray[np.float32]) -> NDArray[np.float32]:
x += self.xshift
y += self.yshift
return ((x**2 - 10.0 * np.cos(2.0 * np.pi * x)) + (y**2 - 10.0 * np.cos(2.0 * np.pi * y)) + 20.0) * -1.0
if __name__ == '__main__':
suppress_qt_warnings()
fig, ax1 = plt.subplots(nrows = 1, ncols = 1, figsize=(12, 8), dpi = 80)
sf = ShafferF62D(xshift = np.float32(30.0), yshift = np.float32(-30.0))
sf.evaluate([100.0, 100.0], [100.0, 100.0])
sf.show(ax1)
plt.show()