OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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
35
37
38public:
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 }
110
111 bool viable() {
112 return checkBounds() && checkConstraints();
113 }
114
120 unsigned int id_m = 0;
121
122private:
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 }
155 Expressions::Result_t result =
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 }
167
173};
174
175#endif
176
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
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
Expression Expr_t
type of an expression
Definition Expression.h:63
boost::tuple< double, bool > Result_t
Definition Expression.h:66
Expressions::Result_t evaluate(variableDictionary_t vars)
evaluate an expression given a value dictionary of free variables
Definition Expression.h:133
std::set< std::string > getReqVars() const
Definition Expression.h:124
void serialize(Archive &ar, const unsigned int)
serialization of structure
Definition Individual.h:95
std::vector< std::string > names_t
gene names
Definition Individual.h:43
unsigned int id_m
id
Definition Individual.h:120
genes_t genes_m
genes of an individual
Definition Individual.h:116
names_t names_m
gene names
Definition Individual.h:170
objectives_t objectives_m
values of objectives of an individual
Definition Individual.h:118
bool checkBounds()
check bounds
Definition Individual.h:124
std::vector< std::pair< double, double > > bounds_t
bounds on design variables
Definition Individual.h:47
bool viable()
test if individual within bounds and constraints
Definition Individual.h:111
double new_gene(size_t gene_idx)
Definition Individual.h:103
Individual(bounds_t gene_bounds, names_t names, constraints_t constraints)
create a new individual and initialize with random genes
Definition Individual.h:55
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
std::vector< double > genes_t
representation of genes
Definition Individual.h:41
bool checkConstraints()
check if all constraints on design variables are checked
Definition Individual.h:138
std::vector< double > objectives_t
objectives array
Definition Individual.h:45
Expressions::Named_t constraints_t
constraints
Definition Individual.h:49
Individual(std::shared_ptr< Individual > individual)
copy another individual
Definition Individual.h:84