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

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

Generated on Thu Aug 5 03:02:57 2010 by  doxygen 1.4.7