auxiliarize.cpp
Go to the documentation of this file.
1 /* $Id: auxiliarize.cpp 792 2012-01-24 17:24:15Z pbelotti $
2  *
3  * Name: auxiliarize.cpp
4  * Author: Pietro Belotti
5  * Purpose: replace occurrences of original variable in a problem with
6  * auxiliary with the same index
7  *
8  * (C) Carnegie-Mellon University, 2007-10.
9  * This file is licensed under the Eclipse Public License (EPL)
10  */
11 
12 #include "CouenneProblem.hpp"
13 #include "CouenneProblemElem.hpp"
14 #include "CouenneExprAux.hpp"
15 #include "CouenneExprClone.hpp"
16 #include "CouenneDepGraph.hpp"
17 
18 #include <cstdio>
19 #include <cassert>
20 
21 using namespace Couenne;
22 
26 
28 
29  if (graph_ && subst && aux -> Index () != subst -> Index ())
30  graph_ -> replaceIndex (aux -> Index (), subst -> Index ());
31 
32  if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
33  printf ("replacing "); if (aux) aux -> print ();
34  if (subst) {printf (" with "); subst -> print ();}
35  printf ("\n");
36  }
37 
38  bool same_var = (subst == NULL);
39 
40  if (!subst)
41  subst = aux;
42 
43  // find original variable with index = w -> Index ()
44 
45  int index = aux -> Index ();
46 
47  assert (index >= 0);
48 
49  std::vector <exprVar *>::iterator orig;
50 
51  for (orig = variables_.begin ();
52  orig != variables_.end (); ++orig)
53 
54  if ((((*orig) -> Type () == VAR) || !same_var) &&
55  ((*orig) -> Index () == index)) // found it
56 
57  break;
58 
59  assert (orig != variables_ . end ());
60 
61  if (orig == variables_ . end ()) {
62  printf ("CouenneProblem::auxiliarize: no original variables correspond\n");
63  return;
64  }
65 
66  // all common expressions
67 
68  for (std::vector <expression *>::iterator i = commonexprs_.begin ();
69  i != commonexprs_.end (); ++i) {
70 
71  expression *body = *i;
72 
73  if (body) {
74 
75  if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
76  printf ("replacing within common expression [%d]: ", i - commonexprs_.begin ()); fflush (stdout); (*i) -> print (); printf ("\n");
77  }
78 
79  if ((body -> Type () == VAR) ||
80  (body -> Type () == AUX)) {
81 
82  if (body -> Index () == (*orig) -> Index ()) {
83 
84  delete body;
85  *i = new exprClone (subst);
86  }
87  } else body -> replace (*orig, subst);
88  }
89  }
90 
91  // all objectives
92 
93  for (std::vector <CouenneObjective *>::iterator i = objectives_.begin ();
94  i != objectives_.end (); ++i) {
95 
96  expression *body = (*i) -> Body ();
97 
98  if (body) {
99 
100  if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
101  printf ("replacing within objective: "); fflush (stdout); (*i) -> print ();
102  }
103 
104  if ((body -> Type () == VAR) ||
105  (body -> Type () == AUX)) {
106 
107  if (body -> Index () == (*orig) -> Index ()) {
108 
109  delete body;//(*i) -> Body ();
110  (*i) -> Body (new exprClone (subst));
111  }
112  } else body -> replace (*orig, subst);
113  }
114  }
115 
116  // and all constraints
117 
118  for (std::vector <CouenneConstraint *>::iterator i = constraints_.begin ();
119  i != constraints_.end (); ++i) {
120 
121  expression *body = (*i) -> Body ();
122 
123  if (body) {
124 
125  if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
126  printf ("replacing within constraint [%d]: ", i - constraints_.begin ()); fflush (stdout); (*i) -> print ();
127  }
128 
129  if ((body -> Type () == VAR) ||
130  (body -> Type () == AUX)) {
131 
132  if (body -> Index () == (*orig) -> Index ()) {
133 
134  delete body;//(*i) -> Body ();
135  (*i) -> Body (new exprClone (subst));
136  }
137  } else body -> replace (*orig, subst);
138  }
139  }
140 
141  // substitute it with w in all auxiliaries
142 
143  for (std::vector <exprVar *>::iterator i = variables_.begin ();
144  i != variables_.end (); ++i)
145 
146  if (((*i) -> Type () == AUX) && // replace in all aux's image
147  ((*i) -> Multiplicity () > 0) && // this variable is actually used
148  ((*i) -> Index () != (*orig) -> Index ())) { // skip same variable
149 
150  if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
151  printf ("replacing aux "); fflush (stdout); (*i) -> print ();
152  printf (" := "); fflush (stdout); (*i) -> Image () -> print ();
153  printf ("\n");
154  }
155 
156  expression *image = (*i) -> Image ();
157 
158  if ((image -> Type () == VAR) ||
159  (image -> Type () == AUX)) {
160 
161  if (image -> Index () == (*orig) -> Index ()) {
162 
163  delete image;
164  (*i) -> Image (new exprClone (subst));
165  //printf (" ---> "); (*i) -> Image () -> print ();
166  } //else (*i) -> Image () -> replace (*orig, aux);
167  } else image -> replace (*orig, subst);
168 
169  //printf ("\n");
170  }
171 
172  // replace it with new auxiliary
173 
174  if (same_var)
175  *orig = aux;
176 }
std::vector< CouenneObjective * > objectives_
Objectives.
DepGraph * graph_
Dependence (acyclic) graph: shows dependence of all auxiliary variables on one another and on origina...
void print(std::ostream &=std::cout)
Display current representation of problem: objective, linear and nonlinear constraints, and auxiliary variables.
Definition: problemIO.cpp:24
void replace(CouenneProblem *p, int wind, int xind)
std::vector< expression * > commonexprs_
AMPL&#39;s common expressions (read from AMPL through structures cexps and cexps1)
std::vector< CouenneConstraint * > constraints_
Constraints.
fint end
expression clone (points to another expression)
const Ipopt::EJournalCategory J_REFORMULATE(Ipopt::J_USER7)
std::vector< exprVar * > variables_
Variables (original, auxiliary, and defined)
variable-type operator
JnlstPtr jnlst_
SmartPointer to the Journalist.
Expression base class.
void auxiliarize(exprVar *, exprVar *=NULL)
Replace all occurrences of original variable with new aux given as argument.
Definition: auxiliarize.cpp:27