def test_bounds_and_initial_points(self): np.random.seed(13) bounds = {'x': np.array([[-10., 11.0], [-20.0, 1.0]])} desvars = {'x': np.array([0., 0.25])} model_low = simple_2D_low_model(desvars) model_high = simple_2D_high_model(desvars) method_instance = BaseMethod(model_low, model_high, bounds, disp=False) init_points = np.array([[6.33175062, -15.01163438], [7.30984919, 0.28073316], [10.42462339, -10.4775658], [2.78989172, -3.71394319], [3.47388024, -4.83761718]]) np.testing.assert_allclose(method_instance.design_vectors, init_points) np.random.seed(13) method_instance = BaseMethod(model_low, model_high, bounds, disp=False, num_initial_points=3) np.testing.assert_allclose(method_instance.design_vectors, init_points[:3, :])
def test_set_initial_point(self): np.random.seed(13) bounds = {'x': np.array([[0.0, 1.0], [0.0, 1.0]])} desvars = {'x': np.array([0., 0.25])} model_low = simple_2D_low_model(desvars) model_high = simple_2D_high_model(desvars) trust_region = BaseMethod(model_low, model_high, bounds, disp=False) trust_region.add_objective('y') trust_region.set_initial_point([0.5, 0.5]) np.testing.assert_allclose(trust_region.design_vectors[-1, :], [0.5, 0.5])
def test_optimization(self): np.random.seed(13) bounds = {'x' : np.array([[0.0, 1.0], [0.0, 1.0]])} desvars = {'x' : np.array([0., 0.25])} model_low = simple_2D_low_model(desvars) model_high = simple_2D_high_model(desvars) trust_region = SimpleTrustRegion(model_low, model_high, bounds, disp=False) trust_region.add_objective('y') results = trust_region.optimize() np.testing.assert_allclose(results['optimal_design'], [0., 0.333], atol=1e-3)
def test_approximation(self): np.random.seed(13) bounds = {'x': np.array([[0.0, 1.0], [0.0, 1.0]])} desvars = {'x': np.array([0., 0.25])} model_low = simple_2D_low_model(desvars) model_high = simple_2D_high_model(desvars) method_instance = BaseMethod(model_low, model_high, bounds, disp=False) method_instance.add_objective('y') method_instance.construct_approximations() func = method_instance.approximation_functions['y'] flattened_desvars = model_low.flatten_desvars(desvars) np.testing.assert_allclose(func(flattened_desvars), -5.33064616)
def test_run_outputs(self): desvars_init = {} desvars_init['x'] = [2., 1.] model = simple_2D_high_model(desvars_init) self.assertEqual(model.total_size, 2) flattened_desvars = model.flatten_desvars(desvars_init) outputs = model.run(flattened_desvars) self.assertEqual(outputs['y'], 18.) self.assertEqual(outputs['con'], 21.) flattened_desvars_2d = np.array([[2., 1.], [1., 0.5]]) outputs = model.run_vec(flattened_desvars_2d) np.testing.assert_allclose(outputs['y'], [18., 0.5]) np.testing.assert_allclose(outputs['con'], [21., 1.625])
def test_save_outputs(self): desvars_init = {} desvars_init['x'] = [2., 1.] model = simple_2D_high_model(desvars_init, 'test.pkl') self.assertEqual(model.total_size, 2) flattened_desvars = model.flatten_desvars(desvars_init) outputs = model.run(flattened_desvars) self.assertEqual(outputs['y'], 18.) self.assertEqual(outputs['con'], 21.) # Should return the same values that were saved outputs = model.run(flattened_desvars) self.assertEqual(outputs['y'], 18.) self.assertEqual(outputs['con'], 21.) # Should only have called the model once self.assertEqual(len(model.saved_desvars), 1)
def test_constrained_optimization(self): np.random.seed(13) bounds = {"x": np.array([[0.0, 1.0], [0.0, 1.0]])} desvars = {"x": np.array([0.0, 0.25])} model_low = simple_2D_low_model(desvars) model_high = simple_2D_high_model(desvars) trust_region = SimpleTrustRegion(model_low, model_high, bounds, num_initial_points=10, disp=False) trust_region.add_objective("y") trust_region.add_constraint("con", equals=0.0) results = trust_region.optimize(plot=False, num_iterations=10) np.testing.assert_allclose(results["optimal_design"], [0.0, 0.10987], atol=1e-3) np.testing.assert_allclose(results["outputs"]["con"], 0.0, atol=1e-5)
import numpy as np from weis.multifidelity.models.testbed_components import simple_2D_high_model, simple_2D_low_model from weis.multifidelity.methods.trust_region import SimpleTrustRegion # Following Algo 2.1 from Andrew March's dissertation np.random.seed(13) bounds = {'x': np.array([[0.0, 1.0], [0.0, 1.0]])} desvars = {'x': np.array([0., 0.25])} model_low = simple_2D_low_model(desvars) model_high = simple_2D_high_model(desvars) trust_region = SimpleTrustRegion(model_low, model_high, bounds) trust_region.add_objective('y') trust_region.set_initial_point(desvars['x']) trust_region.optimize()
n_dims = 2 num_initial_points = 4 x_init = np.random.rand(num_initial_points, n_dims) x = x_init.copy() print() print(x_init) list_of_desvars = [] for i in range(num_initial_points): desvars = OrderedDict() desvars['x'] = x[i, :] list_of_desvars.append(desvars) low = simple_2D_low_model(desvars, 'low_2D_results.pkl') high = simple_2D_high_model(desvars, 'high_2D_results.pkl') lofi_function = low.run_vec hifi_function = high.run_vec for i in range(21): y_low = lofi_function(list_of_desvars) y_high = hifi_function(list_of_desvars) # Construct RBF interpolater for error function differences = y_high - y_low input_arrays = np.split(x, x.shape[1], axis=1) input_arrays = [x.flatten() for x in input_arrays]