def setUp(self): ndim = 10 nt = 50 ne = 100 problems = OrderedDict() problems["Branin"] = Branin(ndim=2) problems["Rosenbrock"] = Rosenbrock(ndim=3) problems["sphere"] = Sphere(ndim=ndim) problems["exp"] = TensorProduct(ndim=ndim, func="exp") problems["tanh"] = TensorProduct(ndim=ndim, func="tanh") problems["cos"] = TensorProduct(ndim=ndim, func="cos") sms = OrderedDict() sms["KPLS"] = KPLS(eval_n_comp=True) t_errors = {} e_errors = {} t_errors["KPLS"] = 1e-3 e_errors["KPLS"] = 2.5 n_comp_opt = {} n_comp_opt["Branin"] = 2 n_comp_opt["Rosenbrock"] = 1 n_comp_opt["sphere"] = 1 n_comp_opt["exp"] = 3 n_comp_opt["tanh"] = 1 n_comp_opt["cos"] = 1 self.nt = nt self.ne = ne self.problems = problems self.sms = sms self.t_errors = t_errors self.e_errors = e_errors self.n_comp_opt = n_comp_opt
def test_sphere(self): import numpy as np import matplotlib.pyplot as plt from smt.problems import Sphere ndim = 2 problem = Sphere(ndim=ndim) num = 100 x = np.ones((num, ndim)) x[:, 0] = np.linspace(-10, 10., num) x[:, 1] = 0. y = problem(x) yd = np.empty((num, ndim)) for i in range(ndim): yd[:, i] = problem(x, kx=i).flatten() print(y.shape) print(yd.shape) plt.plot(x[:, 0], y[:, 0]) plt.xlabel('x') plt.ylabel('y') plt.show()
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_gekpls(self): import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from smt.surrogate_models import GEKPLS from smt.problems import Sphere from smt.sampling_methods import LHS # Construction of the DOE fun = Sphere(ndim=2) sampling = LHS(xlimits=fun.xlimits, criterion="m") xt = sampling(20) yt = fun(xt) # Compute the gradient for i in range(2): yd = fun(xt, kx=i) yt = np.concatenate((yt, yd), axis=1) # Build the GEKPLS model n_comp = 2 sm = GEKPLS( theta0=[1e-2] * n_comp, xlimits=fun.xlimits, extra_points=1, print_prediction=False, n_comp=n_comp, ) sm.set_training_values(xt, yt[:, 0]) for i in range(2): sm.set_training_derivatives(xt, yt[:, 1 + i].reshape((yt.shape[0], 1)), i) sm.train() # Test the model X = np.arange(fun.xlimits[0, 0], fun.xlimits[0, 1], 0.25) Y = np.arange(fun.xlimits[1, 0], fun.xlimits[1, 1], 0.25) X, Y = np.meshgrid(X, Y) Z = np.zeros((X.shape[0], X.shape[1])) for i in range(X.shape[0]): for j in range(X.shape[1]): Z[i, j] = sm.predict_values( np.hstack((X[i, j], Y[i, j])).reshape((1, 2)) ) fig = plt.figure() ax = fig.gca(projection="3d") surf = ax.plot_surface(X, Y, Z) plt.show()
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 setUp(self): ndim = 2 self.nt = 50 self.ne = 10 self.problem = Sphere(ndim=ndim) self.sms = sms = OrderedDict() if compiled_available: sms['IDW'] = IDW() sms['RBF'] = RBF() sms['RMTB'] = RMTB(regularization_weight=1e-8, nonlinear_maxiter=100, solver_tolerance=1e-16) sms['RMTC'] = RMTC(regularization_weight=1e-8, nonlinear_maxiter=100, solver_tolerance=1e-16)
def setUp(self): ndim = 3 nt = 500 ne = 100 problems = OrderedDict() problems["sphere"] = Sphere(ndim=ndim) sms = OrderedDict() if compiled_available: sms["RMTC"] = RMTC(num_elements=6, extrapolate=True) sms["RMTB"] = RMTB(order=4, num_ctrl_pts=10, extrapolate=True) self.nt = nt self.ne = ne self.problems = problems self.sms = sms
def setUp(self): ndim = 2 nt = 5000 ne = 100 problems = OrderedDict() problems['sphere'] = Sphere(ndim=ndim) sms = OrderedDict() if compiled_available: sms['RBF'] = RBF() sms['RMTC'] = RMTC() sms['RMTB'] = RMTB() self.nt = nt self.ne = ne self.problems = problems self.sms = sms
def setUp(self): ndim = 2 nt = 5000 ne = 100 problems = OrderedDict() problems['sphere'] = Sphere(ndim=ndim) sms = OrderedDict() if compiled_available: sms['RBF'] = RBF() sms['RMTC'] = RMTC() sms['RMTB'] = RMTB() sms['MFK'] = MFK(theta0=[1e-2] * ndim, eval_noise=True) self.nt = nt self.ne = ne self.problems = problems self.sms = sms
def test_qp_mixed_2D_INT(self): xtypes = [FLOAT, INT] xlimits = [[-10, 10], [-10, 10]] mixint = MixedIntegerContext(xtypes, xlimits) sm = mixint.build_surrogate_model(QP(print_prediction=False)) sampling = mixint.build_sampling_method(LHS, criterion="m") fun = Sphere(ndim=2) xt = sampling(10) yt = fun(xt) sm.set_training_values(xt, yt) sm.train() eq_check = True for i in range(xt.shape[0]): if abs(float(xt[i, :][1]) - int(float(xt[i, :][1]))) > 10e-8: eq_check = False self.assertTrue(eq_check)
def setUp(self): ndim = 2 nt = 5000 ne = 100 problems = OrderedDict() problems["sphere"] = Sphere(ndim=ndim) sms = OrderedDict() if compiled_available: sms["RBF"] = RBF() sms["RMTC"] = RMTC() sms["RMTB"] = RMTB() sms["MFK"] = MFK(theta0=[1e-2] * ndim) self.nt = nt self.ne = ne self.problems = problems self.sms = sms
def setUp(self): ndim = 10 nt = 500 ne = 100 problems = OrderedDict() problems["sphere"] = Sphere(ndim=ndim) problems["exp"] = TensorProduct(ndim=ndim, func="exp") problems["tanh"] = TensorProduct(ndim=ndim, func="tanh") problems["cos"] = TensorProduct(ndim=ndim, func="cos") sms = OrderedDict() sms["LS"] = LS() sms["QP"] = QP() sms["KRG"] = KRG(theta0=[4e-1] * ndim) sms["KPLS"] = KPLS() if compiled_available: sms["IDW"] = IDW() sms["RBF"] = RBF() t_errors = {} t_errors["LS"] = 1.0 t_errors["QP"] = 1.0 t_errors["KRG"] = 1e-4 t_errors["IDW"] = 1e-15 t_errors["RBF"] = 1e-2 t_errors["KPLS"] = 1e-3 e_errors = {} e_errors["LS"] = 2.5 e_errors["QP"] = 2.0 e_errors["KRG"] = 2.0 e_errors["IDW"] = 4 e_errors["RBF"] = 2 e_errors["KPLS"] = 2.5 self.nt = nt self.ne = ne self.problems = problems self.sms = sms self.t_errors = t_errors self.e_errors = e_errors
def test_krg_mixed_3D(self): xtypes = [FLOAT, (ENUM, 3), INT] xlimits = [[-10, 10], ["blue", "red", "green"], [-10, 10]] mixint = MixedIntegerContext(xtypes, xlimits) sm = mixint.build_surrogate_model(KRG(print_prediction=False)) sampling = mixint.build_sampling_method(LHS, criterion="m") fun = Sphere(ndim=3) xt = sampling(20) yt = fun(xt) sm.set_training_values(xt, yt) sm.train() eq_check = True for i in range(xt.shape[0]): if abs(float(xt[i, :][2]) - int(float(xt[i, :][2]))) > 10e-8: eq_check = False if not (xt[i, :][1] == 0 or xt[i, :][1] == 1 or xt[i, :][1] == 2): eq_check = False self.assertTrue(eq_check)
def setUp(self): ndim = 3 nt = 5000 ne = 500 problems = OrderedDict() problems["sphere"] = Sphere(ndim=ndim) problems["exp"] = TensorProduct(ndim=ndim, func="exp") problems["tanh"] = TensorProduct(ndim=ndim, func="tanh") problems["cos"] = TensorProduct(ndim=ndim, func="cos") sms = OrderedDict() if compiled_available: sms["RMTC"] = RMTC() sms["RMTB"] = RMTB() t_errors = {} t_errors["RMTC"] = 1e-1 t_errors["RMTB"] = 1e-1 e_errors = {} e_errors["RMTC"] = 1e-1 e_errors["RMTB"] = 1e-1 ge_t_errors = {} ge_t_errors["RMTC"] = 1e-2 ge_t_errors["RMTB"] = 1e-2 ge_e_errors = {} ge_e_errors["RMTC"] = 1e-2 ge_e_errors["RMTB"] = 1e-2 self.nt = nt self.ne = ne self.problems = problems self.sms = sms self.t_errors = t_errors self.e_errors = e_errors self.ge_t_errors = ge_t_errors self.ge_e_errors = ge_e_errors
def setUp(self): ndim = 3 nt = 5000 ne = 500 problems = OrderedDict() problems['sphere'] = Sphere(ndim=ndim) problems['exp'] = TensorProduct(ndim=ndim, func='exp') problems['tanh'] = TensorProduct(ndim=ndim, func='tanh') problems['cos'] = TensorProduct(ndim=ndim, func='cos') sms = OrderedDict() if compiled_available: sms['RMTC'] = RMTC() sms['RMTB'] = RMTB() t_errors = {} t_errors['RMTC'] = 1e-1 t_errors['RMTB'] = 1e-1 e_errors = {} e_errors['RMTC'] = 1e-1 e_errors['RMTB'] = 1e-1 ge_t_errors = {} ge_t_errors['RMTC'] = 1e-2 ge_t_errors['RMTB'] = 1e-2 ge_e_errors = {} ge_e_errors['RMTC'] = 1e-2 ge_e_errors['RMTB'] = 1e-2 self.nt = nt self.ne = ne self.problems = problems self.sms = sms self.t_errors = t_errors self.e_errors = e_errors self.ge_t_errors = ge_t_errors self.ge_e_errors = ge_e_errors
def setUp(self): ndim = 10 nt = 500 ne = 100 problems = OrderedDict() problems['sphere'] = Sphere(ndim=ndim) problems['exp'] = TensorProduct(ndim=ndim, func='exp') problems['tanh'] = TensorProduct(ndim=ndim, func='tanh') problems['cos'] = TensorProduct(ndim=ndim, func='cos') sms = OrderedDict() sms['LS'] = LS() sms['QP'] = QP() sms['KRG'] = KRG(theta0=[4e-1]*ndim) if compiled_available: sms['IDW'] = IDW() sms['RBF'] = RBF() t_errors = {} t_errors['LS'] = 1.0 t_errors['QP'] = 1.0 t_errors['KRG'] = 1e-6 t_errors['IDW'] = 1e-15 t_errors['RBF'] = 1e-2 e_errors = {} e_errors['LS'] = 2.5 e_errors['QP'] = 2.0 e_errors['KRG'] = 2.0 e_errors['IDW'] = 1.5 e_errors['RBF'] = 1.5 self.nt = nt self.ne = ne self.problems = problems self.sms = sms self.t_errors = t_errors self.e_errors = e_errors
def test_derivatives(self): # Construction of the DOE fun = Sphere(ndim=2) sampling = LHS(xlimits=fun.xlimits, criterion="m") xt = sampling(20) yt = fun(xt) # Compute the training derivatives for i in range(2): yd = fun(xt, kx=i) yt = np.concatenate((yt, yd), axis=1) # check KRG models sm_krg_c = KRG(poly="constant", print_global=False) sm_krg_c.set_training_values(xt, yt[:, 0]) sm_krg_c.train() TestKRG._check_derivatives(sm_krg_c, xt, yt) sm_krg_l = KRG(poly="linear", print_global=False) sm_krg_l.set_training_values(xt, yt[:, 0]) sm_krg_l.train() TestKRG._check_derivatives(sm_krg_l, xt, yt)
def setUp(self): ndim = 2 nt = 10000 ne = 1000 problems = OrderedDict() problems["sphere"] = Sphere(ndim=ndim) problems["exp"] = TensorProduct(ndim=ndim, func="exp", width=5) problems["tanh"] = TensorProduct(ndim=ndim, func="tanh", width=5) problems["cos"] = TensorProduct(ndim=ndim, func="cos", width=5) sms = OrderedDict() sms["LS"] = LS() sms["QP"] = QP() if compiled_available: sms["RMTC"] = RMTC(num_elements=20, energy_weight=1e-10) sms["RMTB"] = RMTB(num_ctrl_pts=40, energy_weight=1e-10) t_errors = {} t_errors["LS"] = 1.0 t_errors["QP"] = 1.0 t_errors["RMTC"] = 1.0 t_errors["RMTB"] = 1.0 e_errors = {} e_errors["LS"] = 1.5 e_errors["QP"] = 1.5 e_errors["RMTC"] = 1.0 e_errors["RMTB"] = 1.0 self.nt = nt self.ne = ne self.problems = problems self.sms = sms self.t_errors = t_errors self.e_errors = e_errors
def setUp(self): ndim = 2 nt = 10000 ne = 1000 problems = OrderedDict() problems['sphere'] = Sphere(ndim=ndim) problems['exp'] = TensorProduct(ndim=ndim, func='exp', width=5) problems['tanh'] = TensorProduct(ndim=ndim, func='tanh', width=5) problems['cos'] = TensorProduct(ndim=ndim, func='cos', width=5) sms = OrderedDict() sms['LS'] = LS() sms['QP'] = QP() if compiled_available: sms['RMTC'] = RMTC(num_elements=20, energy_weight=1e-10) sms['RMTB'] = RMTB(num_ctrl_pts=40, energy_weight=1e-10) t_errors = {} t_errors['LS'] = 1.0 t_errors['QP'] = 1.0 t_errors['RMTC'] = 1e-2 t_errors['RMTB'] = 1e-2 e_errors = {} e_errors['LS'] = 1.5 e_errors['QP'] = 1.5 e_errors['RMTC'] = 1e-2 e_errors['RMTB'] = 1e-2 self.nt = nt self.ne = ne self.problems = problems self.sms = sms self.t_errors = t_errors self.e_errors = e_errors
def test_sphere(self): self.run_test(Sphere(ndim=1)) self.run_test(Sphere(ndim=3))
except: compiled_available = False try: import matplotlib.pyplot as plt plot_status = True except: plot_status = False ########### Initialization of the problem, construction of the training and validation points ndim = 10 ndoe = int(10 * ndim) # Define the function fun = Sphere(ndim=ndim) # Construction of the DOE sampling = LHS(xlimits=fun.xlimits, criterion="m") xt = sampling(ndoe) # Compute the output yt = fun(xt) # Compute the gradient for i in range(ndim): yd = fun(xt, kx=i) yt = np.concatenate((yt, yd), axis=1) # Construction of the validation points ntest = 500 sampling = LHS(xlimits=fun.xlimits) xtest = sampling(ntest)