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

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

added header. adding FBBT to two-implied bound tightening

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