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

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

added CouenneInterface? argument to initializeCouenne

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