[shark] 26/58: added prototype for the new internal interface (compatible with project partners)

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 66b3ed022c936f2a8c73c2218b2b4c4076d6e078
Author: Oswin Krause <oswin.krause at di.ku.dk>
Date:   Wed Jan 13 12:42:16 2016 +0100

    added prototype for the new internal interface (compatible with project partners)
---
 include/shark/Algorithms/DirectSearch/CMA.h | 456 +++++++++++++++-------------
 src/Algorithms/DirectSearch/CMA.cpp         | 118 ++++---
 2 files changed, 308 insertions(+), 266 deletions(-)

diff --git a/include/shark/Algorithms/DirectSearch/CMA.h b/include/shark/Algorithms/DirectSearch/CMA.h
index 41b7583..c672f4f 100644
--- a/include/shark/Algorithms/DirectSearch/CMA.h
+++ b/include/shark/Algorithms/DirectSearch/CMA.h
@@ -44,236 +44,254 @@
 
 
 namespace shark {
+/**
+* \brief Implements the CMA-ES.
+*
+*  The algorithm is described in
+*
+*  Hansen, N., S. Kern (2004). Evaluating the CMA Evolution Strategy
+*  on Multimodal Test Functions. In Proceedings of the Eighth
+*  International Conference on Parallel Problem Solving from Nature
+*  (PPSN VIII), pp. 282-291, LNCS, Springer-Verlag
+*/
+class CMA : public AbstractSingleObjectiveOptimizer<RealVector >
+{
+public:
 	/**
-	* \brief Implements the CMA-ES.
-	*
-	*  The algorithm is described in
+	* \brief Models the recombination type.
+	*/
+	enum RecombinationType {
+		EQUAL = 0,
+		LINEAR = 1,
+		SUPERLINEAR = 2
+	};
+
+	/**
+	* \brief Default c'tor.
+	*/
+	SHARK_EXPORT_SYMBOL CMA();
+
+	/// \brief From INameable: return the class name.
+	std::string name() const
+	{ return "CMA-ES"; }
+
+	/**
+	* \brief Calculates the center of gravity of the given population \f$ \in \mathbb{R}^d\f$.
 	*
-	*  Hansen, N., S. Kern (2004). Evaluating the CMA Evolution Strategy
-	*  on Multimodal Test Functions. In Proceedings of the Eighth
-	*  International Conference on Parallel Problem Solving from Nature
-	*  (PPSN VIII), pp. 282-291, LNCS, Springer-Verlag
+	* 
+	*/
+	template<typename Container, typename Extractor>
+	RealVector weightedSum( const Container & container, const RealVector & weights, const Extractor & e ) {
+
+		RealVector result( m_numberOfVariables, 0. );
+
+		for( std::size_t j = 0; j < container.size(); j++ )
+			result += weights( j ) * e( container[j] );
+
+		return( result );
+	}
+
+	/**
+	* \brief Calculates lambda for the supplied dimensionality n.
+	*/
+	SHARK_EXPORT_SYMBOL static std::size_t suggestLambda( std::size_t dimension ) ;
+
+	/**
+	* \brief Calculates mu for the supplied lambda and the recombination strategy.
+	*/
+	SHARK_EXPORT_SYMBOL static std::size_t suggestMu( std::size_t lambda, RecombinationType recomb = SUPERLINEAR ) ;
+
+	void read( InArchive & archive );
+	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 Initializes the algorithm for the supplied objective function.
+	*/
+	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.
 	*/
-	class CMA : public AbstractSingleObjectiveOptimizer<RealVector >
-	{
-	public:
-		/**
-		* \brief Models the recombination type.
-		*/
-		enum RecombinationType {
-			EQUAL = 0,
-			LINEAR = 1,
-			SUPERLINEAR = 2
-		};
-
-		/**
-		* \brief Default c'tor.
-		*/
-		SHARK_EXPORT_SYMBOL CMA();
-
-		/// \brief From INameable: return the class name.
-		std::string name() const
-		{ return "CMA-ES"; }
-
-		/**
-		* \brief Calculates the center of gravity of the given population \f$ \in \mathbb{R}^d\f$.
-		*
-		* 
-		*/
-		template<typename Container, typename Extractor>
-		RealVector weightedSum( const Container & container, const RealVector & weights, const Extractor & e ) {
-
-			RealVector result( m_numberOfVariables, 0. );
-
-			for( std::size_t j = 0; j < container.size(); j++ )
-				result += weights( j ) * e( container[j] );
-
-			return( result );
-		}
-
-		/**
-		* \brief Calculates lambda for the supplied dimensionality n.
-		*/
-		SHARK_EXPORT_SYMBOL static std::size_t suggestLambda( std::size_t dimension ) ;
-
-		/**
-		* \brief Calculates mu for the supplied lambda and the recombination strategy.
-		*/
-		SHARK_EXPORT_SYMBOL static std::size_t suggestMu( std::size_t lambda, RecombinationType recomb = SUPERLINEAR ) ;
-
-		void read( InArchive & archive );
-		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 Initializes the algorithm for the supplied objective function.
-		*/
-		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 current step size. */
-		double sigma() const {
-			return m_sigma;
-		}
-
-		/** \brief Accesses the current step size. */
-		void setSigma(double sigma) {
-			m_sigma = sigma;
-		}
-
-
-		/** \brief Accesses the current population mean. */
-		RealVector const& mean() const {
-			return m_mean;
-		}
-
-		/** \brief Accesses the current weighting vector. */
-		RealVector const& weights() const {
-			return m_weights;
-		}
-
-		/** \brief Accesses the evolution path for the covariance matrix update. */
-		RealVector const& evolutionPath() const {
-			return m_evolutionPathC;
-		}
-
-		/** \brief Accesses the evolution path for the step size update. */
-		RealVector const& evolutionPathSigma() const {
-			return m_evolutionPathSigma;
-		}
-
-		/** \brief Accesses the covariance matrix of the normal distribution used for generating offspring individuals. */
-		RealMatrix const& covarianceMatrix() const {
-			return m_mutationDistribution.covarianceMatrix();
-		}
-
-		/** 
-		 * \brief Accesses the recombination type.
-		 */
-		RecombinationType recombinationType() const {
-			return m_recombinationType;
-		}
-
-		/** 
-		 * \brief Returns a mutable reference to the recombination type.
-		 */
-		RecombinationType & recombinationType() {
-			return m_recombinationType;
-		}
-
-		/**
-		 * \brief Returns a const reference tothe lower bound on sigma times smalles eigenvalue.
-		 */
-		const double & lowerBound() const {
-			return m_lowerBound;
-		}
-
-		/**
-		 * \brief Returns a mutable reference to the lower bound on sigma times smalles eigenvalue.
-		 */
-		double& lowerBound() {
-			return m_lowerBound;
-		}
-
-		/**
-		 * \brief Returns the size of the parent population \f$\mu\f$.
-		 */
-		std::size_t mu() const {
-			return m_mu;
-		}
-		
-		/**
-		 * \brief Returns a mutabl rference to the size of the parent population \f$\mu\f$.
-		 */
-		std::size_t& mu(){
-			return m_mu;
-		}
-		
-		/**
-		 * \brief Returns a immutable reference to the size of the offspring population \f$\mu\f$.
-		 */
-		std::size_t lambda()const{
-			return m_lambda;
-		}
-
-		/**
-		 * \brief Returns a mutable reference to the size of the offspring population \f$\mu\f$.
-		 */
-		std::size_t & lambda(){
-			return m_lambda;
-		}
-
-		/**
-		 * \brief Returns eigenvectors of covariance matrix (not considering step size)
-		 */
-		RealMatrix const& eigenVectors() const {
-			return m_mutationDistribution.eigenVectors();
-		}
-
-		/**
-		 * \brief Returns a eigenvectors of covariance matrix (not considering step size)
-		 */
-		RealVector const& eigenValues() const {
-			return m_mutationDistribution.eigenValues();
-		}
-
-		/**
-		 * \brief Returns condition of covariance matrix
-		 */
-		double condition() const {
-			RealVector const& eigenValues = m_mutationDistribution.eigenValues();
-			return max(eigenValues)/min(eigenValues); 
-		}
-
-
-	protected:
-		/**
-		* \brief Updates the strategy parameters based on the supplied offspring population.
-		*/
-		SHARK_EXPORT_SYMBOL void updateStrategyParameters( const std::vector<Individual<RealVector, double, RealVector> > & offspring ) ;
+	SHARK_EXPORT_SYMBOL void step(ObjectiveFunctionType const& function);
+
+	/** \brief Accesses the current step size. */
+	double sigma() const {
+		return m_sigma;
+	}
+
+	/** \brief Accesses the current step size. */
+	void setSigma(double sigma) {
+		m_sigma = sigma;
+	}
 	
-		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.
+	void setInitSigma(double initSigma){
+		m_initSigma =initSigma;
+	}
+
+
+	/** \brief Accesses the current population mean. */
+	RealVector const& mean() const {
+		return m_mean;
+	}
+
+	/** \brief Accesses the current weighting vector. */
+	RealVector const& weights() const {
+		return m_weights;
+	}
+
+	/** \brief Accesses the evolution path for the covariance matrix update. */
+	RealVector const& evolutionPath() const {
+		return m_evolutionPathC;
+	}
+
+	/** \brief Accesses the evolution path for the step size update. */
+	RealVector const& evolutionPathSigma() const {
+		return m_evolutionPathSigma;
+	}
+
+	/** \brief Accesses the covariance matrix of the normal distribution used for generating offspring individuals. */
+	RealMatrix const& covarianceMatrix() const {
+		return m_mutationDistribution.covarianceMatrix();
+	}
+
+	/** 
+	 * \brief Accesses the recombination type.
+	 */
+	RecombinationType recombinationType() const {
+		return m_recombinationType;
+	}
+
+	/** 
+	 * \brief Returns a mutable reference to the recombination type.
+	 */
+	RecombinationType & recombinationType() {
+		return m_recombinationType;
+	}
 
-		RecombinationType m_recombinationType; ///< Stores the recombination type.
+	/**
+	 * \brief Returns a const reference tothe lower bound on sigma times smalles eigenvalue.
+	 */
+	const double & lowerBound() const {
+		return m_lowerBound;
+	}
 
-		double m_sigma;
-		double m_cC; 
-		double m_c1; 
-		double m_cMu; 
-		double m_cSigma;
-		double m_dSigma;
-		double m_muEff;
+	/**
+	 * \brief Returns a mutable reference to the lower bound on sigma times smalles eigenvalue.
+	 */
+	double& lowerBound() {
+		return m_lowerBound;
+	}
 
-		double m_lowerBound;
+	/**
+	 * \brief Returns the size of the parent population \f$\mu\f$.
+	 */
+	std::size_t mu() const {
+		return m_mu;
+	}
+	
+	/**
+	 * \brief Returns a mutabl rference to the size of the parent population \f$\mu\f$.
+	 */
+	std::size_t& mu(){
+		return m_mu;
+	}
+	
+	/**
+	 * \brief Returns a immutable reference to the size of the offspring population \f$\mu\f$.
+	 */
+	std::size_t lambda()const{
+		return m_lambda;
+	}
 
-		RealVector m_mean;
-		RealVector m_weights;
+	/**
+	 * \brief Returns a mutable reference to the size of the offspring population \f$\mu\f$.
+	 */
+	std::size_t & lambda(){
+		return m_lambda;
+	}
 
-		RealVector m_evolutionPathC;
-		RealVector m_evolutionPathSigma;
+	/**
+	 * \brief Returns eigenvectors of covariance matrix (not considering step size)
+	 */
+	RealMatrix const& eigenVectors() const {
+		return m_mutationDistribution.eigenVectors();
+	}
 
-		std::size_t m_counter; ///< counter for generations
+	/**
+	 * \brief Returns a eigenvectors of covariance matrix (not considering step size)
+	 */
+	RealVector const& eigenValues() const {
+		return m_mutationDistribution.eigenValues();
+	}
 
-		MultiVariateNormalDistribution m_mutationDistribution;
-	};
+	/**
+	 * \brief Returns condition of covariance matrix
+	 */
+	double condition() const {
+		RealVector const& eigenValues = m_mutationDistribution.eigenValues();
+		return max(eigenValues)/min(eigenValues); 
+	}
+
+
+protected:
+	/// \brief The type of individual used for the CMA
+	typedef Individual<RealVector, double, RealVector> 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 std::vector<IndividualType> updatePopulation( std::vector<IndividualType > const& offspring ) ;
+
+	SHARK_EXPORT_SYMBOL  void doInit(
+		AbstractConstraintHandler<SearchPointType> const* handler,
+		std::vector<SearchPointType> const& points,
+		std::vector<ResultType> const&,
+		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.
+
+	RecombinationType m_recombinationType; ///< Stores the recombination type.
+
+	double m_initSigma;
+	double m_sigma;
+	double m_cC; 
+	double m_c1; 
+	double m_cMu; 
+	double m_cSigma;
+	double m_dSigma;
+	double m_muEff;
+
+	double m_lowerBound;
+
+	RealVector m_mean;
+	RealVector m_weights;
+
+	RealVector m_evolutionPathC;
+	RealVector m_evolutionPathSigma;
+
+	std::size_t m_counter; ///< counter for generations
+
+	MultiVariateNormalDistribution m_mutationDistribution;
+};
 }
 
 #endif
diff --git a/src/Algorithms/DirectSearch/CMA.cpp b/src/Algorithms/DirectSearch/CMA.cpp
index a16fddb..87154b5 100644
--- a/src/Algorithms/DirectSearch/CMA.cpp
+++ b/src/Algorithms/DirectSearch/CMA.cpp
@@ -91,6 +91,7 @@ std::size_t CMA::suggestMu( std::size_t lambda, RecombinationType recomb) {
 
 CMA::CMA()
 :m_recombinationType( SUPERLINEAR )
+, m_initSigma( 0 )
 , m_sigma( 0 )
 , m_cC( 0 )
 , m_c1( 0 )
@@ -160,14 +161,22 @@ void CMA::write( OutArchive & archive ) const {
 
 
 void CMA::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 = CMA::suggestLambda( p.size() );
 	std::size_t mu = CMA::suggestMu(lambda, m_recombinationType);
-	init( function,
-		p,
+	AbstractConstraintHandler<SearchPointType> const* handler = 0;
+	if (function.hasConstraintHandler())
+		handler = &function.getConstraintHandler();
+	doInit(
+		handler,
+		points,
+		functionValues,
 		lambda,
-		mu,
-		1.0/std::sqrt(double(p.size()))
+		mu
 	);
 }
 
@@ -176,19 +185,44 @@ void CMA::init( ObjectiveFunctionType & function, SearchPointType const& p) {
 */
 void CMA::init( 
 	ObjectiveFunctionType& function, 
-	SearchPointType const& initialSearchPoint,
+	SearchPointType const& p,
 	std::size_t lambda,
 	std::size_t mu,
 	double initialSigma,				       
 	const boost::optional< RealMatrix > & initialCovarianceMatrix
 ) {
+	SIZE_CHECK(p.size() == function.numberOfVariables());
 	checkFeatures(function);
 	function.init();
-	
-	m_numberOfVariables = function.numberOfVariables();
+	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();
+	}
+}
+void CMA::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_lambda = lambda;
 	m_mu = mu;
-	m_sigma = initialSigma;
+	m_sigma =  (m_initSigma == 0)? 1.0/std::sqrt(double(m_numberOfVariables)): m_initSigma;
 
 	m_mean.resize( m_numberOfVariables );
 	m_evolutionPathC.resize( m_numberOfVariables );
@@ -197,10 +231,7 @@ void CMA::init(
 	m_mean.clear();
 	m_evolutionPathC.clear();
 	m_evolutionPathSigma.clear();
-	if(initialCovarianceMatrix){
-		m_mutationDistribution.covarianceMatrix() = *initialCovarianceMatrix;
-		m_mutationDistribution.update();
-	}
+	
 		
 	//weighting of the k-best individuals
 	m_weights.resize(m_mu);
@@ -230,20 +261,31 @@ void CMA::init(
 	double alphaMu = 2.;
 	m_cMu = std::min(1. - m_c1, alphaMu * (m_muEff - 2. + 1./m_muEff) / (sqr(m_numberOfVariables + 2) + alphaMu * m_muEff / 2)); // eq. (49)
 
-	m_mean = initialSearchPoint;
-	m_best.point = initialSearchPoint;
-	m_best.value = function(initialSearchPoint);
-
+	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];
 	m_lowerBound = 1E-20;
 	m_counter = 0;
 }
 
-/**
-* \brief Updates the strategy parameters based on the supplied offspring population.
-*/
-void CMA::updateStrategyParameters( const std::vector<Individual<RealVector, double, RealVector> > & offspring ) {
-	RealVector z = weightedSum( offspring, m_weights, StepExtractor() ); // eq. (38)
-	RealVector m = weightedSum( offspring, m_weights, PointExtractor() ); // eq. (39) 
+std::vector<CMA::IndividualType> CMA::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() = sample.second;
+		offspring[i].searchPoint() = m_mean + m_sigma * sample.first;
+	}
+	return offspring;
+}
+
+std::vector<CMA::IndividualType > 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());
+	m_counter++;
+	RealVector z = weightedSum( selectedOffspring, m_weights, StepExtractor() ); // eq. (38)
+	RealVector m = weightedSum( selectedOffspring, m_weights, PointExtractor() ); // eq. (39) 
 	RealVector y = (m - m_mean) / m_sigma;
 
 	// Covariance matrix update
@@ -251,8 +293,8 @@ void CMA::updateStrategyParameters( const std::vector<Individual<RealVector, dou
 	RealMatrix Z( m_numberOfVariables, m_numberOfVariables, 0.0); // matric for rank-mu update
 	for( std::size_t i = 0; i < m_mu; i++ ) {
 		noalias(Z) += m_weights( i ) * blas::outer_prod(
-			offspring[i].searchPoint() - m_mean,
-			offspring[i].searchPoint() - m_mean
+			selectedOffspring[i].searchPoint() - m_mean,
+			selectedOffspring[i].searchPoint() - m_mean
 		);
 	}
 	double expectedChi = chi((std::size_t)m_numberOfVariables);
@@ -282,32 +324,14 @@ void CMA::updateStrategyParameters( const std::vector<Individual<RealVector, dou
 	if( m_sigma * std::sqrt( std::fabs( ev ) ) < m_lowerBound )
 		m_sigma = m_lowerBound / std::sqrt( std::fabs( ev ) );
 
-	
+	return selectedOffspring;
 }
-
-/**
-* \brief Executes one iteration of the algorithm.
-*/
 void CMA::step(ObjectiveFunctionType const& function){
-
-	std::vector< Individual<RealVector, double, RealVector> > offspring( m_lambda );
-
+	std::vector<IndividualType> offspring = generateOffspring();
 	PenalizingEvaluator penalizingEvaluator;
-	for( std::size_t i = 0; i < offspring.size(); i++ ) {
-		MultiVariateNormalDistribution::result_type sample = m_mutationDistribution();
-		offspring[i].chromosome() = sample.second;
-		offspring[i].searchPoint() = m_mean + m_sigma * sample.first;
-	}
 	penalizingEvaluator( function, offspring.begin(), offspring.end() );
-
-	// Selection
-	std::vector< Individual<RealVector, double, RealVector> > parents( m_mu );
-	ElitistSelection<FitnessExtractor> selection;
-	selection(offspring.begin(),offspring.end(),parents.begin(), parents.end());
-	// Strategy parameter update
-	m_counter++; // increase generation counter
-	updateStrategyParameters( parents );
-
+	std::vector<IndividualType> const& parents  = updatePopulation(offspring);
 	m_best.point= parents[ 0 ].searchPoint();
 	m_best.value= parents[ 0 ].unpenalizedFitness();
 }
+

-- 
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