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

Go to the documentation of this file.
00001 /* $Id: problemIO.cpp 154 2009-06-16 18:52:53Z pbelotti $ */
00002 /*
00003  * Name:    problemIO.cpp
00004  * Author:  Pietro Belotti
00005  * Purpose: methods of the class CouenneProblem
00006  *
00007  * (C) Carnegie-Mellon University, 2006. 
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         if ((*i) -> Image ())
00049           (*i) -> Image () -> print (out, false); 
00050       }
00051 
00052       CouNumber 
00053         lb = domain_.lb ((*i) -> Index ()),
00054         ub = domain_.ub ((*i) -> Index ());
00055 
00056       if ((fabs (lb)     < COUENNE_EPS) &&
00057           (fabs (ub - 1) < COUENNE_EPS) &&
00058           (*i) -> isInteger ()) out << " binary";
00059       else {
00060 
00061         out << " [ " << lb << " , " << ub << " ]";
00062         if ((*i) -> isInteger ()) out << " integer";
00063       }
00064 
00065       out << std::endl;
00066     }
00067 
00068   if (commonexprs_.size ()) {
00069     out << "common expressions:" << std::endl;
00070     for (std::vector <expression *>::iterator i = commonexprs_.begin ();
00071          i != commonexprs_.end (); ++i) {
00072       (*i) -> print (out);
00073       out << std::endl;
00074     }
00075   }
00076 
00077   if (optimum_) {
00078     out << "best known solution: (" << *optimum_;
00079     for (int i=1; i < nVars (); i++)
00080       out << ' ' << optimum_ [i];
00081     out << ')' << std::endl;
00082   }
00083 
00084   if (fabs (bestObj_) < COUENNE_INFINITY)
00085     out << "best known objective: " << bestObj_ << std::endl;
00086 
00087   out << "end" << std::endl;
00088 } 
00089 
00090 
00092 bool CouenneProblem::readOptimum (std::string *fname) {
00093 
00094   FILE *f;
00095 
00096   if (fname == NULL) {
00097 
00098     fname = &problemName_;
00099 
00100     int base = fname -> rfind ('/'), size;
00101     if (base < 0) base = 0; else base++;
00102 
00103     size = fname -> find  ('.', base) - base;
00104 
00105     char *filename = new char [size+5];
00106     CoinFillN (filename, size+5, (char) 0);
00107     fname -> copy (filename, 1+size, base);
00108     strcat (filename, "txt");
00109     f = fopen (filename, "r");
00110     delete [] filename;
00111   } else f = fopen (fname -> c_str (), "r");
00112 
00113   if (!f) return false;
00114 
00115   optimum_ = (CouNumber *) realloc (optimum_, nVars () * sizeof (CouNumber));
00116 
00117   CoinFillN (optimum_, nVars (), 0.);
00118 
00119   // read optimal objective function first
00120   if (fscanf (f, "%lf", &bestObj_) < 1) {
00121     fclose (f);
00122     printf ("could not read objective from file \"%s\"\n", fname -> c_str ());
00123     return false;
00124   }
00125 
00126   // read optimal values of variables
00127   for (int i = 0; i < nOrigVars_; i++)
00128     if (fscanf (f, "%lf", optimum_ + i) < 1) {
00129       fclose (f);
00130       printf ("could not read optimal value of x_%d from file \"%s\"\n", i, fname -> c_str ());
00131       return false;
00132     }
00133 
00134   if (opt_window_ < 1e50) // restrict solution space around known optimum
00135     for (int i = 0; i < nOrigVars_; i++) {
00136       Lb (i) = CoinMax (Lb (i), optimum_ [i] - opt_window_ * (1 + fabs (optimum_ [i])));
00137       Ub (i) = CoinMin (Ub (i), optimum_ [i] + opt_window_ * (1 + fabs (optimum_ [i])));
00138     }
00139 
00140   // expand solution to auxiliary space
00141   getAuxs (optimum_);
00142 
00143   fclose (f);
00144   return true;
00145 }
00146 
00147 
00149 void CouenneProblem::readCutoff (const std::string &fname) {
00150 
00151   CouNumber val;
00152 
00153   FILE *f = fopen (fname.c_str (), "r");
00154   if (!f) return;
00155 
00156   if (fscanf (f, "%lf", &val) < 1) 
00157     return;
00158 
00159   setCutOff (val);
00160 }

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