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

#ifndef Beagle_Evolver_hpp
#define Beagle_Evolver_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/AbstractAllocT.hpp"
#include "beagle/WrapperT.hpp"
#include "beagle/System.hpp"
#include "beagle/Operator.hpp"
#include "beagle/Map.hpp"
#include "beagle/OperatorMap.hpp"
#include "beagle/Vivarium.hpp"
#include "beagle/ConfigurationDumper.hpp"
#include "beagle/IntegerVector.hpp"

namespace Beagle {

/*!
 *  \class Evolver beagle/Evolver.hpp "beagle/Evolver.hpp"
 *  \brief Beagle's basic evolver class.
 *  \ingroup ECF
 *  \ingroup Op
 */
class Evolver : public Object {

public:

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

           Evolver();
  virtual ~Evolver() { }

  void                   addBootStrapOp(std::string inName);
  void                   addMainLoopOp(std::string inName);
  void                   addOperator(Operator::Handle inOperator);
  const Operator::Handle getOperator(std::string inName) const;
  Operator::Handle       getOperator(std::string inName);
  virtual void           initialize(System::Handle ioSystem, int& ioArgc, char** ioArgv);
  virtual void           initialize(System::Handle ioSystem, std::string inConfigFilename);
  virtual void           evolve(Vivarium::Handle ioVivarium);
  virtual void           read(XMLNode::Handle& inNode);
  virtual void           readEvolverFile(std::string inFilename);
  Operator::Handle       removeOperator(std::string inName);
  virtual void           write(XMLStreamer& ioStreamer) const;
  
  /*!
   *  \brief Return a constant reference to the bootstrap operator set.
   *  \return Bootstrap operator set constant reference.
   */
  inline const Operator::Bag& getBootStrapSet() const
  {
    return mBootStrapSet;
  }

  /*!
   *  \brief Return a reference to the bootstrap operator set.
   *  \return Bootstrap operator set reference.
   */
  inline Operator::Bag& getBootStrapSet()
  {
    return mBootStrapSet;
  }

  /*!
   *  \brief Return a constant reference to the main-loop operator set.
   *  \return Main-loop operator set constant reference.
   */
  inline const Operator::Bag& getMainLoopSet() const
  {
    return mMainLoopSet;
  }

  /*!
   *  \brief Return a reference to the main-loop operator set.
   *  \return Main-loop operator set reference.
   */
  inline Operator::Bag& getMainLoopSet()
  {
    return mMainLoopSet;
  }

  /*!
   *  \return Operators map.
   */
  inline OperatorMap& getOperatorMap()
  {
    return mOperatorMap;
  }

  /*!
   *  \return Operators map.
   */
  inline const OperatorMap& getOperatorMap() const
  {
    return mOperatorMap;
  }
      
protected:
  virtual void initializeOperators(System& ioSystem);
  virtual void parseCommandLine(System& ioSystem, int& ioArgc, char** ioArgv);
  virtual void postInitOperators(System& ioSystem);

  System::Handle              mSystemHandle;  //!< Handle to the used system.
  Operator::Bag               mBootStrapSet;  //!< Bootstrap operator set.
  Operator::Bag               mMainLoopSet;   //!< Main-loop operator set.
  OperatorMap                 mOperatorMap;   //!< Operator map.
  ConfigurationDumper::Handle mConfigDumper;  //!< Configuration dumper to file parameter.
  String::Handle              mFileName;      //!< Configuration file name.
  IntegerVector::Handle       mPopSize;       //!< Population size for the evolution.
  
};

}

#endif // Beagle_Evolver_hpp
