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 printf("Warning: %g will be considered to be Infinity by SCIP\n", val);
00037 }
00038 #endif
00039
00040
00042 OsiSolverInterface *createCloneMILP (const CouenneFeasPump *fp, CbcModel *model, bool isMILP);
00043
00044
00046 void addDistanceConstraints (const CouenneFeasPump *fp, OsiSolverInterface *lp, double *sol, bool isMILP);
00047
00048
00052 CouNumber CouenneFeasPump::solveMILP (CouNumber *nSol0, CouNumber *&iSol, int niter, int* nsuciter) {
00053
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 bool firstCall = (milp_ == NULL);
00095
00096
00097
00098 if (firstCall) {
00099
00100
00101
00102 milp_ = createCloneMILP (this, model_, true);
00103
00104
00105
00106
00107
00108
00109
00110
00111 if ((compDistInt_ == FP_DIST_POST) && !postlp_)
00112 postlp_ = createCloneMILP (this, model_, false);
00113 }
00114
00115 int nInitRows = milp_ -> getNumRows ();
00116
00117 CouNumber * nlpSolExp;
00118
00119 if (nSol0) {
00120
00121 nlpSolExp = new CouNumber [problem_ -> nVars ()];
00122
00123 CoinCopyN (nSol0, problem_ -> nOrigVars (), nlpSolExp);
00124 problem_ -> getAuxs (nlpSolExp);
00125
00126 } else
00127 nlpSolExp = CoinCopyOfArray (milp_ -> getColSolution (),
00128 problem_ -> nVars ());
00129
00130
00131 addDistanceConstraints (this, milp_, nlpSolExp, true);
00132
00133 delete [] nlpSolExp;
00134
00135 int nFinalRows = milp_ -> getNumRows ();
00136
00137
00138
00139
00140
00141
00142 if (firstCall)
00143 init_MILP ();
00144
00145 if (false) {
00146 static int cntr = 0;
00147 char filename [30];
00148 sprintf (filename, "fp-milp%04d", cntr++);
00149 milp_ -> writeLp (filename);
00150 }
00151
00152 double obj = findSolution (iSol, niter, nsuciter);
00153
00154 if ((nSol0 && iSol) &&
00155 (problem_ -> Jnlst () -> ProduceOutput (Ipopt::J_ERROR, J_NLPHEURISTIC))) {
00156
00157 double dist = 0.;
00158 int nNonint = 0;
00159
00160 for (int i = 0; i < problem_ -> nVars (); ++i) {
00161
00162 if (problem_ -> Var (i) -> isInteger () &&
00163 (fabs (iSol [i] - ceil (iSol [i] - .5)) > 1e-4))
00164 ++nNonint;
00165
00166 dist +=
00167 (iSol [i] - nSol0 [i]) *
00168 (iSol [i] - nSol0 [i]);
00169 }
00170
00171 printf ("FP: after MILP, distance %g, %d nonintegers\n", sqrt (dist), nNonint);
00172 }
00173
00174
00175
00176
00177
00178
00179
00180 if (iSol &&
00181 (compDistInt_ != FP_DIST_ALL)) {
00182
00183
00184
00185
00186
00187 bool numerics = false;
00188
00189 if (compDistInt_ == FP_DIST_INT) {
00190
00191 for (std::vector <exprVar *>::iterator i = problem_ -> Variables (). begin ();
00192 i != problem_ -> Variables (). end (); ++i)
00193
00194 if (( (*i) -> Multiplicity () > 0) &&
00195 ! ((*i) -> isInteger ()) &&
00196 (fabs (iSol [(*i) -> Index ()]) > NUMERICS_THRES)) {
00197
00198 numerics = true;
00199 break;
00200 }
00201 }
00202
00203 if (numerics || (compDistInt_ == FP_DIST_POST)) {
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 if (!postlp_)
00217 postlp_ = createCloneMILP (this, model_, false);
00218
00219 int nvars = postlp_ -> getNumCols ();
00220
00221
00222 double
00223 *saveLB = CoinCopyOfArray (postlp_ -> getColLower (), nvars),
00224 *saveUB = CoinCopyOfArray (postlp_ -> getColUpper (), nvars),
00225 *newLB = CoinCopyOfArray (postlp_ -> getColLower (), nvars),
00226 *newUB = CoinCopyOfArray (postlp_ -> getColUpper (), nvars);
00227
00228
00229
00230 for (int i = problem_ -> nVars (); i--;)
00231 if (milp_ -> isInteger (i))
00232 newLB [i] = newUB [i] = iSol [i];
00233
00234 postlp_ -> setColLower (newLB);
00235 postlp_ -> setColUpper (newUB);
00236
00237
00238
00239 int nInitRowsLP = postlp_ -> getNumRows ();
00240 addDistanceConstraints (this, postlp_, iSol, false);
00241 int nFinalRowsLP = postlp_ -> getNumRows ();
00242
00243
00244
00245 postlp_ -> initialSolve ();
00246
00247
00248
00249 if (postlp_ -> isProvenOptimal ())
00250 CoinCopyN (postlp_ -> getColSolution (), problem_ -> nVars (), iSol);
00251
00252 postlp_ -> setColLower (saveLB);
00253 postlp_ -> setColUpper (saveUB);
00254
00255
00256
00257 delete [] saveLB;
00258 delete [] saveUB;
00259 delete [] newLB;
00260 delete [] newUB;
00261
00262
00263
00264 int
00265 nDeleted = nFinalRowsLP - nInitRowsLP,
00266 *deleted = new int [nDeleted],
00267 nCurRow = nInitRowsLP;
00268
00269 for (int i = nDeleted; i--;)
00270 deleted [i] = nCurRow++;
00271
00272 postlp_ -> deleteRows (nDeleted, deleted);
00273
00274 delete [] deleted;
00275 }
00276 }
00277
00278
00279
00280 int
00281 nDeleted = nFinalRows - nInitRows,
00282 *deleted = new int [nDeleted],
00283 nCurRow = nInitRows;
00284
00285 for (int i = nDeleted; i--;)
00286 deleted [i] = nCurRow++;
00287
00288 milp_ -> deleteRows (nDeleted, deleted);
00289
00290 delete [] deleted;
00291
00292 return obj;
00293 }