⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mutationcmafltvecop.cpp

📁 非常好的进化算法EC 实现平台 可以实现多种算法 GA GP
💻 CPP
字号:
/* *  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/src/MutationCMAFltVecOp.cpp *  \brief  Source code of class GA::MutationCMAFltVecOp. *  \author Christian Gagne *  \author Marc Parizeau *  $Revision: 1.8 $ *  $Date: 2005/10/04 16:25:09 $ */#include "beagle/GA.hpp"#include <cfloat>#include <float.h>using namespace Beagle;/*! *  \brief Construct CMA-ES mutation operator. *  \param inMutationPbName CMA-ES mutation probability parameter name used in register. *  \param inName Name of the CMA-ES operator. */GA::MutationCMAFltVecOp::MutationCMAFltVecOp(Beagle::string inMutationPbName,											 Beagle::string inName) :  MutationOp(inMutationPbName, inName){ }/*! *  \brief Initialize the CMA-ES mutation operator. *  \param ioSystem System of the evolution. */void GA::MutationCMAFltVecOp::initialize(System& ioSystem){  Beagle_StackTraceBeginM();  MutationOp::initialize(ioSystem);  if(ioSystem.getRegister().isRegistered("ga.cmaes.b")) {    mB = castHandleT<Matrix>(ioSystem.getRegister()["ga.cmaes.b"]);  } else {    mB = new Matrix;    Register::Description lDescription(      "CMA-ES B matrix",      "Matrix",      "",      "CMA-ES B matrix containing the covariance matrix eigenvectors."    );    ioSystem.getRegister().addEntry("ga.cmaes.b", mB, lDescription);  }  if(ioSystem.getRegister().isRegistered("ga.cmaes.d")) {    mD = castHandleT<Vector>(ioSystem.getRegister()["ga.cmaes.d"]);  } else {    mD = new Vector;    Register::Description lDescription(      "CMA-ES D vector",      "Vector",      "",      "CMA-ES D vector containing the square root of the covariance matrix eigenvalues."    );    ioSystem.getRegister().addEntry("ga.cmaes.d", mD, lDescription);  }  if(ioSystem.getRegister().isRegistered("ga.cmaes.sigma")) {    mSigma = castHandleT<Double>(ioSystem.getRegister()["ga.cmaes.sigma"]);  } else {    mSigma = new Double(0.5);    Register::Description lDescription(      "CMA-ES sigma value",      "Double",      "0.5",      "CMA-ES sigma value moduling the mutation step size."    );    ioSystem.getRegister().addEntry("ga.cmaes.sigma", mSigma, lDescription);  }  if(ioSystem.getRegister().isRegistered("ga.float.maxvalue")) {    mMaxValue = castHandleT<DoubleArray>(ioSystem.getRegister()["ga.float.maxvalue"]);  } else {    mMaxValue = new DoubleArray(1,DBL_MAX);    std::ostringstream lOSS;    lOSS << "Maximum values assigned to vector's floats. ";    lOSS << "Value can be a scalar, which limit the value for all float ";    lOSS << "vector parameters, or a vector which limit the value for the parameters ";    lOSS << "individually. If the maximum value is smaller than the ";    lOSS << "float vector size, the limit used for the last values of the float vector ";    lOSS << "is equal to the last value of the maximum value vector.";    Register::Description lDescription(      "Maximum vector values",      "DoubleArray",      dbl2str(DBL_MAX),      lOSS.str().c_str()    );    ioSystem.getRegister().addEntry("ga.float.maxvalue", mMaxValue, lDescription);  }  if(ioSystem.getRegister().isRegistered("ga.float.minvalue")) {    mMinValue = castHandleT<DoubleArray>(ioSystem.getRegister()["ga.float.minvalue"]);  } else {    mMinValue = new DoubleArray(1,DBL_MIN);    std::ostringstream lOSS;    lOSS << "Minimum  values assigned to vector's floats. ";    lOSS << "Value can be a scalar, which limit the value for all float ";    lOSS << "vector parameters, or a vector which limit the value for the parameters ";    lOSS << "individually. If the minimum value is smaller than the ";    lOSS << "float vector size, the limit used for the last values of the float vector ";    lOSS << "is equal to the last value of the minimum value vector.";    Register::Description lDescription(      "Minimum values",      "DoubleArray",      dbl2str(DBL_MIN),      lOSS.str().c_str()    );    ioSystem.getRegister().addEntry("ga.float.minvalue", mMinValue, lDescription);  }  Beagle_StackTraceEndM("void GA::MutationCMAFltVecOp::initialize(System& ioSystem)");}/*! *  \brief Apply CMA-ES mutation on a real-valued GA individual. *  \param ioIndividual Real-valued GA individual to mutate. *  \param ioContext Context of the evolution. *  \return True if the individual is effectively mutated, false if not. */bool GA::MutationCMAFltVecOp::mutate(Individual& ioIndividual, Context& ioContext){  Beagle_StackTraceBeginM();  Beagle_LogVerboseM(    ioContext.getSystem().getLogger(),    "mutation", "Beagle::GA::MutationCMAFltVecOp",    string("Applying CMA-ES mutation")  );  if(ioIndividual.size()!=1) throw Beagle_RunTimeExceptionM(    "CMA-ES mutation can't be applied on individuals composed by more than one float vector");  GA::FloatVector::Handle lVector=castHandleT<GA::FloatVector>(ioIndividual[0]);  Beagle_AssertM(lVector->size()==mD->size());  const double lSigma=mSigma->getWrappedValue();  Vector lArz(lVector->size());  for(unsigned int i=0; i<lArz.size(); ++i)    lArz[i]=ioContext.getSystem().getRandomizer().rollGaussian(0.0, lSigma * (*mD)[i]);  Vector lMutFactor;  mB->multiply((Matrix&)lMutFactor, lArz);  Beagle_AssertM(lMutFactor.size()==lVector->size());  for(unsigned int i=0; i<lVector->size(); ++i) {    const double lMaxVal = i<mMaxValue->size() ? (*mMaxValue)[i] : mMaxValue->back();    const double lMinVal = i<mMinValue->size() ? (*mMinValue)[i] : mMinValue->back();    (*lVector)[i] += lMutFactor[i];    if((*lVector)[i] > lMaxVal) (*lVector)[i] = lMaxVal;    if((*lVector)[i] < lMinVal) (*lVector)[i] = lMinVal;  }  Beagle_LogDebugM(    ioContext.getSystem().getLogger(),    "mutation", "Beagle::GA::MutationCMAFltVecOp",    string("Float vector generated by CMA-ES mutation: ")+lVector->serialize()  );  return true;  Beagle_StackTraceEndM("bool GA::MutationCMAFltVecOp::mutate(Individual& ioIndividual, Context& ioContext)");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -