/home/coin/SVN-release/OS-2.0.0/Couenne/src/problem/writeAMPL.cpp

Go to the documentation of this file.
00001 /* $Id: writeAMPL.cpp 154 2009-06-16 18:52:53Z pbelotti $ */
00002 /*
00003  * Name:    extended.cpp
00004  * Author:  Pietro Belotti
00005  * Purpose: save problem in AMPL 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 
00017 // store problem in a .mod file (AMPL)
00018 
00019 void CouenneProblem::writeAMPL (const std::string &fname,  
00020                                 bool aux) {                
00021 
00022   std::ofstream f (fname.c_str ());
00023 
00024   f << std::setprecision (10);
00025 
00026   // original variables, integer and non //////////////////////////////////////////////
00027 
00028   f << "# Problem name: " << fname << std::endl << std::endl 
00029     << "# original variables" << std::endl << std::endl;
00030 
00031   for (int i=0; i < nVars (); i++) {
00032 
00033     f << "var ";
00034     variables_ [i] -> print (f);
00035     if (Lb (i) > - COUENNE_INFINITY + 1) f << " >= " << Lb (i);
00036     if (Ub (i) < + COUENNE_INFINITY - 1) f << " <= " << Ub (i);
00037     if (variables_ [i] -> isInteger ())   f << " integer";
00038     if (fabs (X (i)) < COUENNE_INFINITY)    
00039       f << " default " << X (i); 
00040     f << ';' << std::endl;
00041   }
00042 
00043 
00044   // defined (aux) variables, declaration ///////////////////////////////////////////
00045   /*
00046   if (aux) {
00047 
00048     initAuxs (x_, lb_, ub_);
00049 
00050     f << std::endl << "# auxiliary variables" << std::endl << std::endl;
00051 
00052     for (std::vector <exprVar *>::iterator i = variables_.begin ();
00053          i != variables_.end ();
00054          i++) 
00055 
00056       if ((*i) -> Type () == AUX) {
00057 
00058         exprAux *aux = dynamic_cast <exprAux *> (*i);
00059 
00060         if (aux -> Multiplicity () > 0) {
00061 
00062           f << "var "; (*i) -> print (f, false, this);
00063           //    f << " = ";  (*i) -> Image () -> print (f);
00064           CouNumber bound;
00065 
00066           if ((bound = (*((*i) -> Lb ())) ()) > - COUENNE_INFINITY) f << " >= " << bound;
00067           if ((bound = (*((*i) -> Ub ())) ()) <   COUENNE_INFINITY) f << " <= " << bound;
00068           if ((*i) -> isInteger ()) f << " integer";
00069 
00070           f << " default " << (*((*i) -> Image ())) () << ';' << std::endl;
00071         }
00072       }
00073   }
00074   */
00075 
00076   // objective function /////////////////////////////////////////////////////////////
00077 
00078   f << std::endl << "# objective" << std::endl << std::endl;
00079 
00080   //if (objectives_ [0] -> Sense () == MINIMIZE) 
00081   f << "minimize";
00082   //else                                         f << "maximize";
00083 
00084   f << " obj: ";  
00085   objectives_ [0] -> Body () -> print (f, !aux); 
00086   f << ';' << std::endl; 
00087 
00088 
00089   // defined (aux) variables, with formula ///////////////////////////////////////////
00090 
00091   if (aux) {
00092 
00093     f << std::endl << "# aux. variables defined" << std::endl << std::endl;
00094 
00095     for (int i=0; i < nVars (); i++)
00096 
00097       if ((variables_ [i] -> Type () == AUX) && 
00098           (variables_ [i] -> Multiplicity () > 0)) {
00099 
00100         f << "aux" << i << ": "; variables_ [i] -> print (f, false);
00101         f << " = ";  
00102 
00103         variables_ [i] -> Image () -> print (f, false);
00104         f << ';' << std::endl;
00105       }
00106   }
00107 
00108 
00109   // write constraints //////////////////////////////////////////////////////////////
00110 
00111   f << std::endl << "# constraints" << std::endl << std::endl;
00112 
00113   if (!aux) // print h_i(x,y) <= ub, >= lb
00114     for (std::vector <exprVar *>::iterator i = variables_.begin ();
00115          i != variables_.end ();
00116          ++i) 
00117 
00118       if (((*i) -> Type () == AUX) && 
00119           ((*i) -> Multiplicity () > 0)) {
00120         
00121         CouNumber bound;
00122 
00123         if ((bound = (*i) -> lb ()) > - COUENNE_INFINITY) {
00124           f << "conAuxLb" << (*i) -> Index () << ": ";
00125           (*i) -> print (f, true);
00126           f << ">= " << bound << ';' << std::endl;
00127         }
00128 
00129         if ((bound = (*i) -> ub ()) <   COUENNE_INFINITY) {
00130           f << "conAuxUb" << (*i) -> Index () << ": ";
00131           (*i) -> print (f, true);
00132           f << "<= " << bound << ';' << std::endl;
00133         }
00134       }
00135 
00136 
00137   for (int i=0; i < nCons (); i++) {
00138 
00139     // get numerical value of lower, upper bound
00140     CouNumber lb = (constraints_ [i] -> Lb () -> Value ()),
00141               ub = (constraints_ [i] -> Ub () -> Value ());
00142 
00143     f << "con" << i << ": ";
00144     constraints_ [i] -> Body () -> print (f, !aux);
00145 
00146     if (lb > - COUENNE_INFINITY + 1) {
00147       f << ' ';
00148       if (fabs (ub-lb) > COUENNE_EPS) 
00149         f << '>';
00150       f << "= " << lb << ';' << std::endl;
00151     }
00152     else  f << " <= " << ub << ';' << std::endl;
00153 
00154     // if range constraint, print it once again
00155 
00156     if ((   lb > - COUENNE_INFINITY + 1) 
00157         && (ub <   COUENNE_INFINITY - 1)
00158         && (fabs (ub-lb) > COUENNE_EPS)) {
00159 
00160       f << "con" << i << "_rng: ";
00161       constraints_ [i] -> Body () -> print (f, !aux);
00162       f << " <= " << ub << ';' << std::endl;
00163     }
00164   }
00165 
00166   f.close ();
00167 }

Generated on Mon Aug 3 03:02:21 2009 by  doxygen 1.4.7