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

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

fixed SOS branching -- use "enable_sos true" to use. Use getMatrByRow and convert instead of getMatrByCol in two-implied cuts

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