problemIO.cpp
Go to the documentation of this file.
1 /* $Id: problemIO.cpp 752 2011-08-08 03:45:07Z pbelotti $
2  *
3  * Name: problemIO.cpp
4  * Author: Pietro Belotti
5  * Purpose: methods of the class CouenneProblem
6  *
7  * (C) Carnegie-Mellon University, 2006-10.
8  * This file is licensed under the Eclipse Public License (EPL)
9  */
10 
11 #include <vector>
12 #include <fstream>
13 
14 #include "CoinHelperFunctions.hpp"
15 
16 #include "CouenneExpression.hpp"
17 #include "CouenneExprAux.hpp"
18 #include "CouenneProblem.hpp"
19 #include "CouenneProblemElem.hpp"
20 
21 using namespace Couenne;
22 
23 // output content of the problem
24 void CouenneProblem::print (std::ostream &out) {
25 
26  out << "objectives:" << std::endl;
27  for (std::vector <CouenneObjective *>::iterator i = objectives_.begin ();
28  i != objectives_.end (); ++i)
29  (*i) -> print (out);
30 
31  out << "constraints:" << std::endl;
32  for (std::vector <CouenneConstraint *>::iterator i = constraints_.begin ();
33  i != constraints_.end (); ++i)
34  (*i) -> print (out);
35 
36  out << "variables:" << std::endl;
37  for (std::vector <exprVar *>::iterator i = variables_.begin ();
38  i != variables_.end (); ++i)
39 
40  if (((*i) -> Type () != AUX) ||
41  ((*i) -> Multiplicity () > 0) ||
42  (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE))) {
43 
44  (*i) -> print (out);
45 
46  if (((*i) -> Type () == AUX) &&
47  (((*i) -> Multiplicity () > 0) ||
48  (jnlst_ -> ProduceOutput (Ipopt::J_ALL, J_REFORMULATE)))) {
49 
50  out << " (r:" << (*i) -> rank ()
51  << ", m:" << (*i) -> Multiplicity () << ") "
52  << (((*i) -> sign () == expression::AUX_EQ) ? ':' :
53  ((*i) -> sign () == expression::AUX_GEQ) ? '>' :
54  ((*i) -> sign () == expression::AUX_LEQ) ? '<' : '?')
55  << "= ";// << "<" << (*i) -> Image () << "> ";
56 
57  if ((*i) -> Image ())
58  (*i) -> Image () -> print (out, false);
59  }
60 
61  CouNumber
62  lb = domain_.lb ((*i) -> Index ()),
63  ub = domain_.ub ((*i) -> Index ());
64 
65  if ((fabs (lb) < COUENNE_EPS) &&
66  (fabs (ub - 1) < COUENNE_EPS) &&
67  (*i) -> isInteger ()) out << " binary";
68  else {
69 
70  out << " [ ";
71 
72  if (lb < -COUENNE_INFINITY) out << "-inf"; else out << lb; out << " , ";
73  if (ub > COUENNE_INFINITY) out << "inf"; else out << ub; out << " ]";
74 
75  if ((*i) -> isInteger ()) out << " integer";
76  }
77 
78  out << std::endl;
79  }
80 
81  if (commonexprs_.size ()) {
82  out << "common expressions:" << std::endl;
83  for (std::vector <expression *>::iterator i = commonexprs_.begin ();
84  i != commonexprs_.end (); ++i) {
85  out << "v_"
86  << nOrigVars_ - commonexprs_ . size () + i - commonexprs_.begin () << " := ";
87  (*i) -> print (out);
88  out << std::endl;
89  }
90  }
91 
92  if (optimum_) {
93  out << "best known solution: (" << *optimum_;
94  for (int i=1; i < nVars (); i++)
95  out << ' ' << optimum_ [i];
96  out << ')' << std::endl;
97  }
98 
99  if (fabs (bestObj_) < COUENNE_INFINITY)
100  out << "best known objective: " << bestObj_ << std::endl;
101 
102  out << "end" << std::endl;
103 }
104 
105 
107 bool CouenneProblem::readOptimum (std::string *fname) {
108 
109  FILE *f;
110 
111  if (fname == NULL) {
112 
113  fname = &problemName_;
114 
115  int base = fname -> rfind ('/'), size;
116  if (base < 0) base = 0; else base++;
117 
118  size = fname -> find ('.', base) - base;
119 
120  char *filename = new char [size+5];
121  CoinFillN (filename, size+5, (char) 0);
122  fname -> copy (filename, 1+size, base);
123  strcat (filename, "txt");
124  f = fopen (filename, "r");
125  delete [] filename;
126  } else f = fopen (fname -> c_str (), "r");
127 
128  if (!f) return false;
129 
130  optimum_ = (CouNumber *) realloc (optimum_, nVars () * sizeof (CouNumber));
131 
132  CoinFillN (optimum_, nVars (), 0.);
133 
134  // read optimal objective function first
135  if (fscanf (f, "%lf", &bestObj_) < 1) {
136  fclose (f);
137  printf ("Couenne: warning, could not read objective from file \"%s\"\n", fname -> c_str ());
138  return false;
139  }
140 
141  // read optimal values of variables
142  for (int i = 0; i < nOrigVars_; i++)
143  if (fscanf (f, "%lf", optimum_ + i) < 1) {
144  fclose (f);
145  printf ("Couenne: warning, could not read optimal value of x_%d from file \"%s\"\n", i, fname -> c_str ());
146  return false;
147  } else
148  if (variables_ [i] -> isDefinedInteger ())
149  optimum_ [i] = ceil (optimum_ [i] - .5);
150 
151  if (opt_window_ < 1e50) // restrict solution space around known optimum
152  for (int i = 0; i < nOrigVars_; i++) {
153  Lb (i) = CoinMax (Lb (i), optimum_ [i] - opt_window_ * (1 + fabs (optimum_ [i])));
154  Ub (i) = CoinMin (Ub (i), optimum_ [i] + opt_window_ * (1 + fabs (optimum_ [i])));
155  }
156 
157  // expand solution to auxiliary space
158  getAuxs (optimum_);
159 
160  fclose (f);
161  return true;
162 }
int nVars() const
Total number of variables.
CouNumber & ub(register int index)
current upper bound
std::vector< CouenneObjective * > objectives_
Objectives.
CouNumber & lb(register int index)
current lower bound
std::string problemName_
problem name
void print(std::ostream &=std::cout)
Display current representation of problem: objective, linear and nonlinear constraints, and auxiliary variables.
Definition: problemIO.cpp:24
CouNumber bestObj_
Best known objective function.
std::vector< expression * > commonexprs_
AMPL&#39;s common expressions (read from AMPL through structures cexps and cexps1)
void fint fint fint real fint real real real real * f
void getAuxs(CouNumber *) const
Get auxiliary variables from original variables.
Definition: problem.cpp:162
CouNumber opt_window_
window around known optimum (for testing purposes)
std::vector< CouenneConstraint * > constraints_
Constraints.
const Ipopt::EJournalCategory J_REFORMULATE(Ipopt::J_USER7)
#define COUENNE_EPS
std::vector< exprVar * > variables_
Variables (original, auxiliary, and defined)
Domain domain_
current point and bounds;
CouNumber * Ub() const
Return vector of upper bounds.
CouNumber * optimum_
Best solution known to be loaded from file – for testing purposes.
double CouNumber
main number type in Couenne
int nOrigVars_
Number of original variables.
#define COUENNE_INFINITY
bool readOptimum(std::string *fname=NULL)
Read best known solution from file given in argument.
Definition: problemIO.cpp:107
JnlstPtr jnlst_
SmartPointer to the Journalist.
CouNumber * Lb() const
Return vector of lower bounds.
bool isInteger(CouNumber x)
is this number integer?