source: Couenne/src/main/BonCouenne.cpp @ 1

Last change on this file since 1 was 1, checked in by coin, 12 years ago

Initial import

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