00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <cstdlib>
00016
00017 #include "CoinPragma.hpp"
00018 #include "CoinError.hpp"
00019 #include "CoinTime.hpp"
00020
00021 #include "CouenneUserInterface.hpp"
00022 #ifdef COIN_HAS_ASL
00023 #include "CouenneAmplInterface.hpp"
00024 #endif
00025 #ifdef COIN_HAS_OS
00026 #include "CouenneOSInterface.hpp"
00027 #endif
00028
00029 #include "BonRegisteredOptions.hpp"
00030 #include "BonCbc.hpp"
00031
00032 #include "BonCouenneSetup.hpp"
00033 #include "BonCouenneInterface.hpp"
00034
00035
00036 #include "CbcCutGenerator.hpp"
00037 #include "CouenneCutGenerator.hpp"
00038 #include "CouenneProblem.hpp"
00039
00040 #ifdef COIN_HAS_SCIP
00041 #include "lpiswitch.h"
00042 #endif
00043
00044
00045 namespace Ipopt {
00046 class OptionsList;
00047 class Journalist;
00048 }
00049
00050 using Ipopt::SmartPtr;
00051
00052 namespace Bonmin {
00053 class RegisteredOptions;
00054 class TMINLP;
00055 class Bab;
00056 class BabSetupBase;
00057 class TNLPSolver;
00058 class OsiTMINLPInterface;
00059 }
00060
00061 using namespace Couenne;
00062
00063
00064 #define PRINTED_PRECISION 1e-5
00065
00066 using Ipopt::SmartPtr;
00067
00068 static const int infeasible = 1;
00069
00070 bool parseCommandLine(int argc, char* argv[], Ipopt::SmartPtr<Ipopt::OptionsList> options) {
00071 assert(IsValid(options));
00072
00073 if (argc==3 && strcmp(argv[1], "-AMPL")==0)
00074 options->SetStringValue("nlfile", argv[2]);
00075
00076 if (argc==3 && strcmp(argv[1], "-OSIL")==0)
00077 options->SetStringValue("osilfile", argv[2]);
00078
00079 return true;
00080 }
00081
00082 int main (int argc, char *argv[]) {
00083 WindowsErrorPopupBlocker();
00084
00085 #ifdef COIN_HAS_SCIP
00086 SCIPlpiSwitchSetDefaultSolver();
00087 #endif
00088
00089 double time_start = CoinCpuTime();
00090
00091
00092 SmartPtr<Bonmin::RegisteredOptions> roptions = new Bonmin::RegisteredOptions();
00093 Couenne::CouenneSetup::registerAllOptions(roptions);
00094 #ifdef COIN_HAS_ASL
00095 CouenneAmplInterface::registerOptions(roptions);
00096 #endif
00097 #ifdef COIN_HAS_OS
00098 CouenneOSInterface::registerOptions(roptions);
00099 #endif
00100
00101 SmartPtr<Ipopt::Journalist> jnlst = new Ipopt::Journalist();
00102
00103
00104 SmartPtr<Ipopt::OptionsList> options = new Ipopt::OptionsList(GetRawPtr(roptions), jnlst);
00105 if (!parseCommandLine(argc, argv, options))
00106 return EXIT_FAILURE;
00107
00108
00109
00110 CouenneUserInterface* userinterface = NULL;
00111
00112 std::string dummy;
00113 #ifdef COIN_HAS_ASL
00114 if (!userinterface && options->GetStringValue("nlfile", dummy, "")) {
00115 userinterface = new CouenneAmplInterface(options, jnlst);
00116 ((CouenneAmplInterface*)userinterface) -> setRegisteredOptions(roptions);
00117 }
00118 #endif
00119 #ifdef COIN_HAS_OS
00120 if (!userinterface && options->GetStringValue("osilfile", dummy, "")) {
00121 userinterface = new CouenneOSInterface();
00122 }
00123 #endif
00124
00125 if (!userinterface) {
00126 fprintf(stderr, "Error: No input file given.\n");
00127 return EXIT_FAILURE;
00128 }
00129
00130 if (!userinterface->setupJournals())
00131 return EXIT_FAILURE;
00132
00133 CouenneProblem* problem = userinterface->getCouenneProblem();
00134 if (!problem)
00135 return EXIT_FAILURE;
00136 problem->initOptions(options);
00137
00138 SmartPtr<Bonmin::TMINLP> tminlp = userinterface->getTMINLP();
00139 if (Ipopt::IsNull(tminlp))
00140 return EXIT_FAILURE;
00141
00142 try {
00143 Bonmin::Bab bb;
00144 bb.setUsingCouenne (true);
00145
00146 CouenneSetup couenne;
00147 couenne.setOptionsAndJournalist(roptions, options, jnlst);
00148 if (!couenne.InitializeCouenne (NULL, problem, tminlp))
00149 throw infeasible;
00150
00151 double timeLimit = 0;
00152 options -> GetNumericValue ("time_limit", timeLimit, "couenne.");
00153 couenne.setDoubleParameter (Bonmin::BabSetupBase::MaxTime, timeLimit - (time_start = (CoinCpuTime () - time_start)));
00154
00155 if (!userinterface->addBabPlugins(bb))
00156 return EXIT_FAILURE;
00157
00158 bb (couenne);
00159
00160
00161 double global_opt;
00162 options -> GetNumericValue ("couenne_check", global_opt, "couenne.");
00163
00164 if (global_opt < COUENNE_INFINITY) {
00165 double opt = bb.model (). getBestPossibleObjValue ();
00166
00167 jnlst -> Printf(Ipopt::J_SUMMARY, J_PROBLEM, "Global Optimum Test on %-40s %s\n",
00168 problem -> problemName ().c_str (),
00169 (fabs (opt - global_opt) /
00170 (1. + CoinMax (fabs (opt), fabs (global_opt))) < PRINTED_PRECISION) ?
00171 "OK" : "FAILED");
00172
00173 } else if (couenne.displayStats ()) {
00174
00175 int nr=-1, nt=-1;
00176 double st=-1;
00177
00178 CouenneCutGenerator* cg = NULL;
00179 if (bb.model (). cutGenerators ())
00180 cg = dynamic_cast <CouenneCutGenerator *> (bb.model (). cutGenerators () [0] -> generator ());
00181 if (cg) cg -> getStats (nr, nt, st);
00182 else jnlst -> Printf(Ipopt::J_WARNING, J_PROBLEM, "Warning: Could not get pointer to CouenneCutGenerator\n");
00183
00184 jnlst -> Printf(Ipopt::J_SUMMARY, J_PROBLEM, "Stats: %-15s %4d [var] %4d [int] %4d [con] %4d [aux] "
00185 "%6d [root] %8d [tot] %6g [sep] %8g [time] %8g [bb] "
00186 "%20e [lower] %20e [upper] %7d [nodes]\n",
00187 problem -> problemName ().c_str (),
00188 problem -> nOrigVars (),
00189 problem -> nOrigIntVars(),
00190 problem -> nOrigCons (),
00191 problem -> nVars () - problem -> nOrigVars (),
00192 nr, nt, st,
00193 CoinCpuTime () - time_start,
00194 cg ? (CoinCpuTime () - cg -> rootTime ()) : CoinCpuTime (),
00195 bb.model (). getBestPossibleObjValue (),
00196 bb.model (). getObjValue (),
00197
00198
00199 bb.numNodes ()
00200
00201
00202 );
00203 }
00204
00205 if (!userinterface->writeSolution(bb))
00206 return EXIT_FAILURE;
00207
00208 } catch(Bonmin::TNLPSolver::UnsolvedError *E) {
00209 E->writeDiffFiles();
00210 E->printError(std::cerr);
00211
00212
00213
00214
00215
00216 } catch(Bonmin::OsiTMINLPInterface::SimpleError &E) {
00217 std::cerr<<E.className()<<"::"<<E.methodName()
00218 <<std::endl
00219 <<E.message()<<std::endl;
00220
00221 } catch(CoinError &E) {
00222 std::cerr<<E.className()<<"::"<<E.methodName()
00223 <<std::endl
00224 <<E.message()<<std::endl;
00225
00226 } catch (Ipopt::OPTION_INVALID &E) {
00227 std::cerr<<"Ipopt exception : "<<E.Message()<<std::endl;
00228
00229 } catch (int generic_error) {
00230 if (generic_error == infeasible)
00231 jnlst->Printf(Ipopt::J_SUMMARY, J_PROBLEM, "problem infeasible\n");
00232 }
00233
00234 delete userinterface;
00235
00236 return EXIT_SUCCESS;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259