[shark] 31/58: new try for init interface
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Wed Mar 16 10:05:31 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 1d6698220b7965cf2a1fe6968ffce39afab02eb7
Author: Oswin <oswin.krause at di.ku.dk>
Date: Thu Feb 4 13:57:02 2016 +0100
new try for init interface
---
Test/Algorithms/DirectSearch/CMA.cpp | 14 +--
Test/Algorithms/DirectSearch/SMS-EMOA.cpp | 85 ++++++++++++++++++
Test/CMakeLists.txt | 1 +
Test/ObjectiveFunctions/Benchmarks.cpp | 7 --
examples/EA/SOO/CMASimple.tpp | 3 +-
include/shark/Algorithms/DirectSearch/CMA.h | 32 +++++--
.../Operators/Mutation/PolynomialMutation.h | 27 ++----
.../Recombination/SimulatedBinaryCrossover.h | 27 ++----
.../Algorithms/DirectSearch/RealCodedNSGAII.h | 27 +++++-
include/shark/Algorithms/DirectSearch/SMS-EMOA.h | 100 +++++++++++----------
.../Trainers/Distribution/NormalTrainer.h | 2 +-
src/Algorithms/DirectSearch/CMA.cpp | 52 +++--------
12 files changed, 224 insertions(+), 153 deletions(-)
diff --git a/Test/Algorithms/DirectSearch/CMA.cpp b/Test/Algorithms/DirectSearch/CMA.cpp
index d7e3098..2b26626 100644
--- a/Test/Algorithms/DirectSearch/CMA.cpp
+++ b/Test/Algorithms/DirectSearch/CMA.cpp
@@ -53,15 +53,15 @@ BOOST_AUTO_TEST_CASE( CMA_Rosenbrock )
test_function( optimizer, function, _trials = 10, _iterations = 1000, _epsilon = 1E-10 );
}
-BOOST_AUTO_TEST_CASE( CMA__Ellipsoid_Niko )
+BOOST_AUTO_TEST_CASE( CMA_Ellipsoid_Niko )
{
const unsigned N = 10;
RealVector x0(10, 0.1);
Ellipsoid elli(N, 1E6);
-
+ elli.init();
CMA cma;
- std::size_t lambda = CMA::suggestLambda(N);
- cma.init(elli, x0,lambda,CMA::suggestMu(lambda),0.1);
+ cma.setInitialSigma(0.1);
+ cma.init(elli, x0);
BOOST_REQUIRE(cma.sigma() == 0.1);
for(unsigned i=0; i<6000; i++) cma.step( elli );
@@ -74,10 +74,10 @@ BOOST_AUTO_TEST_CASE( CMA_Sphere_Niko )
const unsigned N = 10;
RealVector x0(10, 0.1);
Sphere sphere(N);
-
+ sphere.init();
CMA cma;
- std::size_t lambda = CMA::suggestLambda(N);
- cma.init(sphere, x0,lambda,CMA::suggestMu(lambda),1.e-4);
+ cma.setInitialSigma(1.e-4);
+ cma.init(sphere, x0);
BOOST_REQUIRE(cma.sigma() == 1.e-4);
bool sigmaHigh = false;
diff --git a/Test/Algorithms/DirectSearch/SMS-EMOA.cpp b/Test/Algorithms/DirectSearch/SMS-EMOA.cpp
new file mode 100644
index 0000000..5440164
--- /dev/null
+++ b/Test/Algorithms/DirectSearch/SMS-EMOA.cpp
@@ -0,0 +1,85 @@
+#define BOOST_TEST_MODULE DirectSearch_SMSEMOA
+#include <boost/test/unit_test.hpp>
+#include <boost/test/floating_point_comparison.hpp>
+
+#include <shark/Algorithms/DirectSearch/SMS-EMOA.h>
+#include <shark/ObjectiveFunctions/Benchmarks/Benchmarks.h>
+
+using namespace shark;
+
+struct PointExtractor{
+
+ template<class T>
+ RealVector const& operator()(T const& arg)const{
+ return arg.value;
+ }
+};
+
+double testObjectiveFunctionMOOHelper(
+ MultiObjectiveFunction& f,
+ std::size_t mu,
+ std::size_t iterations,
+ RealVector const& reference
+){
+ SMSEMOA smsemoa;
+ smsemoa.mu() = mu;
+ smsemoa.init(f);
+
+ for(std::size_t i = 0; i != iterations; ++i){
+ smsemoa.step(f);
+ //~ if(i%(iterations/100)==0)
+ //~ std::clog<<"\r"<<i<<" "<<std::flush;
+ }
+ BOOST_REQUIRE_EQUAL(smsemoa.solution().size(), mu);
+ HypervolumeCalculator hyp;
+ double volume = hyp(PointExtractor(),smsemoa.solution(),reference);
+ std::cout<<"\r"<<f.name()<<": "<<volume<<std::endl;
+ return volume;
+// BOOST_CHECK_SMALL(volume - targetVolume, 5.e-3);
+}
+
+void testObjectiveFunctionMOO(
+ MultiObjectiveFunction& f,
+ std::size_t mu,
+ double targetVolume,
+ std::size_t iterations,
+ RealVector const& reference
+){
+ std::vector<double> result(10);
+ for (std::size_t i=0; i<result.size(); i++) result[i] = testObjectiveFunctionMOOHelper(f, mu, iterations, reference);
+ double best = *std::max_element(result.begin(), result.end());
+ BOOST_CHECK_SMALL(best - targetVolume, 5.e-3);
+}
+
+
+BOOST_AUTO_TEST_SUITE (Algorithms_DirectSearch_SMSEMOA)
+
+BOOST_AUTO_TEST_CASE( SMSEMOA_HYPERVOLUME_Functions ) {
+ RealVector reference(2);
+ reference(0) = 11;
+ reference(1) = 11;
+ DTLZ2 dtlz2(5);
+ double dtlz2Volume = 120.178966;
+ testObjectiveFunctionMOO(dtlz2,10,dtlz2Volume,10000,reference);
+ DTLZ4 dtlz4(5);
+ double dtlz4Volume = 120.178966;
+ testObjectiveFunctionMOO(dtlz4,10,dtlz4Volume,10000,reference);
+ //~ DTLZ7 dtlz7(5); //not sure whether correctly implemented
+ //~ double dtlz7Volume = 115.964708;
+ //~ testObjectiveFunctionMOO(dtlz7,10,dtlz7Volume,10000,reference);
+ ZDT1 zdt1(5);
+ double zdt1Volume = 120.613761;
+ testObjectiveFunctionMOO(zdt1,10,zdt1Volume,10000,reference);
+ ZDT2 zdt2(5); //fails somehow
+ double zdt2Volume = 120.286820;
+ testObjectiveFunctionMOO(zdt2,10,zdt2Volume,10000,reference);
+ ZDT3 zdt3(5);
+ double zdt3Volume = 128.748470;
+ testObjectiveFunctionMOO(zdt3,10,zdt3Volume,10000,reference);
+ ZDT6 zdt6(5);
+ double zdt6Volume = 117.483246;
+ testObjectiveFunctionMOO(zdt6,10,zdt6Volume,10000,reference);
+}
+
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/Test/CMakeLists.txt b/Test/CMakeLists.txt
index 0fbfb68..478e315 100644
--- a/Test/CMakeLists.txt
+++ b/Test/CMakeLists.txt
@@ -85,6 +85,7 @@ shark_add_test( Algorithms/DirectSearch/VDCMA.cpp DirectSearch_VDCMA )
shark_add_test( Algorithms/DirectSearch/MOCMA.cpp DirectSearch_MOCMA )
shark_add_test( Algorithms/DirectSearch/SteadyStateMOCMA.cpp DirectSearch_SteadyStateMOCMA )
shark_add_test( Algorithms/DirectSearch/RealCodedNSGAII.cpp DirectSearch_RealCodedNSGAII )
+shark_add_test( Algorithms/DirectSearch/SMS-EMOA.cpp DirectSearch_SMS-EMOA )
shark_add_test( Algorithms/DirectSearch/FastNonDominatedSort.cpp DirectSearch_FastNonDominatedSort )
shark_add_test( Algorithms/DirectSearch/ParetoDominanceComparator.cpp DirectSearch_ParetoDominanceComparator )
diff --git a/Test/ObjectiveFunctions/Benchmarks.cpp b/Test/ObjectiveFunctions/Benchmarks.cpp
index a62d7f8..15e61a0 100644
--- a/Test/ObjectiveFunctions/Benchmarks.cpp
+++ b/Test/ObjectiveFunctions/Benchmarks.cpp
@@ -7,13 +7,6 @@
#include <boost/progress.hpp>
#include <boost/serialization/vector.hpp>
-#include <shark/Algorithms/DirectSearch/SteadyStateMOCMA.h>
-#include <shark/Algorithms/DirectSearch/SMS-EMOA.h>
-#include <shark/Algorithms/DirectSearch/RealCodedNSGAII.h>
-
-#include <shark/Algorithms/DirectSearch/HypervolumeApproximator.h>
-#include <shark/Algorithms/DirectSearch/FitnessExtractor.h>
-
#include <shark/ObjectiveFunctions/Benchmarks/Benchmarks.h>
#include <shark/Statistics/Statistics.h>
diff --git a/examples/EA/SOO/CMASimple.tpp b/examples/EA/SOO/CMASimple.tpp
index 16eebce..cf4e984 100755
--- a/examples/EA/SOO/CMASimple.tpp
+++ b/examples/EA/SOO/CMASimple.tpp
@@ -52,7 +52,8 @@ int main( int argc, char ** argv ) {
// Initialize the optimizer for the objective function instance.
//###begin<optimizer>
CMA cma;
- cma.init( sphere, sphere.proposeStartingPoint(), 0.1 ); // Explicitely set initial global step size.
+ cma.setInitialSigma(0.1);
+ cma.init( sphere, sphere.proposeStartingPoint()); // Explicitely set initial global step size.
//###end<optimizer>
// Iterate the optimizer until a solution of sufficient quality is found.
diff --git a/include/shark/Algorithms/DirectSearch/CMA.h b/include/shark/Algorithms/DirectSearch/CMA.h
index 082539b..ed6f7fa 100644
--- a/include/shark/Algorithms/DirectSearch/CMA.h
+++ b/include/shark/Algorithms/DirectSearch/CMA.h
@@ -116,14 +116,6 @@ public:
SHARK_EXPORT_SYMBOL void init(
ObjectiveFunctionType& function,
SearchPointType const& initialSearchPoint,
- double initialSigma
- );
- /**
- * \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,
@@ -134,6 +126,13 @@ public:
* \brief Executes one iteration of the algorithm.
*/
SHARK_EXPORT_SYMBOL void step(ObjectiveFunctionType const& function);
+
+ /// \brief sets the initial step length sigma
+ ///
+ /// It is by default <=0 which means that sigma =1/sqrt(numVariables)
+ void setInitialSigma(double initSigma){
+ m_initSigma = initSigma;
+ }
/** \brief Accesses the current step size. */
double sigma() const {
@@ -193,6 +192,17 @@ public:
return m_mu;
}
+ /// \brief Sets the number of selected samples
+ void setMu(std::size_t mu){
+ m_mu = mu;
+ m_userSetMu = true;
+ }
+ /// \brief Sets the number of sampled points
+ void setLambda(std::size_t lambda){
+ m_lambda = lambda;
+ m_userSetLambda = true;
+ }
+
/**
* \brief Returns a immutable reference to the size of the offspring population \f$\mu\f$.
*/
@@ -234,7 +244,6 @@ protected:
SHARK_EXPORT_SYMBOL void updatePopulation( std::vector<IndividualType > const& offspring ) ;
SHARK_EXPORT_SYMBOL void doInit(
- AbstractConstraintHandler<SearchPointType> const* handler,
std::vector<SearchPointType> const& points,
std::vector<ResultType> const& functionValues,
std::size_t lambda,
@@ -247,8 +256,13 @@ private:
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.
+ bool m_userSetMu; /// <The user set a value via setMu, do not overwrite with default
+ bool m_userSetLambda; /// <The user set a value via setMu, do not overwrite with default
+ double m_initSigma; ///< use provided initial value of sigma<=0 =>algorithm chooses
+
RecombinationType m_recombinationType; ///< Stores the recombination type.
+
double m_sigma;
double m_cC;
double m_c1;
diff --git a/include/shark/Algorithms/DirectSearch/Operators/Mutation/PolynomialMutation.h b/include/shark/Algorithms/DirectSearch/Operators/Mutation/PolynomialMutation.h
index 182f932..8cbf1f3 100644
--- a/include/shark/Algorithms/DirectSearch/Operators/Mutation/PolynomialMutation.h
+++ b/include/shark/Algorithms/DirectSearch/Operators/Mutation/PolynomialMutation.h
@@ -44,25 +44,12 @@ namespace shark {
/// \brief Default c'tor.
PolynomialMutator() : m_nm( 20.0 ) {}
- /// \brief Initializes the operator for the supplied constraints
- ///
- /// \param [in] constraints the constraint handler of the function or 0 if it does not have one
- void init( AbstractConstraintHandler<PointType> const* constraints,std::size_t n ) {
- m_prob = 1./f.numberOfVariables();
- if(!constraints){
- m_lower = blas::repeat(-1E20,n);
- m_upper = blas::repeat(1E20,n);
- }
- else if (constraints && constraints->isBoxConstrained()) {
- typedef BoxConstraintHandler<PointType> ConstraintHandler;
- ConstraintHandler const& handler = static_cast<ConstraintHandler const&>(*constraints);
-
- m_lower = handler.lower();
- m_upper = handler.upper();
-
- } else{
- throw SHARKEXCEPTION("[SimulatedBinaryCrossover::init] Algorithm does only allow box constraints");
- }
+ /// \brief Initializes the operator for the supplied box-constraints
+ void init( RealVector const& lower, RealVector const& upper ) {
+ SIZE_CHECK(lower.size() == upper.size());
+ m_prob = 1./lower.size();
+ m_lower = lower;
+ m_upper = upper;
}
/// \brief Mutates the supplied individual.
@@ -70,7 +57,7 @@ namespace shark {
/// for accessing the actual search point.
/// \param [in,out] ind Individual to be mutated.
template<typename IndividualType>
- void operator()( IndividualType & ind ) {
+ void operator()( IndividualType & ind )const{
double delta, deltaQ, expp, u = 0.;
RealVector& point = ind.searchPoint();
diff --git a/include/shark/Algorithms/DirectSearch/Operators/Recombination/SimulatedBinaryCrossover.h b/include/shark/Algorithms/DirectSearch/Operators/Recombination/SimulatedBinaryCrossover.h
index 53921b6..f659a3e 100644
--- a/include/shark/Algorithms/DirectSearch/Operators/Recombination/SimulatedBinaryCrossover.h
+++ b/include/shark/Algorithms/DirectSearch/Operators/Recombination/SimulatedBinaryCrossover.h
@@ -45,25 +45,12 @@ namespace shark {
: m_nc( 20.0 )
, m_prob( 0.5 ) {}
- /// \brief Initializes the operator for the supplied constraints
- ///
- /// \param [in] constraints The constraint handler of the function or 0 if it does not have one
- void init( AbstractConstraintHandler<PointType> const* constraints, std::size_t n ) {
- m_prob = 0.5;
- if(!constraints){
- m_lower = blas::repeat(-1E20,n);
- m_upper = blas::repeat(1E20,n);
- }
- else if (constraints && constraints->isBoxConstrained()) {
- typedef BoxConstraintHandler<PointType> ConstraintHandler;
- ConstraintHandler const& handler = static_cast<ConstraintHandler const&>(*constraints);
-
- m_lower = handler.lower();
- m_upper = handler.upper();
-
- } else{
- throw SHARKEXCEPTION("[SimulatedBinaryCrossover::init] Algorithm does only allow box constraints");
- }
+ /// \brief Initializes the operator for the supplied box-constraints
+ void init( RealVector const& lower, RealVector const& upper ) {
+ SIZE_CHECK(lower.size() == upper.size());
+ m_prob = 1./lower.size();
+ m_lower = lower;
+ m_upper = upper;
}
/// \brief Mates the supplied individuals.
@@ -71,7 +58,7 @@ namespace shark {
/// \param [in,out] i1 Individual to be mated.
/// \param [in,out] i2 Individual to be mated.
template<typename IndividualType>
- void operator()( IndividualType & i1, IndividualType & i2 ) {
+ void operator()( IndividualType & i1, IndividualType & i2 )const{
RealVector& point1 = i1.searchPoint();
RealVector& point2 = i2.searchPoint();
diff --git a/include/shark/Algorithms/DirectSearch/RealCodedNSGAII.h b/include/shark/Algorithms/DirectSearch/RealCodedNSGAII.h
index 9b00bb7..fabb032 100644
--- a/include/shark/Algorithms/DirectSearch/RealCodedNSGAII.h
+++ b/include/shark/Algorithms/DirectSearch/RealCodedNSGAII.h
@@ -153,7 +153,25 @@ public:
std::vector<SearchPointType> const& startingPoints
){
checkFeatures(function);
- function.init();
+ 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");
+ values[i] = function.eval(startingPoints[i]);
+ }
+
+ std::size_t dim = function.numberOfVariables();
+ RealVector lowerBounds(dim, -1E20);
+ RealVector upperBounds(dim, 1E20);
+ if (function.hasConstraintHandler() && function.getConstraintHandler().isBoxConstrained()) {
+ typedef BoxConstraintHandler<SearchPointType> ConstraintHandler;
+ ConstraintHandler const& handler = static_cast<ConstraintHandler const&>(function.getConstraintHandler());
+
+ lowerBounds = handler.lower();
+ upperBounds = handler.upper();
+ } else{
+ throw SHARKEXCEPTION("[RealCodedNSGAII::init] Algorithm does only allow box constraints");
+ }
//create parent set
m_pop.reserve( 2 * mu() );
@@ -172,8 +190,11 @@ public:
//make room for offspring
m_pop.resize(2*mu());
- m_crossover.init(function);
- m_mutator.init(function);
+
+
+
+ m_crossover.init(lowerBounds, upperBounds);
+ m_mutator.init(lowerBounds, upperBounds);
}
/**
diff --git a/include/shark/Algorithms/DirectSearch/SMS-EMOA.h b/include/shark/Algorithms/DirectSearch/SMS-EMOA.h
index 5efbd8c..9941d9a 100644
--- a/include/shark/Algorithms/DirectSearch/SMS-EMOA.h
+++ b/include/shark/Algorithms/DirectSearch/SMS-EMOA.h
@@ -60,9 +60,6 @@ namespace shark {
* European Journal of Operational Research.
*/
class SMSEMOA : public AbstractMultiObjectiveOptimizer<RealVector >{
-protected:
- /// \brief The individual type of the SMS-EMOA.
- typedef shark::Individual<RealVector,RealVector> IndividualType;
public:
SMSEMOA() {
m_mu = 100;
@@ -92,13 +89,16 @@ public:
unsigned int mu()const{
return m_mu;
}
+
+ unsigned int& mu(){
+ return m_mu;
+ }
void read( InArchive & archive ){
archive & BOOST_SERIALIZATION_NVP( m_pop );
archive & BOOST_SERIALIZATION_NVP( m_mu );
archive & BOOST_SERIALIZATION_NVP( m_best );
- archive & BOOST_SERIALIZATION_NVP( m_evaluator );
archive & BOOST_SERIALIZATION_NVP( m_selection );
archive & BOOST_SERIALIZATION_NVP( m_crossover );
archive & BOOST_SERIALIZATION_NVP( m_mutator );
@@ -109,7 +109,6 @@ public:
archive & BOOST_SERIALIZATION_NVP( m_mu );
archive & BOOST_SERIALIZATION_NVP( m_best );
- archive & BOOST_SERIALIZATION_NVP( m_evaluator );
archive & BOOST_SERIALIZATION_NVP( m_selection );
archive & BOOST_SERIALIZATION_NVP( m_crossover );
archive & BOOST_SERIALIZATION_NVP( m_mutator );
@@ -128,19 +127,26 @@ public:
std::vector<SearchPointType> const& startingPoints
){
checkFeatures(function);
- function.init();
-
- AbstractConstraintHandler<SearchPointType> const* handler = 0;
- if (function.hasConstraintHandler())
- handler = &function.getConstraintHandler();
- RealVector values(startingPoints.size());
+ std::vector<RealVector> values(startingPoints.size());
for(std::size_t i = 0; i != startingPoints.size(); ++i){
- if(!startingPoints[i].isFeasible())
- throw SHAREXCEPTION("[SMS-EMOA::init] starting point(s) not feasible");
+ if(!function.isFeasible(startingPoints[i]))
+ throw SHARKEXCEPTION("[SMS-EMOA::init] starting point(s) not feasible");
values[i] = function.eval(startingPoints[i]);
}
- doInit(handler,startingPoints,values100,20.0,20.0,0.9);
+ std::size_t dim = function.numberOfVariables();
+ RealVector lowerBounds(dim, -1E20);
+ RealVector upperBounds(dim, 1E20);
+ if (function.hasConstraintHandler() && function.getConstraintHandler().isBoxConstrained()) {
+ typedef BoxConstraintHandler<SearchPointType> ConstraintHandler;
+ ConstraintHandler const& handler = static_cast<ConstraintHandler const&>(function.getConstraintHandler());
+
+ lowerBounds = handler.lower();
+ upperBounds = handler.upper();
+ } else{
+ throw SHARKEXCEPTION("[SMS-EMOA::init] Algorithm does only allow box constraints");
+ }
+ this->doInit(startingPoints,values,lowerBounds, upperBounds,mu(),nm(),nc(),crossoverProbability());
}
/**
@@ -155,19 +161,14 @@ public:
updatePopulation(offspring);
}
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 void updatePopulation( std::vector<IndividualType > const& offspring ) ;
+ /// \brief The individual type of the SMS-EMOA.
+ typedef shark::Individual<RealVector,RealVector> IndividualType;
void doInit(
- AbstractConstraintHandler<SearchPointType> const* handler,
- std::vector<SearchPointType> const& points,
+ std::vector<SearchPointType> const& startingPoints,
std::vector<ResultType> const& functionValues,
+ RealVector const& lowerBounds,
+ RealVector const& upperBounds,
std::size_t mu,
double nm,
double nc,
@@ -179,23 +180,32 @@ protected:
m_crossoverProbability = crossover_prob;
m_best.resize( mu );
m_pop.resize( mu );
- std::size_t numPoints = std::min(mu,points.size());
- for(std::size_t i = 0; i != numPoints; ++i){
- m_pop[i].searchPoint() = startingPoints[i];
- m_pop[i].penalizedFitness() = functionValues[i];
- m_pop[i].unpenalizedFitness() = functionValues[i];
- m_best[i].point = m_pop[i].searchPoint();
- m_best[i].value = m_pop[i].unpenalizedFitness();
+ //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_pop[i].searchPoint() = startingPoints[i];
+ m_pop[i].penalizedFitness() = functionValues[i];
+ m_pop[i].unpenalizedFitness() = functionValues[i];
+ m_best[i].point = m_pop[i].searchPoint();
+ m_best[i].value = m_pop[i].unpenalizedFitness();
+ }
}
- for(std::size_t i = numPoints; i != mu()+1; ++i){
- m_pop[i] = m_pop[Rng::discrete(0,numPoints-1)];
- m_best[i].point = m_pop[i].searchPoint();
- m_best[i].value = m_pop[i].unpenalizedFitness();
+ //copy points randomly
+ for(std::size_t i = numPoints; i != mu; ++i){
+ std::size_t index = Rng::discrete(0,numPoints-1);
+ m_pop[i].searchPoint() = startingPoints[index];
+ m_pop[i].penalizedFitness() = functionValues[index];
+ m_pop[i].unpenalizedFitness() = functionValues[index];
+ m_best[i].point = m_pop[i].searchPoint();
+ m_best[i].value = m_pop[i].unpenalizedFitness();
}
m_selection( m_pop, mu );
m_pop.push_back(m_pop[0]);
- m_crossover.init(handler,points[0].size());
- m_mutator.init(handler,points[0].size());
+
+ m_crossover.init(lowerBounds,upperBounds);
+ m_mutator.init(lowerBounds,upperBounds);
}
std::vector<IndividualType> generateOffspring()const{
@@ -205,8 +215,7 @@ protected:
}
void updatePopulation( std::vector<IndividualType> const& offspring) {
- m_pop.back(offspring.back());
- m_evaluator( function, m_pop.back() );
+ m_pop.back() = offspring.back();
m_selection( m_pop, mu());
//if the individual got selected, insert it into the parent population
@@ -224,14 +233,14 @@ protected:
private:
IndividualType createOffspring(
- std::vector<IndividualType>::iterator begin,
- std::vector<IndividualType>::iterator end
+ std::vector<IndividualType>::const_iterator begin,
+ std::vector<IndividualType>::const_iterator end
)const{
std::size_t popSize = end-begin;
- TournamentSelection< Individual::RankOrdering > selection;
+ TournamentSelection< IndividualType::RankOrdering > selection;
- Individual mate1( *selection( begin, end ) );
- Individual mate2( *selection( begin, end) );
+ IndividualType mate1( *selection( begin, end ) );
+ IndividualType mate2( *selection( begin, end) );
if( Rng::coinToss( m_crossoverProbability ) ) {
m_crossover( mate1, mate2 );
@@ -246,10 +255,9 @@ private:
}
}
- std::vector<Individual> m_pop; ///< Population of size \f$\mu + 1\f$.
+ std::vector<IndividualType> m_pop; ///< Population of size \f$\mu + 1\f$.
unsigned int m_mu; ///< Size of parent generation
- PenalizingEvaluator m_evaluator; ///< Evaluation operator.
IndicatorBasedSelection<HypervolumeIndicator> m_selection; ///< Selection operator relying on the (contributing) hypervolume indicator.
SimulatedBinaryCrossover< RealVector > m_crossover; ///< Crossover operator.
PolynomialMutator m_mutator; ///< Mutation operator.
diff --git a/include/shark/Algorithms/Trainers/Distribution/NormalTrainer.h b/include/shark/Algorithms/Trainers/Distribution/NormalTrainer.h
index 2243714..818e271 100644
--- a/include/shark/Algorithms/Trainers/Distribution/NormalTrainer.h
+++ b/include/shark/Algorithms/Trainers/Distribution/NormalTrainer.h
@@ -73,7 +73,7 @@ public:
SIZE_CHECK(input.size() > 1u);
namespace bae = boost::accumulators::extract;
InternalAccumulatorType accu;
-#ifdef BOOST_VERSION < 106000
+#if (BOOST_VERSION < 106000)
boost::range::for_each(input, boost::bind(boost::ref(accu), _1));
#else
boost::range::for_each(input, boost::bind(boost::ref(accu), boost::placeholders::_1));
diff --git a/src/Algorithms/DirectSearch/CMA.cpp b/src/Algorithms/DirectSearch/CMA.cpp
index a270659..1576114 100644
--- a/src/Algorithms/DirectSearch/CMA.cpp
+++ b/src/Algorithms/DirectSearch/CMA.cpp
@@ -90,7 +90,10 @@ std::size_t CMA::suggestMu( std::size_t lambda, RecombinationType recomb) {
}
CMA::CMA()
-:m_recombinationType( SUPERLINEAR )
+: m_userSetMu(false)
+, m_userSetLambda(false)
+, m_initSigma(-1)
+, m_recombinationType( SUPERLINEAR )
, m_sigma( 0 )
, m_cC( 0 )
, m_c1( 0 )
@@ -162,44 +165,19 @@ 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));
- AbstractConstraintHandler<SearchPointType> const* handler = 0;
- if (function.hasConstraintHandler())
- handler = &function.getConstraintHandler();
- std::size_t lambda = CMA::suggestLambda( p.size() );
- doInit(
- handler,
- points,
- functionValues,
- lambda,
- CMA::suggestMu(lambda, m_recombinationType),
- 1.0/std::sqrt(double(m_numberOfVariables))
- );
-}
-void CMA::init(
- ObjectiveFunctionType& function,
- SearchPointType const& p,
- double initialSigma
-) {
- SIZE_CHECK(p.size() == function.numberOfVariables());
- checkFeatures(function);
- function.init();
- std::vector<RealVector> points(1,p);
- std::vector<double> functionValues(1,function.eval(p));
- AbstractConstraintHandler<SearchPointType> const* handler = 0;
- if (function.hasConstraintHandler())
- handler = &function.getConstraintHandler();
- std::size_t lambda = CMA::suggestLambda( p.size() );
+ std::size_t lambda = m_userSetLambda? m_lambda:CMA::suggestLambda( p.size() );
+ std::size_t mu = m_userSetMu? m_mu:CMA::suggestMu(lambda, m_recombinationType);
+ RANGE_CHECK(mu < lambda);
+ double sigma = m_initSigma > 0? m_initSigma : 1.0/std::sqrt(double(m_numberOfVariables));
doInit(
- handler,
points,
functionValues,
lambda,
- CMA::suggestMu(lambda, m_recombinationType),
- initialSigma
+ mu,
+ sigma
);
}
@@ -212,15 +190,13 @@ void CMA::init(
const boost::optional< RealMatrix > & initialCovarianceMatrix
) {
SIZE_CHECK(p.size() == function.numberOfVariables());
+ RANGE_CHECK(mu < lambda);
+ setMu(mu);
+ setLambda(lambda);
checkFeatures(function);
- function.init();
std::vector<RealVector> points(1,p);
std::vector<double> functionValues(1,function.eval(p));
- AbstractConstraintHandler<SearchPointType> const* handler = 0;
- if (function.hasConstraintHandler())
- handler = &function.getConstraintHandler();
doInit(
- handler,
points,
functionValues,
lambda,
@@ -233,7 +209,6 @@ void CMA::init(
}
}
void CMA::doInit(
- AbstractConstraintHandler<SearchPointType> const* constraints,
std::vector<SearchPointType> const& initialSearchPoints,
std::vector<ResultType> const& initialValues,
std::size_t lambda,
@@ -333,7 +308,6 @@ void CMA::updatePopulation( std::vector<IndividualType> const& offspring ) {
m_evolutionPathSigma = (1. - m_cSigma)*m_evolutionPathSigma + std::sqrt( m_cSigma * (2. - m_cSigma) * m_muEff ) * CInvY; // eq. (40)
m_sigma *= std::exp((m_cSigma / m_dSigma) * (norm_2(m_evolutionPathSigma) / expectedChi - 1.)); // eq. (39)
-
// update mutation distribution
m_mutationDistribution.update();
--
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