<commit_before>__author__ = '<NAME>'

from apsis.optimizers.bayesian_optimization import BayesianOptimizer
from nose.tools import assert_is_none, assert_equal, assert_dict_equal, \
    assert_true, assert_false
from apsis.optimizers.bayesian.acquisition_functions import ExpectedImprovement, ProbabilityOfImprovement
from apsis.models.experiment import Experiment
from apsis.models.parameter_definition import MinMaxNumericParamDef
from apsis.models.candidate import Candidate

class testAcquisitionFunction(object):

    def test_EI(self):
        exp = Experiment("test", {"x": MinMaxNumericParamDef(0, 1)})
        opt = BayesianOptimizer(exp, {"initial_random_runs": 3,
                                      "max_searcher": "LBFGSB"})

        for i in range(3):
            cands = opt.get_next_candidates(2)
            cand_one = cands[0]
            cand_two = cands[1]
            assert_true(isinstance(cand_one, Candidate))
            cand_one.result = 2
            exp.add_finished(cand_one)
            opt.update(exp)
            assert_true(isinstance(cand_two, Candidate))
            cand_two.result = 2
            exp.add_finished(cand_two)
            opt.update(exp)
        cands = opt.get_next_candidates(num_candidates=3)
        assert_equal(len(cands), 3)

    def test_PoI(self):
        exp = Experiment("test", {"x": MinMaxNumericParamDef(0, 1)})
        opt = BayesianOptimizer(exp, {"initial_random_runs": 3, "acquisition": ProbabilityOfImprovement})
        assert_true(isinstance(opt.acquisition_function, ProbabilityOfImprovement))

        for i in range(3):
            cands = opt.get_next_candidates(2)
            cand_one = cands[0]
            cand_two = cands[1]
            assert_true(isinstance(cand_one, Candidate))
            cand_one.result = 2
            exp.add_finished(cand_one)
            opt.update(exp)
            assert_true(isinstance(cand_two, Candidate))
            cand_two.result = 2
            exp.add_finished(cand_two)
            opt.update(exp)<commit_msg>Added a few more acquisition tests.<commit_after>__author__ = '<NAME>'

from apsis.optimizers.bayesian_optimization import BayesianOptimizer
from nose.tools import assert_is_none, assert_equal, assert_dict_equal, \
    assert_true, assert_false
from apsis.optimizers.bayesian.acquisition_functions import ExpectedImprovement, ProbabilityOfImprovement
from apsis.models.experiment import Experiment
from apsis.models.parameter_definition import MinMaxNumericParamDef
from apsis.models.candidate import Candidate

class testAcquisitionFunction(object):

    def test_EI(self):
        exp = Experiment("test", {"x": MinMaxNumericParamDef(0, 1)})
        opt = BayesianOptimizer(exp, {"initial_random_runs": 3,
                                      "max_searcher": "LBFGSB"})

        for i in range(3):
            cands = opt.get_next_candidates(2)
            cand_one = cands[0]
            cand_two = cands[1]
            assert_true(isinstance(cand_one, Candidate))
            cand_one.result = 2
            exp.add_finished(cand_one)
            opt.update(exp)
            assert_true(isinstance(cand_two, Candidate))
            cand_two.result = 2
            exp.add_finished(cand_two)
            opt.update(exp)
        cands = opt.get_next_candidates(num_candidates=5)
        assert_equal(len(cands), 5)

        opt.acquisition_function.params["multi_searcher"] = "random_best"
        cands = opt.get_next_candidates(2)

    def test_PoI(self):
        exp = Experiment("test", {"x": MinMaxNumericParamDef(0, 1)})
        opt = BayesianOptimizer(exp, {"initial_random_runs": 3, "acquisition": ProbabilityOfImprovement})
        assert_true(isinstance(opt.acquisition_function, ProbabilityOfImprovement))

        for i in range(3):
            cands = opt.get_next_candidates(2)
            cand_one = cands[0]
            cand_two = cands[1]
            assert_true(isinstance(cand_one, Candidate))
            cand_one.result = 2
            exp.add_finished(cand_one)
            opt.update(exp)
            assert_true(isinstance(cand_two, Candidate))
            cand_two.result = 2
            exp.add_finished(cand_two)
            opt.update(exp)
{'fraction_non_alphanumeric': 0.09142857142857143, 'fraction_numerical': 0.009761904761904762, 'mean_word_length': 2.7342222222222223, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3227980', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1291, 'n_words': 226}