OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
SampleIndividual.h
Go to the documentation of this file.
1 #ifndef __SAMPLE_INDIVIDUAL_H__
2 #define __SAMPLE_INDIVIDUAL_H__
3 
4 #include <algorithm>
5 #include <cmath>
6 #include <iostream>
7 #include <iomanip>
8 #include <utility>
9 #include <vector>
10 
12 
13 
14 #include "boost/smart_ptr.hpp"
15 
24 
25 public:
26 
28  typedef std::vector<double> genes_t;
30  typedef std::vector<std::string> names_t;
32  typedef std::vector<double> objectives_t;
33 
35  {}
36 
38  : names_m(names)
39  {
40  genes.resize(names.size(), 0.0);
41  }
42 
44  template<class Archive>
45  void serialize(Archive & ar, const unsigned int version) {
46  ar & genes;
47  ar & objectives;
48  ar & id;
49  }
50 
56  unsigned int id = 0;
57 
58  int getIndex(std::string name) {
59  auto res = std::find(std::begin(names_m), std::end(names_m), name);
60 
61  if (res == std::end(names_m)) {
62  throw OpalException("SampleIndividual::getIndex()",
63  "Variable '" + name + "' not contained.");
64  }
65  return std::distance(std::begin(names_m), res);
66  }
67 
68 
69  std::string getName(size_t i) {
70  return names_m[i];
71  }
72 
73  void print(std::ostream &out) const {
74  out << std::setw(8) << id << std::endl;
75  for (unsigned int i = 0; i < genes.size(); ++ i) {
76  out << names_m[i] << ": " << genes[i] << std::endl;
77  }
78  }
79 private:
82 };
83 
84 inline
85 std::ostream & operator<<(std::ostream & out, const SampleIndividual &ind) {
86  ind.print(out);
87 
88  return out;
89 }
90 #endif
std::ostream & operator<<(std::ostream &os, const Attribute &attr)
Definition: Attribute.cpp:167
std::vector< std::string > names_t
gene names
unsigned int id
id
The base class for all OPAL exceptions.
Definition: OpalException.h:28
std::vector< double > objectives_t
objectives array
void print(std::ostream &out) const
int version
opal version of input file
Definition: Options.cpp:97
int getIndex(std::string name)
std::vector< double > genes_t
representation of genes
SampleIndividual(names_t names)
genes_t genes
genes of an individual
const T * find(const T table[], const std::string &name)
Look up name.
Definition: TFind.h:34
std::string getName(size_t i)
objectives_t objectives
values of objectives of an individual
void serialize(Archive &ar, const unsigned int version)
serialization of structure
const std::string name
names_t names_m
gene names
Inform & endl(Inform &inf)
Definition: Inform.cpp:42