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-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_MUTATION_POLYNOMIALMUTATION_H
33 #define SHARK_ALGORITHMS_DIRECT_SEARCH_OPERATORS_MUTATION_POLYNOMIALMUTATION_H
34 
36 #include <shark/LinAlg/Base.h>
37 #include <shark/Core/Random.h>
38 
39 namespace shark {
40 
41  /// \brief Polynomial mutation operator.
43 
44  /// \brief Default c'tor.
45  PolynomialMutator() : m_nm( 25.0 ) {}
46 
47  /// \brief Initializes the operator for the supplied box-constraints
48  void init( RealVector const& lower, RealVector const& upper ) {
49  SIZE_CHECK(lower.size() == upper.size());
50  m_prob = 1./lower.size();
51  m_lower = lower;
52  m_upper = upper;
53  }
54 
55  /// \brief Mutates the supplied individual.
56  ///
57  /// for accessing the actual search point.
58  /// \param [in,out] ind Individual to be mutated.
59  template<typename IndividualType>
60  void operator()(random::rng_type& rng, IndividualType & ind )const{
61  RealVector& point = ind.searchPoint();
62 
63  for( unsigned int i = 0; i < point.size(); i++ ) {
64 
65  if( random::coinToss(rng, m_prob ) ) {
66  if( point[i] < m_lower( i ) || point[i] > m_upper( i ) ) {
67  point[i] = random::uni(rng,m_lower(i),m_upper(i));
68  } else {
69  // Calculate normalized distance from boundaries
70  double delta1 = (m_upper( i ) - point[i]) / (m_upper( i ) - m_lower( i ));
71  double delta2 = (point[i] - m_lower( i ) ) / (m_upper( i ) - m_lower( i ));
72 
73  //compute change in delta
74  double deltaQ=0;
75  double u = random::uni(rng,0,1);
76  if( u <= .5 ) {
77  double delta = std::pow(delta1 , m_nm + 1.);
78  deltaQ = 2.0 * u + (1.0 - 2.0 * u) * delta;
79  deltaQ = std::pow(deltaQ, 1.0/(m_nm+1.0)) - 1. ;
80  } else {
81  double delta = std::pow(delta2 , m_nm + 1.);
82  deltaQ = 2 * (1- u) + 2. * (u - .5) * delta;
83  deltaQ = 1. - std::pow(deltaQ , 1.0/(m_nm+1.0));
84  }
85 
86  point[i] += deltaQ * (m_upper( i ) - m_lower( i ) );
87 
88  // -> from Deb's implementation, not contained in any paper
89  if (point[i] < m_lower( i ))
90  point[i] = m_lower( i );
91 
92  if (point[i] > m_upper( i ))
93  point[i] = m_upper( i );
94 
95  }
96  }
97  }
98  }
99 
100  /// \brief Serializes this instance to the supplied archive.
101  ///
102  /// \param [in,out] archive The archive to serialize to.
103  /// \param [in] version Version information (optional and not used here).
104  template<typename Archive>
105  void serialize( Archive & archive, const unsigned int version ) {
106  archive & BOOST_SERIALIZATION_NVP( m_nm );
107  archive & BOOST_SERIALIZATION_NVP( m_prob );
108  archive & BOOST_SERIALIZATION_NVP( m_upper );
109  archive & BOOST_SERIALIZATION_NVP( m_lower );
110  }
111 
112  double m_nm;
113  double m_prob;
114 
115  RealVector m_upper;
116  RealVector m_lower;
117  };
118 }
119 
120 #endif