def test_cache(self): x = np.array([[-2., 0.], [-0.5, 1.5], [1., 3.], [8.5, 4.5], [-3.5, 6.], [4., 7.5], [-5., 9.], [5.5, 10.5], [10., 12.], [7., 13.5], [2.5, 15.]]) y = np.array([[branin(case)] for case in x]) surrogate_before = KrigingSurrogate(nugget=0., eval_rmse=True, training_cache='test_cache.npz') surrogate_before.train(x, y) surrogate = KrigingSurrogate(nugget=0., eval_rmse=True, training_cache='test_cache.npz') surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_near_equal(mu, [y0], 1e-9) assert_near_equal(sigma, [[0]], 1e-4) mu, sigma = surrogate.predict([5., 5.]) assert_near_equal(mu, [[16.72]], 1e-1) assert_near_equal(sigma, [[15.27]], 1e-2) os.unlink('test_cache.npz')
def test_one_pt(self): surrogate = KrigingSurrogate() x = [[0.]] y = [[1.]] with self.assertRaises(ValueError) as cm: surrogate.train(x, y) self.assertEqual(str(cm.exception), 'KrigingSurrogate requires at least' ' 2 training points.')
def test_one_pt(self): surrogate = KrigingSurrogate() x = [[0.]] y = [[1.]] with self.assertRaises(ValueError) as cm: surrogate.train(x, y) self.assertEqual(str(cm.exception), 'KrigingSurrogate require at least' ' 2 training points.')
def test_scalar_derivs(self): surrogate = KrigingSurrogate() x = array([[0.], [1.], [2.], [3.]]) y = x.copy() surrogate.train(x, y) jac = surrogate.linearize(array([[0.]])) assert_rel_error(self, jac[0][0], 1., 1e-3)
def test_scalar_derivs(self): surrogate = KrigingSurrogate(nugget=0.) x = np.array([[0.], [1.], [2.], [3.]]) y = x.copy() surrogate.train(x, y) jac = surrogate.linearize(np.array([[0.]])) assert_rel_error(self, jac[0][0], 1., 1e-3)
def test_1d_ill_conditioned(self): # Test for least squares solver utilization when ill-conditioned x = np.array([[case] for case in np.linspace(0., 1., 40)]) y = np.sin(x) surrogate = KrigingSurrogate() surrogate.train(x, y) new_x = np.array([0.5]) mu, sigma = surrogate.predict(new_x) self.assertTrue(sigma < 3.e-8) assert_rel_error(self, mu, np.sin(0.5), 1e-6)
def test_vector_derivs(self): surrogate = KrigingSurrogate() n = 15 x = np.array([[a, b] for a, b in itertools.product(np.linspace(0, 1, n), repeat=2)]) y = np.array([[a+b, a-b, a+2*b] for a, b in x]) surrogate.train(x, y) jac = surrogate.linearize(np.array([[0.5, 0.5]])) assert_near_equal(jac, np.array([[1, 1], [1, -1], [1, 2]]), 5e-4)
def test_1d_ill_conditioned(self): # Test for least squares solver utilization when ill-conditioned x = np.array([[case] for case in np.linspace(0., 1., 40)]) y = np.sin(x) surrogate = KrigingSurrogate(eval_rmse=True) surrogate.train(x, y) new_x = np.array([0.5]) mu, sigma = surrogate.predict(new_x) self.assertTrue(sigma < 1.e-5) assert_near_equal(mu, [[np.sin(0.5)]], 1e-5)
def test_vector_derivs(self): surrogate = KrigingSurrogate() n = 15 x = np.array([[a, b] for a, b in itertools.product(np.linspace(0, 1, n), repeat=2)]) y = np.array([[a+b, a-b, a+2*b] for a, b in x]) surrogate.train(x, y) jac = surrogate.linearize(np.array([[0.5, 0.5]])) assert_rel_error(self, jac, np.array([[1, 1], [1, -1], [1, 2]]), 5e-4)
def test_vector_derivs(self): surrogate = KrigingSurrogate() x = array([[a, b] for a, b in itertools.product(linspace(0, 1, 10), repeat=2)]) y = array([[a+b, a-b] for a, b in x]) surrogate.train(x, y) jac = surrogate.linearize(array([[0.5, 0.5]])) assert_rel_error(self, jac, array([[1, 1], [1, -1]]), 1e-5)
def test_1d_training(self): x = np.array([[0.0], [2.0], [3.0], [4.0], [6.0]]) y = np.array([[branin_1d(case)] for case in x]) surrogate = KrigingSurrogate(nugget=0., eval_rmse=True) surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, [y0], 1e-9) assert_rel_error(self, sigma, [[0]], 1e-5)
def test_1d_training(self): x = array([[0.0], [2.0], [3.0], [4.0], [6.0]]) y = array([[branin_1d(case)] for case in x]) surrogate = KrigingSurrogate() surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) assert_rel_error(self, sigma, 0, 1e-6)
def test_no_training_data(self): surrogate = KrigingSurrogate() try: surrogate.predict([0., 1.]) except RuntimeError as err: self.assertEqual( str(err), "KrigingSurrogate has not been trained, " "so no prediction can be made.") else: self.fail("RuntimeError Expected")
def test_1d_training(self): x = np.array([[0.0], [2.0], [3.0], [4.0], [6.0]]) y = np.array([[branin_1d(case)] for case in x]) surrogate = KrigingSurrogate(nugget=0.) surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) assert_rel_error(self, sigma, 0, 1e-6)
def test_1d_training(self): x = np.array([[0.0], [2.0], [3.0], [4.0], [6.0]]) y = np.array([[branin_1d(case)] for case in x]) surrogate = KrigingSurrogate(nugget=0., eval_rmse=True) surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_near_equal(mu, [y0], 1e-9) assert_near_equal(sigma, [[0]], 1e-5)
def test_no_training_data(self): surrogate = KrigingSurrogate() try: surrogate.predict([0., 1.]) except RuntimeError as err: self.assertEqual(str(err), "KrigingSurrogate has not been trained, " "so no prediction can be made.") else: self.fail("RuntimeError Expected")
def test_1d_predictor(self): x = array([[0.0], [2.0], [3.0], [4.0], [6.0]]) y = array([[branin_1d(case)] for case in x]) surrogate = KrigingSurrogate() surrogate.train(x, y) new_x = array([pi]) mu, sigma = surrogate.predict(new_x) assert_rel_error(self, mu, 0.397887, 1e-1) assert_rel_error(self, sigma, 0.0294172, 1e-2)
def test_vector_input(self): surrogate = KrigingSurrogate(nugget=0.) x = np.array([[0., 0., 0.], [1., 1., 1.]]) y = np.array([[0.], [3.]]) surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) assert_rel_error(self, sigma, 0, 1e-6)
def test_1d_predictor(self): x = np.array([[0.0], [2.0], [3.0], [4.0], [6.0]]) y = np.array([[branin_1d(case)] for case in x]) surrogate = KrigingSurrogate(eval_rmse=True) surrogate.train(x, y) new_x = np.array([3.5]) mu, sigma = surrogate.predict(new_x) assert_near_equal(mu, [[branin_1d(new_x)]], 1e-1) assert_near_equal(sigma, [[0.07101449]], 1e-2)
def test_1d_predictor(self): x = np.array([[0.0], [2.0], [3.0], [4.0], [6.0]]) y = np.array([[branin_1d(case)] for case in x]) surrogate = KrigingSurrogate() surrogate.train(x, y) new_x = np.array([3.5]) mu, sigma = surrogate.predict(new_x) assert_rel_error(self, mu, branin_1d(new_x), 1e-1) assert_rel_error(self, sigma, 0.07101449, 1e-2)
def test_vector_output(self): surrogate = KrigingSurrogate(nugget=0., eval_rmse=True) y = np.array([[0., 0.], [1., 1.], [2., 0.]]) x = np.array([[0.], [2.], [4.]]) surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_near_equal(mu, [y0], 1e-9) assert_near_equal(sigma, [[0, 0]], 1e-6)
def test_vector_input(self): surrogate = KrigingSurrogate() x = array([[0., 0., 0.], [1., 1., 1.]]) y = array([[0.], [3.]]) surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) assert_rel_error(self, sigma, 0, 1e-6)
def test_vector_input(self): surrogate = KrigingSurrogate(nugget=0., eval_rmse=True) x = np.array([[0., 0., 0.], [1., 1., 1.]]) y = np.array([[0.], [3.]]) surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, [y0], 1e-9) assert_rel_error(self, sigma, [[0]], 1e-6)
def test_vector_output(self): surrogate = KrigingSurrogate() y = array([[0., 0.], [1., 1.], [2., 0.]]) x = array([[0.], [2.], [4.]]) surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) assert_rel_error(self, sigma, 0, 1e-6)
def test_vector_output(self): surrogate = KrigingSurrogate(nugget=0., eval_rmse=True) y = np.array([[0., 0.], [1., 1.], [2., 0.]]) x = np.array([[0.], [2.], [4.]]) surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, [y0], 1e-9) assert_rel_error(self, sigma, [[0, 0]], 1e-6)
def test_vector_output(self): surrogate = KrigingSurrogate(nugget=0.) y = np.array([[0., 0.], [1., 1.], [2., 0.]]) x = np.array([[0.], [2.], [4.]]) surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) assert_rel_error(self, sigma, 0, 1e-6)
def test_vectorized_kriging(self): # Test for coverage (handling the rmse) size = 3 # create a vectorized MetaModelUnStructuredComp for sine trig = MetaModelUnStructuredComp( vec_size=size, default_surrogate=KrigingSurrogate(eval_rmse=True)) trig.add_input('x', np.zeros(size)) trig.add_output('y', np.zeros(size)) # add it to a Problem prob = Problem() prob.model.add_subsystem('trig', trig) prob.setup(check=False) # provide training data trig.options['train:x'] = np.linspace(0, 10, 20) trig.options['train:y'] = .5 * np.sin(trig.options['train:x']) # train the surrogate and check predicted value prob['trig.x'] = np.array([2.1, 3.2, 4.3]) prob.run_model() assert_rel_error(self, prob['trig.y'], np.array(.5 * np.sin(prob['trig.x'])), 1e-4) self.assertEqual(len(prob.model.trig._metadata('y')['rmse']), 3)
def test_sin_metamodel_rmse(self): # create MetaModelUnStructuredComp with Kriging, using the rmse option sin_mm = MetaModelUnStructuredComp() sin_mm.add_input('x', 0.) sin_mm.add_output('f_x', 0.) sin_mm.options['default_surrogate'] = KrigingSurrogate(eval_rmse=True) # add it to a Problem prob = Problem() prob.model.add_subsystem('sin_mm', sin_mm) prob.setup(check=False) # train the surrogate and check predicted value sin_mm.options['train:x'] = np.linspace(0, 10, 20) sin_mm.options['train:f_x'] = np.sin(sin_mm.options['train:x']) prob['sin_mm.x'] = 2.1 prob.run_model() assert_rel_error(self, prob['sin_mm.f_x'], np.sin(2.1), 1e-4) # mean self.assertTrue( self, sin_mm._metadata('f_x')['rmse'] < 1e-5) # std deviation
def test_2d(self): x = np.array([[-2., 0.], [-0.5, 1.5], [1., 3.], [8.5, 4.5], [-3.5, 6.], [4., 7.5], [-5., 9.], [5.5, 10.5], [10., 12.], [7., 13.5], [2.5, 15.]]) y = np.array([[branin(case)] for case in x]) surrogate = KrigingSurrogate(nugget=0., eval_rmse=True) surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) assert_rel_error(self, sigma, 0, 1e-4) mu, sigma = surrogate.predict([5., 5.]) assert_rel_error(self, mu, 16.72, 1e-1) assert_rel_error(self, sigma, 15.27, 1e-2)
def __init__(self): super(TrigMM, self).__init__() # Create meta_model for f_x as the response sin_mm = self.add("sin_mm", MetaModel()) sin_mm.add_param('x', val=0.) sin_mm.add_output('f_x:float', val=0., surrogate=FloatKrigingSurrogate()) sin_mm.add_output('f_x:norm_dist', val=(0., 0.), surrogate=KrigingSurrogate())
def test_sin_metamodel_obj_return(self): # create a MetaModel for Sin and add it to a Problem sin_mm = MetaModel() sin_mm.add_param('x', 0.) sin_mm.add_output('f_x', (0., 0.)) prob = Problem(Group()) prob.root.add('sin_mm', sin_mm) # check that missing surrogate is detected in check_setup stream = cStringIO() prob.setup(out_stream=stream) msg = ("No default surrogate model is defined and the " "following outputs do not have a surrogate model:\n" "['f_x']\n" "Either specify a default_surrogate, or specify a " "surrogate model for all outputs.") self.assertTrue(msg in stream.getvalue()) # check that output with no specified surrogate gets the default sin_mm.default_surrogate = KrigingSurrogate() prob.setup(check=False) surrogate = prob.root.unknowns.metadata('sin_mm.f_x').get('surrogate') self.assertTrue(isinstance(surrogate, KrigingSurrogate), 'sin_mm.f_x should get the default surrogate') # train the surrogate and check predicted value prob['sin_mm.train:x'] = np.linspace(0, 10, 20) prob['sin_mm.train:f_x'] = np.sin(prob['sin_mm.train:x']) prob['sin_mm.x'] = 2.1 prob.run() assert_rel_error(self, prob['sin_mm.f_x'][0], np.sin(2.1), 1e-4) # mean self.assertTrue(self, prob['sin_mm.f_x'][1] < 1e-5) #std deviation
def test_2d(self): x = array([[-2., 0.], [-0.5, 1.5], [1., 3.], [8.5, 4.5], [-3.5, 6.], [4., 7.5], [-5., 9.], [5.5, 10.5], [10., 12.], [7., 13.5], [2.5, 15.]]) y = array([[branin(case)] for case in x]) surrogate = KrigingSurrogate() surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) assert_rel_error(self, sigma, 0, 1e-6) mu, sigma = surrogate.predict([5., 5.]) assert_rel_error(self, mu, branin([5., 5.]), 1e-1) assert_rel_error(self, sigma, 5.79, 1e-2)
def test_2d(self): x = np.array([[-2., 0.], [-0.5, 1.5], [1., 3.], [8.5, 4.5], [-3.5, 6.], [4., 7.5], [-5., 9.], [5.5, 10.5], [10., 12.], [7., 13.5], [2.5, 15.]]) y = np.array([[branin(case)] for case in x]) surrogate = KrigingSurrogate(nugget=0., eval_rmse=True) surrogate.train(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, [y0], 1e-9) assert_rel_error(self, sigma, [[0]], 1e-4) mu, sigma = surrogate.predict([5., 5.]) assert_rel_error(self, mu, [[16.72]], 1e-1) assert_rel_error(self, sigma, [[15.27]], 1e-2)
ModelInfo_g[mm].X_org = np.append(ModelInfo_g[mm].X_org, x0I[nonNAN]).reshape( num_pt, num_xI) ModelInfo_g[mm].y = np.append(ModelInfo_g[mm].y, g[nonNAN, mm]).reshape(num_pt, 1) if eflag[nonNAN] >= 1: fea_pt += 1 FEA_obj = np.append(FEA_obj, obj[nonNAN]).reshape(fea_pt, 1) FEA_xopt = np.append( FEA_xopt, np.concatenate( (x0I[nonNAN, :], xC_opt[nonNAN, :]))).reshape(fea_pt, num_xI + num_xC) # Call the surrogate building function surrogate = KrigingSurrogate() #Use ModelInfo_obj in the future release # surrogate.train(ModelInfo_obj.X_org, ModelInfo_obj.y) surrogate.train(ModelInfo_obj.X_hat, ModelInfo_obj.y, False) ModelInfo_obj.X = surrogate.X # ModelInfo_obj.ynorm = surrogate.Y ModelInfo_obj.thetas = surrogate.thetas ModelInfo_obj.mu = surrogate.mu #np.mean(surrogate.Y) #This value should always be 0.0 ModelInfo_obj.SigmaSqr = surrogate.SigmaSqr #surrogate.sigma2/np.square(surrogate.Y_std) #This value should always be 1.0 ModelInfo_obj.c_r = surrogate.c_r #surrogate.alpha ModelInfo_obj.R_inv = surrogate.R_inv #surrogate.Vh.T.dot(np.einsum('i,ij->ij', surrogate.S_inv, surrogate.U.T)) surrogate.predict(ModelInfo_obj.X_hat[ii], False) exit() # ModelInfo_obj.Y_mean = surrogate.Y_mean # ModelInfo_obj.Y_std = surrogate.Y_std # ModelInfo_obj.X_std = surrogate.X_std.reshape(num_xI,1) # ModelInfo_obj.X_mean = surrogate.X_mean.reshape(num_xI,1)
ModelInfo_obj.y = np.append(ModelInfo_obj.y, obj[nonNAN]).reshape(num_pt, 1) ModelInfo_obj.eflag = np.append(ModelInfo_obj.eflag, eflag[nonNAN]).reshape(num_pt, 1) # Surrogate data for the constraint functions # Goes here .... if eflag[nonNAN] >= 1: fea_pt += 1 FEA_obj = np.append(FEA_obj, obj[nonNAN]).reshape(fea_pt, 1) FEA_xopt = np.append(FEA_xopt, [x0I[nonNAN], xC_opt[nonNAN]]).reshape( fea_pt, num_xI + num_xC) # Call the surrogate building function surrogate = KrigingSurrogate() #Use ModelInfo_obj in the future release surrogate.train(ModelInfo_obj.X_org, ModelInfo_obj.y) ModelInfo_obj.X = surrogate.X ModelInfo_obj.ynorm = surrogate.Y ModelInfo_obj.thetas = surrogate.thetas ModelInfo_obj.mu = np.mean(surrogate.Y) #This value should always be 0.0 ModelInfo_obj.SigmaSqr = surrogate.sigma2 / np.square( surrogate.Y_std) #This value should always be 1.0 ModelInfo_obj.c_r = surrogate.alpha ModelInfo_obj.R_inv = surrogate.Vh.T.dot( np.einsum('i,ij->ij', surrogate.S_inv, surrogate.U.T)) ModelInfo_obj.Y_mean = surrogate.Y_mean ModelInfo_obj.Y_std = surrogate.Y_std ModelInfo_obj.X_std = surrogate.X_std.reshape(num_xI, 1) ModelInfo_obj.X_mean = surrogate.X_mean.reshape(num_xI, 1) print "\nSurrogate building of the objective is complete..."