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

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

Generated on Tue Mar 30 03:04:38 2010 by  doxygen 1.4.7