Esempio n. 1
0
def test_resetting_planner(two_param_space):
    planner = Genetic(pop_size=3)
    planner.set_param_space(param_space=two_param_space)
    # run once
    obs = Observations()
    for i in range(5):
        param = planner.recommend(observations=obs)
        obj = np.sum(param.to_array() ** 2)
        value = ParameterVector(dict={'objective': obj})
        obs.add_observation(param, value)

    # run again from scratch
    planner.reset()
    obs = Observations()
    for i in range(5):
        param = planner.recommend(observations=obs)
        obj = np.sum(param.to_array() ** 2)
        value = ParameterVector(dict={'objective': obj})
        obs.add_observation(param, value)
def test_declaration():
    np.random.seed(100691)
    param_vects = np.random.uniform(low=0, high=1, size=(3, 2))
    values_vects = np.random.uniform(low=0, high=1, size=(3, 1))
    params = []
    values = []
    for param_vect, value_vect in zip(param_vects, values_vects):
        params.append(
            ParameterVector().from_dict({"p0": param_vect[0], "p1": param_vect[1]})
        )
        values.append(ParameterVector().from_dict({"obj0": value_vect[0]}))

    observations = Observations()
    observations.add_observation(params, values)

    test_params = [param.to_array() for param in params]
    test_values = [value.to_array() for value in values]

    assert np.linalg.norm(test_params - observations.get_params()) < 1e-7
    assert np.linalg.norm(test_values - observations.get_values()) < 1e-7