CouenneExprJac.cpp
Go to the documentation of this file.
1 /* $Id: CouenneExprJac.cpp 883 2012-08-03 13:39:53Z pbelotti $
2  *
3  * Name: CouenneExprMatr.cpp
4  * Authors: Pietro Belotti, Lehigh University
5  * Purpose: Implementation, matrix expressions
6  *
7  * This file is licensed under the Eclipse Public License (EPL)
8  */
9 
10 #include <stdio.h> // ! must go
11 
12 #include "CoinHelperFunctions.hpp"
13 
14 #include "CouenneExprJac.hpp"
15 #include "CouenneProblem.hpp"
16 #include "CouenneProblemElem.hpp"
17 #include "CouenneExprAux.hpp"
18 
19 using namespace Couenne;
20 
21 //#define DEBUG
22 
23 // empty constructor
25  nnz_ (0),
26  iRow_ (NULL),
27  jCol_ (NULL),
28  expr_ (NULL),
29  nRows_ (0) {}
30 
31 
32 //destructor
34 
35  if (nnz_) {
36 
37  free (iRow_);
38  free (jCol_);
39 
40  for (int i=0; i<nnz_; i++)
41  delete expr_ [i];
42 
43  free (expr_);
44  }
45 }
46 
47 // copy constructor
49 {operator= (rhs);}
50 
51 
52 // clone
54 {return new ExprJac (*this);}
55 
56 
57 // assignment
59 
60  nnz_ = rhs. nnz_;
61  nRows_ = rhs. nRows_;
62 
63  iRow_ = (nnz_ && rhs.iRow_) ? (int *) malloc (nnz_ * sizeof (int)) : NULL;
64  jCol_ = (nnz_ && rhs.jCol_) ? (int *) malloc (nnz_ * sizeof (int)) : NULL;
65 
66  CoinCopyN (rhs.iRow_, nnz_, iRow_);
67  CoinCopyN (rhs.jCol_, nnz_, jCol_);
68 
69  if (nnz_) {
70 
71  expr_ = (expression **) malloc (nnz_ * sizeof (expression *));
72 
73  for (int i=0; i<nnz_; i++)
74  expr_ [i] = expr_ [i] -> clone ();
75 
76  } else expr_ = NULL;
77 
78  return *this;
79 }
80 
81 
83 
84 #define reallocStep 100
85 static void reAlloc (int nCur, int &nMax, int *&r, int *&c, expression **&e) {
86 
87  if (nCur >= nMax) {
88 
89  nMax += reallocStep;
90 
91  r = (int *) realloc (r, nMax * sizeof (int));
92  c = (int *) realloc (c, nMax * sizeof (int));
93  e = (expression **) realloc (e, nMax * sizeof (expression *));
94  }
95 }
96 
97 // constructor
99 
100  nnz_ (0),
101  iRow_ (NULL),
102  jCol_ (NULL),
103  expr_ (NULL),
104  nRows_ (0) {
105 
113 
115  int
116  cursize = 0,
117  nRealCons = 0;
118 
119  reAlloc (nnz_, cursize, iRow_, jCol_, expr_);
120 
121  // constraints ////////////////////////////////////////////////////////////
122 
123  for (int i = 0; i < p -> nCons (); i++) {
124 
125  CouenneConstraint *c = p -> Con (i);
126 
127  if (c -> Body () -> Type () == AUX ||
128  c -> Body () -> Type () == VAR)
129  continue;
130 
131  // This is a constraint of the form f(x) <= 0. Find out the
132  // variables (original or aux) it depends on, directly
133  // (STOP_AT_AUX)
134 
135  std::set <int> deplist;
136 
137  c -> Body () -> DepList (deplist, STOP_AT_AUX);
138 
139  int nTerms = 0;
140 
141  for (std::set <int>::iterator k = deplist.begin (); k != deplist.end (); ++k) {
142 
143  // highly unlikely that x_k shows up in c's body, but you never know...
144  if (p -> Var (*k) -> Multiplicity () <= 0)
145  continue;
146 
147  expression
148  *J = c -> Body () -> differentiate (*k), // derivative of the
149  // constraint's body
150  // w.r.t. x_i
151 
152  *sJ = J -> simplify (), // a simplification
153  *rJ = sJ ? sJ : J; // the real one
154 
155  if (sJ)
156  delete J; // the only remaining expression won't be wasted
157 
158  if ((rJ -> Type () == CONST) &&
159  (rJ -> Value () == 0.))
160  continue;
161 
162  // there is a nonzero entry!
163 
164  reAlloc (nnz_ + 1, cursize, iRow_, jCol_, expr_);
165 
166  rJ -> realign (p);
167 
168  iRow_ [nnz_] = nRealCons;
169  jCol_ [nnz_] = *k;
170  expr_ [nnz_] = rJ;
171 
172  nnz_++;
173  nTerms++;
174  }
175 
176  if (nTerms) {
177  ++nRealCons; // increase the counter of real constraints
178  ++nRows_; // and of rows
179  }
180  }
181 
182  // auxiliaries ////////////////////////////////////////////////////////////
183 
185 
186  for (int i = 0; i < p -> nVars (); i++) {
187 
188  exprVar *e = p -> Var (i);
189 
190  if ((e -> Type () != AUX) ||
191  (e -> Multiplicity () <= 0))
192  continue;
193 
194  // this is a variable definition of the form y </>/= f(x). Find
195  // out the variables (original or aux) it depends on, directly
196  // (STOP_AT_AUX)
197 
198  std::set <int> deplist;
199 
200  e -> Image () -> DepList (deplist, STOP_AT_AUX);
201 
202  deplist.insert (e -> Index ());
203 
204  int nTerms = 0;
205 
206  for (std::set <int>::iterator k = deplist.begin (); k != deplist.end (); ++k) {
207 
208  if (p -> Var (*k) -> Multiplicity () <= 0)
209  continue;
210 
211  expression
212  *J = (*k == e -> Index ()) ?
213  new exprConst (-1.) :
214  e -> Image () -> differentiate (*k), // derivative of the
215  // constraint's body
216  // w.r.t. x_i
217 
218  *sJ = J -> simplify (), // a simplification
219  *rJ = sJ ? sJ : J; // the real one
220 
221  if (sJ)
222  delete J; // the only remaining expression won't be wasted
223 
224  if ((rJ -> Type () == CONST) &&
225  (rJ -> Value () == 0.))
226  continue;
227 
228  rJ -> realign (p);
229 
230  // there is a nonzero entry!
231 
232  reAlloc (nnz_ + 1, cursize, iRow_, jCol_, expr_);
233 
234  iRow_ [nnz_] = nRealCons;
235  jCol_ [nnz_] = *k;
236  expr_ [nnz_] = rJ;
237 
238  ++nnz_;
239  ++nTerms;
240  }
241 
242  if (nTerms) {
243  ++nRealCons; // increase the counter of real constraints
244  ++nRows_;
245  }
246  }
247 
248 #ifdef DEBUG
249  printf ("jacobian: %d nonzeros, %d rows\n", nnz_, nRows_);
250 
251  for (int i=0; i<nnz_; i++) {
252 
253  printf ("[%d,%d]: ", iRow_ [i], jCol_ [i]);
254 
255  fflush (stdout);
256  expr_ [i] -> print ();
257  printf ("\n");
258  }
259 #endif
260 }
Jacobian of the problem (computed through Couenne expression classes).
int * iRow_
row indices (read this way by eval_jac_g)
constant-type operator
ExprJac & operator=(const ExprJac &)
void fint fint fint real fint real real real real real real real real real * e
static void reAlloc(int nCur, int &nMax, int *&r, int *&c, expression **&e)
Class to represent nonlinear constraints.
#define reallocStep
code for refilling jacobian
Class for MINLP problems with symbolic information.
int * jCol_
col indices
void fint fint * k
void fint fint fint real fint real real real real real real real * r
expression ** expr_
nonzero expression elements (there are nnz_ of them)
int nRows_
number of actual constraints
int nnz_
number of (symbolic) nonzeroes
variable-type operator
Expression base class.
real c