/
method_ipopt.c
527 lines (462 loc) · 16 KB
/
method_ipopt.c
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
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2010, Linköpings University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
/*! \file ipopt_initialization.c
*/
#include "../../../../Compiler/runtime/config.h"
#include "method_ipopt.h"
#include "simulation_data.h"
#include "omc_error.h"
#ifdef WITH_IPOPT
#include "openmodelica.h"
#include "openmodelica_func.h"
#include "model_help.h"
#include "read_matlab4.h"
#include "events.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <coin/IpStdCInterface.h>
typedef struct IPOPT_DATA
{
DATA *data;
INIT_DATA *initData;
int useScaling;
int useSymbolic;
} IPOPT_DATA;
/*! \fn ipopt_f
*
* \param [in] [n]
* \param [in] [x]
* \param [in] [new_x]
* \param [out] [obj_value]
* \param [ref] [user_data]
*
* \author lochel
*/
static Bool ipopt_f(int n, double *x, Bool new_x, double *obj_value, void *user_data)
{
IPOPT_DATA *ipopt_data = (IPOPT_DATA*)user_data;
setZ(ipopt_data->initData, x);
*obj_value = leastSquareWithLambda(ipopt_data->initData, 1.0);
return TRUE;
}
/*! \fn ipopt_grad_f
*
* \param [in] [n]
* \param [in] [x]
* \param [in] [new_x]
* \param [out] [grad_f]
* \param [ref] [user_data]
*
* \author lochel
*/
static Bool ipopt_grad_f(int n, double *x, Bool new_x, double *grad_f, void *user_data)
{
int i;
double xp, xn;
double h = 1e-6;
double hh;
for(i=0; i<n; ++i)
{
hh = (abs(x[i]) > 1e-3) ? h*abs(x[i]) : h;
x[i] += hh;
ipopt_f(n, x, new_x, &xp, user_data);
x[i] -= 2.0*hh;
ipopt_f(n, x, new_x, &xn, user_data);
x[i] += hh;
grad_f[i] = (xp-xn)/(2.0*hh);
}
return TRUE;
}
/*! \fn ipopt_g
*
* \param [in] [n]
* \param [in] [x]
* \param [in] [new_x]
* \param [out] [g]
* \param [ref] [user_data]
*
* \author lochel
*/
static Bool ipopt_g(int n, double *x, Bool new_x, int m, double *g, void *user_data)
{
int i;
IPOPT_DATA *ipopt_data = (IPOPT_DATA*)user_data;
double obj_value;
ipopt_f(n, x, new_x, &obj_value, user_data);
for(i=0; i<m; ++i)
g[i] = ipopt_data->initData->initialResiduals[i];
return TRUE;
}
/*! \fn functionJacG_sparse
*
* \param [ref] [data]
* \param [out] [jac]
*
* \author lochel
*/
int functionJacG_sparse(DATA* data, double* jac)
{
int color, seedVar, i, l, k=0;
int index = data->callback->INDEX_JAC_G;
const int maxColor = data->simulationInfo.analyticJacobians[index].sparsePattern.maxColors;
const int numSeedVars = data->simulationInfo.analyticJacobians[index].sizeCols;
for(color=0; color<maxColor; color++)
{
for(i=0; i<numSeedVars; i++)
if(data->simulationInfo.analyticJacobians[index].sparsePattern.colorCols[i]-1 == color)
data->simulationInfo.analyticJacobians[index].seedVars[i] = 1;
data->callback->functionJacG_column(data);
for(seedVar=0; seedVar<numSeedVars; seedVar++)
{
if(data->simulationInfo.analyticJacobians[index].seedVars[seedVar] == 1)
{
if(seedVar == 0)
i = 0;
else
i = data->simulationInfo.analyticJacobians[index].sparsePattern.leadindex[seedVar-1];
for(; i < data->simulationInfo.analyticJacobians[index].sparsePattern.leadindex[seedVar]; i++)
{
l = data->simulationInfo.analyticJacobians[index].sparsePattern.index[i]-1;
jac[k++] = data->simulationInfo.analyticJacobians[index].resultVars[l];
}
}
}
for(i=0; i<numSeedVars; i++)
if(data->simulationInfo.analyticJacobians[index].sparsePattern.colorCols[i]-1 == color)
data->simulationInfo.analyticJacobians[index].seedVars[i] = 0;
}
return 0;
}
/*! \fn ipopt_jac_g
*
* \param [in] [n]
* \param [in] [x]
* \param [in] [new_x]
* \param [in] [m]
* \param [in] [nele_jac]
* \param [out] [iRow]
* \param [out] [jCol]
* \param [out] [values]
* \param [ref] [user_data]
*
* \author lochel
*/
static Bool ipopt_jac_g(int n, double *x, Bool new_x, int m, int nele_jac,
int *iRow, int *jCol, double *values, void *user_data)
{
IPOPT_DATA *ipopt_data = (IPOPT_DATA*)user_data;
DATA *data = ipopt_data->data;
if(values == NULL)
{
int i, j;
int idx = 0;
if(ipopt_data->useSymbolic == 1)
{
/*
* SPARSE
*
*/
infoStreamPrint(LOG_INIT, 0, "ipopt using symbolic sparse jacobian G");
if(ACTIVE_STREAM(LOG_INIT)) {
infoStreamPrint(LOG_INIT, 0, "sparsity pattern");
for(i=0; i<n; ++i) {
printf(" | | column %3d: [ ", i+1);
for(j=0; idx<ipopt_data->initData->simData->simulationInfo.analyticJacobians[data->callback->INDEX_JAC_G].sparsePattern.leadindex[i]; ++j) {
if(j+1 == ipopt_data->initData->simData->simulationInfo.analyticJacobians[data->callback->INDEX_JAC_G].sparsePattern.index[idx]) {
idx++;
printf("*");
} else {
printf("0");
}
}
for(; j<m; ++j)
printf("0");
printf("]\n");
}
printf("\n");
}
idx = 0;
for(i=0; i<n; ++i) {
for(j=0; idx<ipopt_data->initData->simData->simulationInfo.analyticJacobians[data->callback->INDEX_JAC_G].sparsePattern.leadindex[i]; ++j) {
if(j+1 == ipopt_data->initData->simData->simulationInfo.analyticJacobians[data->callback->INDEX_JAC_G].sparsePattern.index[idx]) {
jCol[idx] = i;
iRow[idx] = j;
idx++;
}
}
}
} else {
/*
* DENSE
*
*/
infoStreamPrint(LOG_INIT, 0, "ipopt using numeric dense jacobian G");
idx = 0;
for(i=0; i<n; ++i)
{
for(j=0; j<m; ++j)
{
jCol[idx] = i;
iRow[idx] = j;
idx++;
}
}
}
assert(idx == nele_jac);
}
else
{
/* return the values of the jacobian of the constraints */
infoStreamPrint(LOG_DEBUG, 0, "ipopt jacobian G");
if(ipopt_data->useSymbolic == 1)
{
functionJacG_sparse(ipopt_data->initData->simData, values);
if(ACTIVE_STREAM(LOG_DEBUG)) /* TODO: This is not XML data, is it? */
{
int i, j;
int idx = 0;
for(i=0; i<n; ++i)
{
printf(" | | column %3d: [ ", i+1);
for(j=0; idx<ipopt_data->initData->simData->simulationInfo.analyticJacobians[data->callback->INDEX_JAC_G].sparsePattern.leadindex[i]; ++j)
{
if(j+1 == ipopt_data->initData->simData->simulationInfo.analyticJacobians[data->callback->INDEX_JAC_G].sparsePattern.index[idx])
{
printf("%10.5g ", values[idx]);
idx++;
}
else
printf("%10.5g ", 0.0);
}
for(; j<m; ++j)
printf("%10.5g ", 0.0);
printf("]\n");
}
}
}
else
{
int i, j;
int idx = 0;
double h = 1e-6;
double hh;
double *gp = (double*)malloc(m * sizeof(double));
double *gn = (double*)malloc(m * sizeof(double));
for(i=0; i<n; ++i)
{
hh = (abs(x[i]) > 1e-3) ? h*abs(x[i]) : h;
x[i] += hh;
ipopt_g(n, x, new_x, m, gp, user_data);
x[i] -= 2.0*hh;
ipopt_g(n, x, new_x, m, gn, user_data);
x[i] += hh;
for(j=0; j<m; ++j)
{
values[idx] = (gp[j]-gn[j])/(2.0*hh);
idx++;
}
}
free(gp);
free(gn);
if(ACTIVE_STREAM(LOG_DEBUG))
{
int i, j;
for(i=0; i<n; ++i)
{
printf(" | | column %3d: [ ", i+1);
for(j=0; j<m; ++j)
printf("%10.5g ", values[j*n+i]);
printf("]\n");
}
}
}
}
return TRUE;
}
/*! \fn ipopt_h
*
* \param [in] [n]
* \param [in] [x]
* \param [in] [new_x]
* \param [in] [obj_factor]
* \param [in] [m]
* \param [in] [lambda]
* \param [in] [new_lambda]
* \param [in] [nele_hess]
* \param [out] [iRow]
* \param [out] [jCol]
* \param [out] [values]
* \param [ref] [user_data]
*
* \author lochel
*/
static Bool ipopt_h(int n, double *x, Bool new_x, double obj_factor, int m, double *lambda, Bool new_lambda,
int nele_hess, int *iRow, int *jCol, double *values, void *user_data)
{
assert(0);
return TRUE;
}
/*! \fn int ipopt_initialization(INIT_DATA *initData, int useScaling)
*
* This function is used if ipopt is choosen for initialization.
*
* \param [ref] [initData]
* \param [in] [useScaling]
*
* \author lochel
*/
int ipopt_initialization(DATA *data,INIT_DATA *initData, int useScaling)
{
int n = initData->nVars; /* number of variables */
int m = (initData->nInitResiduals > initData->nVars) ? 0 : initData->nInitResiduals; /* number of constraints */
double* x_L = NULL; /* lower bounds on x */
double* x_U = NULL; /* upper bounds on x */
double* g_L = NULL; /* lower bounds on g */
double* g_U = NULL; /* upper bounds on g */
double* x = NULL; /* starting point and solution vector */
double* mult_g = NULL; /* constraint multipliers at the solution */
double* mult_x_L = NULL; /* lower bound multipliers at the solution */
double* mult_x_U = NULL; /* upper bound multipliers at the solution */
double obj; /* objective value */
int i; /* generic counter */
int nele_jac = n*m; /* number of nonzeros in the Jacobian of the constraints */
int nele_hess = 0; /* number of nonzeros in the Hessian of the Lagrangian (lower or upper triangual part only) */
IpoptProblem nlp = NULL; /* ipopt-problem */
enum ApplicationReturnStatus status; /* solve return code */
IPOPT_DATA ipopt_data;
ipopt_data.data = data;
ipopt_data.initData = initData;
ipopt_data.useScaling = useScaling;
ipopt_data.useSymbolic = (data->callback->initialAnalyticJacobianG(initData->simData) == 0 ? 1 : 0);
if(ipopt_data.useSymbolic == 1)
{
/* sparse */
nele_jac = initData->simData->simulationInfo.analyticJacobians[data->callback->INDEX_JAC_G].sparsePattern.leadindex[n-1];
infoStreamPrint(LOG_INIT, 0, "number of zeros in the Jacobian of the constraints (jac_g): %d", n*m-nele_jac);
infoStreamPrint(LOG_INIT, 0, "number of nonzeros in the Jacobian of the constraints (jac_g): %d", nele_jac);
}
/* allocate space for the variable bounds */
x_L = (double*)malloc(n * sizeof(double));
x_U = (double*)malloc(n * sizeof(double));
/* allocate space for the constraint bounds */
g_L = (double*)malloc(m * sizeof(double));
g_U = (double*)malloc(m * sizeof(double));
/* allocate space for the initial point */
x = (double*)malloc(n * sizeof(double));
/* set values of optimization variable bounds */
for(i=0; i<n; ++i)
{
x[i] = initData->start[i];
x_L[i] = initData->min[i];
x_U[i] = initData->max[i];
}
/* set values of constraint bounds */
for(i=0; i<m; ++i)
{
g_L[i] = 0.0;
g_U[i] = 0.0;
}
/* create the IpoptProblem */
nlp = CreateIpoptProblem(
n, /* Number of optimization variables */
x_L, /* Lower bounds on variables */
x_U, /* Upper bounds on variables */
m, /* Number of constraints */
g_L, /* Lower bounds on constraints */
g_U, /* Upper bounds on constraints */
nele_jac, /* Number of non-zero elements in constraint Jacobian */
nele_hess, /* Number of non-zero elements in Hessian of Lagrangian */
0, /* indexing style for iRow & jCol; 0 for C style, 1 for Fortran style */
&ipopt_f, /* Callback function for evaluating objective function */
&ipopt_g, /* Callback function for evaluating constraint functions */
&ipopt_grad_f, /* Callback function for evaluating gradient of objective function */
&ipopt_jac_g, /* Callback function for evaluating Jacobian of constraint functions */
&ipopt_h); /* Callback function for evaluating Hessian of Lagrangian function */
assertStreamPrint(0 != nlp, "creating of ipopt problem has failed");
/* We can free the memory now - the values for the bounds have been
copied internally in CreateIpoptProblem */
free(x_L);
free(x_U);
free(g_L);
free(g_U);
/* Set some options. Note the following ones are only examples,
they might not be suitable for your problem. */
AddIpoptNumOption(nlp, "tol", 1e-7);
AddIpoptIntOption(nlp, "print_level", ACTIVE_STREAM(LOG_INIT) ? 5 : 0);
AddIpoptIntOption(nlp, "max_iter", 5000);
AddIpoptStrOption(nlp, "mu_strategy", "adaptive");
AddIpoptStrOption(nlp, "hessian_approximation", "limited-memory");
/* allocate space to store the bound multipliers at the solution */
mult_g = (double*)malloc(m*sizeof(double));
mult_x_L = (double*)malloc(n*sizeof(double));
mult_x_U = (double*)malloc(n*sizeof(double));
/* solve the problem */
status = IpoptSolve(
nlp, /* Problem that is to be optimized */
x, /* Input: Starting point; Output: Optimal solution */
NULL, /* Values of constraint at final point */
&obj, /* Final value of objective function */
mult_g, /* Final multipliers for constraints */
mult_x_L, /* Final multipliers for lower variable bounds */
mult_x_U, /* Final multipliers for upper variable bounds */
&ipopt_data); /* Pointer to user data */
setZ(initData, x);
/* free allocated memory */
FreeIpoptProblem(nlp);
free(x);
free(mult_g);
free(mult_x_L);
free(mult_x_U);
/* debug output */
dumpInitialization(data,initData);
if(status != Solve_Succeeded && status != Solved_To_Acceptable_Level)
throwStreamPrint("ipopt failed. see last warning. use [-lv LOG_INIT] for more output.");
/* return (int)status; */
return reportResidualValue(initData);
}
#else
/*! \fn int ipopt_initialization(INIT_DATA *initData, int useScaling)
*
* This function is used if no ipopt support is avaible but ipopt is choosen
* for initialization.
*
* \param [ref] [initData]
* \param [in] [useScaling]
*
* \author lochel
*/
int ipopt_initialization(INIT_DATA *initData, int useScaling)
{
throwStreamPrint("no ipopt support activated");
return 0;
}
#endif