BitflipMutator.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Bit flip 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_BITFLIP_MUTATION_H
33 #define SHARK_ALGORITHMS_DIRECT_SEARCH_OPERATORS_MUTATION_BITFLIP_MUTATION_H
34 
35 #include <shark/Core/Random.h>
36 
37 namespace shark {
38 
39 /// \brief Bitflip mutation operator.
40 ///
41 /// Given a binary vector, a coin is flipped for every element. If it is heads, the element of the vector
42 /// is flipped. By initializing the mutator with the objective function, this strength is set to a dfault value
43 /// of 1/dimensions, thus in the mean one element is flipped.
45 
46  /// \brief Default c'tor.
48 
49  /// \brief Initializes the operator for the supplied fitness function.
50  ///
51  /// \param [in] f Instance of the objective function to initialize the operator for.
52  template<typename Function>
53  void init(const Function &f) {
54  m_mutationStrength = 1./f.numberOfVariables();
55  }
56 
57  /// \brief Mutates the supplied individual.
58  ///
59  /// \param [in,out] ind Individual to be mutated.
60  template<typename Rng, typename IndividualType>
61  void operator()(Rng& rng, IndividualType &ind) {
62 
63  for (unsigned int i = 0; i < ind.searchPoint().size(); i++) {
65  ind.searchPoint()[ i ] = !ind.searchPoint()[ i ];
66  }
67  }
68  }
69 
70  /// \brief Serializes this instance to the supplied archive.
71  ///
72  /// \param [in,out] archive The archive to serialize to.
73  /// \param [in] version Version information (optional and not used here).
74  template<typename Archive>
75  void serialize(Archive &archive, const unsigned int version) {
76  archive &m_mutationStrength;
77  }
78 
80 };
81 }
82 
83 #endif