def test_float_optimization(tmp_dir):

    def build_model(hp):
        return (((((- 1) * (hp['a'] ** 3)) + (hp['b'] ** 3)) + hp['c']) - abs(hp['d']))

    class PolynomialTuner(kt.engine.base_tuner.BaseTuner):

        def run_trial(self, trial):
            hps = trial.hyperparameters
            score = self.hypermodel.build(hps)
            self.oracle.update_trial(trial.trial_id, {'score': score})
    hps = hp_module.HyperParameters()
    hps.Float('a', (- 1), 1)
    hps.Float('b', (- 1), 1)
    hps.Float('c', (- 1), 1)
    hps.Float('d', (- 1), 1)
    tuner = PolynomialTuner(hypermodel=build_model, oracle=kt.oracles.BayesianOptimization(objective=kt.Objective('score', 'max'), hyperparameters=hps, max_trials=50), directory=tmp_dir)
    tuner.search()
    (atol, rtol) = (0.1, 0.1)
    best_trial = tuner.oracle.get_best_trials()[0]
    best_hps = best_trial.hyperparameters
    assert np.isclose(best_trial.score, 3, atol=atol, rtol=rtol)
    assert np.isclose(best_hps['a'], (- 1), atol=atol, rtol=rtol)
    assert np.isclose(best_hps['b'], 1, atol=atol, rtol=rtol)
    assert np.isclose(best_hps['c'], 1, atol=atol, rtol=rtol)
    assert np.isclose(best_hps['d'], 0, atol=atol, rtol=rtol)
def test_float_optimization(tmp_dir):

    def build_model(hp):
        return (((((- 1) * (hp['a'] ** 3)) + (hp['b'] ** 3)) + hp['c']) - abs(hp['d']))

    class PolynomialTuner(kt.engine.base_tuner.BaseTuner):

        def run_trial(self, trial):
            hps = trial.hyperparameters
            score = self.hypermodel.build(hps)
            self.oracle.update_trial(trial.trial_id, {'score': score})
    hps = hp_module.HyperParameters()
    hps.Float('a', (- 1), 1)
    hps.Float('b', (- 1), 1)
    hps.Float('c', (- 1), 1)
    hps.Float('d', (- 1), 1)
    tuner = PolynomialTuner(hypermodel=build_model, oracle=kt.oracles.BayesianOptimization(objective=kt.Objective('score', 'max'), hyperparameters=hps, max_trials=50), directory=tmp_dir)
    tuner.search()
    (atol, rtol) = (0.1, 0.1)
    best_trial = tuner.oracle.get_best_trials()[0]
    best_hps = best_trial.hyperparameters
    assert np.isclose(best_trial.score, 3, atol=atol, rtol=rtol)
    assert np.isclose(best_hps['a'], (- 1), atol=atol, rtol=rtol)
    assert np.isclose(best_hps['b'], 1, atol=atol, rtol=rtol)
    assert np.isclose(best_hps['c'], 1, atol=atol, rtol=rtol)
    assert np.isclose(best_hps['d'], 0, atol=atol, rtol=rtol)