11import numpy as np
22from numpy .random import default_rng
3- from random import random
43from typing import List , Optional , Dict
54
65
@@ -11,8 +10,6 @@ class Analytical:
1110 Args:
1211 offset (float):
1312 Offset value. Defaults to 0.0.
14- hz (float):
15- Horizontal value. Defaults to 0.
1613 seed (int):
1714 Seed value for random number generation. Defaults to 126.
1815
@@ -22,8 +19,6 @@ class Analytical:
2219 Attributes:
2320 offset (float):
2421 Offset value.
25- hz (float):
26- Horizontal value.
2722 sigma (float):
2823 Noise level.
2924 seed (int):
@@ -34,16 +29,15 @@ class Analytical:
3429 Dictionary containing control parameters for the function.
3530 """
3631
37- def __init__ (self , offset : float = 0.0 , hz : float = 0 , sigma = 0.0 , seed : int = 126 ) -> None :
32+ def __init__ (self , offset : float = 0.0 , sigma = 0.0 , seed : int = 126 ) -> None :
3833 self .offset = offset
39- self .hz = hz
4034 self .sigma = sigma
4135 self .seed = seed
4236 self .rng = default_rng (seed = self .seed )
4337 self .fun_control = {"sigma" : sigma , "seed" : None , "sel_var" : None }
4438
4539 def __repr__ (self ) -> str :
46- return f"analytical(offset={ self .offset } , hz ={ self .hz } , seed={ self .seed } )"
40+ return f"analytical(offset={ self .offset } , sigma ={ self .sigma } , seed={ self .seed } )"
4741
4842 def _prepare_input_data (self , X , fun_control ):
4943 if fun_control is not None :
@@ -53,7 +47,7 @@ def _prepare_input_data(self, X, fun_control):
5347 X = np .atleast_2d (X )
5448 return X
5549
56- def add_noise (self , y : List [float ]) -> np .ndarray :
50+ def _add_noise (self , y : List [float ]) -> np .ndarray :
5751 """
5852 Adds noise to the input data.
5953 This method takes in a list of float values y as input and adds noise to
@@ -72,7 +66,7 @@ def add_noise(self, y: List[float]) -> np.ndarray:
7266 import numpy as np
7367 y = np.array([1, 2, 3, 4, 5])
7468 fun = analytical(sigma=1.0, seed=123)
75- fun.add_noise (y)
69+ fun._add_noise (y)
7670 array([0.01087865, 1.63221335, 4.28792526, 4.19397442, 5.9202309 ])
7771
7872 """
@@ -136,7 +130,7 @@ def fun_branin_factor(self, X: np.ndarray, fun_control: Optional[Dict] = None) -
136130 y [j ] = y [j ] + 10
137131 elif x3 [j ] == 2 :
138132 y [j ] = y [j ] - 10
139- return self .add_noise (y )
133+ return self ._add_noise (y )
140134
141135 def fun_linear (self , X : np .ndarray , fun_control : Optional [Dict ] = None ) -> np .ndarray :
142136 """Linear function.
@@ -160,10 +154,8 @@ def fun_linear(self, X: np.ndarray, fun_control: Optional[Dict] = None) -> np.nd
160154
161155 """
162156 X = self ._prepare_input_data (X , fun_control )
163- y = np .array ([], dtype = float )
164- for i in range (X .shape [0 ]):
165- y = np .append (y , np .sum (X [i ]))
166- return self .add_noise (y )
157+ y = np .sum (X , axis = 1 )
158+ return self ._add_noise (y )
167159
168160 def fun_sphere (self , X : np .ndarray , fun_control : Optional [Dict ] = None ) -> np .ndarray :
169161 """Sphere function.
@@ -189,7 +181,7 @@ def fun_sphere(self, X: np.ndarray, fun_control: Optional[Dict] = None) -> np.nd
189181 X = self ._prepare_input_data (X , fun_control )
190182 offset = np .ones (X .shape [1 ]) * self .offset
191183 y = np .sum ((X - offset ) ** 2 , axis = 1 )
192- return self .add_noise (y )
184+ return self ._add_noise (y )
193185
194186 def fun_cubed (self , X : np .ndarray , fun_control : Optional [Dict ] = None ) -> np .ndarray :
195187 """Cubed function. Implements the function f(x) = sum((x_i - offset)^3).
@@ -214,7 +206,7 @@ def fun_cubed(self, X: np.ndarray, fun_control: Optional[Dict] = None) -> np.nda
214206 X = self ._prepare_input_data (X , fun_control )
215207 offset = np .ones (X .shape [1 ]) * self .offset
216208 y = np .sum ((X - offset ) ** 3 , axis = 1 )
217- return self .add_noise (y )
209+ return self ._add_noise (y )
218210
219211 def fun_forrester (self , X : np .ndarray , fun_control : Optional [Dict ] = None ) -> np .ndarray :
220212 """Forrester function. Function used by [Forr08a, p.83].
@@ -240,7 +232,7 @@ def fun_forrester(self, X: np.ndarray, fun_control: Optional[Dict] = None) -> np
240232 """
241233 X = self ._prepare_input_data (X , fun_control )
242234 y = ((6.0 * X - 2 ) ** 2 ) * np .sin (12 * X - 4 )
243- return self .add_noise (y )
235+ return self ._add_noise (y )
244236
245237 def fun_branin (self , X : np .ndarray , fun_control : Optional [Dict ] = None ) -> np .ndarray :
246238 r"""Branin function. The 2-dim Branin function is defined as
@@ -295,7 +287,7 @@ def fun_branin(self, X: np.ndarray, fun_control: Optional[Dict] = None) -> np.nd
295287 s = 10
296288 t = 1 / (8 * np .pi )
297289 y = a * (x2 - b * x1 ** 2 + c * x1 - r ) ** 2 + s * (1 - t ) * np .cos (x1 ) + s
298- return self .add_noise (y )
290+ return self ._add_noise (y )
299291
300292 def fun_branin_modified (self , X : np .ndarray , fun_control : Optional [Dict ] = None ) -> np .ndarray :
301293 """Modified Branin function.
@@ -332,7 +324,7 @@ def fun_branin_modified(self, X: np.ndarray, fun_control: Optional[Dict] = None)
332324 e = 10
333325 ff = 1 / (8 * np .pi )
334326 y = (a * (X2 - b * X1 ** 2 + c * X1 - d ) ** 2 + e * (1 - ff ) * np .cos (X1 ) + e ) + 5 * x
335- return self .add_noise (y )
327+ return self ._add_noise (y )
336328
337329 def fun_sin_cos (self , X , fun_control = None ):
338330 """Sinusoidal function.
@@ -358,8 +350,8 @@ def fun_sin_cos(self, X, fun_control=None):
358350 raise Exception
359351 x0 = X [:, 0 ]
360352 x1 = X [:, 1 ]
361- y = 2.0 * np .sin (x0 + self .hz ) + 0.5 * np .cos (x1 + self .hz )
362- return self .add_noise (y )
353+ y = 2.0 * np .sin (x0 - self .offset ) + 0.5 * np .cos (x1 - self .offset )
354+ return self ._add_noise (y )
363355
364356 def fun_runge (self , X : np .ndarray , fun_control : Optional [Dict ] = None ) -> np .ndarray :
365357 """Runge function. Formula: f(x) = 1/ (1 + sum(x_i) - offset)^2. Dim: k >= 1.
@@ -386,7 +378,7 @@ def fun_runge(self, X: np.ndarray, fun_control: Optional[Dict] = None) -> np.nda
386378 squared_diff = (X - offset ) ** 2
387379 sum_squared_diff = np .sum (squared_diff , axis = 1 )
388380 y = 1 / (1 + sum_squared_diff )
389- return self .add_noise (y )
381+ return self ._add_noise (y )
390382
391383 def fun_wingwt (self , X : np .ndarray , fun_control : Optional [Dict ] = None ) -> np .ndarray :
392384 r"""Wing weight function.
@@ -460,7 +452,7 @@ def fun_wingwt(self, X: np.ndarray, fun_control: Optional[Dict] = None) -> np.nd
460452 W = 0.036 * Sw ** 0.758 * Wfw ** 0.0035 * (A / np .cos (L ) ** 2 ) ** 0.6 * q ** 0.006
461453 W *= la ** 0.04 * (100 * Rtc / np .cos (L )) ** (- 0.3 ) * (Nz * Wdg ) ** (0.49 )
462454 W += Sw * Wp
463- return self .add_noise (y = W )
455+ return self ._add_noise (y = W )
464456
465457 def fun_xsin (self , X : np .ndarray , fun_control : Optional [Dict ] = None ) -> np .ndarray :
466458 """Example function.
@@ -482,7 +474,7 @@ def fun_xsin(self, X: np.ndarray, fun_control: Optional[Dict] = None) -> np.ndar
482474 """
483475 X = self ._prepare_input_data (X , fun_control )
484476 y = X * np .sin (1.0 / X )
485- return self .add_noise (y )
477+ return self ._add_noise (y )
486478
487479 def fun_rosen (self , X : np .ndarray , fun_control : Optional [Dict ] = None ) -> np .ndarray :
488480 """Rosenbrock function.
@@ -508,7 +500,7 @@ def fun_rosen(self, X: np.ndarray, fun_control: Optional[Dict] = None) -> np.nda
508500 x1 = X [:, 1 ]
509501 b = 10
510502 y = (x0 - 1 ) ** 2 + b * (x1 - x0 ** 2 ) ** 2
511- return self .add_noise (y )
503+ return self ._add_noise (y )
512504
513505 def fun_random_error (self , X : np .ndarray , fun_control : Optional [Dict ] = None ) -> np .ndarray :
514506 """Return errors for testing spot stability.
@@ -531,9 +523,8 @@ def fun_random_error(self, X: np.ndarray, fun_control: Optional[Dict] = None) ->
531523 X = self ._prepare_input_data (X , fun_control )
532524 # Compute the sum of rows of X
533525 y = np .sum (X , axis = 1 )
534-
535526 # Determine which elements to set to np.nan
536- nan_mask = random (y .shape ) < 0.1
527+ nan_mask = self . rng . random (size = y .shape ) < 0.1
537528 y [nan_mask ] = np .nan
538529
539- return self .add_noise (y )
530+ return self ._add_noise (y )
0 commit comments