OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
NaiveOnePointCrossover.h
Go to the documentation of this file.
1 //
2 // Struct NaiveOnePointCrossover
3 //
4 // Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
5 // All rights reserved
6 //
7 // Implemented as part of the PhD thesis
8 // "Toward massively parallel multi-objective optimization with application to
9 // particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
10 //
11 // This file is part of OPAL.
12 //
13 // OPAL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
20 //
21 #include "boost/smart_ptr.hpp"
22 #include "Util/CmdArguments.h"
23 
24 template <class T> struct NaiveOnePointCrossover
25 {
26  void crossover(boost::shared_ptr<T> ind1, boost::shared_ptr<T> ind2,
27  CmdArguments_t /*args*/) {
28 
29  typedef typename T::genes_t genes_t;
30  genes_t genes_ind2;
31  genes_ind2 = ind2->genes_m;
32 
33  // determine crossover position u.a.r.
34  size_t position = static_cast<size_t>(
35  ((double) ind1->genes_m.size() * (double) rand() / (RAND_MAX + 1.0))
36  );
37 
38  for(size_t i = position; i < ind1->genes_m.size(); i++) {
39  ind2->genes_m[i] = ind1->genes_m[i];
40  ind1->genes_m[i] = genes_ind2[i];
41  }
42  }
43 };
boost::shared_ptr< CmdArguments > CmdArguments_t
Definition: CmdArguments.h:176
void crossover(boost::shared_ptr< T > ind1, boost::shared_ptr< T > ind2, CmdArguments_t)