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

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

other changes to make Couenne less dependent on ASL

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