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

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

trying to get common expressions right. using orbital BT. fix on exprGroup::print(). some cosmetic changes

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