OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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 <iomanip>
28#include <iostream>
29#include <limits>
30#include <utility>
31#include <vector>
32
34
36
37public:
39 typedef std::vector<double> genes_t;
41 typedef std::vector<std::string> names_t;
43 typedef std::vector<double> objectives_t;
44
47
49 : names_m(names)
50 {
51 genes.resize(names.size(), 0.0);
52 }
53
55 void writeState(std::ostream& os) const {
56 os << std::setprecision(std::numeric_limits<double>::max_digits10);
57 os << id << ' ' << genes.size();
58 for (double g : genes) os << ' ' << g;
59 os << ' ' << objectives.size();
60 for (double o : objectives) os << ' ' << o;
61 os << '\n';
62 }
63
65 void readState(std::istream& is) {
66 std::size_t ngenes = 0, nobjs = 0;
67 is >> id >> ngenes;
68 genes.resize(ngenes);
69 for (double& g : genes) is >> g;
70 is >> nobjs;
71 objectives.resize(nobjs);
72 for (double& o : objectives) is >> o;
73 }
74
80 unsigned int id = 0;
81
82 int getIndex(std::string name) {
83 auto res = std::find(std::begin(names_m), std::end(names_m), name);
84
85 if (res == std::end(names_m)) {
86 throw OpalException("SampleIndividual::getIndex()",
87 "Variable '" + name + "' not contained.");
88 }
89 return std::distance(std::begin(names_m), res);
90 }
91
92 std::string getName(std::size_t i) {
93 return names_m[i];
94 }
95
96 void print(std::ostream& out) const {
97 out << std::setw(8) << id << std::endl;
98 for (unsigned int i = 0; i < genes.size(); ++ i) {
99 out << names_m[i] << ": " << genes[i] << std::endl;
100 }
101 }
102private:
105};
106
107inline
108std::ostream& operator<<(std::ostream& out, const SampleIndividual& ind) {
109 ind.print(out);
110
111 return out;
112}
113#endif
std::ostream & operator<<(std::ostream &out, const SampleIndividual &ind)
const std::string name
std::vector< std::string > names_t
gene names
genes_t genes
genes of an individual
names_t names_m
gene names
void readState(std::istream &is)
read this individual's state from a stream, replaces boost::serialization
std::string getName(std::size_t i)
std::vector< double > objectives_t
objectives array
SampleIndividual(names_t names)
int getIndex(std::string name)
void writeState(std::ostream &os) const
write this individual's state to a stream, replaces boost::serialization
std::vector< double > genes_t
representation of genes
objectives_t objectives
values of objectives of an individual
void print(std::ostream &out) const
The base class for all OPAL exceptions.