def test_rosenbrock(self): import numpy as np import matplotlib.pyplot as plt from smt.problems import Rosenbrock ndim = 2 problem = Rosenbrock(ndim=ndim) num = 100 x = np.ones((num, ndim)) x[:, 0] = np.linspace(-2, 2., 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 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_rosenbrock_2D(self): n_iter = 30 fun = Rosenbrock(ndim=2) xlimits = fun.xlimits criterion = "UCB" #'EI' or 'SBO' or 'UCB' xdoe = FullFactorial(xlimits=xlimits)(10) ego = EGO(xdoe=xdoe, n_iter=n_iter, criterion=criterion, xlimits=xlimits) x_opt, y_opt, _, _, _, _, _ = ego.optimize(fun=fun) self.assertTrue(np.allclose([[1, 1]], x_opt, rtol=0.5)) self.assertAlmostEqual(0.0, float(y_opt), delta=1)
def test_rosenbrock_2D(self): n_iter = 40 fun = Rosenbrock(ndim=2) xlimits = fun.xlimits criterion = "UCB" #'EI' or 'SBO' or 'UCB' xdoe = FullFactorial(xlimits=xlimits)(10) ego = EGO( xdoe=xdoe, n_iter=n_iter, criterion=criterion, xlimits=xlimits, random_state=0, # change seed as it fails on travis macos py3.7 ) x_opt, y_opt, _, _, _ = ego.optimize(fun=fun) self.assertTrue(np.allclose([[1, 1]], x_opt, rtol=0.5)) self.assertAlmostEqual(0.0, float(y_opt), delta=1)
def test_rosenbrock_2D_parallel(self): n_iter = 15 n_parallel = 5 fun = Rosenbrock(ndim=2) xlimits = fun.xlimits criterion = "UCB" #'EI' or 'SBO' or 'UCB' xdoe = FullFactorial(xlimits=xlimits)(10) qEI = "KB" ego = EGO( xdoe=xdoe, n_iter=n_iter, criterion=criterion, xlimits=xlimits, n_parallel=n_parallel, qEI=qEI, evaluator=ParallelEvaluator(), ) x_opt, y_opt, _, _, _ = ego.optimize(fun=fun) print("Rosenbrock: ", x_opt) self.assertTrue(np.allclose([[1, 1]], x_opt, rtol=0.5)) self.assertAlmostEqual(0.0, float(y_opt), delta=1)
from __future__ import print_function, division import numpy as np import pylab from smt.problems import CantileverBeam, Carre, ReducedProblem, RobotArm, Rosenbrock from smt.problems import TensorProduct, TorsionVibration, WaterFlow, WeldedBeam, WingWeight from smt.sampling import LHS, Random, FullFactorial, Clustered from smt import LS, PA2, KPLS, IDW, RBF, RMTS, RMTB # prob = Carre(ndim=3) # prob = TensorProduct(ndim=3, func='cos', width=1.) # prob = WeldedBeam(ndim=3) # prob = CantileverBeam(ndim=3) prob = Rosenbrock(ndim=4) # prob = RobotArm(ndim=4) sampling = LHS(xlimits=prob.xlimits) sampling = FullFactorial(xlimits=prob.xlimits, clip=True) # sampling = Random(xlimits=prob.xlimits) # sampling = Clustered(kernel=sampling) ndim = prob.options['ndim'] sm = RMTS(xlimits=prob.xlimits, num_elements=6, max_print_depth=5) sm = RMTB(xlimits=prob.xlimits, order=3, num_ctrl_pts=10, max_print_depth=5, min_energy=True) # sm = IDW() # sm = KPLS(name='KRG', n_comp=ndim, theta0=[1.0]*ndim)
def test_rosenbrock(self): self.run_test(Rosenbrock(ndim=2)) self.run_test(Rosenbrock(ndim=3))
plot_status = True except: plot_status = False import scipy.interpolate from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from matplotlib import cm ########### Initialization of the problem, construction of the training and validation points ndim = 2 ndoe = 20 #int(10*ndim) # Define the function fun = Rosenbrock(ndim=ndim) # Construction of the DOE sampling = LHS(xlimits=fun.xlimits, criterion='m') xt = sampling(ndoe) # Compute the outputs yt = fun(xt) # Construction of the validation points ntest = 200 #500 sampling = LHS(xlimits=fun.xlimits) xtest = sampling(ntest) ytest = fun(xtest) # To plot the Rosenbrock function x = np.linspace(-2, 2, 50)