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

Last change on this file since 41 was 41, checked in by pbelotti, 12 years ago

fix

File size: 6.0 KB
Line 
1// (C) Copyright International Business Machines Corporation and Carnegie Mellon University 2006, 2007
2// All Rights Reserved.
3// This code is published under the Common Public License.
4//
5// Authors :
6// Pietro Belotti, Carnegie Mellon University,
7// Pierre Bonami, International Business Machines Corporation
8//
9// Date : 12/19/2006
10
11
12#if defined(_MSC_VER)
13// Turn off compiler warning about long names
14#  pragma warning(disable:4786)
15#endif
16#include <iomanip>
17#include <fstream>
18
19#include <stdlib.h>
20
21#include "CoinTime.hpp"
22#include "BonminConfig.h"
23#include "BonCouenneInterface.hpp"
24#include "BonIpoptSolver.hpp"
25
26#include "BonCouenneSetup.hpp"
27
28#include "BonCbc.hpp"
29#ifdef COIN_HAS_FILTERSQP
30#include "BonFilterSolver.hpp"
31#endif
32
33#include "CbcCutGenerator.hpp"
34#include "CouenneProblem.hpp"
35#include "CouenneCutGenerator.hpp"
36
37using namespace Bonmin;
38
39// the maximum difference between a printed optimum and a CouNumber
40#define PRINTED_PRECISION 1e-5
41
42#include "exprVar.hpp"
43#include "exprConst.hpp"
44#include "exprSum.hpp"
45#include "exprClone.hpp"
46#include "CouenneProblemElem.hpp"
47#include "CouenneProblem.hpp"
48
49int main (int argc, char *argv[])
50{
51  using namespace Ipopt;
52
53  char * pbName = NULL;
54  double time_start = CoinCpuTime();
55
56  const int infeasible = 1;
57
58  try {
59
60    Bab bb;
61    bb.setUsingCouenne (true);
62
63    CouenneProblem *p = NULL;
64
65#if 0
66    p = new CouenneProblem;
67
68    p -> addVariable (false, p -> domain ());
69    p -> addVariable (false, p -> domain ());
70    p -> addVariable (false, p -> domain ());
71    p -> addVariable (false, p -> domain ());
72
73    p -> addObjective    (new exprSum (new exprClone (p->Var (1)), new exprClone (p->Var (2))), "min");
74    p -> addLEConstraint (new exprSum (new exprClone (p->Var (0)), new exprClone (p->Var (2))),
75                          new exprConst (1));
76    p -> addEQConstraint (new exprSum (new exprClone (p->Var (1)), new exprClone (p->Var (2))),
77                          new exprConst (1));
78    p -> addEQConstraint (new exprSum (new exprClone (p->Var (1)), new exprClone (p->Var (3))),
79                          new exprConst (1));
80    p -> addEQConstraint (new exprSum (new exprClone (p->Var (2)), new exprClone (p->Var (3))),
81                          new exprConst (1));
82#endif
83
84    CouenneSetup bonmin;
85    if (!bonmin.InitializeCouenne (argv, p))
86      throw infeasible;
87
88#if 0
89    CouenneFeasibility feasibility;
90    bb.model().setProblemFeasibility (feasibility);
91#endif
92
93    /// update time limit (read/preprocessing might have taken some)
94    double timeLimit = 0;
95    bonmin.options () -> GetNumericValue ("time_limit", timeLimit, "bonmin.");
96    bonmin.setDoubleParameter (BabSetupBase::MaxTime, 
97                               timeLimit - (time_start = (CoinCpuTime () - time_start)));
98
99    //////////////////////////////////
100
101    bb (bonmin); // do branch and bound
102
103    //////////////////////////////////
104
105    std::cout.precision (10);
106
107    //////////////////////////////
108    CouenneCutGenerator *cg = NULL;
109
110    if (bb.model (). cutGenerators ())
111      cg = dynamic_cast <CouenneCutGenerator *> 
112        (bb.model (). cutGenerators () [0] -> generator ());
113
114    ////////////////////////////////
115    int nr=-1, nt=-1;
116    double st=-1;
117
118    if (cg) cg -> getStats (nr, nt, st);
119    else printf ("Warning, could not get pointer to CouenneCutGenerator\n");
120
121    CouenneProblem *cp = cg ? cg -> Problem () : NULL;
122
123    // retrieve test value to check
124    double global_opt;
125    bonmin.options () -> GetNumericValue ("couenne_check", global_opt, "couenne.");
126
127    if (global_opt < COUENNE_INFINITY) { // some value found in couenne.opt
128
129      double opt = bb.model (). getBestPossibleObjValue ();
130
131      printf ("Global Optimum Test on %-40s %s\n", 
132              cp ? cp -> problemName ().c_str () : "unknown", 
133              (fabs (opt - global_opt) / 
134               (1. + CoinMax (fabs (opt), fabs (global_opt))) < PRINTED_PRECISION) ? 
135              (char *) "OK" : (char *) "FAILED");
136              //opt, global_opt,
137              //fabs (opt - global_opt));
138
139    } else // good old statistics
140
141    if (bonmin.displayStats ()) { // print statistics
142
143      // CAUTION: assuming first cut generator is our CouenneCutGenerator
144
145      if (cg && !cp) printf ("Warning, could not get pointer to problem\n");
146      else
147        printf ("Stats: %-15s %4d [var] %4d [int] %4d [con] %4d [aux] "
148                "%6d [root] %8d [tot] %6g [sep] %8g [time] %8g [bb] "
149                "%20e [lower] %20e [upper] %7d [nodes]\n",// %s %s\n",
150                cp ? cp -> problemName ().c_str () : "unknown",
151                (cp) ? cp -> nOrigVars     () : -1, 
152                (cp) ? cp -> nOrigIntVars  () : -1, 
153                (cp) ? cp -> nOrigCons     () : -1,
154                (cp) ? (cp -> nVars     () - 
155                        cp -> nOrigVars ()): -1,
156                nr, nt, st, 
157                CoinCpuTime () - time_start,
158                cg ? (CoinCpuTime () - cg -> rootTime ()) : CoinCpuTime (),
159                bb.model (). getBestPossibleObjValue (),
160                bb.model (). getObjValue (),
161                //bb.bestBound (),
162                //bb.bestObj (),
163                bb.numNodes ());
164                //bb.iterationCount ());
165                //status.c_str (), message.c_str ());
166    }
167
168//    nlp_and_solver -> writeAmplSolFile (message, bb.bestSolution (), NULL);
169  }
170  catch(TNLPSolver::UnsolvedError *E) {
171     E->writeDiffFiles();
172     E->printError(std::cerr);
173    //There has been a failure to solve a problem with Ipopt.
174    //And we will output file with information on what has been changed in the problem to make it fail.
175    //Now depending on what algorithm has been called (B-BB or other) the failed problem may be at different place.
176    //    const OsiSolverInterface &si1 = (algo > 0) ? nlpSolver : *model.solver();
177  }
178  catch(OsiTMINLPInterface::SimpleError &E) {
179    std::cerr<<E.className()<<"::"<<E.methodName()
180             <<std::endl
181             <<E.message()<<std::endl;
182  }
183  catch(CoinError &E) {
184    std::cerr<<E.className()<<"::"<<E.methodName()
185             <<std::endl
186             <<E.message()<<std::endl;
187  }
188  catch (Ipopt::OPTION_INVALID &E)
189  {
190   std::cerr<<"Ipopt exception : "<<E.Message()<<std::endl;
191  }
192  catch (int generic_error) {
193    if (generic_error == infeasible)
194      printf ("problem infeasible\n");
195  }
196
197//  catch(...) {
198//    std::cerr<<pbName<<" unrecognized excpetion"<<std::endl;
199//    std::cerr<<pbName<<"\t Finished \t exception"<<std::endl;
200//    throw;
201//  }
202
203  delete [] pbName;
204  return 0;
205}
Note: See TracBrowser for help on using the repository browser.