11#
22# author: Jungtaek Kim ([email protected] ) 3- # last updated: May 1, 2021
3+ # last updated: December 13, 2022
44#
55
66import numpy as np
77
88EPSILON = 1e-4
99
1010
11- class Function ( object ) :
11+ class Function :
1212 def __init__ (self , dimensionality , bounds , global_minimizers , global_minimum , function , dim_problem = None , seed = None ):
1313 assert isinstance (dimensionality , int ) or dimensionality is np .inf
1414 assert isinstance (bounds , np .ndarray )
1515 assert isinstance (global_minimizers , np .ndarray )
1616 assert isinstance (global_minimum , float )
1717 assert callable (function )
18- assert isinstance (dim_problem , int ) or dim_problem is None
18+ assert isinstance (dim_problem , (type (None ), int ))
19+ assert isinstance (seed , (type (None ), int ))
1920 assert len (bounds .shape ) == 2
2021 assert bounds .shape [1 ] == 2
2122 assert (bounds [:, 0 ] <= bounds [:, 1 ]).all ()
@@ -30,6 +31,7 @@ def __init__(self, dimensionality, bounds, global_minimizers, global_minimum, fu
3031 self .random_state = np .random .RandomState (seed )
3132
3233 self .validate_properties ()
34+ self .set_name ()
3335
3436 @property
3537 def dimensionality (self ):
@@ -47,6 +49,14 @@ def global_minimizers(self):
4749 def global_minimum (self ):
4850 return self ._global_minimum
4951
52+ def set_name (self ):
53+ name = self .__class__ .__name__ .lower ()
54+
55+ if self .dimensionality is np .inf :
56+ self .name = f'{ name } _{ self .dim_problem } '
57+ else :
58+ self .name = name
59+
5060 def get_bounds (self ):
5161 if self .dimensionality is np .inf :
5262 return np .array (list (self .bounds ) * self .dim_problem )
@@ -134,7 +144,7 @@ def output_sparse_gaussian_noise(self, X, scale_noise=0.1, sparsity=0.01):
134144
135145 noise = self .random_state .randn (num_X )
136146 mask = self .random_state .uniform (low = 0.0 , high = 1.0 , size = num_X ) < sparsity
137- noise *= mask .astype (np . float )
147+ noise *= mask .astype (float )
138148 by += scale_noise * noise
139149
140150 Y = np .expand_dims (by , axis = 1 )
@@ -172,7 +182,7 @@ def output_sparse_student_t_noise(self, X, scale_noise=0.1, dof=4.0, sparsity=0.
172182
173183 noise = self .random_state .standard_t (dof , size = num_X )
174184 mask = self .random_state .uniform (low = 0.0 , high = 1.0 , size = num_X ) < sparsity
175- noise *= mask .astype (np . float )
185+ noise *= mask .astype (float )
176186 by += scale_noise * noise
177187
178188 Y = np .expand_dims (by , axis = 1 )
@@ -228,3 +238,6 @@ def sample_uniform(self, num_points, seed=None):
228238 points = bounds [:, 0 ] + (bounds [:, 1 ] - bounds [:, 0 ]) * points
229239
230240 return points
241+
242+ def __call__ (self , X ):
243+ return self .output (X )
0 commit comments