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

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

Generated on Thu Sep 22 03:05:59 2011 by  doxygen 1.4.7