SimulatedBinaryCrossover.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Simulated binary crossover operator.
5  *
6  *
7  *
8  * \author T.Voss
9  * \date 2010
10  *
11  *
12  * \par Copyright 1995-2015 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://image.diku.dk/shark/>
17  *
18  * Shark is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Lesser General Public License as published
20  * by the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * Shark is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 #ifndef SHARK_ALGORITHMS_DIRECT_SEARCH_OPERATORS_CROSSOVER_SBX_H
33 #define SHARK_ALGORITHMS_DIRECT_SEARCH_OPERATORS_CROSSOVER_SBX_H
34 
35 #include <shark/Rng/GlobalRng.h>
37 
38 namespace shark {
39 
40  /// \brief Simulated binary crossover operator.
41  template<typename PointType>
43 
45  : m_nc( 20.0 )
46  , m_prob( 0.5 ) {}
47 
48  /// \brief Initializes the operator for the supplied fitness function.
49  ///
50  /// \param [in] f Instance of the objective function to initialize the operator for.
51  template<typename Function>
52  void init( const Function & f ) {
53  m_prob = 0.5;
54  if(!f.isConstrained()){
55  m_lower = blas::repeat(-1E20,f.numberOfVariables());
56  m_upper = blas::repeat(1E20,f.numberOfVariables());
57  }
58  else if (f.hasConstraintHandler() && f.getConstraintHandler().isBoxConstrained()) {
59  typedef BoxConstraintHandler<PointType> ConstraintHandler;
60  ConstraintHandler const& handler = static_cast<ConstraintHandler const&>(f.getConstraintHandler());
61 
62  m_lower = handler.lower();
63  m_upper = handler.upper();
64 
65  } else{
66  throw SHARKEXCEPTION("[SimulatedBinaryCrossover::init] Algorithm does only allow box constraints");
67  }
68  }
69 
70  /// \brief Mates the supplied individuals.
71  ///
72  /// \param [in,out] i1 Individual to be mated.
73  /// \param [in,out] i2 Individual to be mated.
74  template<typename IndividualType>
76  RealVector& point1 = i1.searchPoint();
77  RealVector& point2 = i2.searchPoint();
78 
79  for( unsigned int i = 0; i < point1.size(); i++ ) {
80 
81  if( !Rng::coinToss( m_prob ) )
82  continue;
83 
84  double y1 = 0;
85  double y2 = 0;
86  if( point2[i] < point1[i] ) {
87  y1 = point2[i];
88  y2 = point1[i];
89  } else {
90  y1 = point1[i];
91  y2 = point2[i];
92  }
93 
94  double betaQ = 0.0;
95  if( std::abs(y2 - y1) > 1E-7 ) {
96  // Find beta value
97  double beta = 0;
98  if( (y1 - m_lower( i )) > (m_upper( i ) - y2) )
99  beta = 1 + 2 * (m_upper( i ) - y2) / (y2 - y1);
100  else
101  beta = 1 + 2 * (y1 - m_lower( i )) / (y2 - y1);
102 
103 
104  double expp = m_nc + 1.;
105  // Find alpha
106  double alpha = 2. - std::pow(1.0/beta , expp);
107  double u = Rng::uni( 0., 1. );
108 
109  if( u <= 1. / alpha ) {
110  alpha *= u;
111  betaQ = std::pow( alpha, 1.0/expp );
112  } else {
113  alpha *= u;
114  alpha = 1. / (2. - alpha);
115  betaQ = std::pow( alpha, 1.0/expp );
116  }
117  } else { // if genes are equal -> from Deb's implementation, not contained in any paper
118  betaQ = 1.;
119  }
120 
121  point1[i] = 0.5 * ((y1 + y2) - betaQ * (y2 - y1));
122  point2[i] = 0.5 * ((y1 + y2) + betaQ * (y2 - y1));
123  // randomly swap loci
124  if( Rng::coinToss() ) std::swap(point1[i], point2[i]);
125 
126 
127  // -> from Deb's implementation, not contained in any paper
128  point1[i] = std::max( point1[i], m_lower( i ) );
129  point1[i] = std::min( point1[i], m_upper( i ) );
130  point2[i] = std::max( point2[i], m_lower( i ) );
131  point2[i] = std::min( point2[i], m_upper( i ) );
132  }
133 
134  }
135 
136  /// \brief Serializes this instance to the supplied archive.
137  /// \tparam Archive The type of the archive the instance shall be serialized to.
138  /// \param [in,out] archive The archive to serialize to.
139  /// \param [in] version Version information (optional and not used here).
140  template<typename Archive>
141  void serialize( Archive & archive, const unsigned int version ) {
142  archive & BOOST_SERIALIZATION_NVP( m_nc );
143  archive & BOOST_SERIALIZATION_NVP( m_prob );
144  archive & BOOST_SERIALIZATION_NVP( m_upper );
145  archive & BOOST_SERIALIZATION_NVP( m_lower );
146  }
147 
148  double m_nc; ///< Parameter nc.
149  double m_prob; ///< Crossover probability.
150 
151  RealVector m_upper; ///< Upper bound (box constraint).
152  RealVector m_lower; ///< Lower bound (box constraint).
153  };
154 }
155 
156 #endif