def test_1d_50_var(self): self.ndim = 1 self.nt = 50 self.ne = 50 np.random.seed(0) xt = np.random.sample(self.nt).reshape((-1, 1)) yt = self.function_test_1d(xt) moe = MOE( smooth_recombination=True, heaviside_optimization=True, n_clusters=3, xt=xt, yt=yt, variances_support=True, ) moe.train() # validation data np.random.seed(1) xe = np.random.sample(self.ne) ye = self.function_test_1d(xe) rms_error = compute_rms_error(moe, xe, ye) self.assert_error(rms_error, 0.0, 3e-1) moe.predict_variances(xe) moe_hard = MOE( smooth_recombination=False, heaviside_optimization=True, n_clusters=3, xt=xt, yt=yt, variances_support=True, ) moe_hard.train() moe_hard.predict_variances(xe) if TestMOE.plot: import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D y = moe.predict_values(xe) plt.figure(1) plt.plot(ye, ye, "-.") plt.plot(ye, y, ".") plt.xlabel(r"$y$ actual") plt.ylabel(r"$y$ prediction") plt.figure(2) xv = np.linspace(0, 1, 100) yv = self.function_test_1d(xv) y = moe.predict_values(xv) y_std = np.sqrt(moe.predict_variances(xv)) plt.plot(xv, yv, "--k", linewidth=1) plt.plot(xv, y, "-b", linewidth=1) plt.plot(xv, y+y_std, "--b", linewidth=1) plt.plot(xv, y-y_std, "--b", linewidth=1) plt.show()
def test_1d_50(self): self.ndim = 1 self.nt = 50 self.ne = 50 np.random.seed(0) xt = np.random.sample(self.nt).reshape((-1, 1)) yt = self.function_test_1d(xt) moe = MOE(smooth_recombination=True, heaviside_optimization=True, n_clusters=3, xt=xt, yt=yt) moe.train() # validation data np.random.seed(1) xe = np.random.sample(self.ne) ye = self.function_test_1d(xe) rms_error = compute_rms_error(moe, xe, ye) self.assert_error(rms_error, 0., 3e-1) if TestMOE.plot: y = moe.predict_values(xe) plt.figure(1) plt.plot(ye, ye, '-.') plt.plot(ye, y, '.') plt.xlabel(r'$y$ actual') plt.ylabel(r'$y$ prediction') plt.figure(2) xv = np.linspace(0, 1, 100) yv = self.function_test_1d(xv) plt.plot(xv, yv, '-.') plt.plot(xe, y, 'o') plt.show()
def test_1d_50_surrogate_model(self): self.ndim = 1 self.nt = 50 self.ne = 50 np.random.seed(0) xt = np.random.sample(self.nt).reshape((-1, 1)) yt = self.function_test_1d(xt) moe = MOESurrogateModel( smooth_recombination=True, heaviside_optimization=True, n_clusters=3, xt=xt, yt=yt, ) self.assertIsInstance(moe.moe, MOE) moe.train() self.assertFalse(moe.supports["variances"]) # validation data np.random.seed(1) xe = np.random.sample(self.ne) ye = self.function_test_1d(xe) rms_error = compute_rms_error(moe, xe, ye) self.assert_error(rms_error, 0.0, 3e-1) self.assertRaises(RuntimeError, lambda: moe.predict_variances(xe)) moe_var = MOESurrogateModel( smooth_recombination=True, heaviside_optimization=True, n_clusters=3, xt=xt, yt=yt, variances_support=True, ) moe_var.train() self.assertTrue(moe_var.supports["variances"]) moe_var.predict_variances(xe) if TestMOE.plot: import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D y = moe.predict_values(xe) plt.figure(1) plt.plot(ye, ye, "-.") plt.plot(ye, y, ".") plt.xlabel(r"$y$ actual") plt.ylabel(r"$y$ prediction") plt.figure(2) xv = np.linspace(0, 1, 100) yv = self.function_test_1d(xv) plt.plot(xv, yv, "-.") plt.plot(xe, y, "o") plt.show()
def test_branin_2d_200(self): self.ndim = 2 self.nt = 200 self.ne = 200 prob = Branin(ndim=self.ndim) # training data sampling = FullFactorial(xlimits=prob.xlimits, clip=True) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) # mixture of experts moe = MOE(n_clusters=5) moe.set_training_values(xt, yt) moe.options["heaviside_optimization"] = True moe.train() # validation data np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) rms_error = compute_rms_error(moe, xe, ye) self.assert_error(rms_error, 0.0, 1e-1) if TestMOE.plot: import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D y = moe.analyse_results(x=xe, operation="predict_values") plt.figure(1) plt.plot(ye, ye, "-.") plt.plot(ye, y, ".") plt.xlabel(r"$y$ actual") plt.ylabel(r"$y$ prediction") fig = plt.figure(2) ax = fig.add_subplot(111, projection="3d") ax.scatter(xt[:, 0], xt[:, 1], yt) plt.title("Branin function") plt.show()
def test_norm1_2d_200(self): self.ndim = 2 self.nt = 200 self.ne = 200 prob = LpNorm(ndim=self.ndim) # training data sampling = FullFactorial(xlimits=prob.xlimits, clip=True) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) # mixture of experts moe = MOE(smooth_recombination=False, n_clusters=5) moe.set_training_values(xt, yt) moe.train() # validation data np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) rms_error = compute_rms_error(moe, xe, ye) self.assert_error(rms_error, 0., 1e-1) if TestMOE.plot: import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D y = moe.predict_values(xe) plt.figure(1) plt.plot(ye, ye, '-.') plt.plot(ye, y, '.') plt.xlabel(r'$y$ actual') plt.ylabel(r'$y$ prediction') fig = plt.figure(2) ax = fig.add_subplot(111, projection='3d') ax.scatter(xt[:, 0], xt[:, 1], yt) plt.title('L1 Norm') plt.show()
def test_branin_2d_200(self): self.ndim = 2 self.nt = 200 self.ne = 200 prob = Branin(ndim=self.ndim) # training data sampling = FullFactorial(xlimits=prob.xlimits, clip=True) np.random.seed(0) xt = sampling(self.nt) yt = prob(xt) # mixture of experts moe = MOE(n_clusters=5) moe.set_training_values(xt, yt) moe.options['heaviside_optimization'] = True moe.train() # validation data np.random.seed(1) xe = sampling(self.ne) ye = prob(xe) rms_error = compute_rms_error(moe, xe, ye) self.assert_error(rms_error, 0., 1e-1) if TestMOE.plot: y = moe.analyse_results(x=xe, operation='predict_values') plt.figure(1) plt.plot(ye, ye, '-.') plt.plot(ye, y, '.') plt.xlabel(r'$y$ actual') plt.ylabel(r'$y$ prediction') fig = plt.figure(2) ax = fig.add_subplot(111, projection='3d') ax.scatter(xt[:, 0], xt[:, 1], yt) plt.title('Branin function') plt.show()