// 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 "BonHeuristicFPump.hpp" #include "CoinHelperFunctions.hpp" #include "CbcModel.hpp" #include "OsiAuxInfo.hpp" #include "CoinTime.hpp" #include #include using namespace std; //#define DEBUG_BON_HEURISTIC_FPUMP namespace Bonmin { class score_sorter { public: //! Constructor score_sorter(const vector& score): score_(score) {} bool operator() (const int x, const int y) const { return score_[x]>score_[y]; } private: const vector& score_; }; HeuristicFPump::HeuristicFPump() : CbcHeuristic(), setup_(NULL), objective_norm_(1), enableAdvanced_(false) {} HeuristicFPump::HeuristicFPump(BonminSetup * setup) : CbcHeuristic(), setup_(setup), objective_norm_(1), enableAdvanced_(false) { Initialize(setup->options()); } HeuristicFPump::HeuristicFPump(const HeuristicFPump ©) : CbcHeuristic(copy), setup_(copy.setup_), objective_norm_(copy.objective_norm_), enableAdvanced_(copy.enableAdvanced_) {} HeuristicFPump & HeuristicFPump::operator=(const HeuristicFPump & rhs) { if(this != &rhs) { CbcHeuristic::operator=(rhs); setup_ = rhs.setup_; objective_norm_ = rhs.objective_norm_; enableAdvanced_ = rhs.enableAdvanced_; } return *this; } int HeuristicFPump::solution(double &solutionValue, double *betterSolution) { if(model_->getNodeCount() || model_->getCurrentPassNumber() > 1) return 0; bool integerSolutionAlreadyExists = false; if(model_->getSolutionCount()) { // bestSolutionValue = model_->getObjValue(); integerSolutionAlreadyExists = true; if(!enableAdvanced_) return 0; assert(solutionValue < 1.0e50); } const int maxNumberIterations = 200; const double toleranceObjectiveFP = 1.0e-5; 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; #if 0 OsiSolverInterface * solver = model_->solver(); solver->getDblParam(OsiPrimalTolerance,primalTolerance); #endif 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(); #ifdef DEBUG_BON_HEURISTIC_FPUMP const double* g_sol = minlp->g_sol(); const double* g_l = minlp->g_l(); const double* g_u = minlp->g_u(); // print bounds_info for(int i=0; iAddBoundedIntegerOption("feasibility_pump_objective_norm","Norm of feasibility pump objective function", 1, 2, 1,""); roptions->setOptionExtraInfo("feasibility_pump_objective_norm", 63); roptions->AddStringOption2("heuristic_feasibility_pump", "whether the heuristic feasibility pump should be used", "no", "no", "", "yes", "", ""); roptions->setOptionExtraInfo("heuristic_feasibility_pump", 63); roptions->SetRegisteringCategory("Primal Heuristics (undocumented)", RegisteredOptions::UndocumentedCategory); roptions->AddStringOption2("unstable_fp","use at your own risks", "no", "no", "", "yes", "",""); roptions->setOptionExtraInfo("unstable_fp", 63); } void HeuristicFPump::Initialize(Ipopt::SmartPtr options){ options->GetIntegerValue("feasibility_pump_objective_norm", objective_norm_, "bonmin."); options->GetEnumValue("unstable_fp", enableAdvanced_, "bonmin."); } RoundingFPump::RoundingFPump(TMINLP2TNLP* minlp) : minlp_(minlp) { gutsOfConstructor(); } RoundingFPump::~RoundingFPump() { delete [] col_and_jac_g_; } void RoundingFPump::gutsOfConstructor() { 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 double* x_sol = minlp_->x_sol(); // Get the indicies of the jacobian // This is also a way of knowing which variables are // used in each row int* indexRow = new int[nnz_jac_g]; int* indexCol = new int[nnz_jac_g]; minlp_->eval_jac_g(numberColumns_, x_sol, false, numberRows_, nnz_jac_g, indexRow, indexCol, 0); // get the jacobian for the solution with zeros double* jac_g = new double [nnz_jac_g]; double* zero_sol = new double [numberColumns_]; minlp_->get_starting_point(numberColumns_, 1, zero_sol, 0, NULL, NULL, numberRows_, 0, NULL); //memset(zero_sol, 0, numberColumns_ * sizeof(double)); minlp_->eval_jac_g(numberColumns_, zero_sol, true, numberRows_, nnz_jac_g, 0, 0, jac_g); col_and_jac_g_ = new vector >[numberRows_]; int indexCorrection = (index_style == Ipopt::TNLP::C_STYLE) ? 0 : 1; for(int i=0; i value(thisIndexCol, static_cast(jac_g[i])); col_and_jac_g_[thisIndexRow].push_back(value); } delete [] indexRow; delete [] indexCol; delete [] jac_g; delete [] zero_sol; } void RoundingFPump::round(const double integerTolerance, const double primalTolerance, double* solution) { const Bonmin::TMINLP::VariableType* variableType = minlp_->var_types(); const double* x_l = minlp_->x_l(); const double* x_u = minlp_->x_u(); const double* g_l = minlp_->g_l(); const double* g_u = minlp_->g_u(); for(int iRow=0; iRow > jac_g = col_and_jac_g_[iRow]; for (unsigned int j=0; j=1.0-integerTolerance || jac_g[j].second != 1.0 || variableType[iColumn] == Bonmin::TMINLP::CONTINUOUS) { sosConstraint = false; break; } else { weightedSum += counter * solution[iColumn]; counter++; } } #ifdef DEBUG_BON_HEURISTIC_FPUMP if(sosConstraint) { cout<<"weightedSum= "<