/home/coin/SVN-release/OS-2.4.2/Couenne/src/problem/problemIO.cpp

Go to the documentation of this file.
00001 /* $Id: problemIO.cpp 752 2011-08-08 03:45:07Z pbelotti $
00002  *
00003  * Name:    problemIO.cpp
00004  * Author:  Pietro Belotti
00005  * Purpose: methods of the class CouenneProblem
00006  *
00007  * (C) Carnegie-Mellon University, 2006-10.
00008  * This file is licensed under the Eclipse Public License (EPL)
00009  */
00010 
00011 #include <vector>
00012 #include <fstream>
00013 
00014 #include "CoinHelperFunctions.hpp"
00015 
00016 #include "CouenneExpression.hpp"
00017 #include "CouenneExprAux.hpp"
00018 #include "CouenneProblem.hpp"
00019 #include "CouenneProblemElem.hpp"
00020 
00021 using namespace Couenne;
00022 
00023 // output content of the problem
00024 void CouenneProblem::print (std::ostream &out) {
00025 
00026   out << "objectives:" << std::endl;
00027   for (std::vector <CouenneObjective *>::iterator i = objectives_.begin ();
00028        i != objectives_.end (); ++i)
00029     (*i) -> print (out);
00030 
00031   out << "constraints:" << std::endl;
00032   for (std::vector <CouenneConstraint *>::iterator i = constraints_.begin ();
00033        i != constraints_.end (); ++i)
00034     (*i) -> print (out);
00035 
00036   out << "variables:" << std::endl;
00037   for (std::vector <exprVar *>::iterator i = variables_.begin ();
00038        i != variables_.end (); ++i)
00039 
00040     if (((*i) -> Type () != AUX) || 
00041         ((*i) -> Multiplicity () > 0) ||
00042         (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE))) {
00043 
00044       (*i) -> print (out);
00045 
00046       if (((*i) -> Type () == AUX) && 
00047           (((*i) -> Multiplicity () > 0) ||
00048            (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)))) {
00049 
00050         out << " (r:" << (*i) -> rank () 
00051             << ", m:" << (*i) -> Multiplicity () << ") " 
00052             << (((*i) -> sign () == expression::AUX_EQ)  ? ':' :
00053                 ((*i) -> sign () == expression::AUX_GEQ) ? '>' : 
00054                 ((*i) -> sign () == expression::AUX_LEQ) ? '<' : '?')
00055             << "= ";// << "<" << (*i) -> Image () << "> ";
00056 
00057         if ((*i) -> Image ())
00058           (*i) -> Image () -> print (out, false); 
00059       }
00060 
00061       CouNumber 
00062         lb = domain_.lb ((*i) -> Index ()),
00063         ub = domain_.ub ((*i) -> Index ());
00064 
00065       if ((fabs (lb)     < COUENNE_EPS) &&
00066           (fabs (ub - 1) < COUENNE_EPS) &&
00067           (*i) -> isInteger ()) out << " binary";
00068       else {
00069 
00070         out << " [ ";
00071 
00072         if (lb < -COUENNE_INFINITY) out << "-inf"; else out << lb; out << " , ";
00073         if (ub >  COUENNE_INFINITY) out <<  "inf"; else out << ub; out << " ]";
00074 
00075         if ((*i) -> isInteger ()) out << " integer";
00076       }
00077 
00078       out << std::endl;
00079     }
00080 
00081   if (commonexprs_.size ()) {
00082     out << "common expressions:" << std::endl;
00083     for (std::vector <expression *>::iterator i = commonexprs_.begin ();
00084          i != commonexprs_.end (); ++i) {
00085       out << "v_"    
00086           << nOrigVars_ - commonexprs_ . size () + i - commonexprs_.begin () << " := ";
00087       (*i) -> print (out);
00088       out << std::endl;
00089     }
00090   }
00091 
00092   if (optimum_) {
00093     out << "best known solution: (" << *optimum_;
00094     for (int i=1; i < nVars (); i++)
00095       out << ' ' << optimum_ [i];
00096     out << ')' << std::endl;
00097   }
00098 
00099   if (fabs (bestObj_) < COUENNE_INFINITY)
00100     out << "best known objective: " << bestObj_ << std::endl;
00101 
00102   out << "end" << std::endl;
00103 } 
00104 
00105 
00107 bool CouenneProblem::readOptimum (std::string *fname) {
00108 
00109   FILE *f;
00110 
00111   if (fname == NULL) {
00112 
00113     fname = &problemName_;
00114 
00115     int base = fname -> rfind ('/'), size;
00116     if (base < 0) base = 0; else base++;
00117 
00118     size = fname -> find  ('.', base) - base;
00119 
00120     char *filename = new char [size+5];
00121     CoinFillN (filename, size+5, (char) 0);
00122     fname -> copy (filename, 1+size, base);
00123     strcat (filename, "txt");
00124     f = fopen (filename, "r");
00125     delete [] filename;
00126   } else f = fopen (fname -> c_str (), "r");
00127 
00128   if (!f) return false;
00129 
00130   optimum_ = (CouNumber *) realloc (optimum_, nVars () * sizeof (CouNumber));
00131 
00132   CoinFillN (optimum_, nVars (), 0.);
00133 
00134   // read optimal objective function first
00135   if (fscanf (f, "%lf", &bestObj_) < 1) {
00136     fclose (f);
00137     printf ("Couenne: warning, could not read objective from file \"%s\"\n", fname -> c_str ());
00138     return false;
00139   }
00140 
00141   // read optimal values of variables
00142   for (int i = 0; i < nOrigVars_; i++)
00143     if (fscanf (f, "%lf", optimum_ + i) < 1) {
00144       fclose (f);
00145       printf ("Couenne: warning, could not read optimal value of x_%d from file \"%s\"\n", i, fname -> c_str ());
00146       return false;
00147     } else
00148       if (variables_ [i] -> isDefinedInteger ()) 
00149         optimum_ [i] = ceil (optimum_ [i] - .5);
00150 
00151   if (opt_window_ < 1e50) // restrict solution space around known optimum
00152     for (int i = 0; i < nOrigVars_; i++) {
00153       Lb (i) = CoinMax (Lb (i), optimum_ [i] - opt_window_ * (1 + fabs (optimum_ [i])));
00154       Ub (i) = CoinMin (Ub (i), optimum_ [i] + opt_window_ * (1 + fabs (optimum_ [i])));
00155     }
00156 
00157   // expand solution to auxiliary space
00158   getAuxs (optimum_);
00159 
00160   fclose (f);
00161   return true;
00162 }

Generated on Wed Nov 30 03:04:08 2011 by  doxygen 1.4.7