[shark] 41/58: ported RealCodedNSGAII and fixed some smaller issues

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Mar 16 10:05:32 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 d40a416ea7bff95f2114cb4cf78522ff7cbe37ce
Author: Oswin Krause <oswin.krause at di.ku.dk>
Date:   Tue Feb 9 10:57:40 2016 +0100

    ported RealCodedNSGAII and fixed some smaller issues
---
 Test/Algorithms/DirectSearch/RealCodedNSGAII.cpp   |   2 +-
 include/shark/Algorithms/DirectSearch/MOCMA.h      |   5 +-
 .../Algorithms/DirectSearch/RealCodedNSGAII.h      | 185 +++++++++++++--------
 include/shark/Algorithms/DirectSearch/SMS-EMOA.h   |  20 +--
 4 files changed, 129 insertions(+), 83 deletions(-)

diff --git a/Test/Algorithms/DirectSearch/RealCodedNSGAII.cpp b/Test/Algorithms/DirectSearch/RealCodedNSGAII.cpp
index 9bc6fd2..1065d84 100644
--- a/Test/Algorithms/DirectSearch/RealCodedNSGAII.cpp
+++ b/Test/Algorithms/DirectSearch/RealCodedNSGAII.cpp
@@ -63,7 +63,7 @@ BOOST_AUTO_TEST_CASE( RealCodedNSGAII_HYPERVOLUME_Functions ) {
 	testObjectiveFunctionMOO(dtlz2,10,dtlz2Volume,1000,reference);
 	DTLZ4 dtlz4(5);
 	double dtlz4Volume = 120.178966;
-	testObjectiveFunctionMOO(dtlz4,10,dtlz4Volume,5000,reference);
+	testObjectiveFunctionMOO(dtlz4,10,dtlz4Volume,1000,reference);
 	//~ DTLZ7 dtlz7(5); //not sure whether correctly implemented
 	//~ double dtlz7Volume = 115.964708;
 	//~ testObjectiveFunctionMOO(dtlz7,10,dtlz7Volume,10000,reference);
diff --git a/include/shark/Algorithms/DirectSearch/MOCMA.h b/include/shark/Algorithms/DirectSearch/MOCMA.h
index 93cfd25..1a32863 100644
--- a/include/shark/Algorithms/DirectSearch/MOCMA.h
+++ b/include/shark/Algorithms/DirectSearch/MOCMA.h
@@ -147,7 +147,7 @@ public:
 		std::vector<RealVector> values(startingPoints.size());
 		for(std::size_t i = 0; i != startingPoints.size(); ++i){
 			if(!function.isFeasible(startingPoints[i]))
-				throw SHARKEXCEPTION("[SteadyStateMOCMA::init] starting point(s) not feasible");
+				throw SHARKEXCEPTION("[MOCMA::init] starting point(s) not feasible");
 			values[i] = function.eval(startingPoints[i]);
 		}
 		this->doInit(startingPoints,values,mu(),initialSigma() );
@@ -219,7 +219,6 @@ protected:
 	}
 	
 	void updatePopulation(  std::vector<IndividualType> const& offspringVec) {
-		m_parents.push_back(offspringVec[0]);
 		m_parents.insert(m_parents.end(),offspringVec.begin(),offspringVec.end());
 		m_selection( m_parents, mu());
 		
@@ -241,7 +240,7 @@ protected:
 		}
 		
 		//partition the selected individuals to the front and remove the unselected ones
-		std::partition(m_parents.begin(), m_parents.end(),CMAIndividual<RealVector>::IsSelected);
+		std::partition(m_parents.begin(), m_parents.end(),IndividualType::IsSelected);
 		m_parents.erase(m_parents.begin()+mu(),m_parents.end());
 
 		//update solution set
diff --git a/include/shark/Algorithms/DirectSearch/RealCodedNSGAII.h b/include/shark/Algorithms/DirectSearch/RealCodedNSGAII.h
index e27804a..3bf8fea 100644
--- a/include/shark/Algorithms/DirectSearch/RealCodedNSGAII.h
+++ b/include/shark/Algorithms/DirectSearch/RealCodedNSGAII.h
@@ -57,23 +57,7 @@ namespace shark {
 *  IEEE TRANSACTIONS ON EVOLUTIONARY COMPUTATION, VOL. 6, NO. 2, APRIL 2002
 */
 template<typename Indicator>
-class IndicatorBasedRealCodedNSGAII : public AbstractMultiObjectiveOptimizer<RealVector >{
-private:
-	/**
-	* \brief The individual type of the NSGA-II.
-	*/
-	typedef shark::Individual<RealVector,RealVector> Individual;
-
-	std::vector<Individual> m_pop; ///< Population of size \f$\mu + 1\f$.
-	std::size_t m_mu; ///< Size of parent generation
-
-	IndicatorBasedSelection<Indicator> m_selection; ///< Selection operator relying on the (contributing) hypervolume indicator.
-
-	PenalizingEvaluator m_evaluator; ///< Evaluation operator. 
-	SimulatedBinaryCrossover< RealVector > m_crossover; ///< Crossover operator.
-	PolynomialMutator m_mutator; ///< Mutation operator.
-
-	double m_crossoverProbability; ///< Crossover probability.			
+class IndicatorBasedRealCodedNSGAII : public AbstractMultiObjectiveOptimizer<RealVector >{		
 public:
 
 	/**
@@ -81,7 +65,7 @@ public:
 	*/
 	IndicatorBasedRealCodedNSGAII(){
 		mu() = 100;
-		crossoverProbability() = 0.8;
+		crossoverProbability() = 0.9;
 		nc() = 20.0;
 		nm() = 20.0;
 		this->m_features |= AbstractMultiObjectiveOptimizer<RealVector >::CAN_SOLVE_CONSTRAINED;
@@ -101,10 +85,10 @@ public:
 	}
 	
 	double nm()const{
-		return m_mutator.m_nm;
+		return m_mutation.m_nm;
 	}
 	double& nm(){
-		return m_mutator.m_nm;
+		return m_mutation.m_nm;
 	}
 	
 	double nc()const{
@@ -129,25 +113,32 @@ public:
 	*/
 	template<typename Archive>
 	void serialize( Archive & archive, const unsigned int version ) {
-		archive & m_pop;
+		archive & m_parents;
 		archive & m_mu;
 		archive & m_best;
 
-		archive & m_evaluator;
 		archive & m_crossover;
-		archive & m_mutator;
+		archive & m_mutation;
 
 		archive & m_crossoverProbability;
 	}
 
-	using AbstractMultiObjectiveOptimizer<RealVector >::init;
+	void init( ObjectiveFunctionType& function){
+		checkFeatures(function);
+		if(!function.canProposeStartingPoint())
+			throw SHARKEXCEPTION( "[RealCodedNSGAII::init] Objective function does not propose a starting point");
+		std::vector<RealVector> points(mu());
+		for(std::size_t i = 0; i != mu(); ++i){
+			points[i] = function.proposeStartingPoint();
+		}
+		init(function,points);
+	}
 	/**
-	* \brief Initializes the algorithm for the supplied objective function.
-	* \tparam ObjectiveFunction The type of the objective function, 
-	* needs to adhere to the concept of an AbstractObjectiveFunction.
-	* \param [in] function The objective function
-	* \param [in] startingPoints Starting point to initialize the algorithm for.
-	*/
+	 * \brief Initializes the algorithm for the supplied objective function.
+	 * 
+	 * \param [in] function The objective function.
+	 * \param [in] startingPoints A set of intiial search points.
+	 */
 	void init( 
 		ObjectiveFunctionType& function, 
 		std::vector<SearchPointType> const& startingPoints
@@ -156,7 +147,7 @@ public:
 		std::vector<RealVector> values(startingPoints.size());
 		for(std::size_t i = 0; i != startingPoints.size(); ++i){
 			if(!function.isFeasible(startingPoints[i]))
-				throw SHARKEXCEPTION("[SMS-EMOA::init] starting point(s) not feasible");
+				throw SHARKEXCEPTION("[RealCodedNSGAII::init] starting point(s) not feasible");
 			values[i] = function.eval(startingPoints[i]);
 		}
 		
@@ -173,63 +164,119 @@ public:
 			throw SHARKEXCEPTION("[RealCodedNSGAII::init] Algorithm does only allow box constraints");
 		}
 		
-		//create parent set
-		m_pop.reserve( 2 * mu() );
-		m_pop.resize(mu());
-		m_best.resize(mu());
-		for(std::size_t i = 0; i != mu(); ++i){
-			m_pop[i].searchPoint()= function.proposeStartingPoint();
-		}
-		//evaluate initial parent set and create best front
-		m_evaluator( function, m_pop.begin(),m_pop.begin()+mu() );
-		m_selection( m_pop,m_mu );
-		for(std::size_t i = 0; i != mu(); ++i){
-			m_best[i].point = m_pop[i].searchPoint();
-			m_best[i].value = m_pop[i].unpenalizedFitness();
-		}
-		//make room for offspring
-		m_pop.resize(2*mu());
-		
-		
-		
-		
-		m_crossover.init(lowerBounds, upperBounds);
-		m_mutator.init(lowerBounds, upperBounds);
+		doInit(startingPoints,values,lowerBounds, upperBounds, mu(), nm(), nc(), crossoverProbability());
 	}
-
+	
 	/**
 	 * \brief Executes one iteration of the algorithm.
 	 * 
 	 * \param [in] function The function to iterate upon.
 	 */
 	void step( ObjectiveFunctionType const& function ) {
-		TournamentSelection< Individual::RankOrdering > matingSelection;
+		std::vector<IndividualType> offspring = generateOffspring();
+		PenalizingEvaluator penalizingEvaluator;
+		penalizingEvaluator( function, offspring.begin(), offspring.end() );
+		updatePopulation(offspring);
+	}
+protected:
+	/// \brief The individual type of the NSGA-II.
+	typedef shark::Individual<RealVector,RealVector> IndividualType;
+
+	void doInit(
+		std::vector<SearchPointType> const& startingPoints,
+		std::vector<ResultType> const& functionValues,
+		RealVector const& lowerBounds,
+		RealVector const& upperBounds,
+		std::size_t mu,
+		double nm,
+		double nc,
+		double crossover_prob
+	){
+		m_mu = mu;
+		m_mutation.m_nm = nm;
+		m_crossover.m_nc = nc;
+		m_crossoverProbability = crossover_prob;
+		m_best.resize( mu );
+		m_parents.resize( mu );
+		//if the number of supplied points is smaller than mu, fill everything in
+		std::size_t numPoints = 0;
+		if(startingPoints.size()<=mu){
+			numPoints = startingPoints.size();
+			for(std::size_t i = 0; i != numPoints; ++i){
+				m_parents[i].searchPoint() = startingPoints[i];
+				m_parents[i].penalizedFitness() = functionValues[i];
+				m_parents[i].unpenalizedFitness() = functionValues[i];
+			}
+		}
+		//copy points randomly
+		for(std::size_t i = numPoints; i != mu; ++i){
+			std::size_t index = Rng::discrete(0,startingPoints.size()-1);
+			m_parents[i].searchPoint() = startingPoints[index];
+			m_parents[i].penalizedFitness() = functionValues[index];
+			m_parents[i].unpenalizedFitness() = functionValues[index];
+		}
+		//create initial mu best points
+		for(std::size_t i = 0; i != mu; ++i){
+			m_best[i].point = m_parents[i].searchPoint();
+			m_best[i].value = m_parents[i].unpenalizedFitness();
+		}
+		m_selection( m_parents, mu );
 		
-		matingSelection(
-			m_pop.begin(), 
-			m_pop.begin() + mu(),
-			m_pop.begin() + mu(),
-			m_pop.end()
+		m_crossover.init(lowerBounds,upperBounds);
+		m_mutation.init(lowerBounds,upperBounds);
+	}
+	
+	std::vector<IndividualType> generateOffspring()const{
+		TournamentSelection< IndividualType::RankOrdering > selection;
+		std::vector<IndividualType> offspring(mu());
+		selection(
+			m_parents.begin(), 
+			m_parents.end(),
+			offspring.begin(),
+			offspring.end()
 		);
 
 		for( std::size_t i = 0; i < mu()-1; i+=2 ) {
-			if( Rng::coinToss( 0.8 ) ) {
-				m_crossover( m_pop[mu() + i ], m_pop[mu() + i +1] );
+			if( Rng::coinToss( m_crossoverProbability ) ) {
+				m_crossover( offspring[i], offspring[i +1] );
 			}
 		}
 		for( std::size_t i = 0; i < mu(); i++ ) {
-			m_mutator( m_pop[mu() + i] );
+			m_mutation( offspring[i] );
 		}
-		m_evaluator( function, m_pop.begin()+mu(), m_pop.end() );
-		m_selection( m_pop, m_mu );
+		return offspring;
+	}
 
-		std::partition( m_pop.begin(), m_pop.end(), Individual::IsSelected );	
+	/**
+	 * \brief Executes one iteration of the algorithm.
+	 * 
+	 * \param [in] function The function to iterate upon.
+	 */
+	void updatePopulation(  std::vector<IndividualType> const& offspringVec) {
+		m_parents.insert(m_parents.end(),offspringVec.begin(),offspringVec.end());
+		m_selection( m_parents, mu());
+		
+		//partition the selected individuals to the front and remove the unselected ones
+		std::partition(m_parents.begin(), m_parents.end(),IndividualType::IsSelected);
+		m_parents.erase(m_parents.begin()+mu(),m_parents.end());
 
-		for( std::size_t i = 0; i != mu(); ++i ) {
-			noalias(m_best[i].value) = m_pop[i].unpenalizedFitness();
-			noalias(m_best[i].point) = m_pop[i].searchPoint();
+		//update solution set
+		for (std::size_t i = 0; i < mu(); i++) {
+			noalias(m_best[i].point) = m_parents[i].searchPoint();
+			m_best[i].value = m_parents[i].unpenalizedFitness();
 		}
 	}
+private:
+
+	std::vector<IndividualType> m_parents; ///< Population of size \f$\mu + 1\f$.
+	std::size_t m_mu; ///< Size of parent generation
+
+	IndicatorBasedSelection<Indicator> m_selection; ///< Selection operator relying on the (contributing) hypervolume indicator.
+
+	SimulatedBinaryCrossover< RealVector > m_crossover; ///< Crossover operator.
+	PolynomialMutator m_mutation; ///< Mutation operator.
+
+	double m_crossoverProbability; ///< Crossover probability.	
 };
 
 typedef IndicatorBasedRealCodedNSGAII< HypervolumeIndicator > RealCodedNSGAII;
diff --git a/include/shark/Algorithms/DirectSearch/SMS-EMOA.h b/include/shark/Algorithms/DirectSearch/SMS-EMOA.h
index 1991a9f..c0d3482 100644
--- a/include/shark/Algorithms/DirectSearch/SMS-EMOA.h
+++ b/include/shark/Algorithms/DirectSearch/SMS-EMOA.h
@@ -146,7 +146,7 @@ public:
 		} else{
 			throw SHARKEXCEPTION("[SMS-EMOA::init] Algorithm does only allow box constraints");
 		}
-		this->doInit(startingPoints,values,lowerBounds, upperBounds,mu(),nm(),nc(),crossoverProbability());
+		doInit(startingPoints,values,lowerBounds, upperBounds,mu(),nm(),nc(),crossoverProbability());
 	}
 
 	/**
@@ -188,21 +188,21 @@ protected:
 				m_parents[i].searchPoint() = startingPoints[i];
 				m_parents[i].penalizedFitness() = functionValues[i];
 				m_parents[i].unpenalizedFitness() = functionValues[i];
-				m_best[i].point = m_parents[i].searchPoint();
-				m_best[i].value = m_parents[i].unpenalizedFitness();
 			}
 		}
 		//copy points randomly
 		for(std::size_t i = numPoints; i != mu; ++i){
 			std::size_t index = Rng::discrete(0,startingPoints.size()-1);
 			m_parents[i].searchPoint() = startingPoints[index];
-				m_parents[i].penalizedFitness() = functionValues[index];
-				m_parents[i].unpenalizedFitness() = functionValues[index];
-				m_best[i].point = m_parents[i].searchPoint();
-				m_best[i].value = m_parents[i].unpenalizedFitness();
+			m_parents[i].penalizedFitness() = functionValues[index];
+			m_parents[i].unpenalizedFitness() = functionValues[index];
+		}
+		//create initial mu best points
+		for(std::size_t i = 0; i != mu; ++i){
+			m_best[i].point = m_parents[i].searchPoint();
+			m_best[i].value = m_parents[i].unpenalizedFitness();
 		}
 		m_selection( m_parents, mu );
-		m_parents.push_back(m_parents[0]);
 		
 		m_crossover.init(lowerBounds,upperBounds);
 		m_mutator.init(lowerBounds,upperBounds);
@@ -215,7 +215,7 @@ protected:
 	}
 	
 	void updatePopulation(  std::vector<IndividualType> const& offspring) {
-		m_parents.back() = offspring.back();
+		m_parents.push_back(offspring[0]);
 		m_selection( m_parents, mu());
 
 		//if the individual got selected, insert it into the parent population
@@ -229,6 +229,7 @@ protected:
 				}
 			}
 		}
+		m_parents.pop_back();
 	}
 	
 	std::vector<IndividualType> m_parents; ///< Population of size \f$\mu + 1\f$.
@@ -238,7 +239,6 @@ private:
 		std::vector<IndividualType>::const_iterator begin,
 		std::vector<IndividualType>::const_iterator end
 	)const{
-		std::size_t popSize = end-begin;
 		TournamentSelection< IndividualType::RankOrdering > selection;
 
 		IndividualType mate1( *selection( begin, end ) );

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