00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "CbcModel.hpp"
00012
00013 #include "CouenneConfig.h"
00014 #include "CouenneFeasPump.hpp"
00015 #include "CouenneMINLPInterface.hpp"
00016 #include "CouenneProblem.hpp"
00017 #include "CouenneProblemElem.hpp"
00018 #include "CouenneExprVar.hpp"
00019
00020 #include "CouenneFPpool.hpp"
00021
00022 #ifdef COIN_HAS_SCIP
00023
00024 #include "scip/scip.h"
00025 #include "scip/cons_linear.h"
00026 #include "scip/scipdefplugins.h"
00027 #endif
00028
00029 using namespace Couenne;
00030
00031 #define NUMERICS_THRES 1e19
00032
00033 #ifdef COIN_HAS_SCIP
00034 void CouenneFeasPump::checkInfinity(SCIP *scip, SCIP_Real val, double infinity){
00035 if( SCIPisInfinity(scip, val) && val < infinity)
00036 problem_ -> Jnlst () -> Printf (Ipopt::J_WARNING, J_NLPHEURISTIC,
00037 "Warning: %g will be considered to be Infinity by SCIP.\n", val);
00038 }
00039 #endif
00040
00041
00043 OsiSolverInterface *createCloneMILP (const CouenneFeasPump *fp, CbcModel *model, bool isMILP, int *match);
00044
00045
00047 void addDistanceConstraints (const CouenneFeasPump *fp, OsiSolverInterface *lp, double *sol, bool isMILP, int *match);
00048
00049
00053 CouNumber CouenneFeasPump::solveMILP (const CouNumber *nSol0, CouNumber *&iSol, int niter, int* nsuciter) {
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 bool firstCall = (milp_ == NULL);
00096
00097
00098
00099
00100
00101
00102 if (firstCall) {
00103
00104 match_ = new int [problem_ -> nVars ()];
00105
00106 for (int i=problem_ -> nVars (); i--;)
00107 match_ [i] = -1;
00108
00109
00110
00111 milp_ = createCloneMILP (this, model_, true, match_);
00112
00113
00114
00115
00116
00117 for (int i=problem_ -> nVars (); i--;) {
00118 milp_ -> setColLower (i, problem_ -> Lb (i));
00119 milp_ -> setColUpper (i, problem_ -> Ub (i));
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129 if ((compDistInt_ == FP_DIST_POST) && !postlp_)
00130 postlp_ = createCloneMILP (this, model_, false, NULL);
00131 }
00132
00133 #if 0
00134 printf ("======================================================================================================================================\n");
00135 for (int i=0,j; i<problem_->nVars (); ++i)
00136 if (match_ [i] >= 0) {
00137 printf ("(%d,%d)", i, match_ [i]);
00138 if ((++j) % 6 == 0) printf ("\n");
00139 }
00140 printf ("======================================================================================================================================\n");
00141 #endif
00142
00143 int nInitRows = milp_ -> getNumRows ();
00144
00145 CouNumber * nlpSolExp;
00146
00147 if (nSol0) {
00148
00149 nlpSolExp = new CouNumber [problem_ -> nVars ()];
00150
00151 CoinCopyN (nSol0, problem_ -> nOrigVars (), nlpSolExp);
00152 problem_ -> getAuxs (nlpSolExp);
00153
00154 } else
00155 nlpSolExp = CoinCopyOfArray (milp_ -> getColSolution (),
00156 problem_ -> nVars ());
00157
00158
00159 addDistanceConstraints (this, milp_, nlpSolExp, true, match_);
00160
00161
00162
00163 delete [] nlpSolExp;
00164
00165 int nFinalRows = milp_ -> getNumRows ();
00166
00167
00168
00169
00170
00171
00172 if (firstCall)
00173 init_MILP ();
00174
00175 if (false) {
00176 static int cntr = 0;
00177 char filename [30];
00178 sprintf (filename, "fp-milp%04d", cntr++);
00179 milp_ -> writeLp (filename);
00180 printf ("saving FP_MILP %d\n", cntr);
00181 }
00182
00183 double obj = findSolution (nSol0, iSol, niter, nsuciter);
00184
00185 if ((nSol0 && iSol) &&
00186 (problem_ -> Jnlst () -> ProduceOutput (Ipopt::J_WARNING, J_NLPHEURISTIC))) {
00187
00188 double dist = 0.;
00189 int nNonint = 0;
00190
00191 for (int i = 0; i < problem_ -> nVars (); ++i) {
00192
00193 if (problem_ -> Var (i) -> Multiplicity () <= 0)
00194 continue;
00195
00196 if (problem_ -> Var (i) -> isInteger () &&
00197 (fabs (iSol [i] - floor (iSol [i] + .5)) > COUENNE_EPS))
00198 ++nNonint;
00199
00200 dist +=
00201 (iSol [i] - nSol0 [i]) *
00202 (iSol [i] - nSol0 [i]);
00203 }
00204
00205 problem_ -> Jnlst () -> Printf (Ipopt::J_WARNING, J_NLPHEURISTIC, "FP: after MILP, distance %g, %d nonintegers\n", sqrt (dist), nNonint);
00206 }
00207
00208
00209
00210
00211
00212
00213
00214 if (iSol &&
00215 (compDistInt_ != FP_DIST_ALL)) {
00216
00217
00218
00219
00220
00221 bool numerics = false;
00222
00223 if (compDistInt_ == FP_DIST_INT) {
00224
00225 for (std::vector <exprVar *>::iterator i = problem_ -> Variables (). begin ();
00226 i != problem_ -> Variables (). end (); ++i)
00227
00228 if (( (*i) -> Multiplicity () > 0) &&
00229 ! ((*i) -> isInteger ()) &&
00230 (fabs (iSol [(*i) -> Index ()]) > NUMERICS_THRES)) {
00231
00232 numerics = true;
00233 break;
00234 }
00235 }
00236
00237 if (numerics || (compDistInt_ == FP_DIST_POST)) {
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 if (!postlp_)
00251 postlp_ = createCloneMILP (this, model_, false, NULL);
00252
00253 int nvars = postlp_ -> getNumCols ();
00254
00255
00256 double
00257 *saveLB = CoinCopyOfArray (postlp_ -> getColLower (), nvars),
00258 *saveUB = CoinCopyOfArray (postlp_ -> getColUpper (), nvars),
00259 *newLB = CoinCopyOfArray (postlp_ -> getColLower (), nvars),
00260 *newUB = CoinCopyOfArray (postlp_ -> getColUpper (), nvars);
00261
00262
00263
00264 for (int i = problem_ -> nVars (); i--;)
00265
00266 if ((problem_ -> Var (i) -> Multiplicity () > 0) &&
00267 (milp_ -> isInteger (i)))
00268
00269 newLB [i] = newUB [i] = iSol [i] = floor (iSol [i] + .5);
00270
00271 postlp_ -> setColLower (newLB);
00272 postlp_ -> setColUpper (newUB);
00273
00274
00275
00276 int nInitRowsLP = postlp_ -> getNumRows ();
00277 addDistanceConstraints (this, postlp_, iSol, false, match_);
00278 int nFinalRowsLP = postlp_ -> getNumRows ();
00279
00280
00281
00282 postlp_ -> initialSolve ();
00283
00284
00285
00286 if (postlp_ -> isProvenOptimal ())
00287 CoinCopyN (postlp_ -> getColSolution (), problem_ -> nVars (), iSol);
00288
00289 postlp_ -> setColLower (saveLB);
00290 postlp_ -> setColUpper (saveUB);
00291
00292
00293
00294 delete [] saveLB;
00295 delete [] saveUB;
00296 delete [] newLB;
00297 delete [] newUB;
00298
00299
00300
00301 int
00302 nDeleted = nFinalRowsLP - nInitRowsLP,
00303 *deleted = new int [nDeleted],
00304 nCurRow = nInitRowsLP;
00305
00306 for (int i = nDeleted; i--;)
00307 deleted [i] = nCurRow++;
00308
00309 postlp_ -> deleteRows (nDeleted, deleted);
00310
00311 delete [] deleted;
00312 }
00313 }
00314
00315
00316
00317 int
00318 nDeleted = nFinalRows - nInitRows,
00319 *deleted = new int [nDeleted],
00320 nCurRow = nInitRows;
00321
00322 for (int i = nDeleted; i--;)
00323 deleted [i] = nCurRow++;
00324
00325 milp_ -> deleteRows (nDeleted, deleted);
00326
00327 delete [] deleted;
00328
00329 return obj;
00330 }