OnePointCrossover.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Implements one-point crossover operator.
5  *
6  *
7  * \author T.Voss O.Krause
8  * \date 2010-2011
9  *
10  *
11  * \par Copyright 1995-2017 Shark Development Team
12  *
13  * <BR><HR>
14  * This file is part of Shark.
15  * <http://shark-ml.org/>
16  *
17  * Shark is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Lesser General Public License as published
19  * by the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * Shark is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
29  *
30  */
31 #ifndef SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_RECOMBINATION_ONE_POINT_CROSSOVER_H
32 #define SHARK_ALGORITHMS_DIRECTSEARCH_OPERATORS_RECOMBINATION_ONE_POINT_CROSSOVER_H
33 
34 #include <shark/Core/Random.h>
35 
36 namespace shark {
37 
38 /// \brief Implements one-point crossover.
39 ///
40 /// Given two input points of same size n, draws a random number between 0 and n-1. all variables
41 /// smaller than this index have the value of the left, all elements to the right have the value of the
42 /// right parent.
44  /// \brief Performs the one-point crossover
45  template<class randomType, typename PointType>
46  PointType operator()(randomType& rng, const PointType & mom, const PointType & dad ) {
47  SIZE_CHECK(mom.size() == dad.size());
48  std::size_t point = random::discrete(rng, std::size_t(0), mom.size() - 1 );
49 
50  PointType offspring( mom.size() );
51  std::copy( mom.begin(), mom.begin() + point, offspring.begin() );
52  std::copy( dad.begin() + point, dad.end(), offspring.begin() + point );
53 
54  return offspring ;
55 
56  }
57 };
58 
59 }
60 
61 #endif