/*
 *  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, G1K 7P4
 *  http://vision.gel.ulaval.ca
 *
 */

/*!
 *  \file   beagle/GA/CrossoverUniformOpT.hpp
 *  \brief  Definition of the class GA::CrossoverUniformOpT.
 *  \author Christian Gagne
 *  \author Marc Parizeau
 *  $Revision: 1.9 $
 *  $Date: 2004/04/25 23:12:19 $
 */

#ifndef Beagle_GA_CrossoverUniformOpT_hpp
#define Beagle_GA_CrossoverUniformOpT_hpp

#include <string>

#include "beagle/config.hpp"
#include "beagle/macros.hpp"
#include "beagle/Object.hpp"
#include "beagle/CrossoverOp.hpp"

namespace Beagle {
namespace GA {

/*!
 *  \class CrossoverUniformOpT beagle/GA/CrossoverUniformOpT.hpp
 *    "beagle/GA/CrossoverUniformOpT.hpp"
 *  \brief GA uniform crossover operator class.
 *  \param T GA representation type used in crossover.
 *  \ingroup GAF
 *
 *  The GA genotype type T must fullfil the std::vector template interface.
 */
template <class T>
class CrossoverUniformOpT : public CrossoverOp {

public:

  //! GA::CrossoverUniformOpT allocator type.
  typedef AllocatorT<CrossoverUniformOpT<T>,CrossoverOp::Alloc>
          Alloc;
  //! GA::CrossoverUniformOpT handle type.
  typedef PointerT<CrossoverUniformOpT<T>,CrossoverOp::Handle>
          Handle;
  //! GA::CrossoverUniformOpT bag type.
  typedef ContainerT<CrossoverUniformOpT<T>,CrossoverOp::Bag>
          Bag;

  explicit CrossoverUniformOpT(std::string inMatingPbName="ga.cxunif.prob",
                               std::string inDistribPbName="ga.cxunif.distribprob",
                               std::string inName="GA-CrossoverUniformOpT");
  virtual ~CrossoverUniformOpT() { }

  virtual void initialize(System& ioSystem);
  virtual bool mate(Individual& ioIndiv1, Context& ioContext1,
                    Individual& ioIndiv2, Context& ioContext2);
  virtual void readWithMap(XMLNode::Handle& inNode, OperatorMap& inOpMap);
  virtual void writeContent(XMLStreamer& ioStreamer) const;
                    
protected:
  Float::Handle mDistribProba;      //!< Distribution probability of the uniform crossover.
  std::string   mDistribProbaName;  //!< Name used in the register for the distribution probability.
                                 
};

}
}


/*!
 *  \brief Construct a GA uniform crossover operator.
 *  \param inMatingPbName Mating probability parameter name.
 *  \param inDistribPbName Distribution probability parameter name.
 *  \param inName Name of the operator.
 */
template <class T>
Beagle::GA::CrossoverUniformOpT<T>::CrossoverUniformOpT(std::string inMatingPbName,
                                                        std::string inDistribPbName,
                                                        std::string inName) :
  Beagle::CrossoverOp(inMatingPbName, inName),
  mDistribProbaName(inDistribPbName)
{ }


/*!
 *  \brief Initialize the GA uniform crossover operator.
 *  \param ioSystem System of the evolution.
 */
template <class T>
void Beagle::GA::CrossoverUniformOpT<T>::initialize(Beagle::System& ioSystem)
{
  CrossoverOp::initialize(ioSystem);

  if(ioSystem.getRegister().isRegistered(mMatingProbaName)) {
    ioSystem.getRegister().deleteEntry(mMatingProbaName);
  }

  if(ioSystem.getRegister().isRegistered(mMatingProbaName)) {
    mMatingProba = castHandleT<Float>(ioSystem.getRegister()[mMatingProbaName]);
  } else {
    mMatingProba = new Float(float(0.3));
    Register::Description lDescription(
      "Individual uniform crossover pb.",
      "Float",
      "0.3",
      "GA uniform crossover probability of a single individual."
    );
    ioSystem.getRegister().addEntry(mMatingProbaName, mMatingProba, lDescription);
  }

  if(ioSystem.getRegister().isRegistered(mDistribProbaName)) {
    mDistribProba = castHandleT<Float>(ioSystem.getRegister()[mDistribProbaName]);
  } else {
    mDistribProba = new Float(float(0.5));
    Register::Description lDescription(
      "Uniform crossover distribution prob.",
      "Float",
      "0.5",
      std::string("Uniform GA crossover distribution probability of a single individual. ")+
      std::string("Value of 0.5 means equal distribution of individuals elements during ")+
      std::string("crossover. Value of 0.25 means that individuals are composed by 25% ")+
      std::string("of elements of one individual, and 75% of the elements of the other mated ")+
      std::string("individuals.")
    );
    ioSystem.getRegister().addEntry(mDistribProbaName, mDistribProba, lDescription);
  }
}


/*!
 *  \brief Mate two GA individuals for uniform crossover.
 *  \param ioIndiv1   First individual to mate.
 *  \param ioContext1 Evolutionary context of the first individual.
 *  \param ioIndiv2   Second individual to mate.
 *  \param ioContext2 Evolutionary context of the second individual.
 *  \return True if the individuals are effectively mated, false if not.
 */
template <class T>
bool Beagle::GA::CrossoverUniformOpT<T>::mate(Beagle::Individual& ioIndiv1,
                                              Beagle::Context&    ioContext1,
                                              Beagle::Individual& ioIndiv2,
                                              Beagle::Context&    ioContext2)
{
  unsigned int lNbGenotypes = minOf<unsigned int>(ioIndiv1.size(), ioIndiv2.size());
  if(lNbGenotypes == 0) return false;

  Beagle_LogDebugM(
    ioContext1.getSystem().getLogger(),
    "crossover", "Beagle::GA::CrossoverUniformOpT",
    std::string("The first individual mated is (before GA uniform crossover): ")+
    ioIndiv1.serialize()
  );
  Beagle_LogDebugM(
    ioContext1.getSystem().getLogger(),
    "crossover", "Beagle::GA::CrossoverUniformOpT",
    std::string("The second individual mated is (before GA uniform crossover): ")+
    ioIndiv2.serialize()
  );
    
  for(unsigned int i=0; i<lNbGenotypes; ++i) {
    typename T::Handle lGenotype1 = castHandleT<T>(ioIndiv1[i]);
    typename T::Handle lGenotype2 = castHandleT<T>(ioIndiv2[i]);
    unsigned int lSize = minOf<unsigned int>(lGenotype1->size(), lGenotype2->size());
    for(unsigned int j=0; j<lSize; ++j) {
      double lRolledPb = ioContext1.getSystem().getRandomizer().rollUniform();
      if(lRolledPb <= mDistribProba->getWrappedValue()) {
        typename T::value_type lTemp = (*lGenotype1)[j];
        (*lGenotype1)[j] = (*lGenotype2)[j];
        (*lGenotype2)[j] = lTemp; 
      }
    }
  }

  Beagle_LogDebugM(
    ioContext1.getSystem().getLogger(),
    "crossover", "Beagle::GA::CrossoverUniformOpT",
    std::string("The first individual mated is (after GA uniform crossover): ")+
    ioIndiv1.serialize()
  );
  Beagle_LogDebugM(
    ioContext1.getSystem().getLogger(),
    "crossover", "Beagle::GA::CrossoverUniformOpT",
    std::string("The second individual mated is (after GA uniform crossover): ")+
    ioIndiv2.serialize()
  );
  
  return true;
}


/*!
 *  \brief Read a uniform crossover operator from XML tree.
 *  \param inNode XML node to use to read crossover operator.
 *  \param inOpMap Operator map to use to read crossover operator.
 */
template <class T>
void Beagle::GA::CrossoverUniformOpT<T>::readWithMap(XMLNode::Handle& inNode, OperatorMap& inOpMap)
{
  if((inNode->getType() != XMLNode::eTag) || (inNode->getTagName() != getName())) {
    std::ostringstream lOSS;
    lOSS << "tag <" << getName() << "> expected!" << std::flush;
    throw Beagle_IOExceptionNodeM(*inNode, lOSS.str());
  }
  std::string lMatingProbaReadName = inNode->getTagAttribute("matingpb");
  if(lMatingProbaReadName.empty() == false) mMatingProbaName = lMatingProbaReadName;
  std::string lDistribProbaReadName = inNode->getTagAttribute("distrpb");
  if(lDistribProbaReadName.empty() == false) mDistribProbaName = lDistribProbaReadName;
  // For backward compatibility, accept ancient name "mutdistribpb"
  std::string lDistribProbaReadNameLegacy = inNode->getTagAttribute("mutdistribpb");
  if(lDistribProbaReadNameLegacy.empty() == false) mDistribProbaName = lDistribProbaReadNameLegacy;
}


/*!
 *  \brief Write uniform crossover operator into XML streamer.
 *  \param ioStreamer XML streamer to write crossover operator into.
 */
template <class T>
void Beagle::GA::CrossoverUniformOpT<T>::writeContent(XMLStreamer& ioStreamer) const
{
  CrossoverOp::writeContent(ioStreamer);
  ioStreamer.insertAttribute("distrpb", mDistribProbaName);
}


#endif // Beagle_GA_CrossoverUniformOpT_hpp
