writeGAMS.cpp
Go to the documentation of this file.
1 /* $Id: writeGAMS.cpp 606 2011-06-06 12:47:02Z stefan $
2  *
3  * Name: writeGAMS.cpp
4  * Author: Pietro Belotti
5  * Purpose: save a problem in GAMS format
6  *
7  * (C) Carnegie-Mellon University, 2006.
8  * This file is licensed under the Eclipse Public License (EPL)
9  */
10 
11 #include <fstream>
12 #include <iomanip> // to use the setprecision manipulator
13 #include <cassert>
14 
15 #include "CouenneProblem.hpp"
16 #include "CouenneProblemElem.hpp"
17 #include "CouenneExprVar.hpp"
18 
19 using namespace Couenne;
20 
24 void CouenneProblem::writeGAMS (const std::string &fname) {
25 
26  std::ofstream f (fname.c_str ());
27 
28  f << std::setprecision (10);
29 
30  const int nline = 20;
31 
32  // header //////////////////////////////////////////////////////////////
33 
34  f << "* MINLP Written by Couenne (https://projects.coin-or.org/Couenne)" << std::endl
35  << "* problem name: " << problemName_ << std::endl
36  << "* " << nVars () << " variables, " << nCons () << " constraints" << std::endl << std::endl;
37 
38  // preamble ////////////////////////////////////////////////////////////
39 
40  f << " variables ";
41 
42  for (int i=0, j=0; i < nVars (); i++)
43  if (Var (i) -> Multiplicity () > 0) {
44 
45  if (j) f << ',';
46  if (!(++j % nline)) f << std::endl << " ";
47 
48  Var (i) -> print (f);
49 
50  /*if (Var (i) -> isInteger ())
51  if ((fabs (Var (i) -> lb ()) < COUENNE_EPS) &&
52  (fabs (Var (i) -> ub () - 1) < COUENNE_EPS))
53  f << 'b';
54  else f << 'i';
55  else f << 'x';
56  f << Var (i) -> Index ();*/
57  }
58 
59  f << ",objvar;" << std::endl << std::endl;
60 
61  bool
62  have_positive = false,
63  have_binary = false,
64  have_integer = false;
65 
66  // check if there are positive variables
67  for (int i=0; i < nVars (); i++)
68  if ((Var (i) -> Multiplicity () > 0) &&
69  (fabs (Var (i) -> lb ()) < COUENNE_EPS) &&
70  !(Var (i) -> isInteger ())) {
71  //(fabs (Var (i) -> ub () - 1) < COUENNE_EPS))) {
72 
73  have_positive = true;
74  break;
75  }
76 
77  // check if there are binary variables
78  for (int i=0; i < nVars (); i++)
79  if ((Var (i) -> Multiplicity () > 0) &&
80  Var (i) -> isInteger () &&
81  (fabs (Var (i) -> lb ()) < COUENNE_EPS) &&
82  (fabs (Var (i) -> ub () - 1) < COUENNE_EPS)) {
83 
84  have_binary = true;
85  break;
86  }
87 
88  // check if there are integer variables
89  for (int i=0; i < nVars (); i++)
90  if ((Var (i) -> Multiplicity () > 0) &&
91  (Var (i) -> isInteger ()) &&
92  ((fabs (Var (i) -> lb ()) > COUENNE_EPS) ||
93  (fabs (Var (i) -> ub () - 1) > COUENNE_EPS))) {
94 
95  have_integer = true;
96  break;
97  }
98 
99  // positive /////////////////////////////////////////////////////////////////
100  if (have_positive) {
101 
102  f << " positive variables ";
103  // those with lower bound exactly zero (to save space...)
104 
105  for (int i=0, j=0; i < nVars (); i++)
106  if ((Var (i) -> Multiplicity () > 0) &&
107  (fabs (Var (i) -> lb ()) < COUENNE_EPS) &&
108  !(Var (i) -> isInteger ())) {
109  //(fabs (Var (i) -> ub () - 1) < COUENNE_EPS))) {
110 
111  if (j) f << ',';
112  if (!(++j % nline)) f << std::endl << " ";
113 
114  Var (i) -> print (f);
115 
116  /*if (Var (i) -> isInteger ()) f << 'i';
117  else f << 'x';
118  f << Var (i) -> Index ();*/
119  }
120 
121  f << ';' << std::endl << std::endl;
122  }
123 
124  // binary /////////////////////////////////////////////////////////////////
125  if (have_binary) {
126 
127  f << " binary variables ";
128 
129  for (int i=0, j=0; i < nVars (); i++)
130  if ((Var (i) -> Multiplicity () > 0) &&
131  Var (i) -> isInteger () &&
132  (fabs (Var (i) -> lb ()) < COUENNE_EPS) &&
133  (fabs (Var (i) -> ub () - 1) < COUENNE_EPS)) {
134 
135  if (j) f << ',';
136  if (!(++j % nline)) f << std::endl << " ";
137  Var (i) -> print (f);
138  //f << 'b' << Var (i) -> Index ();
139  }
140  f << ';' << std::endl << std::endl;
141  }
142 
143  // integer /////////////////////////////////////////////////////////////////
144  if (have_integer) {
145 
146  f << " integer variables ";
147 
148  for (int i=0, j=0; i < nVars (); i++)
149  if ((Var (i) -> Multiplicity () > 0) &&
150  (Var (i) -> isInteger ()) &&
151  ((fabs (Var (i) -> lb ()) > COUENNE_EPS) ||
152  (fabs (Var (i) -> ub () - 1) > COUENNE_EPS))) {
153 
154  if (j) f << ',';
155  if (!(++j % nline)) f << std::endl << " ";
156  Var (i) -> print (f);
157  //f << 'i' << Var (i) -> Index ();
158  }
159  f << ';' << std::endl << std::endl;
160  }
161 
162  f << " equations ";
163 
164  int i=0, j=0;
165 
166  bool no_eqns = true;
167 
168  for (; i < nCons (); i++) {
169 
170  if (j) f << ',';
171  if (!(++j % nline)) f << std::endl << " ";
172  f << 'e' << j;
173  no_eqns = false;
174  }
175 
176  for (; i < nVars (); i++)
177  if ((Var (i) -> Type () == AUX) &&
178  (Var (i) -> Multiplicity () > 0)) {
179 
180  if (j) f << ',';
181  if (!(++j % nline)) f << std::endl << " ";
182  f << 'e' << j;
183  no_eqns = false;
184  }
185 
186  if (!no_eqns) f << ',';
187  f << "objdef;" << std::endl << std::endl;
188 
189  // equations ///////////////////////////////////////////////////////////
190 
191  i=j=0;
192 
193  for (; i < nCons (); i++) {
194 
195  f << 'e' << ++j << ".. ";
196 
197  CouNumber
198  lb = (*(Con (i) -> Lb ())) (),
199  ub = (*(Con (i) -> Ub ())) ();
200 
201  assert ((lb > -COUENNE_INFINITY) ||
202  (ub < COUENNE_INFINITY));
203 
204  if (fabs (lb - ub) < COUENNE_EPS) {
205  Con (i) -> Body () -> print (f);
206  f << " =E= " << lb;
207  } else if (lb > -COUENNE_INFINITY) {
208  Con (i) -> Body () -> print (f);
209  f << " =G= " << lb;
210  } else if (ub < COUENNE_INFINITY) {
211  Con (i) -> Body () -> print (f);
212  f << " =L= " << ub;
213  }
214 
215  f << ';' << std::endl << std::endl;
216  }
217 
218  for (; i < nVars (); i++)
219  if ((Var (i) -> Type () == AUX) &&
220  (Var (i) -> Multiplicity () > 0)) {
221 
222  f << 'e' << ++j << ".. ";
223  f << " - ";
224  Var (i) -> print (f);
225  f << " + ";
226  Var (i) -> Image () -> print ();
227  f << " =E= 0;" << std::endl << std::endl;
228  }
229 
230  f << "objdef.. - objvar + ";
231  Obj (0) -> Body () -> print (f);
232  f << " =E= 0;" << std::endl << std::endl;
233 
234  // initial levels //////////////////////////////////////////////////////
235 
236  for (int i=0; i < nVars (); i++)
237  if (Var (i) -> Multiplicity () > 0) {
238 
239  Var (i) -> print (f);
240  f << ".l = " << X (i) << ';' << std::endl;
241 
242  if ((fabs (Lb (i)) > COUENNE_EPS) && (Lb (i) > -COUENNE_INFINITY)) {
243  Var (i) -> print (f);
244  f << ".lo = " << Lb (i) << ';' << std::endl;
245  }
246 
247  if (Ub (i) < COUENNE_INFINITY) {
248  Var (i) -> print (f);
249  f << ".up = " << Ub (i) << ';' << std::endl;
250  }
251  }
252 
253  // bounds //////////////////////////////////////////////////////////////
254 
255  f << "Model m / all /;" << std::endl
256  << "m.limrow=0; m.limcol=0;" << std::endl
257  << "Solve m using MINLP minimizing objvar;" << std::endl;
258 
259  f.close ();
260 }
int nVars() const
Total number of variables.
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
static char * j
Definition: OSdtoa.cpp:3622
void fint fint fint real fint real real real real * f
void writeGAMS(const std::string &fname)
Write nonlinear problem to a .gms file.
Definition: writeGAMS.cpp:24
CouenneConstraint * Con(int i) const
i-th constraint
#define COUENNE_EPS
exprVar * Var(int i) const
Return pointer to i-th variable.
CouNumber * Ub() const
Return vector of upper bounds.
double CouNumber
main number type in Couenne
#define COUENNE_INFINITY
CouNumber * X() const
Return vector of variables.
int nCons() const
Get number of constraints.
CouenneObjective * Obj(int i) const
i-th objective
CouNumber * Lb() const
Return vector of lower bounds.
bool isInteger(CouNumber x)
is this number integer?