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

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

Added Couenne namespace. Committed old code for Feas Pump. Added code for ellipsoid cuts. Added (empty) CouenneMINLPInterface that might replace AMPL interface to Ipopt. Fixed a few warnings. Solved a few namespace problems due to ambiguity in Bonmin and AMPL. Added (empty) code for sdp cuts.

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