00001
00002
00003
00004
00005
00006
00007
00008
00009
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
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))
00056
00057 break;
00058
00059 assert (orig != variables_ . end ());
00060
00061 if (orig == variables_ . end ()) {
00062 printf ("CouenneProblem::auxiliarize: no original variables correspond\n");
00063 return;
00064 }
00065
00066
00067
00068 for (std::vector <expression *>::iterator i = commonexprs_.begin ();
00069 i != commonexprs_.end (); ++i) {
00070
00071 expression *body = *i;
00072
00073 if (body) {
00074
00075 if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
00076 printf ("replacing within common expression [%d]: ", i - commonexprs_.begin ()); fflush (stdout); (*i) -> print (); printf ("\n");
00077 }
00078
00079 if ((body -> Type () == VAR) ||
00080 (body -> Type () == AUX)) {
00081
00082 if (body -> Index () == (*orig) -> Index ()) {
00083
00084 delete body;
00085 *i = new exprClone (subst);
00086 }
00087 } else body -> replace (*orig, subst);
00088 }
00089 }
00090
00091
00092
00093 for (std::vector <CouenneObjective *>::iterator i = objectives_.begin ();
00094 i != objectives_.end (); ++i) {
00095
00096 expression *body = (*i) -> Body ();
00097
00098 if (body) {
00099
00100 if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
00101 printf ("replacing within objective: "); fflush (stdout); (*i) -> print ();
00102 }
00103
00104 if ((body -> Type () == VAR) ||
00105 (body -> Type () == AUX)) {
00106
00107 if (body -> Index () == (*orig) -> Index ()) {
00108
00109 delete body;
00110 (*i) -> Body (new exprClone (subst));
00111 }
00112 } else body -> replace (*orig, subst);
00113 }
00114 }
00115
00116
00117
00118 for (std::vector <CouenneConstraint *>::iterator i = constraints_.begin ();
00119 i != constraints_.end (); ++i) {
00120
00121 expression *body = (*i) -> Body ();
00122
00123 if (body) {
00124
00125 if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
00126 printf ("replacing within constraint [%d]: ", i - constraints_.begin ()); fflush (stdout); (*i) -> print ();
00127 }
00128
00129 if ((body -> Type () == VAR) ||
00130 (body -> Type () == AUX)) {
00131
00132 if (body -> Index () == (*orig) -> Index ()) {
00133
00134 delete body;
00135 (*i) -> Body (new exprClone (subst));
00136 }
00137 } else body -> replace (*orig, subst);
00138 }
00139 }
00140
00141
00142
00143 for (std::vector <exprVar *>::iterator i = variables_.begin ();
00144 i != variables_.end (); ++i)
00145
00146 if (((*i) -> Type () == AUX) &&
00147 ((*i) -> Multiplicity () > 0) &&
00148 ((*i) -> Index () != (*orig) -> Index ())) {
00149
00150 if (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)) {
00151 printf ("replacing aux "); fflush (stdout); (*i) -> print ();
00152 printf (" := "); fflush (stdout); (*i) -> Image () -> print ();
00153 printf ("\n");
00154 }
00155
00156 expression *image = (*i) -> Image ();
00157
00158 if ((image -> Type () == VAR) ||
00159 (image -> Type () == AUX)) {
00160
00161 if (image -> Index () == (*orig) -> Index ()) {
00162
00163 delete image;
00164 (*i) -> Image (new exprClone (subst));
00165
00166 }
00167 } else image -> replace (*orig, subst);
00168
00169
00170 }
00171
00172
00173
00174 if (same_var)
00175 *orig = aux;
00176 }