/*
 *  Open BEAGLE
 *  Copyright (C) 2001-2004 by Christian Gagne and Marc Parizeau
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Contact:
 *  Laboratoire de Vision et Systemes Numeriques
 *  Departement de genie electrique et de genie informatique
 *  Universite Laval, Quebec, Canada, G1V 4J7
 *  http://vision.gel.ulaval.ca
 *
 */

/*!
 *  \file   beagle/NSGA2Op.hpp
 *  \brief  Definition of the class NSGA2Op.
 *  \author Christian Gagne
 *  \author Marc Parizeau
 *  $Revision: 1.3 $
 *  $Date: 2004/01/15 21:08:57 $
 */


#ifndef Beagle_NSGA2Op_hpp
#define Beagle_NSGA2Op_hpp

#include "beagle/config.hpp"
#include "beagle/macros.hpp"
#include "beagle/Object.hpp"
#include "beagle/Pointer.hpp"
#include "beagle/PointerT.hpp"
#include "beagle/Allocator.hpp"
#include "beagle/AllocatorT.hpp"
#include "beagle/AbstractAllocT.hpp"
#include "beagle/ContainerAllocator.hpp"
#include "beagle/Container.hpp"
#include "beagle/ContainerT.hpp"
#include "beagle/Operator.hpp"
#include "beagle/ReplacementStrategyOp.hpp"
#include "beagle/FitnessMultiObj.hpp"

namespace Beagle {

/*!
 *  \class NSGA2Op beagle/NSGA2Op.hpp "beagle/NSGA2Op.hpp"
 *  \brief NSGA2 multiobjective replacement strategy operator class.
 *  \ingroup ECF
 *  \ingroup Op
 *  \ingroup MultiObj
 *
 *  The NSGA2 replacement strategy implement the elitist multiobjective evolutionary algorithm NSGA2
 *  (Non-dominating Sorting Genetic Algorithm) of Deb, Pratap, Agarwal, and Meyarivan.
 *  \par Reference
 *  Kalyanmoy Deb, Amrit Pratab, Sameer Agarwal, and T. Meyarivan, "A Fast and Elitist
 *  Multiobjective Genetic Algorithm: NSGA-II", IEEE transactions on Evolutionary Computations,
 *  vol. 6, no. 2, April 2002, pages 182-197.
 *
 */
class NSGA2Op : public ReplacementStrategyOp {

public:

  //! NSGA2Op allocator type.
  typedef AllocatorT<NSGA2Op,ReplacementStrategyOp::Alloc>
          Alloc;
  //! NSGA2Op handle type.
  typedef PointerT<NSGA2Op,ReplacementStrategyOp::Handle>
          Handle;
  //! NSGA2Op bag type.
  typedef ContainerT<NSGA2Op,ReplacementStrategyOp::Bag>
          Bag;

           NSGA2Op(std::string inName="NSGA2Op");
  virtual ~NSGA2Op() { }

  virtual void operate(Deme& ioDeme, Context& ioContext);
  virtual void postInit(System& ioSystem);

protected:

  void evalCrowdingDistance(std::vector< std::pair<double,unsigned int> >& outDistances,
                            const Individual::Bag& inIndividualPool) const;
                            
  void sortFastND(std::vector< std::vector<unsigned int> >& outParetoFronts,
                  unsigned int inSortStop,
                  const Individual::Bag& inIndividualPool,
                  Context& ioContext) const;

public:

  //! CrowdingPair is used internally in the evalCrowdingDistance method.
  typedef std::pair<unsigned int, FitnessMultiObj::Handle> CrowdingPair;

  //! Used internally to sort solutions over their different objectives.
  class IsLessCrowdingPairPredicate {
  public:
    IsLessCrowdingPairPredicate(unsigned int inSortingObj=0) : mSortingObj(inSortingObj) { }
    ~IsLessCrowdingPairPredicate() { }
    inline bool operator()(const CrowdingPair& inLeftPair, const CrowdingPair& inRightPair) const
    {
      Beagle_AssertM(mSortingObj < inLeftPair.second->size());
      Beagle_AssertM(mSortingObj < inRightPair.second->size());
      return (*inLeftPair.second)[mSortingObj] < (*inRightPair.second)[mSortingObj];
    }
  private:
    unsigned int mSortingObj;
  };
  
};

}

#endif // Beagle_NSGA2Op_hpp


