-
Notifications
You must be signed in to change notification settings - Fork 4
/
hexapod.cpp
283 lines (228 loc) · 9.37 KB
/
hexapod.cpp
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
#include <iostream>
#include <sstream>
#include <string>
#include <limbo/limbo.hpp>
#include <exhaustive_search_archive.hpp>
#include <map_elites/binary_map.hpp>
#include <mean_archive.hpp>
#include <hexapod_dart/hexapod_dart_simu.hpp>
using namespace limbo;
struct Params {
struct bayes_opt_boptimizer : public defaults::bayes_opt_boptimizer {
};
struct bayes_opt_bobase : public defaults::bayes_opt_bobase {
BO_PARAM(int, stats_enabled, true);
};
// no noise
struct kernel : public defaults::kernel {
BO_PARAM(double, noise, 1e-10);
};
struct kernel_maternfivehalves : public defaults::kernel_maternfivehalves {
BO_PARAM(double, l, 0.4);
};
struct stop_maxiterations {
BO_DYN_PARAM(int, iterations);
};
struct acqui_ucb : public defaults::acqui_ucb {
BO_PARAM(double, alpha, 0.2);
};
struct archiveparams {
struct elem_archive {
std::vector<double> duty_cycle;
float fit;
std::vector<double> controller;
};
struct classcomp {
bool operator()(const std::vector<double>& lhs, const std::vector<double>& rhs) const
{
assert(lhs.size() == 6 && rhs.size() == 6);
int i = 0;
while (i < 5 && std::round(lhs[i] * 4) == std::round(rhs[i] * 4)) //lhs[i]==rhs[i])
i++;
return std::round(lhs[i] * 4) < std::round(rhs[i] * 4); //lhs[i]<rhs[i];
}
};
typedef std::map<std::vector<double>, elem_archive, classcomp> archive_t;
static std::map<std::vector<double>, elem_archive, classcomp> archive;
};
};
Params::archiveparams::archive_t load_archive(std::string archive_name);
namespace global {
std::shared_ptr<hexapod_dart::Hexapod> global_robot;
std::vector<int> brokenLegs;
} // namespace global
struct Eval {
BO_PARAM(size_t, dim_in, 6);
BO_PARAM(size_t, dim_out, 1);
// the function to be optimized
Eigen::VectorXd operator()(const Eigen::VectorXd& x) const
{
std::vector<double> key(x.size(), 0);
Eigen::VectorXd::Map(key.data(), key.size()) = x;
std::vector<double> ctrl = Params::archiveparams::archive.at(key).controller;
hexapod_dart::HexapodDARTSimu<> simu(ctrl, global::global_robot->clone());
simu.run(5);
return tools::make_vector(simu.covered_distance());
}
};
void lecture(const std::vector<double>& ctrl)
{
hexapod_dart::HexapodDARTSimu<> simu(ctrl, global::global_robot->clone());
simu.run(5);
std::cout << "Covered distance: " << simu.covered_distance() << std::endl;
}
void init_simu(std::string robot_file, std::vector<int> broken_legs = std::vector<int>())
{
std::vector<hexapod_dart::HexapodDamage> damages(broken_legs.size());
for (size_t i = 0; i < broken_legs.size(); ++i)
damages.push_back(hexapod_dart::HexapodDamage("leg_removal", std::to_string(broken_legs[i])));
global::global_robot = std::make_shared<hexapod_dart::Hexapod>(robot_file, damages);
}
std::map<std::vector<double>, Params::archiveparams::elem_archive, Params::archiveparams::classcomp> load_archive(std::string archive_name)
{
std::map<std::vector<double>, Params::archiveparams::elem_archive, Params::archiveparams::classcomp> archive;
size_t lastindex = archive_name.find_last_of(".");
std::string extension = archive_name.substr(lastindex + 1);
if (extension == "bin") {
std::cout << "Loading binary file..." << std::endl;
try {
binary_map::BinaryMap m = binary_map::load(archive_name);
std::vector<binary_map::Elem> v = m.elems;
std::vector<float> dims = m.dims;
assert(dims.size() == 6);
for (size_t i = 0; i < v.size(); i++) {
Params::archiveparams::elem_archive elem;
std::vector<int> pos = v[i].pos;
std::vector<double> candidate(6);
for (size_t j = 0; j < dims.size(); j++) {
candidate[j] = pos[j] / double(dims[j]);
}
elem.duty_cycle = candidate;
elem.fit = v[i].fit;
elem.controller.clear();
std::copy(v[i].phen.begin(), v[i].phen.end(), std::back_inserter(elem.controller));
archive[candidate] = elem;
}
}
catch (...) {
std::cerr << "ERROR: Could not load the archive." << std::endl;
return archive;
}
}
else {
std::cout << "Loading text file..." << std::endl;
std::ifstream monFlux(archive_name.c_str());
if (monFlux) {
std::string line;
while (std::getline(monFlux, line)) {
std::istringstream iss(line);
std::vector<double> numbers;
double num;
while (iss >> num) {
numbers.push_back(num);
}
if (numbers.size() < 43)
continue;
int init_i = 0;
if (numbers.size() > 43)
init_i = 1;
Params::archiveparams::elem_archive elem;
std::vector<double> candidate(6);
for (int i = 0; i < 43; i++) {
double data = numbers[init_i + i];
if (i <= 5) {
candidate[i] = data;
elem.duty_cycle.push_back(data);
}
if (i == 6) {
elem.fit = data;
}
if (i >= 7)
elem.controller.push_back(data);
}
if (elem.controller.size() == 36) {
archive[candidate] = elem;
}
}
}
else {
std::cerr << "ERROR: Could not load the archive." << std::endl;
return archive;
}
}
std::cout << archive.size() << " elements loaded" << std::endl;
return archive;
}
Params::archiveparams::archive_t Params::archiveparams::archive;
BO_DECLARE_DYN_PARAM(int, Params::stop_maxiterations, iterations);
int main(int argc, char** argv)
{
std::vector<std::string> cmd_args;
for (int i = 0; i < argc; i++)
cmd_args.push_back(std::string(argv[i]));
std::vector<std::string>::iterator legs_it = std::find(cmd_args.begin(), cmd_args.end(), "-l");
std::vector<std::string>::iterator ctrl_it = std::find(cmd_args.begin(), cmd_args.end(), "-c");
std::vector<std::string>::iterator n_it = std::find(cmd_args.begin(), cmd_args.end(), "-n");
std::vector<int> brokenleg;
if (legs_it != cmd_args.end()) {
std::vector<std::string>::iterator end_it = (legs_it < ctrl_it) ? ctrl_it : cmd_args.end();
end_it = (end_it < n_it || n_it < legs_it) ? end_it : n_it;
for (std::vector<std::string>::iterator ii = legs_it + 1; ii != end_it; ii++) {
brokenleg.push_back(atoi((*ii).c_str()));
}
if (brokenleg.size() >= 6) {
std::cerr << "The robot should at least have one leg!" << std::endl;
if (global::global_robot)
global::global_robot.reset();
return -1;
}
}
global::brokenLegs = brokenleg;
init_simu(std::string(std::getenv("RESIBOTS_DIR")) + "/share/hexapod_models/URDF/pexod.urdf", global::brokenLegs);
if (ctrl_it != cmd_args.end()) {
std::vector<std::string>::iterator end_it = ctrl_it + 37;
std::vector<double> ctrl;
for (std::vector<std::string>::iterator ii = ctrl_it + 1; ii != end_it; ii++) {
ctrl.push_back(atof((*ii).c_str()));
}
if (ctrl.size() != 36) {
std::cerr << "You have to provide 36 controller parameters!" << std::endl;
if (global::global_robot)
global::global_robot.reset();
return -1;
}
lecture(ctrl);
if (global::global_robot)
global::global_robot.reset();
return 1;
}
// you need a map if you're not just replaying a controller
if (argc < 2) {
std::cerr << "Please provide a map" << std::endl;
if (global::global_robot)
global::global_robot.reset();
return -1;
}
Params::archiveparams::archive = load_archive(argv[1]);
if (n_it != cmd_args.end()) {
Params::stop_maxiterations::set_iterations(atoi((n_it + 1)->c_str()));
}
else
Params::stop_maxiterations::set_iterations(10);
typedef kernel::MaternFiveHalves<Params> Kernel_t;
typedef opt::ExhaustiveSearchArchive<Params> InnerOpt_t;
typedef boost::fusion::vector<stop::MaxIterations<Params>> Stop_t;
typedef mean::MeanArchive<Params> Mean_t;
typedef boost::fusion::vector<stat::Samples<Params>, stat::BestObservations<Params>, stat::ConsoleSummary<Params>> Stat_t;
typedef init::NoInit<Params> Init_t;
typedef model::GP<Params, Kernel_t, Mean_t> GP_t;
typedef acqui::UCB<Params, GP_t> Acqui_t;
bayes_opt::BOptimizer<Params, modelfun<GP_t>, initfun<Init_t>, acquifun<Acqui_t>, acquiopt<InnerOpt_t>, statsfun<Stat_t>, stopcrit<Stop_t>> opt;
opt.optimize(Eval());
auto val = opt.best_observation();
Eigen::VectorXd result = opt.best_sample().transpose();
std::cout << val << " res " << result.transpose() << std::endl;
if (global::global_robot)
global::global_robot.reset();
return 0;
}