Esempio n. 1
0
    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')
Esempio n. 2
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")
Esempio n. 3
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")
Esempio n. 4
0
    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)
Esempio n. 5
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)
Esempio n. 6
0
 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)
Esempio n. 7
0
 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)
Esempio n. 8
0
    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)
Esempio n. 9
0
 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)
Esempio n. 10
0
    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)
Esempio n. 11
0
    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)
Esempio n. 12
0
    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)
Esempio n. 13
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)
Esempio n. 14
0
    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)
Esempio n. 15
0
    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)
Esempio n. 16
0
    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)
Esempio n. 17
0
    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)
Esempio n. 18
0
    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)
Esempio n. 19
0
    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)
Esempio n. 20
0
    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)
Esempio n. 21
0
    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)
Esempio n. 22
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)
Esempio n. 23
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)
Esempio n. 24
0
    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)
Esempio n. 25
0
            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)
    print "Surrogate building of the objective is complete..."

    # Call the surrogate for the constraints
    if M > 0:
        for mm in xrange(M):
            surrogate = KrigingSurrogate()
            surrogate.train(ModelInfo_g[mm].X_org, ModelInfo_g[mm].y, False)
            ModelInfo_g[mm].X = surrogate.X
            ModelInfo_g[mm].thetas = surrogate.thetas
            ModelInfo_g[