def setUp(self): ndim = 3 nt = 100 ne = 100 ncomp = 1 problems = OrderedDict() 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=[1e-2] * ndim) sms['KPLS'] = KPLS(theta0=[1e-2] * ncomp, n_comp=ncomp) sms['KPLSK'] = KPLSK(theta0=[1] * ncomp, n_comp=ncomp) sms['GEKPLS'] = GEKPLS(theta0=[1e-2] * ncomp, n_comp=ncomp, delta_x=1e-1) if compiled_available: sms['IDW'] = IDW() sms['RBF'] = RBF() sms['RMTC'] = RMTC() sms['RMTB'] = RMTB() t_errors = {} t_errors['LS'] = 1.0 t_errors['QP'] = 1.0 t_errors['KRG'] = 1e-5 t_errors['KPLS'] = 1e-5 t_errors['KPLSK'] = 1e-5 t_errors['GEKPLS'] = 1e-5 if compiled_available: t_errors['IDW'] = 1e-15 t_errors['RBF'] = 1e-2 t_errors['RMTC'] = 1e-1 t_errors['RMTB'] = 1e-1 e_errors = {} e_errors['LS'] = 1.5 e_errors['QP'] = 1.5 e_errors['KRG'] = 1e-2 e_errors['KPLS'] = 1e-2 e_errors['KPLSK'] = 1e-2 e_errors['GEKPLS'] = 1e-2 if compiled_available: e_errors['IDW'] = 1e0 e_errors['RBF'] = 1e0 e_errors['RMTC'] = 2e-1 e_errors['RMTB'] = 2e-1 self.nt = nt self.ne = ne self.ndim = ndim self.problems = problems self.sms = sms self.t_errors = t_errors self.e_errors = e_errors
def test_rmtb(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import RMTB xt = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) yt = np.array([0.0, 1.0, 1.5, 0.5, 1.0]) xlimits = np.array([[0.0, 4.0]]) sm = RMTB( xlimits=xlimits, order=4, num_ctrl_pts=20, energy_weight=1e-15, regularization_weight=0.0, ) sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) plt.plot(xt, yt, "o") plt.plot(x, y) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction"]) plt.show()
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 get_F110_interp(): this_dir = os.path.split(__file__)[0] xt, yt, xlimits = get_data() interp = RMTB( xlimits=xlimits, num_ctrl_pts=15, order=4, approx_order=2, nonlinear_maxiter=40, solver_tolerance=1.e-20, # solver='lu', derivative_solver='lu', energy_weight=1.e-4, regularization_weight=0.e-18, extrapolate=False, print_global=False, data_dir=os.path.join(this_dir, '_smt_cache'), ) # interp = KRG(theta0=[0.1]*3, data_dir='_smt_cache/') interp.set_training_values(xt, yt) interp.train() return interp
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 train(self, X_train, y_train): if self.flavour == 'bspline': self.smt_model = RMTB(xlimits=self.xlimits, smoothness=self.smoothness, approx_order=self.approx_order, line_search=self.line_search, order=self.order, num_ctrl_pts=self.num_ctrl_pts) if self.flavour == 'cubic': self.smt_model = RMTC(xlimits=self.xlimits, smoothness=self.smoothness, approx_order=self.approx_order, line_search=self.line_search, order=self.order, num_elements=self.num_elements) super(RMTSModel, self).train(X_train, y_train)
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 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 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 rMTBSimba(xt, yt, xtest, ytest, funXLimits): t = RMTB(xlimits=funXLimits, min_energy=True, nonlinear_maxiter=20, print_prediction=False) t.set_training_values(xt, yt) # Add the gradient information # for i in range(ndim): # t.set_training_derivatives(xt,yt[:, 1+i].reshape((yt.shape[0],1)),i) t.train() # Prediction of the validation points print('RMTB, err: ' + str(compute_rms_error(t, xtest, ytest))) # plot prediction/true values title = 'RMTB' return t, title, xtest, ytest
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 train(self, train_method, **kwargs): """Trains the surrogate model with given training data. Parameters ---------- train_method : str Training method among ``IDW``, ``KPLS``, ``KPLSK``, ``KRG``, ``LS``, ``QP``, ``RBF``, ``RMTB``, ``RMTC`` kwargs : dict Additional keyword arguments supported by SMT objects """ if train_method == 'IDW': self.trained = IDW(**kwargs) elif train_method == 'KPLS': self.trained = KPLS(**kwargs) elif train_method == 'KPLSK': self.trained = KPLSK(**kwargs) elif train_method == 'KRG': self.trained = KRG(**kwargs) elif train_method == 'LS': self.trained = LS(**kwargs) elif train_method == 'QP': self.trained = QP(**kwargs) elif train_method == 'RBF': self.trained = RBF(**kwargs) elif train_method == 'RMTB': self.trained = RMTB(xlimits=self.limits, **kwargs) elif train_method == 'RMTC': self.trained = RMTC(xlimits=self.limits, **kwargs) else: raise ValueError( 'train_method must be one between IDW, KPLS, KPLSK, KRG, LS, QP, RBF, RMTB, RMTC' ) self.trained.set_training_values(self.x_samp, self.m_prop) self.trained.train()
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 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 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
from smt.surrogate_models import RMTB from smt.examples.one_D_step.one_D_step import get_one_d_step, plot_one_d_step xt, yt, xlimits = get_one_d_step() interp = RMTB(num_ctrl_pts=100, xlimits=xlimits, nonlinear_maxiter=20, solver_tolerance=1e-16, energy_weight=1e-14, regularization_weight=0.) interp.set_training_values(xt, yt) interp.train() plot_one_d_step(xt, yt, xlimits, interp)
def setUp(self): ndim = 3 nt = 100 ne = 100 ncomp = 1 problems = OrderedDict() 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=[1e-2] * ndim) sms["MFK"] = MFK(theta0=[1e-2] * ndim) sms["KPLS"] = KPLS(theta0=[1e-2] * ncomp, n_comp=ncomp) sms["KPLSK"] = KPLSK(theta0=[1] * ncomp, n_comp=ncomp) sms["GEKPLS"] = GEKPLS(theta0=[1e-2] * ncomp, n_comp=ncomp, delta_x=1e-1) sms["GENN"] = genn() if compiled_available: sms["IDW"] = IDW() sms["RBF"] = RBF() sms["RMTC"] = RMTC() sms["RMTB"] = RMTB() t_errors = {} t_errors["LS"] = 1.0 t_errors["QP"] = 1.0 t_errors["KRG"] = 1e0 t_errors["MFK"] = 1e0 t_errors["KPLS"] = 1e0 t_errors["KPLSK"] = 1e0 t_errors["GEKPLS"] = 1e0 t_errors["GENN"] = 1e0 if compiled_available: t_errors["IDW"] = 1e0 t_errors["RBF"] = 1e-2 t_errors["RMTC"] = 1e-1 t_errors["RMTB"] = 1e-1 e_errors = {} e_errors["LS"] = 1.5 e_errors["QP"] = 1.5 e_errors["KRG"] = 1e-2 e_errors["MFK"] = 1e-2 e_errors["KPLS"] = 1e-2 e_errors["KPLSK"] = 1e-2 e_errors["GEKPLS"] = 1e-2 e_errors["GENN"] = 1e-2 if compiled_available: e_errors["IDW"] = 1e0 e_errors["RBF"] = 1e0 e_errors["RMTC"] = 2e-1 e_errors["RMTB"] = 2e-1 self.nt = nt self.ne = ne self.ndim = ndim self.problems = problems self.sms = sms self.t_errors = t_errors self.e_errors = e_errors
tempdata[i, j, k].copy() ])) counter += 1 else: skipcount += 1 a = np.array(krigedata) xt = a[:, 0:3] yt = a[:, 3:] print('Data points: ' + str(counter) + ' Skipped: ' + str(skipcount)) # define a RMTS spline interpolant # TODO replace with a different SMT surrogate limits = np.array([[0.2, 0.8], [0.05, 1.0], [0.0, 3.5]]) sm = RMTB(order=3, xlimits=limits, nonlinear_maxiter=100) sm.set_training_values(xt, yt) sm.train() # plot a grid of values at a slice of throttle = 0.5 machs = np.linspace(0.2, 0.8, 25) alts = np.linspace(0.0, 35000., 25) machs, alts = np.meshgrid(machs, alts) pred = np.zeros((25, 25, 3)) pred2 = np.zeros((25, 25, 3)) # altitude is scaled by 1 / 10000 everywhere to make the indepdent variables of O(1) # this is necessary for certain methods like IDW # TODO examine other slices (for example holding Mach = 0.5 and varying other two parameters) for i in range(25):
# assemble the test and training set using holdout method test_set = arr training_set = np.zeros((0, 6)) for j in range(n_split): if j != i_array: training_set = np.vstack([training_set, arrays[j]]) xt = training_set[:, 0:3] yt = training_set[:, 5] # train the model # define a RMTS spline interpolant # TODO replace with a different SMT surrogate limits = np.array([[0.2, 0.8], [0.05, 1.0], [0.0, 3.5]]) sm = RMTB(print_global=False, order=3, xlimits=limits, nonlinear_maxiter=100) # sm1 = KRG(hyper_opt='TNC', corr='abs_exp') # sm2 = KPLS(n_comp=3, corr='abs_exp', hyper_opt='TNC') sm3 = KPLSK(print_global=False, n_comp=3, theta0=np.ones(3), corr='squar_exp') sm4 = QP(print_global=False, ) sm5 = LS(print_global=False, ) sm1 = KPLS(print_global=False, n_comp=3, theta0=np.ones(3), corr='abs_exp') sm2 = KRG(print_global=False, theta0=np.ones(3), corr='abs_exp') sm6 = MOE(smooth_recombination=False, n_clusters=2) experts_list = dict() experts_list['KRG'] = (KRG, {'theta0': np.ones(3), 'corr': 'abs_exp'}) experts_list['RBF'] = (RBF, dict())
# 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) ytest = fun(xtest) ########### The RMTB model t = RMTB( xlimits=fun.xlimits, min_energy=True, nonlinear_maxiter=20, print_prediction=False, ) t.set_training_values(xt, yt[:, 0]) # Add the gradient information for i in range(ndim): t.set_training_derivatives(xt, yt[:, 1 + i].reshape((yt.shape[0], 1)), i) t.train() # Prediction of the validation points y = t.predict_values(xtest) print("RMTB, err: " + str(compute_rms_error(t, xtest, ytest))) if plot_status: k, l = 0, 0 f, axarr = plt.subplots(3, 2)
def setup(self): nn = self.options['num_nodes'] raw1_file = self.options['raw1_file'] raw2_file = self.options['raw2_file'] raw1 = np.genfromtxt(raw1_file) raw2 = np.loadtxt(raw2_file) nc = self.nc = 7 self.np = 12 ncp = self.nc * self.np self.na = 10 self.nz = 73 self.ne = 37 angle = np.zeros(self.na) azimuth = np.zeros(self.nz) elevation = np.zeros(self.ne) index = 0 for i in range(self.na): angle[i] = raw1[index] index += 1 for i in range(self.nz): azimuth[i] = raw1[index] index += 1 index -= 1 azimuth[self.nz - 1] = 2.0 * np.pi for i in range(self.ne): elevation[i] = raw1[index] index += 1 angle[0] = 0.0 angle[-1] = np.pi / 2.0 azimuth[0] = 0.0 azimuth[-1] = 2 * np.pi elevation[0] = 0.0 elevation[-1] = np.pi counter = 0 data = np.zeros((self.na, self.nz, self.ne, self.np * self.nc)) flat_size = self.na * self.nz * self.ne for p in range(self.np): for c in range(nc): data[:, :, :, counter] = \ raw2[nc * p + c][119:119 + flat_size].reshape((self.na, self.nz, self.ne)) counter += 1 # self.MBI = MBI(data, [angle, azimuth, elevation], # [4, 10, 8], # [4, 4, 4]) angles, azimuths, elevations = np.meshgrid(angle, azimuth, elevation, indexing='ij') xt = np.array( [angles.flatten(), azimuths.flatten(), elevations.flatten()]).T yt = np.zeros((flat_size, ncp)) counter = 0 for p in range(self.np): for c in range(nc): yt[:, counter] = data[:, :, :, counter].flatten() counter += 1 xlimits = np.array([ [angle[0], angle[-1]], [azimuth[0], azimuth[-1]], [elevation[0], elevation[-1]], ]) this_dir = os.path.split(__file__)[0] # Create the _smt_cache directory if it doesn't exist if not os.path.exists(os.path.join(this_dir, '_smt_cache')): os.makedirs(os.path.join(this_dir, '_smt_cache')) self.interp = interp = RMTB( xlimits=xlimits, num_ctrl_pts=8, order=4, approx_order=4, nonlinear_maxiter=2, solver_tolerance=1.e-20, energy_weight=1.e-4, regularization_weight=1.e-14, # smoothness=np.array([1., 1., 1.]), extrapolate=False, print_global=True, data_dir=os.path.join(this_dir, '_smt_cache'), ) interp.set_training_values(xt, yt) interp.train() if multiview_installed: info = { 'nx': 3, 'ny': ncp, 'user_func': interp.predict_values, 'resolution': 100, 'plot_size': 8, 'dimension_names': ['Angle', 'Azimuth', 'Elevation'], 'bounds': xlimits.tolist(), 'X_dimension': 0, 'Y_dimension': 1, 'scatter_points': [xt, yt], 'dist_range': 0.0, } # Initialize display parameters and draw GUI MultiView(info) self.x = np.zeros((nn, 3)) # Inputs self.add_input('fin_angle', 0.0, units='rad', desc='Fin angle of solar panel') self.add_input( 'azimuth', np.zeros((nn, )), units='rad', desc='Azimuth angle of the sun in the body-fixed frame over time') self.add_input( 'elevation', np.zeros((nn, )), units='rad', desc='Elevation angle of the sun in the body-fixed frame over time' ) # Outputs self.add_output( 'exposed_area', np.zeros((nn, self.nc, self.np)), desc='Exposed area to sun for each solar cell over time', units='m**2', lower=-5e-3, upper=1.834e-1) self.declare_partials('exposed_area', 'fin_angle') rows = np.tile(np.arange(ncp), nn) + np.repeat(ncp * np.arange(nn), ncp) cols = np.tile(np.repeat(0, ncp), nn) + np.repeat(np.arange(nn), ncp) self.declare_partials('exposed_area', 'azimuth', rows=rows, cols=cols) self.declare_partials('exposed_area', 'elevation', rows=rows, cols=cols)
from smt.surrogate_models import RMTB from smt.examples.rans_crm_wing.rans_crm_wing import get_rans_crm_wing, plot_rans_crm_wing xt, yt, xlimits = get_rans_crm_wing() interp = RMTB(num_ctrl_pts=20, xlimits=xlimits, nonlinear_maxiter=100, energy_weight=1e-12) interp.set_training_values(xt, yt) interp.train() plot_rans_crm_wing(xt, yt, xlimits, interp)