def testGetLastPoint(self): generated_points = np.array([[1, 2, 3], [4, 5, 6]]) RandomModelWithPoints = RandomModel(generated_points=generated_points) result = RandomModelWithPoints._get_last_point() expected = torch.tensor([[4], [5], [6]]) comparison = result == expected self.assertEqual(comparison.any(), True)
class RandomModelTest(TestCase): def setUp(self): self.random_model = RandomModel() def testRandomModelGenSamples(self): with self.assertRaises(NotImplementedError): self.random_model._gen_samples(n=1, tunable_d=1) def testRandomModelGenUnconstrained(self): with self.assertRaises(NotImplementedError): self.random_model._gen_unconstrained( n=1, d=2, tunable_feature_indices=np.array([])) def testConvertEqualityConstraints(self): fixed_features = {3: 0.7, 1: 0.5} d = 4 C, c = self.random_model._convert_equality_constraints( d, fixed_features) c_expected = torch.tensor([[0.5], [0.7]], dtype=torch.double) C_expected = torch.tensor([[0, 1, 0, 0], [0, 0, 0, 1]], dtype=torch.double) c_comparison = c == c_expected C_comparison = C == C_expected self.assertEqual(c_comparison.any(), True) self.assertEqual(C_comparison.any(), True) self.assertEqual( self.random_model._convert_equality_constraints(d, None), None) def testConvertInequalityConstraints(self): A = np.array([[1, 2], [3, 4]]) b = np.array([[5], [6]]) A_result, b_result = self.random_model._convert_inequality_constraints( (A, b)) A_expected = torch.tensor([[1, 2], [3, 4]], dtype=torch.double) b_expected = torch.tensor([[5], [6]], dtype=torch.double) A_comparison = A_result == A_expected b_comparison = b_result == b_expected self.assertEqual(A_comparison.any(), True) self.assertEqual(b_comparison.any(), True) self.assertEqual( self.random_model._convert_inequality_constraints(None), None) def testConvertBounds(self): bounds = [(1, 2), (3, 4), (5, 6)] bounds_result = self.random_model._convert_bounds(bounds) bounds_expected = torch.tensor([[1, 3, 5], [2, 4, 6]], dtype=torch.double) bounds_comparison = bounds_result == bounds_expected self.assertEqual(bounds_comparison.any(), True) self.assertEqual(self.random_model._convert_bounds(None), None) def testGetLastPoint(self): generated_points = np.array([[1, 2, 3], [4, 5, 6]]) RandomModelWithPoints = RandomModel(generated_points=generated_points) result = RandomModelWithPoints._get_last_point() expected = torch.tensor([[4], [5], [6]]) comparison = result == expected self.assertEqual(comparison.any(), True)
class RandomModelTest(TestCase): def setUp(self): self.random_model = RandomModel() def testRandomModelGenSamples(self): with self.assertRaises(NotImplementedError): self.random_model._gen_samples(n=1, tunable_d=1) def testRandomModelGenUnconstrained(self): with self.assertRaises(NotImplementedError): self.random_model._gen_unconstrained( n=1, d=2, tunable_feature_indices=np.array([]))
def testGen(self, mock_init, mock_gen): # Test with constraints modelbridge = RandomModelBridge() modelbridge.parameters = ["x", "y", "z"] modelbridge.transforms = OrderedDict() modelbridge.model = RandomModel() observation_features, weights, best_obsf, _ = modelbridge._gen( n=3, search_space=self.search_space, pending_observations={}, fixed_features=ObservationFeatures({"z": 3.0}), optimization_config=None, model_gen_options=self.model_gen_options, ) gen_args = mock_gen.mock_calls[0][2] self.assertEqual(gen_args["n"], 3) self.assertEqual(gen_args["bounds"], [(0.0, 1.0), (1.0, 2.0), (0.0, 5.0)]) self.assertTrue( np.array_equal( gen_args["linear_constraints"][0], np.array([[1.0, -1, 0.0], [-1.0, 0.0, -1.0]]), )) self.assertTrue( np.array_equal(gen_args["linear_constraints"][1], np.array([[0.0], [-3.5]]))) self.assertEqual(gen_args["fixed_features"], {2: 3.0}) self.assertEqual(gen_args["model_gen_options"], {"option": "yes"}) self.assertEqual(observation_features[0].parameters, { "x": 1.0, "y": 2.0, "z": 3.0 }) self.assertEqual(observation_features[1].parameters, { "x": 3.0, "y": 4.0, "z": 3.0 }) self.assertTrue(np.array_equal(weights, np.array([1.0, 2.0]))) # Test with no constraints, no fixed feature, no pending observations search_space = SearchSpace(self.parameters[:2]) modelbridge.parameters = ["x", "y"] modelbridge._gen( n=3, search_space=search_space, pending_observations={}, fixed_features=ObservationFeatures({}), optimization_config=None, model_gen_options=self.model_gen_options, ) gen_args = mock_gen.mock_calls[1][2] self.assertEqual(gen_args["bounds"], [(0.0, 1.0), (1.0, 2.0)]) self.assertIsNone(gen_args["linear_constraints"]) self.assertIsNone(gen_args["fixed_features"])
def setUp(self): self.random_model = RandomModel()