-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPopulation.hpp
298 lines (253 loc) · 7.49 KB
/
Population.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
//
// Population.h
// parallel-nsgaII-backend
//
// Created by a1091793 on 18/11/2015.
// Copyright © 2015 University of Adelaide. All rights reserved.
//
#ifndef Population_h
#define Population_h
#include <vector>
#include <fstream>
#include <random>
#include <boost/filesystem.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include "Individual.hpp"
#include "Comparator.hpp"
class Population : public std::vector<IndividualSPtr>
{
private:
FrontsSPtr front_sets;
bool valid_obj_and_constraints;
bool valid_fronts;
public:
Population() :
valid_obj_and_constraints(false), valid_fronts(false)
{
}
void
calcFronts()
{
front_sets = debNonDominatedSort(*this);
valid_fronts = true;
}
FrontsSPtr
getFronts()
{
if (!valid_fronts)
{
front_sets = debNonDominatedSort(*this);
valid_fronts = true;
}
return (front_sets);
}
Population(int population_size, ProblemDefinitionsSPtr defs):
valid_obj_and_constraints(false), valid_fronts(false)
{
this->reserve(population_size);
for (int i = 0; i < population_size; ++i)
{
std::vector<IndividualSPtr>::push_back(IndividualSPtr(new Individual(defs)));
}
}
Population(boost::filesystem::path file, ProblemDefinitionsSPtr defs):
valid_obj_and_constraints(false), valid_fronts(false)
{
if (!boost::filesystem::exists(file))
{
std::cerr << "Cannot construct pop from " << file << "; File does not exisit\n";
}
else
{
std::ifstream fs(file.string().c_str());
if (fs.is_open())
{
std::string line;
while (std::getline(fs, line)) {
std::vector<IndividualSPtr>::push_back(IndividualSPtr(new Individual(line, defs)));
}
}
else
{
std::cerr << "Cannot construct pop from " << file << "; File could not be openned\n";
}
}
}
Population(boost::filesystem::path file, ProblemDefinitions * defs):
valid_obj_and_constraints(false), valid_fronts(false)
{
if (!boost::filesystem::exists(file))
{
std::cerr << "Cannot construct pop from " << file << "; File does not exisit\n";
}
else
{
std::ifstream fs(file.string().c_str());
if (fs.is_open())
{
std::string line;
while (std::getline(fs, line)) {
std::vector<IndividualSPtr>::push_back(IndividualSPtr(new Individual(line, defs)));
}
}
else
{
std::cerr << "Cannot construct pop from " << file << "; File could not be openned\n";
}
}
}
void
append(const Population & appending_pop)
{
this->insert(this->end(), appending_pop.begin(), appending_pop.end());
valid_fronts = false;
}
const std::vector<IndividualSPtr>::size_type
populationSize() const
{
return (this->size());
}
void
push_back(IndividualSPtr ind)
{
std::vector<IndividualSPtr>::push_back(ind);
valid_fronts = false;
}
void
invalidate()
{
valid_obj_and_constraints = false;
valid_fronts = false;
}
void
validateObjAndConstraints()
{
valid_obj_and_constraints = true;
}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::make_nvp("PopVec", boost::serialization::base_object<std::vector<IndividualSPtr> >(*this) );
// ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(std::vector<Individual>);
}
friend std::ostream& operator<<(std::ostream& os, const Population& pop);
};
inline PopulationSPtr
restore_population(boost::filesystem::path filename, ProblemDefinitionsSPtr defs)
{
PopulationSPtr pop(new Population);
ProblemDefinitions * defs_ptr = defs.get();
std::string extension = filename.extension().string();
if (extension == ".txt")
{
pop.reset(new Population(filename, defs_ptr));
}
else if(extension == ".xml")
{
// open the archive
std::ifstream ifs(filename.c_str());
assert(ifs.good());
assert(ifs.is_open());
boost::archive::xml_iarchive ia(ifs);
// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(pop);
}
else
{
std::cout << "Unrecognised file format for loading population from\n";
}
return (pop);
}
template<typename RNG>
PopulationSPtr
intialisePopulationRandomDVAssignment(int population_size, ProblemDefinitionsSPtr defs, RNG & rng)
{
PopulationSPtr pop(new Population(population_size, defs));
for (int i = 0; i < defs->real_lowerbounds.size(); ++i)
{
std::uniform_real_distribution<double> uniform(defs->real_lowerbounds[i],defs->real_upperbounds[i]);
BOOST_FOREACH(IndividualSPtr ind, *pop)
{
ind->setRealDV(i, uniform(rng));
}
}
for (int i = 0; i < defs->unordered_lowerbounds.size(); ++i)
{
std::uniform_int_distribution<int> uniform(defs->unordered_lowerbounds[i],defs->unordered_upperbounds[i]);
BOOST_FOREACH(IndividualSPtr ind, *pop)
{
ind->setUnorderedDV(i, uniform(rng));
}
}
return (pop);
}
inline std::ostream&
operator<<(std::ostream& os, const PopulationSPtr pop)
{
os << *pop;
return os;
}
inline std::ostream&
operator<<(std::ostream& os, const Population& pop)
{
ObjectiveValueCompator obj_comparator(0);
Population temp_pop(pop);
std::sort(temp_pop.begin(), temp_pop.end(), obj_comparator);
BOOST_FOREACH(IndividualSPtr ind, temp_pop)
{
os << ind << "\n";
}
return os;
}
inline void
print(const Population& pop, boost::filesystem::path file)
{
std::string extension = file.extension().string();
if (extension == ".txt")
{
std::ofstream of(file.string().c_str());
if (of)
{
of << pop;
}
}
else if(extension == ".xml")
{
std::ofstream ofs(file.string().c_str());
if (ofs)
{
boost::archive::xml_oarchive oa(ofs);
oa << boost::serialization::make_nvp("Population", pop);
}
}
else
{
std::string filename = file.stem().string();
if (filename == ".") filename = "population";
boost::filesystem::path file_i_extn = file.parent_path() / (filename + ".txt");
std::ofstream of(file_i_extn.string().c_str());
if (of)
{
of << pop;
}
file_i_extn = file.parent_path() / (filename + ".xml");
std::ofstream ofs(file_i_extn.string().c_str());
if (ofs)
{
boost::archive::xml_oarchive oa(ofs);
oa << boost::serialization::make_nvp("Population", pop);
}
}
}
inline void
print(const PopulationSPtr pop, boost::filesystem::path file)
{
print(*pop, file);
}
#include "DebsNondominatedSorting.hpp"
#endif /* Population_h */