00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <fstream>
00012 #include <iomanip>
00013
00014 #include "CouenneProblem.hpp"
00015 #include "CouenneProblemElem.hpp"
00016 #include "CouenneExprVar.hpp"
00017
00018 using namespace Couenne;
00019
00020
00021
00022 void CouenneProblem::writeAMPL (const std::string &fname,
00023 bool aux) {
00024
00025 std::ofstream f (fname.c_str ());
00026
00027 f << std::setprecision (10);
00028
00029
00030
00031 f << "# Problem name: " << fname << std::endl << std::endl
00032 << "# original variables" << std::endl << std::endl;
00033
00034 for (int i=0; i < nVars (); i++) {
00035
00036 f << "var ";
00037 variables_ [i] -> print (f);
00038 if (Lb (i) > - COUENNE_INFINITY/2) f << " >= " << Lb (i);
00039 if (Ub (i) < + COUENNE_INFINITY/2) f << " <= " << Ub (i);
00040 if (variables_ [i] -> isInteger ()) f << " integer";
00041 if (fabs (X (i)) < COUENNE_INFINITY)
00042 f << " default " << X (i);
00043 f << ';' << std::endl;
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 f << std::endl << "# objective" << std::endl << std::endl;
00082
00083
00084 f << "minimize";
00085
00086
00087 f << " obj: ";
00088 objectives_ [0] -> Body () -> print (f, !aux);
00089 f << ';' << std::endl;
00090
00091
00092
00093
00094 if (aux) {
00095
00096 f << std::endl << "# aux. variables defined" << std::endl << std::endl;
00097
00098 for (int i=0; i < nVars (); i++)
00099
00100 if ((variables_ [i] -> Type () == AUX) &&
00101 (variables_ [i] -> Multiplicity () > 0)) {
00102
00103 f << "aux" << i << ": "; variables_ [i] -> print (f, false);
00104 f << " = ";
00105
00106 variables_ [i] -> Image () -> print (f, false);
00107 f << ';' << std::endl;
00108 }
00109 }
00110
00111
00112
00113
00114 f << std::endl << "# constraints" << std::endl << std::endl;
00115
00116 if (!aux)
00117 for (std::vector <exprVar *>::iterator i = variables_.begin ();
00118 i != variables_.end ();
00119 ++i)
00120
00121 if (((*i) -> Type () == AUX) &&
00122 ((*i) -> Multiplicity () > 0)) {
00123
00124 CouNumber bound;
00125
00126 if ((bound = (*i) -> lb ()) > - COUENNE_INFINITY) {
00127 f << "conAuxLb" << (*i) -> Index () << ": ";
00128 (*i) -> print (f, true);
00129 f << ">= " << bound << ';' << std::endl;
00130 }
00131
00132 if ((bound = (*i) -> ub ()) < COUENNE_INFINITY) {
00133 f << "conAuxUb" << (*i) -> Index () << ": ";
00134 (*i) -> print (f, true);
00135 f << "<= " << bound << ';' << std::endl;
00136 }
00137 }
00138
00139
00140 for (int i=0; i < nCons (); i++) {
00141
00142
00143 CouNumber lb = (constraints_ [i] -> Lb () -> Value ()),
00144 ub = (constraints_ [i] -> Ub () -> Value ());
00145
00146 f << "con" << i << ": ";
00147 constraints_ [i] -> Body () -> print (f, !aux);
00148
00149 if (lb > - COUENNE_INFINITY + 1) {
00150 f << ' ';
00151 if (fabs (ub-lb) > COUENNE_EPS)
00152 f << '>';
00153 f << "= " << lb << ';' << std::endl;
00154 }
00155 else f << " <= " << ub << ';' << std::endl;
00156
00157
00158
00159 if (( lb > - COUENNE_INFINITY + 1)
00160 && (ub < COUENNE_INFINITY - 1)
00161 && (fabs (ub-lb) > COUENNE_EPS)) {
00162
00163 f << "con" << i << "_rng: ";
00164 constraints_ [i] -> Body () -> print (f, !aux);
00165 f << " <= " << ub << ';' << std::endl;
00166 }
00167 }
00168
00169 f.close ();
00170 }