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

Go to the documentation of this file.
00001 /* $Id: auxiliarize.cpp 268 2009-10-22 03:41:03Z 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-08.
00009  * This file is licensed under the Common Public License (CPL)
00010  */
00011 
00012 #include "CouenneProblem.hpp"
00013 #include "exprClone.hpp"
00014 
00015 //#define DEBUG
00016 
00020 
00021 void CouenneProblem::auxiliarize (exprVar *aux, exprVar *subst) {
00022 
00023 #ifdef DEBUG
00024   printf ("replacing  "); if (aux)   aux   -> print (); 
00025   printf (" with ");      if (subst) subst -> print (); 
00026   printf ("\n");
00027 #endif
00028 
00029   bool same_var = (subst == NULL);
00030 
00031   if (!subst) 
00032     subst = aux;
00033 
00034   // find original variable with index = w -> Index ()
00035 
00036   int index = aux -> Index ();
00037 
00038   assert (index >= 0);
00039 
00040   std::vector <exprVar *>::iterator orig;
00041 
00042   for (orig  = variables_.begin ();
00043        orig != variables_.end (); ++orig)
00044 
00045     if ((((*orig) -> Type () == VAR) || !same_var) && 
00046         ((*orig) -> Index () == index)) // found it
00047 
00048       break;
00049 
00050   if (orig == variables_ . end ()) {
00051     printf ("CouenneProblem::auxiliarize: no original variables correspond\n");
00052     return;
00053   }
00054 
00055   // all objectives
00056 
00057   for (std::vector <CouenneObjective *>::iterator i = objectives_.begin ();
00058        i != objectives_.end (); ++i) {
00059 
00060     expression *body = (*i) -> Body ();
00061 
00062     if (body) {
00063       if ((body -> Type () == VAR) || 
00064           (body -> Type () == AUX)) {
00065 
00066         if (body -> Index () == (*orig) -> Index ()) {
00067       
00068           delete body;//(*i) -> Body ();
00069           (*i) -> Body (new exprClone (subst));
00070         }
00071       } else body -> replace (*orig, subst);
00072     }
00073   }
00074 
00075   // and all constraints
00076 
00077   for (std::vector <CouenneConstraint *>::iterator i = constraints_.begin ();
00078        i != constraints_.end (); ++i) {
00079 
00080     expression *body = (*i) -> Body ();
00081 
00082     if (body) {
00083       if ((body -> Type () == VAR) ||
00084           (body -> Type () == AUX)) {
00085 
00086         if (body -> Index () == (*orig) -> Index ()) {
00087       
00088           delete body;//(*i) -> Body ();
00089           (*i) -> Body (new exprClone (subst));
00090         }
00091       } else body -> replace (*orig, subst);
00092     }
00093   }
00094 
00095   // substitute it with w in all auxiliaries
00096 
00097   for (std::vector <exprVar *>::iterator i = variables_.begin ();
00098        i != variables_.end (); ++i)
00099 
00100     if (((*i) -> Type () == AUX) &&                  // replace in all aux's image
00101         //((*i) -> Multiplicity () > 0) &&             // this variable is actually used
00102         ((*i) -> Index () != (*orig) -> Index ())) { // skip same variable
00103 
00104 #ifdef DEBUG
00105       printf ("replacing aux "); (*i) -> print (); 
00106       printf (" := "); (*i) -> Image () -> print (); 
00107       printf ("\n");
00108 #endif
00109 
00110       expression *image = (*i) -> Image ();
00111 
00112       if ((image -> Type () == VAR) || 
00113           (image -> Type () == AUX)) {
00114 
00115         if (image -> Index () == (*orig) -> Index ()) {
00116 
00117           delete image;
00118           (*i) -> Image (new exprClone (subst));
00119           //printf (" ---> "); (*i) -> Image () -> print (); 
00120         } //else (*i) -> Image () -> replace (*orig, aux);
00121       } else image  -> replace (*orig, subst);
00122 
00123       //printf ("\n");
00124     }
00125 
00126   // replace it with new auxiliary
00127 
00128   if (same_var)
00129     *orig = aux;
00130 }

Generated on Mon May 3 03:05:22 2010 by  doxygen 1.4.7