Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
Algorithms
DirectSearch
Operators
Mutation
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
* <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_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.
44
struct
BitflipMutator
{
45
46
/// \brief Default c'tor.
47
BitflipMutator
() :
m_mutationStrength
(0) {}
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] rng Random number generator.
60
/// \param [in,out] ind Individual to be mutated.
61
template
<
typename
Rng,
typename
Indiv
id
ualType>
62
void
operator()
(Rng& rng,
IndividualType
&ind) {
63
64
for
(
unsigned
int
i = 0; i < ind.
searchPoint
().size(); i++) {
65
if
(
random::coinToss
(rng,
m_mutationStrength
)) {
66
ind.
searchPoint
()[ i ] = !ind.
searchPoint
()[ i ];
67
}
68
}
69
}
70
71
/// \brief Serializes this instance to the supplied archive.
72
///
73
/// \param [in,out] archive The archive to serialize to.
74
/// \param [in] version Version information (optional and not used here).
75
template
<
typename
Archive>
76
void
serialize
(Archive &archive,
const
unsigned
int
version) {
77
archive &
m_mutationStrength
;
78
}
79
80
double
m_mutationStrength
;
81
};
82
}
83
84
#endif