/home/coin/SVN-release/OS-2.4.1/Couenne/src/problem/writeGAMS.cpp

Go to the documentation of this file.
00001 /* $Id: writeGAMS.cpp 606 2011-06-06 12:47:02Z stefan $
00002  *
00003  * Name:    writeGAMS.cpp
00004  * Author:  Pietro Belotti
00005  * Purpose: save a problem in GAMS format
00006  *
00007  * (C) Carnegie-Mellon University, 2006. 
00008  * This file is licensed under the Eclipse Public License (EPL)
00009  */
00010 
00011 #include <fstream>
00012 #include <iomanip> // to use the setprecision manipulator
00013 #include <cassert>
00014 
00015 #include "CouenneProblem.hpp"
00016 #include "CouenneProblemElem.hpp"
00017 #include "CouenneExprVar.hpp"
00018 
00019 using namespace Couenne;
00020 
00024 void CouenneProblem::writeGAMS (const std::string &fname) {
00025 
00026   std::ofstream f (fname.c_str ());
00027 
00028   f << std::setprecision (10);
00029 
00030   const int nline = 20;
00031 
00032   // header //////////////////////////////////////////////////////////////
00033 
00034   f << "* MINLP Written by Couenne (https://projects.coin-or.org/Couenne)" << std::endl
00035     << "* problem name: " << problemName_ << std::endl
00036     << "* " << nVars () << " variables, " << nCons () << " constraints" << std::endl << std::endl;
00037 
00038   // preamble ////////////////////////////////////////////////////////////
00039 
00040   f << "  variables   ";
00041 
00042   for (int i=0, j=0; i < nVars (); i++) 
00043     if (Var (i) -> Multiplicity () > 0) {
00044 
00045       if (j) f << ','; 
00046       if (!(++j % nline)) f << std::endl << "             ";
00047 
00048       Var (i) -> print (f);
00049 
00050       /*if (Var (i) -> isInteger ())
00051         if ((fabs (Var (i) -> lb ())     < COUENNE_EPS) && 
00052             (fabs (Var (i) -> ub () - 1) < COUENNE_EPS))
00053           f << 'b';
00054         else f << 'i';
00055       else f << 'x';
00056       f << Var (i) -> Index ();*/
00057     }
00058 
00059   f << ",objvar;" << std::endl << std::endl;
00060 
00061   bool 
00062     have_positive = false, 
00063     have_binary   = false,
00064     have_integer  = false;
00065 
00066   // check if there are positive variables
00067   for (int i=0; i < nVars (); i++)
00068     if ((Var (i) -> Multiplicity () > 0) &&
00069         (fabs (Var (i) -> lb ()) < COUENNE_EPS)  &&
00070         !(Var (i) -> isInteger ())) {
00071       //(fabs (Var (i) -> ub () - 1) < COUENNE_EPS))) {
00072 
00073       have_positive = true;
00074       break;
00075     }
00076 
00077   // check if there are binary variables
00078   for (int i=0; i < nVars (); i++)
00079     if ((Var (i) -> Multiplicity () > 0) &&
00080         Var (i) -> isInteger () &&
00081         (fabs (Var (i) -> lb ())     < COUENNE_EPS) && 
00082         (fabs (Var (i) -> ub () - 1) < COUENNE_EPS)) {
00083 
00084       have_binary = true;
00085       break;
00086     }
00087 
00088   // check if there are integer variables
00089   for (int i=0; i < nVars (); i++)
00090     if ((Var (i) -> Multiplicity () > 0) &&
00091         (Var (i) -> isInteger ()) &&
00092         ((fabs (Var (i) -> lb ())     > COUENNE_EPS) ||
00093          (fabs (Var (i) -> ub () - 1) > COUENNE_EPS))) {
00094 
00095       have_integer = true;
00096       break;
00097     }
00098 
00099   // positive /////////////////////////////////////////////////////////////////
00100   if (have_positive) {
00101 
00102     f << "  positive variables "; 
00103     // those with lower bound exactly zero (to save space...)
00104 
00105     for (int i=0, j=0; i < nVars (); i++) 
00106       if ((Var (i) -> Multiplicity () > 0) && 
00107           (fabs (Var (i) -> lb ()) < COUENNE_EPS)  &&
00108           !(Var (i) -> isInteger ())) {
00109           //(fabs (Var (i) -> ub () - 1) < COUENNE_EPS))) {
00110 
00111         if (j) f << ','; 
00112         if (!(++j % nline)) f << std::endl << "             ";
00113 
00114         Var (i) -> print (f);
00115 
00116         /*if (Var (i) -> isInteger ()) f << 'i';
00117         else                         f << 'x';
00118         f << Var (i) -> Index ();*/
00119       }
00120 
00121     f << ';' << std::endl << std::endl;
00122   }
00123 
00124   // binary /////////////////////////////////////////////////////////////////
00125   if (have_binary) {
00126 
00127     f << "  binary variables ";
00128 
00129     for (int i=0, j=0; i < nVars (); i++) 
00130       if ((Var (i) -> Multiplicity () > 0) && 
00131           Var (i) -> isInteger () &&
00132           (fabs (Var (i) -> lb ())     < COUENNE_EPS) && 
00133           (fabs (Var (i) -> ub () - 1) < COUENNE_EPS)) {
00134 
00135         if (j) f << ','; 
00136         if (!(++j % nline)) f << std::endl << "             ";
00137         Var (i) -> print (f);
00138         //f << 'b' << Var (i) -> Index ();
00139       }
00140     f << ';' << std::endl << std::endl;
00141   }
00142 
00143   // integer /////////////////////////////////////////////////////////////////
00144   if (have_integer) {
00145 
00146     f << "  integer variables ";
00147 
00148     for (int i=0, j=0; i < nVars (); i++) 
00149       if ((Var (i) -> Multiplicity () > 0) &&
00150           (Var (i) -> isInteger ()) &&
00151           ((fabs (Var (i) -> lb ())     > COUENNE_EPS) ||
00152            (fabs (Var (i) -> ub () - 1) > COUENNE_EPS))) {
00153 
00154         if (j) f << ','; 
00155         if (!(++j % nline)) f << std::endl << "             ";
00156         Var (i) -> print (f);
00157         //f << 'i' << Var (i) -> Index ();
00158       }
00159     f << ';' << std::endl << std::endl;
00160   }
00161 
00162   f << "  equations ";
00163 
00164   int i=0, j=0;
00165 
00166   bool no_eqns = true;
00167 
00168   for (; i < nCons (); i++) {
00169 
00170     if (j) f << ','; 
00171     if (!(++j % nline)) f << std::endl << "                  ";
00172     f << 'e' << j;
00173     no_eqns = false;
00174   }
00175 
00176   for (; i < nVars (); i++) 
00177     if ((Var (i) -> Type () == AUX) &&
00178         (Var (i) -> Multiplicity () > 0)) {
00179 
00180       if (j) f << ','; 
00181       if (!(++j % nline)) f << std::endl << "                ";
00182       f << 'e' << j;
00183       no_eqns = false;
00184     }
00185 
00186   if (!no_eqns) f << ',';
00187   f << "objdef;" << std::endl << std::endl;
00188 
00189   // equations ///////////////////////////////////////////////////////////
00190 
00191   i=j=0;
00192 
00193   for (; i < nCons (); i++) {
00194 
00195     f << 'e' << ++j << "..  ";
00196 
00197     CouNumber 
00198       lb = (*(Con (i) -> Lb ())) (),
00199       ub = (*(Con (i) -> Ub ())) ();
00200 
00201     assert ((lb > -COUENNE_INFINITY) || 
00202             (ub <  COUENNE_INFINITY));
00203 
00204     if (fabs (lb - ub) < COUENNE_EPS) {
00205       Con (i) -> Body () -> print (f);
00206       f << " =E= " << lb;
00207     } else if (lb > -COUENNE_INFINITY) {
00208       Con (i) -> Body () -> print (f);
00209       f << " =G= " << lb;
00210     } else if (ub <  COUENNE_INFINITY) {
00211       Con (i) -> Body () -> print (f);
00212       f << " =L= " << ub;
00213     }
00214 
00215     f << ';' << std::endl << std::endl;
00216   }
00217 
00218   for (; i < nVars (); i++) 
00219     if ((Var (i) -> Type () == AUX) &&
00220         (Var (i) -> Multiplicity () > 0)) {
00221 
00222       f << 'e' << ++j << "..  ";
00223       f << " - ";
00224       Var (i) -> print (f); 
00225       f << " + ";
00226       Var (i) -> Image () -> print ();
00227       f << " =E= 0;" << std::endl << std::endl;
00228     }
00229 
00230   f << "objdef.. - objvar + ";
00231   Obj (0) -> Body () -> print (f);
00232   f << " =E= 0;" << std::endl << std::endl;
00233 
00234   // initial levels //////////////////////////////////////////////////////
00235 
00236   for (int i=0; i < nVars (); i++) 
00237     if (Var (i) -> Multiplicity () > 0) {
00238 
00239       Var (i) -> print (f); 
00240       f << ".l = " << X (i) << ';' << std::endl;
00241 
00242       if ((fabs (Lb (i)) > COUENNE_EPS) && (Lb (i) > -COUENNE_INFINITY)) {
00243         Var (i) -> print (f); 
00244         f << ".lo = " << Lb (i) << ';' << std::endl;
00245       }
00246 
00247       if (Ub (i) < COUENNE_INFINITY) {
00248         Var (i) -> print (f); 
00249         f << ".up = " << Ub (i) << ';' << std::endl;
00250       }
00251     }
00252 
00253   // bounds //////////////////////////////////////////////////////////////
00254 
00255   f << "Model m / all /;" << std::endl
00256     << "m.limrow=0; m.limcol=0;" << std::endl
00257     << "Solve m using MINLP minimizing objvar;" << std::endl;
00258 
00259   f.close ();
00260 }

Generated on Thu Nov 10 03:05:46 2011 by  doxygen 1.4.7