Exemple #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')
Exemple #2
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.linearize(array([[0.5, 0.5]]))
        assert_rel_error(self, jac, array([[1, 1], [1, -1]]), 1e-5)
Exemple #3
0
    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)
Exemple #4
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)
Exemple #5
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.')
Exemple #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)
Exemple #7
0
    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)
Exemple #8
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)
Exemple #9
0
    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)
Exemple #10
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 requires at least'
                                            ' 2 training points.')
Exemple #11
0
    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)
Exemple #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_rel_error(self, mu, [y0], 1e-9)
            assert_rel_error(self, sigma, [[0]], 1e-5)
Exemple #13
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)
Exemple #14
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)
Exemple #15
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)
Exemple #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)
Exemple #17
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)
Exemple #18
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)
Exemple #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(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)
Exemple #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_near_equal(mu, [y0], 1e-9)
            assert_near_equal(sigma, [[0, 0]], 1e-6)
Exemple #21
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)
Exemple #22
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)
Exemple #23
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)
Exemple #24
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)
Exemple #25
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)
Exemple #26
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)
Exemple #27
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)
Exemple #28
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)
Exemple #29
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)
Exemple #30
0
                                                      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)
    print "Surrogate building of the objective is complete..."
                                    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..."