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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""
Class for handling position-value pairs.
#### Description
This class is used to represent a position-value pair where the position is
an array and the value is a float. It provides a method to evaluate a
function at the given position and store the result as the value.
#### Parameters
**pos** (`npt.NDArray`)
: An array representing the position.
**val** (`float`)
: A float representing the value at the position.
#### Attributes
**pos** (`npt.NDArray`)
: An array representing the position.
**val** (`float`)
: A float representing the value at the position. This value is updated
when the `EvalFunc` method is called.
#### Notes
The `EvalFunc` method takes a callable object (a function) as an argument,
evaluates this function at the position stored in the `pos` attribute, and
updates the `val` attribute with the result.
"""
:
:
"""
Evaluates a function at the position and updates the value.
#### Parameters
**ObjFunc** (`callable`)
: A function to evaluate at the position. This function should take an
array (the position) as an argument and return a float (the value).
#### Notes
This method evaluates the provided function at the position stored in
the `pos` attribute and updates the `val` attribute with the result.
"""
=
"""
Class for tracking function bounds.
#### Description
This class is used to represent and enforce numerical limits or bounds on a
function. It provides methods to check if a given position is within the
bounds, generate a random point within the bounds, and clip a point to the
bounds.
#### Parameters
**low** (`npt.NDArray`)
: An array representing the lower bounds that the function must not be
below.
**high** (`npt.NDArray`)
: An array representing the upper bounds that the function must not exceed.
**slack** (`float`, optional)
: The amount by which bounds may be exceeded without an error. Default is
`1e-6`.
**dims** (`int`, optional)
: The number of dimensions, which should be the same as the length of the
`low` and `high` arrays. Default is `1`.
#### Attributes
**low** (`npt.NDArray`)
: An array representing the lower bounds.
**high** (`npt.NDArray`)
: An array representing the upper bounds.
**slack** (`float`)
: The amount by which bounds may be exceeded without an error.
**dims** (`int`)
: The number of dimensions.
#### Notes
The `check` method checks if a given position is within the bounds. If the
position is not within the slack of the bounds, an `OutOfBounds` exception
is raised.
The `mkpoint` method generates a random point within the bounds.
The `clip` method clips a given point to the bounds.
"""
:
:
: = 1e-6
: = 1
"""
Checks if a position is within the bounds.
#### Parameters
**pos** (`npt.NDArray`)
: The position to check.
#### Notes
This method checks if the provided position is within the slack of the
bounds. If the position is not within the slack of the bounds, an
`OutOfBounds` exception is raised.
"""
return
"""
Generates a random point within the bounds.
#### Notes
This method generates a random point within the bounds using a uniform
distribution. The number of dimensions of the point is equal to the
`dims` attribute.
TODO: Handle other constraints (undefined regions)
"""
return
"""
Clips a point to the bounds.
#### Parameters
**point** (`npt.NDArray`)
: The point to clip.
#### Notes
This method clips the provided point to the bounds. If the point is
below the lower bound, it is set to the lower bound. If the point is
above the upper bound, it is set to the upper bound.
"""
return
"""
Abstract base class for an objective function.
#### Description
This class represents an objective function that can be evaluated at a
single point or multiple points. It keeps track of the number of function
calls and enforces numerical limits on the function.
#### Parameters
**limits** (`NumLimit`)
: An instance of the `NumLimit` class representing the numerical limits of
the function.
**global_min** (`FPair`, optional)
: An instance of the `FPair` class representing the global minimum of the
function. Default is `None`.
#### Attributes
**calls** (`int`)
: The number of times the function has been called.
**limits** (`NumLimit`)
: The numerical limits of the function.
**globmin** (`FPair`)
: The global minimum of the function.
#### Notes
Subclasses must implement the `__call__`, `singlepoint`, `multipoint`, and
`__repr__` methods. The `__call__` method determines whether to call the
`singlepoint` or `multipoint` method based on the shape of the input.
"""
"""
Initializes an instance of the ObjectiveFunction class.
#### Parameters
**limits** (`NumLimit`)
: An instance of the `NumLimit` class representing the numerical limits
of the function.
**global_min** (`FPair`, optional)
: An instance of the `FPair` class representing the global minimum of
the function. Default is `None`.
"""
= 0
=
=
"""
Checks if a class is a subclass of ObjectiveFunction.
#### Parameters
**subclass**
: The class to check.
#### Notes
This method checks if the provided class implements the `__call__`,
`singlepoint`, `multipoint`, and `__repr__` methods. If it does, the
class is considered a subclass of `ObjectiveFunction`.
"""
return
"""
Evaluates the function.
#### Parameters
**pos** (`npt.NDArray`)
: The position(s) at which to evaluate the function.
#### Notes
This method checks the shape of the input and decides whether to call
the `singlepoint` or `multipoint` method. It increments the `calls`
attribute each time the function is evaluated.
TODO: calls in multipoint may be more than once
"""
+= 1
return
+= 1
return
"""
Evaluates the function at a single point.
#### Parameters
**pos** (`npt.NDArray`)
: The position at which to evaluate the function.
#### Notes
This method must be implemented by subclasses.
"""
"""
Evaluates the function at multiple points.
#### Parameters
**pos** (`npt.NDArray`)
: The positions at which to evaluate the function.
#### Notes
This method must be implemented by subclasses. It allows for a faster
implementation in C.
TODO: This allows for a faster implementation in C
"""
"""
Returns a string representation of the function.
#### Notes
This method must be implemented by subclasses. It should return a string
that names the function.
"""