PolynomialMutation.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Polynomial mutation 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_MUTATION_POLYNOMIALMUTATION_H
33 #define SHARK_ALGORITHMS_DIRECT_SEARCH_OPERATORS_MUTATION_POLYNOMIALMUTATION_H
34 
36 #include <shark/LinAlg/Base.h>
37 #include <shark/Rng/GlobalRng.h>
38 
39 namespace shark {
40 
41  /// \brief Polynomial mutation operator.
43 
44  /// \brief Default c'tor.
45  PolynomialMutator() : m_nm( 20.0 ) {}
46 
47  /// \brief Initializes the operator for the supplied fitness function.
48  ///
49  /// \param [in] f Instance of the objective function to initialize the operator for.
50  template<typename Function>
51  void init( const Function & f ) {
52  m_prob = 1./f.numberOfVariables();
53  if(!f.isConstrained()){
54  m_lower = blas::repeat(-1E20,f.numberOfVariables());
55  m_upper = blas::repeat(1E20,f.numberOfVariables());
56  }
57  else if (f.hasConstraintHandler() && f.getConstraintHandler().isBoxConstrained()) {
59  ConstraintHandler const& handler = static_cast<ConstraintHandler const&>(f.getConstraintHandler());
60 
61  m_lower = handler.lower();
62  m_upper = handler.upper();
63 
64  } else{
65  throw SHARKEXCEPTION("[PolynomialMutator::init] Algorithm does only allow box constraints");
66  }
67  }
68 
69  /// \brief Mutates the supplied individual.
70  ///
71  /// for accessing the actual search point.
72  /// \param [in,out] ind Individual to be mutated.
73  template<typename IndividualType>
74  void operator()( IndividualType & ind ) {
75  double delta, deltaQ, expp, u = 0.;
76 
77  RealVector& point = ind.searchPoint();
78 
79  for( unsigned int i = 0; i < point.size(); i++ ) {
80 
81  if( Rng::coinToss( m_prob ) ) {
82  u = Rng::uni( 0., 1. );
83  if( point[i] <= m_lower( i ) || point[i] >= m_upper( i ) ) {
84  point[i] = u * (m_upper( i ) - m_lower( i ) ) + m_lower( i );
85  } else {
86  // Calculate delta
87  if( (point[i] - m_lower( i ) ) < (m_upper( i ) - point[i]) )
88  delta = (point[i] - m_lower( i ) ) / (m_upper( i ) - m_lower( i ) );
89  else
90  delta = (m_upper( i ) - point[i]) / (m_upper( i ) - m_lower( i ));
91 
92  delta = 1. - delta;
93  expp = (m_nm + 1.);
94  delta = ::pow(delta , expp);
95  expp = 1. / expp;
96 
97  if( u <= .5 ) {
98  deltaQ = 2. * u + (1 - 2. * u) * delta;
99  deltaQ = ::pow(deltaQ, expp) - 1. ;
100  } else {
101  deltaQ = 2. - 2. * u + 2. * (u - .5) * delta;
102  deltaQ = 1. - ::pow(deltaQ , expp);
103  }
104 
105  point[i] += deltaQ * (m_upper( i ) - m_lower( i ) );
106 
107  // -> from Deb's implementation, not contained in any paper
108  if (point[i] < m_lower( i ))
109  point[i] = m_lower( i );
110 
111  if (point[i] > m_upper( i ))
112  point[i] = m_upper( i );
113 
114  }
115  }
116  }
117  }
118 
119  /// \brief Serializes this instance to the supplied archive.
120  ///
121  /// \param [in,out] archive The archive to serialize to.
122  /// \param [in] version Version information (optional and not used here).
123  template<typename Archive>
124  void serialize( Archive & archive, const unsigned int version ) {
125  archive & BOOST_SERIALIZATION_NVP( m_nm );
126  archive & BOOST_SERIALIZATION_NVP( m_prob );
127  archive & BOOST_SERIALIZATION_NVP( m_upper );
128  archive & BOOST_SERIALIZATION_NVP( m_lower );
129  }
130 
131  double m_nm;
132  double m_prob;
133 
134  RealVector m_upper;
135  RealVector m_lower;
136  };
137 }
138 
139 #endif