Beispiel #1
0
def test_user_function_result_validation():
    # 2d x
    with pytest.raises(ValueError):
        UserFunctionResult(np.array([[1]]), np.array([1]))

    # 2d y
    with pytest.raises(ValueError):
        UserFunctionResult(np.array([1]), np.array([[1]]))

    # 2d cost
    with pytest.raises(ValueError):
        UserFunctionResult(np.array([1]), np.array([1]), cost=np.array([[1]]))
Beispiel #2
0
def mock_user_function():
    user_function_results = [UserFunctionResult(np.array([0]), np.array([0]))]

    user_function = mock.create_autospec(UserFunction)
    user_function.evaluate.return_value = user_function_results

    return user_function
Beispiel #3
0
def test_loop():
    n_iterations = 5
    x_init = np.random.rand(5, 1)
    y_init = np.random.rand(5, 1)
    yc_init = np.random.rand(5, 1)
    x = ContinuousParameter('x', 0, 1)
    bo = UnknownConstraintGPBayesianOptimization(variables_list=[x],
                                                 X=x_init,
                                                 Y=y_init,
                                                 Yc=yc_init,
                                                 batch_size=1)
    results = None
    for _ in range(n_iterations + 1):
        X_new = bo.get_next_points(results)
        Y_new = f(X_new)
        Yc_new = fc(X_new)
        results = [
            UserFunctionResult(X_new[0], Y_new[0], Y_constraint=Yc_new[0])
        ]
    # Check we got the correct number of points
    assert bo.loop_state.X.shape[0] == n_iterations + 5
Beispiel #4
0
 def evaluate(self, X: np.ndarray):
     res = []
     for xi in X:
         yi = self.b.objective_function(xi)["function_value"]
         res.append(UserFunctionResult(xi, np.array([yi])))
     return res
Beispiel #5
0
    X_init = init_design.get_samples(args.n_init)
    Y_init, C_init = evaluate(X_init)

    model = BOGP(X_init=X_init, Y_init=Y_init)

    acquisition = LogExpectedImprovement(model)

    # acquisition_optimizer = DirectOptimizer(space)
    acquisition_optimizer = DifferentialEvolution(space)
    candidate_point_calculator = Sequential(acquisition, acquisition_optimizer)

    bo = BayesianOptimizationLoop(model=model, space=space, X_init=X_init, Y_init=Y_init, acquisition=acquisition,
                                  candidate_point_calculator=candidate_point_calculator)
    initial_results = []
    for i in range(X_init.shape[0]):
        initial_results.append(UserFunctionResult(X_init[i], Y_init[i], C_init[i]))
    loop_state = LoopState(initial_results)
    bo.loop_state = loop_state

    bo.run_loop(user_function=obj,
                stopping_condition=FixedIterationsStoppingCondition(args.num_iterations - args.n_init))
    C = bo.loop_state.C
    incumbents = []
    curr_inc = None
    curr_inc_val = np.inf
    for yi, xi in zip(bo.loop_state.Y, bo.loop_state.X):
        if curr_inc_val > yi[0]:
            curr_inc = xi
            curr_inc_val = yi[0]
        incumbents.append(curr_inc)