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