OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Individual.h
Go to the documentation of this file.
1 #ifndef __INDIVIDUAL_H__
2 #define __INDIVIDUAL_H__
3 
4 #include <algorithm>
5 #include <cmath>
6 #include <iostream>
7 #include <utility>
8 #include <vector>
9 
10 #include "Expression/Expression.h"
11 
12 #include "boost/smart_ptr.hpp"
13 
21 class Individual {
22 
23 public:
24 
26  typedef std::vector<double> genes_t;
28  typedef std::vector<std::string> names_t;
30  typedef std::vector<double> objectives_t;
32  typedef std::vector< std::pair<double, double> > bounds_t;
35 
37  {}
38 
40  Individual(bounds_t gene_bounds, names_t names, constraints_t constraints)
41  : bounds_m(gene_bounds)
42  , names_m(names)
43  , constraints_m(constraints)
44  {
45  genes_m.resize(bounds_m.size(), 0.0);
46  objectives_m.resize(bounds_m.size(), 0.0);
47 
48  // names should be equal length to bounds
49  if (names_m.size() != bounds_m.size()) {
50  // shouldn't happen
51  std::cerr << "Individual::Individual(): names not equal length to bounds, shouldn't happen exiting" << std::endl;
52  exit(1);
53  }
54 
55  int iter = 0;
56  while (iter < 100) { // somewhat arbitrary limit
57  for (size_t i=0; i < bounds_m.size(); i++) {
58  new_gene(i);
59  }
60  // check constraints
61  bool allSatisfied = checkConstraints();
62  if (allSatisfied == true) break;
63  // else next try
64  iter++;
65  }
66  }
67 
69  Individual(boost::shared_ptr<Individual> individual) {
70  genes_m = genes_t(individual->genes_m);
71  objectives_m = objectives_t(individual->objectives_m);
72  bounds_m = bounds_t(individual->bounds_m);
73  names_m = names_t(individual->names_m);
74  constraints_m = constraints_t(individual->constraints_m);
75  id_m = individual->id_m;
76  }
77 
79  template<class Archive>
80  void serialize(Archive & ar, const unsigned int version) {
81  ar & genes_m;
82  ar & objectives_m;
83  ar & id_m;
84  }
85 
88  double new_gene(size_t gene_idx) {
89  double max = std::max(bounds_m[gene_idx].first, bounds_m[gene_idx].second);
90  double min = std::min(bounds_m[gene_idx].first, bounds_m[gene_idx].second);
91  double delta = std::abs(max - min);
92  genes_m[gene_idx] = rand() / (RAND_MAX + 1.0) * delta + min;
93  return genes_m[gene_idx];
94  }
96  bool viable() {
97  return checkBounds() && checkConstraints();
98  }
99 
105  unsigned int id_m = 0;
106 
107 private:
109  bool checkBounds() {
110  for (size_t i=0; i < bounds_m.size(); i++) {
111  double value = genes_m[i];
112  double max = std::max(bounds_m[i].first, bounds_m[i].second);
113  double min = std::min(bounds_m[i].first, bounds_m[i].second);
114  bool is_valid = (value >= min && value<= max);
115  if (is_valid == false) {
116  return false;
117  }
118  }
119  return true;
120  }
121 
124  for (auto namedConstraint : constraints_m) {
125  Expressions::Expr_t *constraint = namedConstraint.second;
126  std::set<std::string> req_vars = constraint->getReqVars();
127  variableDictionary_t variable_dictionary;
128  // fill variable dictionary. all required variables should be genes.
129  for (std::string req_var : req_vars) {
130  auto it = std::find(names_m.begin(),names_m.end(),req_var);
131  if (it==names_m.end()) {
132  // should not happen
133  std::cerr << "Individual::checkConstraints(): " << req_var << " is not a design variable" << std::endl;
134  exit(1);
135  }
136  size_t gene_idx = std::distance(names_m.begin(),it);
137  double value = genes_m[gene_idx];
138  variable_dictionary.insert(std::pair<std::string, double>(req_var, value));
139  }
140  Expressions::Result_t result =
141  constraint->evaluate(variable_dictionary);
142 
143  double evaluation = boost::get<0>(result);
144  bool is_valid = boost::get<1>(result);
145 
146  if (is_valid==false || evaluation==0) {
147  return false;
148  }
149  }
150  return true;
151  }
158 };
159 
160 #endif
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
objectives_t objectives_m
values of objectives of an individual
Definition: Individual.h:103
bounds_t bounds_m
bounds on each gene
Definition: Individual.h:153
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:123
std::vector< double > genes_t
representation of genes
Definition: Individual.h:26
Expression to be evaluated in the framework.
Definition: Expression.h:75
bool checkBounds()
check bounds
Definition: Individual.h:109
Individual(boost::shared_ptr< Individual > individual)
copy another individual
Definition: Individual.h:69
double new_gene(size_t gene_idx)
Definition: Individual.h:88
unsigned int id_m
id
Definition: Individual.h:105
genes_t genes_m
genes of an individual
Definition: Individual.h:101
boost::tuple< double, bool > Result_t
Definition: Expression.h:37
std::set< std::string > getReqVars() const
Definition: Expression.h:105
int version
opal version of input file
Definition: Options.cpp:97
void serialize(Archive &ar, const unsigned int version)
serialization of structure
Definition: Individual.h:80
const T * find(const T table[], const std::string &name)
Look up name.
Definition: TFind.h:34
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
Definition: Expression.h:45
bool checkConstraints()
check if all constraints on design variables are checked
Definition: Individual.h:123
Expressions::Result_t evaluate(variableDictionary_t vars)
evaluate an expression given a value dictionary of free variables
Definition: Expression.h:114
constraints_t constraints_m
constraints that depend only on design variables
Definition: Individual.h:157
Individual(bounds_t gene_bounds, names_t names, constraints_t constraints)
create a new individual and initialize with random genes
Definition: Individual.h:40
std::map< std::string, double > variableDictionary_t
Definition: Expression.h:26
std::vector< std::string > names_t
gene names
Definition: Individual.h:28
Expressions::Named_t constraints_t
constraints
Definition: Individual.h:34
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:95
names_t names_m
gene names
Definition: Individual.h:155
std::vector< std::pair< double, double > > bounds_t
bounds on design variables
Definition: Individual.h:32
std::vector< double > objectives_t
objectives array
Definition: Individual.h:30
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
bool viable()
test if individual within bounds and constraints
Definition: Individual.h:96