def test_no_training_data(self): 
     krig1 = KrigingSurrogate()
     
     try: 
         krig1.predict([0.,1.])
     except RuntimeError,err:
         self.assertEqual(str(err),"KrigingSurrogate has not been trained, so no prediction can be made")
    def test_1d_kriging2(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(1.18375,krig1.thetas,places=7)
 def test_get_uncertain_value(self): 
     x = array([[0.05], [.25], [0.61], [0.95]])
     y = array([0.738513784857542,-0.210367746201974,-0.489015457891476,12.3033138316612])
     krig1 = KrigingSurrogate(x,y)
     
     self.assertEqual(krig1.get_uncertain_value(1).mu,NormalDistribution(1.,0.).mu)
     self.assertEqual(krig1.get_uncertain_value(1).sigma,NormalDistribution(1.,0.).sigma)
 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(x,y)
     new_x = array([0.5])
     pred = krig1.predict(new_x)
    
     self.assertAlmostEqual(8.7709e-09,pred.sigma,places=7)
     self.assertAlmostEqual(0.479425538688,pred.mu,places=7)
 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(x,y)
     new_x = array([0.5])
     pred = krig1.predict(new_x)
     
     self.assertTrue(isinstance(pred,NormalDistribution))
     self.assertAlmostEqual(2.5086,pred.sigma,places=3)
     self.assertAlmostEqual(-1.37201,pred.mu,places=3)
Beispiel #6
0
    def test_multi_surrogate_models(self):
        metamodel = MetaModel()
        metamodel.name = 'meta'
        metamodel.surrogate = {
            'd': KrigingSurrogate(),
            'c': LogisticRegression()
        }
        metamodel.model = Simple()
        metamodel.recorder = DumbRecorder()
        simple = Simple()

        metamodel.a = simple.a = 1.
        metamodel.b = simple.b = 2.
        metamodel.train_next = True
        simple.run()
        metamodel.run()

        metamodel.a = simple.a = 3.
        metamodel.b = simple.b = 4.
        metamodel.train_next = True
        simple.run()
        metamodel.run()

        self.assertTrue(isinstance(metamodel.d, NormalDistribution))
        self.assertTrue(isinstance(metamodel.c, float))
Beispiel #7
0
    def test_default_execute(self):
        metamodel = MetaModel()
        metamodel.name = 'meta'
        metamodel.default_surrogate = KrigingSurrogate()
        metamodel.model = Simple()
        metamodel.recorder = DumbRecorder()
        simple = Simple()

        #import pdb; pdb.set_trace()

        metamodel.a = simple.a = 1.
        metamodel.b = simple.b = 2.
        metamodel.train_next = True
        simple.run()
        metamodel.run()

        metamodel.a = simple.a = 1.
        metamodel.b = simple.b = 2.
        metamodel.train_next = True
        simple.run()
        metamodel.run()

        self.assertEqual(metamodel.c.getvalue(), 3.)
        self.assertEqual(metamodel.d.getvalue(), -1.)
        self.assertEqual(metamodel.c.getvalue(), simple.c)
        self.assertEqual(metamodel.d.getvalue(), simple.d)
    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(x,y)
        pred = krig1.predict([-2.,0.])
        self.assertAlmostEqual(bran(x[0]),pred.mu,places=5)
        
        pred = krig1.predict([5.,5.])
        
        self.assertAlmostEqual(14.513550,pred.sigma,places=2)
        self.assertAlmostEqual(18.759264,pred.mu,places=2)
Beispiel #9
0
    def test_warm_start(self):
        metamodel = MetaModel()
        metamodel.name = 'meta'
        metamodel.default_surrogate = KrigingSurrogate()
        metamodel.model = Simple()
        metamodel.recorder = DumbRecorder()
        simple = Simple()

        cases = []

        metamodel.a = 1.
        metamodel.b = 2.
        metamodel.train_next = True
        metamodel.run()
        inputs = [('meta2.a', metamodel.a), ('meta2.b', metamodel.b)]
        outputs = [('meta2.c', metamodel.c.mu), ('meta2.d', metamodel.d.mu)]
        cases.append(Case(inputs=inputs, outputs=outputs))

        metamodel.a = 3.
        metamodel.b = 5.
        metamodel.train_next = True
        metamodel.run()
        inputs = [('meta2.a', metamodel.a), ('meta2.b', metamodel.b)]
        outputs = [('meta2.c', metamodel.c.mu), ('meta2.d', metamodel.d.mu)]
        cases.append(Case(inputs=inputs, outputs=outputs))

        case_iter = ListCaseIterator(cases)

        metamodel2 = MetaModel()
        metamodel2.name = 'meta2'
        metamodel2.default_surrogate = KrigingSurrogate()
        metamodel2.model = Simple()
        metamodel2.recorder = DumbRecorder()
        metamodel2.warm_start_data = case_iter

        metamodel2.a = simple.a = 1
        metamodel2.b = simple.b = 2
        metamodel.train_next = True
        metamodel2.run()
        simple.run()

        self.assertEqual(metamodel2.c.getvalue(), 3.)
        self.assertEqual(metamodel2.d.getvalue(), -1.)
        self.assertEqual(metamodel2.c.getvalue(), simple.c)
        self.assertEqual(metamodel2.d.getvalue(), simple.d)
Beispiel #10
0
    def test_excludes_with_vartrees(self):
        self.a.mm.default_surrogate = KrigingSurrogate()
        self.a.mm.model = InandOutTree()
        self.a.mm.excludes = ['ins', 'outs']

        # now try changing the excludes
        self.a.mm.excludes = ['outs']
        self.assertEqual(self.a.mm.surrogate_input_names(), ['ins.a', 'ins.b'])
        self.assertEqual(self.a.mm.surrogate_output_names(), [])
Beispiel #11
0
    def test_proxy(self):
        meta = MetaModel()
        meta.default_surrogate = KrigingSurrogate()
        meta.model = SimpleProxy()

        for i in range(3):
            meta.train_next = True
            meta.run()

        self.assertEqual(len(meta._training_data['f_x']), 3)
Beispiel #12
0
 def test_multi_surrogate_models_bad_surrogate_dict(self): 
     metamodel = MetaModel()
     metamodel.name = 'meta'
     metamodel.sur_d = KrigingSurrogate()
     try: 
         metamodel.model = Simple()
     except RuntimeError,err: 
         self.assertEqual("meta: No default surrogate model is defined and the following outputs do not have a surrogate model: ['c']. "
         "Either specify default_surrogate, or specify a surrogate model for all "
         "outputs.",str(err))
Beispiel #13
0
    def test_default_surrogate_change(self):
        metamodel = MetaModel()
        mmins = set(metamodel.list_inputs())
        mmouts = set(metamodel.list_outputs())
        metamodel.default_surrogate = KrigingSurrogate()
        metamodel.model = Simple()
        metamodel.default_surrogate = LogisticRegression()
        attrs = metamodel.get_attributes(io_only=False)
        for s in attrs['Slots']:
            self.assertNotEqual(s['name'], 'c')
            self.assertNotEqual(s['name'], 'd')
        inputs = set(metamodel.list_inputs())
        outputs = set(metamodel.list_outputs())
        self.assertEquals(inputs - mmins, set(['a', 'b']))
        self.assertEquals(outputs - mmouts, set(['c', 'd']))
        for i in range(3):
            metamodel.train_next = True
            metamodel.run()

        self.assertTrue(len(metamodel._training_data['c']) == 3)
        self.assertTrue(len(metamodel._training_data['d']) == 3)
        self.assertTrue(len(metamodel._training_input_history) == 3)

        metamodel.includes = ['a', 'b', 'c', 'd']

        self.assertTrue(len(metamodel._training_data['c']) == 3)
        self.assertTrue(len(metamodel._training_data['d']) == 3)
        self.assertTrue(len(metamodel._training_input_history) == 3)

        # removing an output should not clobber the rest of the training data
        metamodel.includes = ['a', 'b', 'c']

        self.assertTrue(len(metamodel._training_data['c']) == 3)
        self.assertTrue('d' not in metamodel._training_data)
        self.assertTrue(len(metamodel._training_input_history) == 3)

        # now put a different model in with the same inputs/outputs
        metamodel.model = SimpleMatch()
        metamodel.includes = ['a', 'b', 'c', 'd']
        inputs = set(metamodel.list_inputs())
        outputs = set(metamodel.list_outputs())
        self.assertEquals(inputs - mmins, set(['a', 'b']))
        self.assertEquals(outputs - mmouts, set(['c', 'd']))

        self.assertTrue(len(metamodel._training_data['c']) == 0)
        self.assertTrue(len(metamodel._training_data['d']) == 0)
        self.assertTrue(len(metamodel._training_input_history) == 0)

        # now put a different model in
        metamodel.model = Simple2()
        metamodel.includes = ['w', 'x', 'y', 'z']
        inputs = set(metamodel.list_inputs())
        outputs = set(metamodel.list_outputs())
        self.assertEquals(inputs - mmins, set(['w', 'x']))
        self.assertEquals(outputs - mmouts, set(['y', 'z']))
Beispiel #14
0
 def test_multi_surrogate_models_bad_surrogate_dict(self):
     metamodel = MetaModel()
     metamodel.name = 'meta'
     metamodel.surrogate = {'d': KrigingSurrogate()}
     try:
         metamodel.model = Simple()
     except ValueError, err:
         self.assertEqual(
             'meta: No default surrogate model was specified. '
             'Either specify a default, or specify a surrogate model for all '
             'outputs', str(err))
Beispiel #15
0
 def test_exclude_vartree_leaf(self):
     # Should not be allowed in this simplified
     #   implementation of variable trees in metamodel
     self.a.mm.model = InandOutTree()
     self.a.mm.default_surrogate = KrigingSurrogate()
     try:
         self.a.mm.includes = ['ins.a','outs.y']
     except Exception as err:
         self.assertEqual(str(err),
                          'mm: Can only include top level variable trees, not leaves')
     else:
         self.fail('Expected Exception')
Beispiel #16
0
    def test_excludes(self):
        metamodel = MyMetaModel()
        metamodel.default_surrogate = KrigingSurrogate()
        metamodel.excludes = ['b', 'd']
        metamodel.model = Simple()
        self.assertEqual(metamodel.surrogate_input_names(), ['a'])
        self.assertEqual(metamodel.surrogate_output_names(), ['c'])

        # now try changing the excludes
        metamodel.excludes = ['a', 'c']
        self.assertEqual(metamodel.surrogate_input_names(), ['b'])
        self.assertEqual(metamodel.surrogate_output_names(), ['d'])
Beispiel #17
0
    def test_excludes(self):
        metamodel = MyMetaModel()
        metamodel.surrogate = {'default': KrigingSurrogate()}
        metamodel.excludes = ['a', 'd']
        metamodel.model = Simple()
        self.assertEqual(metamodel.list_inputs_to_model(), ['b'])
        self.assertEqual(metamodel.list_outputs_from_model(), ['c'])

        # now try changing the excludes
        metamodel.excludes = ['b', 'c']
        self.assertEqual(metamodel.list_inputs_to_model(), ['a'])
        self.assertEqual(metamodel.list_outputs_from_model(), ['d'])
Beispiel #18
0
 def test_include_exclude_with_vartrees(self):
     self.a.mm.model = InandOutTree()
     self.a.mm.default_surrogate = KrigingSurrogate()
     self.a.mm.includes = ['ins']
     self.a.mm.excludes = ['outs']
     try:
         self.a.mm.run()
     except Exception as err:
         self.assertEqual(
             str(err), 'mm: includes and excludes are mutually exclusive')
     else:
         self.fail('Expected Exception')
Beispiel #19
0
    def _trained_asm(self, avals, bvals):
        asm = set_as_top(Assembly())
        asm.add('metamodel', MetaModel())
        asm.metamodel.surrogate = {'default': KrigingSurrogate()}
        asm.metamodel.model = Simple()
        asm.metamodel.recorder = DumbRecorder()
        asm.driver.workflow.add(['metamodel'])

        for a, b in zip(avals, bvals):
            asm.metamodel.a = a
            asm.metamodel.b = b
            asm.metamodel.train_next = 1
            asm.metamodel.run()

        return asm
Beispiel #20
0
    def test_comp_error(self):
        a = Assembly()
        a.add('m', MetaModel())
        a.m.surrogate = {'default': KrigingSurrogate()}
        a.m.model = DummyError()

        a.m.train_next = True

        a.driver.workflow.add('m')
        try:
            a.run()
        except RuntimeError as err:
            self.assertEqual("m.model: Test Error", str(err))
        else:
            self.fail("RuntimeError expected")
Beispiel #21
0
    def _get_assembly(self):
        asm = set_as_top(Assembly())
        asm.add('metamodel', MetaModel())
        asm.add('comp1', Simple())
        asm.add('comp2', Simple())
        asm.metamodel.surrogate = {'default': KrigingSurrogate()}
        asm.metamodel.model = Simple()
        asm.metamodel.recorder = DumbRecorder()
        asm.driver.workflow.add(['metamodel', 'comp1', 'comp2'])

        asm.connect('comp1.c', 'metamodel.a')
        asm.connect('comp1.d', 'metamodel.b')
        asm.connect('metamodel.c', 'comp2.a')
        asm.connect('metamodel.d', 'comp2.b')
        return asm
Beispiel #22
0
    def test_reset_training_data_event(self):
        metamodel = MetaModel()
        metamodel.name = 'meta'
        metamodel.surrogate = {'default': KrigingSurrogate()}
        metamodel.model = Simple()
        metamodel.recorder = DumbRecorder()
        simple = Simple()

        metamodel.a = 1.
        metamodel.b = 2.
        metamodel.train_next = True
        metamodel.run()

        metamodel.a = 2.
        metamodel.b = 3.
        metamodel.train_next = True
        metamodel.run()
Beispiel #23
0
    def test_model_change(self):
        metamodel = MetaModel()
        mmins = set(metamodel.list_inputs())
        mmouts = set(metamodel.list_outputs())
        metamodel.surrogate = {'default': KrigingSurrogate()}
        metamodel.model = Simple()
        inputs = set(metamodel.list_inputs())
        outputs = set(metamodel.list_outputs())
        self.assertEquals(inputs - mmins, set(['a', 'b']))
        self.assertEquals(outputs - mmouts, set(['c', 'd']))

        # now put a different model in
        metamodel.model = Simple2()
        inputs = set(metamodel.list_inputs())
        outputs = set(metamodel.list_outputs())
        self.assertEquals(inputs - mmins, set(['w', 'x']))
        self.assertEquals(outputs - mmouts, set(['y', 'z']))
Beispiel #24
0
 def _get_assembly(self, default=True, meta=True):
     asm = set_as_top(Assembly())
     asm.add('comp1', Simple())
     asm.add('comp2', Simple())
     if meta:
         asm.add('metamodel', MetaModel())
         
         if default:
             asm.metamodel.default_surrogate = KrigingSurrogate()
         
         asm.metamodel.model = Simple()
         asm.metamodel.recorder = DumbRecorder()
     else:
         asm.add('metamodel', Simple()) # put a real Simple comp in place of metamodel
         
     asm.driver.workflow.add(['metamodel','comp1','comp2'])
     
     asm.connect('comp1.c','metamodel.a')
     asm.connect('comp1.d','metamodel.b')
     asm.connect('metamodel.c','comp2.a')
     asm.connect('metamodel.d','comp2.b')
     return asm
Beispiel #25
0
    def test_include_exclude(self):
        metamodel = MyMetaModel()
        metamodel.surrogate = {'default': KrigingSurrogate()}
        metamodel.includes = ['a', 'd']
        try:
            metamodel.excludes = ['b', 'c']
        except RuntimeError as err:
            self.assertEqual(str(err),
                             ': includes and excludes are mutually exclusive')
        else:
            self.fail('Expected RuntimeError')
        self.assertEqual(metamodel.excludes, [])

        metamodel.includes = []
        metamodel.excludes = ['b', 'c']
        try:
            metamodel.includes = ['a', 'd']
        except Exception as err:
            self.assertEqual(str(err),
                             ': includes and excludes are mutually exclusive')
        else:
            self.fail('Expected Exception')
        self.assertEqual(metamodel.includes, [])
from time import time

import numpy as np
from numpy import pi, cos, sin

from openmdao.lib.surrogatemodels.kriging_surrogate import KrigingSurrogate

np.random.seed(12345)
N = 200

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 = np.random.random((N, 2))*10.0
y = np.array([bran(case) for case in x])

krig1 = KrigingSurrogate()

t0 = time()
krig1.train(x, y)
print 'Training Time elapsed', time() - t0

xx = np.random.random((N, 2))*10.0
t0 = time()
for jj in range(len(xx)):
    pred = krig1.predict(xx[jj, :])
print 'predicting Time elapsed', time() - t0

Beispiel #27
0
        metamodel.name = 'meta'
        metamodel.model = Simple()
        metamodel.surrogates['d'] = KrigingSurrogate()
        try:
            metamodel.run()
        except RuntimeError, err:
            self.assertEqual(
                "meta: No default surrogate model is defined and the following outputs do not have a surrogate model: ['c']. "
                "Either specify default_surrogate, or specify a surrogate model for all "
                "outputs.", str(err))
        else:
            self.fail('ValueError expected')

        metamodel = MetaModel()
        metamodel.name = 'meta'
        metamodel.surrogates['d'] = KrigingSurrogate()
        metamodel.includes = ['a', 'b', 'd']
        try:
            metamodel.model = Simple()
        except ValueError, err:
            if 'meta: No surrogate was provided for "c". All outputs must have a surrogate' == str(
                    err):
                self.fail(
                    'should not get a value error for variable c. It is not included in the metamodel'
                )

    def test_multi_surrogate_models(self):
        metamodel = MetaModel()
        metamodel.name = 'meta'
        metamodel.model = Simple()
        metamodel.surrogates['d'] = KrigingSurrogate()
Beispiel #28
0
        metamodel = MetaModel()
        metamodel.name = 'meta'
        metamodel.surrogate = {'d': KrigingSurrogate()}
        try:
            metamodel.model = Simple()
        except ValueError, err:
            self.assertEqual(
                'meta: No default surrogate model was specified. '
                'Either specify a default, or specify a surrogate model for all '
                'outputs', str(err))
        else:
            self.fail('ValueError expected')

        metamodel = MetaModel()
        metamodel.name = 'meta'
        metamodel.surrogate = {'d': KrigingSurrogate()}
        metamodel.includes = ['a', 'b', 'd']
        try:
            metamodel.model = Simple()
        except ValueError, err:
            if 'meta: Dict provided for "surrogates" does not include a value for "c". All outputs must be specified' == str(
                    err):
                self.fail(
                    'should not get a value error for variable c. It is not included in the metamodel'
                )

    def test_multi_surrogate_models(self):
        metamodel = MetaModel()
        metamodel.name = 'meta'
        metamodel.surrogate = {
            'd': KrigingSurrogate(),
Beispiel #29
0
 def test_in_and_out_tree_multiple_surrogates(self):
     self.a.mm.model = InandOutTree()
     self.a.mm.surrogates['outs.x'] = FloatKrigingSurrogate()
     self.a.mm.surrogates['outs.y'] = KrigingSurrogate()
     self._run_sweep(self.a, 'ins.a', 'ins.b', 'outs.x', 'outs.y')
Beispiel #30
0
 def test_in_tree_only_multiple_surrogates(self):
     self.a.mm.model = InTreeOnly()
     self.a.mm.surrogates['x'] = FloatKrigingSurrogate()
     self.a.mm.surrogates['y'] = KrigingSurrogate()
     self._run_sweep(self.a, 'ins.a', 'ins.b', 'x', 'y')
Beispiel #31
0
    def configure(self):

        self.add('mm', MetaModel())
        self.mm.surrogate = {'default': KrigingSurrogate()}
        self.mm.model = Dummy()
Beispiel #32
0
    def configure(self):

        self.add('mm', MetaModel())
        self.mm.default_surrogate = KrigingSurrogate()
        self.mm.model = Dummy()