// Copyright (C) 2007, International Business Machines Corporation and others. // All Rights Reserved. // This code is published under the Eclipse Public License. // // Authors : // Joao P. Goncalves, International Business Machines Corporation // // Date : November 12, 2007 #include "BonminConfig.h" #include "BonHeuristicDiveMIP.hpp" #include "CoinHelperFunctions.hpp" #include "CbcModel.hpp" #include "BonHeuristicDive.hpp" #include "BonSubMipSolver.hpp" #include "BonCbcLpStrategy.hpp" #ifdef COIN_HAS_CPX #include "OsiCpxSolverInterface.hpp" #endif #include "OsiClpSolverInterface.hpp" #include "OsiAuxInfo.hpp" #include "CoinTime.hpp" #include #include #include "CoinHelperFunctions.hpp" //#define DEBUG_BON_HEURISTIC_DIVE_MIP using namespace std; namespace Bonmin { HeuristicDiveMIP::HeuristicDiveMIP(BonminSetup * setup) : CbcHeuristic(), setup_(setup), howOften_(100), mip_(NULL) { Initialize(setup); } void HeuristicDiveMIP::Initialize(BonminSetup * b){ delete mip_; mip_ = new SubMipSolver (*b, b->prefix()); } HeuristicDiveMIP::HeuristicDiveMIP(const HeuristicDiveMIP ©) : CbcHeuristic(copy), setup_(copy.setup_), howOften_(copy.howOften_), mip_(new SubMipSolver(*copy.mip_)) { } HeuristicDiveMIP & HeuristicDiveMIP::operator=(const HeuristicDiveMIP & rhs) { if(this != &rhs) { CbcHeuristic::operator=(rhs); setup_ = rhs.setup_; howOften_ = rhs.howOften_; delete mip_; if(rhs.mip_) mip_ = new SubMipSolver(*rhs.mip_); } return *this; } HeuristicDiveMIP::~HeuristicDiveMIP(){ delete mip_; } struct MatComp{ const int * iRow; const int * jCol; /// Destructor bool operator()(int i,int j){ return (jCol[i] < jCol[j]) || (jCol[i] == jCol[j] && iRow[i] < iRow[j]); } }; int HeuristicDiveMIP::solution(double &solutionValue, double *betterSolution) { if(model_->getNodeCount() || model_->getCurrentPassNumber() > 1) return 0; if ((model_->getNodeCount()%howOften_)!=0||model_->getCurrentPassNumber()>1) return 0; int returnCode = 0; // 0 means it didn't find a feasible solution OsiTMINLPInterface * nlp = NULL; if(setup_->getAlgorithm() == B_BB) nlp = dynamic_cast(model_->solver()->clone()); else nlp = dynamic_cast(setup_->nonlinearSolver()->clone()); TMINLP2TNLP* minlp = nlp->problem(); // set tolerances double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); double primalTolerance = 1.0e-6; int numberColumns; int numberRows; int nnz_jac_g; int nnz_h_lag; Ipopt::TNLP::IndexStyleEnum index_style; minlp->get_nlp_info(numberColumns, numberRows, nnz_jac_g, nnz_h_lag, index_style); const Bonmin::TMINLP::VariableType* variableType = minlp->var_types(); const double* x_sol = minlp->x_sol(); const double* x_l = minlp->x_l(); const double* x_u = minlp->x_u(); //const double* g_sol = minlp->g_sol(); const double* g_l = minlp->g_l(); const double* g_u = minlp->g_u(); adjustPrimalTolerance(minlp, primalTolerance); assert(isNlpFeasible(minlp, primalTolerance)); // Get information about the linear and nonlinear part of the instance TMINLP* tminlp = nlp->model(); Ipopt::TNLP::LinearityType* variableLinearNonLinear = new Ipopt::TNLP::LinearityType [numberColumns]; tminlp->get_variables_linearity(numberColumns, variableLinearNonLinear); vector linearVariable; vector nonlinearVariable; for (int iColumn=0;iColumneval_jac_g(numberColumns, x_sol, false, numberRows, nnz_jac_g, indexRow, indexCol, 0); vector sortedIndex(nnz_jac_g); CoinIotaN(sortedIndex(), nnz_jac_g, 0); MatComp c; c.iRow = indexRow; c.jCol = indexCol; std::sort(sortedIndex.begin(), sortedIndex.end(), c); int* row = new int[nnz_jac_g]; int* columnStart = new int[numberColumns]; int* columnLength = new int[numberColumns]; CoinZeroN(columnStart, numberColumns); CoinZeroN(columnLength, numberColumns); vector > column(numberRows); // stores the index of // the variables in // each row vector > columnInt(numberRows); // stores the index of // the integer variables in // each row std::vector numberColumnsLinear(numberRows, 0); // stores the number // of the linear variables in // each row int indexCorrection = (index_style == Ipopt::TNLP::C_STYLE) ? 0 : 1; int iniCol = -1; for(int i=0; i integerNonlinearColumns; // stores the integer variables int numberFractionalNonlinearVariables = 0; for (size_t iNLCol=0;iNLColintegerTolerance) { numberFractionalNonlinearVariables++; } } } setInternalVariables(minlp); int iteration = -1; while(numberFractionalNonlinearVariables) { iteration++; // select a fractional variable to bound int bestColumn = -1; int bestRound = -1; // -1 rounds down, +1 rounds up selectVariableToBranch(minlp, integerNonlinearColumns, newSolution, bestColumn, bestRound); if(bestColumn >= 0) { if(bestRound < 0) minlp->SetVariableUpperBound(bestColumn, floor(newSolution[bestColumn])); else minlp->SetVariableLowerBound(bestColumn, ceil(newSolution[bestColumn])); } else { break; } nlp->initialSolve(); if(minlp->optimization_status() != Ipopt::SUCCESS) { break; } memcpy(newSolution,x_sol,numberColumns*sizeof(double)); numberFractionalNonlinearVariables = 0; for(int iIntCol=0; iIntCol<(int)integerNonlinearColumns.size(); iIntCol++) { int iColumn = integerNonlinearColumns[iIntCol]; double value=newSolution[iColumn]; if (fabs(floor(value+0.5)-value)>integerTolerance) numberFractionalNonlinearVariables++; } double newSolutionValue; minlp->eval_f(numberColumns, newSolution, true, newSolutionValue); } // now we are going to solve a MIP with the linear part of the problem int numberFractionalLinearVariables = 0; for (size_t iLCol=0;iLColintegerTolerance) { numberFractionalLinearVariables++; } } } bool feasible = true; if(numberFractionalLinearVariables) { int numberMIPRows = 0; int* mapRows = new int[numberRows]; for(int iRow=0; iRow 0) { mapRows[iRow] = numberMIPRows++; } } // set all linear variables to zero in order to compute the // impact of the nonlinear variables in each row int numberIntegerLinearColumns = 0; for (size_t iLCol=0;iLColeval_grad_f(numberColumns,newSolution,true,gradient_f); // create row lower and upper bounds for MILP minlp->eval_g(numberColumns, newSolution, true, numberRows, new_g_sol); double* row_lb = new double[numberMIPRows]; double* row_ub = new double[numberMIPRows]; for(int iRow=0; iRow -1) { assert(mapRows[iRow] < numberMIPRows); if(g_l[iRow] == (-1.0) * nlp->getInfinity()) row_lb[mapRows[iRow]] = g_l[iRow]; else row_lb[mapRows[iRow]] = g_l[iRow] - new_g_sol[iRow]; if(g_u[iRow] == nlp->getInfinity()) row_ub[mapRows[iRow]] = g_u[iRow]; else row_ub[mapRows[iRow]] = g_u[iRow] - new_g_sol[iRow]; } } // get the jacobian so that we know the coefficients of the MILP matrix double* jac_g = new double [nnz_jac_g]; minlp->eval_jac_g(numberColumns, x_sol, false, numberRows, nnz_jac_g, 0, 0, jac_g); // Define the constraint matrix for MILP CoinPackedMatrix* matrix = new CoinPackedMatrix(true,0,0); matrix->setDimensions(numberMIPRows,0); // create objective function and columns lower and upper bounds for MILP // and create columns for matrix in MILP double* objective = new double[numberLinearColumns]; double* col_lb = new double[numberLinearColumns]; double* col_ub = new double[numberLinearColumns]; int* indexIntegerColumn = new int[numberIntegerLinearColumns]; int numberIndexIntegerColumn = 0; for (size_t iLCol=0;iLColappendCol(newRow); if (variableType[iColumn] != Bonmin::TMINLP::CONTINUOUS) indexIntegerColumn[numberIndexIntegerColumn++] = static_cast(iLCol); } // load the problem to OSI OsiSolverInterface *si = mip_->solver(); bool delete_si = false; if(si == NULL){ si = new OsiClpSolverInterface; mip_->setLpSolver(si); delete_si = true; } CoinMessageHandler * handler = model_->messageHandler()->clone(); si->passInMessageHandler(handler); si->messageHandler()->setLogLevel(0); si->loadProblem(*matrix, col_lb, col_ub, objective, row_lb, row_ub); si->setInteger(indexIntegerColumn, numberIndexIntegerColumn); mip_->optimize(DBL_MAX, 0, 60); if(mip_->getLastSolution()) { const double* solution = mip_->getLastSolution(); assert(si->getNumCols() == static_cast(numberLinearColumns)); for (size_t iLCol=0;iLCol x_u[iColumn]) { feasible = false; break; } if (variableType[iColumn] != Bonmin::TMINLP::CONTINUOUS) { if (fabs(floor(value+0.5)-value)>integerTolerance) { feasible = false; break; } } } minlp->eval_g(numberColumns, newSolution, true, numberRows, new_g_sol); for(int iRow=0; iRowg_u[iRow]+primalTolerance) { if(minlp->optimization_status() != SUCCESS) { feasible = false; break; } else { #ifdef DEBUG_BON_HEURISTIC_DIVE_MIP cout<<"It should be infeasible because: "<SetVariableUpperBound(iColumn, value); minlp->SetVariableLowerBound(iColumn, value); } } } if(feasible) { nlp->initialSolve(); if(minlp->optimization_status() != Ipopt::SUCCESS) { feasible = false; } memcpy(newSolution,x_sol,numberColumns*sizeof(double)); } } #endif if(feasible) { double newSolutionValue; minlp->eval_f(numberColumns, newSolution, true, newSolutionValue); if(newSolutionValue < solutionValue) { memcpy(betterSolution,newSolution,numberColumns*sizeof(double)); solutionValue = newSolutionValue; returnCode = 1; } } delete [] variableLinearNonLinear; delete [] indexRow; delete [] indexCol; delete [] row; delete [] columnStart; delete [] columnLength; delete [] newSolution; delete [] new_g_sol; delete nlp; #ifdef DEBUG_BON_HEURISTIC_DIVE_MIP std::cout<<"DiveMIP returnCode = "<