Example #1
0
def test_record_feedbacks():
    opt = CMAESOptimizer(initial_params=np.zeros(2))
    ctrl = Controller(environment=ObjectiveFunction(),
                      behavior_search=JustOptimizer(opt),
                      record_feedbacks=True, accumulate_feedbacks=False)
    returns = ctrl.learn()
    assert_array_equal(returns, ctrl.feedbacks_)
Example #2
0
def test_missing_behavior_search():
    ctrl = Controller(environment=ObjectiveFunction())
    beh = DummyBehavior(initial_params=np.array([0.0, 0.0]))
    beh.init(0, 2)
    feedback = ctrl.episode_with(beh)
    assert_equal(len(feedback), 1)
    assert_less(feedback[0], ctrl.environment.get_maximum_feedback())
Example #3
0
def test_record_inputs():
    opt = CMAESOptimizer(initial_params=np.zeros(2))
    ctrl = Controller(environment=ObjectiveFunction(),
                      behavior_search=JustOptimizer(opt),
                      record_inputs=True)
    returns = ctrl.learn()
    assert_equal(len(returns), 10)
    assert_equal(np.array(ctrl.inputs_).shape, (10, 1, 2))
Example #4
0
def test_pickle_controller():
    names = ["CMAESOptimizer", "REPSOptimizer"]
    optimizers = [CMAESOptimizer, REPSOptimizer]
    for name, Optimizer in zip(names, optimizers):
        opt = Optimizer(initial_params=np.zeros(2))
        ctrl = Controller(environment=ObjectiveFunction(),
                          behavior_search=JustOptimizer(opt))
        assert_pickle(name, ctrl)
Example #5
0
def test_learn_controller_cmaes_sphere():
    opt = CMAESOptimizer(initial_params=np.zeros(2), random_state=0)
    ctrl = Controller(environment=ObjectiveFunction(random_state=0),
                      behavior_search=JustOptimizer(opt),
                      n_episodes=200)
    returns = ctrl.learn()
    dist_to_maximum = returns.max() - ctrl.environment.get_maximum_feedback()
    assert_greater(dist_to_maximum, -1e-5)
Example #6
0
def test_no_behavior_search_subclass():
    class NoBehaviorSearch(object):
        pass

    assert_raises_regexp(
        TypeError, "requires subclass of 'BehaviorSearch'",
        Controller, environment=ObjectiveFunction(),
        behavior_search=NoBehaviorSearch())
Example #7
0
def test_set_context():
    env = ObjectiveFunction(name="Sphere", n_params=2, random_state=0)
    env.init()

    cenv = ContextualObjectiveFunction(name="LinearContextualSphere",
                                       n_params=2,
                                       n_context_dims=1,
                                       random_state=0)
    env2 = SetContext(cenv, context=np.array([0.0]))
    env2.init()

    assert_equal(env.get_num_inputs(), env2.get_num_inputs())
    assert_equal(env.get_num_outputs(), env2.get_num_outputs())

    assert_almost_equal(env.get_maximum_feedback(),
                        env2.get_maximum_feedback())

    params = np.zeros(2)
    f = np.empty(2)
    for i, e in enumerate([env, env2]):
        e.reset()
        e.set_inputs(params)
        e.step_action()
        out = np.empty(1)
        e.get_outputs(out)
        assert_true(e.is_evaluation_done())
        f[i] = e.get_feedback()[:]
        assert_false(e.is_behavior_learning_done())
    assert_almost_equal(f[0], f[1])
Example #8
0
def test_record_test_results():
    opt = CMAESOptimizer(initial_params=np.zeros(2))
    ctrl = Controller(environment=ObjectiveFunction(),
                      behavior_search=JustOptimizer(opt),
                      n_episodes_before_test=10, n_episodes=100)
    ctrl.learn()
    results = np.array(ctrl.test_results_)
    assert_equal(results.shape[0], 10)
    assert_true(np.all(results[:-1] <= results[1:]))
Example #9
0
def test_controller_cmaes_sphere():
    opt = CMAESOptimizer(initial_params=np.zeros(2))
    ctrl = Controller(environment=ObjectiveFunction(),
                      behavior_search=JustOptimizer(opt))
    returns = ctrl.learn()
    assert_equal(len(returns), 10)
Example #10
0
def test_learning_fails_with_missing_behavior_search():
    controller = Controller(environment=ObjectiveFunction())
    assert_raises_regexp(ValueError, "BehaviorSearch is required",
                         controller.learn)
"""
==================
Objective Function
==================

Plot an artificial objective function that can be used as a benchmark for
black-box optimization.
"""
print(__doc__)

import matplotlib.pyplot as plt
from bolero.environment import ObjectiveFunction

fig = plt.figure()
env = ObjectiveFunction(random_state=0)
env.plot(fig)
plt.show()
Example #12
0
def test_noncontextual_environment():
    assert_raises_regexp(TypeError,
                         "requires contextual environment",
                         ContextualController,
                         environment=ObjectiveFunction())