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

Last change on this file since 445 was 445, checked in by pbelotti, 10 years ago

fix a couple of compilation errors

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