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

Go to the documentation of this file.
00001 /* $Id: auxiliarize.cpp 154 2009-06-16 18:52:53Z 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) -> Index () != (*orig) -> Index ())) { // skip same variable
00102 
00103 #ifdef DEBUG
00104       //printf ("replacing aux "); (*i) -> print (); 
00105       //printf (" := "); (*i) -> Image () -> print (); 
00106       //printf ("\n");
00107 #endif
00108 
00109       expression *image = (*i) -> Image ();
00110 
00111       if ((image -> Type () == VAR) || 
00112           (image -> Type () == AUX)) {
00113 
00114         if (image -> Index () == (*orig) -> Index ()) {
00115 
00116           delete image;
00117           (*i) -> Image (new exprClone (subst));
00118           //printf (" ---> "); (*i) -> Image () -> print (); 
00119         } //else (*i) -> Image () -> replace (*orig, aux);
00120       } else image  -> replace (*orig, subst);
00121 
00122       //printf ("\n");
00123     }
00124 
00125   // replace it with new auxiliary
00126 
00127   if (same_var)
00128     *orig = aux;
00129 }

Generated on Thu Oct 8 03:02:57 2009 by  doxygen 1.4.7