source: trunk/Couenne/src/main/BonCouenne.cpp @ 488

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

CPL -> EPL

  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1// $Id: BonCouenne.cpp 488 2011-01-10 17:06:41Z 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, Carnegie Mellon University,
9// Pierre Bonami, International Business Machines Corporation
10//
11// Date : 12/19/2006
12
13
14#if defined(_MSC_VER)
15// Turn off compiler warning about long names
16#  pragma warning(disable:4786)
17#endif
18#include <iomanip>
19#include <fstream>
20
21#include <stdlib.h>
22
23#include "CouenneConfig.h"
24
25#include "CoinTime.hpp"
26#include "CoinError.hpp"
27#include "BonCouenneInterface.hpp"
28
29#include "BonCouenneSetup.hpp"
30
31#include "BonCbc.hpp"
32//#include "BonFilterSolver.hpp"
33
34#include "CbcCutGenerator.hpp"
35#include "CouenneProblem.hpp"
36#include "CouenneCutGenerator.hpp"
37
38using namespace Couenne;
39
40// the maximum difference between a printed optimum and a CouNumber
41#define PRINTED_PRECISION 1e-5
42
43#include "CouenneExprVar.hpp"
44#include "CouenneExprConst.hpp"
45#include "CouenneExprSum.hpp"
46#include "CouenneExprClone.hpp"
47#include "CouenneProblemElem.hpp"
48#include "CouenneProblem.hpp"
49#include "CouenneJournalist.hpp"
50
51int main (int argc, char *argv[]) {
52
53    printf ("Couenne %s --  an Open-Source exact solver for MINLP\n", 
54            strcmp (COUENNE_VERSION, "trunk") ? COUENNE_VERSION : "");
55
56    printf ("\
57Mailing list: couenne@list.coin-or.org\n\
58Instructions: http://www.coin-or.org/Couenne\n");
59
60  WindowsErrorPopupBlocker();
61  using namespace Ipopt;
62
63  char * pbName = NULL;
64
65  const int infeasible = 1;
66
67  try {
68
69    Bonmin::Bab bb;
70    bb.setUsingCouenne (true);
71
72    CouenneProblem *p = NULL;
73    CouenneInterface *ci = NULL;
74
75#if 0
76    //ci = new CouenneInterface;
77    p = new CouenneProblem;
78
79    p -> addVariable (false, p -> domain ());
80    p -> addVariable (false, p -> domain ());
81    p -> addVariable (false, p -> domain ());
82    p -> addVariable (false, p -> domain ());
83
84    p -> addObjective    (new exprSum (new exprClone (p->Var (1)), new exprClone (p->Var (2))), "min");
85    p -> addLEConstraint (new exprSum (new exprClone (p->Var (0)), new exprClone (p->Var (2))),
86                          new exprConst (1));
87    p -> addEQConstraint (new exprSum (new exprClone (p->Var (1)), new exprClone (p->Var (2))),
88                          new exprConst (1));
89    p -> addEQConstraint (new exprSum (new exprClone (p->Var (1)), new exprClone (p->Var (3))),
90                          new exprConst (1));
91    p -> addEQConstraint (new exprSum (new exprClone (p->Var (2)), new exprClone (p->Var (3))),
92                          new exprConst (1));
93#endif
94
95    CouenneSetup couenne;
96    if (!couenne.InitializeCouenne (argv, p, NULL, ci, &bb))
97      throw infeasible;
98
99    // initial printout
100
101    ConstJnlstPtr jnlst = couenne. couennePtr () -> Jnlst ();
102
103    CouenneProblem *prob = couenne. couennePtr () -> Problem ();
104
105    jnlst -> Printf (J_ERROR, J_COUENNE, "\
106Loaded instance \"%s\"\n\
107Constraints:     %8d\n\
108Variables:       %8d (%d integer)\n\
109Auxiliaries:     %8d (%d integer)\n\n",
110                     prob -> problemName ().c_str (),
111                     prob -> nOrigCons (),
112                     prob -> nOrigVars (),
113                     prob -> nOrigIntVars (),
114                     prob -> nVars    () - prob -> nOrigVars (),
115                     prob -> nIntVars () - prob -> nOrigIntVars ());
116
117    double time_start = CoinCpuTime();
118
119#if 0
120    CouenneFeasibility feasibility;
121    bb.model().setProblemFeasibility (feasibility);
122#endif
123
124    /// update time limit (read/preprocessing might have taken some)
125    double timeLimit = 0;
126    couenne.options () -> GetNumericValue ("time_limit", timeLimit, "couenne.");
127    couenne.setDoubleParameter (Bonmin::BabSetupBase::MaxTime, 
128                                CoinMax (1., timeLimit - time_start));
129
130    jnlst -> Printf (J_ERROR, J_COUENNE, "Starting branch-and-bound\n");
131
132    //////////////////////////////////
133
134    bb (couenne); // do branch and bound
135
136    //////////////////////////////////
137
138    std::cout.precision (10);
139
140    //////////////////////////////
141    CouenneCutGenerator *cg = NULL;
142
143    // there is only one cut generator, so scan array until
144    // dynamic_cast returns non-NULL
145
146    if (bb.model (). cutGenerators ()) {
147
148      int nGen = bb.model (). numberCutGenerators ();
149     
150      for (int i=0; !cg && i < nGen; i++)
151        cg = dynamic_cast <CouenneCutGenerator *> 
152          (bb.model (). cutGenerators () [i] -> generator ());
153    }
154
155    ////////////////////////////////
156    int nr=-1, nt=-1;
157    double st=-1;
158
159    if (cg) cg -> getStats (nr, nt, st);
160    else printf ("Warning, could not get pointer to CouenneCutGenerator\n");
161
162    CouenneProblem *cp = cg ? cg -> Problem () : NULL;
163
164    // retrieve test value to check
165    double global_opt;
166    couenne.options () -> GetNumericValue ("couenne_check", global_opt, "couenne.");
167
168    double 
169      ub = bb.model (). getObjValue (),
170      lb = bb.model (). getBestPossibleObjValue ();
171
172    char *gapstr = new char [80];
173
174    sprintf (gapstr, "%.2f%%", 100. * (ub - lb) / (1. + fabs (lb)));
175
176    jnlst -> Printf (J_ERROR, J_COUENNE, "\n\
177Linearization cuts added at root node:   %8d\n\
178Linearization cuts added in total        %8d  (separation time: %gs)\n\
179Total solving time:                      %8gs (%gs in branch-and-bound)\n\
180Lower bound:                           %10g\n\
181Upper bound:                           %10g  (gap: %s)\n\
182Branch-and-bound nodes:                  %8d\n\n",
183                     nr, nt, st, 
184                     CoinCpuTime () - time_start,
185                     cg ? (CoinCpuTime () - cg -> rootTime ()) : CoinCpuTime (),
186                     lb, 
187                     ub,
188                     (ub > COUENNE_INFINITY/1e4) ? "inf" : gapstr,
189                     bb.numNodes ());
190
191    delete [] gapstr;
192
193    if (global_opt < COUENNE_INFINITY) { // some value found in couenne.opt
194
195      double opt = bb.model (). getBestPossibleObjValue ();
196
197      printf ("Global Optimum Test on %-40s %s\n", 
198              cp ? cp -> problemName ().c_str () : "unknown", 
199              (fabs (opt - global_opt) / 
200               (1. + CoinMax (fabs (opt), fabs (global_opt))) < PRINTED_PRECISION) ? 
201              (const char *) "OK" : (const char *) "FAILED");
202              //opt, global_opt,
203              //fabs (opt - global_opt));
204
205    } else // good old statistics
206
207    if (couenne.displayStats ()) { // print statistics
208
209      // CAUTION: assuming first cut generator is our CouenneCutGenerator
210
211      if (cg && !cp) printf ("Warning, could not get pointer to problem\n");
212      else
213        printf ("Stats: %-15s %4d [var] %4d [int] %4d [con] %4d [aux] "
214                "%6d [root] %8d [tot] %6g [sep] %8g [time] %8g [bb] "
215                "%20e [lower] %20e [upper] %7d [nodes]\n",// %s %s\n",
216                cp ? cp -> problemName (). c_str () : "unknown",
217                (cp) ? cp -> nOrigVars     () : -1, 
218                (cp) ? cp -> nOrigIntVars  () : -1, 
219                (cp) ? cp -> nOrigCons     () : -1,
220                (cp) ? (cp -> nVars     () - 
221                        cp -> nOrigVars ()): -1,
222                nr, nt, st, 
223                CoinCpuTime () - time_start,
224                cg ? (CoinCpuTime () - cg -> rootTime ()) : CoinCpuTime (),
225                bb.model (). getBestPossibleObjValue (),
226                bb.model (). getObjValue (),
227                //bb.bestBound (),
228                //bb.bestObj (),
229                bb.numNodes ());
230                //bb.iterationCount ());
231                //status.c_str (), message.c_str ());
232    }
233
234//    nlp_and_solver -> writeAmplSolFile (message, bb.bestSolution (), NULL);
235  }
236  catch(Bonmin::TNLPSolver::UnsolvedError *E) {
237     E->writeDiffFiles();
238     E->printError(std::cerr);
239    //There has been a failure to solve a problem with Ipopt.
240    //And we will output file with information on what has been changed in the problem to make it fail.
241    //Now depending on what algorithm has been called (B-BB or other) the failed problem may be at different place.
242    //    const OsiSolverInterface &si1 = (algo > 0) ? nlpSolver : *model.solver();
243  }
244  catch (Bonmin::OsiTMINLPInterface::SimpleError &E) {
245    std::cerr<<E.className()<<"::"<<E.methodName()
246             <<std::endl
247             <<E.message()<<std::endl;
248  }
249  catch (CoinError &E) {
250    std::cerr<<E.className()<<"::"<<E.methodName()
251             <<std::endl
252             <<E.message()<<std::endl;
253  }
254  catch (Ipopt::OPTION_INVALID &E)
255  {
256   std::cerr<<"Ipopt exception : "<<E.Message()<<std::endl;
257  }
258  catch (int generic_error) {
259    if (generic_error == infeasible)
260      printf ("problem infeasible\n");
261  }
262
263//  catch(...) {
264//    std::cerr<<pbName<<" unrecognized excpetion"<<std::endl;
265//    std::cerr<<pbName<<"\t Finished \t exception"<<std::endl;
266//    throw;
267//  }
268
269  delete [] pbName;
270  return 0;
271}
Note: See TracBrowser for help on using the repository browser.