00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <fstream>
00012 #include <iomanip>
00013 #include <cassert>
00014
00015 #include "CouenneProblem.hpp"
00016 #include "CouenneProblemElem.hpp"
00017 #include "CouenneExprVar.hpp"
00018
00019 using namespace Couenne;
00020
00024 void CouenneProblem::writeGAMS (const std::string &fname) {
00025
00026 std::ofstream f (fname.c_str ());
00027
00028 f << std::setprecision (10);
00029
00030 const int nline = 20;
00031
00032
00033
00034 f << "* MINLP Written by Couenne (https://projects.coin-or.org/Couenne)" << std::endl
00035 << "* problem name: " << problemName_ << std::endl
00036 << "* " << nVars () << " variables, " << nCons () << " constraints" << std::endl << std::endl;
00037
00038
00039
00040 f << " variables ";
00041
00042 for (int i=0, j=0; i < nVars (); i++)
00043 if (Var (i) -> Multiplicity () > 0) {
00044
00045 if (j) f << ',';
00046 if (!(++j % nline)) f << std::endl << " ";
00047
00048 Var (i) -> print (f);
00049
00050
00051
00052
00053
00054
00055
00056
00057 }
00058
00059 f << ",objvar;" << std::endl << std::endl;
00060
00061 bool
00062 have_positive = false,
00063 have_binary = false,
00064 have_integer = false;
00065
00066
00067 for (int i=0; i < nVars (); i++)
00068 if ((Var (i) -> Multiplicity () > 0) &&
00069 (fabs (Var (i) -> lb ()) < COUENNE_EPS) &&
00070 !(Var (i) -> isInteger ())) {
00071
00072
00073 have_positive = true;
00074 break;
00075 }
00076
00077
00078 for (int i=0; i < nVars (); i++)
00079 if ((Var (i) -> Multiplicity () > 0) &&
00080 Var (i) -> isInteger () &&
00081 (fabs (Var (i) -> lb ()) < COUENNE_EPS) &&
00082 (fabs (Var (i) -> ub () - 1) < COUENNE_EPS)) {
00083
00084 have_binary = true;
00085 break;
00086 }
00087
00088
00089 for (int i=0; i < nVars (); i++)
00090 if ((Var (i) -> Multiplicity () > 0) &&
00091 (Var (i) -> isInteger ()) &&
00092 ((fabs (Var (i) -> lb ()) > COUENNE_EPS) ||
00093 (fabs (Var (i) -> ub () - 1) > COUENNE_EPS))) {
00094
00095 have_integer = true;
00096 break;
00097 }
00098
00099
00100 if (have_positive) {
00101
00102 f << " positive variables ";
00103
00104
00105 for (int i=0, j=0; i < nVars (); i++)
00106 if ((Var (i) -> Multiplicity () > 0) &&
00107 (fabs (Var (i) -> lb ()) < COUENNE_EPS) &&
00108 !(Var (i) -> isInteger ())) {
00109
00110
00111 if (j) f << ',';
00112 if (!(++j % nline)) f << std::endl << " ";
00113
00114 Var (i) -> print (f);
00115
00116
00117
00118
00119 }
00120
00121 f << ';' << std::endl << std::endl;
00122 }
00123
00124
00125 if (have_binary) {
00126
00127 f << " binary variables ";
00128
00129 for (int i=0, j=0; i < nVars (); i++)
00130 if ((Var (i) -> Multiplicity () > 0) &&
00131 Var (i) -> isInteger () &&
00132 (fabs (Var (i) -> lb ()) < COUENNE_EPS) &&
00133 (fabs (Var (i) -> ub () - 1) < COUENNE_EPS)) {
00134
00135 if (j) f << ',';
00136 if (!(++j % nline)) f << std::endl << " ";
00137 Var (i) -> print (f);
00138
00139 }
00140 f << ';' << std::endl << std::endl;
00141 }
00142
00143
00144 if (have_integer) {
00145
00146 f << " integer variables ";
00147
00148 for (int i=0, j=0; i < nVars (); i++)
00149 if ((Var (i) -> Multiplicity () > 0) &&
00150 (Var (i) -> isInteger ()) &&
00151 ((fabs (Var (i) -> lb ()) > COUENNE_EPS) ||
00152 (fabs (Var (i) -> ub () - 1) > COUENNE_EPS))) {
00153
00154 if (j) f << ',';
00155 if (!(++j % nline)) f << std::endl << " ";
00156 Var (i) -> print (f);
00157
00158 }
00159 f << ';' << std::endl << std::endl;
00160 }
00161
00162 f << " equations ";
00163
00164 int i=0, j=0;
00165
00166 bool no_eqns = true;
00167
00168 for (; i < nCons (); i++) {
00169
00170 if (j) f << ',';
00171 if (!(++j % nline)) f << std::endl << " ";
00172 f << 'e' << j;
00173 no_eqns = false;
00174 }
00175
00176 for (; i < nVars (); i++)
00177 if ((Var (i) -> Type () == AUX) &&
00178 (Var (i) -> Multiplicity () > 0)) {
00179
00180 if (j) f << ',';
00181 if (!(++j % nline)) f << std::endl << " ";
00182 f << 'e' << j;
00183 no_eqns = false;
00184 }
00185
00186 if (!no_eqns) f << ',';
00187 f << "objdef;" << std::endl << std::endl;
00188
00189
00190
00191 i=j=0;
00192
00193 for (; i < nCons (); i++) {
00194
00195 f << 'e' << ++j << ".. ";
00196
00197 CouNumber
00198 lb = (*(Con (i) -> Lb ())) (),
00199 ub = (*(Con (i) -> Ub ())) ();
00200
00201 assert ((lb > -COUENNE_INFINITY) ||
00202 (ub < COUENNE_INFINITY));
00203
00204 if (fabs (lb - ub) < COUENNE_EPS) {
00205 Con (i) -> Body () -> print (f);
00206 f << " =E= " << lb;
00207 } else if (lb > -COUENNE_INFINITY) {
00208 Con (i) -> Body () -> print (f);
00209 f << " =G= " << lb;
00210 } else if (ub < COUENNE_INFINITY) {
00211 Con (i) -> Body () -> print (f);
00212 f << " =L= " << ub;
00213 }
00214
00215 f << ';' << std::endl << std::endl;
00216 }
00217
00218 for (; i < nVars (); i++)
00219 if ((Var (i) -> Type () == AUX) &&
00220 (Var (i) -> Multiplicity () > 0)) {
00221
00222 f << 'e' << ++j << ".. ";
00223 f << " - ";
00224 Var (i) -> print (f);
00225 f << " + ";
00226 Var (i) -> Image () -> print ();
00227 f << " =E= 0;" << std::endl << std::endl;
00228 }
00229
00230 f << "objdef.. - objvar + ";
00231 Obj (0) -> Body () -> print (f);
00232 f << " =E= 0;" << std::endl << std::endl;
00233
00234
00235
00236 for (int i=0; i < nVars (); i++)
00237 if (Var (i) -> Multiplicity () > 0) {
00238
00239 Var (i) -> print (f);
00240 f << ".l = " << X (i) << ';' << std::endl;
00241
00242 if ((fabs (Lb (i)) > COUENNE_EPS) && (Lb (i) > -COUENNE_INFINITY)) {
00243 Var (i) -> print (f);
00244 f << ".lo = " << Lb (i) << ';' << std::endl;
00245 }
00246
00247 if (Ub (i) < COUENNE_INFINITY) {
00248 Var (i) -> print (f);
00249 f << ".up = " << Ub (i) << ';' << std::endl;
00250 }
00251 }
00252
00253
00254
00255 f << "Model m / all /;" << std::endl
00256 << "m.limrow=0; m.limcol=0;" << std::endl
00257 << "Solve m using MINLP minimizing objvar;" << std::endl;
00258
00259 f.close ();
00260 }