-
Notifications
You must be signed in to change notification settings - Fork 468
/
CMAESOptimizer.cpp
319 lines (266 loc) · 10.3 KB
/
CMAESOptimizer.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
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
/* -------------------------------------------------------------------------- *
* Simbody(tm): SimTKmath *
* -------------------------------------------------------------------------- *
* This is part of the SimTK biosimulation toolkit originating from *
* Simbios, the NIH National Center for Physics-Based Simulation of *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
* *
* Portions copyright (c) 2006-14 Stanford University and the Authors. *
* Authors: Chris Dembia *
* Contributors: Michael Sherman, Nikolaus Hansen *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
* not use this file except in compliance with the License. You may obtain a *
* copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* -------------------------------------------------------------------------- */
#include "CMAESOptimizer.h"
#include <bitset>
namespace SimTK {
#define SimTK_CMAES_PRINT(diag, cmds) \
do { \
if (std::bitset<2>(diag).test(0)) { cmds; } \
} \
while(false)
#define SimTK_CMAES_FILE(diag, cmds) \
do { \
if (std::bitset<2>(diag).test(1)) { cmds; } \
} \
while(false)
CMAESOptimizer::CMAESOptimizer(const OptimizerSystem& sys) : OptimizerRep(sys)
{
SimTK_VALUECHECK_ALWAYS(2, sys.getNumParameters(), INT_MAX, "nParameters",
"CMAESOptimizer");
}
Optimizer::OptimizerRep* CMAESOptimizer::clone() const {
return new CMAESOptimizer(*this);
}
Real CMAESOptimizer::optimize(SimTK::Vector& results)
{
const OptimizerSystem& sys = getOptimizerSystem();
int n = sys.getNumParameters();
// Initialize objective function value and cmaes data structure.
// =============================================================
Real f;
cmaes_t evo;
// Initialize parallelism, if requested.
std::string parallel;
std::unique_ptr<ParallelExecutor> executor;
if (getAdvancedStrOption("parallel", parallel)) {
// Number of parallel processes/threads.
int nthreads = ParallelExecutor::getNumProcessors();
getAdvancedIntOption("nthreads", nthreads);
// Multithreading.
if (parallel == "multithreading") {
executor.reset(new ParallelExecutor(nthreads));
}
}
// Check that the initial point is feasible.
// =========================================
checkInitialPointIsFeasible(results);
// Initialize cmaes.
// =================
double* funvals = init(evo, results);
SimTK_CMAES_PRINT(diagnosticsLevel, printf("%s\n", cmaes_SayHello(&evo)));
// Optimize.
// =========
while (!cmaes_TestForTermination(&evo)) {
// Sample a population.
// ====================
double*const* pop = cmaes_SamplePopulation(&evo);
// Resample to keep population within limits.
// ==========================================
resampleToObeyLimits(evo, pop);
// Evaluate the objective function on the samples.
// ===============================================
evaluateObjectiveFunctionOnPopulation(evo, pop, funvals, executor.get());
// Update the distribution (mean, covariance, etc.).
// =================================================
cmaes_UpdateDistribution(&evo, funvals);
}
// Wrap up.
// ========
SimTK_CMAES_PRINT(diagnosticsLevel,
printf("Stop:\n%s\n", cmaes_TestForTermination(&evo)));
// Update results and objective function value.
const double* xbestever = cmaes_GetPtr(&evo, "xbestever");
for (int i = 0; i < n; i++) {
results[i] = xbestever[i];
}
f = cmaes_Get(&evo, "fbestever");
SimTK_CMAES_FILE(diagnosticsLevel,
cmaes_WriteToFile(&evo, "all", "allcmaes.dat"));
// Free memory.
cmaes_exit(&evo);
return f;
}
void CMAESOptimizer::checkInitialPointIsFeasible(const Vector& x) const {
const OptimizerSystem& sys = getOptimizerSystem();
if( sys.getHasLimits() ) {
Real *lower, *upper;
sys.getParameterLimits( &lower, &upper );
for (int i = 0; i < sys.getNumParameters(); i++) {
SimTK_APIARGCHECK4_ALWAYS(
lower[i] <= x[i] && x[i] <= upper[i],
"CMAESOptimizer", "checkInitialPointIsFeasible",
"Initial guess results[%d] = %f "
"is not within limits [%f, %f].",
i, x[i], lower[i], upper[i]);
}
}
}
double* CMAESOptimizer::init(cmaes_t& evo, SimTK::Vector& results) const
{
const OptimizerSystem& sys = getOptimizerSystem();
int n = sys.getNumParameters();
// Prepare to call cmaes_init_para.
// ================================
// popsize
// -------
int popsize = 0;
getAdvancedIntOption("popsize", popsize);
// init_stepsize
// --------
double init_stepsize = 0;
double* stddev = NULL;
Vector init_stepsizeArray;
if (getAdvancedRealOption("init_stepsize", init_stepsize)) {
init_stepsizeArray.resize(n);
for (int i = 0; i < n; i++) {
init_stepsizeArray[i] = init_stepsize;
}
stddev = &init_stepsizeArray[0];
}
// seed
// ----
int seed = 0;
if (getAdvancedIntOption("seed", seed)) {
SimTK_VALUECHECK_NONNEG_ALWAYS(seed, "seed",
"CMAESOptimizer::processSettingsBeforeCMAESInit");
}
// input parameter filename
// ------------------------
std::string input_parameter_filename = "none";
SimTK_CMAES_FILE(diagnosticsLevel,
input_parameter_filename = "writeonly";);
// Call cmaes_init_para.
// =====================
// Here, we specify the subset of options that can be passed to
// cmaes_init_para.
cmaes_init_para(&evo,
n, // dimension
&results[0], // xstart
stddev, // stddev
seed, // seed
popsize, // lambda
input_parameter_filename.c_str() // input_parameter_filename
);
// Set settings that are usually read in from cmaes_initials.par.
// ==============================================================
process_readpara_settings(evo);
// Once we've updated settings in cmaes_readpara_t,
// finalize the initialization.
return cmaes_init_final(&evo);
}
void CMAESOptimizer::process_readpara_settings(cmaes_t& evo) const
{
// Termination criteria
// ====================
// stopMaxIter
// -----------
// maxIterations is a protected member variable of OptimizerRep.
evo.sp.stopMaxIter = maxIterations;
// stopTolFun
// ----------
// convergenceTolerance is a protected member variable of OptimizerRep.
evo.sp.stopTolFun = convergenceTolerance;
// stopMaxFunEvals
// ---------------
int stopMaxFunEvals;
if (getAdvancedIntOption("stopMaxFunEvals", stopMaxFunEvals)) {
SimTK_VALUECHECK_NONNEG_ALWAYS(stopMaxFunEvals, "stopMaxFunEvals",
"CMAESOptimizer::processSettingsAfterCMAESInit");
evo.sp.stopMaxFunEvals = stopMaxFunEvals;
}
// stopFitness
// -----------
double stopFitness;
if (getAdvancedRealOption("stopFitness", stopFitness)) {
evo.sp.stStopFitness.flg = 1;
evo.sp.stStopFitness.val = stopFitness;
}
// stopTolFunHist
// --------------
double stopTolFunHist;
if (getAdvancedRealOption("stopTolFunHist", stopTolFunHist)) {
evo.sp.stopTolFunHist = stopTolFunHist;
}
// stopTolX
// --------
double stopTolX;
if (getAdvancedRealOption("stopTolX", stopTolX)) {
evo.sp.stopTolX = stopTolX;
}
// stopTolXFactor
// --------------
double stopTolUpXFactor;
if (getAdvancedRealOption("stopTolUpXFactor", stopTolUpXFactor)) {
evo.sp.stopTolUpXFactor = stopTolUpXFactor;
}
// maxtime
// =======
double maxtime;
if (getAdvancedRealOption("maxTimeFractionForEigendecomposition", maxtime))
{
evo.sp.updateCmode.maxtime = maxtime;
}
}
void CMAESOptimizer::resampleToObeyLimits(cmaes_t& evo, double*const* pop)
{
const OptimizerSystem& sys = getOptimizerSystem();
if( sys.getHasLimits() ) {
Real *lower, *upper;
sys.getParameterLimits( &lower, &upper );
for (int i = 0; i < cmaes_Get(&evo, "popsize"); i++) {
bool feasible = false;
while (!feasible) {
feasible = true;
for (int j = 0; j < sys.getNumParameters(); j++) {
if (pop[i][j] < lower[j] || pop[i][j] > upper[j]) {
feasible = false;
pop = cmaes_ReSampleSingle(&evo, i);
break;
}
}
}
}
}
}
void CMAESOptimizer::evaluateObjectiveFunctionOnPopulation(
cmaes_t& evo, double*const* pop, double* funvals,
ParallelExecutor* executor)
{
const OptimizerSystem& sys = getOptimizerSystem();
// Execute in parallel.
if (executor) {
Task task(*this, sys.getNumParameters(), pop, funvals);
executor->execute(task, (int)cmaes_Get(&evo, "popsize"));
}
// Execute normally.
else {
for (int i = 0; i < cmaes_Get(&evo, "popsize"); i++) {
objectiveFuncWrapper(sys.getNumParameters(),
pop[i], true, &funvals[i], this);
}
}
}
#undef SimTK_CMAES_PRINT
#undef SimTK_CMAES_FILE
#undef SimTK_CMAES_SMART_PTR
} // namespace SimTK