From 54c1f492685a7338a259783eaa11491beef3b310 Mon Sep 17 00:00:00 2001 From: Saves Paul Date: Tue, 1 Aug 2023 14:17:42 +0200 Subject: [PATCH] fix bug smt2.0b3 (#93) * fix sampler * black * kwargs * fix rounding lhs * fix definitevely * black * test * remove ==[] for future deprecations * remove ==[] for future deprecations --- smt/applications/ego.py | 15 +++++++++++++-- smt/applications/mixed_integer.py | 6 ++++-- smt/surrogate_models/krg_based.py | 5 ++--- smt/utils/design_space.py | 31 ++++++++++++++++++++++--------- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/smt/applications/ego.py b/smt/applications/ego.py index cd5fede2c..2f29d92a7 100644 --- a/smt/applications/ego.py +++ b/smt/applications/ego.py @@ -21,6 +21,7 @@ FloatVariable, CategoricalVariable, ) +from smt.sampling_methods import LHS class Evaluator(object): @@ -264,11 +265,21 @@ def _setup_optimizer(self, fun): self.design_space, work_in_folded_space=True, ) - self._sampling = self.mixint.build_sampling_method() + self._sampling = self.mixint.build_sampling_method( + LHS, + criterion="ese", + random_state=self.options["random_state"], + new_sampler=True, + ) else: self.mixint = None - self._sampling = lambda n: self.design_space.sample_valid_x(n)[0] + self._sampling = lambda n: self.design_space.sample_valid_x( + n, + criterion="ese", + random_state=self.options["random_state"], + new_sampler=True, + )[0] self.categorical_kernel = None # Build DOE diff --git a/smt/applications/mixed_integer.py b/smt/applications/mixed_integer.py index 3939f3bc2..4ba334640 100644 --- a/smt/applications/mixed_integer.py +++ b/smt/applications/mixed_integer.py @@ -295,14 +295,16 @@ def __init__(self, design_space, work_in_folded_space=True): def design_space(self) -> BaseDesignSpace: return self._design_space - def build_sampling_method(self, *_, **__): + def build_sampling_method(self, *_, **kwargs): """ Build MixedIntegerSamplingMethod from given SMT sampling method. """ return_folded = self._work_in_folded_space def sample(n): - x, _ = self._design_space.sample_valid_x(n, unfolded=not return_folded) + x, _ = self._design_space.sample_valid_x( + n, unfolded=not return_folded, **kwargs + ) return x return sample diff --git a/smt/surrogate_models/krg_based.py b/smt/surrogate_models/krg_based.py index b2f3379db..b3c58221c 100644 --- a/smt/surrogate_models/krg_based.py +++ b/smt/surrogate_models/krg_based.py @@ -507,8 +507,7 @@ def _matrix_data_corr( else: X_pls_space, _ = compute_X_cont(X, design_space) if cat_kernel_comps is not None or ncomp < 1e5: - ###Modifier la condition : if PLS cont - if self.pls_coeff_cont == []: + if np.size(self.pls_coeff_cont) == 0: X, y = self._compute_pls(X_pls_space.copy(), y.copy()) self.pls_coeff_cont = self.coeff_pls if cat_kernel in [MixIntKernelType.GOWER, MixIntKernelType.CONT_RELAX]: @@ -1739,7 +1738,7 @@ def grad_minus_reduced_likelihood_function(log10t): theta=best_optimal_theta ) # Optimization fail - elif best_optimal_par == []: + elif np.size(best_optimal_par) == 0: print("Optimization failed. Try increasing the ``nugget``") raise ve # Break the while loop diff --git a/smt/utils/design_space.py b/smt/utils/design_space.py index e484abc6c..5d432adcc 100644 --- a/smt/utils/design_space.py +++ b/smt/utils/design_space.py @@ -299,7 +299,9 @@ def _decode_dv(x_encoded: np.ndarray, i_dv_decode): ] return decoded_des_vectors[0] if is_1d else decoded_des_vectors - def sample_valid_x(self, n: int, unfolded=False) -> Tuple[np.ndarray, np.ndarray]: + def sample_valid_x( + self, n: int, unfolded=False, **kwargs + ) -> Tuple[np.ndarray, np.ndarray]: """ Sample n design vectors and additionally return the is_acting matrix. @@ -319,7 +321,7 @@ def sample_valid_x(self, n: int, unfolded=False) -> Tuple[np.ndarray, np.ndarray """ # Sample from the design space - x, is_acting = self._sample_valid_x(n) + x, is_acting = self._sample_valid_x(n, **kwargs) # Check conditionally-acting status if np.any(~is_acting[:, ~self.is_conditionally_acting]): @@ -543,7 +545,7 @@ def _correct_get_acting(self, x: np.ndarray) -> Tuple[np.ndarray, np.ndarray]: """ raise NotImplementedError - def _sample_valid_x(self, n: int) -> Tuple[np.ndarray, np.ndarray]: + def _sample_valid_x(self, n: int, **kwargs) -> Tuple[np.ndarray, np.ndarray]: """ Sample n design vectors and additionally return the is_acting matrix. @@ -637,6 +639,9 @@ class DesignSpace(BaseDesignSpace): def __init__( self, design_variables: Union[List[DesignVariable], list, np.ndarray], seed=None ): + self.sampler = None + self.new_sampler = True + # Assume float variable bounds as inputs def _is_num(val): try: @@ -738,17 +743,25 @@ def _correct_get_acting(self, x: np.ndarray) -> Tuple[np.ndarray, np.ndarray]: return x_corr, is_acting - def _sample_valid_x(self, n: int) -> Tuple[np.ndarray, np.ndarray]: + def _sample_valid_x(self, n: int, **kwargs) -> Tuple[np.ndarray, np.ndarray]: """Sample design vectors""" # Simplified implementation: sample design vectors in unfolded space x_limits_unfolded = self.get_unfolded_num_bounds() - sampler = LHS(xlimits=x_limits_unfolded, random_state=self.seed) - x = sampler(n) - - # Cast to discrete and fold - self._normalize_x(x) + if "random_state" in kwargs.keys(): + self.seed = kwargs["random_state"] + if "new_sampler" in kwargs.keys() and kwargs["new_sampler"]: + kwargs.pop("new_sampler", None) + if self.new_sampler: + self.sampler = LHS(xlimits=x_limits_unfolded, **kwargs) + self.new_sampler = False + if self.sampler is None: + self.sampler = LHS(xlimits=x_limits_unfolded, **kwargs) + x = self.sampler(n) + + # Fold and cast to discrete x, _ = self.fold_x(x) + self._normalize_x(x) # Get acting information and impute return self.correct_get_acting(x)