/home/coin/SVN-release/OS-2.4.0/Couenne/src/standardize/auxiliarize.cpp

Go to the documentation of this file.
00001 /* $Id: auxiliarize.cpp 751 2011-08-05 22:31:55Z pbelotti $
00002  *
00003  * Name:    auxiliarize.cpp
00004  * Author:  Pietro Belotti
00005  * Purpose: replace occurrences of original variable in a problem with
00006  *          auxiliary with the same index
00007  *
00008  * (C) Carnegie-Mellon University, 2007-10.
00009  * This file is licensed under the Eclipse Public License (EPL)
00010  */
00011 
00012 #include "CouenneProblem.hpp"
00013 #include "CouenneProblemElem.hpp"
00014 #include "CouenneExprAux.hpp"
00015 #include "CouenneExprClone.hpp"
00016 #include "CouenneDepGraph.hpp"
00017 
00018 #include <cstdio>
00019 #include <cassert>
00020 
00021 using namespace Couenne;
00022 
00026 
00027 void CouenneProblem::auxiliarize (exprVar *aux, exprVar *subst) {
00028 
00029   if (graph_ && subst && aux -> Index () != subst -> Index ())
00030     graph_ -> replaceIndex (aux -> Index (), subst -> Index ());
00031 
00032   if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
00033     printf ("replacing  "); if (aux)   aux   -> print (); 
00034     if (subst) {printf (" with "); subst -> print ();}
00035     printf ("\n");
00036   }
00037 
00038   bool same_var = (subst == NULL);
00039 
00040   if (!subst) 
00041     subst = aux;
00042 
00043   // find original variable with index = w -> Index ()
00044 
00045   int index = aux -> Index ();
00046 
00047   assert (index >= 0);
00048 
00049   std::vector <exprVar *>::iterator orig;
00050 
00051   for (orig  = variables_.begin ();
00052        orig != variables_.end (); ++orig)
00053 
00054     if ((((*orig) -> Type () == VAR) || !same_var) && 
00055         ((*orig) -> Index () == index)) // found it
00056 
00057       break;
00058 
00059   if (orig == variables_ . end ()) {
00060     printf ("CouenneProblem::auxiliarize: no original variables correspond\n");
00061     return;
00062   }
00063 
00064   // all common expressions
00065 
00066   for (std::vector <expression *>::iterator i = commonexprs_.begin ();
00067        i != commonexprs_.end (); ++i) {
00068 
00069     expression *body = *i;
00070 
00071     if (body) {
00072 
00073       if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
00074         printf ("replacing within common expression [%d]: ", i - commonexprs_.begin ()); fflush (stdout); (*i) -> print (); printf ("\n");
00075       }
00076 
00077       if ((body -> Type () == VAR) || 
00078           (body -> Type () == AUX)) {
00079 
00080         if (body -> Index () == (*orig) -> Index ()) {
00081       
00082           delete body;
00083           *i = new exprClone (subst);
00084         }
00085       } else body -> replace (*orig, subst);
00086     }
00087   }
00088 
00089   // all objectives
00090 
00091   for (std::vector <CouenneObjective *>::iterator i = objectives_.begin ();
00092        i != objectives_.end (); ++i) {
00093 
00094     expression *body = (*i) -> Body ();
00095 
00096     if (body) {
00097 
00098       if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
00099         printf ("replacing within objective: "); fflush (stdout); (*i) -> print (); 
00100       }
00101 
00102       if ((body -> Type () == VAR) || 
00103           (body -> Type () == AUX)) {
00104 
00105         if (body -> Index () == (*orig) -> Index ()) {
00106       
00107           delete body;//(*i) -> Body ();
00108           (*i) -> Body (new exprClone (subst));
00109         }
00110       } else body -> replace (*orig, subst);
00111     }
00112   }
00113 
00114   // and all constraints
00115 
00116   for (std::vector <CouenneConstraint *>::iterator i = constraints_.begin ();
00117        i != constraints_.end (); ++i) {
00118 
00119     expression *body = (*i) -> Body ();
00120 
00121     if (body) {
00122 
00123       if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
00124         printf ("replacing within constraint [%d]: ", i - constraints_.begin ()); fflush (stdout); (*i) -> print (); 
00125       }
00126 
00127       if ((body -> Type () == VAR) ||
00128           (body -> Type () == AUX)) {
00129 
00130         if (body -> Index () == (*orig) -> Index ()) {
00131       
00132           delete body;//(*i) -> Body ();
00133           (*i) -> Body (new exprClone (subst));
00134         }
00135       } else body -> replace (*orig, subst);
00136     }
00137   }
00138 
00139   // substitute it with w in all auxiliaries
00140 
00141   for (std::vector <exprVar *>::iterator i = variables_.begin ();
00142        i != variables_.end (); ++i)
00143 
00144     if (((*i) -> Type () == AUX) &&                  // replace in all aux's image
00145         ((*i) -> Multiplicity () > 0) &&             // this variable is actually used
00146         ((*i) -> Index () != (*orig) -> Index ())) { // skip same variable
00147 
00148       if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
00149         printf ("replacing aux "); fflush (stdout); (*i) -> print (); 
00150         printf (" := "); fflush (stdout); (*i) -> Image () -> print (); 
00151         printf ("\n");
00152       }
00153 
00154       expression *image = (*i) -> Image ();
00155 
00156       if ((image -> Type () == VAR) || 
00157           (image -> Type () == AUX)) {
00158 
00159         if (image -> Index () == (*orig) -> Index ()) {
00160 
00161           delete image;
00162           (*i) -> Image (new exprClone (subst));
00163           //printf (" ---> "); (*i) -> Image () -> print (); 
00164         } //else (*i) -> Image () -> replace (*orig, aux);
00165       } else image  -> replace (*orig, subst);
00166 
00167       //printf ("\n");
00168     }
00169 
00170   // replace it with new auxiliary
00171 
00172   if (same_var)
00173     *orig = aux;
00174 }

Generated on Thu Sep 22 03:05:59 2011 by  doxygen 1.4.7