def test_optimization_problem(): parameter1 = Parameter( name="param1", category="categorical", search_space={"values": ["a", "b"]} ) parameter2 = Parameter(name="param2", category="uniform", search_space={"low": 1, "high": 2}) parameters = [parameter1, parameter2] optimization_problem = OptimizationProblem(parameters) observation1 = Observation(sample={"param1": "a", "param2": 1.5}, loss=1.5) optimization_problem.add_observation(observation1) observation2 = Observation(sample={"param1": "b", "param2": 1.8}, loss=1.8) optimization_problem.add_observation(observation2) observation3 = Observation(sample={"param1": "b", "param2": 1.05}, loss=0.1) optimization_problem.add_observation(observation3) assert type(optimization_problem.parameters) == list assert len(optimization_problem.observations) == 3 assert optimization_problem.parameters_name == set(["param1", "param2"]) assert observation1.sample in optimization_problem.samples assert len(optimization_problem.samples) == 3 assert optimization_problem.best_sample == {"param1": "b", "param2": 1.05} assert optimization_problem.sorted_observations[0].sample == {"param1": "b", "param2": 1.05} assert optimization_problem.finite is False assert len(optimization_problem.find_observations({"param1": "b", "param2": 1.05})) == 1 a, b = optimization_problem.observations_quantile(0.5) assert len(a) == 1 assert len(b) == 2 assert optimization_problem.get_best_k_samples(1)[0].sample == {"param1": "b", "param2": 1.05}
def test_parameter_init(): parameter = Parameter("alpha", "uniform", {"high": 5, "low": 0}) assert parameter.name == "alpha" assert parameter.category == "uniform" assert parameter.search_space == {"high": 5, "low": 0, "step": None} assert parameter.check_value(2) is True assert len(parameter.draw(size=10)) == 10 assert parameter.pdf(np.array([2.5])) == np.array([1 / 5]) assert str(parameter) == "alpha"
def test_parameter_from_dict_bad_category(): data = { "name": "alpha", "category": "lol", "search_space": { "high": 5, "low": 0 } } with pytest.raises(ValidationError): Parameter.from_dict(data)
def test_parameter_from_dict(): data = { "name": "alpha", "category": "uniform", "search_space": { "high": 5, "low": 0 } } parameter = Parameter.from_dict(data) assert parameter.name == "alpha" assert parameter.category == "uniform" assert parameter.search_space == {"high": 5, "low": 0, "step": None} assert parameter.check_value(2) is True assert len(parameter.draw(size=10)) == 10 assert parameter.pdf(np.array([2.5])) == np.array([1 / 5])
def test_parameter_from_dict_missing_key(): data = {"name": "alpha", "search_space": {"high": 5, "low": 0}} with pytest.raises(ValidationError): Parameter.from_dict(data)
def test_parameter_from_dict_bad_format(): data = [1, 2] with pytest.raises(ValidationError): Parameter.from_dict(data)