Skip to content

Commit

Permalink
Clean internal optimization within Kriging (SMTorg#505)
Browse files Browse the repository at this point in the history
* test TNC

* cobyla for sgp

* fix for gekpls

* fix gradient algorithm

* pl_exp_3
  • Loading branch information
Paul-Saves authored Jan 30, 2024
1 parent 5f50bc4 commit 7320305
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 17 deletions.
7 changes: 7 additions & 0 deletions smt/applications/mfkpls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def _initialize(self):
desc="Correlation function type",
types=(str),
)
declare(
"hyper_opt",
"Cobyla",
values=("Cobyla"),
desc="Optimiser for hyperparameters optimisation",
types=str,
)
declare("n_comp", 1, types=int, desc="Number of principal components")
self.name = "MFKPLS"

Expand Down
7 changes: 7 additions & 0 deletions smt/applications/mfkplsk.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def _initialize(self):
desc="Correlation function type",
types=(str),
)
declare(
"hyper_opt",
"Cobyla",
values=("Cobyla"),
desc="Optimiser for hyperparameters optimisation",
types=str,
)
self.name = "MFKPLSK"

def _componentwise_distance(self, dx, opt=0):
Expand Down
4 changes: 2 additions & 2 deletions smt/applications/mixed_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ def __init__(
"""
super().__init__()
self._surrogate = surrogate

if isinstance(self._surrogate, KrgBased):
raise ValueError(
"Using MixedIntegerSurrogateModel integer model with "
Expand All @@ -109,7 +108,6 @@ def __init__(
self._input_in_folded_space = input_in_folded_space
self.supports = self._surrogate.supports
self.options["print_global"] = False

if "poly" in self._surrogate.options:
if self._surrogate.options["poly"] != "constant":
raise ValueError("constant regression must be used with mixed integer")
Expand Down Expand Up @@ -198,6 +196,7 @@ def __init__(
+ " is not supported. Please use MixedIntegerSurrogateModel instead."
)
self.options["design_space"] = self._surrogate.design_space
self._surrogate.options["hyper_opt"] = "Cobyla"

self._input_in_folded_space = input_in_folded_space
self.supports = self._surrogate.supports
Expand Down Expand Up @@ -333,6 +332,7 @@ def build_kriging_model(self, surrogate):
Build MixedIntegerKrigingModel from given SMT surrogate model.
"""
surrogate.options["design_space"] = self._design_space
surrogate.options["hyper_opt"] = "Cobyla"
return MixedIntegerKrigingModel(
surrogate=surrogate,
input_in_folded_space=self._work_in_folded_space,
Expand Down
24 changes: 19 additions & 5 deletions smt/applications/tests/test_vfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ def test_vfm(self):
Bridge_candidate = "KRG"
type_bridge = "Multiplicative"
optionsLF = {}
optionsB = {"theta0": [1e-2] * ndim, "print_prediction": False, "deriv": False}
optionsB = {
"theta0": [1e-2] * ndim,
"print_prediction": False,
"deriv": False,
"hyper_opt": "Cobyla",
}

# Construct low/high fidelity data and validation points
sampling = LHS(xlimits=funLF.xlimits, criterion="m", random_state=42)
Expand Down Expand Up @@ -138,7 +143,12 @@ def run_vfm_example(self):
Bridge_candidate = "KRG"
type_bridge = "Multiplicative"
optionsLF = {}
optionsB = {"theta0": [1e-2] * ndim, "print_prediction": False, "deriv": False}
optionsB = {
"theta0": [1e-2] * ndim,
"print_prediction": False,
"deriv": False,
"hyper_opt": "Cobyla",
}

# Construct low/high fidelity data and validation points
sampling = LHS(xlimits=funLF.xlimits, criterion="m")
Expand Down Expand Up @@ -200,7 +210,9 @@ def test_KRG_KRG_additive(self):
yp = M.predict_values(np.atleast_2d(xt[0]))
dyp = M.predict_derivatives(np.atleast_2d(xt[0]), kx=0)
self.assert_error(yp, np.array([[0.015368, 0.367424]]), atol=2e-2, rtol=3e-2)
self.assert_error(dyp, np.array([[0.07007729, 3.619421]]), atol=3e-1, rtol=1e-2)
self.assert_error(
dyp, np.array([[-3.11718627e-03, 3.19506239e00]]), atol=3e-1, rtol=1e-2
)

def test_QP_KRG_additive(self):
with Silence():
Expand All @@ -214,7 +226,7 @@ def test_QP_KRG_additive(self):

self.assert_error(yp, np.array([[0.015368, 0.367424]]), atol=1e-2, rtol=1e-2)
self.assert_error(
dyp, np.array([[1.16130832e-03, 4.36712162e00]]), atol=3e-1, rtol=1e-2
dyp, np.array([[0.02596425, 4.70243162]]), atol=3e-1, rtol=1e-2
)

def test_KRG_KRG_mult(self):
Expand All @@ -228,7 +240,9 @@ def test_KRG_KRG_mult(self):
dyp = M.predict_derivatives(np.atleast_2d(xt[0]), kx=0)

self.assert_error(yp, np.array([[0.015368, 0.367424]]), atol=2e-2, rtol=3e-2)
self.assert_error(dyp, np.array([[0.07007729, 3.619421]]), atol=3e-1, rtol=1e-2)
self.assert_error(
dyp, np.array([[-3.11718627e-03, 3.19506239e00]]), atol=3e-1, rtol=1e-2
)

def test_QP_KRG_mult(self):
with Silence():
Expand Down
7 changes: 7 additions & 0 deletions smt/surrogate_models/gekpls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def _initialize(self):
types=int,
desc="Number of extra points per training point",
)
declare(
"hyper_opt",
"Cobyla",
values=("Cobyla"),
desc="Optimiser for hyperparameters optimisation",
types=str,
)
self.supports["training_derivatives"] = True

def _check_param(self):
Expand Down
10 changes: 7 additions & 3 deletions smt/surrogate_models/krg_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _initialize(self):
)
declare(
"hyper_opt",
"Cobyla",
"TNC",
values=("Cobyla", "TNC"),
desc="Optimiser for hyperparameters optimisation",
types=str,
Expand Down Expand Up @@ -1016,7 +1016,7 @@ def _reduced_likelihood_gradient(self, theta):
gamma = par["gamma"]
Q = par["Q"]
G = par["G"]
sigma_2 = par["sigma2"]
sigma_2 = par["sigma2"] + self.options["nugget"]

nb_theta = len(theta)
grad_red = np.zeros(nb_theta)
Expand Down Expand Up @@ -1908,6 +1908,10 @@ def grad_minus_reduced_likelihood_function(log10t):
optimal_theta_res = optimal_theta_res_loop

elif self.options["hyper_opt"] == "TNC":
if self.options["use_het_noise"]:
raise ValueError(
"For heteroscedastic noise, please use Cobyla"
)
theta_all_loops = 10**theta_all_loops
for theta0_loop in theta_all_loops:
optimal_theta_res_loop = optimize.minimize(
Expand All @@ -1916,7 +1920,7 @@ def grad_minus_reduced_likelihood_function(log10t):
method="TNC",
jac=grad_minus_reduced_likelihood_function,
bounds=bounds_hyp,
options={"maxiter": 100},
options={"maxfun": 2 * limit},
)
if optimal_theta_res_loop["fun"] < optimal_theta_res["fun"]:
optimal_theta_res = optimal_theta_res_loop
Expand Down
7 changes: 7 additions & 0 deletions smt/surrogate_models/sgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ def _initialize(self):
desc="Gaussian noise on observed training data",
types=(list, np.ndarray),
)
declare(
"hyper_opt",
"Cobyla",
values=("Cobyla"),
desc="Optimiser for hyperparameters optimisation",
types=str,
)
declare(
"eval_noise",
True, # for SGP evaluate noise by default
Expand Down
8 changes: 7 additions & 1 deletion smt/surrogate_models/tests/test_krg_het_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ def test_predict_output(self):
xt_full = np.array(3 * xt.tolist())
yt_full = np.concatenate((yt, yt + 0.2 * yt_std_rand, yt - 0.2 * yt_std_rand))

sm = KRG(theta0=[1.0], eval_noise=True, use_het_noise=True, n_start=1)
sm = KRG(
theta0=[1.0],
eval_noise=True,
use_het_noise=True,
n_start=1,
hyper_opt="Cobyla",
)
sm.set_training_values(xt_full, yt_full)
sm.train()

Expand Down
2 changes: 1 addition & 1 deletion smt/tests/test_array_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_KRG(self):
d0 = interp.predict_derivatives(np.atleast_2d(xt[10, :]), 0)

self.assert_error(
d0, np.array([[0.06874097, 4.366292277996716]]), atol=0.55, rtol=0.15
d0, np.array([[0.24897752, 3.72290526]]), atol=0.55, rtol=0.15
)

def test_RBF(self):
Expand Down
7 changes: 2 additions & 5 deletions smt/tests/test_kpls_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ def setUp(self):
n_comp_opt["Branin"] = 2
n_comp_opt["Rosenbrock"] = 1
n_comp_opt["sphere"] = 1
if platform.startswith("linux"): # result depends on platform
n_comp_opt["exp"] = 2
else:
n_comp_opt["exp"] = 3
n_comp_opt["exp"] = 3
n_comp_opt["tanh"] = 1
n_comp_opt["cos"] = 1
n_comp_opt["cos"] = 2

self.nt = nt
self.ne = ne
Expand Down

0 comments on commit 7320305

Please sign in to comment.