def run_mixed_integer_qp_example(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import QP from smt.applications.mixed_integer import MixedIntegerSurrogateModel, INT 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]) # xtypes = [FLOAT, INT, (ENUM, 3), (ENUM, 2)] # FLOAT means x1 continuous # INT means x2 integer # (ENUM, 3) means x3, x4 & x5 are 3 levels of the same categorical variable # (ENUM, 2) means x6 & x7 are 2 levels of the same categorical variable sm = MixedIntegerSurrogateModel(xtypes=[INT], xlimits=[[0, 4]], surrogate=QP()) 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 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_qp(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import QP xt = np.array([[0.0, 1.0, 2.0, 3.0, 4.0]]).T yt = np.array([[0.2, 1.4, 1.5, 0.5, 1.0], [0.0, 1.0, 2.0, 4, 3]]).T sm = QP() sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) t1, _ = plt.plot(xt, yt[:, 0], "o", "C0") p1 = plt.plot(x, y[:, 0], "C0", label="Prediction 1") t2, _ = plt.plot(xt, yt[:, 1], "o", "C1") p2 = plt.plot(x, y[:, 1], "C1", label="Prediction 2") plt.xlabel("x") plt.ylabel("y") plt.legend() plt.show()
def test_ny(self): xt, yt, _ = get_rans_crm_wing() interp = QP() interp.set_training_values(xt, yt) interp.train() v0 = np.zeros((4, 2)) for ix, i in enumerate([10, 11, 12, 13]): v0[ix, :] = interp.predict_values(np.atleast_2d(xt[i, :])) v1 = interp.predict_values(np.atleast_2d(xt[10:14, :])) expected_diff = np.zeros((4, 2)) np.testing.assert_allclose(v1 - v0, expected_diff, atol=1e-15)
class SS_model_QP(SS_model_base): def __init__(self, data, x_headers, y_header): super().__init__(data, x_headers, y_header) self.model = QP(print_training=False, print_global=False) def get_hyperparameters(self): return "quadratic model" def fit(self): self.model.set_training_values(self.X_train, self.y_train) self.model.train() def model_predict(self): X = np.array(self.X_pred) self.y_pred = self.model.predict_values(X) self.y_std = None
def quadraticModel(xt, yt, xtest, ytest): ########### The QP model t = QP(print_prediction=False) t.set_training_values(xt, yt) t.train() title = 'QP model: validation of the prediction model' print('QP, err: ' + str(compute_rms_error(t, xtest, ytest))) return t, title, xtest, ytest
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_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 __init__(self, systemsize, input_dict, data_type=np.float): QuantityOfInterest.__init__(self, systemsize, data_type=data_type) # Load the eigenmodes fname = input_dict['surrogate info full path'] surrogate_info = np.load(fname) surrogate_samples = surrogate_info['input_samples'] fval_arr = surrogate_info['fvals'] # Create the surrogate self.surrogate_type = input_dict['surrogate_type'] if self.surrogate_type == 'quadratic': self.surrogate = QP() elif self.surrogate_type == 'kriging': theta0 = input_dict['kriging_theta'] self.surrogate = KRG(theta0=[theta0], corr=input_dict['correlation function']) else: raise NotImplementedError self.surrogate.set_training_values(surrogate_samples.T, fval_arr) self.surrogate.train()
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 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 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_qp(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import QP 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]) sm = QP() 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_qp(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import QP xt = np.array([0., 1., 2., 3., 4.]) yt = np.array([0., 1., 1.5, 0.5, 1.0]) sm = QP() sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0., 4., 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 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
# 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()) experts_list['KPLS'] = (KPLS, { 'n_comp': 3, 'theta0': np.ones(3), 'corr': 'abs_exp' }) experts_list['KPLSK'] = (KPLSK, { 'n_comp': 3, 'theta0': np.ones(3),
def test_smt_qp(self): self._check_smt(QP())
def __init__(self, data, x_headers, y_header): super().__init__(data, x_headers, y_header) self.model = QP(print_training=False, print_global=False)
if plot_status: axarr[k, l].plot(ydtest[:, i], ydtest[:, i], "-.") axarr[k, l].plot(ydtest[:, i], yd_prediction[:, i], ".") if l == 2: l = 0 k += 1 else: l += 1 if plot_status: plt.show() ########### The QP model t = QP(print_prediction=False) t.set_training_values(xt, yt[:, 0]) t.train() # Prediction of the validation points y = t.predict_values(xtest) print("QP, err: " + str(compute_rms_error(t, xtest, ytest))) if plot_status: k, l = 0, 0 f, axarr = plt.subplots(4, 3) axarr[k, l].plot(ytest, ytest, "-.") axarr[k, l].plot(ytest, y, ".") l += 1 axarr[3, 2].arrow(0.3, 0.3, 0.2, 0) axarr[3, 2].arrow(0.3, 0.3, 0.0, 0.4)