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

#ifndef Beagle_Deme_hpp
#define Beagle_Deme_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/ContainerAllocator.hpp"
#include "beagle/Container.hpp"
#include "beagle/ContainerT.hpp"
#include "beagle/Genotype.hpp"
#include "beagle/Individual.hpp"
#include "beagle/IndividualAlloc.hpp"
#include "beagle/Stats.hpp"
#include "beagle/HallOfFame.hpp"
#include "beagle/DemeAlloc.hpp"
#include "beagle/Logger.hpp"

namespace Beagle {

/*!
 *  \class Deme beagle/Deme.hpp "beagle/Deme.hpp"
 *  \brief An deme is a population of individual, a "hall of fame", and some statistics.
 *  \ingroup ECF
 *  \ingroup Pop
 */
class Deme : public Individual::Bag {

public:

  //! Deme allocator type.
  typedef DemeAlloc
          Alloc;
  //! Deme handle type.
  typedef PointerT<Deme,Individual::Bag::Handle>
          Handle;
  //! Deme mixed bag type.
  typedef ContainerT<Deme,Individual::Bag::Bag>
          Bag;

  explicit Deme(Individual::Alloc::Handle inIndAlloc=NULL);
  explicit Deme(Individual::Alloc::Handle inIndAlloc,
                Stats::Alloc::Handle inStatsAlloc,
                HallOfFame::Alloc::Handle inHOFAlloc,
                size_type inN=0);  
  explicit Deme(Genotype::Alloc::Handle inGenotypeAlloc);
  explicit Deme(Genotype::Alloc::Handle inGenotypeAlloc, Fitness::Alloc::Handle inFitnessAlloc);
           Deme(const Deme& inOriginalDeme);
  virtual ~Deme() { }

  Deme& operator=(const Deme& inOriginalDeme);

  virtual void copyData(const Container& inOrigContainer);
  virtual void readWithContext(XMLNode::Handle& inNode, Context& ioContext);
  virtual void readPopulation(XMLNode::Handle& inNode, Context& ioContext);
  virtual void write(XMLStreamer& ioStreamer) const;
  virtual void writePopulation(XMLStreamer& ioStreamer) const;
  
  /*!
   *  \brief Get the current deme Hall-of-Fame.
   *  \return Constant reference to the Hall-of-Fame.
   */
  inline const HallOfFame& getHallOfFame() const
  {
    return *mHallOfFame;
  }

  /*!
   *  \brief Get the current deme Hall-of-Fame.
   *  \return Constant reference to the Hall-of-Fame.
   */
  inline HallOfFame& getHallOfFame()
  {
    return *mHallOfFame;
  }

  /*!
   *  \brief Get the hall-of-fame allocator.
   *  \return Constant handle to the hall-of-fame allocator.
   */
  inline const HallOfFame::Alloc::Handle getHallOfFameAlloc() const
  {
    return mHOFAlloc;
  }

  /*!
   *  \brief Get the hall-of-fame allocator.
   *  \return Handle to the hall-of-fame allocator.
   */
  inline HallOfFame::Alloc::Handle getHallOfFameAlloc()
  {
    return mHOFAlloc;
  }

  /*!
   *  \brief Get the deme evolution statistics.
   *  \return Constant handle to the deme evolution statistics.
   */
  inline const Stats::Handle getStats() const
  {
    return mStats;
  }

  /*!
   *  \brief Get the deme evolution statistics.
   *  \return Handle to the deme evolution statistics.
   */
  inline Stats::Handle getStats()
  {
    return mStats;
  }

  /*!
   *  \brief Get the statistics allocator.
   *  \return Handle to the evolution statistics allocator.
   */
  inline const Stats::Alloc::Handle getStatsAlloc() const
  {
    return mStatsAlloc;
  }

  /*!
   *  \brief Get the statistics allocator.
   *  \return Handle to the evolution statistics allocator.
   */
  inline Stats::Alloc::Handle getStatsAlloc()
  {
    return mStatsAlloc;
  }

  /*!
   *  \brief Set the hall-of-fame allocator.
   *  \param inHOFAlloc Hall-of-fame allocator to use.
   */
  inline void setHallOfFameAlloc(HallOfFame::Alloc::Handle inHOFAlloc)
  {
    mHOFAlloc = inHOFAlloc;
  }

  /*!
   *  \brief Set the hall-of-fame.
   *  \param inHallOfFame Hall-of-fame to use.
   */
  inline void setHallOfFame(HallOfFame::Handle inHallOfFame)
  {
    mHallOfFame = inHallOfFame;
  }
  
  /*!
   *  \brief Set the statistics allocator.
   *  \param inStatsAlloc Statistics allocator.
   */
  inline void setStatsAlloc(Stats::Alloc::Handle inStatsAlloc)
  {
    mStatsAlloc = inStatsAlloc;
  }
  
  /*!
   *  \brief Set the statistics.
   *  \param inStats Statistics.
   */
  inline void setStats(Stats::Handle inStats)
  {
    mStats = inStats;
  }
  
protected:
  HallOfFame::Alloc::Handle  mHOFAlloc;     //!< Hall-of-fame allocator.
  HallOfFame::Handle         mHallOfFame;   //!< Deme hall-of-fame.
  Stats::Alloc::Handle       mStatsAlloc;   //!< Statistics allocator.
  Stats::Handle              mStats;        //!< Deme statistics.

public:
  virtual void read(XMLNode::Handle&);     // Undefined for Deme!
  
};

}

#endif // Beagle_Deme_hpp
