Beispiel #1
0
    def test_1d_kriging1(self):

        x = array([[0.05], [.25], [0.61], [0.95]])
        y = array([0.738513784857542, -0.210367746201974, -0.489015457891476, 12.3033138316612])
        krig1 = KrigingSurrogate()
        krig1.train(x, y)

        self.assertAlmostEqual(.4771, krig1.thetas[0], places=4)
Beispiel #2
0
    def test_no_training_data(self):
        krig1 = KrigingSurrogate()

        try:
            krig1.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")
Beispiel #3
0
    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.jacobian(array([[0.5, 0.5]]))
        assert_rel_error(self, jac, array([[1, 1], [1, -1]]), 1e-5)
Beispiel #4
0
    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.')
Beispiel #5
0
    def test_scalar_derivs(self):
        surrogate = KrigingSurrogate()

        x = array([[0.], [1.], [2.], [3.]])
        y = x.copy()

        surrogate.train(x, y)
        jac = surrogate.jacobian(array([[0.]]))

        assert_rel_error(self, jac[0][0], 1., 1e-3)
Beispiel #6
0
    def test_1d_kriging3(self):
        # Test for least squares solver utilization when ill-conditioned
        x = [[case] for case in linspace(0., 1., 40)]
        y = sin(x).flatten()
        krig1 = KrigingSurrogate()
        krig1.train(x, y)
        new_x = array([0.5])
        mu, sigma = krig1.predict(new_x)

        self.assertAlmostEqual(8.7709e-09, sigma, places=7)
        self.assertAlmostEqual(0.479425538688, mu, places=7)
Beispiel #7
0
    def test_1d_kriging_predictor(self):
        x = array([[0.05], [.25], [0.61], [0.95]])
        y = array([0.738513784857542, -0.210367746201974, -0.489015457891476, 12.3033138316612])

        krig1 = KrigingSurrogate()
        krig1.train(x, y)
        new_x = array([0.5])
        mu, sigma = krig1.predict(new_x)

        self.assertAlmostEqual(.41552, sigma, places=3)
        self.assertAlmostEqual( -1.725, mu, places=3)
Beispiel #8
0
    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")
Beispiel #9
0
    def test_1d_ill_conditioned(self):
        # Test for least squares solver utilization when ill-conditioned
        x = array([[case] for case in linspace(0., 1., 40)])
        y = sin(x)
        surrogate = KrigingSurrogate()
        surrogate.train(x, y)
        new_x = array([0.5])
        mu, sigma = surrogate.predict(new_x)

        self.assertTrue(sigma < 1.1e-8)
        assert_rel_error(self, mu, sin(0.5), 1e-6)
Beispiel #10
0
    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)
Beispiel #11
0
    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)
Beispiel #12
0
    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)
Beispiel #13
0
    def test_2d_kriging(self):
        def bran(x):
            y = (x[1]-(5.1/(4.*pi**2.))*x[0]**2.+5.*x[0]/pi-6.)**2.+10.*(1.-1./(8.*pi))*cos(x[0])+10.
            return y

        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([bran(case) for case in x])

        krig1 = KrigingSurrogate()
        krig1.train(x, y)
        mu, sigma = krig1.predict([-2., 0.])
        self.assertAlmostEqual(bran(x[0]), mu, places=5)

        mu, sigma = krig1.predict([5., 5.])

        self.assertAlmostEqual(5.79, sigma, places=0)
        self.assertAlmostEqual(25.34, mu, places=1)
Beispiel #14
0
    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_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], 0.86323233, 1e-4)  # mean
        self.assertTrue(self, prob['sin_mm.f_x'][1] < 1e-5)  #std deviation