/*
 *  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/ReplacementStrategyOp.hpp
 *  \brief  Definition of the class ReplacementStrategyOp.
 *  \author Christian Gagne
 *  \author Marc Parizeau
 *  $Revision: 1.2 $
 *  $Date: 2004/01/15 21:08:57 $
 */

#ifndef Beagle_ReplacementStrategyOp_hpp
#define Beagle_ReplacementStrategyOp_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/BreederNode.hpp"
#include "beagle/BreederOp.hpp"

namespace Beagle {


/*!
 *  \class ReplacementStrategyOp beagle/ReplacementStrategyOp.hpp "beagle/ReplacementStrategyOp.hpp"
 *  \brief Replacement strategy operator abstract class.
 *  \ingroup ECF
 *  \ingroup Op
 *
 *  The ReplacementStrategyOp class is the entry point the a breeding tree, and have a
 *  breeder root node. Each breeder are hierachically organized into a tree. A breeder receive a
 *  breeding pool to process, and must return from it one bred individual.
 */
class ReplacementStrategyOp : public Operator {

public:

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

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

  /*!
   *  \brief Apply the operation on a deme in the given context.
   *  \param ioDeme Reference to the deme on which the operation takes place.
   *  \param ioContext Evolutionary context of the operation.
   */
  virtual void operate(Deme& ioDeme, Context& ioContext) = 0;

  virtual void initialize(System& ioSystem);
  virtual void postInit(System& ioSystem);
  virtual void readWithMap(XMLNode::Handle& inNode, OperatorMap& inOpMap);
  virtual void write(XMLStreamer& ioStreamer) const;

  /*!
   *  \return Breeder root node.
   */
  inline BreederNode::Handle getRootNode() const
  {
    return mBreederRoot;
  }

  /*!
   *  \brief Set breeder root node.
   *  \param inRootNode Breeder root node.
   */
  inline void setRootNode(BreederNode::Handle inRootNode)
  {
    mBreederRoot = inRootNode;
  }

protected:
  void         buildRoulette(std::vector<double>& outRoulette, Context& ioContext) const;
  unsigned int rollRoulette(const std::vector<double>& inRoulette, Context& ioContext) const;
  
private:
  BreederNode::Handle mBreederRoot;    //!< Breeder root node.

};

}

#endif // Beagle_ReplacementStrategyOp_hpp
