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

Last change on this file since 154 was 154, checked in by pbelotti, 11 years ago

Id'd all remaining files

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