19 #include "CoinHelperFunctions.hpp"
20 #include "CoinFinite.hpp"
22 using namespace Couenne;
32 {
return (v1 -> Index () < v2 -> Index ());}
37 std::vector <std::pair <exprVar *, CouNumber> > &lcoeff,
38 std::vector <quadElem> &qcoeff,
46 typedef std::map <exprVar *, CouNumber, cmpVar> rowMap;
47 typedef std::map <exprVar *, rowMap, cmpVar> matrixMap;
51 for (std::vector <quadElem>::iterator qel = qcoeff.begin (); qel != qcoeff.end (); ++qel) {
56 *varI = qel -> varI (),
57 *varJ = qel -> varJ ();
72 matrixMap::iterator rowp = qMap.find (varI);
74 if (rowp == qMap.end ()) {
76 std::pair <exprVar *, CouNumber> newcell (varJ, coe);
78 rmap.insert (newcell);
80 std::pair <exprVar *, rowMap> newrow (varI, rmap);
85 rowMap::iterator cell = rowp -> second.find (varJ);
87 if (cell == rowp -> second.end ()) {
89 std::pair <exprVar *, CouNumber> newcell (varJ, coe);
90 rowp -> second.insert (newcell);
96 rowp -> second.erase (cell);
103 for (matrixMap::iterator row = qMap.begin (); row != qMap.end (); ++row) {
110 std::pair <CouNumber, CouNumber> newbound (-COIN_DBL_MAX, COIN_DBL_MAX);
111 std::pair <exprVar *, std::pair <CouNumber, CouNumber> > newvar (row -> first, newbound);
115 for (rowMap::iterator cell = row -> second.begin (); cell != row -> second.end (); ++cell) {
117 line.push_back (std::pair <exprVar *, CouNumber> (*cell));
122 std::pair <CouNumber, CouNumber> newbound (-COIN_DBL_MAX, COIN_DBL_MAX);
123 std::pair <exprVar *, std::pair <CouNumber, CouNumber> > newvar (cell -> first, newbound);
128 matrix_.push_back (std::pair <exprVar *, sparseQcol> (row -> first, line));
137 bounds_ (src.bounds_),
138 nqterms_ (src.nqterms_) {
140 for (sparseQ::iterator row = src.
matrix_.begin (); row != src.
matrix_ .
end (); ++row) {
144 for (sparseQcol::iterator i = row -> second. begin (); i != row -> second.
end (); ++i)
145 column.push_back (std::pair <exprVar *, CouNumber>
147 (
new exprVar (i -> first ->
Index (), d), i -> second));
149 matrix_.push_back (std::pair <exprVar *, sparseQcol>
158 <std::pair <exprVar *, CouNumber> > > >::iterator row;
160 for (row = src.
eigen_ . begin ();
163 std::vector <std::pair <exprVar *, CouNumber> > eigVec;
165 for (std::vector <std::pair <exprVar *, CouNumber> >::iterator
166 i = row -> second. begin ();
167 i != row -> second.
end (); ++i)
168 eigVec.push_back (std::pair <exprVar *, CouNumber>
169 (dynamic_cast <exprVar *> (i -> first ->
clone (d)), i -> second));
171 eigen_.push_back (std::pair <CouNumber, std::vector
172 <std::pair <exprVar *, CouNumber> > > (row -> first, eigVec));
189 for (
size_t n =
matrix_.size (), i=0;
n--; i++) {
195 for (
int m = row.size (),
j=0;
m--;
j++) {
199 if (fabs (row [
j]. second + 1.) <
COUENNE_EPS) out <<
"- ";
201 if (row [
j]. second > 0.) out <<
'+';
202 out << row [
j]. second <<
"*";
206 if (row [
j].first ->
Index () == xind) {
212 row [
j]. first ->
print (out, descend);
229 std::map <exprVar *, CouNumber> lmap;
234 for (lincoeff::iterator el =
lcoeff_.begin (); el !=
lcoeff_.end (); ++el)
238 for (sparseQ::iterator row =
matrix_.begin (); row !=
matrix_.end (); ++row) {
240 int xind = row -> first ->
Index ();
242 for (sparseQcol::iterator col = row -> second.begin (); col != row -> second.end (); ++col) {
244 int yind = col -> first ->
Index ();
250 if (yind == index) {var = col -> first; coe *= 2;}
251 else var = col -> first;
252 else if (yind == index) var = row -> first;
255 std::map <exprVar *, CouNumber>::iterator i = lmap.find (var);
257 if (i != lmap.end()) {
261 std::pair <exprVar *, CouNumber> npair (var, coe);
271 for (
int i = 0; i <
nargs_; i++)
289 return new exprSum (arglist, nargs);
294 for (std::map <exprVar *, CouNumber>::iterator i = lmap.begin (); i != lmap.end (); ++i)
295 coe.push_back (std::pair <exprVar *, CouNumber> (i -> first, i -> second));
297 return new exprGroup (c0, coe, arglist, nargs);
313 for (sparseQ::iterator
319 if (row1 -> first ->
Index () < row2 -> first ->
Index ())
return -1;
320 if (row1 -> first ->
Index () > row2 -> first ->
Index ())
return 1;
322 if (row1 -> second.size () < row2 -> second.size ())
return -1;
323 if (row1 -> second.size () > row2 -> second.size ())
return 1;
329 for (sparseQcol::iterator
330 col1 = row1 -> second.begin (),
331 col2 = row2 -> second.begin ();
332 col1 != row1 -> second.end ();
335 if (col1 -> first ->
Index () < col2 -> first ->
Index ())
return -1;
336 if (col1 -> first ->
Index () > col2 -> first ->
Index ())
return 1;
338 if (col1 -> second < col2 -> second -
COUENNE_EPS)
return -1;
339 if (col1 -> second > col2 -> second +
COUENNE_EPS)
return 1;
358 for (sparseQ::iterator row =
matrix_.begin (); row !=
matrix_.end (); ++row) {
360 if ((r = row -> first ->
rank ()) > maxrank) maxrank = r;
362 for (sparseQcol::iterator col = row -> second.begin (); col != row -> second.end (); ++col)
363 if ((r = col -> first ->
rank ()) > maxrank) maxrank = r;
375 for (sparseQ::iterator row =
matrix_.begin (); row !=
matrix_.end (); ++row) {
377 dep -> insert (g -> lookup (row -> first ->
Index ()));
379 for (sparseQcol::iterator col = row -> second.begin (); col != row -> second.end (); ++col)
380 dep -> insert (g -> lookup (col -> first ->
Index ()));
392 for (sparseQ::iterator row =
matrix_.begin (); row !=
matrix_.end (); ++row) {
393 deps += row -> first ->
DepList (deplist, type);
394 for (sparseQcol::iterator col = row -> second.begin (); col != row -> second.end (); ++col)
395 deps += col -> first ->
DepList (deplist, type);
408 for (sparseQ::iterator row =
matrix_.begin (); row !=
matrix_.end (); ++row) {
410 bool intI = row -> first ->
isInteger ();
412 for (sparseQcol::iterator col = row -> second.begin (); col != row -> second.end (); ++col) {
420 if (intI && intJ && intCoe)
424 && row -> first -> isFixed ()
425 && col -> first -> isFixed ()
427 row -> first -> lb () *
428 col -> first -> lb ()))
431 if (!intI && (row -> first -> isFixed ()) && ::
isInteger ((*(row -> first)) ()))
continue;
432 if (!intJ && (col -> first -> isFixed ()) && ::
isInteger ((*(col -> first)) ()))
continue;
450 int xind = x ->
Index ();
451 int wind = w ->
Index ();
453 for (sparseQ::iterator row =
matrix_.begin (); row !=
matrix_.end (); ++row) {
456 if ((vr ->
Index () == xind)) {
463 for (sparseQcol::iterator col = row -> second.begin (); col != row -> second.end (); ++col) {
466 if ((vc ->
Index () == wind)) {
480 for (sparseQ::iterator row =
matrix_.begin (); row !=
matrix_.end (); ++row) {
492 row -> first = p -> Var (indVar);
498 for (sparseQcol::iterator col = row -> second.begin (); col != row -> second.end (); ++col) {
510 col -> first = p -> Var (indVar);
524 fprintf (stderr,
"exprQuad::closestFeasible() not available for VT\n");
534 for (sparseQ::iterator row =
matrix_.begin (); row !=
matrix_.end (); ++row) {
537 for (sparseQcol::iterator col = row -> second.begin (); col != row -> second.end (); ++col)
538 gradEl += col -> second * x [col -> first ->
Index ()];
540 grad += gradEl * gradEl;
virtual int DepList(std::set< int > &deplist, enum dig_type type=ORIG_ONLY)
fill in the set with all indices of variables appearing in the expression
virtual expression * differentiate(int index)
Compute derivative of this expression with respect to variable whose index is passed as argument...
virtual int dependsOn(int *ind, int n, enum dig_type type=STOP_AT_AUX)
dependence on variable set: return cardinality of subset of the set of indices in first argument whic...
virtual void fillDepSet(std::set< DepNode *, compNode > *, DepGraph *)
update dependence set with index of this variable
class Group, with constant, linear and nonlinear terms:
virtual void realign(const CouenneProblem *p)
replace variable x with new (aux) w
virtual int compare(exprQuad &)
Compare two exprQuad.
virtual bool isInteger()
is this expression integer?
virtual void print(std::ostream &=std::cout, bool=false) const
Print expression to iostream.
virtual int rank()
Used in rank-based branching variable choice.
virtual void replace(exprVar *x, exprVar *w)
replace variable x with new (aux) w
exprSum(expression **=NULL, int=0)
Constructors, destructor.
CouNumber gradientNorm(const double *x)
return l-2 norm of gradient at given point
virtual bool isInteger()
is this expression integer?
virtual int rank()
used in rank-based branching variable choice
int nqterms_
number of non-zeroes in Q
exprGroup(CouNumber, lincoeff &, expression **=NULL, int=0)
Constructor.
void fint fint fint real fint real real real real real real real real real * e
virtual enum nodeType Type() const
Node type.
virtual int Index() const
Return index of variable (only valid for exprVar and exprAux)
lincoeff lcoeff_
coefficients and indices of the linear term
Class for MINLP problems with symbolic information.
exprQuad(CouNumber c0, std::vector< std::pair< exprVar *, CouNumber > > &lcoeff, std::vector< quadElem > &qcoeff, expression **al=NULL, int n=0)
Constructor.
virtual int DepList(std::set< int > &deplist, enum dig_type type=ORIG_ONLY)
fill in the set with all indices of variables appearing in the expression
std::vector< std::pair< CouNumber, std::vector< std::pair< exprVar *, CouNumber > > > > eigen_
eigenvalues and eigenvectors
void fint fint fint real fint real real real real real real real * r
virtual expression * simplify()
simplification
virtual void replace(exprVar *x, exprVar *w)
replace variable x with new (aux) w
std::map< exprVar *, std::pair< CouNumber, CouNumber > > bounds_
current bounds (checked before re-computing eigenvalues/vectors)
expression ** arglist_
argument list is an array of pointers to other expressions
std::vector< std::pair< exprVar *, CouNumber > > lincoeff
double CouNumber
main number type in Couenne
virtual void closestFeasible(expression *varind, expression *vardep, CouNumber &left, CouNumber &right) const
compute $y^{lv}$ and $y^{uv}$ for Violation Transfer algorithm
int nargs_
number of arguments (cardinality of arglist)
virtual expression * simplify()
Simplify expression.
virtual int compare(exprGroup &)
only compare with people of the same kind
class exprQuad, with constant, linear and quadratic terms
virtual void fillDepSet(std::set< DepNode *, compNode > *dep, DepGraph *g)
Fill dependence set of the expression associated with this auxiliary variable.
dig_type
type of digging when filling the dependence list
void fint fint fint real fint real real real real real real * g
virtual expression * clone(Domain *d=NULL) const
cloning method
void fint fint fint real fint real real real real real real real real * w
virtual const expression * Original() const
If this is an exprClone of a exprClone of an expr???, point to the original expr??? instead of an exprClone – improve computing efficiency.
Define a dynamic point+bounds, with a way to save and restore previous points+bounds through a LIFO s...
std::vector< std::pair< exprVar *, CouNumber > > sparseQcol
matrix
virtual void print(std::ostream &=std::cout, bool=false) const
Print expression to an iostream.
void fint fint fint real fint real * x