def test_one_pt(self): surrogate = ResponseSurface() x = array([[0.]]) y = array([[1.]]) surrogate.train(x, y) assert_rel_error(self, surrogate.betas, array([[1.], [0.], [0.]]), 1e-9)
def test_basics(self): # create a metamodel component mm = MetaModelUnStructuredComp() mm.add_input('x1', 0.) mm.add_input('x2', 0.) mm.add_output('y1', 0.) mm.add_output('y2', 0., surrogate=FloatKrigingSurrogate()) mm.options['default_surrogate'] = ResponseSurface() # add metamodel to a problem prob = Problem(model=Group()) prob.model.add_subsystem('mm', mm) prob.setup(check=False) # check that surrogates were properly assigned surrogate = mm._metadata('y1').get('surrogate') self.assertTrue(isinstance(surrogate, ResponseSurface)) surrogate = mm._metadata('y2').get('surrogate') self.assertTrue(isinstance(surrogate, FloatKrigingSurrogate)) # populate training data mm.options['train:x1'] = [1.0, 2.0, 3.0] mm.options['train:x2'] = [1.0, 3.0, 4.0] mm.options['train:y1'] = [3.0, 2.0, 1.0] mm.options['train:y2'] = [1.0, 4.0, 7.0] # run problem for provided data point and check prediction prob['mm.x1'] = 2.0 prob['mm.x2'] = 3.0 self.assertTrue(mm.train) # training will occur before 1st run prob.run_model() assert_rel_error(self, prob['mm.y1'], 2.0, .00001) assert_rel_error(self, prob['mm.y2'], 4.0, .00001) # run problem for interpolated data point and check prediction prob['mm.x1'] = 2.5 prob['mm.x2'] = 3.5 self.assertFalse(mm.train) # training will not occur before 2nd run prob.run_model() assert_rel_error(self, prob['mm.y1'], 1.5934, .001) # change default surrogate, re-setup and check that metamodel re-trains mm.options['default_surrogate'] = FloatKrigingSurrogate() prob.setup(check=False) surrogate = mm._metadata('y1').get('surrogate') self.assertTrue(isinstance(surrogate, FloatKrigingSurrogate)) self.assertTrue(mm.train) # training will occur after re-setup
def test_no_training_data(self): surrogate = ResponseSurface() try: surrogate.predict([0., 1.]) except RuntimeError as err: self.assertEqual(str(err), "ResponseSurface has not been trained, so no prediction can be made.") else: self.fail("RuntimeError Expected")
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 = ResponseSurface() surrogate.train(x, y) new_x = array([0.5]) mu = surrogate.predict(new_x) assert_rel_error(self, mu, sin(0.5), 1e-3)
def test_1d_training(self): x = array([[0.0], [2.0], [3.0]]) y = array([[branin_1d(case)] for case in x]) surrogate = ResponseSurface() surrogate.train(x, y) for x0, y0 in zip(x, y): mu = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9)
def test_vector_derivs(self): surrogate = ResponseSurface() 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)
def test_scalar_derivs(self): surrogate = ResponseSurface() 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)
def test_vector_input(self): surrogate = ResponseSurface() x = array([[0., 0., 0.], [1., 1., 1.]]) y = array([[0.], [3.]]) surrogate.train(x, y) for x0, y0 in zip(x, y): mu = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9)
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 = ResponseSurface() surrogate.train(x, y) new_x = array([pi]) mu = surrogate.predict(new_x) assert_rel_error(self, mu, 1.73114, 1e-4)
def test_vector_output(self): surrogate = ResponseSurface() x = array([[0.], [2.], [4.]]) y = array([[0., 0.], [1., 1.], [2., 0.]]) surrogate.train(x, y) for x0, y0 in zip(x, y): mu = surrogate.predict(x0) assert_near_equal(mu, y0, 1e-9)
def test_2d(self): x = array([[-2., 0.], [-0.5, 1.5], [1., 1.], [0., .25], [.25, 0.], [.66, .33]]) y = array([[branin(case)] for case in x]) surrogate = ResponseSurface() surrogate.train(x, y) for x0, y0 in zip(x, y): mu = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) mu = surrogate.predict(array([.5, .5])) assert_rel_error(self, mu, branin([.5, .5]), 1e-1)
def test_warning_bug(self): # Make sure we don't warn that we are doing FD when the surrogate has analytic derivs. x_train = np.arange(0., 10.) y_train = np.arange(10., 20.) z_train = x_train**2 + y_train**2 p = Problem() p.model = m = Group() params = IndepVarComp() params.add_output('x', val=0.) params.add_output('y', val=0.) m.add_subsystem('params', params, promotes=['*']) sm = MetaModelUnStructuredComp(default_surrogate=ResponseSurface()) sm.add_input('x', val=0.) sm.add_input('y', val=0.) sm.add_output('z', val=0.) sm.options['train:x'] = x_train sm.options['train:y'] = y_train sm.options['train:z'] = z_train # With or without the line below does not matter # Only when method is set to fd, then RuntimeWarning disappears sm.declare_partials('*', '*', method='exact') m.add_subsystem('sm', sm, promotes=['*']) m.add_design_var('x', lower=0., upper=10.) m.add_design_var('y', lower=0., upper=10.) m.add_objective('z') p.setup(check=True) stderr = sys.stderr str_err = StringIO() sys.stderr = str_err try: p.final_setup() finally: sys.stderr = stderr output = str_err.getvalue() self.assertTrue('finite difference' not in output)
def test_warm_start(self): # create metamodel with warm_restart = True mm = MetaModelUnStructured() mm.add_input('x1', 0.) mm.add_input('x2', 0.) mm.add_output('y1', 0.) mm.add_output('y2', 0.) mm.default_surrogate = ResponseSurface() mm.warm_restart = True # add to problem prob = Problem() prob.model.add_subsystem('mm', mm) prob.setup(check=False) # provide initial training data mm.metadata['train:x1'] = [1.0, 3.0] mm.metadata['train:x2'] = [1.0, 4.0] mm.metadata['train:y1'] = [3.0, 1.0] mm.metadata['train:y2'] = [1.0, 7.0] # run against a data point and check result prob['mm.x1'] = 2.0 prob['mm.x2'] = 3.0 prob.run_model() assert_rel_error(self, prob['mm.y1'], 1.9085, .001) assert_rel_error(self, prob['mm.y2'], 3.9203, .001) # Add 3rd training point, moves the estimate for that point # back to where it should be. mm.metadata['train:x1'] = [2.0] mm.metadata['train:x2'] = [3.0] mm.metadata['train:y1'] = [2.0] mm.metadata['train:y2'] = [4.0] mm.train = True # currently need to tell meta to re-train prob.run_model() assert_rel_error(self, prob['mm.y1'], 2.0, .00001) assert_rel_error(self, prob['mm.y2'], 4.0, .00001)
def test_warm_start(self): # create metamodel with warm_restart = True meta = MetaModel() meta.add_param('x1', 0.) meta.add_param('x2', 0.) meta.add_output('y1', 0.) meta.add_output('y2', 0.) meta.default_surrogate = ResponseSurface() meta.warm_restart = True # add to problem prob = Problem(Group()) prob.root.add('meta', meta) prob.setup(check=False) # provide initial training data prob['meta.train:x1'] = [1.0, 3.0] prob['meta.train:x2'] = [1.0, 4.0] prob['meta.train:y1'] = [3.0, 1.0] prob['meta.train:y2'] = [1.0, 7.0] # run against a data point and check result prob['meta.x1'] = 2.0 prob['meta.x2'] = 3.0 prob.run() assert_rel_error(self, prob['meta.y1'], 1.9085, .001) assert_rel_error(self, prob['meta.y2'], 3.9203, .001) # Add 3rd training point, moves the estimate for that point # back to where it should be. prob['meta.train:x1'] = [2.0] prob['meta.train:x2'] = [3.0] prob['meta.train:y1'] = [2.0] prob['meta.train:y2'] = [4.0] meta.train = True # currently need to tell meta to re-train prob.run() assert_rel_error(self, prob['meta.y1'], 2.0, .00001) assert_rel_error(self, prob['meta.y2'], 4.0, .00001)
def test_metamodel_deprecated(self): # run same test as above, only with the deprecated component, # to ensure we get the warning and the correct answer. # self-contained, to be removed when class name goes away. from openmdao.components.meta_model_unstructured_comp import MetaModel # deprecated msg = "'MetaModel' has been deprecated. Use 'MetaModelUnStructuredComp' instead." with assert_warning(DeprecationWarning, msg): mm = MetaModel() mm.add_input('x1', 0.) mm.add_input('x2', 0.) mm.add_output('y1', 0.) mm.add_output('y2', 0., surrogate=FloatKrigingSurrogate()) msg = "The 'default_surrogate' attribute provides backwards compatibility with " \ "earlier version of OpenMDAO; use options['default_surrogate'] instead." with assert_warning(DeprecationWarning, msg): mm.default_surrogate = ResponseSurface() # add metamodel to a problem prob = Problem(model=Group()) prob.model.add_subsystem('mm', mm) prob.setup(check=False) # check that surrogates were properly assigned surrogate = mm._metadata('y1').get('surrogate') self.assertTrue(isinstance(surrogate, ResponseSurface)) surrogate = mm._metadata('y2').get('surrogate') self.assertTrue(isinstance(surrogate, FloatKrigingSurrogate)) # populate training data msg = "The 'metadata' attribute provides backwards compatibility " \ "with earlier version of OpenMDAO; use 'options' instead." with assert_warning(DeprecationWarning, msg): mm.metadata['train:x1'] = [1.0, 2.0, 3.0] mm.metadata['train:x2'] = [1.0, 3.0, 4.0] mm.metadata['train:y1'] = [3.0, 2.0, 1.0] mm.metadata['train:y2'] = [1.0, 4.0, 7.0] # run problem for provided data point and check prediction prob['mm.x1'] = 2.0 prob['mm.x2'] = 3.0 self.assertTrue(mm.train) # training will occur before 1st run prob.run_model() assert_rel_error(self, prob['mm.y1'], 2.0, .00001) assert_rel_error(self, prob['mm.y2'], 4.0, .00001) # run problem for interpolated data point and check prediction prob['mm.x1'] = 2.5 prob['mm.x2'] = 3.5 self.assertFalse(mm.train) # training will not occur before 2nd run prob.run_model() assert_rel_error(self, prob['mm.y1'], 1.5934, .001) # change default surrogate, re-setup and check that metamodel re-trains msg = "The 'default_surrogate' attribute provides backwards compatibility with " \ "earlier version of OpenMDAO; use options['default_surrogate'] instead." with assert_warning(DeprecationWarning, msg): mm.default_surrogate = FloatKrigingSurrogate() prob.setup(check=False) surrogate = mm._metadata('y1').get('surrogate') self.assertTrue(isinstance(surrogate, FloatKrigingSurrogate)) self.assertTrue(mm.train) # training will occur after re-setup prob['mm.x1'] = 2.5 prob['mm.x2'] = 3.5 prob.run_model() assert_rel_error(self, prob['mm.y1'], 1.5, 1e-2)
def test_basics(self): # create a metamodel component mm = MetaModel() mm.add_param('x1', 0.) mm.add_param('x2', 0.) mm.add_output('y1', 0.) mm.add_output('y2', 0., surrogate=FloatKrigingSurrogate()) mm.default_surrogate = ResponseSurface() # add metamodel to a problem prob = Problem(root=Group()) prob.root.add('mm', mm) prob.setup(check=False) # check that surrogates were properly assigned surrogate = prob.root.unknowns.metadata('mm.y1').get('surrogate') self.assertTrue(isinstance(surrogate, ResponseSurface)) surrogate = prob.root.unknowns.metadata('mm.y2').get('surrogate') self.assertTrue(isinstance(surrogate, FloatKrigingSurrogate)) # populate training data prob['mm.train:x1'] = [1.0, 2.0, 3.0] prob['mm.train:x2'] = [1.0, 3.0, 4.0] prob['mm.train:y1'] = [3.0, 2.0, 1.0] prob['mm.train:y2'] = [1.0, 4.0, 7.0] # run problem for provided data point and check prediction prob['mm.x1'] = 2.0 prob['mm.x2'] = 3.0 self.assertTrue(mm.train) # training will occur before 1st run prob.run() assert_rel_error(self, prob['mm.y1'], 2.0, .00001) assert_rel_error(self, prob['mm.y2'], 4.0, .00001) # run problem for interpolated data point and check prediction prob['mm.x1'] = 2.5 prob['mm.x2'] = 3.5 self.assertFalse(mm.train) # training will not occur before 2nd run prob.run() assert_rel_error(self, prob['mm.y1'], 1.5934, .001) # change default surrogate, re-setup and check that metamodel re-trains mm.default_surrogate = FloatKrigingSurrogate() prob.setup(check=False) surrogate = prob.root.unknowns.metadata('mm.y1').get('surrogate') self.assertTrue(isinstance(surrogate, FloatKrigingSurrogate)) self.assertTrue(mm.train) # training will occur after re-setup mm.warm_restart = True # use existing training data prob['mm.x1'] = 2.5 prob['mm.x2'] = 3.5 prob.run() assert_rel_error(self, prob['mm.y1'], 2., 1e-2)