[shark] 27/58: removed parent population return value of updatePopulation and ported CMSA

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Mar 16 10:05:30 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch master
in repository shark.

commit 269a0a7fda8484db1883c7a74d2cf0853330f58a
Author: Oswin Krause <oswin.krause at di.ku.dk>
Date:   Wed Jan 13 15:29:11 2016 +0100

    removed parent population return value of updatePopulation and ported CMSA
---
 include/shark/Algorithms/DirectSearch/CMA.h  |   2 +-
 include/shark/Algorithms/DirectSearch/CMSA.h | 223 ++++++++++++++-------------
 src/Algorithms/DirectSearch/CMA.cpp          |  12 +-
 src/Algorithms/DirectSearch/CMSA.cpp         | 109 +++++++++----
 4 files changed, 203 insertions(+), 143 deletions(-)

diff --git a/include/shark/Algorithms/DirectSearch/CMA.h b/include/shark/Algorithms/DirectSearch/CMA.h
index c672f4f..9f20c42 100644
--- a/include/shark/Algorithms/DirectSearch/CMA.h
+++ b/include/shark/Algorithms/DirectSearch/CMA.h
@@ -254,7 +254,7 @@ protected:
 	SHARK_EXPORT_SYMBOL std::vector<IndividualType> generateOffspring( ) const;
 
 	/// \brief Updates the strategy parameters based on the supplied offspring population.
-	SHARK_EXPORT_SYMBOL std::vector<IndividualType> updatePopulation( std::vector<IndividualType > const& offspring ) ;
+	SHARK_EXPORT_SYMBOL void updatePopulation( std::vector<IndividualType > const& offspring ) ;
 
 	SHARK_EXPORT_SYMBOL  void doInit(
 		AbstractConstraintHandler<SearchPointType> const* handler,
diff --git a/include/shark/Algorithms/DirectSearch/CMSA.h b/include/shark/Algorithms/DirectSearch/CMSA.h
index b3052f6..7c579ff 100644
--- a/include/shark/Algorithms/DirectSearch/CMSA.h
+++ b/include/shark/Algorithms/DirectSearch/CMSA.h
@@ -51,118 +51,121 @@
 
 
 namespace shark {
+/**
+* \brief Implements the CMSA.
+*
+*  The algorithm is described in
+*
+*  H. G. Beyer, B. Sendhoff (2008). 
+*  Covariance Matrix Adaptation Revisited: The CMSA Evolution Strategy
+*  In Proceedings of the Tenth International Conference on Parallel Problem Solving from Nature
+*  (PPSN X), pp. 123-132, LNCS, Springer-Verlag
+*/
+class CMSA : public AbstractSingleObjectiveOptimizer<RealVector > {
+	/** \cond */
+
+	struct LightChromosome {
+		RealVector step;
+		double sigma;
+	};
+	/** \endcond */
+public:
+
+	/// \brief Default c'tor.
+	CMSA() : m_mu( 100 ), m_lambda( 200 ) {
+		m_features |= REQUIRES_VALUE;
+	}
+
+	/// \brief From INameable: return the class name.
+	std::string name() const
+	{ return "CMSA"; }
+
+	/// \brief Calculates the center of gravity of the given population \f$ \in \mathbb{R}^d\f$.
+	template<typename Container, typename Extractor>
+	RealVector cog( const Container & container, const Extractor & e ) {
+
+		RealVector result( m_numberOfVariables, 0. );
+
+		for( std::size_t j = 0; j < container.size(); j++ )
+			result += 1./m_mu * e( container[j] );
+
+		return result;
+	}
+
+	SHARK_EXPORT_SYMBOL void read( InArchive & archive );
+	SHARK_EXPORT_SYMBOL void write( OutArchive & archive ) const;
+
+	using AbstractSingleObjectiveOptimizer<RealVector >::init;
+	
+	/// \brief Initializes the algorithm for the supplied objective function.
+	SHARK_EXPORT_SYMBOL void init( ObjectiveFunctionType& function, SearchPointType const& p);
+	
 	/**
-	* \brief Implements the CMSA.
-	*
-	*  The algorithm is described in
-	*
-	*  H. G. Beyer, B. Sendhoff (2008). 
-	*  Covariance Matrix Adaptation Revisited: The CMSA Evolution Strategy
-	*  In Proceedings of the Tenth International Conference on Parallel Problem Solving from Nature
-	*  (PPSN X), pp. 123-132, LNCS, Springer-Verlag
+	* \brief Initializes the algorithm for the supplied objective function.
 	*/
-	class CMSA : public AbstractSingleObjectiveOptimizer<RealVector > {
-		/** \cond */
-
-		struct LightChromosome {
-			RealVector step;
-			double sigma;
-		};
-		/** \endcond */
-		/**
-		* \brief Individual type of the CMSA implementation.
-		*/
-		typedef Individual< RealVector, double, LightChromosome > IndividualType;
-
-	public:
-
-		/**
-		* \brief Default c'tor.
-		*/
-		CMSA() : m_mu( 100 ), m_lambda( 200 ) {
-			m_features |= REQUIRES_VALUE;
-		}
-
-		/// \brief From INameable: return the class name.
-		std::string name() const
-		{ return "CMSA"; }
-
-		/**
-		* \brief Calculates the center of gravity of the given population \f$ \in \mathbb{R}^d\f$.
-		*
-		* 
-		*/
-		template<typename Container, typename Extractor>
-		RealVector cog( const Container & container, const Extractor & e ) {
-
-			RealVector result( m_numberOfVariables, 0. );
-
-			for( std::size_t j = 0; j < container.size(); j++ )
-				result += 1./m_mu * e( container[j] );
-
-			return result;
-		}
-
-		SHARK_EXPORT_SYMBOL void read( InArchive & archive );
-		SHARK_EXPORT_SYMBOL void write( OutArchive & archive ) const;
-
-		using AbstractSingleObjectiveOptimizer<RealVector >::init;
-		/**
-		* \brief Initializes the algorithm for the supplied objective function.
-		*/
-		SHARK_EXPORT_SYMBOL void init( ObjectiveFunctionType& function, SearchPointType const& p);
-
-		/**
-		* \brief Executes one iteration of the algorithm.
-		*/
-		SHARK_EXPORT_SYMBOL void step(ObjectiveFunctionType const& function);
-
-		/**
-		* \brief Accesses the size of the parent population.
-		*/
-		std::size_t mu() const {
-			return m_mu;
-		}
-
-		/**
-		* \brief Accesses the size of the parent population, allows for l-value semantics.
-		*/
-		std::size_t & mu() {
-			return m_mu;
-		}
-
-		/**
-		* \brief Accesses the size of the offspring population.
-		*/
-		std::size_t lambda() const {
-			return m_lambda;
-		}
-
-		/**
-		* \brief Accesses the size of the offspring population, allows for l-value semantics.
-		*/
-		std::size_t & lambda() {
-			return m_lambda;
-		}
-	protected:
-		
-		std::size_t m_numberOfVariables; ///< Stores the dimensionality of the search space.
-		std::size_t m_mu; ///< The size of the parent population.
-		std::size_t m_lambda; ///< The size of the offspring population, needs to be larger than mu.
-
-		double m_sigma; ///< The current step size.
-		double m_cSigma; 
-		double m_cC; ///< Constant for adapting the covariance matrix.
-
-		RealVector m_mean; ///< The current cog of the population.
-
-		shark::MultiVariateNormalDistribution m_mutationDistribution; ///< Multi-variate normal mutation distribution.   
-	private:
-		/**
-		* \brief Updates the strategy parameters based on the supplied offspring population.
-		*/
-		SHARK_EXPORT_SYMBOL void updateStrategyParameters( const std::vector< IndividualType > & offspringNew ) ;
-	};
+	SHARK_EXPORT_SYMBOL void init( 
+		ObjectiveFunctionType& function, 
+		SearchPointType const& initialSearchPoint,
+		std::size_t lambda,
+		std::size_t mu,
+		double initialSigma,				       
+		const boost::optional< RealMatrix > & initialCovarianceMatrix = boost::optional< RealMatrix >()
+	);
+
+	/// \brief Executes one iteration of the algorithm.
+	SHARK_EXPORT_SYMBOL void step(ObjectiveFunctionType const& function);
+
+	/// \brief Accesses the size of the parent population.
+	std::size_t mu() const {
+		return m_mu;
+	}
+
+	/// \brief Accesses the size of the parent population, allows for l-value semantics.
+	std::size_t & mu() {
+		return m_mu;
+	}
+
+	/// \brief Accesses the size of the offspring population.
+	std::size_t lambda() const {
+		return m_lambda;
+	}
+
+	/// \brief Accesses the size of the offspring population, allows for l-value semantics.
+	std::size_t & lambda() {
+		return m_lambda;
+	}
+protected:
+	/// \brief The type of individual used by the CMSA
+	typedef Individual< RealVector, double, LightChromosome > IndividualType;
+
+	/// \brief Samples lambda individuals from the search distribution	
+	SHARK_EXPORT_SYMBOL std::vector<IndividualType> generateOffspring( ) const;
+
+	/// \brief Updates the strategy parameters based on the supplied offspring population.
+	SHARK_EXPORT_SYMBOL void updatePopulation( std::vector< IndividualType > const& offspring );
+
+	/// \brief Initializes the internal data structures of the CMSA
+	SHARK_EXPORT_SYMBOL  void doInit(
+		AbstractConstraintHandler<SearchPointType> const* handler,
+		std::vector<SearchPointType> const& points,
+		std::vector<ResultType> const& functionValues,
+		std::size_t lambda,
+		std::size_t mu
+	);
+private:	
+	std::size_t m_numberOfVariables; ///< Stores the dimensionality of the search space.
+	std::size_t m_mu; ///< The size of the parent population.
+	std::size_t m_lambda; ///< The size of the offspring population, needs to be larger than mu.
+
+	double m_initSigma; ///< The initial step size
+	double m_sigma; ///< The current step size.
+	double m_cSigma; 
+	double m_cC; ///< Constant for adapting the covariance matrix.
+
+	RealVector m_mean; ///< The current cog of the population.
+
+	shark::MultiVariateNormalDistribution m_mutationDistribution; ///< Multi-variate normal mutation distribution.   
+};
 }
 
 #endif
diff --git a/src/Algorithms/DirectSearch/CMA.cpp b/src/Algorithms/DirectSearch/CMA.cpp
index 87154b5..ebbfb81 100644
--- a/src/Algorithms/DirectSearch/CMA.cpp
+++ b/src/Algorithms/DirectSearch/CMA.cpp
@@ -279,7 +279,7 @@ std::vector<CMA::IndividualType> CMA::generateOffspring( ) const{
 	return offspring;
 }
 
-std::vector<CMA::IndividualType > CMA::updatePopulation( std::vector<IndividualType> const& offspring ) {
+void CMA::updatePopulation( std::vector<IndividualType> const& offspring ) {
 	std::vector< IndividualType > selectedOffspring( m_mu );
 	ElitistSelection<FitnessExtractor> selection;
 	selection(offspring.begin(),offspring.end(),selectedOffspring.begin(), selectedOffspring.end());
@@ -323,15 +323,15 @@ std::vector<CMA::IndividualType > CMA::updatePopulation( std::vector<IndividualT
 	double ev = m_mutationDistribution.eigenValues()( m_mutationDistribution.eigenValues().size() - 1 );
 	if( m_sigma * std::sqrt( std::fabs( ev ) ) < m_lowerBound )
 		m_sigma = m_lowerBound / std::sqrt( std::fabs( ev ) );
-
-	return selectedOffspring;
+	
+	//store best point
+	m_best.point= selectedOffspring[ 0 ].searchPoint();
+	m_best.value= selectedOffspring[ 0 ].unpenalizedFitness();
 }
 void CMA::step(ObjectiveFunctionType const& function){
 	std::vector<IndividualType> offspring = generateOffspring();
 	PenalizingEvaluator penalizingEvaluator;
 	penalizingEvaluator( function, offspring.begin(), offspring.end() );
-	std::vector<IndividualType> const& parents  = updatePopulation(offspring);
-	m_best.point= parents[ 0 ].searchPoint();
-	m_best.value= parents[ 0 ].unpenalizedFitness();
+	updatePopulation(offspring);
 }
 
diff --git a/src/Algorithms/DirectSearch/CMSA.cpp b/src/Algorithms/DirectSearch/CMSA.cpp
index f4a49f0..9d93839 100644
--- a/src/Algorithms/DirectSearch/CMSA.cpp
+++ b/src/Algorithms/DirectSearch/CMSA.cpp
@@ -38,7 +38,8 @@
  #define SHARK_COMPILE_DLL
 #include <shark/Algorithms/DirectSearch/CMSA.h>
 #include <shark/Algorithms/DirectSearch/Operators/Evaluation/PenalizingEvaluator.h>
-
+#include <shark/Algorithms/DirectSearch/Operators/Selection/ElitistSelection.h>
+#include <shark/Algorithms/DirectSearch/FitnessExtractor.h>
 using namespace shark;
 
 
@@ -60,56 +61,110 @@ namespace{
 }
 
 void CMSA::init( ObjectiveFunctionType & function, SearchPointType const& p) {
+	SIZE_CHECK(p.size() == function.numberOfVariables());
 	checkFeatures(function);
 	function.init();
+	std::vector<RealVector> points(1,p);
+	std::vector<double> functionValues(1,function.eval(p));
+	std::size_t lambda = 4 * p.size();
+	std::size_t mu = lambda / 4;
+	AbstractConstraintHandler<SearchPointType> const* handler = 0;
+	if (function.hasConstraintHandler())
+		handler = &function.getConstraintHandler();
+	doInit(
+		handler,
+		points,
+		functionValues,
+		lambda,
+		mu
+	);
+}
+void CMSA::init( 
+	ObjectiveFunctionType& function, 
+	SearchPointType const& p,
+	std::size_t lambda,
+	std::size_t mu,
+	double initialSigma,				       
+	const boost::optional< RealMatrix > & initialCovarianceMatrix
+) {
 	
-	m_numberOfVariables = p.size();
+	SIZE_CHECK(p.size() == function.numberOfVariables());
+	checkFeatures(function);
+	function.init();
+	std::vector<RealVector> points(1,p);
+	std::vector<double> functionValues(1,function.eval(p));
+	m_initSigma = initialSigma;
+	AbstractConstraintHandler<SearchPointType> const* handler = 0;
+	if (function.hasConstraintHandler())
+		handler = &function.getConstraintHandler();
+	doInit(
+		handler,
+		points,
+		functionValues,
+		lambda,
+		mu
+	);
+	if(initialCovarianceMatrix){
+		m_mutationDistribution.covarianceMatrix() = *initialCovarianceMatrix;
+		m_mutationDistribution.update();
+	}
+}
 
-	m_lambda = 4 * m_numberOfVariables;
-	m_mu = m_lambda / 4;
+void CMSA::doInit( 
+	AbstractConstraintHandler<SearchPointType> const* constraints, 
+	std::vector<SearchPointType> const& initialSearchPoints,
+	std::vector<ResultType> const& initialValues,
+	std::size_t lambda,
+	std::size_t mu
+) {
+	m_numberOfVariables = initialSearchPoints[0].size();
 
-	m_mean = p;
-	m_mutationDistribution.resize( m_numberOfVariables );
+	m_lambda = lambda;
+	m_mu = mu;
 
-	m_sigma = 1.0;
+	m_mutationDistribution.resize( m_numberOfVariables );
+	m_sigma =  (m_initSigma == 0)? 1.0/std::sqrt(double(m_numberOfVariables)): m_initSigma;
 	m_cSigma = 1./::sqrt( 2. * m_numberOfVariables );
 	m_cC = 1. + (m_numberOfVariables*(m_numberOfVariables + 1.))/(2.*m_mu);
+	
+	std::size_t pos = std::min_element(initialValues.begin(),initialValues.end())-initialValues.begin();
+	m_mean = initialSearchPoints[pos];
+	m_best.point = initialSearchPoints[pos];
+	m_best.value = initialValues[pos];
 }
 
 
 void CMSA::step(ObjectiveFunctionType const& function){
-	std::vector< IndividualType > offspring( m_lambda );
-
+	std::vector<IndividualType> offspring = generateOffspring();
 	PenalizingEvaluator penalizingEvaluator;
+	penalizingEvaluator( function, offspring.begin(), offspring.end() );
+	updatePopulation(offspring);
+}
+
+std::vector<CMSA::IndividualType> CMSA::generateOffspring( ) const{
+	std::vector< IndividualType > offspring( m_lambda );
 	for( std::size_t i = 0; i < offspring.size(); i++ ) {		    
 		MultiVariateNormalDistribution::result_type sample = m_mutationDistribution();
 		offspring[i].chromosome().sigma = m_sigma * ::exp( m_cSigma * Rng::gauss( 0, 1 ) );
 		offspring[i].chromosome().step = sample.first;
 		offspring[i].searchPoint() = m_mean + offspring[i].chromosome().sigma * sample.first;
 	}
-	penalizingEvaluator( function, offspring.begin(), offspring.end() );
-
-	// Selection
-	std::sort( offspring.begin(), offspring.end(), FitnessComparator() );
-	std::vector< IndividualType > parentsNew( offspring.begin(), offspring.begin() + m_mu );
-
-	// Strategy parameter update
-	updateStrategyParameters( parentsNew );
-
-	m_best.point = parentsNew.front().searchPoint();
-	m_best.value = parentsNew.front().unpenalizedFitness();
+	return offspring;
 }
-
-void CMSA::updateStrategyParameters( const std::vector< CMSA::IndividualType > & offspringNew ) {
-	RealVector xPrimeNew = cog( offspringNew, PointExtractor() );
+void CMSA::updatePopulation(std::vector< IndividualType > const& offspring ) {
+	std::vector< IndividualType > selectedOffspring( m_mu );
+	ElitistSelection<FitnessExtractor> selection;
+	selection(offspring.begin(),offspring.end(),selectedOffspring.begin(), selectedOffspring.end());
+	
+	RealVector xPrimeNew = cog( selectedOffspring, PointExtractor() );
 	// Covariance Matrix Update
 	RealMatrix Znew( m_numberOfVariables, m_numberOfVariables,0.0 );
 	RealMatrix& C = m_mutationDistribution.covarianceMatrix();
 	// Rank-mu-Update
 	for( std::size_t i = 0; i < m_mu; i++ ) {
 		noalias(Znew) += 1./m_mu * blas::outer_prod( 
-			offspringNew[i].chromosome().step,
-			offspringNew[i].chromosome().step
+			selectedOffspring[i].chromosome().step,
+			selectedOffspring[i].chromosome().step
 		);
 	}
 	noalias(C) = (1. - 1./m_cC) * C + 1./m_cC * Znew;
@@ -119,10 +174,12 @@ void CMSA::updateStrategyParameters( const std::vector< CMSA::IndividualType > &
 	double sigmaNew = 0.;
 	//double sigma = 0.;
 	for( std::size_t i = 0; i < m_mu; i++ ) {
-		sigmaNew += 1./m_mu * offspringNew[i].chromosome().sigma;
+		sigmaNew += 1./m_mu * selectedOffspring[i].chromosome().sigma;
 	}
 	m_sigma = sigmaNew;
 	m_mean = xPrimeNew;
+	m_best.point= selectedOffspring[ 0 ].searchPoint();
+	m_best.value= selectedOffspring[ 0 ].unpenalizedFitness();
 }
 
 void CMSA::read( InArchive & archive ) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/shark.git



More information about the debian-science-commits mailing list