Skip to content

Commit

Permalink
fix CONT_RELAX with Hierarchical variables (#664)
Browse files Browse the repository at this point in the history
* fix cr hier

* ruff

* init zscale
  • Loading branch information
Paul-Saves authored Oct 18, 2024
1 parent 3765270 commit 38f6e37
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
2 changes: 1 addition & 1 deletion smt/applications/tests/test_mixed_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ def test_hierarchical_design_space_example_all_categorical_decreed(self):
sm.train()
y_s = sm.predict_values(Xt)[:, 0]
_pred_RMSE = np.linalg.norm(y_s - Yt) / len(Yt)
self.assertLess(_pred_RMSE, 1e-3)
self.assertLess(_pred_RMSE, 1e-6)
y_sv = sm.predict_variances(Xt)[:, 0]
_var_RMSE = np.linalg.norm(y_sv) / len(Yt)
self.assertLess(_var_RMSE, 1e-6)
Expand Down
25 changes: 8 additions & 17 deletions smt/surrogate_models/krg_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ def _new_train(self):
_,
) = standardization(X2.copy(), y.copy())
D, _ = cross_distances(self.X2_norma)
D = np.abs(D)
self.Lij, self.n_levels = cross_levels(
X=self.X_train, ij=self.ij, design_space=self.design_space
)
Expand Down Expand Up @@ -985,19 +986,7 @@ def _reduced_likelihood_function(self, theta):
X2 - self.X2_offset
) / self.X2_scale
dx, _ = cross_distances(self.X2_norma[str(self._lvl)])
else:
X2, _, _ = self.design_space.unfold_x(
self.training_points[None][0][0]
)
(
self.X2_norma,
_,
self.X2_offset,
_,
self.X2_scale,
_,
) = standardization(X2, self.training_points[None][0][1])
dx, _ = cross_distances(self.X2_norma)

try:
r = self._matrix_data_corr(
corr=self.options["corr"],
Expand All @@ -1012,6 +1001,7 @@ def _reduced_likelihood_function(self, theta):
cat_kernel=self.options["categorical_kernel"],
kplsk_second_loop=self.kplsk_second_loop,
).reshape(-1, 1)

if np.isnan(r).any():
return reduced_likelihood_function_value, par
except FloatingPointError:
Expand Down Expand Up @@ -1580,7 +1570,7 @@ def _predict_init(self, x, is_acting):
if self.options["categorical_kernel"] == MixIntKernelType.CONT_RELAX:
Xpred, _, _ = self.design_space.unfold_x(x)
Xpred_norma = (Xpred - self.X2_offset) / self.X2_scale
dx = differences(Xpred_norma, Y=self.X2_norma.copy())
dx = np.abs(differences(Xpred_norma, Y=self.X2_norma.copy()))

if (
"n_comp" not in self.options._dict.keys()
Expand All @@ -1598,8 +1588,10 @@ def _predict_init(self, x, is_acting):
is_acting_y=self.is_acting_train,
mixint_type=MixIntKernelType.GOWER,
)
if np.any(self.design_space.is_conditionally_acting):
dx[:, np.logical_not(self.unfolded_cat)] = dnum / self.X_scale
if np.any(self.design_space.is_conditionally_acting):
dx[:, np.logical_not(self.unfolded_cat)] = (
dnum / self.X2_scale[np.logical_not(self.unfolded_cat)]
)
Lij, _ = cross_levels(
X=x, ij=ij, design_space=self.design_space, y=self.X_train
)
Expand Down Expand Up @@ -1680,7 +1672,6 @@ def _predict_values(self, x: np.ndarray, is_acting=None) -> np.ndarray:
# Initialization
if not (self.is_continuous):
x, is_acting, n_eval, ij, Lij, dx = self._predict_init(x, is_acting)

r = self._matrix_data_corr(
corr=self.options["corr"],
design_space=self.design_space,
Expand Down
13 changes: 2 additions & 11 deletions smt/utils/kriging.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,8 @@ def gower_componentwise_distances(
Z_num = Z[:, ~cat_features]
z_num_is_acting = z_is_acting[:, ~cat_features]
num_is_decreed = is_decreed[~cat_features]
X_num_out = Z_num[x_index,]
Y_num_out = Z_num[y_index,]
num_bounds = design_space.get_num_bounds()[~cat_features, :]
Z_scale = 1
if num_bounds.shape[0] > 0:
Z_offset = num_bounds[:, 0]
Z_max = num_bounds[:, 1]
Expand All @@ -327,15 +326,7 @@ def gower_componentwise_distances(
y,
hierarchical_kernel,
)
D_num_out, _ = compute_D_num(
X_num_out,
Y_num_out,
x_num_is_acting,
y_num_is_acting,
num_is_decreed,
y,
hierarchical_kernel,
)
D_num_out = D_num * Z_scale
D = np.concatenate((D_cat, D_num), axis=1) * 0
D[:, np.logical_not(cat_features)] = D_num
D[:, cat_features] = D_cat
Expand Down

0 comments on commit 38f6e37

Please sign in to comment.