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