OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
SampleIndividual.h
Go to the documentation of this file.
1 //
2 // Class SampleIndividual
3 // Structure for an individual in the population holding genes values.
4 //
5 // Copyright (c) 2018, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // Yves Ineichen, ETH Zürich
7 // All rights reserved
8 //
9 // Implemented as part of the PhD thesis
10 // "Precise Simulations of Multibunches in High Intensity Cyclotrons"
11 //
12 // This file is part of OPAL.
13 //
14 // OPAL is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
21 //
22 #ifndef __SAMPLE_INDIVIDUAL_H__
23 #define __SAMPLE_INDIVIDUAL_H__
24 
25 #include <algorithm>
26 #include <cmath>
27 #include <iostream>
28 #include <iomanip>
29 #include <utility>
30 #include <vector>
31 
33 
34 
35 #include "boost/smart_ptr.hpp"
36 
38 
39 public:
40 
42  typedef std::vector<double> genes_t;
44  typedef std::vector<std::string> names_t;
46  typedef std::vector<double> objectives_t;
47 
49  {}
50 
52  : names_m(names)
53  {
54  genes.resize(names.size(), 0.0);
55  }
56 
58  template<class Archive>
59  void serialize(Archive & ar, const unsigned int /*version*/) {
60  ar & genes;
61  ar & objectives;
62  ar & id;
63  }
64 
70  unsigned int id = 0;
71 
72  int getIndex(std::string name) {
74 
75  if (res == std::end(names_m)) {
76  throw OpalException("SampleIndividual::getIndex()",
77  "Variable '" + name + "' not contained.");
78  }
79  return std::distance(std::begin(names_m), res);
80  }
81 
82 
83  std::string getName(size_t i) {
84  return names_m[i];
85  }
86 
87  void print(std::ostream &out) const {
88  out << std::setw(8) << id << std::endl;
89  for (unsigned int i = 0; i < genes.size(); ++ i) {
90  out << names_m[i] << ": " << genes[i] << std::endl;
91  }
92  }
93 private:
96 };
97 
98 inline
99 std::ostream & operator<<(std::ostream & out, const SampleIndividual &ind) {
100  ind.print(out);
101 
102  return out;
103 }
104 #endif
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
std::ostream & operator<<(std::ostream &out, const SampleIndividual &ind)
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
const std::string name
const T * find(const T table[], const std::string &name)
Look up name.
Definition: TFind.h:34
std::vector< std::string > names_t
gene names
genes_t genes
genes of an individual
unsigned int id
id
names_t names_m
gene names
std::vector< double > objectives_t
objectives array
SampleIndividual(names_t names)
int getIndex(std::string name)
std::vector< double > genes_t
representation of genes
void serialize(Archive &ar, const unsigned int)
serialization of structure
objectives_t objectives
values of objectives of an individual
std::string getName(size_t i)
void print(std::ostream &out) const
The base class for all OPAL exceptions.
Definition: OpalException.h:28