source: trunk/Couenne/src/main/Couenne.cpp @ 530

Last change on this file since 530 was 530, checked in by pbelotti, 9 years ago

removed unnecessary #includes of CouenneConfig?.h. fixed a few warnings.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1// $Id: Couenne.cpp 530 2011-03-12 16:52:06Z pbelotti $
2//
3// (C) Copyright International Business Machines Corporation and Carnegie Mellon University 2006, 2007
4// All Rights Reserved.
5// This code is published under the Eclipse Public License (EPL).
6//
7// Authors :
8// Pietro Belotti, Lehigh University
9// Stefan Vigerske, Humboldt University
10//
11// Date : 07/06/2009
12
13//#include "CouenneConfig.h"
14
15#include <cstdlib>
16
17#include "CoinPragma.hpp"
18#include "CoinError.hpp"
19#include "CoinTime.hpp"
20
21#include "CouenneUserInterface.hpp"
22#ifdef COIN_HAS_ASL
23#include "CouenneAmplInterface.hpp"
24#endif
25#ifdef COIN_HAS_OS
26#include "CouenneOSInterface.hpp"
27#endif
28
29#include "BonRegisteredOptions.hpp"
30#include "BonCbc.hpp"
31
32#include "BonCouenneSetup.hpp"
33#include "BonCouenneInterface.hpp"
34
35// for printing of statistics
36#include "CbcCutGenerator.hpp"     
37#include "CouenneCutGenerator.hpp"
38#include "CouenneProblem.hpp"
39
40
41namespace Ipopt {
42  class OptionsList;
43  class Journalist;
44}
45
46using Ipopt::SmartPtr;
47
48namespace Bonmin {
49  class RegisteredOptions;
50  class TMINLP;
51  class Bab;
52  class BabSetupBase;
53  class TNLPSolver;
54  class OsiTMINLPInterface;
55}
56
57using namespace Couenne;
58
59// the maximum difference between a printed optimum and a CouNumber
60#define PRINTED_PRECISION 1e-5
61
62using Ipopt::SmartPtr;
63
64static const int infeasible = 1;
65
66bool parseCommandLine(int argc, char* argv[], Ipopt::SmartPtr<Ipopt::OptionsList> options) {
67  assert(IsValid(options));
68       
69  if (argc==3 && strcmp(argv[1], "-AMPL")==0)
70    options->SetStringValue("nlfile", argv[2]);
71
72  if (argc==3 && strcmp(argv[1], "-OSIL")==0)
73    options->SetStringValue("osilfile", argv[2]);
74
75  return true;
76}
77
78int main (int argc, char *argv[]) {
79  WindowsErrorPopupBlocker();
80
81  double time_start = CoinCpuTime();
82 
83  // register options to prepare for parsing the command line
84  SmartPtr<Bonmin::RegisteredOptions> roptions = new Bonmin::RegisteredOptions();
85  Couenne::CouenneSetup::registerAllOptions(roptions);
86#ifdef COIN_HAS_ASL
87  CouenneAmplInterface::registerOptions(roptions);
88#endif
89#ifdef COIN_HAS_OS
90  CouenneOSInterface::registerOptions(roptions);
91#endif
92       
93  SmartPtr<Ipopt::Journalist> jnlst = new Ipopt::Journalist();
94  // do not add journals yet, maybe the user wants to do so; but what if parsing the command line gives errors?
95       
96  SmartPtr<Ipopt::OptionsList> options = new Ipopt::OptionsList(GetRawPtr(roptions), jnlst);
97  if (!parseCommandLine(argc, argv, options))
98    return EXIT_FAILURE;
99       
100       
101
102  CouenneUserInterface* userinterface = NULL;
103       
104  std::string dummy;
105#ifdef COIN_HAS_ASL
106  if (!userinterface && options->GetStringValue("nlfile", dummy, "")) {
107    userinterface = new CouenneAmplInterface(options, jnlst);
108    ((CouenneAmplInterface*)userinterface) -> setRegisteredOptions(roptions); // for some reason the TMINLP constructor needs the registered options
109  }
110#endif
111#ifdef COIN_HAS_OS
112  if (!userinterface && options->GetStringValue("osilfile", dummy, "")) {
113    userinterface = new CouenneOSInterface();
114  }
115#endif
116       
117  if (!userinterface) {
118    fprintf(stderr, "Error: No input file given.\n");
119    return EXIT_FAILURE;
120  }
121       
122  if (!userinterface->setupJournals())
123    return EXIT_FAILURE;
124       
125  CouenneProblem* problem = userinterface->getCouenneProblem();
126  if (!problem)
127    return EXIT_FAILURE;
128  problem->initOptions(options);
129       
130  SmartPtr<Bonmin::TMINLP> tminlp = userinterface->getTMINLP();
131  if (Ipopt::IsNull(tminlp))
132    return EXIT_FAILURE;
133
134  try {
135    Bonmin::Bab bb;
136    bb.setUsingCouenne (true);
137
138    CouenneSetup couenne;
139    couenne.setOptionsAndJournalist(roptions, options, jnlst);
140    if (!couenne.InitializeCouenne (NULL, problem, tminlp))
141      throw infeasible;
142
143    double timeLimit = 0;
144    options -> GetNumericValue ("time_limit", timeLimit, "couenne.");
145    couenne.setDoubleParameter (Bonmin::BabSetupBase::MaxTime, timeLimit - (time_start = (CoinCpuTime () - time_start)));
146 
147    if (!userinterface->addBabPlugins(bb))
148      return EXIT_FAILURE;
149
150    bb (couenne); // do branch and bound
151   
152    // retrieve test value to check
153    double global_opt;
154    options -> GetNumericValue ("couenne_check", global_opt, "couenne.");
155
156    if (global_opt < COUENNE_INFINITY) { // some value found in couenne.opt
157      double opt = bb.model (). getBestPossibleObjValue ();
158
159      jnlst -> Printf(Ipopt::J_SUMMARY, J_PROBLEM, "Global Optimum Test on %-40s %s\n", 
160                      problem -> problemName ().c_str (), 
161                      (fabs (opt - global_opt) / 
162                       (1. + CoinMax (fabs (opt), fabs (global_opt))) < PRINTED_PRECISION) ? 
163                      "OK" : "FAILED");
164
165    } else if (couenne.displayStats ()) { // print statistics
166
167      int nr=-1, nt=-1;
168      double st=-1;
169
170      CouenneCutGenerator* cg = NULL;
171      if (bb.model (). cutGenerators ())
172        cg = dynamic_cast <CouenneCutGenerator *>       (bb.model (). cutGenerators () [0] -> generator ());
173      if (cg) cg -> getStats (nr, nt, st);
174      else jnlst -> Printf(Ipopt::J_WARNING, J_PROBLEM, "Warning: Could not get pointer to CouenneCutGenerator\n");
175
176      jnlst -> Printf(Ipopt::J_SUMMARY, J_PROBLEM, "Stats: %-15s %4d [var] %4d [int] %4d [con] %4d [aux] "
177                      "%6d [root] %8d [tot] %6g [sep] %8g [time] %8g [bb] "
178                      "%20e [lower] %20e [upper] %7d [nodes]\n",// %s %s\n",
179                      problem -> problemName ().c_str (),
180                      problem -> nOrigVars   (), 
181                      problem -> nOrigIntVars(), 
182                      problem -> nOrigCons   (),
183                      problem -> nVars       () - problem -> nOrigVars (),
184                      nr, nt, st, 
185                      CoinCpuTime () - time_start,
186                      cg ? (CoinCpuTime () - cg -> rootTime ()) : CoinCpuTime (),
187                      bb.model (). getBestPossibleObjValue (),
188                      bb.model (). getObjValue (),
189                      //bb.bestBound (),
190                      //bb.bestObj (),
191                      bb.numNodes ()
192                      //bb.iterationCount (),
193                      //status.c_str (), message.c_str ()
194                      );
195    }   
196
197    if (!userinterface->writeSolution(bb))
198      return EXIT_FAILURE;
199       
200  } catch(Bonmin::TNLPSolver::UnsolvedError *E) {
201    E->writeDiffFiles();
202    E->printError(std::cerr);
203    //There has been a failure to solve a problem with Ipopt.
204    //And we will output file with information on what has been changed in the problem to make it fail.
205    //Now depending on what algorithm has been called (B-BB or other) the failed problem may be at different place.
206    //    const OsiSolverInterface &si1 = (algo > 0) ? nlpSolver : *model.solver();
207     
208  } catch(Bonmin::OsiTMINLPInterface::SimpleError &E) {
209    std::cerr<<E.className()<<"::"<<E.methodName()
210             <<std::endl
211             <<E.message()<<std::endl;
212   
213  } catch(CoinError &E) {
214    std::cerr<<E.className()<<"::"<<E.methodName()
215             <<std::endl
216             <<E.message()<<std::endl;
217   
218  } catch (Ipopt::OPTION_INVALID &E) {
219    std::cerr<<"Ipopt exception : "<<E.Message()<<std::endl;
220   
221  } catch (int generic_error) {
222    if (generic_error == infeasible)
223      jnlst->Printf(Ipopt::J_SUMMARY, J_PROBLEM, "problem infeasible\n");
224  }
225 
226  delete userinterface;
227 
228  return EXIT_SUCCESS;
229}
230
231
232
233//int main (int argc, char **argv) {
234
235// read options
236
237// create problem
238
239// create bb solver
240
241// add cut generators
242// add heuristics
243// add branching rules
244// add bound reduction
245
246// solve
247
248// print result
249
250//  return retval;
251//}
Note: See TracBrowser for help on using the repository browser.