Skip to content

Commit

Permalink
fix bug smt2.0b3 (#93)
Browse files Browse the repository at this point in the history
* fix sampler

* black

* kwargs

* fix rounding lhs

* fix definitevely

* black

* test

* remove ==[] for future deprecations

* remove ==[] for future deprecations
  • Loading branch information
Paul-Saves authored Aug 1, 2023
1 parent 899b7d0 commit 54c1f49
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
15 changes: 13 additions & 2 deletions smt/applications/ego.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
FloatVariable,
CategoricalVariable,
)
from smt.sampling_methods import LHS


class Evaluator(object):
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions smt/applications/mixed_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions smt/surrogate_models/krg_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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
Expand Down
31 changes: 22 additions & 9 deletions smt/utils/design_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]):
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 54c1f49

Please sign in to comment.