Пример #1
0
def ei_evaluate_constraint_only(obj_model, constraint_models, cand, current_best, compute_grad):

    improvement = lambda mean: np.maximum(0.0, current_best-mean)
    improvement_grad = lambda mean_grad: -mean_grad*np.less(mean, current_best).flatten()

    # If unconstrained, just compute the GP mean
    if len(constraint_models) == 0:
        if compute_grad:
            mean, var, grad_mean_x, grad_var_x = obj_model.predict(cand, compute_grad=True)
            return improvement(mean), improvement_grad(grad_mean_x)
        else:
            mean, var = obj_model.predict(cand, compute_grad=False)
            return improvement(mean)

    if cand.ndim == 1:
        cand = cand[None]

    N_cand = cand.shape[0]

    ############## ---------------------------------------- ############
    ##############                                          ############
    ##############   Part that depends on the objective     ############
    ##############                                          ############
    ############## ---------------------------------------- ############
    if current_best is None:
        ei = 1.
        ei_grad = 0.
    else:
        target = current_best

        # Compute the predictive mean and variance
        if not compute_grad:
            mean, var = obj_model.predict(cand, compute_grad=False)
        else:
            mean, var, grad_mean_x, grad_var_x = obj_model.predict(cand, compute_grad=True)
            ei_grad = improvement_grad(grad_mean_x)
        ei = improvement(mean)
    ############## ---------------------------------------- ############
    ##############                                          ############
    ##############  Part that depends on the constraints    ############
    ##############                                          ############
    ############## ---------------------------------------- ############
    # Compute p(valid) for ALL constraints
    if not compute_grad:
        p_valid_prod = total_constraint_confidence(constraint_models, cand, compute_grad=False)
    else:
        p_valid_prod, p_grad_prod = total_constraint_confidence(constraint_models, cand, compute_grad=True)

    ############## ---------------------------------------- ############
    ##############                                          ############
    ##############    Combine the two parts (obj and con)   ############
    ##############                                          ############
    ############## ---------------------------------------- ############

    acq = ei * p_valid_prod

    if not compute_grad:
        return acq
    else:
        return acq, ei_grad * p_valid_prod + p_grad_prod * ei
Пример #2
0
def constraint_weighted_ei(obj_model, constraint_models, cand, current_best,
                           compute_grad):

    numConstraints = len(constraint_models)

    if cand.ndim == 1:
        cand = cand[None]

    N_cand = cand.shape[0]

    ############## ---------------------------------------- ############
    ##############                                          ############
    ##############   Part that depends on the objective     ############
    ##############                                          ############
    ############## ---------------------------------------- ############
    if current_best is None:
        ei = 1.
        ei_grad = 0.
    else:
        target = current_best

        # Compute the predictive mean and variance
        if not compute_grad:
            ei = compute_ei(obj_model, cand, target, compute_grad=compute_grad)
        else:
            ei, ei_grad = compute_ei(obj_model,
                                     cand,
                                     target,
                                     compute_grad=compute_grad)

    ############## ---------------------------------------- ############
    ##############                                          ############
    ##############  Part that depends on the constraints    ############
    ##############                                          ############
    ############## ---------------------------------------- ############
    # Compute p(valid) for ALL constraints
    if not compute_grad:
        p_valid_prod = total_constraint_confidence(constraint_models,
                                                   cand,
                                                   compute_grad=False)
    else:
        p_valid_prod, p_grad_prod = total_constraint_confidence(
            constraint_models, cand, compute_grad=True)

    ############## ---------------------------------------- ############
    ##############                                          ############
    ##############    Combine the two parts (obj and con)   ############
    ##############                                          ############
    ############## ---------------------------------------- ############

    acq = ei * p_valid_prod

    if not compute_grad:
        return acq
    else:
        return acq, ei_grad * p_valid_prod + p_grad_prod * ei