/*-------------------------------------------------------------------------------------*/
/*  NOMAD - Nonsmooth Optimization by Mesh Adaptive Direct search - version 3.0.0      */
/*                                                                                     */
/*  Copyright (C) 2001-2008  Mark Abramson        - the Boeing Company, Seattle        */
/*                           Charles Audet        - Ecole Polytechnique, Montreal      */
/*                           Gilles Couture       - Ecole Polytechnique, Montreal      */
/*                           John Dennis          - Rice University, Houston           */
/*                           Sebastien Le Digabel - Ecole Polytechnique, Montreal      */
/*                                                                                     */
/*  funded in part by AFOSR and Exxon Mobil                                            */
/*                                                                                     */
/*  Author: Sebastien Le Digabel                                                       */
/*                                                                                     */
/*  Contact information:                                                               */
/*    Ecole Polytechnique de Montreal - GERAD                                          */
/*    C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada                  */
/*    e-mail: Sebastien.Le.Digabel@gerad.ca                                            */
/*    phone : 1-514-340-6053 #6673                                                     */
/*    fax   : 1-514-340-5665                                                           */
/*                                                                                     */
/*  This program is free software: you can redistribute it and/or modify it under the  */
/*  terms of the GNU General Public License as published by the Free Software          */
/*  Foundation, either version 3 of the License, or (at your option) any later         */
/*  version.                                                                           */
/*                                                                                     */
/*  This program is distributed in the hope that it will be useful, but WITHOUT ANY    */
/*  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A    */
/*  PARTICULAR PURPOSE.  See the GNU General Public License for more details.          */
/*                                                                                     */
/*  You should have received a copy of the GNU General Public License along with this  */
/*  program. If not, see <http://www.gnu.org/licenses/>.                               */
/*                                                                                     */
/*  You can find information on the NOMAD software at www.gerad.ca/nomad               */
/*-------------------------------------------------------------------------------------*/
#ifndef __BARRIER__
#define __BARRIER__

#include "Cache.hpp"
#include "Filter_Point.hpp"

class Barrier : private Uncopyable {

private:

  const Parameters     & _p;                // parameters
  NOMAD::Double          _h_max;            // maximal value for h
  const Eval_Point     * _best_feasible;    // best feasible point
  const Eval_Point     * _ref;              // reference point of the progr. barrier
  std::set<Filter_Point> _filter;           // the filter
  std::set<int>          _prefilter;        // pre-filter based on point tags
  int                    _rho_leaps;        // number of trigger (rho) leaps
                                            //   (progressive barrier)
  const Eval_Point     * _poll_center;      // primary poll center
  const Eval_Point     * _sec_poll_center;  // secondary poll center

  int                    _peb_changes;      // number of constraints that have been changed
                                            // to _PEB_E_

  int                    _peb_filter_reset; // number of times that the filter has been
                                            // reseted following a PEB change of status

  std::list<const Eval_Point *> _peb_lop;   // with PEB constraints, list of all points that
                                            // we tried to insert in filter

  std::ostream         & _out;              // display

  // insertion of a feasible point:
  success_type insert_feasible   ( const Eval_Point & x );

  // insertion of an infeasible point:
  success_type insert_infeasible ( const Eval_Point & x );
 
  // change the value of _h_max:
  void set_h_max ( const NOMAD::Double & h_max );

  // filter insertion:
  void filter_insertion ( const Eval_Point & x , bool & insert );

public:

  // constructor:
  explicit Barrier ( const Parameters & p   ,
		     std::ostream     & out   ) :
    _p               (p                   ) ,
    _h_max           (p.get_h_max_0()     ) ,
    _best_feasible   (NULL                ) ,
    _ref             (NULL                ) ,
    _rho_leaps       (0                   ) ,
    _poll_center     (NULL                ) , 
    _sec_poll_center (NULL                ) ,
    _peb_changes     (0                   ) ,
    _peb_filter_reset(0                   ) ,
    _out             (out                 )   {}

  // destructor:
  ~Barrier ( void ) {}

  // reset the barrier:
  void reset ( void );

  // poll center selection:
  void select_poll_center ( success_type last_it_success );

  // barrier updates:
  bool update ( success_type last_it_success );

  // display:
  void display ( std::ostream & out ) const;
  void display ( void               ) const { display(_out); }

  // GET methods:
  const Eval_Point    * get_best_infeasible ( void ) const;
  const Eval_Point    * get_best_feasible   ( void ) const { return _best_feasible;   }
  const NOMAD::Double & get_h_max           ( void ) const { return _h_max;           }
  const Eval_Point    * get_poll_center     ( void ) const { return _poll_center;     }
  const Eval_Point    * get_sec_poll_center ( void ) const { return _sec_poll_center; }

  // insertion of an Eval_Point in the barrier:
  success_type insert ( const Eval_Point & x );

  // check the PEB constraints to change eventually
  // their status from _PEB_P_ to _PEB_E_:
  void check_PEB_constraints ( const Eval_Point & x , int display_degree );

};

// display:
inline std::ostream & operator << ( std::ostream & out , const Barrier & f ) {
  f.display(out); return out;
}

#endif
