/*---------------------------------------------------------*/
NOMAD::success_type NOMAD::Barrier::insert_infeasible ( const NOMAD::Eval_Point & x )
{
    const NOMAD::Eval_Point * old_bi = get_best_infeasible();
    
    // filter insertion:
    // -----------------
    bool insert;
    filter_insertion ( x , insert );
    
    // filter:
    // -------
    if ( _p.get_barrier_type() == NOMAD::FILTER )
    {
        const NOMAD::Eval_Point * bi = get_best_infeasible();
        if ( !bi )
            return NOMAD::UNSUCCESSFUL;
        if ( old_bi )
        {
            if ( bi->get_h().value() < old_bi->get_h().value() )
                return NOMAD::FULL_SUCCESS;
            if ( insert )
                return NOMAD::PARTIAL_SUCCESS;
            return NOMAD::UNSUCCESSFUL;
        }
        return NOMAD::FULL_SUCCESS;
    }
    
    // progressive barrier:
    // --------------------
    
    // with PEB constraints, we remember all points that we tried to insert:
    if ( _p.get_barrier_type() == NOMAD::PEB_P )
        _peb_lop.push_back ( &x );
    
    // first infeasible successes are considered as partial successes
    // (improving iterations)
    if ( !_ref )
        return NOMAD::PARTIAL_SUCCESS;
    
    double hx = x.get_h().value();
    double fx = x.get_f().value();
    double hr = _ref->get_h().value();
    double fr = _ref->get_f().value();
    
    // failure:
    if ( hx > hr || ( hx == hr && fx >= fr ) )
        return NOMAD::UNSUCCESSFUL;
    
    // partial success:
    if ( fx > fr )
        return NOMAD::PARTIAL_SUCCESS;
    
    // full success:
    return NOMAD::FULL_SUCCESS;
}
/*---------------------------------------------------------*/
void NOMAD::Barrier::insert ( const NOMAD::Eval_Point & x )
{
    // we compare the eval types (_SGTE_ or _TRUTH_) of x and *this:
    if ( x.get_eval_type() != _eval_type )
        throw Barrier::Insert_Error ( "Barrier.cpp" , __LINE__ ,
                                     "insertion of an Eval_Point into the bad Barrier object" );
    
    // basic check:
    if ( !x.is_eval_ok() )
    {
        _one_eval_succ = NOMAD::UNSUCCESSFUL;
        return;
    }
    
    // pre-filter: if tag(x) is already in the pre-filter,
    // then return _UNSUCCESSFUL_:
    size_t size_before = _prefilter.size();
    _prefilter.insert ( x.get_tag() );
    if ( _prefilter.size() == size_before )
    {
        _one_eval_succ = NOMAD::UNSUCCESSFUL;
        return;
    }
    
    // insertion in _all_inserted:
    _all_inserted.push_back ( &x );
    
    // other checks:
    const NOMAD::Double & h = x.get_h();
    if ( !x.is_EB_ok             () ||
        !x.get_f().is_defined   () ||
        !h.is_defined           () ||
        h.value() > _h_max.value()    )
    {
        _one_eval_succ = NOMAD::UNSUCCESSFUL;
        return;
    }
    
    // insert_feasible or insert_infeasible:
    _one_eval_succ = ( x.is_feasible ( _p.get_h_min() ) ) ?
    insert_feasible ( x ) : insert_infeasible(x);
    
    if ( _one_eval_succ > _success )
        _success = _one_eval_succ;
}