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

/*!
 *  \defgroup Pop Population
 *  \ingroup ECF
 *  \brief Components of the population, part of the generic EC framework.
 *
 *  The population is structured into four layers: a vivarium, the demes, the individuals and the 
 *  genotypes. The first layer, the vivarium, comprises the whole population of a specific evolution, 
 *  that is, an aggregate of one or more demes. A deme is a population that evolves 
 *  in an independent environment. Generally, at each generation, there are some individuals that 
 *  migrate between the demes that compose a vivarium. A deme is implemented in class Beagle::Deme. 
 *  The class provides and implements an interface of standard methods necessary to evolve its own
 *  population. The class Beagle::Vivarium describes a bag of demes, which is itself an bag of 
 *  individuals. Each deme also has a hall-of-fame where the best-of-run individuals are conserved, 
 *  and its statistics.
 *
 *  The next underlying layer, the individual, is defined in class Beagle::Individual. It is an bag 
 *  of genotypes, the parts that compose a genome. The genotypes are tightly related to the 
 *  representation of the individuals for a given EC algorithm. The genotype interface is declared in 
 *  the abstract class Beagle::Genotype.
 *
 */

 
#ifndef Beagle_Vivarium_hpp
#define Beagle_Vivarium_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/Container.hpp"
#include "beagle/ContainerT.hpp"
#include "beagle/ContainerAllocatorT.hpp"
#include "beagle/Deme.hpp"
#include "beagle/DemeAlloc.hpp"
#include "beagle/Stats.hpp"
#include "beagle/VivariumAlloc.hpp"

namespace Beagle {

/*!
 *  \class Vivarium beagle/Vivarium.hpp "beagle/Vivarium.hpp"
 *  \brief A vivarium is a set of demes.
 *  \ingroup ECF
 *  \ingroup Pop
 */
class Vivarium : public Deme::Bag {

public:

  //! Vivarium allocator type.
  typedef VivariumAlloc
          Alloc;
  //! Vivarium handle type.
  typedef PointerT<Vivarium,Deme::Bag::Handle>
          Handle;
  //! Vivarium bag type.
  typedef ContainerT<Vivarium,Deme::Bag::Bag>
          Bag;

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

  Vivarium& operator=(const Vivarium& inOrigVivarium);
  
  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 vivarium evolution statistics.
   *  \return Constant handle to the vivarium evolution statistics.
   */
  inline const Stats::Handle getStats() const
  {
    return mStats;
  }

  /*!
   *  \brief Get the vivarium evolution statistics.
   *  \return Handle to the vivarium 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;   //!< Vivarium's hall-of-fame.
  Stats::Alloc::Handle       mStatsAlloc;   //!< Statistics allocator.
  Stats::Handle              mStats;        //!< Vivarium statistics.

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

}

#endif // Beagle_Vivarium_hpp

