def test_KRG_KRG_additive(self): with Silence(): M, xt = setupCRM( LF_candidate="KRG", Bridge_candidate="KRG", type_bridge="Additive" ) with Silence(): 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)
def run_test(self): method_name = inspect.stack()[1][3] sname = method_name.split("_")[1] prob = self.problem sampling = FullFactorial(xlimits=prob.xlimits, clip=False) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) # dyt = {} # for kx in range(prob.xlimits.shape[0]): # dyt[kx] = prob(xt, kx=kx) np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) sm0 = self.sms[sname] sm = sm0.__class__() sm.options = sm0.options.clone() if sm.options.is_declared("xlimits"): sm.options["xlimits"] = prob.xlimits sm.options["print_global"] = False sm.set_training_values(xt, yt) sm.update_training_values(yt) with Silence(): sm.train() ye0 = sm.predict_values(xe) h = 1e-3 jac_fd = np.zeros((self.ne, self.nt)) for ind in range(self.nt): sm.update_training_values(yt + h * np.eye(self.nt, M=1, k=-ind)) with Silence(): sm.train() ye = sm.predict_values(xe) jac_fd[:, ind] = (ye - ye0)[:, 0] / h jac_fd = jac_fd.reshape((self.ne, self.nt, 1)) jac_an = sm.predict_output_derivatives(xe)[None] if print_output: print(np.linalg.norm(jac_fd - jac_an)) self.assert_error(jac_fd, jac_an, rtol=5e-2)
def test_QP_KRG_additive(self): with Silence(): M, xt = setupCRM( LF_candidate="QP", Bridge_candidate="KRG", type_bridge="Additive" ) with Silence(): 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=1e-2, rtol=1e-2) self.assert_error( dyp, np.array([[1.16130832e-03, 4.36712162e00]]), atol=3e-1, rtol=1e-2 )
def test_KRG_KRG_mult(self): with Silence(): M, xt = setupCRM( LF_candidate="KRG", Bridge_candidate="KRG", type_bridge="Multiplicative" ) with Silence(): 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.01569909, 0.3669085]]), atol=2e-2, rtol=2e-2) self.assert_error( dyp, np.array([[0.11448649, 5.2084347]]), atol=5e-1, rtol=8e-2 )
def test_QP_KRG_additive(self): with Silence(): M, xt = setupCRM( LF_candidate="QP", Bridge_candidate="KRG", type_bridge="Additive" ) with Silence(): 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.0142592, 0.36382042]]), atol=1e-2, rtol=1e-2) self.assert_error( dyp, np.array([[0.2780505, 4.91227849]]), atol=3e-1, rtol=1e-2 )
def test_mfk_1fidelity(self): self.problems = ["exp", "tanh", "cos"] for fname in self.problems: prob = TensorProduct(ndim=self.ndim, func=fname) sampling = LHS(xlimits=prob.xlimits, random_state=0) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) for i in range(self.ndim): yt = np.concatenate((yt, prob(xt, kx=i)), axis=1) sampling = LHS(xlimits=prob.xlimits, random_state=1) xv = sampling(self.ne) yv = prob(xv) sm = MFK( theta0=[1e-2] * self.ndim, print_global=False, ) sm.set_training_values(xt, yt[:, 0]) with Silence(): sm.train() t_error = compute_rms_error(sm) e_error = compute_rms_error(sm, xv, yv) self.assert_error(t_error, 0.0, 1e-6) self.assert_error(e_error, 0.0, 1e-6)
def run_test(self, sname, extrap_train=False, extrap_predict=False): prob = self.problems['carre'] sampling = LHS(xlimits=prob.xlimits) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) sm0 = self.sms[sname] sm = sm0.__class__() sm.options = sm0.options.clone() if sm.options.is_declared('xlimits'): sm.options['xlimits'] = prob.xlimits sm.options['print_global'] = False x = np.zeros((1, xt.shape[1])) x[0, :] = prob.xlimits[:, 1] + 1.0 y = prob(x) sm.training_pts = {'exact': {}} sm.add_training_pts('exact', xt, yt) if extrap_train: sm.add_training_pts('exact', x, y) with Silence(): sm.train() if extrap_predict: sm.predict(x)
def run_test(self, sname, extrap_train=False, extrap_predict=False): prob = self.problems["sphere"] sampling = LHS(xlimits=prob.xlimits) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) sm0 = self.sms[sname] sm = sm0.__class__() sm.options = sm0.options.clone() if sm.options.is_declared("xlimits"): sm.options["xlimits"] = prob.xlimits sm.options["print_global"] = False x = np.zeros((1, xt.shape[1])) x[0, :] = prob.xlimits[:, 1] + 1.0 y = prob(x) sm.set_training_values(xt, yt) if extrap_train: sm.set_training_values(x, y) with Silence(): sm.train() if extrap_predict: sm.predict_values(x)
def test_IDW(self): xt, yt, interp = setup_sm(sm_name='IDW') with Silence(): d0 = interp.predict_derivatives(np.atleast_2d(xt[10, :]), 0) self.assert_error(d0, np.array([[0., 0.]]), atol=0.2, rtol=0.03)
def test_mfk(self): self.problems = ["exp", "tanh", "cos"] for fname in self.problems: prob = TensorProduct(ndim=self.ndim, func=fname) sampling = FullFactorial(xlimits=prob.xlimits, clip=True) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) for i in range(self.ndim): yt = np.concatenate((yt, prob(xt, kx=i)), axis=1) y_lf = 2 * prob(xt) + 2 x_lf = deepcopy(xt) np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) sm = MFK(theta0=[1e-2] * self.ndim) if sm.options.is_declared("xlimits"): sm.options["xlimits"] = prob.xlimits sm.options["print_global"] = False sm.set_training_values(xt, yt[:, 0]) sm.set_training_values(x_lf, y_lf[:, 0], name=0) with Silence(): sm.train() t_error = compute_rms_error(sm) e_error = compute_rms_error(sm, xe, ye) self.assert_error(t_error, 0.0, 1) self.assert_error(e_error, 0.0, 1)
def test_mfkplsk_derivs(self): if self.ndim < 2: print("To try test_mfkplsk_derivs the dimension must be greater than 1") prob = Sphere(ndim=self.ndim) sampling = LHS(xlimits=prob.xlimits) # Modif MM nt = 100 np.random.seed(0) xt = sampling(nt) yt = prob(xt) dyt = {} for kx in range(prob.xlimits.shape[0]): dyt[kx] = prob(xt, kx=kx) y_lf = 2 * prob(xt) + 2 x_lf = deepcopy(xt) np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) dye = {} for kx in range(prob.xlimits.shape[0]): dye[kx] = prob(xe, kx=kx) # modif MM sm = MFKPLSK() if sm.options.is_declared("xlimits"): sm.options["xlimits"] = prob.xlimits sm.options["print_global"] = False # to test some options sm.options["eval_noise"] = False # modif MM sm.options["n_comp"] = self.n_comp sm.options["theta0"] = [1e-2] * self.n_comp sm.set_training_values(xt, yt) sm.set_training_values(x_lf, y_lf, name=0) with Silence(): sm.train() t_error = compute_rms_error(sm) e_error = compute_rms_error(sm, xe, ye) e_error0 = compute_rms_error(sm, xe, dye[0], 0) e_error1 = compute_rms_error(sm, xe, dye[1], 1) if print_output: print( "%8s %6s %18.9e %18.9e %18.9e %18.9e" % (pname[:6], sname, t_error, e_error, e_error0, e_error1) ) self.assert_error(e_error0, 0.0, 1e-1) self.assert_error(e_error1, 0.0, 1e-1)
def test_QP_KRG_mult(self): with Silence(): M, xt = setupCRM( LF_candidate="QP", Bridge_candidate="KRG", type_bridge="Multiplicative" ) with Silence(): 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.01537882, 0.36681699]]), atol=3e-1, rtol=1e-2 ) self.assert_error( dyp, np.array([[0.21520949, 4.50217261]]), atol=3e-1, rtol=1e-2 )
def test_KRG(self): xt, yt, interp = setup_sm(sm_name="KRG") with Silence(): 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)
def test_LS(self): xt, yt, interp = setup_sm(sm_name='LS') with Silence(): d0 = interp.predict_derivatives(np.atleast_2d(xt[10, :]), 0) self.assert_error(d0, np.array([[0.2912748, 5.39911101]]), atol=0.2, rtol=0.03)
def test_KRG(self): xt, yt, interp = setup_sm(sm_name='KRG') with Silence(): d0 = interp.predict_derivatives(np.atleast_2d(xt[10, :]), 0) self.assert_error(d0, np.array([[0.06874097, 5.26604232]]), atol=0.4, rtol=0.1)
def test_RBF(self): xt, yt, interp = setup_sm(sm_name='RBF') with Silence(): d0 = interp.predict_derivatives(np.atleast_2d(xt[10, :]), 0) self.assert_error(d0, np.array([[0.15741522, 4.80265154]]), atol=0.2, rtol=0.03)
def test_QP(self): xt, yt, interp = setup_sm(sm_name='QP') with Silence(): d0 = interp.predict_derivatives(np.atleast_2d(xt[10, :]), 0) self.assert_error(d0, np.array([[0.02588578, 5.86555448]]), atol=1e-6, rtol=1e-6)
def setup_sm(sm_name, settings={}): xt, yt, xlimits = get_rans_crm_wing() _tmp = __import__('smt', globals(), locals(), ['surrogate_models'], 0) interp = getattr(_tmp.surrogate_models, sm_name)(**settings) interp.set_training_values(xt, yt) with Silence(): interp.train() return xt, yt, interp
def test_linear_search(self): for ls in ["bracketed", "cubic", "quadratic", "null"]: self.sms[ls] = RMTB(xlimits=self.xlimits, line_search=ls, print_global=False) self.sms[ls].set_training_values(self.xt, self.yt) with Silence(): self.sms[ls].train() error = compute_rms_error(self.sms[ls], self.xref, self.yref) self.assert_error(error, 0.0, 1e-1)
def run_MF_test(self): method_name = inspect.stack()[1][3] pname = method_name.split("_")[1] sname = method_name.split("_")[2] prob = self.problems[pname] sampling = LHS(xlimits=prob.xlimits) nt = 500 np.random.seed(0) xt = sampling(500) yt = prob(xt) dyt = {} for kx in range(prob.xlimits.shape[0]): dyt[kx] = prob(xt, kx=kx) y_lf = 2 * prob(xt) + 2 x_lf = deepcopy(xt) np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) dye = {} for kx in range(prob.xlimits.shape[0]): dye[kx] = prob(xe, kx=kx) sm0 = self.sms[sname] sm = sm0.__class__() sm.options = sm0.options.clone() if sm.options.is_declared("xlimits"): sm.options["xlimits"] = prob.xlimits sm.options["print_global"] = False sm.set_training_values(xt, yt) sm.set_training_values(x_lf, y_lf, name=0) with Silence(): sm.train() t_error = compute_rms_error(sm) e_error = compute_rms_error(sm, xe, ye) e_error0 = compute_rms_error(sm, xe, dye[0], 0) e_error1 = compute_rms_error(sm, xe, dye[1], 1) if print_output: print( "%8s %6s %18.9e %18.9e %18.9e %18.9e" % (pname[:6], sname, t_error, e_error, e_error0, e_error1) ) self.assert_error(e_error0, 0.0, 1e-1) self.assert_error(e_error1, 0.0, 1e-1)
def run_test(self): method_name = inspect.stack()[1][3] pname = method_name.split("_")[1] sname = method_name.split("_")[2] prob = self.problems[pname] sampling = FullFactorial(xlimits=prob.xlimits, clip=True) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) print(prob(xt, kx=0).shape) for i in range(self.ndim): yt = np.concatenate((yt, prob(xt, kx=i)), axis=1) np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) sm0 = self.sms[sname] sm = sm0.__class__() sm.options = sm0.options.clone() if sm.options.is_declared("xlimits"): sm.options["xlimits"] = prob.xlimits sm.options["print_global"] = False if sname in ["KPLS", "KRG", "KPLSK", "GEKPLS"]: optname = method_name.split("_")[3] sm.options["hyper_opt"] = optname sm.set_training_values(xt, yt[:, 0]) if sm.supports["training_derivatives"]: for i in range(self.ndim): sm.set_training_derivatives(xt, yt[:, i + 1], i) with Silence(): sm.train() t_error = compute_rms_error(sm) e_error = compute_rms_error(sm, xe, ye) if sm.supports["variances"]: sm.predict_variances(xe) if pname == "cos": self.assertLessEqual(e_error, self.e_errors[sname] + 1.5) else: self.assertLessEqual(e_error, self.e_errors[sname] + 1e-4) self.assertLessEqual(t_error, self.t_errors[sname] + 1e-4)
def test_mfk_derivs(self): prob = Sphere(ndim=self.ndim) sampling = LHS(xlimits=prob.xlimits) nt = 500 np.random.seed(0) xt = sampling(nt) yt = prob(xt) dyt = {} for kx in range(prob.xlimits.shape[0]): dyt[kx] = prob(xt, kx=kx) y_lf = 2 * prob(xt) + 2 x_lf = deepcopy(xt) np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) dye = {} for kx in range(prob.xlimits.shape[0]): dye[kx] = prob(xe, kx=kx) sm = MFK(theta0=[1e-2] * self.ndim) if sm.options.is_declared("xlimits"): sm.options["xlimits"] = prob.xlimits sm.options["print_global"] = False sm.set_training_values(xt, yt) sm.set_training_values(x_lf, y_lf, name=0) with Silence(): sm.train() t_error = compute_rms_error(sm) e_error = compute_rms_error(sm, xe, ye) e_error0 = compute_rms_error(sm, xe, dye[0], 0) e_error1 = compute_rms_error(sm, xe, dye[1], 1) if print_output: print( "%8s %6s %18.9e %18.9e %18.9e %18.9e" % (pname[:6], sname, t_error, e_error, e_error0, e_error1) ) self.assert_error(e_error0, 0.0, 1e-1) self.assert_error(e_error1, 0.0, 1e-1)
def run_test(self): method_name = inspect.stack()[1][3] pname = method_name.split('_')[1] sname = method_name.split('_')[2] prob = self.problems[pname] sampling = LHS(xlimits=prob.xlimits) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) dyt = {} for kx in range(prob.xlimits.shape[0]): dyt[kx] = prob(xt, kx=kx) np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) dye = {} for kx in range(prob.xlimits.shape[0]): dye[kx] = prob(xe, kx=kx) sm0 = self.sms[sname] sm = sm0.__class__() sm.options = sm0.options.clone() if sm.options.is_declared('xlimits'): sm.options['xlimits'] = prob.xlimits sm.options['print_global'] = False sm.training_pts = {'exact': {}} sm.add_training_pts('exact', xt, yt) with Silence(): sm.train() t_error = sm.compute_rms_error() e_error = sm.compute_rms_error(xe, ye) e_error0 = sm.compute_rms_error(xe, dye[0], 0) e_error1 = sm.compute_rms_error(xe, dye[1], 1) if print_output: print('%8s %6s %18.9e %18.9e %18.9e %18.9e' % (pname[:6], sname, t_error, e_error, e_error0, e_error1)) self.assert_error(e_error0, 0., 1e-1) self.assert_error(e_error1, 0., 1e-1)
def setUp(self): # xt = np.random.rand(5, 1) * 10.0 self.xt = np.array( [[3.6566495, 4.64266046, 7.23645433, 6.04862594, 8.85571712]]).T self.yt = function_test_1d(self.xt) self.xlimits = np.array([[0.0, 25.0]]) self.smref = smref = RMTB(xlimits=self.xlimits, print_global=False) smref.set_training_values(self.xt, self.yt) with Silence(): smref.train() self.xref = np.array([[0.0, 6.25, 12.5, 18.75, 25.0]]).T self.yref = smref.predict_values(self.xref) self.sms = {}
def run_test(self): method_name = inspect.stack()[1][3] pname = method_name.split("_")[1] sname = method_name.split("_")[2] prob = self.problems[pname] sampling = LHS(xlimits=prob.xlimits, random_state=42) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) sm0 = self.sms[sname] sm = sm0.__class__() sm.options = sm0.options.clone() if sm.options.is_declared("xlimits"): sm.options["xlimits"] = prob.xlimits sm.options["print_global"] = False sm.set_training_values(xt, yt) with Silence(): sm.train() l = sm.options["n_comp"] t_error = compute_rms_error(sm) e_error = compute_rms_error(sm, xe, ye) if print_output: print("%8s %6s %18.9e %18.9e" % (pname[:6], sname, t_error, e_error)) self.assert_error(t_error, 0.0, self.t_errors[sname], 1e-5) self.assert_error(e_error, 0.0, self.e_errors[sname], 1e-5) self.assertEqual(l, self.n_comp_opt[pname])
def run_MF_test(self): method_name = inspect.stack()[1][3] pname = method_name.split("_")[1] sname = method_name.split("_")[2] prob = self.problems[pname] sampling = FullFactorial(xlimits=prob.xlimits, clip=True) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) print(prob(xt, kx=0).shape) for i in range(self.ndim): yt = np.concatenate((yt, prob(xt, kx=i)), axis=1) y_lf = 2 * prob(xt) + 2 x_lf = deepcopy(xt) np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) sm0 = self.sms[sname] sm = sm0.__class__() sm.options = sm0.options.clone() if sm.options.is_declared("xlimits"): sm.options["xlimits"] = prob.xlimits sm.options["print_global"] = False sm.set_training_values(xt, yt[:, 0]) sm.set_training_values(x_lf, y_lf[:, 0], name=0) if sm.supports["training_derivatives"]: for i in range(self.ndim): sm.set_training_derivatives(xt, yt[:, i + 1], i) with Silence(): sm.train() t_error = compute_rms_error(sm) e_error = compute_rms_error(sm, xe, ye)
def run_test(self): method_name = inspect.stack()[1][3] pname = method_name.split('_')[1] sname = method_name.split('_')[2] prob = self.problems[pname] sampling = FullFactorial(xlimits=prob.xlimits, clip=True) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) sm0 = self.sms[sname] sm = sm0.__class__() sm.options = sm0.options.clone() if sm.options.is_declared('xlimits'): sm.options['xlimits'] = prob.xlimits sm.options['print_global'] = False sm.training_pts = {'exact': {}} sm.add_training_pts('exact', xt, yt) with Silence(): sm.train() t_error = sm.compute_rms_error() e_error = sm.compute_rms_error(xe, ye) if print_output: print('%8s %6s %18.9e %18.9e' % (pname[:6], sname, t_error, e_error)) self.assert_error(t_error, 0., self.t_errors[sname]) self.assert_error(e_error, 0., self.e_errors[sname])
def test_linear_solver(self): for ls in [ "krylov-dense", "dense-chol", "lu", "ilu", "krylov", "krylov-lu", "krylov-mg", "gs", "jacobi", "mg", "null", ]: self.sms[ls] = RMTB(xlimits=self.xlimits, solver=ls, print_global=False) self.sms[ls].set_training_values(self.xt, self.yt) with Silence(): self.sms[ls].train() error = compute_rms_error(self.sms[ls], self.xref, self.yref) self.assert_error(error, 0.0, 1.1)
def run_test(self): method_name = inspect.stack()[1][3] pname = method_name.split("_")[1] sname = method_name.split("_")[2] prob = self.problems[pname] sampling = FullFactorial(xlimits=prob.xlimits, clip=True) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) dyt = {} for kx in range(prob.xlimits.shape[0]): dyt[kx] = prob(xt, kx=kx) np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) dye = {} for kx in range(prob.xlimits.shape[0]): dye[kx] = prob(xe, kx=kx) sm0 = self.sms[sname] sm = sm0.__class__() sm.options = sm0.options.clone() if sm.options.is_declared("xlimits"): sm.options["xlimits"] = prob.xlimits sm.options["print_global"] = False sm.set_training_values(xt, yt) with Silence(): sm.train() t_error = compute_rms_error(sm) e_error = compute_rms_error(sm, xe, ye) sm = sm0.__class__() sm.options = sm0.options.clone() if sm.options.is_declared("xlimits"): sm.options["xlimits"] = prob.xlimits sm.options["print_global"] = False sm.set_training_values(xt, yt) for kx in range(prob.xlimits.shape[0]): sm.set_training_derivatives(xt, dyt[kx], kx) with Silence(): sm.train() ge_t_error = compute_rms_error(sm) ge_e_error = compute_rms_error(sm, xe, ye) if print_output: print("%8s %6s %18.9e %18.9e %18.9e %18.9e" % (pname[:6], sname, t_error, e_error, ge_t_error, ge_e_error)) self.assert_error(t_error, 0.0, self.t_errors[sname]) self.assert_error(e_error, 0.0, self.e_errors[sname]) self.assert_error(ge_t_error, 0.0, self.ge_t_errors[sname]) self.assert_error(ge_e_error, 0.0, self.ge_e_errors[sname])