readnl.cpp
Go to the documentation of this file.
1 /* $Id: readnl.cpp 1147 2015-05-04 14:01:51Z stefan $
2  *
3  * Name: readnl.cpp
4  * Author: Pietro Belotti
5  * Purpose: define a reader for .nl files. Adapted from ampl2ev3 by L. Liberti and S. Galli
6  *
7  * (C) Carnegie-Mellon University, 2006-10.
8  * This file is licensed under the Eclipse Public License (EPL)
9  */
10 
11 #include "asl.h"
12 
13 #include "nlp.h"
14 #include "getstub.h"
15 #include "opcode.hd"
16 
17 #include "CoinHelperFunctions.hpp"
18 #include "CoinTime.hpp"
19 
20 #include "CouenneProblem.hpp"
21 
22 #if defined HAVE_CSTDINT
23 #include "cstdint"
24 #elif defined HAVE_STDINT_H
25 #include "stdint.h"
26 #endif
27 
28 #include "CouenneTypes.hpp"
29 #include "CouenneExprSum.hpp"
30 #include "CouenneExprOpp.hpp"
31 #include "CouenneExprMul.hpp"
32 #include "CouenneExprSub.hpp"
33 #include "CouenneExprClone.hpp"
34 #include "CouenneExprGroup.hpp"
35 
36 #define OBJ_DE ((const ASL_fg *) asl) -> I.obj_de_
37 #define VAR_E ((const ASL_fg *) asl) -> I.var_e_
38 #define CON_DE ((const ASL_fg *) asl) -> I.con_de_
39 #define OBJ_sense ((const ASL_fg *) asl) -> i.objtype_
40 
41 #define THRESHOLD_OUTPUT_READNL 10000
42 
43 //#define DEBUG
44 
45 using namespace Couenne;
46 
47 // check if an expression is a null pointer or equals zero
48 inline bool is_expr_zero (expr* e)
49 {return ((e==NULL) || ((((Intcast (e->op)) == OPNUM) &&
50  (fabs (((expr_n *)e) -> v) < COUENNE_EPS)
51  // && (fabs (e -> dL) < COUENNE_EPS)
52  // *** CHECK THIS! dL is the derivative
53  )));}
54 
55 void createCommonExpr (CouenneProblem *p, const ASL *asl, int i, int which);
56 
57 // Reads a MINLP from an AMPL .nl file through the ASL methods
58 int CouenneProblem::readnl (const ASL *asl) {
59 
60  problemName_ = filename;
61 
62  // number of defined variables (aka common expressions)
63  ndefined_ = como + comc + comb + como1 + comc1;
64 
65  // see "hooking your solver to AMPL", by David M. Gay, tables 3, 4, and 5
66 
67  // nonlinear in both objectives and constraints
68  if (nlvb >= 0) {
69  for (int i = 0; i < nlvb - nlvbi; i++) addVariable (false, &domain_);
70  for (int i = 0; i < nlvbi; i++) addVariable (true, &domain_);
71  }
72 
73  // nonlinear in either objectives or constraints
74  if (nlvo > nlvc) {
75  for (int i = 0; i < nlvc - (nlvb + nlvci); i++) addVariable (false, &domain_);
76  for (int i = 0; i < nlvci; i++) addVariable (true, &domain_);
77  for (int i = 0; i < nlvo - (nlvc + nlvoi); i++) addVariable (false, &domain_);
78  for (int i = 0; i < nlvoi; i++) addVariable (true, &domain_);
79  } else {
80  for (int i = 0; i < nlvo - (nlvb + nlvoi); i++) addVariable (false, &domain_);
81  for (int i = 0; i < nlvoi; i++) addVariable (true, &domain_);
82  for (int i = 0; i < nlvc - (nlvo + nlvci); i++) addVariable (false, &domain_);
83  for (int i = 0; i < nlvci; i++) addVariable (true, &domain_);
84  }
85 
86  for (int i = 0; i < nwv; i++) addVariable(false, &domain_);//arc
87  for (int i = n_var - (CoinMax (nlvc,nlvo) +niv+nbv+nwv); i--;) addVariable(false, &domain_);//other
88  for (int i = 0; i < nbv; i++) addVariable(true, &domain_);//binary
89  for (int i = 0; i < niv; i++) addVariable(true, &domain_);//int.
90 
91  // add space for common expressions
92  for (int i = ndefined_; i--;) addVariable(false, &domain_);
93 
94  double now = CoinCpuTime ();
95 
96  if (nVars () > THRESHOLD_OUTPUT_READNL) {
97  jnlst_ -> Printf (Ipopt::J_ERROR, J_COUENNE, "Reading problem: ");
98  fflush(stdout);
99  }
100 
101  // common expressions (or defined variables) ///////////////////////////////////////
102 
103 #ifdef DEBUG
104  printf ("ndefd_ = %d\n", ndefined_);
105  printf ("tot var = %d\n", variables_ . size ());
106  printf ("c_vars_ = %d\n", ((const ASL_fg *) asl) -> i.c_vars_ );
107  printf ("comb_ = %d\n", ((const ASL_fg *) asl) -> i.comb_ );
108  printf ("combc_ = %d\n", ((const ASL_fg *) asl) -> i.combc_ );
109  printf ("comc1_ = %d\n", ((const ASL_fg *) asl) -> i.comc1_ );
110  printf ("comc_ = %d\n", ((const ASL_fg *) asl) -> i.comc_ );
111  printf ("como1_ = %d\n", ((const ASL_fg *) asl) -> i.como1_ );
112  printf ("como_ = %d\n", ((const ASL_fg *) asl) -> i.como_ );
113 #endif
114 
115  // Each has a linear and a nonlinear part (thanks to Dominique
116  // Orban: http://www.gerad.ca/~orban/drampl/def-vars.html)
117 
118  for (int i = 0; i < como + comc + comb; i++)
119  createCommonExpr (this, asl, i, 0);
120 
121  for (int i = 0; i < como1 + comc1; i++)
122  createCommonExpr (this, asl, i, 1);
123 
124  //commonexprs_ . erase (commonexprs_ . begin (), commonexprs_ . end ());
125 
126  // objective functions /////////////////////////////////////////////////////////////
127 
128  if (n_obj == 0) {
129 
130  // strange, no objective function. Add one equal to zero
131 
132  jnlst_ -> Printf (Ipopt::J_ERROR, J_COUENNE, "Couenne: warning, no objective function found\nAdded fictitious function f(x)=0\n");
133  addObjective (new exprConst (0.), "min");
134  }
135 
136  for (int i = 0; i < n_obj; i++) {
137 
139  int nterms = 0;
140 
141  // count nonzero terms in linear part
142 
143  for (ograd *objgrad = Ograd [i];
144  objgrad;
145  objgrad = objgrad -> next)
146  if (fabs (objgrad -> coef) > COUENNE_EPS)
147  nterms++;
148 
149  expression
150  *body,
151  *nl = Simplified (nl2e (OBJ_DE [i] . e, asl));
152  // *nls = nl -> simplify ();
153 
154  // if (nls) {
155  // delete nl;
156  // nl = nls;
157  // }
158 
159  if (nterms) { // have linear terms
160 
161  int *indexL = new int [nterms+1];
162  CouNumber *coeff = new CouNumber [nterms];
163 
164  for (ograd *objgrad = Ograd [i]; objgrad; objgrad = objgrad -> next)
165  if (fabs (objgrad -> coef) > COUENNE_EPS) {
166 
167  *indexL++ = objgrad -> varno;
168  *coeff++ = objgrad -> coef;
169  }
170 
171  *indexL = -1;
172 
173  indexL -= nterms;
174  coeff -= nterms;
175 
176  std::vector <std::pair <exprVar *, CouNumber> > lcoeff;
177  indcoe2vector (indexL, coeff, lcoeff);
178 
179  if (nl -> code () == COU_EXPRSUM) {
180  body = exprGroup::genExprGroup (0., lcoeff, nl -> ArgList (), nl -> nArgs ());
181  // delete node without deleting children (they are now in body)
182  nl -> ArgList (NULL);
183  delete nl;
184  }
185  else {
186 
187  expression **nll = new expression * [1];
188 
189  *nll = nl;
190 
191  // apparently, objconst (i) is included in the obj expression
192  body = exprGroup::genExprGroup (0., lcoeff, nll, 1);
193  //body = new exprGroup (objconst (i), indexL, coeff, nll, 1);
194  }
195 
196  delete [] indexL;
197  delete [] coeff;
198 
199  } else
200  // apparently, objconst (i) is included in the obj expression
201  body = nl;
202  //if (fabs (objconst (i) > COUENNE_EPS))
203  //body = new exprSum (nl, new exprConst (objconst (i)));
204  //else
205 
207 
208  expression *subst = Simplified (body); // -> simplify ();
209 
210  // if (subst) {
211  // delete body; // VALGRIND
212  // body = subst;
213  // }
214 
215  // ThirdParty/ASL/solvers/asl.h, line 336: 0 is minimization, 1 is maximization
216  addObjective (body, (OBJ_sense [i] == 0) ? "min" : "max");
217  }
218 
219  // constraints ///////////////////////////////////////////////////////////////////
220 
221  int *nterms = new int [n_con];
222 
223  // allocate space for argument list of all constraints' summations
224  // of linear and nonlinear terms
225 
226  // init array with # terms of each constraint
227  for (int i = n_con; i--;)
228  *nterms++ = 0;
229  nterms -= n_con;
230 
231  cgrad *congrad;
232 
233  // count all linear terms
234  if (A_colstarts && A_vals) // Constraints' linear info is stored in A_vals
235  for (register int j = A_colstarts [n_var]; j--;) {
236 
237  real coeff = A_vals [j];
238 
239  if (fabs (coeff) > COUENNE_EPS)
240  nterms [A_rownos [j]] ++;
241  }
242  else { // Constraints' linear info is stored in Cgrad
243  for (register int i = 0; i < n_con; i++)
244  for (congrad = Cgrad [i];
245  congrad;
246  congrad = congrad -> next)
247  if (fabs (congrad -> coef) > COUENNE_EPS)
248  nterms [i] ++;
249  }
250 
251 
252  // vectors of the linear part
253  CouNumber **coeff = new CouNumber * [n_con];
254  int **indexL = new int * [n_con];
255 
256  for (register int i = n_con; i--;)
257  *indexL++ = NULL;
258 
259  indexL -= n_con;
260 
261 
262  // set linear terms
263 
264  if (A_colstarts && A_vals) // Constraints' linear info is stored in A_vals
265  for (int j = 0; j < n_var; j++)
266  for (register int i = A_colstarts [j], k = A_colstarts [j+1] - i; k--; i++) {
267 
268  int rowno = A_rownos [i],
269  nt = nterms [rowno] --;
270 
271  CouNumber **cline = coeff + rowno;
272  int **iline = indexL + rowno;
273 
274  if (*iline==NULL) {
275  *cline = new CouNumber [nt];
276  *iline = new int [nt+1];
277  (*iline) [nt] = -1;
278  }
279 
280  (*cline) [--nt] = A_vals [i];
281  (*iline) [nt] = j;
282 
283  }
284  else { // Constraints' linear info is stored in Cgrad
285  for (int i=0; i < n_con; i++) {
286 
287  int nt = nterms [i];
288 
289  CouNumber **cline = coeff + i;
290  int **iline = indexL + i;
291 
292  *cline = new CouNumber [nt];
293  *iline = new int [nt+1];
294  (*iline) [nt] = -1;
295 
296  for (congrad = Cgrad [i]; congrad; congrad = congrad -> next)
297  if (fabs (congrad -> coef) > COUENNE_EPS) {
298  (*cline) [--nt] = congrad -> coef;
299  (*iline) [nt] = congrad -> varno;
300  }
301  }
302  }
303 
304  // set constraints' bound and sign and store nonlinear part ///////////////////////////////
305 
306  for (int i = 0; i < n_con; i++) {
307 
308  enum con_sign sign;
309  double lb, ub;
310 
311  if (Urhsx) {
312  lb = LUrhs [i];
313  ub = Urhsx [i];
314  } else {
315  int j = 2*i;
316  lb = LUrhs [j];
317  ub = LUrhs [j+1];
318  }
319 
320  // set constraint sign
321  if (lb > negInfinity)
322  if (ub < Infinity) sign = COUENNE_RNG;
323  else sign = COUENNE_GE;
324  else sign = COUENNE_LE;
325 
326  // this is an equality constraint
327  if (fabs (lb - ub) < COUENNE_EPS)
328  sign = COUENNE_EQ;
329 
330  expression
331  *body,
332  **nll = new expression * [1],
333  *nls;
334 
335  *nll = Simplified (nl2e (CON_DE [i] . e, asl));
336 
337  // nls = (*nll) -> simplify ();
338 
339  // if (nls) {
340  // delete *nll;
341  // *nll = nls;
342  // }
343 
344  if (indexL [i] && (*(indexL [i]) >= 0)) {
345 
346  int code = (*nll) -> code ();
347 
348  std::vector <std::pair <exprVar *, CouNumber> > lcoeff;
349  indcoe2vector (indexL [i], coeff [i], lcoeff);
350 
351  /*std::vector <std::pair <exprVar *, CouNumber> > lcoeff;
352  for (int i=0, *ind = indexL; *ind >= 0; *ind++, i++)
353  lcoeff.push_back (std::pair <exprVar *, CouNumber> (Var (*ind), coeff [i]));*/
354 
355  if ((code == COU_EXPRSUM) ||
356  (code == COU_EXPRGROUP)) {
357 
358  body = exprGroup::genExprGroup (0., lcoeff, (*nll) -> ArgList (), (*nll) -> nArgs ());
359  // delete node without deleting children (they are now in body)
360  (*nll) -> ArgList (NULL);
361  delete *nll;
362  delete [] nll;
363  }
364  else body = exprGroup::genExprGroup (0., lcoeff, nll, 1);
365  }
366  else {
367  body = *nll;
368  delete [] nll;
369  }
370 
371  expression *subst = Simplified (body); // or body->simplify() ?
372 
373  // -> simplify ();
374  // if (subst) {
375  // delete body; // VALGRIND
376  // body = subst;
377  // }
378 
379  // add them (and set lower-upper bound)
380  if ((lb < negInfinity) &&
381  (ub > Infinity)) {
382 
383  printf ("Free constraint %d ignored\n", i);
384 
385  } else
386 
387  // add them (and set lower-upper bound)
388  switch (sign) {
389 
390  case COUENNE_EQ: addEQConstraint (body, new exprConst (ub)); break;
391  case COUENNE_LE: addLEConstraint (body, new exprConst (ub)); break;
392  case COUENNE_GE: addGEConstraint (body, new exprConst (lb)); break;
393  case COUENNE_RNG: addRNGConstraint (body, new exprConst (lb),
394  new exprConst (ub)); break;
395  default: printf ("Could not recognize constraint\n"); return -1;
396  }
397 
398  delete [] indexL [i];
399  delete [] coeff [i];
400  }
401 
402  delete [] indexL;
403  delete [] coeff;
404 
405  // create room for problem's variables and bounds
406  CouNumber
407  *x = (CouNumber *) malloc ((n_var + ndefined_) * sizeof (CouNumber)),
408  *lb = (CouNumber *) malloc ((n_var + ndefined_) * sizeof (CouNumber)),
409  *ub = (CouNumber *) malloc ((n_var + ndefined_) * sizeof (CouNumber));
410 
411  for (int i = n_var + ndefined_; i--;) {
412  x [i] = 0.;
413  lb [i] = -COUENNE_INFINITY;
414  ub [i] = COUENNE_INFINITY;
415  }
416 
417  domain_.push (n_var + ndefined_, x, lb, ub);
418 
419  free (x); free (lb); free (ub);
420 
421  // lower and upper bounds ///////////////////////////////////////////////////////////////
422 
423  if (LUv) {
424 
425  real *Uvx_copy = Uvx;
426 
427  if (!Uvx_copy)
428  for (register int i=0; i<n_var; i++) {
429 
430  register int j = 2*i;
431 
432  Lb (i) = LUv [j];
433  Ub (i) = LUv [j+1];
434  }
435  else
436  for (register int i=n_var; i--;) {
437  Lb (i) = LUv [i];
438  Ub (i) = Uvx_copy [i];
439  }
440 
441  } else
442  for (register int i=n_var; i--;) {
443  Lb (i) = - COUENNE_INFINITY;
444  Ub (i) = COUENNE_INFINITY;
445  }
446 
447  // initial x ////////////////////////////////////////////////////////////////////
448 
449  for (register int i=n_var; i--;)
450 
451  if (X0 && havex0 [i]) X (i) = X0 [i];
452 
453  else {
454 
455  CouNumber x, l = Lb (i), u = Ub (i);
456 
457  if (l < - COUENNE_INFINITY)
458  if (u > COUENNE_INFINITY) x = 0.;
459  else x = u;
460  else if (u > COUENNE_INFINITY) x = l;
461  else x = 0.5 * (l+u);
462 
463  X (i) = x;
464  }
465 
466  for (register int i=n_var; i<ndefined_; i++) {
467 
468  X (i) = 0.;
469  Lb (i) = -COUENNE_INFINITY;
470  Ub (i) = COUENNE_INFINITY;
471  }
472 
473  delete [] nterms;
474 
475  if (nVars () > THRESHOLD_OUTPUT_READNL) {
476  jnlst_ -> Printf (Ipopt::J_ERROR, J_COUENNE, "%.1f seconds\n", CoinCpuTime () - now);
477  fflush(stdout);
478  }
479 
480  return 0;
481 }
482 
483 
484 //
485 // Common code for common expressions (aka defined variables)
486 //
487 
488 void createCommonExpr (CouenneProblem *p, const ASL *asl, int i, int which) {
489 
490  struct cexp *common = ((const ASL_fg *) asl) -> I.cexps_ + i;
491  struct cexp1 *common1 = ((const ASL_fg *) asl) -> I.cexps1_ + i;
492 
493  expression
494  *nle = Simplified (p -> nl2e (which ? common1 -> e : common -> e, asl));
495 
496  // *nls = nle -> simplify ();
497 
498  // if (nls) {
499  // delete nle;
500  // nle = nls;
501  // }
502 
503 #ifdef DEBUG
504  printf ("cexp1 %d [%d]: ", i, p -> Variables () . size ()); nle -> print (); printf (" ||| ");
505 #endif
506 
507  int nlin = which ? common1 -> nlin : common -> nlin; // Number of linear terms
508 
509  if (nlin > 0) {
510 
511  linpart *L = which ? common1 -> L : common -> L;
512 
513  std::vector <std::pair <exprVar *, CouNumber> > lcoeff;
514 
515  for (int j = 0; j < nlin; j++) {
516  //vp = (expr_v *)((char *)L->v.rp - ((char *)&ev.v - (char *)&ev));
517 
518  int indVar = ((uintptr_t) (L [j].v.rp) - (uintptr_t) VAR_E) / sizeof (expr_v);
519  CouNumber coeff = L [j]. fac;
520 
521  lcoeff.push_back (std::pair <exprVar *, CouNumber> (p -> Var (indVar), coeff));
522 
523 #ifdef DEBUG
524  Printf( " %+g x_%d (%-3d)", L [j]. fac, indVar,
525  (expr_v *) (L [j].v.rp) - VAR_E //((const ASL_fg *) asl) -> I.cexps_
526  //L [j]. v.i
527  );
528 #endif
529  }
530 
531  expression **al = new expression * [1];
532  *al = nle;
533 
534  expression *eg;
535 
536  if (lcoeff.size () == 1 &&
537  nle -> Type () == CONST &&
538  nle -> Value () == 0.) {
539 
540  CouNumber coeff = lcoeff [0].second;
541 
542  if (coeff == 1.) eg = new exprClone (lcoeff [0].first);
543  else if (coeff == -1.) eg = new exprOpp (new exprClone (lcoeff [0].first));
544  else eg = new exprMul (new exprConst (coeff), new exprClone (lcoeff [0].first));
545  } else eg = exprGroup::genExprGroup (0, lcoeff, al, 1);
546 
547  int indVar = p -> nVars () - p -> nDefVars () + p -> commonExprs () . size ();
548 
549  if (eg -> Index () != indVar) {
550 
551  expression *body = new exprSub (eg, new exprClone (p -> Var (indVar)));
552  p -> addEQConstraint (body, new exprConst (0.));
553  }
554 
555  p -> commonExprs () . push_back (new exprClone (eg));
556  }
557  else {
558 
559  int indVar = p -> nVars () - p -> nDefVars () + p -> commonExprs () . size ();
560 
561  if (nle -> Index () != indVar) {
562 
563  expression *body = new exprSub (nle, new exprClone (p -> Var (indVar)));
564  p -> addEQConstraint (body, new exprConst (0.));
565  }
566 
567  p -> commonExprs () . push_back (new exprClone (nle));
568  }
569 
570 #ifdef DEBUG
571  printf ("\n");
572 #endif
573  // addAuxiliary (nl2e (((const ASL_fg *) asl) -> I.cexps1_ [i] . e, asl));
574 }
void addLEConstraint(expression *, expression *=NULL)
Add constraint, .
int nVars() const
Total number of variables.
#define THRESHOLD_OUTPUT_READNL
Definition: readnl.cpp:41
void addObjective(expression *, const std::string &="min")
Add (non linear) objective function.
ULong L
Definition: OSdtoa.cpp:1816
class for subtraction,
class opposite,
std::string problemName_
problem name
constant-type operator
bool is_expr_zero(expr *e)
Definition: readnl.cpp:48
static char * j
Definition: OSdtoa.cpp:3622
static expression * genExprGroup(CouNumber, lincoeff &, expression **=NULL, int=0)
Generalized (static) constructor: check parameters and return a constant, a single variable...
Definition: exprGroup.cpp:50
void fint fint fint real fint real real real real real real real real real * e
Bonmin::BqpdSolver::real real
int ndefined_
Number of &quot;defined variables&quot; (aka &quot;common expressions&quot;)
void addEQConstraint(expression *, expression *=NULL)
Add equality constraint .
expression * addVariable(bool isint=false, Domain *d=NULL)
Add original variable.
Class for MINLP problems with symbolic information.
void fint fint * k
expression clone (points to another expression)
#define COUENNE_EPS
std::vector< exprVar * > variables_
Variables (original, auxiliary, and defined)
#define Intcast
Definition: OSdtoa.cpp:38
#define CON_DE
Definition: readnl.cpp:38
void createCommonExpr(CouenneProblem *p, const ASL *asl, int i, int which)
Definition: readnl.cpp:488
Domain domain_
current point and bounds;
CouNumber * Ub() const
Return vector of upper bounds.
void addGEConstraint(expression *, expression *=NULL)
Add constraint, .
double CouNumber
main number type in Couenne
expression * Simplified(expression *complicated)
Macro to return already simplified expression without having to do the if part every time simplify ()...
void addRNGConstraint(expression *, expression *=NULL, expression *=NULL)
Add range constraint, .
#define COUENNE_INFINITY
CouNumber * X() const
Return vector of variables.
con_sign
sign of constraint
void push(int dim, CouNumber *x, CouNumber *lb, CouNumber *ub, bool copy=true)
save current point and start using another
Definition: domain.cpp:166
#define VAR_E
Definition: readnl.cpp:37
JnlstPtr jnlst_
SmartPointer to the Journalist.
#define OBJ_sense
Definition: readnl.cpp:39
fint nt
void indcoe2vector(int *indexL, CouNumber *coeff, std::vector< std::pair< exprVar *, CouNumber > > &lcoeff)
translates pair (indices, coefficients) into vector with pointers to variables
Expression base class.
The in-memory representation of the variables element.
Definition: OSInstance.h:83
const Ipopt::EJournalCategory J_COUENNE(Ipopt::J_USER8)
#define OBJ_DE
Definition: readnl.cpp:36
void fint fint fint real fint real * x
class for multiplications,
CouNumber * Lb() const
Return vector of lower bounds.