00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "BonCbc.hpp"
00012 #include "BonBabInfos.hpp"
00013 #include "CglCutGenerator.hpp"
00014
00015 #include "CouenneCutGenerator.hpp"
00016 #include "CouenneProblem.hpp"
00017 #include "CouenneProblemElem.hpp"
00018 #include "CouenneExprVar.hpp"
00019 #include "CouenneInfeasCut.hpp"
00020
00021 #include "CouenneRecordBestSol.hpp"
00022
00023
00024
00025 #ifdef COIN_HAS_NTY
00026 #include "Nauty.h"
00027 #endif
00028
00029 using namespace Ipopt;
00030
00031 namespace Couenne {
00032
00033 #define Couenne_large_bound2 9.99e12
00034
00035
00036 bool isOptimumCut (const CouNumber *opt, OsiCuts &cs, CouenneProblem *p);
00037
00038
00039
00040 void fictitiousBound (OsiCuts &cs,
00041 CouenneProblem *p,
00042 bool action) {
00043
00044
00045 const CouNumber large_tol = (Couenne_large_bound2 / 1e6);
00046
00047
00048
00049 int ind_obj = p -> Obj (0) -> Body () -> Index ();
00050
00051 if (ind_obj < 0) return;
00052
00053
00054
00055
00056
00057 if (action)
00058
00059 {if (p -> Lb (ind_obj) < - Couenne_large_bound2) p -> Lb (ind_obj) = - Couenne_large_bound2;}
00060
00061 else
00062
00063
00064 {if (fabs (p->Lb(ind_obj)+Couenne_large_bound2)<large_tol) p->Lb(ind_obj) =-COUENNE_INFINITY;}
00065 }
00066
00067
00068
00069 void sparse2dense (int ncols, t_chg_bounds *chg_bds, int *&changed, int &nchanged) {
00070
00071
00072
00073 changed = (int *) realloc (changed, ncols * sizeof (int));
00074 nchanged = 0;
00075
00076 for (register int i=ncols, j=0; i--; j++, chg_bds++)
00077 if (chg_bds -> lower() != t_chg_bounds::UNCHANGED ||
00078 chg_bds -> upper() != t_chg_bounds::UNCHANGED ) {
00079 *changed++ = j;
00080 nchanged++;
00081 }
00082
00083 changed -= nchanged;
00084
00085 }
00086
00087
00089 void updateBranchInfo (const OsiSolverInterface &si, CouenneProblem *p,
00090 t_chg_bounds *chg, const CglTreeInfo &info);
00091
00093
00094 void CouenneCutGenerator::generateCuts (const OsiSolverInterface &si,
00095 OsiCuts &cs,
00096 const CglTreeInfo info)
00097 #if CGL_VERSION_MAJOR == 0 && CGL_VERSION_MINOR <= 57
00098 const
00099 #endif
00100 {
00101
00102
00103
00104
00105 int indObj = problem_ -> Obj (0) -> Body () -> Index ();
00106
00107 if ((indObj >= 0) &&
00108 (si.getColLower () [indObj] > problem_ -> getCutOff () + COUENNE_EPS)) {
00109
00110 WipeMakeInfeas (cs);
00111 return;
00112 }
00113
00114
00115
00116
00117
00118 if (isWiped (cs) ||
00119 (CoinCpuTime () > problem_ -> getMaxCpuTime ()))
00120 return;
00121
00122 #ifdef FM_TRACE_OPTSOL
00123 double currCutOff = problem_->getCutOff();
00124 double bestVal = 1e50;
00125 CouenneRecordBestSol *rs = problem_->getRecordBestSol();
00126 if(rs->getHasSol()) {
00127 bestVal = rs->getVal();
00128 }
00129 if(currCutOff > bestVal) {
00130
00131 problem_ -> setCutOff (bestVal);
00132
00133 if ((indObj >= 0) && (si. getColUpper () [indObj] > bestVal)) {
00134 OsiColCut *objCut = new OsiColCut;
00135 objCut->setUbs(1, &indObj, &bestVal);
00136 cs.insert(objCut);
00137 delete objCut;
00138 }
00139 }
00140 #endif
00141
00142 #ifdef FM_PRINT_INFO
00143 if((BabPtr_ != NULL) && (info.level >= 0) && (info.pass == 0) &&
00144 (BabPtr_->model().getNodeCount() > lastPrintLine)) {
00145 printLineInfo();
00146 lastPrintLine += 1;
00147 }
00148 #endif
00149
00150 const int infeasible = 1;
00151
00152 int nInitCuts = cs.sizeRowCuts ();
00153
00154 CouNumber
00155 *&realOpt = problem_ -> bestSol (),
00156 *saveOptimum = realOpt;
00157
00158 if (!firstcall_ && realOpt) {
00159
00160
00161
00162
00163 CouNumber *opt = realOpt;
00164
00165 const CouNumber
00166 *sol = si.getColSolution (),
00167 *lb = si.getColLower (),
00168 *ub = si.getColUpper ();
00169
00170 int objind = problem_ -> Obj (0) -> Body () -> Index ();
00171
00172 for (int j=0, i=problem_ -> nVars (); i--; j++, opt++, lb++, ub++)
00173 if ((j != objind) &&
00174 ((*opt < *lb - COUENNE_EPS * (1 + CoinMin (fabs (*opt), fabs (*lb)))) ||
00175 (*opt > *ub + COUENNE_EPS * (1 + CoinMin (fabs (*opt), fabs (*ub)))))) {
00176
00177 jnlst_ -> Printf (J_VECTOR, J_CONVEXIFYING,
00178 "out of bounds, ignore x%d = %g [%g,%g] opt = %g\n",
00179 problem_ -> nVars () - i - 1, *sol, *lb, *ub, *opt);
00180
00181
00182
00183 realOpt = NULL;
00184 break;
00185 }
00186 }
00187
00188
00189
00190
00191
00192
00193
00194 jnlst_ -> Printf (J_DETAILED, J_CONVEXIFYING,
00195 "generateCuts: level = %d, pass = %d, intree = %d\n",
00196 info.level, info.pass, info.inTree);
00197
00198 Bonmin::BabInfo * babInfo = dynamic_cast <Bonmin::BabInfo *> (si.getAuxiliaryInfo ());
00199
00200 if (babInfo)
00201 babInfo -> setFeasibleNode ();
00202
00203 double now = CoinCpuTime ();
00204 int ncols = problem_ -> nVars ();
00205
00206
00207
00208
00209
00210 t_chg_bounds *chg_bds = new t_chg_bounds [ncols];
00211
00212
00213
00214
00215
00216
00217
00218 problem_ -> installCutOff ();
00219
00220 if (firstcall_) {
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 for (int i=0; i < ncols; i++)
00231 if (problem_ -> Var (i) -> Multiplicity () > 0) {
00232 chg_bds [i].setLower (t_chg_bounds::CHANGED);
00233 chg_bds [i].setUpper (t_chg_bounds::CHANGED);
00234 }
00235
00236
00237
00238 if (problem_ -> doFBBT () &&
00239 (! (problem_ -> boundTightening (chg_bds, info, babInfo))))
00240 jnlst_ -> Printf (J_STRONGWARNING, J_CONVEXIFYING,
00241 "Couenne: WARNING, first convexification is infeasible\n");
00242
00243
00244
00245
00246
00247 int nnlc = problem_ -> nCons ();
00248
00249 for (int i=0; i<nnlc; i++) {
00250
00251 if (CoinCpuTime () > problem_ -> getMaxCpuTime ())
00252 break;
00253
00254
00255 CouenneConstraint *con = problem_ -> Con (i);
00256
00257
00258 int objindex = con -> Body () -> Index ();
00259
00260 if ((objindex >= 0) &&
00261 ((con -> Body () -> Type () == AUX) ||
00262 (con -> Body () -> Type () == VAR))) {
00263
00264
00265 exprVar *conaux = problem_ -> Var (objindex);
00266
00267 if (conaux &&
00268 (conaux -> Type () == AUX) &&
00269 (conaux -> Image ()) &&
00270 (conaux -> Image () -> Linearity () <= LINEAR)) {
00271
00272
00273
00274
00275 double
00276 lb = (*(con -> Lb ())) (),
00277 ub = (*(con -> Ub ())) ();
00278
00279 OsiColCut newBound;
00280 if (lb > -COUENNE_INFINITY) newBound.setLbs (1, &objindex, &lb);
00281 if (ub < COUENNE_INFINITY) newBound.setUbs (1, &objindex, &ub);
00282
00283 cs.insert (newBound);
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316 } else {
00317
00318
00319 assert (false);
00320 }
00321 }
00322
00323 if (jnlst_ -> ProduceOutput (J_ITERSUMMARY, J_CONVEXIFYING)) {
00324 if (cs.sizeRowCuts ()) {
00325 jnlst_ -> Printf (J_ITERSUMMARY, J_CONVEXIFYING,"Couenne: %d constraint row cuts\n",
00326 cs.sizeRowCuts ());
00327 for (int i=0; i<cs.sizeRowCuts (); i++)
00328 cs.rowCutPtr (i) -> print ();
00329 }
00330 if (cs.sizeColCuts ()) {
00331 jnlst_ -> Printf (J_ITERSUMMARY, J_CONVEXIFYING,"Couenne: %d constraint col cuts\n",
00332 cs.sizeColCuts ());
00333 for (int i=0; i<cs.sizeColCuts (); i++)
00334 cs.colCutPtr (i) -> print ();
00335 }
00336 }
00337 } else {
00338
00339
00340
00341
00342 problem_ -> domain () -> push (&si, &cs);
00343
00344 if (indObj >= 0) {
00345
00346
00347
00348 double lp_bound = problem_ -> domain () -> x (indObj);
00349
00350
00351 {if (lp_bound > problem_ -> Lb (indObj)) problem_ -> Lb (indObj) = lp_bound;}
00352
00353 }
00354
00355 updateBranchInfo (si, problem_, chg_bds, info);
00356 }
00357
00358
00359 for (int i = problem_ -> nCons (); i--;) {
00360
00361
00362 CouenneConstraint *con = problem_ -> Con (i);
00363
00364
00365 int objindex = con -> Body () -> Index ();
00366
00367 if ((objindex >= 0) &&
00368 ((con -> Body () -> Type () == AUX) ||
00369 (con -> Body () -> Type () == VAR))) {
00370
00371
00372 CouNumber
00373 l = con -> Lb () -> Value (),
00374 u = con -> Ub () -> Value ();
00375
00376
00377 problem_ -> Lb (objindex) = CoinMax (l, problem_ -> Lb (objindex));
00378 problem_ -> Ub (objindex) = CoinMin (u, problem_ -> Ub (objindex));
00379 }
00380 }
00381
00382 problem_ -> installCutOff ();
00383
00384 fictitiousBound (cs, problem_, false);
00385
00386 int *changed = NULL, nchanged;
00387
00388
00389
00390
00391
00392
00393
00394 try {
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 if (!firstcall_ &&
00421 problem_ -> doRCBT () &&
00422 problem_ -> redCostBT (&si, chg_bds) &&
00423 !(problem_ -> btCore (chg_bds)))
00424 throw infeasible;
00425
00426
00427 if (problem_ -> doFBBT () &&
00428
00429 (! (problem_ -> boundTightening (chg_bds, info, babInfo))))
00430 throw infeasible;
00431
00432
00433 if (!firstcall_ &&
00434 problem_ -> obbt (this, si, cs, info, babInfo, chg_bds) < 0)
00435 throw infeasible;
00436
00437
00438
00439 if ((problem_ -> doFBBT () ||
00440 problem_ -> doOBBT () ||
00441 problem_ -> doABT ()) &&
00442 (jnlst_ -> ProduceOutput (J_VECTOR, J_CONVEXIFYING))) {
00443
00444 jnlst_ -> Printf(J_VECTOR, J_CONVEXIFYING,"== after bt =============\n");
00445 for (int i = 0; i < problem_ -> nVars (); i++)
00446 if (problem_ -> Var (i) -> Multiplicity () > 0)
00447 jnlst_->Printf(J_VECTOR, J_CONVEXIFYING,"%4d %+20.8g [%+20.8g,%+20.8g]\n", i,
00448 problem_ -> X (i), problem_ -> Lb (i), problem_ -> Ub (i));
00449 jnlst_->Printf(J_VECTOR, J_CONVEXIFYING,"=============================\n");
00450 }
00451
00452
00453
00454 #ifdef COIN_HAS_NTY
00455
00456
00457
00458
00459 if (problem_ -> orbitalBranching () && !firstcall_) {
00460
00461 CouNumber
00462 *lb = problem_ -> Lb (),
00463 *ub = problem_ -> Ub ();
00464
00465 std::vector<std::vector<int> > *new_orbits = problem_ -> getNtyInfo () -> getOrbits();
00466
00467 for (int i=0, ii = problem_ -> getNtyInfo () -> getNumOrbits (); ii--; i++){
00468
00469 CouNumber
00470 ll = -COUENNE_INFINITY,
00471 uu = COUENNE_INFINITY;
00472
00473 std::vector <int> orbit = (*new_orbits)[i];
00474
00475 if (orbit.size () <= 1)
00476 continue;
00477
00478 if (jnlst_ -> ProduceOutput (J_VECTOR, J_BOUNDTIGHTENING)) {
00479 printf ("orbit bounds: "); fflush (stdout);
00480 for(int j = 0; j < orbit.size (); j++) {
00481 printf ("x_%d [%g,%g] ", orbit[j], lb [orbit [j]], ub [orbit [j]]);
00482 fflush (stdout);
00483 }
00484 }
00485
00486 for (int j = 0; j < orbit.size (); j++) {
00487
00488 int indOrb = orbit [j];
00489
00490 if (indOrb < problem_ -> nVars ()) {
00491
00492 if (lb [indOrb] > ll) ll = lb [indOrb];
00493 if (ub [indOrb] < uu) uu = ub [indOrb];
00494 }
00495 }
00496
00497 jnlst_ -> Printf (J_VECTOR, J_BOUNDTIGHTENING,
00498 " --> new common bounds: [%g,%g]\n", ll, uu);
00499
00500 for(int j = 0; j < orbit.size (); j++) {
00501
00502 int indOrb = orbit [j];
00503
00504 if (indOrb < problem_ -> nVars ()){
00505
00506 lb [indOrb] = ll;
00507 ub [indOrb] = uu;
00508 }
00509 }
00510 }
00511
00512 delete new_orbits;
00513 }
00514
00515 #endif
00516
00517
00518
00519 sparse2dense (ncols, chg_bds, changed, nchanged);
00520
00521 double *nlpSol = NULL;
00522
00523
00524
00525 if (true) {
00526
00527 if (babInfo)
00528 nlpSol = const_cast <double *> (babInfo -> nlpSolution ());
00529
00530
00531
00532 int logAbtLev = problem_ -> logAbtLev ();
00533
00534 if (problem_ -> doABT () &&
00535 ((logAbtLev != 0) ||
00536 (info.level == 0)) &&
00537 (info.pass == 0) &&
00538 ((logAbtLev < 0) ||
00539 (info.level <= logAbtLev) ||
00540 (CoinDrand48 () <
00541 pow (2., (double) logAbtLev - (info.level + 1))))) {
00542
00543 jnlst_ -> Printf(J_VECTOR, J_BOUNDTIGHTENING," performing ABT\n");
00544 if (! (problem_ -> aggressiveBT (nlp_, chg_bds, info, babInfo)))
00545 throw infeasible;
00546
00547 sparse2dense (ncols, chg_bds, changed, nchanged);
00548 }
00549
00550
00551
00552
00553
00554
00555
00556
00557 bool save_av = addviolated_;
00558 addviolated_ = false;
00559
00560
00561 problem_ -> domain () -> push
00562 (problem_ -> nVars (),
00563 problem_ -> domain () -> x (),
00564 problem_ -> domain () -> lb (),
00565 problem_ -> domain () -> ub (), false);
00566
00567
00568 if (nlpSol) {
00569 CoinCopyN (nlpSol, problem_ -> nOrigVars (), problem_ -> domain () -> x ());
00570
00571
00572 problem_ -> getAuxs (problem_ -> domain () -> x ());
00573 }
00574
00575 if (jnlst_ -> ProduceOutput (J_VECTOR, J_CONVEXIFYING)) {
00576 jnlst_ -> Printf(J_VECTOR, J_CONVEXIFYING,"== genrowcuts on NLP =============\n");
00577 for (int i = 0; i < problem_ -> nVars (); i++)
00578 if (problem_ -> Var (i) -> Multiplicity () > 0)
00579 jnlst_->Printf(J_VECTOR, J_CONVEXIFYING,"%4d %+20.8g [%+20.8g,%+20.8g]\n", i,
00580 problem_ -> X (i),
00581 problem_ -> Lb (i),
00582 problem_ -> Ub (i));
00583 jnlst_->Printf(J_VECTOR, J_CONVEXIFYING,"=============================\n");
00584 }
00585
00586 problem_ -> domain () -> current () -> isNlp () = true;
00587 genRowCuts (si, cs, nchanged, changed, chg_bds);
00588
00589 problem_ -> domain () -> pop ();
00590
00591 addviolated_ = save_av;
00592
00593
00594 if (babInfo)
00595 babInfo -> setHasNlpSolution (false);
00596
00597 } else {
00598
00599 if (jnlst_ -> ProduceOutput (J_VECTOR, J_CONVEXIFYING)) {
00600 jnlst_ -> Printf(J_VECTOR, J_CONVEXIFYING,"== genrowcuts on LP =============\n");
00601 for (int i = 0; i < problem_ -> nVars (); i++)
00602 if (problem_ -> Var (i) -> Multiplicity () > 0)
00603 jnlst_->Printf(J_VECTOR, J_CONVEXIFYING,"%4d %+20.8g [%+20.8g,%+20.8g]\n", i,
00604 problem_ -> X (i),
00605 problem_ -> Lb (i),
00606 problem_ -> Ub (i));
00607 jnlst_->Printf(J_VECTOR, J_CONVEXIFYING,"=============================\n");
00608 }
00609
00610 genRowCuts (si, cs, nchanged, changed, chg_bds);
00611 }
00612
00613
00614 if (nchanged)
00615 genColCuts (si, cs, nchanged, changed);
00616
00617 if (firstcall_ && (cs.sizeRowCuts () >= 1))
00618 jnlst_->Printf(J_ITERSUMMARY, J_CONVEXIFYING,
00619 "Couenne: %d initial row cuts\n", cs.sizeRowCuts ());
00620
00621 if (realOpt &&
00622 isOptimumCut (realOpt, cs, problem_))
00623 jnlst_->Printf(J_ITERSUMMARY, J_CONVEXIFYING,
00624 "Warning: Optimal solution was cut\n");
00625 }
00626
00627 catch (int exception) {
00628
00629 if ((exception == infeasible) && (!firstcall_)) {
00630
00631 jnlst_ -> Printf (J_ITERSUMMARY, J_CONVEXIFYING,
00632 "Couenne: Infeasible node\n");
00633
00634 WipeMakeInfeas (cs);
00635 }
00636
00637 if (babInfo)
00638 babInfo -> setInfeasibleNode ();
00639 }
00640
00641 delete [] chg_bds;
00642
00643 if (changed)
00644 free (changed);
00645
00646 if (firstcall_) {
00647
00648 jnlst_ -> Printf (J_SUMMARY, J_CONVEXIFYING,
00649 "Couenne: %d cuts (%d row, %d col) for linearization\n",
00650 cs.sizeRowCuts () + cs.sizeColCuts (),
00651 cs.sizeRowCuts (), cs.sizeColCuts ());
00652
00653 fictitiousBound (cs, problem_, true);
00654 firstcall_ = false;
00655 ntotalcuts_ = nrootcuts_ = cs.sizeRowCuts ();
00656
00657 } else {
00658
00659 problem_ -> domain () -> pop ();
00660
00661 ntotalcuts_ += (cs.sizeRowCuts () - nInitCuts);
00662
00663 if (saveOptimum)
00664 realOpt = saveOptimum;
00665 }
00666
00667 septime_ += CoinCpuTime () - now;
00668
00669 if (jnlst_ -> ProduceOutput (J_ITERSUMMARY, J_CONVEXIFYING)) {
00670
00671 if (cs.sizeColCuts ()) {
00672 jnlst_ -> Printf (J_ITERSUMMARY, J_CONVEXIFYING,"Couenne col cuts:\n");
00673 for (int i=0; i<cs.sizeColCuts (); i++)
00674 cs.colCutPtr (i) -> print ();
00675 }
00676 }
00677
00678 if (!(info.inTree))
00679 rootTime_ = CoinCpuTime ();
00680 }
00681
00682 }