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-2017 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://shark-ml.org/>
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/Core/Random.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 box-constraints
49  void init( RealVector const& lower, RealVector const& upper ) {
50  SIZE_CHECK(lower.size() == upper.size());
51  m_prob = 1./lower.size();
52  m_lower = lower;
53  m_upper = upper;
54  }
55 
56  /// \brief Mates the supplied individuals.
57  ///
58  /// \param [in,out] i1 Individual to be mated.
59  /// \param [in,out] i2 Individual to be mated.
60  template<class randomType, typename IndividualType>
61  void operator()(randomType& rng, IndividualType & i1, IndividualType & i2 )const{
62  RealVector& point1 = i1.searchPoint();
63  RealVector& point2 = i2.searchPoint();
64 
65  for( unsigned int i = 0; i < point1.size(); i++ ) {
66 
67  if( !random::coinToss(rng, m_prob ) )
68  continue;
69 
70  double y1 = 0;
71  double y2 = 0;
72  if( point2[i] < point1[i] ) {
73  y1 = point2[i];
74  y2 = point1[i];
75  } else {
76  y1 = point1[i];
77  y2 = point2[i];
78  }
79 
80  double betaQ1 = 0.0;
81  double betaQ2 = 0.0;
82  if( std::abs(y2 - y1) < 1E-7 )continue;//equal
83 
84  // Find beta value2
85  double beta1 = 1 + 2 * (y1 - m_lower( i )) / (y2 - y1);
86  double beta2 = 1 + 2 * (m_upper( i ) - y2) / (y2 - y1);
87  double expp = m_nc + 1.;
88  // Find alpha
89  double alpha1 = 2. - std::pow(beta1 , -expp);
90  double alpha2 = 2. - std::pow(beta2 , -expp);
91 
92  double u = random::uni(rng, 0., 1. );
93  alpha1 *=u;
94  alpha2 *=u;
95  if( u > 1. / alpha1 ) {
96  alpha1 = 1. / (2. - alpha1);
97  }
98  if( u > 1. / alpha2 ) {
99  alpha2 = 1. / (2. - alpha2);
100  }
101  betaQ1 = std::pow( alpha1, 1.0/expp );
102  betaQ2 = std::pow( alpha2, 1.0/expp );
103 
104  //recombine points
105  point1[i] = 0.5 * ((y1 + y2) - betaQ1 * (y2 - y1));
106  point2[i] = 0.5 * ((y1 + y2) + betaQ2 * (y2 - y1));
107  // randomly swap loci
108  if( random::coinToss(rng,0.5) ) std::swap(point1[i], point2[i]);
109 
110 
111  // -> from Deb's implementation, not contained in any paper
112  point1[i] = std::max( point1[i], m_lower( i ) );
113  point1[i] = std::min( point1[i], m_upper( i ) );
114  point2[i] = std::max( point2[i], m_lower( i ) );
115  point2[i] = std::min( point2[i], m_upper( i ) );
116  }
117 
118  }
119 
120  /// \brief Serializes this instance to the supplied archive.
121  /// \tparam Archive The type of the archive the instance shall be serialized to.
122  /// \param [in,out] archive The archive to serialize to.
123  /// \param [in] version Version information (optional and not used here).
124  template<typename Archive>
125  void serialize( Archive & archive, const unsigned int version ) {
126  archive & BOOST_SERIALIZATION_NVP( m_nc );
127  archive & BOOST_SERIALIZATION_NVP( m_prob );
128  archive & BOOST_SERIALIZATION_NVP( m_upper );
129  archive & BOOST_SERIALIZATION_NVP( m_lower );
130  }
131 
132  double m_nc; ///< Parameter nc.
133  double m_prob; ///< Crossover probability.
134 
135  RealVector m_upper; ///< Upper bound (box constraint).
136  RealVector m_lower; ///< Lower bound (box constraint).
137  };
138 }
139 
140 #endif