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