OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Individual.h
Go to the documentation of this file.
1 //
2 // Class Individual
3 // Structure for an individual in the population holding genes and objective
4 // values.
5 //
6 // @see Types.h
7 //
8 // Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
9 // All rights reserved
10 //
11 // Implemented as part of the PhD thesis
12 // "Toward massively parallel multi-objective optimization with application to
13 // particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
14 //
15 // This file is part of OPAL.
16 //
17 // OPAL is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
24 //
25 #ifndef __INDIVIDUAL_H__
26 #define __INDIVIDUAL_H__
27 
28 #include <algorithm>
29 #include <cmath>
30 #include <iostream>
31 #include <utility>
32 #include <vector>
33 
34 #include "Expression/Expression.h"
35 
36 #include "boost/smart_ptr.hpp"
37 
38 class Individual {
39 
40 public:
41 
43  typedef std::vector<double> genes_t;
45  typedef std::vector<std::string> names_t;
47  typedef std::vector<double> objectives_t;
49  typedef std::vector< std::pair<double, double> > bounds_t;
52 
54  {}
55 
57  Individual(bounds_t gene_bounds, names_t names, constraints_t constraints)
58  : bounds_m(gene_bounds)
59  , names_m(names)
60  , constraints_m(constraints)
61  {
62  genes_m.resize(bounds_m.size(), 0.0);
63  objectives_m.resize(bounds_m.size(), 0.0);
64 
65  // names should be equal length to bounds
66  if (names_m.size() != bounds_m.size()) {
67  // shouldn't happen
68  std::cerr << "Individual::Individual(): names not equal length to bounds, shouldn't happen exiting" << std::endl;
69  exit(1);
70  }
71 
72  int iter = 0;
73  while (iter < 100) { // somewhat arbitrary limit
74  for (size_t i=0; i < bounds_m.size(); i++) {
75  new_gene(i);
76  }
77  // check constraints
78  bool allSatisfied = checkConstraints();
79  if (allSatisfied == true) break;
80  // else next try
81  iter++;
82  }
83  }
84 
86  Individual(boost::shared_ptr<Individual> individual) {
87  genes_m = genes_t(individual->genes_m);
88  objectives_m = objectives_t(individual->objectives_m);
89  bounds_m = bounds_t(individual->bounds_m);
90  names_m = names_t(individual->names_m);
91  constraints_m = constraints_t(individual->constraints_m);
92  id_m = individual->id_m;
93  }
94 
96  template<class Archive>
97  void serialize(Archive & ar, const unsigned int /*version*/) {
98  ar & genes_m;
99  ar & objectives_m;
100  ar & id_m;
101  }
102 
105  double new_gene(size_t gene_idx) {
106  double max = std::max(bounds_m[gene_idx].first, bounds_m[gene_idx].second);
107  double min = std::min(bounds_m[gene_idx].first, bounds_m[gene_idx].second);
108  double delta = std::abs(max - min);
109  genes_m[gene_idx] = rand() / (RAND_MAX + 1.0) * delta + min;
110  return genes_m[gene_idx];
111  }
113  bool viable() {
114  return checkBounds() && checkConstraints();
115  }
116 
122  unsigned int id_m = 0;
123 
124 private:
126  bool checkBounds() {
127  for (size_t i=0; i < bounds_m.size(); i++) {
128  double value = genes_m[i];
129  double max = std::max(bounds_m[i].first, bounds_m[i].second);
130  double min = std::min(bounds_m[i].first, bounds_m[i].second);
131  bool is_valid = (value >= min && value<= max);
132  if (is_valid == false) {
133  return false;
134  }
135  }
136  return true;
137  }
138 
141  for (auto namedConstraint : constraints_m) {
142  Expressions::Expr_t *constraint = namedConstraint.second;
143  std::set<std::string> req_vars = constraint->getReqVars();
144  variableDictionary_t variable_dictionary;
145  // fill variable dictionary. all required variables should be genes.
146  for (std::string req_var : req_vars) {
147  auto it = std::find(names_m.begin(),names_m.end(),req_var);
148  if (it==names_m.end()) {
149  // should not happen
150  std::cerr << "Individual::checkConstraints(): " << req_var << " is not a design variable" << std::endl;
151  exit(1);
152  }
153  size_t gene_idx = std::distance(names_m.begin(),it);
154  double value = genes_m[gene_idx];
155  variable_dictionary.insert(std::pair<std::string, double>(req_var, value));
156  }
157  Expressions::Result_t result =
158  constraint->evaluate(variable_dictionary);
159 
160  double evaluation = boost::get<0>(result);
161  bool is_valid = boost::get<1>(result);
162 
163  if (is_valid==false || evaluation==0) {
164  return false;
165  }
166  }
167  return true;
168  }
175 };
176 
177 #endif
178 
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:84
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:76
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::map< std::string, double > variableDictionary_t
Definition: Expression.h:55
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
Definition: Expression.h:74
const T * find(const T table[], const std::string &name)
Look up name.
Definition: TFind.h:34
boost::tuple< double, bool > Result_t
Definition: Expression.h:66
std::set< std::string > getReqVars() const
Definition: Expression.h:124
Expressions::Result_t evaluate(variableDictionary_t vars)
evaluate an expression given a value dictionary of free variables
Definition: Expression.h:133
void serialize(Archive &ar, const unsigned int)
serialization of structure
Definition: Individual.h:97
std::vector< std::string > names_t
gene names
Definition: Individual.h:45
unsigned int id_m
id
Definition: Individual.h:122
genes_t genes_m
genes of an individual
Definition: Individual.h:118
names_t names_m
gene names
Definition: Individual.h:172
objectives_t objectives_m
values of objectives of an individual
Definition: Individual.h:120
bool checkBounds()
check bounds
Definition: Individual.h:126
std::vector< std::pair< double, double > > bounds_t
bounds on design variables
Definition: Individual.h:49
bool viable()
test if individual within bounds and constraints
Definition: Individual.h:113
double new_gene(size_t gene_idx)
Definition: Individual.h:105
Individual(bounds_t gene_bounds, names_t names, constraints_t constraints)
create a new individual and initialize with random genes
Definition: Individual.h:57
bounds_t bounds_m
bounds on each gene
Definition: Individual.h:170
constraints_t constraints_m
constraints that depend only on design variables
Definition: Individual.h:174
Individual(boost::shared_ptr< Individual > individual)
copy another individual
Definition: Individual.h:86
std::vector< double > genes_t
representation of genes
Definition: Individual.h:43
bool checkConstraints()
check if all constraints on design variables are checked
Definition: Individual.h:140
std::vector< double > objectives_t
objectives array
Definition: Individual.h:47
Expressions::Named_t constraints_t
constraints
Definition: Individual.h:51