32 #ifndef SHARK_ALGORITHMS_DIRECT_SEARCH_REAL_CODED_NSGA_II_H 33 #define SHARK_ALGORITHMS_DIRECT_SEARCH_REAL_CODED_NSGA_II_H 59 template<
typename Indicator>
75 return "RealCodedNSGAII";
80 return m_crossoverProbability;
84 return m_crossoverProbability;
88 return m_mutation.
m_nm;
91 return m_mutation.
m_nm;
95 return m_crossover.
m_nc;
98 return m_crossover.
m_nc;
101 std::size_t
mu()
const{
109 archive >> BOOST_SERIALIZATION_NVP(m_parents);
110 archive >> BOOST_SERIALIZATION_NVP(m_mu);
111 archive >> BOOST_SERIALIZATION_NVP(
m_best);
113 archive >> BOOST_SERIALIZATION_NVP( m_selection );
114 archive >> BOOST_SERIALIZATION_NVP(m_crossover);
115 archive >> BOOST_SERIALIZATION_NVP(m_mutation);
116 archive >> BOOST_SERIALIZATION_NVP(m_crossoverProbability);
119 archive << BOOST_SERIALIZATION_NVP(m_parents);
120 archive << BOOST_SERIALIZATION_NVP(m_mu);
121 archive << BOOST_SERIALIZATION_NVP(
m_best);
123 archive << BOOST_SERIALIZATION_NVP( m_selection );
124 archive << BOOST_SERIALIZATION_NVP(m_crossover);
125 archive << BOOST_SERIALIZATION_NVP(m_mutation);
126 archive << BOOST_SERIALIZATION_NVP(m_crossoverProbability);
131 if(!
function.canProposeStartingPoint())
132 throw SHARKEXCEPTION(
"[RealCodedNSGAII::init] Objective function does not propose a starting point");
133 std::vector<RealVector> points(
mu());
134 for(std::size_t i = 0; i !=
mu(); ++i){
135 points[i] =
function.proposeStartingPoint();
137 init(
function,points);
147 std::vector<SearchPointType>
const& initialSearchPoints
150 std::vector<RealVector> values(initialSearchPoints.size());
151 for(std::size_t i = 0; i != initialSearchPoints.size(); ++i){
152 if(!
function.isFeasible(initialSearchPoints[i]))
153 throw SHARKEXCEPTION(
"[RealCodedNSGAII::init] starting point(s) not feasible");
154 values[i] =
function.eval(initialSearchPoints[i]);
157 std::size_t dim =
function.numberOfVariables();
158 RealVector lowerBounds(dim, -1E20);
159 RealVector upperBounds(dim, 1E20);
160 if (
function.hasConstraintHandler() &&
function.getConstraintHandler().isBoxConstrained()) {
162 ConstraintHandler
const& handler =
static_cast<ConstraintHandler const&
>(
function.getConstraintHandler());
164 lowerBounds = handler.
lower();
165 upperBounds = handler.upper();
167 throw SHARKEXCEPTION(
"[RealCodedNSGAII::init] Algorithm does only allow box constraints");
181 penalizingEvaluator(
function, offspring.begin(), offspring.end() );
189 std::vector<SearchPointType>
const& initialSearchPoints,
190 std::vector<ResultType>
const& functionValues,
191 RealVector
const& lowerBounds,
192 RealVector
const& upperBounds,
196 double crossover_prob
202 m_crossoverProbability = crossover_prob;
204 m_parents.resize( mu );
206 std::size_t numPoints = 0;
207 if(initialSearchPoints.size()<=
mu){
208 numPoints = initialSearchPoints.size();
209 for(std::size_t i = 0; i != numPoints; ++i){
210 m_parents[i].searchPoint() = initialSearchPoints[i];
211 m_parents[i].penalizedFitness() = functionValues[i];
212 m_parents[i].unpenalizedFitness() = functionValues[i];
216 for(std::size_t i = numPoints; i !=
mu; ++i){
217 std::size_t index =
discrete(*mpe_rng, 0,initialSearchPoints.size()-1);
218 m_parents[i].searchPoint() = initialSearchPoints[index];
219 m_parents[i].penalizedFitness() = functionValues[index];
220 m_parents[i].unpenalizedFitness() = functionValues[index];
223 for(std::size_t i = 0; i !=
mu; ++i){
224 m_best[i].point = m_parents[i].searchPoint();
225 m_best[i].value = m_parents[i].unpenalizedFitness();
227 m_selection( m_parents, mu );
229 m_crossover.
init(lowerBounds,upperBounds);
230 m_mutation.
init(lowerBounds,upperBounds);
235 std::vector<IndividualType> offspring(
mu());
244 for( std::size_t i = 0; i <
mu()-1; i+=2 ) {
245 if(
coinToss(*mpe_rng, m_crossoverProbability ) ) {
246 m_crossover(*mpe_rng,offspring[i], offspring[i +1] );
249 for( std::size_t i = 0; i <
mu(); i++ ) {
250 m_mutation(*mpe_rng, offspring[i] );
261 m_parents.insert(m_parents.end(),offspringVec.begin(),offspringVec.end());
262 m_selection( m_parents,
mu());
266 m_parents.erase(m_parents.begin()+
mu(),m_parents.end());
269 for (std::size_t i = 0; i <
mu(); i++) {
271 m_best[i].value = m_parents[i].unpenalizedFitness();
276 std::vector<IndividualType> m_parents;
284 double m_crossoverProbability;