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_training(self): lr = ResponseSurface(self.X_train, self.Y_train) training_reconstruction = [lr.predict(x) for x in self.X_train] residual = sum([x - y for x, y in zip(training_reconstruction, self.Y_train)]) self.assertTrue(residual < 1e-5)
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_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_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.jacobian(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.jacobian(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_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_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_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'], 1.4609, .001)