Example #1
0
    def setUp(self):
        self.k_x = FloatKrigingSurrogate()
        self.k_y = FloatKrigingSurrogate()

        self.k_x.train([(1, 2), (2, 3)], [2, 4])
        self.k_y.train([(1, 2), (2, 3)], [4, 7])

        self.a = set_as_top(MMTest())
Example #2
0
    def setUp(self): 
        self.k_x = FloatKrigingSurrogate()
        self.k_y = FloatKrigingSurrogate()

        self.k_x.train([(1, 2), (2, 3)], [2, 4])
        self.k_y.train([(1, 2), (2, 3)], [4, 7])

        self.a = set_as_top(MMTest())
Example #3
0
    def test_assy_promotion(self):

        class Trig(Component):

            x = Float(0,iotype="in",units="rad")

            f_x_sin = Float(0.0,iotype="out")
            f_x_cos = Float(0.0,iotype="out")

            def execute(self):
                self.f_x_sin = .5*sin(self.x)
                self.f_x_cos = .5*cos(self.x)


        class TrigAsmb(Assembly):

            x = Float(0,iotype="in",units="rad")

            def configure(self):

                self.add('trig1', Trig())
                self.add('trig2', Trig())

                self.connect('x', 'trig1.x')
                self.connect('trig1.f_x_sin', 'trig2.x')

                self.create_passthrough('trig2.f_x_sin')
                self.create_passthrough('trig2.f_x_cos')

                self.driver.workflow.add(['trig1', 'trig2'])

        top = set_as_top(Assembly())
        top.add("trig_meta_model", MetaModel())
        top.trig_meta_model.model = TrigAsmb()
        top.trig_meta_model.surrogates['f_x_sin'] = FloatKrigingSurrogate()
        top.trig_meta_model.surrogates['f_x_cos'] = FloatKrigingSurrogate()

        # Will error if vars don't promote correctly
        top.run()
Example #4
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')
Example #5
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')
Example #6
0
class TestMetaModelWithVtree(unittest.TestCase):
    def setUp(self):
        self.k_x = FloatKrigingSurrogate()
        self.k_y = FloatKrigingSurrogate()

        self.k_x.train([(1, 2), (2, 3)], [2, 4])
        self.k_y.train([(1, 2), (2, 3)], [4, 7])

        self.a = set_as_top(MMTest())

    def tearDown(self):
        self.a = None

    def _run_sweep(self, asmb, a_name, b_name, x_name, y_name):
        in_arrays = (type(a_name) == tuple)

        def set_ab(a, b):
            if in_arrays:
                asmb.mm.set(a_name[0], a, (a_name[1], ))
                asmb.mm.set(b_name[0], b, (b_name[1], ))
            else:
                asmb.mm.set(a_name, a)
                asmb.mm.set(b_name, b)

        asmb.mm.train_next = True

        set_ab(1, 2)
        asmb.run()
        x = asmb.mm.get(x_name)
        y = asmb.mm.get(y_name)
        self.assertEqual(x, 2)
        if (isinstance(y, UncertainDistribution)):
            self.assertEqual(y.getvalue(), 4)
        else:
            self.assertEqual(y, 4)

        asmb.mm.train_next = True
        set_ab(2, 3)
        asmb.run()
        x = asmb.mm.get(x_name)
        y = asmb.mm.get(y_name)
        self.assertEqual(x, 4)
        if (isinstance(y, UncertainDistribution)):
            self.assertEqual(y.getvalue(), 7)
        else:
            self.assertEqual(y, 7)

        #predictions
        set_ab(1, 2)
        asmb.run()

        x = asmb.mm.get(x_name)
        y = asmb.mm.get(y_name)
        self.assertEqual(x, self.k_x.predict((1, 2)))
        if (isinstance(y, UncertainDistribution)):
            self.assertEqual(y.getvalue(), self.k_y.predict((1, 2)))
        else:
            self.assertEqual(y, self.k_y.predict((1, 2)))

        set_ab(1.5, 2.5)
        asmb.run()

        x = asmb.mm.get(x_name)
        y = asmb.mm.get(y_name)
        self.assertEqual(x, self.k_x.predict((1.5, 2.5)))
        if (isinstance(y, UncertainDistribution)):
            self.assertEqual(y.getvalue(), self.k_y.predict((1.5, 2.5)))
        else:
            self.assertEqual(y, self.k_y.predict((1.5, 2.5)))

        set_ab(2, 3)
        asmb.run()

        x = asmb.mm.get(x_name)
        y = asmb.mm.get(y_name)
        self.assertEqual(x, self.k_x.predict((2, 3)))
        if (isinstance(y, UncertainDistribution)):
            self.assertEqual(y.getvalue(), self.k_y.predict((2, 3)))
        else:
            self.assertEqual(y, self.k_y.predict((2, 3)))

    def test_in_tree_only(self):
        self.a.mm.model = InTreeOnly()

        self._run_sweep(self.a, 'ins.a', 'ins.b', 'x', 'y')

    def test_in_and_out_tree(self):
        self.a.mm.model = InandOutTree()
        self._run_sweep(self.a, 'ins.a', 'ins.b', 'outs.x', 'outs.y')

    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')

    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')

    def test_includes_with_vartrees(self):
        self.a.mm.default_surrogate = KrigingSurrogate()
        self.a.mm.includes = ['ins']
        self.a.mm.model = InandOutTree()
        self.assertEqual(self.a.mm.surrogate_input_names(), ['ins.a', 'ins.b'])
        self.assertEqual(self.a.mm.surrogate_output_names(), [])

        # now try changing the includes
        self.a.mm.includes = ['ins', 'outs']
        self.assertEqual(self.a.mm.surrogate_input_names(), ['ins.a', 'ins.b'])
        self.assertEqual(self.a.mm.surrogate_output_names(),
                         ['outs.y', 'outs.x'])

    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(), [])

    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')

    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')
Example #7
0
 def configure(self):
     mm = MetaModel()
     self.add('mm', mm)
     self.mm.default_surrogate = FloatKrigingSurrogate()
     self.driver.workflow.add('mm')
Example #8
0
class TestMetaModelWithVtree(unittest.TestCase):

    def setUp(self): 
        self.k_x = FloatKrigingSurrogate()
        self.k_y = FloatKrigingSurrogate()

        self.k_x.train([(1, 2), (2, 3)], [2, 4])
        self.k_y.train([(1, 2), (2, 3)], [4, 7])

        self.a = set_as_top(MMTest())

    def tearDown(self): 
        self.a = None

    def _run_sweep(self, asmb, a_name, b_name, x_name, y_name):
        in_arrays = (type(a_name)==tuple)

        def set_ab(a, b):
            if in_arrays:
                asmb.mm.set(a_name[0], a, (a_name[1],))
                asmb.mm.set(b_name[0], b, (b_name[1],))
            else: 
                asmb.mm.set(a_name, a)
                asmb.mm.set(b_name, b)  

        asmb.mm.train_next = True
        
        set_ab(1, 2)
        asmb.run()  
        x = asmb.mm.get(x_name)
        y = asmb.mm.get(y_name)
        self.assertEqual(x, 2)
        if ( isinstance(y,UncertainDistribution) ):
            self.assertEqual(y.getvalue(), 4)
        else:
            self.assertEqual(y, 4)

        asmb.mm.train_next = True
        set_ab(2, 3)
        asmb.run()
        x = asmb.mm.get(x_name)
        y = asmb.mm.get(y_name)
        self.assertEqual(x, 4)
        if ( isinstance(y,UncertainDistribution) ):
            self.assertEqual(y.getvalue(), 7)     
        else:
            self.assertEqual(y, 7)     

        #predictions
        set_ab(1, 2)
        asmb.run()

        x = asmb.mm.get(x_name)
        y = asmb.mm.get(y_name)
        self.assertEqual(x, self.k_x.predict((1, 2)))
        if ( isinstance(y,UncertainDistribution) ):
            self.assertEqual(y.getvalue(), self.k_y.predict((1, 2)))
        else:
            self.assertEqual(y, self.k_y.predict((1, 2)))

        set_ab(1.5, 2.5)
        asmb.run()

        x = asmb.mm.get(x_name)
        y = asmb.mm.get(y_name)
        self.assertEqual(x, self.k_x.predict((1.5, 2.5)))
        if ( isinstance(y,UncertainDistribution) ):
            self.assertEqual(y.getvalue(), self.k_y.predict((1.5, 2.5)))
        else:
            self.assertEqual(y, self.k_y.predict((1.5, 2.5)))

        set_ab(2, 3)
        asmb.run()

        x = asmb.mm.get(x_name)
        y = asmb.mm.get(y_name)
        self.assertEqual(x, self.k_x.predict((2, 3)))
        if ( isinstance(y,UncertainDistribution) ):
            self.assertEqual(y.getvalue(), self.k_y.predict((2, 3)))  
        else:
            self.assertEqual(y, self.k_y.predict((2, 3)))  

    def test_in_tree_only(self):
        self.a.mm.model = InTreeOnly()

        self._run_sweep(self.a, 'ins.a', 'ins.b', 'x', 'y')

    def test_in_and_out_tree(self):
        self.a.mm.model = InandOutTree()
        self._run_sweep(self.a, 'ins.a', 'ins.b', 'outs.x', 'outs.y')

    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')

    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')

    def test_includes_with_vartrees(self):
        self.a.mm.default_surrogate = KrigingSurrogate()
        self.a.mm.includes = ['ins']
        self.a.mm.model = InandOutTree()
        self.assertEqual(self.a.mm.surrogate_input_names(), ['ins.a', 'ins.b'])
        self.assertEqual(self.a.mm.surrogate_output_names(), [])
        
        # now try changing the includes
        self.a.mm.includes = ['ins', 'outs']
        self.assertEqual(self.a.mm.surrogate_input_names(), ['ins.a', 'ins.b'])
        self.assertEqual(self.a.mm.surrogate_output_names(), ['outs.y', 'outs.x'])

    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(), [])

    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')

    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')