def testCrossValidate(self, mock_init):
     ma = DiscreteModelBridge()
     model = mock.MagicMock(DiscreteModel, autospec=True, instance=True)
     model.cross_validate.return_value = (
         np.array([[1.0, -1], [2.0, -2]]),
         np.stack((np.array([[1.0, 4.0],
                             [4.0, 6]]), np.array([[2.0, 5.0], [5.0, 7]]))),
     )
     ma.model = model
     ma.parameters = ["x", "y", "z"]
     ma.outcomes = ["a", "b"]
     observation_data = ma._cross_validate(self.observation_features,
                                           self.observation_data,
                                           self.observation_features)
     Xs = [
         [[0, "foo", True], [1, "foo", True], [1, "bar", True]],
         [[0, "foo", True], [1, "foo", True]],
     ]
     Ys = [[[1.0], [2.0], [3.0]], [[-1.0], [-2.0]]]
     Yvars = [[[1.0], [2.0], [3.0]], [[6.0], [7.0]]]
     Xtest = [[0, "foo", True], [1, "foo", True], [1, "bar", True]]
     # Transform to arrays:
     model_cv_args = model.cross_validate.mock_calls[0][2]
     for i, x in enumerate(model_cv_args["Xs_train"]):
         self.assertEqual(x, Xs[i])
     for i, y in enumerate(model_cv_args["Ys_train"]):
         self.assertEqual(y, Ys[i])
     for i, v in enumerate(model_cv_args["Yvars_train"]):
         self.assertEqual(v, Yvars[i])
     self.assertEqual(model_cv_args["X_test"], Xtest)
     # Transform from arrays:
     for i, od in enumerate(observation_data):
         self.assertEqual(od, self.observation_data[i])
 def testPredict(self, mock_init):
     ma = DiscreteModelBridge()
     model = mock.MagicMock(DiscreteModel, autospec=True, instance=True)
     model.predict.return_value = (
         np.array([[1.0, -1], [2.0, -2]]),
         np.stack((np.array([[1.0, 4.0],
                             [4.0, 6]]), np.array([[2.0, 5.0], [5.0, 7]]))),
     )
     ma.model = model
     ma.parameters = ["x", "y", "z"]
     ma.outcomes = ["a", "b"]
     observation_data = ma._predict(self.observation_features)
     X = [[0, "foo", True], [1, "foo", True], [1, "bar", True]]
     self.assertTrue(model.predict.mock_calls[0][2]["X"], X)
     for i, od in enumerate(observation_data):
         self.assertEqual(od, self.observation_data[i])
    def testGen(self, mock_init):
        # Test with constraints
        optimization_config = OptimizationConfig(
            objective=Objective(Metric("a"), minimize=True),
            outcome_constraints=[
                OutcomeConstraint(Metric("b"), ComparisonOp.GEQ, 2, False)
            ],
        )
        ma = DiscreteModelBridge()
        model = mock.MagicMock(DiscreteModel, autospec=True, instance=True)
        model.gen.return_value = ([[0.0, 2.0, 3.0], [1.0, 1.0,
                                                     3.0]], [1.0, 2.0])
        ma.model = model
        ma.parameters = ["x", "y", "z"]
        ma.outcomes = ["a", "b"]
        observation_features, weights, best_observation = ma._gen(
            n=3,
            search_space=self.search_space,
            optimization_config=optimization_config,
            pending_observations=self.pending_observations,
            fixed_features=ObservationFeatures({}),
            model_gen_options=self.model_gen_options,
        )
        gen_args = model.gen.mock_calls[0][2]
        self.assertEqual(gen_args["n"], 3)
        self.assertEqual(gen_args["parameter_values"],
                         [[0.0, 1.0], ["foo", "bar"], [True]])
        self.assertTrue(
            np.array_equal(gen_args["objective_weights"], np.array([-1.0,
                                                                    0.0])))
        self.assertTrue(
            np.array_equal(gen_args["outcome_constraints"][0],
                           np.array([[0.0, -1.0]])))
        self.assertTrue(
            np.array_equal(gen_args["outcome_constraints"][1],
                           np.array([[-2]])))
        self.assertEqual(gen_args["pending_observations"][0], [])
        self.assertEqual(gen_args["pending_observations"][1],
                         [[0, "foo", True]])
        self.assertEqual(gen_args["model_gen_options"], {"option": "yes"})
        self.assertEqual(observation_features[0].parameters, {
            "x": 0.0,
            "y": 2.0,
            "z": 3.0
        })
        self.assertEqual(observation_features[1].parameters, {
            "x": 1.0,
            "y": 1.0,
            "z": 3.0
        })
        self.assertEqual(weights, [1.0, 2.0])

        # Test with no constraints, no fixed feature, no pending observations
        search_space = SearchSpace(self.parameters[:2])
        optimization_config.outcome_constraints = []
        ma.parameters = ["x", "y"]
        ma._gen(
            n=3,
            search_space=search_space,
            optimization_config=optimization_config,
            pending_observations={},
            fixed_features=ObservationFeatures({}),
            model_gen_options={},
        )
        gen_args = model.gen.mock_calls[1][2]
        self.assertEqual(gen_args["parameter_values"],
                         [[0.0, 1.0], ["foo", "bar"]])
        self.assertIsNone(gen_args["outcome_constraints"])
        self.assertIsNone(gen_args["pending_observations"])

        # Test validation
        optimization_config = OptimizationConfig(
            objective=Objective(Metric("a"), minimize=False),
            outcome_constraints=[
                OutcomeConstraint(Metric("b"), ComparisonOp.GEQ, 2, True)
            ],
        )
        with self.assertRaises(ValueError):
            ma._gen(
                n=3,
                search_space=search_space,
                optimization_config=optimization_config,
                pending_observations={},
                fixed_features=ObservationFeatures({}),
                model_gen_options={},
            )