-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADOL-C_sparseNLP.cpp
executable file
·479 lines (368 loc) · 11.1 KB
/
ADOL-C_sparseNLP.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
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
/*----------------------------------------------------------------------------
ADOL-C -- Automatic Differentiation by Overloading in C++
File: ADOL-C_sparseNLP.cpp
Revision: $$
Contents: class myADOLC_sparseNPL for interfacing with Ipopt
Copyright (c) Andrea Walther
This file is part of ADOL-C. This software is provided as open source.
Any use, reproduction, or distribution of the software constitutes
recipient's acceptance of the terms of the accompanying license file.
This code is based on the file MyNLP.cpp contained in the Ipopt package
with the authors: Carl Laird, Andreas Waechter
----------------------------------------------------------------------------*/
/** C++ Example NLP for interfacing a problem with IPOPT and ADOL-C.
* MyADOLC_sparseNLP implements a C++ example showing how to interface
* with IPOPT and ADOL-C through the TNLP interface. This class
* implements the Example 5.1 from "Sparse and Parially Separable
* Test Problems for Unconstrained and Equality Constrained
* Optimization" by L. Luksan and J. Vlcek taking sparsity
* into account.
*
* exploitation of sparsity !!
*
*/
#include <cassert>
#include "ADOL-C_sparseNLP.hpp"
using namespace Ipopt;
/* Constructor. */
MyADOLC_sparseNLP::MyADOLC_sparseNLP(Model* model)
{
model_ = model;
}
MyADOLC_sparseNLP::~MyADOLC_sparseNLP()
{}
bool MyADOLC_sparseNLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
Index& nnz_h_lag, IndexStyleEnum& index_style)
{
n = model_->getNbFloatVars();
m = model_->getNbConstraints();
generate_tapes(n, m, nnz_jac_g, nnz_h_lag);
// use the C style indexing (0-based)
index_style = C_STYLE;
return true;
}
bool MyADOLC_sparseNLP::get_bounds_info(Index n, Number* x_l, Number* x_u,
Index m, Number* g_l, Number* g_u)
{
// Set Bound on the variables.
for (Index i = 0; i<n; i++) {
// Lower bound
if (model_->getFloatVar(i).hasLowerBound())
{
x_l[i] = model_->getFloatVar(i).getLowerBound();
}
else
{
x_l[i] = -1e20;
}
// Upper bound
if (model_->getFloatVar(i).hasUpperBound())
{
x_u[i] = model_->getFloatVar(i).getUpperBound();
}
else
{
x_u[i] = 1e20;
}
}
// Set the bounds for the constraints (<= or ==)
for (Index i = 0; i<m; i++)
{
int boolIndex = model_->getConstraint(i).getLinkedBoolVarIndex();
ConstraintTypes::Type conType = model_->getConstraint(i).getType();
// Index -1 on LinkedBoolVar of a constraint means it's an ordinary(non-reified)
// constraitn and has noboolian variable associated with it.
if (boolIndex == -1) // Ordinary
{
if (conType == ConstraintTypes::eq) // =
{
g_l[i] = 0;
g_u[i] = 0;
}
else if (conType == ConstraintTypes::neq) // <=
{
g_l[i] = -1e20;
g_u[i] = 0;
}
else throw "Invalid Constraint Type!";
}
else // Reified
{
if (model_->getConstraint(i).isImplied())
{
if (conType == ConstraintTypes::eq) // =
{
g_l[i] = 0;
g_u[i] = 0;
}
else if (conType == ConstraintTypes::neq) // <=
{
g_l[i] = -1e20;
g_u[i] = 0;
}
else throw "Invalid Constraint Type!";
}
else //Relax if not implied
{
g_l[i] = -1e20;
g_u[i] = 1e20;
}
}
}
return true;
}
bool MyADOLC_sparseNLP::get_starting_point(Index n, bool init_x, Number* x,
bool init_z, Number* z_L, Number* z_U,
Index m, bool init_lambda,
Number* lambda)
{
assert(init_x == true);
assert(init_z == false);
assert(init_lambda == false);
//Initialize all with zero
for (int i = 0; i < n; i++)
{
x[i] = 0.;
}
// Change the ones with a supplied value.
int nbStartingPoints = model_->getNbStartingPoints();
if (nbStartingPoints >= 1)
{
int varIndex = -1;
for (map<int, double>::const_iterator it = model_->getStartingPoints().begin(); it != model_->getStartingPoints().end(); it++){
varIndex = it->first;
x[varIndex] = it->second;
}
}
return true;
}
template<class T> bool MyADOLC_sparseNLP::eval_obj(Index n, const T *x, T& obj_value)
{
obj_value = 0.;
int nbTerms = model_->getObjective().getNbTerms();
for (int i = 0; i < nbTerms; i++)
{
// Initializing a term with 1 is mandatory because:
// (1) The subterms cannot be multiplied with an empty term (muliplication by 0).
// (2) No matter what the subterms are, multiplication with 1.0 yields the
// desired form of term.
T term = 1.0;
int nbSubTerms = static_cast<int>(model_->getObjective().getExpression()[i].size());
// Multiply with the subterms
for (int j = 0; j < nbSubTerms; j++)
{
int varIndex = model_->getObjective().getExpression()[i][j].getIndex();
double varCoeff = model_->getObjective().getExpression()[i][j].getCoefficient();
int varPower = model_->getObjective().getExpression()[i][j].getPower();
// Check if the subterm j has a variable
if (varIndex != -1)
{
term = term * varCoeff * pow(x[varIndex], varPower);
}
else // Constant term
{
term = term * varCoeff;
}
}
obj_value += term;
}
return true;
}
template<class T> bool MyADOLC_sparseNLP::eval_constraints(Index n, const T *x, Index m, T* g)
{
for (Index k = 0; k < m; k++)
{
int nbTerms = model_->getConstraint(k).getNbTerms();
T expr = 0.0;
for (int i = 0; i < nbTerms; i++)
{
T term = 1.0;
int nbSubTerms = static_cast<int>(model_->getConstraint(k).getExpression()[i].size());
// Multiply with the subterms
for (int j = 0; j < nbSubTerms; j++)
{
int varIndex = model_->getConstraint(k).getExpression()[i][j].getIndex();
double varCoeff = model_->getConstraint(k).getExpression()[i][j].getCoefficient();
int varPower = model_->getConstraint(k).getExpression()[i][j].getPower();
// Check if the subterm j has a variable
if (varIndex != -1)
{
term = term * varCoeff * pow(x[varIndex], varPower);
}
else // Constant term
{
term = term * varCoeff;
}
}
expr += term;
}
g[k] = expr;
}
/*for (Index i = 0; i<m; i++) {
g[i] = 4.*pow(x[i + 1], 2.) + 2.*x[i + 2] + 4.*x[i + 1] - 5.;
}*/
return true;
}
//*************************************************************************
//
//
// Nothing has to be changed below this point !!
//
//
//*************************************************************************
bool MyADOLC_sparseNLP::eval_f(Index n, const Number* x, bool new_x, Number& obj_value)
{
eval_obj(n,x,obj_value);
return true;
}
bool MyADOLC_sparseNLP::eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f)
{
gradient(tag_f,n,x,grad_f);
return true;
}
bool MyADOLC_sparseNLP::eval_g(Index n, const Number* x, bool new_x, Index m, Number* g)
{
eval_constraints(n,x,m,g);
return true;
}
bool MyADOLC_sparseNLP::eval_jac_g(Index n, const Number* x, bool new_x,
Index m, Index nele_jac, Index* iRow, Index *jCol,
Number* values)
{
if (values == NULL) {
// return the structure of the jacobian
for(Index idx=0; idx<nnz_jac; idx++)
{
iRow[idx] = rind_g[idx];
jCol[idx] = cind_g[idx];
}
}
else {
// return the values of the jacobian of the constraints
sparse_jac(tag_g, m, n, 1, x, &nnz_jac, &rind_g, &cind_g, &jacval, options_g);
for(Index idx=0; idx<nnz_jac; idx++)
{
values[idx] = jacval[idx];
}
}
return true;
}
bool MyADOLC_sparseNLP::eval_h(Index n, const Number* x, bool new_x,
Number obj_factor, Index m, const Number* lambda,
bool new_lambda, Index nele_hess, Index* iRow,
Index* jCol, Number* values)
{
if (values == NULL) {
// return the structure. This is a symmetric matrix, fill the lower left
// triangle only.
for(Index idx=0; idx<nnz_L; idx++)
{
iRow[idx] = rind_L[idx];
jCol[idx] = cind_L[idx];
}
}
else {
// return the values. This is a symmetric matrix, fill the lower left
// triangle only
obj_lam[0] = obj_factor;
for(Index idx = 0; idx<m ; idx++)
obj_lam[1+idx] = lambda[idx];
set_param_vec(tag_L,m+1,obj_lam);
sparse_hess(tag_L, n, 1, const_cast<double*>(x), &nnz_L, &rind_L, &cind_L, &hessval, options_L);
for(Index idx = 0; idx <nnz_L ; idx++)
{
values[idx] = hessval[idx];
}
}
return true;
}
void MyADOLC_sparseNLP::finalize_solution(SolverReturn status,
Index n, const Number* x, const Number* z_L, const Number* z_U,
Index m, const Number* g, const Number* lambda,
Number obj_value,
const IpoptData* ip_data,
IpoptCalculatedQuantities* ip_cq)
{
// Save the solution
for (int i = 0; i < model_->getNbFloatVars(); i++)
{
model_->solutionVec_.push_back(x[i]);
}
model_->objectiveValue_ = obj_value;
printf("\n\nObjective value\n");
printf("f(x*) = %e\n", obj_value);
// memory deallocation of ADOL-C variables
delete[] obj_lam;
free(rind_g);
free(cind_g);
free(rind_L);
free(cind_L);
free(jacval);
free(hessval);
}
//*************** ADOL-C part ***********************************
void MyADOLC_sparseNLP::generate_tapes(Index n, Index m, Index& nnz_jac_g, Index& nnz_h_lag)
{
Number *xp = new double[n];
Number *lamp = new double[m];
Number *zl = new double[m];
Number *zu = new double[m];
adouble *xa = new adouble[n];
adouble *g = new adouble[m];
double *lam = new double[m];
double sig;
adouble obj_value;
double dummy;
// int i,j,k,l,ii;
obj_lam = new double[m+1];
get_starting_point(n, 1, xp, 0, zl, zu, m, 0, lamp);
trace_on(tag_f);
for(Index idx=0;idx<n;idx++)
xa[idx] <<= xp[idx];
eval_obj(n,xa,obj_value);
obj_value >>= dummy;
trace_off();
trace_on(tag_g);
for(Index idx=0;idx<n;idx++)
xa[idx] <<= xp[idx];
eval_constraints(n,xa,m,g);
for(Index idx=0;idx<m;idx++)
g[idx] >>= dummy;
trace_off();
trace_on(tag_L);
for(Index idx=0;idx<n;idx++)
xa[idx] <<= xp[idx];
for(Index idx=0;idx<m;idx++)
lam[idx] = 1.0;
sig = 1.0;
eval_obj(n,xa,obj_value);
obj_value *= mkparam(sig);
eval_constraints(n,xa,m,g);
for(Index idx=0;idx<m;idx++)
obj_value += g[idx]*mkparam(lam[idx]);
obj_value >>= dummy;
trace_off();
rind_g = NULL;
cind_g = NULL;
rind_L = NULL;
cind_L = NULL;
options_g[0] = 0; /* sparsity pattern by index domains (default) */
options_g[1] = 0; /* safe mode (default) */
options_g[2] = 0;
options_g[3] = 0; /* column compression (default) */
jacval=NULL;
hessval=NULL;
sparse_jac(tag_g, m, n, 0, xp, &nnz_jac, &rind_g, &cind_g, &jacval, options_g);
nnz_jac_g = nnz_jac;
options_L[0] = 0;
options_L[1] = 1;
sparse_hess(tag_L, n, 0, xp, &nnz_L, &rind_L, &cind_L, &hessval, options_L);
nnz_h_lag = nnz_L;
delete[] lam;
delete[] g;
delete[] xa;
delete[] zu;
delete[] zl;
delete[] lamp;
delete[] xp;
}