-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIndividual.hpp
579 lines (497 loc) · 18.5 KB
/
Individual.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//
// Individual.h
// parallel-nsgaII-backend
//
// Created by a1091793 on 18/11/2015.
// Copyright © 2015 University of Adelaide. All rights reserved.
//
#ifndef Individual_h
#define Individual_h
#include <vector>
#include "ProblemDefinitions.hpp"
#include <boost/archive/tmpdir.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/foreach.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/home/support/common_terminals.hpp>
class Individual;
typedef Individual * IndividualPtr;
typedef boost::shared_ptr<Individual> IndividualSPtr;
class Population;
typedef boost::shared_ptr<Population> PopulationSPtr;
typedef Population Front;
typedef PopulationSPtr FrontSPtr;
typedef std::vector<Front> Fronts;
typedef boost::shared_ptr<Fronts > FrontsSPtr;
inline FrontsSPtr debNonDominatedSort(const Population& population_ref);
typedef std::vector<std::pair<IndividualSPtr, double > > IndividualsWithCrowdingDistanceVec;
inline IndividualsWithCrowdingDistanceVec calculateDebsCrowdingDistance(const Front& front_set);
class EvaluatePopulationBase;
class Individual
{
friend FrontsSPtr debNonDominatedSort(const Population& population_ref);
friend IndividualsWithCrowdingDistanceVec calculateDebsCrowdingDistance(const Front& front_set);
friend class EvaluatePopulation;
friend class ParallelEvaluatePopServerNonBlocking;
template <typename RNG> friend class ParallelEvaluatePopServerNonBlockingContinuousEvolution;
friend class ParallelEvaluatorServerBase;
template <typename RNG> friend class CreepMutation;
template <typename RNG> friend class UniformIntMutation;
template <typename RNG> friend class DebsPolynomialMutation;
template <typename RNG> friend class OnePointCrossover;
template <typename RNG> friend class DebsSBXCrossover;
public:
typedef ProblemDefinitions::RealDVsT RealDVsT;
typedef ProblemDefinitions::UnorderedDVsT UnorderedDVsT;
typedef ProblemDefinitions::OrderedDVsT OrderedDVsT;
typedef ProblemDefinitions::ObjectivesT ObjectivesT;
typedef ProblemDefinitions::ConstraintsT ConstraintsT;
typedef ProblemDefinitions::MinOrMaxType MinOrMaxType;
typedef ProblemDefinitions::ObjectivesDirectionT ObjectivesDirectionT;
typedef ProblemDefinitions::ObjectivesAndConstraintsT ObjectivesAndConstraintsT;
private:
ProblemDefinitionsSPtr definitions;
RealDVsT real_decision_variables;
UnorderedDVsT unordered_dvs;
OrderedDVsT ordered_dvs;
ObjectivesT objectives;
ConstraintsT constraints;
int rank;
double crowding_score;
public:
// Individual(DecisionVariableType dv_type, int number_of_decision_variables)
// Individual(int number_of_real_decision_variables, int number_of_int_decision_variables = 0)
// : definitions(number_of_real_decision_variables, number_of_int_decision_variables)
// {
//
// }
//
// Individual(std::vector<double> & _real_lowerbounds, std::vector<double> & _real_upperbounds,
// std::vector<int> & _int_lowerbounds, std::vector<int> & _int_upperbounds, std::vector<MinOrMaxType> _min_or_max_vec)
// : definitions(_real_lowerbounds, _real_upperbounds, _int_lowerbounds, _int_upperbounds, _min_or_max_vec)
// {
//
// }
//
// Individual(std::vector<double> & _real_lowerbounds, std::vector<double> & _real_upperbounds,
// std::vector<int> & _int_lowerbounds, std::vector<int> & _int_upperbounds, std::vector<MinOrMaxType> _min_or_max_vec,
// std::initializer_list<double> _init_real_dv_list, std::initializer_list<int> _init_int_dv_list)
// : definitions(_real_lowerbounds, _real_upperbounds, _int_lowerbounds, _int_upperbounds, _min_or_max_vec),
// real_decision_variables(_init_real_dv_list), int_decision_variables(_init_int_dv_list)
// {
//
// }
Individual(const Individual & cpy)
: definitions(cpy.definitions),
real_decision_variables(cpy.real_decision_variables),
unordered_dvs(cpy.unordered_dvs),
ordered_dvs(cpy.ordered_dvs),
objectives(cpy.objectives),
constraints(cpy.constraints),
rank(cpy.rank),
crowding_score(cpy.crowding_score)//, mutated(false), crossovered(false), child(false), parent(false)
{
}
Individual(ProblemDefinitionsSPtr defs)
: definitions(defs),
real_decision_variables(defs->real_lowerbounds.size()),
unordered_dvs(defs->unordered_lowerbounds.size()),
ordered_dvs(defs->ordered_lowerbounds.size()),
objectives(defs->minimise_or_maximise.size()),
constraints(defs->number_constraints),
rank(std::numeric_limits<int>::max()),
crowding_score(std::numeric_limits<double>::min())//, mutated(false), crossovered(false), child(false), parent(false)
{
}
Individual(std::string & s, ProblemDefinitionsSPtr defs)
: definitions(defs),
real_decision_variables(defs->real_lowerbounds.size()),
unordered_dvs(defs->unordered_lowerbounds.size()),
objectives(defs->minimise_or_maximise.size()),
ordered_dvs(defs->ordered_lowerbounds.size()),
constraints(defs->number_constraints),
rank(std::numeric_limits<int>::max()),
crowding_score(std::numeric_limits<double>::min())//, mutated(false), crossovered(false), child(false), parent(false)
{
parse(s, defs.get());
}
Individual(std::string & s, ProblemDefinitions * defs)
: definitions(defs),
real_decision_variables(defs->real_lowerbounds.size()),
unordered_dvs(defs->unordered_lowerbounds.size()),
ordered_dvs(defs->ordered_lowerbounds.size()),
objectives(defs->minimise_or_maximise.size()),
constraints(defs->number_constraints),
rank(std::numeric_limits<int>::max()),
crowding_score(std::numeric_limits<double>::min())//, mutated(false), crossovered(false), child(false), parent(false)
{
parse(s, defs);
}
void
parse(std::string& s, ProblemDefinitions* defs)
{
//Format of population seeding file:
//[int_dv1 int_dv2 ... ; real_dv1 real_dv2 ..... ] -> (obj1 obj2 ...; cnstr1 cnstr2....)
//[int_dv1 int_dv2 ... ; real_dv1 real_dv2 ..... ] -> (obj1 obj2 ...; cnstr1 cnstr2....)
//[int_dv1 int_dv2 ... ; real_dv1 real_dv2 ..... ] -> (obj1 obj2 ...; cnstr1 cnstr2....)
//[int_dv1 int_dv2 ... ; real_dv1 real_dv2 ..... ] -> (obj1 obj2 ...; cnstr1 cnstr2....)
//[int_dv1 int_dv2 ... ; real_dv1 real_dv2 ..... ] -> (obj1 obj2 ...; cnstr1 cnstr2....)
//[int_dv1 int_dv2 ... ; real_dv1 real_dv2 ..... ] -> (obj1 obj2 ...; cnstr1 cnstr2....)
//[int_dv1 int_dv2 ... ; real_dv1 real_dv2 ..... ] -> (obj1 obj2 ...; cnstr1 cnstr2....)
//[int_dv1 int_dv2 ... ; real_dv1 real_dv2 ..... ] -> (obj1 obj2 ...; cnstr1 cnstr2....)
//[int_dv1 int_dv2 ... ; real_dv1 real_dv2 ..... ] -> (obj1 obj2 ...; cnstr1 cnstr2....)
//[int_dv1 int_dv2 ... ; real_dv1 real_dv2 ..... ] -> (obj1 obj2 ...; cnstr1 cnstr2....)
//[int_dv1 int_dv2 ... ; real_dv1 real_dv2 ..... ] -> (obj1 obj2 ...; cnstr1 cnstr2....)
namespace qi = boost::spirit::qi;
namespace ph = boost::phoenix;
qi::rule<std::string::iterator, std::vector<int>(), qi::space_type> int_vec_parser = *qi::int_;
qi::rule<std::string::iterator, std::vector<double>(), qi::space_type> real_vec_parser = *qi::double_;
qi::rule<std::string::iterator, qi::space_type> ind_parser =
qi::lit('[') >>
int_vec_parser[ph::ref(unordered_dvs) = qi::_1] >>
qi::lit(';') >>
int_vec_parser[ph::ref(ordered_dvs) = qi::_1] >>
qi::lit(';') >>
real_vec_parser[ph::ref(real_decision_variables) = qi::_1] >>
qi::lit(']')
>> -(
qi::lit("->")
>> qi::lit('(')
>> real_vec_parser[ph::ref(objectives) = qi::_1]
>> qi::lit(';')
>> real_vec_parser[ph::ref(constraints) = qi::_1]
>> qi::lit(')')
)
>> -(qi::lit("Rank:") >> qi::int_[ph::ref(rank) = qi::_1])
>> -(qi::lit("CrowdingDist:") >> qi::double_[ph::ref(crowding_score) = qi::_1])
;
ind_parser.name("individual_parser");
int_vec_parser.name("int_vec_parser");
real_vec_parser.name("real_vec_parser");
// qi::debug(int_vec_parser);
// qi::debug(real_vec_parser);
// qi::debug(ind_parser);
std::string::iterator it = s.begin();
std::string::iterator end = s.end();
bool r = boost::spirit::qi::phrase_parse(it, end, ind_parser, qi::space);
if (!(r && it == end))
{
std::string rest(it, end);
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "stopped at: \"" << rest << "\"\n";
std::cout << "-------------------------\n";
}
if (real_decision_variables.size() < numOfRealDVs())
{
std::cerr << "Check specification - read in " << real_decision_variables.size() << "; However, problem specification indicated there would be " << numOfRealDVs() << " real decision variables." << std::endl;
real_decision_variables.resize(numOfRealDVs());
}
if (unordered_dvs.size() < numOfUnorderedDVs())
{
std::cerr << "Check specification - read in " << unordered_dvs.size() << "; However, problem specification indicated there would be " << numOfUnorderedDVs() << " integer decision variables." << std::endl;
unordered_dvs.resize(numOfUnorderedDVs());
}
if (ordered_dvs.size() < numOfOrderedDVs())
{
std::cerr << "Check specification - read in " << ordered_dvs.size() << "; However, problem specification indicated there would be " << numOfOrderedDVs() << " integer decision variables." << std::endl;
ordered_dvs.resize(numOfOrderedDVs());
}
for (RealDVsT::size_type j = 0; j < real_decision_variables.size(); ++j)
{
if (real_decision_variables[j] < defs->real_lowerbounds[j])
{
std::cerr << "input real decision variable " << real_decision_variables[j] << " at place " << j << " out of bounds; setting to lower bound which is " << defs->real_lowerbounds[j] << std::endl;
real_decision_variables[j] = defs->real_lowerbounds[j];
}
if (real_decision_variables[j] > defs->real_upperbounds[j])
{
std::cerr << "input real decision variable " << real_decision_variables[j] << " at place " << j << " out of bounds; setting to upper bound which is " << defs->real_upperbounds[j] << std::endl;
real_decision_variables[j] = defs->real_upperbounds[j];
}
}
for (UnorderedDVsT::size_type j = 0; j < unordered_dvs.size(); ++j)
{
if (unordered_dvs[j] < defs->unordered_lowerbounds[j])
{
std::cerr << "input unordered decision variable " << unordered_dvs[j] << " at place " << j << " out of bounds; setting to lower bound which is " << defs->unordered_lowerbounds[j] << std::endl;
unordered_dvs[j] = defs->unordered_lowerbounds[j];
}
if (unordered_dvs[j] > defs->unordered_upperbounds[j])
{
std::cerr << "input unordered decision variable " << unordered_dvs[j] << " at place " << j << " out of bounds; setting to upper bound which is " << defs->unordered_upperbounds[j] << std::endl;
unordered_dvs[j] = defs->unordered_upperbounds[j];
}
}
for (OrderedDVsT::size_type j = 0; j < ordered_dvs.size(); ++j)
{
if (ordered_dvs[j] < defs->ordered_lowerbounds[j])
{
std::cerr << "input ordered decision variable " << ordered_dvs[j] << " at place " << j << " out of bounds; setting to lower bound which is " << defs->ordered_lowerbounds[j] << std::endl;
ordered_dvs[j] = defs->ordered_lowerbounds[j];
}
if (ordered_dvs[j] > defs->ordered_upperbounds[j])
{
std::cerr << "input ordered decision variable " << ordered_dvs[j] << " at place " << j << " out of bounds; setting to upper bound which is " << defs->ordered_upperbounds[j] << std::endl;
ordered_dvs[j] = defs->ordered_upperbounds[j];
}
}
if (objectives.size() < numOfObjectives()) objectives.resize(numOfObjectives());
if (constraints.size() < numOfConstraints()) constraints.resize(numOfConstraints());
}
Individual()
{
}
// Should not be able to change problem definitions. THese should be set at construction and not changed, else behaviour could be weird?
//void
//setProblemDefinitions(ProblemDefinitionsSPtr defs)
//{
// definitions.swap(defs);
//}
Individual &
operator= ( const Individual & orig)
{
this->definitions = orig.definitions;
this->real_decision_variables = orig.real_decision_variables;
this->unordered_dvs = orig.unordered_dvs;
this->ordered_dvs = orig.ordered_dvs;
this->objectives = orig.objectives;
this->constraints = orig.constraints;
this->rank = orig.rank;
this->crowding_score = orig.crowding_score;
return (*this);
}
const RealDVsT &
getRealDVVector() const
{
return (real_decision_variables);
}
const UnorderedDVsT &
getUnorderedDVVector() const
{
return (unordered_dvs);
}
const OrderedDVsT &
getOrderedDVVector() const
{
return (unordered_dvs);
}
const ObjectivesT &
getObjectives() const
{
return objectives;
}
const ConstraintsT &
getConstraints() const
{
return constraints;
}
const MinOrMaxType &
isMinimiseOrMaximise(const ProblemDefinitions::ObjectivesDirectionT::size_type index) const
{
return (definitions->minimise_or_maximise[index]);
}
const double &
getRealDV(const RealDVsT::size_type index) const
{
return (real_decision_variables[index]);
}
const int &
getUnorderedDV(const UnorderedDVsT::size_type index) const
{
return (unordered_dvs[index]);
}
const int&
getOrderedDV(const OrderedDVsT::size_type index) const
{
return (ordered_dvs[index]);
}
const double & getObjective(ObjectivesT::size_type index) const
{
return (objectives[index]);
}
const double & getConstraint(ConstraintsT::size_type index) const
{
return (constraints[index]);
}
const int getRank(void) const
{
return (rank);
}
const double getCrowdingScore(void) const
{
return (crowding_score);
}
const double & getRealUpperBound(RealDVsT::size_type index) const
{
return (definitions->real_upperbounds[index]);
}
const double & getRealLowerBound(const RealDVsT::size_type index) const
{
return (definitions->real_lowerbounds[index]);
}
const int getUnorderedUpperBound(const UnorderedDVsT::size_type index) const
{
return (definitions->unordered_upperbounds[index]);
}
const int getUnorderedLowerBound(const UnorderedDVsT::size_type index) const
{
return (definitions->unordered_lowerbounds[index]);
}
const int getOrderedUpperBound(const OrderedDVsT::size_type index) const
{
return (definitions->ordered_upperbounds[index]);
}
const int getOrderedLowerBound(const OrderedDVsT::size_type index) const
{
return (definitions->ordered_lowerbounds[index]);
}
const RealDVsT::size_type numOfRealDVs() const
{
return (definitions->real_lowerbounds.size());
}
const OrderedDVsT::size_type numOfOrderedDVs() const
{
return (definitions->ordered_lowerbounds.size());
}
const UnorderedDVsT::size_type numOfUnorderedDVs() const
{
return (definitions->unordered_lowerbounds.size());
}
const ObjectivesDirectionT::size_type numOfObjectives() const
{
return (definitions->minimise_or_maximise.size());
}
const unsigned int numOfConstraints() const
{
return (definitions->number_constraints);
}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(real_decision_variables);
ar & BOOST_SERIALIZATION_NVP(unordered_dvs);
ar& BOOST_SERIALIZATION_NVP(ordered_dvs);
ar & BOOST_SERIALIZATION_NVP(objectives);
ar & BOOST_SERIALIZATION_NVP(constraints);
ar & BOOST_SERIALIZATION_NVP(rank);
ar & BOOST_SERIALIZATION_NVP(crowding_score);
ar & BOOST_SERIALIZATION_NVP(definitions);
}
friend std::ostream& operator<<(std::ostream& os, const Individual& Individual);
private:
//ObjectivesAndConstraintsT
// getMutableObjectivesAndConstraints()
//{
// return ObjectivesAndConstraintsT(objectives, constraints);
//};
void setObjectivesAndConstraints(ObjectivesAndConstraintsT objs_and_constraints)
{
setObjectives(objs_and_constraints.first);
setConstraints(objs_and_constraints.second);
}
void setObjective(const int index, const double& value)
{
objectives[index] = value;
}
void
setObjectives(ProblemDefinitions::ObjectivesT objs)
{
objectives = objs;
}
void setConstraint(const int index, const double& value)
{
constraints[index] = value;
}
void
setConstraints(ProblemDefinitions::ConstraintsT cons)
{
constraints = cons;
}
void
setOrderedDV(const int index, const int& val)
{
ordered_dvs[index] = val;
}
void
setOrderedDVs(ProblemDefinitions::OrderedDVsT _ordered_dvs)
{
ordered_dvs = _ordered_dvs;
}
void
setUnorderedDV(const int index, const int& val)
{
unordered_dvs[index] = val;
}
void
setUnorderedDVs(ProblemDefinitions::UnorderedDVsT _unordered_dvs)
{
unordered_dvs = _unordered_dvs;
}
void
setRealDV(const int index, const double& val)
{
real_decision_variables[index] = val;
}
void
setRealDVs(ProblemDefinitions::RealDVsT real_dvs)
{
real_decision_variables = real_dvs;
}
void setCrowdingScore(double score)
{
crowding_score = score;
}
void setRank(const int _rank)
{
rank = _rank;
}
};
inline std::ostream&
operator<<(std::ostream& os, const IndividualSPtr ind)
{
os << *ind;
return os;
}
inline std::ostream&
operator<<(std::ostream& os, const Individual & ind)
{
os << "[ ";
for(const int & idv: ind.getUnorderedDVVector())
{
os << idv << " ";
}
os << "; ";
for(const int& idv: ind.getOrderedDVVector())
{
os << idv << " ";
}
os << "; ";
for (const double & ddv: ind.getRealDVVector())
{
os << ddv << " ";
}
os << "] -> ( ";
for (const double & obj: ind.getObjectives())
{
os << obj << " ";
}
os << "; ";
for (const double & cnstrnt: ind.getConstraints())
{
os << cnstrnt << " ";
}
os << ") ";
os << "Rank: " << ind.getRank() << " CrowdingDist: " << ind.getCrowdingScore();
// if (ind.mutated) os << "\tMutated";
// if (ind.crossovered) os << "\tCrossovered" << std::endl;
// if (ind.child) os << "\tFrom_Child";
// if (ind.parent) os << "\tFrom_Parent" << std::endl;
return os;
}
#endif /* Individual_h */