Beispiel #1
0
def test_get_values():
    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]}))

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

    obs_vals = obs.get_values(as_array=True, opposite=False)
    assert [_.to_array() for _ in values] == list(obs_vals)

    obs_vals = obs.get_values(as_array=True, opposite=True)
    assert [-1 * _.to_array() for _ in values] == list(obs_vals)

    obs_vals = obs.get_values(as_array=False, opposite=False)
    assert [_.to_dict() for _ in values] == list(obs_vals)

    obs_vals = obs.get_values(as_array=False, opposite=True)
    obs_dicts = [_.to_dict() for _ in values]
    for _, obs_dict in enumerate(obs_dicts):
        for key, val in obs_dict.items():
            assert val == -1 * obs_vals[_][key]
def test_add_obseration():
    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]}))

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

    assert (
        np.linalg.norm(
            np.array(obs.params) - np.array([param.to_array() for param in params])
        )
        < 1e-7
    )
    assert (
        np.linalg.norm(
            np.array(obs.values) - np.array([value.to_array() for value in values])
        )
        < 1e-7
    )
Beispiel #3
0
def test_planner_ask_tell(two_param_space, stddev):
    planner = Cma(stddev=stddev)
    planner.set_param_space(param_space=two_param_space)
    param = planner.ask()
    value = ParameterVector().from_dict({'objective': 0.})
    obs = Observations()
    obs.add_observation(param, value)
    planner.tell(observations=obs)
Beispiel #4
0
def test_planner_ask_tell(two_param_space, disp, eps, ftol, gtol, maxcor, maxfun, maxiter, maxls):
    planner = DifferentialEvolution()
    planner.set_param_space(param_space=two_param_space)
    param = planner.ask()
    value = ParameterVector().from_dict({'objective': 0.})
    obs = Observations()
    obs.add_observation(param, value)
    planner.tell(observations=obs)
def test_planner_ask_tell(two_param_space, learning_rate, dx, random_seed, init_guess):
    planner = SteepestDescent(learning_rate=1e-3, dx=1e-5, random_seed=None, init_guess=None)
    planner.set_param_space(param_space=two_param_space)
    param = planner.ask()
    value = ParameterVector().from_dict({'objective': 0.})
    obs = Observations()
    obs.add_observation(param, value)
    planner.tell(observations=obs)
Beispiel #6
0
def test_add_obseration():
    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]}))

    obs = Observations()
    obs.add_observation(params, values)
    assert obs.params == params
    assert obs.values == values
def test_planner_ask_tell(two_param_space, goal, rng):
    planner = Smac(goal=goal, rng=rng)
    planner.set_param_space(param_space=two_param_space)
    param = planner.ask()
    value = ParameterVector().from_dict({'objective': 0.})
    obs = Observations()
    obs.add_observation(param, value)
    planner.tell(observations=obs)
Beispiel #8
0
def test_planner_ask_tell(two_param_space, disp, eps, ftol, maxiter):
	planner = Slsqp(disp=disp, eps=eps, ftol=ftol, maxiter=maxiter)
	planner.set_param_space(param_space=two_param_space)
	param = planner.ask()
	value = ParameterVector().from_dict({'objective': 0.})
	obs = Observations()
	obs.add_observation(param, value)
	planner.tell(observations=obs)
Beispiel #9
0
def test_planner_ask_tell(two_param_space, show_progressbar):
    planner = Hyperopt(show_progressbar=show_progressbar)
    planner.set_param_space(param_space=two_param_space)
    param = planner.ask()
    value = ParameterVector().from_dict({'objective': 0.})
    obs = Observations()
    obs.add_observation(param, value)
    planner.tell(observations=obs)
Beispiel #10
0
def test_planner_ask_tell(two_param_space, disp, maxiter, maxfev, initial_simplex, xatol, fatol, adaptive):
	planner = Simplex(disp=disp, maxiter=maxiter, maxfev=maxfev, initial_simplex=initial_simplex, xatol=xatol,
					  fatol=fatol, adaptive=adaptive)
	planner.set_param_space(param_space=two_param_space)
	param = planner.ask()
	value = ParameterVector().from_dict({'objective': 0.})
	obs = Observations()
	obs.add_observation(param, value)
	planner.tell(observations=obs)
Beispiel #11
0
def test_generating_new_offsprings(two_param_space):
    planner = Genetic(pop_size=4)
    planner.set_param_space(param_space=two_param_space)
    obs = Observations()
    for i in range(10):
        param = planner.recommend(observations=obs)
        obj = np.sum(param.to_array()**2)
        value = ParameterVector(dict={'objective': obj})
        obs.add_observation(param, value)
Beispiel #12
0
def test_planner_ask_tell(two_param_space, pop_size, cx_prob, mut_prob, mate_args, mutate_args, select_args):
    planner = Genetic(pop_size=pop_size, cx_prob=cx_prob, mut_prob=mut_prob, mate_args=mate_args, mutate_args=mutate_args,
                   select_args=select_args)
    planner.set_param_space(param_space=two_param_space)
    param = planner.ask()
    value = ParameterVector().from_dict({'objective': 0.})
    obs = Observations()
    obs.add_observation(param, value)
    planner.tell(observations=obs)
def test_planner_ask_tell(two_param_space, batch_size, exact_eval, model_type, acquisition_type):
	planner = Gpyopt(batch_size=batch_size, exact_eval=exact_eval, model_type=model_type,
					 acquisition_type=acquisition_type)
	planner.set_param_space(param_space=two_param_space)
	param = planner.ask()
	value = ParameterVector().from_dict({'objective': 0.})
	obs = Observations()
	obs.add_observation(param, value)
	planner.tell(observations=obs)
    def _ask(self):
        new_params = []
        for param in self._param_space:
            new_param = np.random.uniform(low=param['domain'][0],
                                          high=param['domain'][1])
            new_params.append(new_param)

        param_vector = ParameterVector().from_array(new_params,
                                                    self.param_space)
        return param_vector
Beispiel #15
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_planner_ask_tell(two_param_space, max_iters, options, particles):
    planner = ParticleSwarms(max_iters=max_iters,
                             options=options,
                             particles=particles)
    planner.set_param_space(param_space=two_param_space)
    param = planner.ask()
    value = ParameterVector().from_dict({'objective': 0.})
    obs = Observations()
    obs.add_observation(param, value)
    planner.tell(observations=obs)
def test_custom_planner_class(two_param_space):
    """Tests using a custom planner that is built by inheriting from AbstractPlanner.
    """
    planner = Planner(kind=MyPlannerAlg)
    planner.set_param_space(param_space=two_param_space)
    obs = Observations()
    planner.tell(observations=obs)
    param = planner.ask()
    value = ParameterVector().from_dict({'objective': 0.})
    obs.add_observation(param, value)
    planner.tell(observations=obs)
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
def test_planner_ask_tell(
    two_param_space
):  #, disp, eps, ftol, gtol, maxcor, maxfun, maxiter, maxls):
    #	planner = Lbfgs(disp=disp, eps=eps, ftol=ftol, gtol=gtol, maxcor=maxcor, maxfun=maxfun, maxiter=maxiter, maxls=maxls)
    planner = Sobol()
    planner.set_param_space(param_space=two_param_space)
    param = planner.ask()
    value = ParameterVector().from_dict({'objective': 0.})
    obs = Observations()
    obs.add_observation(param, value)
    planner.tell(observations=obs)
def test_planners_ask_tell(two_param_space, planner_kind):
    """Tests the function Planner
    """
    planner = Planner(kind=planner_kind)
    planner.set_param_space(param_space=two_param_space)
    obs = Observations()
    planner.tell(observations=obs)
    param = planner.ask()
    value = ParameterVector().from_dict({'objective': 0.})
    obs.add_observation(param, value)
    planner.tell(observations=obs)
Beispiel #21
0
def test_planner_ask_tell(two_param_space, batches, boosted, parallel,
                          sampling_strategies):
    planner = Phoenics(batches=batches,
                       boosted=boosted,
                       parallel=parallel,
                       sampling_strategies=sampling_strategies)
    planner.set_param_space(param_space=two_param_space)
    param = planner.ask()
    value = ParameterVector().from_dict({'objective': 0.})
    obs = Observations()
    obs.add_observation(param, value)
    planner.tell(observations=obs)
Beispiel #22
0
def test_planner_ask_tell(two_param_space, disp, maxiter, gtol, norm, eps):
    planner = ConjugateGradient(disp=disp,
                                maxiter=maxiter,
                                gtol=gtol,
                                norm=norm,
                                eps=eps)
    planner.set_param_space(param_space=two_param_space)
    param = planner.ask()
    value = ParameterVector().from_dict({'objective': 0.})
    obs = Observations()
    obs.add_observation(param, value)
    planner.tell(observations=obs)
Beispiel #23
0
def test_planner_ask_tell(two_param_space, levels, budget, shuffle,
                          random_seed):
    planner = Grid(goal='minimize',
                   levels=levels,
                   budget=budget,
                   shuffle=shuffle,
                   random_seed=random_seed)
    planner.set_param_space(param_space=two_param_space)
    param = planner.ask()
    value = ParameterVector(dict={'objective': 0.})
    obs = Observations()
    obs.add_observation(param, value)
    planner.tell(observations=obs)
def test_planners_recommend(two_param_space, planner_kind):
    """Tests the recommend method in all planners
    """
    planner = Planner(kind=planner_kind)
    planner.set_param_space(param_space=two_param_space)
    obs = Observations()

    # special case for LatinHypercube: we need to set the number of iterations in advance!
    num_iter = 3
    if planner_kind == 'LatinHypercube':
        planner.budget = num_iter

    for i in range(num_iter):
        param = planner.recommend(observations=obs)
        value = ParameterVector().from_dict({'objective': 0.})
        obs.add_observation(param, value)