OPAL (Object Oriented Parallel Accelerator Library)  2024.1
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 class Individual {
37 
38 public:
39 
41  typedef std::vector<double> genes_t;
43  typedef std::vector<std::string> names_t;
45  typedef std::vector<double> objectives_t;
47  typedef std::vector< std::pair<double, double> > bounds_t;
50 
52  {}
53 
55  Individual(bounds_t gene_bounds, names_t names, constraints_t constraints)
56  : bounds_m(gene_bounds)
57  , names_m(names)
58  , constraints_m(constraints)
59  {
60  genes_m.resize(bounds_m.size(), 0.0);
61  objectives_m.resize(bounds_m.size(), 0.0);
62 
63  // names should be equal length to bounds
64  if (names_m.size() != bounds_m.size()) {
65  // shouldn't happen
66  std::cerr << "Individual::Individual(): names not equal length to bounds, shouldn't happen exiting" << std::endl;
67  exit(1);
68  }
69 
70  int iter = 0;
71  while (iter < 100) { // somewhat arbitrary limit
72  for (size_t i=0; i < bounds_m.size(); i++) {
73  new_gene(i);
74  }
75  // check constraints
76  bool allSatisfied = checkConstraints();
77  if (allSatisfied == true) break;
78  // else next try
79  iter++;
80  }
81  }
82 
84  Individual(std::shared_ptr<Individual> individual) {
85  genes_m = genes_t(individual->genes_m);
86  objectives_m = objectives_t(individual->objectives_m);
87  bounds_m = bounds_t(individual->bounds_m);
88  names_m = names_t(individual->names_m);
89  constraints_m = constraints_t(individual->constraints_m);
90  id_m = individual->id_m;
91  }
92 
94  template<class Archive>
95  void serialize(Archive & ar, const unsigned int /*version*/) {
96  ar & genes_m;
97  ar & objectives_m;
98  ar & id_m;
99  }
100 
103  double new_gene(size_t gene_idx) {
104  double max = std::max(bounds_m[gene_idx].first, bounds_m[gene_idx].second);
105  double min = std::min(bounds_m[gene_idx].first, bounds_m[gene_idx].second);
106  double delta = std::abs(max - min);
107  genes_m[gene_idx] = rand() / (RAND_MAX + 1.0) * delta + min;
108  return genes_m[gene_idx];
109  }
111  bool viable() {
112  return checkBounds() && checkConstraints();
113  }
114 
120  unsigned int id_m = 0;
121 
122 private:
124  bool checkBounds() {
125  for (size_t i=0; i < bounds_m.size(); i++) {
126  double value = genes_m[i];
127  double max = std::max(bounds_m[i].first, bounds_m[i].second);
128  double min = std::min(bounds_m[i].first, bounds_m[i].second);
129  bool is_valid = (value >= min && value<= max);
130  if (is_valid == false) {
131  return false;
132  }
133  }
134  return true;
135  }
136 
139  for (auto namedConstraint : constraints_m) {
140  Expressions::Expr_t *constraint = namedConstraint.second;
141  std::set<std::string> req_vars = constraint->getReqVars();
142  variableDictionary_t variable_dictionary;
143  // fill variable dictionary. all required variables should be genes.
144  for (std::string req_var : req_vars) {
145  auto it = std::find(names_m.begin(),names_m.end(),req_var);
146  if (it==names_m.end()) {
147  // should not happen
148  std::cerr << "Individual::checkConstraints(): " << req_var << " is not a design variable" << std::endl;
149  exit(1);
150  }
151  size_t gene_idx = std::distance(names_m.begin(),it);
152  double value = genes_m[gene_idx];
153  variable_dictionary.insert(std::pair<std::string, double>(req_var, value));
154  }
156  constraint->evaluate(variable_dictionary);
157 
158  double evaluation = boost::get<0>(result);
159  bool is_valid = boost::get<1>(result);
160 
161  if (is_valid==false || evaluation==0) {
162  return false;
163  }
164  }
165  return true;
166  }
173 };
174 
175 #endif
176 
std::vector< double > genes_t
representation of genes
Definition: Individual.h:41
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
Definition: LICENSE:43
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
std::vector< double > objectives_t
objectives array
Definition: Individual.h:45
Individual(bounds_t gene_bounds, names_t names, constraints_t constraints)
create a new individual and initialize with random genes
Definition: Individual.h:55
Expressions::Result_t evaluate(variableDictionary_t vars)
evaluate an expression given a value dictionary of free variables
Definition: Expression.h:133
objectives_t objectives_m
values of objectives of an individual
Definition: Individual.h:118
Individual(std::shared_ptr< Individual > individual)
copy another individual
Definition: Individual.h:84
std::vector< std::pair< double, double > > bounds_t
bounds on design variables
Definition: Individual.h:47
std::map< std::string, double > variableDictionary_t
Definition: Expression.h:55
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
double new_gene(size_t gene_idx)
Definition: Individual.h:103
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:76
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
Definition: Expression.h:74
bool checkBounds()
check bounds
Definition: Individual.h:124
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:84
bool viable()
test if individual within bounds and constraints
Definition: Individual.h:111
const T * find(const T table[], const std::string &name)
Look up name.
Definition: TFind.h:34
std::set< std::string > getReqVars() const
Definition: Expression.h:124
unsigned int id_m
id
Definition: Individual.h:120
alter the names
Definition: LICENSE:330
void serialize(Archive &ar, const unsigned int)
serialization of structure
Definition: Individual.h:95
names_t names_m
gene names
Definition: Individual.h:170
bounds_t bounds_m
bounds on each gene
Definition: Individual.h:168
constraints_t constraints_m
constraints that depend only on design variables
Definition: Individual.h:172
float result
Definition: test.py:2
boost::tuple< double, bool > Result_t
Definition: Expression.h:66
std::vector< std::string > names_t
gene names
Definition: Individual.h:43
genes_t genes_m
genes of an individual
Definition: Individual.h:116
bool checkConstraints()
check if all constraints on design variables are checked
Definition: Individual.h:138
Expressions::Named_t constraints_t
constraints
Definition: Individual.h:49