//------------------------------------------------------------------- // Generate Stored cuts //------------------------------------------------------------------- void CglStoredUser::generateCuts(const OsiSolverInterface & si, OsiCuts & cs, const CglTreeInfo info) const { // Get basic problem information const double * solution = si.getColSolution(); if (info.inTree&&info.pass>numberPasses_) { // only continue if integer feasible int numberColumns=si.getNumCols(); int i; const double * colUpper = si.getColUpper(); const double * colLower = si.getColLower(); int numberAway=0; for (i=0;i<numberColumns;i++) { double value = solution[i]; // In case slightly away from bounds value = CoinMax(colLower[i],value); value = CoinMin(colUpper[i],value); if (si.isInteger(i)&&fabs(value-fabs(value+0.5))>1.0e-5) numberAway++; } if (numberAway) return; // let code branch } int numberRowCuts = cuts_.sizeRowCuts(); for (int i=0;i<numberRowCuts;i++) { const OsiRowCut * rowCutPointer = cuts_.rowCutPtr(i); double violation = rowCutPointer->violated(solution); if (violation>=requiredViolation_) cs.insert(*rowCutPointer); } }
void CglClique::generateCuts(const OsiSolverInterface& si, OsiCuts & cs, const CglTreeInfo info) { int i; bool has_petol_set = petol != -1.0; if (! has_petol_set) si.getDblParam(OsiPrimalTolerance, petol); int numberOriginalRows = si.getNumRows(); if (info.inTree&&justOriginalRows_) numberOriginalRows = info.formulation_rows; int numberRowCutsBefore = cs.sizeRowCuts(); // First select which rows/columns we are interested in. if (!setPacking_) { selectFractionalBinaries(si); if (!sp_orig_row_ind) { selectRowCliques(si,numberOriginalRows); } } else { selectFractionals(si); delete[] sp_orig_row_ind; sp_numrows = numberOriginalRows; //sp_numcols = si.getNumCols(); sp_orig_row_ind = new int[sp_numrows]; for (i = 0; i < sp_numrows; ++i) sp_orig_row_ind[i] = i; } // Just original rows if (justOriginalRows_&&info.inTree) sp_numrows = CoinMin(info.formulation_rows,sp_numrows); createSetPackingSubMatrix(si); fgraph.edgenum = createNodeNode(); createFractionalGraph(); cl_indices = new int[sp_numcols]; cl_del_indices = new int[sp_numcols]; if (do_row_clique) find_rcl(cs); if (do_star_clique) find_scl(cs); if (!info.inTree&&((info.options&4)==4||((info.options&8)&&!info.pass))) { int numberRowCutsAfter = cs.sizeRowCuts(); for (int i=numberRowCutsBefore;i<numberRowCutsAfter;i++) cs.rowCutPtr(i)->setGloballyValid(); } delete[] cl_indices; cl_indices = 0; delete[] cl_del_indices; cl_del_indices = 0; deleteFractionalGraph(); delete[] node_node; node_node = 0; deleteSetPackingSubMatrix(); if (! has_petol_set) petol = -1; }
CbcBranchingObject * CbcNWay::createCbcBranch(OsiSolverInterface * solver, const OsiBranchingInformation * /*info*/, int /*way*/) { int numberFree = 0; int j; //OsiSolverInterface * solver = model_->solver(); const double * solution = model_->testSolution(); const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); int * list = new int[numberMembers_]; double * sort = new double[numberMembers_]; for (j = 0; j < numberMembers_; j++) { int iColumn = members_[j]; double value = solution[iColumn]; value = CoinMax(value, lower[iColumn]); value = CoinMin(value, upper[iColumn]); if (upper[iColumn] > lower[iColumn]) { double distance = upper[iColumn] - value; list[numberFree] = j; sort[numberFree++] = distance; } } assert (numberFree); // sort CoinSort_2(sort, sort + numberFree, list); // create object CbcBranchingObject * branch; branch = new CbcNWayBranchingObject(model_, this, numberFree, list); branch->setOriginalObject(this); delete [] list; delete [] sort; return branch; }
// Return "down" estimate double CbcSimpleIntegerPseudoCost::downEstimate() const { OsiSolverInterface * solver = model_->solver(); const double * solution = model_->testSolution(); const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); double value = solution[columnNumber_]; value = CoinMax(value, lower[columnNumber_]); value = CoinMin(value, upper[columnNumber_]); if (upper[columnNumber_] == lower[columnNumber_]) { // fixed return 0.0; } double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); double below = floor(value + integerTolerance); double above = below + 1.0; if (above > upper[columnNumber_]) { above = below; below = above - 1; } double downCost = CoinMax((value - below) * downPseudoCost_, 0.0); return downCost; }
// Creates a branching object CbcBranchingObject * CbcSimpleIntegerFixed::createBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info, int way) { const double * solution = model_->testSolution(); const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); double value = solution[columnNumber_]; value = CoinMax(value, lower[columnNumber_]); value = CoinMin(value, upper[columnNumber_]); assert (upper[columnNumber_]>lower[columnNumber_]); if (!model_->hotstartSolution()) { double nearest = floor(value+0.5); double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); if (fabs(value-nearest)<integerTolerance) { // adjust value if (nearest!=upper[columnNumber_]) value = nearest+2.0*integerTolerance; else value = nearest-2.0*integerTolerance; } } else { const double * hotstartSolution = model_->hotstartSolution(); double targetValue = hotstartSolution[columnNumber_]; if (way>0) value = targetValue-0.1; else value = targetValue+0.1; } CbcBranchingObject * branch = new CbcIntegerBranchingObject(model_,columnNumber_,way, value); branch->setOriginalObject(this); return branch; }
// Fill most values void ClpDualRowSteepest::fill(const ClpDualRowSteepest& rhs) { state_ = rhs.state_; mode_ = rhs.mode_; persistence_ = rhs.persistence_; assert (model_->numberRows() == rhs.model_->numberRows()); model_ = rhs.model_; assert(model_); int number = model_->numberRows(); if (rhs.savedWeights_) number = CoinMin(number, rhs.savedWeights_->capacity()); if (rhs.infeasible_ != NULL) { if (!infeasible_) infeasible_ = new CoinIndexedVector(rhs.infeasible_); else *infeasible_ = *rhs.infeasible_; } else { delete infeasible_; infeasible_ = NULL; } if (rhs.weights_ != NULL) { if (!weights_) weights_ = new double[number]; ClpDisjointCopyN(rhs.weights_, number, weights_); } else { delete [] weights_; weights_ = NULL; } if (rhs.alternateWeights_ != NULL) { if (!alternateWeights_) alternateWeights_ = new CoinIndexedVector(rhs.alternateWeights_); else *alternateWeights_ = *rhs.alternateWeights_; } else { delete alternateWeights_; alternateWeights_ = NULL; } if (rhs.savedWeights_ != NULL) { if (!savedWeights_) savedWeights_ = new CoinIndexedVector(rhs.savedWeights_); else *savedWeights_ = *rhs.savedWeights_; } else { delete savedWeights_; savedWeights_ = NULL; } if (rhs.dubiousWeights_) { assert(model_); int number = model_->numberRows(); if (!dubiousWeights_) dubiousWeights_ = new int[number]; ClpDisjointCopyN(rhs.dubiousWeights_, number, dubiousWeights_); } else { delete [] dubiousWeights_; dubiousWeights_ = NULL; } }
/* Fills in array with problem name */ COINLIBAPI void COINLINKAGE Clp_problemName(Clp_Simplex * model, int maxNumberCharacters, char * array) { std::string name = model->model_->problemName(); maxNumberCharacters = CoinMin(maxNumberCharacters, ((int) strlen(name.c_str()))+1) ; strncpy(array, name.c_str(), maxNumberCharacters - 1); array[maxNumberCharacters-1] = '\0'; }
int main(int argc, const char *argv[]) { // Get model in some way ClpInterior model; // Open graph and parameter files //FILE *fpin = fopen("./g.graph","r"); //FILE *fpp = fopen("./gparm","r"); FILE *fpin = fopen("./g.tiny", "r"); FILE *fpp = fopen("./gparm.tiny", "r"); assert(fpin); assert(fpp); myPdco stuff(model, fpin, fpp); Info info; Outfo outfo; Options options; /* * Set the input parameters for LSQR. */ options.gamma = stuff.getD1(); options.delta = stuff.getD2(); options.MaxIter = 40; options.FeaTol = 5.0e-4; options.OptTol = 5.0e-4; options.StepTol = 0.99; // options.x0min = 10.0/num_cols; options.x0min = 0.01; options.z0min = 0.01; options.mu0 = 1.0e-6; options.LSmethod = 3; // 1=Cholesky 2=QR 3=LSQR options.LSproblem = 1; // See below options.LSQRMaxIter = 999; options.LSQRatol1 = 1.0e-3; // Initial atol options.LSQRatol2 = 1.0e-6; // Smallest atol (unless atol1 is smaller) options.LSQRconlim = 1.0e12; info.atolmin = options.LSQRatol2; info.LSdamp = 0.0; // These are already set? model.xsize_ = 50.0 / (model.numberColumns()); model.xsize_ = CoinMin(1.0, model.xsize_); /* * Solve the test problem */ model.pdco(&stuff, options, info, outfo); /* * Examine the results. * Print the residual norms RNORM and ARNORM given by LSQR, and then compute */ return 0; }
/// heuristic method int GuessHeuristic::solution(double &solutionValue, double *betterSolution) { // Get pointer to pseudo costs object const BonChooseVariable* chooseMethod = dynamic_cast<BonChooseVariable*>(model_->branchingMethod()->chooseMethod()); if (!chooseMethod) { (*model_->messageHandler()) << "Can't get pseudo costs!!!\n"; solutionValue = model_->getCurrentMinimizationObjValue(); return -1; } const OsiPseudoCosts& pseudoCosts = chooseMethod->pseudoCosts(); int numberObjects = pseudoCosts.numberObjects(); assert(numberObjects == model_->numberObjects()); const double* upTotalChange = pseudoCosts.upTotalChange(); const double* downTotalChange = pseudoCosts.downTotalChange(); const int* upNumber = pseudoCosts.upNumber(); const int* downNumber = pseudoCosts.downNumber(); double sumUpTot = 0.; int numberUpTot = 0; double sumDownTot = 0.; int numberDownTot = 0; for (int i=0;i<numberObjects;i++) { sumUpTot += upTotalChange[i]; numberUpTot += upNumber[i]; sumDownTot += downTotalChange[i]; numberDownTot += downNumber[i]; } if (!numberUpTot || !numberDownTot) { // don't have ANY pseudo-costs information yet solutionValue = COIN_DBL_MAX; return -1; } double upAvrg=sumUpTot/numberUpTot; double downAvrg=sumDownTot/numberDownTot; OsiObject** object = model_->objects(); solutionValue = model_->getCurrentMinimizationObjValue(); for (int iObj = 0; iObj < numberObjects; iObj++) { //printf("%3d upest=%e uptot=%e upnum=%d downest=%e downtot=%e downnum=%d ", iObj, object[iObj]->upEstimate(), upTotalChange[iObj], upNumber[iObj], object[iObj]->downEstimate(), downTotalChange[iObj], downNumber[iObj]); double upEstimate = upNumber[iObj] ? object[iObj]->upEstimate()*upTotalChange[iObj]/upNumber[iObj] : object[iObj]->upEstimate()*upAvrg; double downEstimate = downNumber[iObj] ? object[iObj]->downEstimate()*downTotalChange[iObj]/downNumber[iObj] : object[iObj]->downEstimate()*downAvrg; //printf("up=%e down=%e\n", upEstimate, downEstimate); solutionValue += CoinMin(upEstimate,downEstimate); } //printf("solutionValue = %e\n", solutionValue); return -1; }
// Resize objective void ClpQuadraticObjective::resize(int newNumberColumns) { if (numberColumns_ != newNumberColumns) { int newExtended = newNumberColumns + (numberExtendedColumns_ - numberColumns_); int i; double *newArray = new double[newExtended]; if (objective_) CoinMemcpyN(objective_, CoinMin(newExtended, numberExtendedColumns_), newArray); delete[] objective_; objective_ = newArray; for (i = numberColumns_; i < newNumberColumns; i++) objective_[i] = 0.0; if (gradient_) { newArray = new double[newExtended]; if (gradient_) CoinMemcpyN(gradient_, CoinMin(newExtended, numberExtendedColumns_), newArray); delete[] gradient_; gradient_ = newArray; for (i = numberColumns_; i < newNumberColumns; i++) gradient_[i] = 0.0; } if (quadraticObjective_) { if (newNumberColumns < numberColumns_) { int *which = new int[numberColumns_ - newNumberColumns]; int i; for (i = newNumberColumns; i < numberColumns_; i++) which[i - newNumberColumns] = i; quadraticObjective_->deleteRows(numberColumns_ - newNumberColumns, which); quadraticObjective_->deleteCols(numberColumns_ - newNumberColumns, which); delete[] which; } else { quadraticObjective_->setDimensions(newNumberColumns, newNumberColumns); } } numberColumns_ = newNumberColumns; numberExtendedColumns_ = newExtended; } }
//---------------------------------------------------------------- // Assignment operator //------------------------------------------------------------------- ClpDualRowSteepest & ClpDualRowSteepest::operator=(const ClpDualRowSteepest& rhs) { if (this != &rhs) { ClpDualRowPivot::operator=(rhs); state_ = rhs.state_; mode_ = rhs.mode_; persistence_ = rhs.persistence_; model_ = rhs.model_; delete [] weights_; delete [] dubiousWeights_; delete infeasible_; delete alternateWeights_; delete savedWeights_; assert(model_); int number = model_->numberRows(); if (rhs.savedWeights_) number = CoinMin(number, rhs.savedWeights_->capacity()); if (rhs.infeasible_ != NULL) { infeasible_ = new CoinIndexedVector(rhs.infeasible_); } else { infeasible_ = NULL; } if (rhs.weights_ != NULL) { weights_ = new double[number]; ClpDisjointCopyN(rhs.weights_, number, weights_); } else { weights_ = NULL; } if (rhs.alternateWeights_ != NULL) { alternateWeights_ = new CoinIndexedVector(rhs.alternateWeights_); } else { alternateWeights_ = NULL; } if (rhs.savedWeights_ != NULL) { savedWeights_ = new CoinIndexedVector(rhs.savedWeights_); } else { savedWeights_ = NULL; } if (rhs.dubiousWeights_) { assert(model_); int number = model_->numberRows(); dubiousWeights_ = new int[number]; ClpDisjointCopyN(rhs.dubiousWeights_, number, dubiousWeights_); } else { dubiousWeights_ = NULL; } } return *this; }
DSP_RTN_CODE BdMaster::init() { BGN_TRY_CATCH /** create problem */ createProblem(); /** set node limit */ si_->setNodeLimit(par_->getIntParam("NODE_LIM")); /** set print level */ si_->setPrintLevel(CoinMin(par_->getIntParam("LOG_LEVEL") + 2, 5)); END_TRY_CATCH_RTN(;,DSP_RTN_ERR)
template <typename T> void CoinDenseVector<T>::resize(int newsize, T value) { if (newsize != nElements_){ assert(newsize > 0); T *newarray = new T[newsize]; int cpysize = CoinMin(newsize, nElements_); CoinMemcpyN( elements_,cpysize,newarray); delete[] elements_; elements_ = newarray; nElements_ = newsize; for(int i=cpysize; i<newsize; i++) elements_[i] = value; } }
//------------------------------------------------------------------- // Copy constructor //------------------------------------------------------------------- ClpDualRowSteepest::ClpDualRowSteepest (const ClpDualRowSteepest & rhs) : ClpDualRowPivot(rhs) { state_ = rhs.state_; mode_ = rhs.mode_; persistence_ = rhs.persistence_; model_ = rhs.model_; if ((model_ && model_->whatsChanged() & 1) != 0) { int number = model_->numberRows(); if (rhs.savedWeights_) number = CoinMin(number, rhs.savedWeights_->capacity()); if (rhs.infeasible_) { infeasible_ = new CoinIndexedVector(rhs.infeasible_); } else { infeasible_ = NULL; } if (rhs.weights_) { weights_ = new double[number]; ClpDisjointCopyN(rhs.weights_, number, weights_); } else { weights_ = NULL; } if (rhs.alternateWeights_) { alternateWeights_ = new CoinIndexedVector(rhs.alternateWeights_); } else { alternateWeights_ = NULL; } if (rhs.savedWeights_) { savedWeights_ = new CoinIndexedVector(rhs.savedWeights_); } else { savedWeights_ = NULL; } if (rhs.dubiousWeights_) { assert(model_); int number = model_->numberRows(); dubiousWeights_ = new int[number]; ClpDisjointCopyN(rhs.dubiousWeights_, number, dubiousWeights_); } else { dubiousWeights_ = NULL; } } else { infeasible_ = NULL; weights_ = NULL; alternateWeights_ = NULL; savedWeights_ = NULL; dubiousWeights_ = NULL; } }
// Resize objective void ClpLinearObjective::resize(int newNumberColumns) { if (numberColumns_ != newNumberColumns) { int i; double * newArray = new double[newNumberColumns]; if (objective_) CoinMemcpyN(objective_, CoinMin(newNumberColumns, numberColumns_), newArray); delete [] objective_; objective_ = newArray; for (i = numberColumns_; i < newNumberColumns; i++) objective_[i] = 0.0; numberColumns_ = newNumberColumns; } }
// Creates a branching object from this infeasible object. BcpsBranchObject * BlisObjectInt::createBranchObject(BcpsModel *m, int direction) const { BlisModel *model = dynamic_cast<BlisModel* >(m); OsiSolverInterface * solver = model->solver(); double integerTolerance = model->BlisPar()->entry(BlisParams::integerTol); const double * solution = solver->getColSolution(); const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); double value = solution[columnIndex_]; //std::cout << "COL"<< columnIndex_ << ": x = " << value << std::endl; // Force value in bounds. value = CoinMax(value, lower[columnIndex_]); value = CoinMin(value, upper[columnIndex_]); double nearest = floor(value + 0.5); assert (upper[columnIndex_] > lower[columnIndex_]); int hotstartStrategy = model->getHotstartStrategy(); if (hotstartStrategy <= 0) { if (fabs(value - nearest) < integerTolerance) { // Already integeral. std::cout << "ERROR: COL" << columnIndex_ << ": x=" << value << ", nearest=" << nearest << ", intTol=" << integerTolerance << std::endl; assert(0); } } else { const double * incumbent = model->incumbent(); double targetValue = incumbent[columnIndex_]; if (direction > 0) { value = targetValue - 0.1; } else { value = targetValue + 0.1; } } return new BlisBranchObjectInt(model, objectIndex_, direction, value); }
void OsiTestSolverInterface::applyColCut(const OsiColCut& cc) { int i; const double* lb_elem = cc.lbs().getElements(); const int* lb_ind = cc.lbs().getIndices(); for (i = cc.lbs().getNumElements() - 1; i >= 0; --i) { collower_[lb_ind[i]] = CoinMax(collower_[lb_ind[i]], lb_elem[i]); } const double* ub_elem = cc.ubs().getElements(); const int* ub_ind = cc.ubs().getIndices(); for (i = cc.ubs().getNumElements() - 1; i >= 0; --i) { colupper_[ub_ind[i]] = CoinMin(colupper_[ub_ind[i]], ub_elem[i]); } }
double CbcTreeArray::getBestPossibleObjective() { double bestPossibleObjective = 1e100; for (int i = 0 ; i < static_cast<int> (nodes_.size()) ; i++) { if (nodes_[i] && nodes_[i]->objectiveValue() < bestPossibleObjective) { bestPossibleObjective = nodes_[i]->objectiveValue(); } } if (lastNode_) { bestPossibleObjective = CoinMin(bestPossibleObjective, lastNode_->objectiveValue()); } CbcCompareDefault * compareDefault = dynamic_cast<CbcCompareDefault *> (comparison_.test_); assert (compareDefault); compareDefault->setBestPossible(bestPossibleObjective); return bestPossibleObjective; }
// Returns true if current solution satsifies one side of branch bool OsiSolverBranch::feasibleOneWay(const OsiSolverInterface & solver) const { bool feasible = false; int numberColumns = solver.getNumCols(); const double * columnLower = solver.getColLower(); const double * columnUpper = solver.getColUpper(); const double * columnSolution = solver.getColSolution(); double primalTolerance; solver.getDblParam(OsiPrimalTolerance,primalTolerance); for (int base = 0; base<4; base +=2) { feasible=true; int i; for (i=start_[base];i<start_[base+1];i++) { int iColumn = indices_[i]; if (iColumn<numberColumns) { double value = CoinMax(bound_[i],columnLower[iColumn]); if (columnSolution[iColumn]<value-primalTolerance) { feasible=false; break; } } else { abort(); // do later (other stuff messed up anyway - e.g. CBC) } } if (!feasible) break; for (i=start_[base+1];i<start_[base+2];i++) { int iColumn = indices_[i]; if (iColumn<numberColumns) { double value = CoinMin(bound_[i],columnUpper[iColumn]); if (columnSolution[iColumn]>value+primalTolerance) { feasible=false; break; } } else { abort(); // do later (other stuff messed up anyway - e.g. CBC) } } if (feasible) break; // OK this way } return feasible; }
OsiRowCut * BlisConstraint::createOsiRowCut() { double lower = CoinMax(getLbHard(), getLbSoft()); double upper = CoinMin(getUbHard(), getUbSoft()); OsiRowCut * cut = new OsiRowCut; if (!cut) { /* Out of memory. */ throw CoinError("Out of Memory", "Blis_constraintToOsiCut", "NONE"); } assert(size_ > 0); cut->setLb(lower); cut->setUb(upper); cut->setRow(size_, indices_, values_); return cut; }
CbcBranchingObject * CbcSimpleIntegerPseudoCost::createCbcBranch(OsiSolverInterface * solver, const OsiBranchingInformation * /*info*/, int way) { //OsiSolverInterface * solver = model_->solver(); const double * solution = model_->testSolution(); const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); double value = solution[columnNumber_]; value = CoinMax(value, lower[columnNumber_]); value = CoinMin(value, upper[columnNumber_]); #ifndef NDEBUG double nearest = floor(value + 0.5); double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); assert (upper[columnNumber_] > lower[columnNumber_]); #endif if (!model_->hotstartSolution()) { assert (fabs(value - nearest) > integerTolerance); } else { const double * hotstartSolution = model_->hotstartSolution(); double targetValue = hotstartSolution[columnNumber_]; if (way > 0) value = targetValue - 0.1; else value = targetValue + 0.1; } CbcIntegerPseudoCostBranchingObject * newObject = new CbcIntegerPseudoCostBranchingObject(model_, columnNumber_, way, value); double up = upPseudoCost_ * (ceil(value) - value); double down = downPseudoCost_ * (value - floor(value)); double changeInGuessed = up - down; if (way > 0) changeInGuessed = - changeInGuessed; changeInGuessed = CoinMax(0.0, changeInGuessed); //if (way>0) //changeInGuessed += 1.0e8; // bias to stay up newObject->setChangeInGuessed(changeInGuessed); newObject->setOriginalObject(this); return newObject; }
bool CglGMI::checkDynamism(const double* cutElem, const int* cutIndex, int cutNz) { double min = param.getINFINIT(); double max = 0.0; double val = 0.0; for (int i = 0; i < cutNz; ++i) { if (!isZero(cutElem[i])) { val = fabs(cutElem[i]); min = CoinMin(min, val); max = CoinMax(max, val); } } if (max > min*param.getMAXDYN()) { #if defined GMI_TRACE_CLEAN printf("Max elem %g, min elem %g, dyn %g; cut discarded\n", max, min, max/min); #endif return false; } else{ return true; } } /* checkDynamism */
double BlisConstraint::violation(const double *lpSolution) { int k, varInd; double activity = 0.0; double rowLower = CoinMax(lbHard_, lbSoft_); double rowUpper = CoinMin(ubHard_, ubSoft_); double violation = -ALPS_DBL_MAX; // Any negative number is OK for (k = 0; k < size_; ++k) { varInd = indices_[k]; activity += values_[varInd] * lpSolution[varInd]; } if (rowLower > -ALPS_INFINITY) { violation = rowLower - activity; } if (rowUpper < ALPS_INFINITY) { violation = CoinMax(violation, activity - rowUpper); } return violation; }
// This looks at solution and sets bounds to contain solution void CbcNWay::feasibleRegion() { int j; OsiSolverInterface * solver = model_->solver(); const double * solution = model_->testSolution(); const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); for (j = 0; j < numberMembers_; j++) { int iColumn = members_[j]; double value = solution[iColumn]; value = CoinMax(value, lower[iColumn]); value = CoinMin(value, upper[iColumn]); if (value >= upper[iColumn] - integerTolerance) { solver->setColLower(iColumn, upper[iColumn]); } else { assert (value <= lower[iColumn] + integerTolerance); solver->setColUpper(iColumn, lower[iColumn]); } } }
// Infeasibility - large is 0.5 double CbcSimpleIntegerFixed::infeasibility(int & preferredWay) const { OsiSolverInterface * solver = model_->solver(); const double * solution = model_->testSolution(); const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); double value = solution[columnNumber_]; value = CoinMax(value, lower[columnNumber_]); value = CoinMin(value, upper[columnNumber_]); /*printf("%d %g %g %g %g\n",columnNumber_,value,lower[columnNumber_], solution[columnNumber_],upper[columnNumber_]);*/ double nearest = floor(value+(1.0-breakEven_)); assert (breakEven_>0.0&&breakEven_<1.0); double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); if (nearest>value) preferredWay=1; else preferredWay=-1; if (preferredWay_) preferredWay=preferredWay_; double weight = fabs(value-nearest); // normalize so weight is 0.5 at break even if (nearest<value) weight = (0.5/breakEven_)*weight; else weight = (0.5/(1.0-breakEven_))*weight; if (fabs(value-nearest)<=integerTolerance) { if (upper[columnNumber_]==lower[columnNumber_]) return 0.0; else return 1.0e-5; } else { return weight; } }
// Useful constructor (which are integer indices) CbcGeneralDepth::CbcGeneralDepth (CbcModel * model, int maximumDepth) : CbcGeneral(model), maximumDepth_(maximumDepth), maximumNodes_(0), whichSolution_(-1), numberNodes_(0), nodeInfo_(NULL) { assert(maximumDepth_ < 1000000); if (maximumDepth_ > 0) maximumNodes_ = (1 << maximumDepth_) + 1 + maximumDepth_; else if (maximumDepth_ < 0) maximumNodes_ = 1 + 1 - maximumDepth_; else maximumNodes_ = 0; #define MAX_NODES 100 maximumNodes_ = CoinMin(maximumNodes_, 1 + maximumDepth_ + MAX_NODES); if (maximumNodes_) { nodeInfo_ = new ClpNodeStuff(); nodeInfo_->maximumNodes_ = maximumNodes_; ClpNodeStuff * info = nodeInfo_; // for reduced costs and duals info->solverOptions_ |= 7; if (maximumDepth_ > 0) { info->nDepth_ = maximumDepth_; } else { info->nDepth_ = - maximumDepth_; info->solverOptions_ |= 32; } ClpNode ** nodeInfo = new ClpNode * [maximumNodes_]; for (int i = 0; i < maximumNodes_; i++) nodeInfo[i] = NULL; info->nodeInfo_ = nodeInfo; } else { nodeInfo_ = NULL; } }
// Force this object within exiting bounds, then fix the bounds at the // the nearest integer value. Assume solution value is within tolerance of // the nearest integer. void BlisObjectInt::feasibleRegion(BcpsModel *m) { BlisModel *model = dynamic_cast<BlisModel* >(m); OsiSolverInterface * solver = model->solver(); const double * solution = solver->getColSolution(); const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); double value = solution[columnIndex_]; // 1) Force value to be in bounds. value = CoinMax(value, lower[columnIndex_]); value = CoinMin(value, upper[columnIndex_]); double nearest = floor(value + 0.5); // 2) Fix variable at the nearest integer assert (fabs(value - nearest) <= 0.01); solver->setColLower(columnIndex_, nearest); solver->setColUpper(columnIndex_, nearest); }
int main(int argc, const char *argv[]) { #if COIN_BIG_INDEX<2 ClpSimplex model; int status; int maxIts = 0; int maxFactor = 100; if (argc < 2) { #if defined(SAMPLEDIR) status = model.readMps(SAMPLEDIR "/p0033.mps", true); #else fprintf(stderr, "Do not know where to find sample MPS files.\n"); exit(1); #endif } else status = model.readMps(argv[1]); if (status) { printf("errors on input\n"); exit(77); } if (argc > 2) { maxFactor = atoi(argv[2]); printf("max factor %d\n", maxFactor); } if (argc > 3) { maxIts = atoi(argv[3]); printf("max its %d\n", maxIts); } // For now scaling off model.scaling(0); if (maxIts) { // Do partial dantzig ClpPrimalColumnSteepest dantzig(5); model.setPrimalColumnPivotAlgorithm(dantzig); //model.messageHandler()->setLogLevel(63); model.setFactorizationFrequency(maxFactor); model.setMaximumIterations(maxIts); model.primal(); if (!model.status()) exit(1); } // find gub int numberRows = model.numberRows(); int * gubStart = new int[numberRows+1]; int * gubEnd = new int[numberRows]; int * which = new int[numberRows]; int * whichGub = new int[numberRows]; int numberColumns = model.numberColumns(); int * mark = new int[numberColumns]; int iRow, iColumn; // delete variables fixed to zero const double * columnLower = model.columnLower(); const double * columnUpper = model.columnUpper(); int numberDelete = 0; for (iColumn = 0; iColumn < numberColumns; iColumn++) { if (columnUpper[iColumn] == 0.0 && columnLower[iColumn] == 0.0) mark[numberDelete++] = iColumn; } if (numberDelete) { model.deleteColumns(numberDelete, mark); numberColumns -= numberDelete; columnLower = model.columnLower(); columnUpper = model.columnUpper(); #if 0 CoinMpsIO writer; writer.setMpsData(*model.matrix(), COIN_DBL_MAX, model.getColLower(), model.getColUpper(), model.getObjCoefficients(), (const char*) 0 /*integrality*/, model.getRowLower(), model.getRowUpper(), NULL, NULL); writer.writeMps("cza.mps", 0, 0, 1); #endif } double * lower = new double[numberRows]; double * upper = new double[numberRows]; const double * rowLower = model.rowLower(); const double * rowUpper = model.rowUpper(); for (iColumn = 0; iColumn < numberColumns; iColumn++) mark[iColumn] = -1; CoinPackedMatrix * matrix = model.matrix(); // get row copy CoinPackedMatrix rowCopy = *matrix; rowCopy.reverseOrdering(); const int * column = rowCopy.getIndices(); const int * rowLength = rowCopy.getVectorLengths(); const CoinBigIndex * rowStart = rowCopy.getVectorStarts(); const double * element = rowCopy.getElements(); int putGub = numberRows; int putNonGub = numberRows; int * rowIsGub = new int [numberRows]; for (iRow = numberRows - 1; iRow >= 0; iRow--) { bool gubRow = true; int first = numberColumns + 1; int last = -1; for (int j = rowStart[iRow]; j < rowStart[iRow] + rowLength[iRow]; j++) { if (element[j] != 1.0) { gubRow = false; break; } else { int iColumn = column[j]; if (mark[iColumn] >= 0) { gubRow = false; break; } else { last = CoinMax(last, iColumn); first = CoinMin(first, iColumn); } } } if (last - first + 1 != rowLength[iRow] || !gubRow) { which[--putNonGub] = iRow; rowIsGub[iRow] = 0; } else { for (int j = rowStart[iRow]; j < rowStart[iRow] + rowLength[iRow]; j++) { int iColumn = column[j]; mark[iColumn] = iRow; } rowIsGub[iRow] = -1; putGub--; gubStart[putGub] = first; gubEnd[putGub] = last + 1; lower[putGub] = rowLower[iRow]; upper[putGub] = rowUpper[iRow]; whichGub[putGub] = iRow; } } int numberNonGub = numberRows - putNonGub; int numberGub = numberRows - putGub; if (numberGub > 0) { printf("** %d gub rows\n", numberGub); int numberNormal = 0; const int * row = matrix->getIndices(); const int * columnLength = matrix->getVectorLengths(); const CoinBigIndex * columnStart = matrix->getVectorStarts(); const double * elementByColumn = matrix->getElements(); int numberElements = 0; bool doLower = false; bool doUpper = false; for (iColumn = 0; iColumn < numberColumns; iColumn++) { if (mark[iColumn] < 0) { mark[numberNormal++] = iColumn; } else { numberElements += columnLength[iColumn]; if (columnLower[iColumn] != 0.0) doLower = true; if (columnUpper[iColumn] < 1.0e20) doUpper = true; } } if (!numberNormal) { printf("Putting back one gub row to make non-empty\n"); for (iColumn = gubStart[putGub]; iColumn < gubEnd[putGub]; iColumn++) mark[numberNormal++] = iColumn; putGub++; numberGub--; } ClpSimplex model2(&model, numberNonGub, which + putNonGub, numberNormal, mark); int numberGubColumns = numberColumns - numberNormal; // sort gubs so monotonic int * which = new int[numberGub]; int i; for (i = 0; i < numberGub; i++) which[i] = i; CoinSort_2(gubStart + putGub, gubStart + putGub + numberGub, which); int * temp1 = new int [numberGub]; for (i = 0; i < numberGub; i++) { int k = which[i]; temp1[i] = gubEnd[putGub+k]; } memcpy(gubEnd + putGub, temp1, numberGub * sizeof(int)); delete [] temp1; double * temp2 = new double [numberGub]; for (i = 0; i < numberGub; i++) { int k = which[i]; temp2[i] = lower[putGub+k]; } memcpy(lower + putGub, temp2, numberGub * sizeof(double)); for (i = 0; i < numberGub; i++) { int k = which[i]; temp2[i] = upper[putGub+k]; } memcpy(upper + putGub, temp2, numberGub * sizeof(double)); delete [] temp2; delete [] which; numberElements -= numberGubColumns; int * start2 = new int[numberGubColumns+1]; int * row2 = new int[numberElements]; double * element2 = new double[numberElements]; double * cost2 = new double [numberGubColumns]; double * lowerColumn2 = NULL; if (doLower) { lowerColumn2 = new double [numberGubColumns]; CoinFillN(lowerColumn2, numberGubColumns, 0.0); } double * upperColumn2 = NULL; if (doUpper) { upperColumn2 = new double [numberGubColumns]; CoinFillN(upperColumn2, numberGubColumns, COIN_DBL_MAX); } numberElements = 0; int numberNonGubRows = 0; for (iRow = 0; iRow < numberRows; iRow++) { if (!rowIsGub[iRow]) rowIsGub[iRow] = numberNonGubRows++; } numberColumns = 0; gubStart[0] = 0; start2[0] = 0; const double * cost = model.objective(); for (int iSet = 0; iSet < numberGub; iSet++) { int iStart = gubStart[iSet+putGub]; int iEnd = gubEnd[iSet+putGub]; for (int k = iStart; k < iEnd; k++) { cost2[numberColumns] = cost[k]; if (columnLower[k]) lowerColumn2[numberColumns] = columnLower[k]; if (columnUpper[k] < 1.0e20) upperColumn2[numberColumns] = columnUpper[k]; for (int j = columnStart[k]; j < columnStart[k] + columnLength[k]; j++) { int iRow = rowIsGub[row[j]]; if (iRow >= 0) { row2[numberElements] = iRow; element2[numberElements++] = elementByColumn[j]; } } start2[++numberColumns] = numberElements; } gubStart[iSet+1] = numberColumns; } model2.replaceMatrix(new ClpGubDynamicMatrix(&model2, numberGub, numberColumns, gubStart, lower + putGub, upper + putGub, start2, row2, element2, cost2, lowerColumn2, upperColumn2)); delete [] rowIsGub; delete [] start2; delete [] row2; delete [] element2; delete [] cost2; delete [] lowerColumn2; delete [] upperColumn2; // For now scaling off model2.scaling(0); // Do partial dantzig ClpPrimalColumnSteepest dantzig(5); model2.setPrimalColumnPivotAlgorithm(dantzig); //model2.messageHandler()->setLogLevel(63); model2.setFactorizationFrequency(maxFactor); model2.setMaximumIterations(4000000); double time1 = CoinCpuTime(); model2.primal(); { ClpGubDynamicMatrix * gubMatrix = dynamic_cast< ClpGubDynamicMatrix*>(model2.clpMatrix()); assert(gubMatrix); const double * solution = model2.primalColumnSolution(); int numberGubColumns = gubMatrix->numberGubColumns(); int firstOdd = gubMatrix->firstDynamic(); int lastOdd = gubMatrix->firstAvailable(); int numberTotalColumns = firstOdd + numberGubColumns; int numberRows = model2.numberRows(); char * status = new char [numberTotalColumns]; double * gubSolution = new double [numberTotalColumns]; int numberSets = gubMatrix->numberSets(); const int * id = gubMatrix->id(); int i; const double * lowerColumn = gubMatrix->lowerColumn(); const double * upperColumn = gubMatrix->upperColumn(); for (i = 0; i < numberGubColumns; i++) { if (gubMatrix->getDynamicStatus(i) == ClpGubDynamicMatrix::atUpperBound) { gubSolution[i+firstOdd] = upperColumn[i]; status[i+firstOdd] = 2; } else if (gubMatrix->getDynamicStatus(i) == ClpGubDynamicMatrix::atLowerBound && lowerColumn) { gubSolution[i+firstOdd] = lowerColumn[i]; status[i+firstOdd] = 1; } else { gubSolution[i+firstOdd] = 0.0; status[i+firstOdd] = 1; } } for (i = 0; i < firstOdd; i++) { ClpSimplex::Status thisStatus = model2.getStatus(i); if (thisStatus == ClpSimplex::basic) status[i] = 0; else if (thisStatus == ClpSimplex::atLowerBound) status[i] = 1; else if (thisStatus == ClpSimplex::atUpperBound) status[i] = 2; else if (thisStatus == ClpSimplex::isFixed) status[i] = 3; else abort(); gubSolution[i] = solution[i]; } for (i = firstOdd; i < lastOdd; i++) { int iBig = id[i-firstOdd] + firstOdd; ClpSimplex::Status thisStatus = model2.getStatus(i); if (thisStatus == ClpSimplex::basic) status[iBig] = 0; else if (thisStatus == ClpSimplex::atLowerBound) status[iBig] = 1; else if (thisStatus == ClpSimplex::atUpperBound) status[iBig] = 2; else if (thisStatus == ClpSimplex::isFixed) status[iBig] = 3; else abort(); gubSolution[iBig] = solution[i]; } char * rowStatus = new char[numberRows]; for (i = 0; i < numberRows; i++) { ClpSimplex::Status thisStatus = model2.getRowStatus(i); if (thisStatus == ClpSimplex::basic) rowStatus[i] = 0; else if (thisStatus == ClpSimplex::atLowerBound) rowStatus[i] = 1; else if (thisStatus == ClpSimplex::atUpperBound) rowStatus[i] = 2; else if (thisStatus == ClpSimplex::isFixed) rowStatus[i] = 3; else abort(); } char * setStatus = new char[numberSets]; int * keyVariable = new int[numberSets]; memcpy(keyVariable, gubMatrix->keyVariable(), numberSets * sizeof(int)); for (i = 0; i < numberSets; i++) { int iKey = keyVariable[i]; if (iKey > lastOdd) iKey = numberTotalColumns + i; else iKey = id[iKey-firstOdd] + firstOdd; keyVariable[i] = iKey; ClpSimplex::Status thisStatus = gubMatrix->getStatus(i); if (thisStatus == ClpSimplex::basic) setStatus[i] = 0; else if (thisStatus == ClpSimplex::atLowerBound) setStatus[i] = 1; else if (thisStatus == ClpSimplex::atUpperBound) setStatus[i] = 2; else if (thisStatus == ClpSimplex::isFixed) setStatus[i] = 3; else abort(); } FILE * fp = fopen("xx.sol", "w"); fwrite(gubSolution, sizeof(double), numberTotalColumns, fp); fwrite(status, sizeof(char), numberTotalColumns, fp); const double * rowsol = model2.primalRowSolution(); int originalNumberRows = model.numberRows(); double * rowsol2 = new double[originalNumberRows]; memset(rowsol2, 0, originalNumberRows * sizeof(double)); model.times(1.0, gubSolution, rowsol2); for (i = 0; i < numberRows; i++) assert(fabs(rowsol[i] - rowsol2[i]) < 1.0e-3); //for (;i<originalNumberRows;i++) //printf("%d %g\n",i,rowsol2[i]); delete [] rowsol2; fwrite(rowsol, sizeof(double), numberRows, fp); fwrite(rowStatus, sizeof(char), numberRows, fp); fwrite(setStatus, sizeof(char), numberSets, fp); fwrite(keyVariable, sizeof(int), numberSets, fp); fclose(fp); delete [] status; delete [] gubSolution; delete [] setStatus; delete [] keyVariable; // ** if going to rstart as dynamic need id_ // also copy coding in useEf.. from ClpGubMatrix (i.e. test for basis) } printf("obj offset is %g\n", model2.objectiveOffset()); printf("Primal took %g seconds\n", CoinCpuTime() - time1); //model2.primal(1); } delete [] mark; delete [] gubStart; delete [] gubEnd; delete [] which; delete [] whichGub; delete [] lower; delete [] upper; #else printf("testGub2 not available with COIN_BIG_INDEX=2\n"); #endif return 0; }
// inner part of dive int CbcHeuristicDive::solution(double & solutionValue, int & numberNodes, int & numberCuts, OsiRowCut ** cuts, CbcSubProblem ** & nodes, double * newSolution) { #ifdef DIVE_DEBUG int nRoundInfeasible = 0; int nRoundFeasible = 0; #endif int reasonToStop = 0; double time1 = CoinCpuTime(); int numberSimplexIterations = 0; int maxSimplexIterations = (model_->getNodeCount()) ? maxSimplexIterations_ : maxSimplexIterationsAtRoot_; // but can't be exactly coin_int_max maxSimplexIterations = CoinMin(maxSimplexIterations,COIN_INT_MAX>>3); OsiSolverInterface * solver = cloneBut(6); // was model_->solver()->clone(); # ifdef COIN_HAS_CLP OsiClpSolverInterface * clpSolver = dynamic_cast<OsiClpSolverInterface *> (solver); if (clpSolver) { ClpSimplex * clpSimplex = clpSolver->getModelPtr(); int oneSolveIts = clpSimplex->maximumIterations(); oneSolveIts = CoinMin(1000+2*(clpSimplex->numberRows()+clpSimplex->numberColumns()),oneSolveIts); clpSimplex->setMaximumIterations(oneSolveIts); if (!nodes) { // say give up easily clpSimplex->setMoreSpecialOptions(clpSimplex->moreSpecialOptions() | 64); } else { // get ray int specialOptions = clpSimplex->specialOptions(); specialOptions &= ~0x3100000; specialOptions |= 32; clpSimplex->setSpecialOptions(specialOptions); clpSolver->setSpecialOptions(clpSolver->specialOptions() | 1048576); if ((model_->moreSpecialOptions()&16777216)!=0) { // cutoff is constraint clpSolver->setDblParam(OsiDualObjectiveLimit, COIN_DBL_MAX); } } } # endif const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); const double * rowLower = solver->getRowLower(); const double * rowUpper = solver->getRowUpper(); const double * solution = solver->getColSolution(); const double * objective = solver->getObjCoefficients(); double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); double primalTolerance; solver->getDblParam(OsiPrimalTolerance, primalTolerance); int numberRows = matrix_.getNumRows(); assert (numberRows <= solver->getNumRows()); int numberIntegers = model_->numberIntegers(); const int * integerVariable = model_->integerVariable(); double direction = solver->getObjSense(); // 1 for min, -1 for max double newSolutionValue = direction * solver->getObjValue(); int returnCode = 0; // Column copy const double * element = matrix_.getElements(); const int * row = matrix_.getIndices(); const CoinBigIndex * columnStart = matrix_.getVectorStarts(); const int * columnLength = matrix_.getVectorLengths(); #ifdef DIVE_FIX_BINARY_VARIABLES // Row copy const double * elementByRow = matrixByRow_.getElements(); const int * column = matrixByRow_.getIndices(); const CoinBigIndex * rowStart = matrixByRow_.getVectorStarts(); const int * rowLength = matrixByRow_.getVectorLengths(); #endif // Get solution array for heuristic solution int numberColumns = solver->getNumCols(); memcpy(newSolution, solution, numberColumns*sizeof(double)); // vectors to store the latest variables fixed at their bounds int* columnFixed = new int [numberIntegers]; double* originalBound = new double [numberIntegers+2*numberColumns]; double * lowerBefore = originalBound+numberIntegers; double * upperBefore = lowerBefore+numberColumns; memcpy(lowerBefore,lower,numberColumns*sizeof(double)); memcpy(upperBefore,upper,numberColumns*sizeof(double)); double * lastDjs=newSolution+numberColumns; bool * fixedAtLowerBound = new bool [numberIntegers]; PseudoReducedCost * candidate = new PseudoReducedCost [numberIntegers]; double * random = new double [numberIntegers]; int maxNumberAtBoundToFix = static_cast<int> (floor(percentageToFix_ * numberIntegers)); assert (!maxNumberAtBoundToFix||!nodes); // count how many fractional variables int numberFractionalVariables = 0; for (int i = 0; i < numberIntegers; i++) { random[i] = randomNumberGenerator_.randomDouble() + 0.3; int iColumn = integerVariable[i]; double value = newSolution[iColumn]; if (fabs(floor(value + 0.5) - value) > integerTolerance) { numberFractionalVariables++; } } const double* reducedCost = NULL; // See if not NLP if (model_->solverCharacteristics()->reducedCostsAccurate()) reducedCost = solver->getReducedCost(); int iteration = 0; while (numberFractionalVariables) { iteration++; // initialize any data initializeData(); // select a fractional variable to bound int bestColumn = -1; int bestRound; // -1 rounds down, +1 rounds up bool canRound = selectVariableToBranch(solver, newSolution, bestColumn, bestRound); // if the solution is not trivially roundable, we don't try to round; // if the solution is trivially roundable, we try to round. However, // if the rounded solution is worse than the current incumbent, // then we don't round and proceed normally. In this case, the // bestColumn will be a trivially roundable variable if (canRound) { // check if by rounding all fractional variables // we get a solution with an objective value // better than the current best integer solution double delta = 0.0; for (int i = 0; i < numberIntegers; i++) { int iColumn = integerVariable[i]; double value = newSolution[iColumn]; if (fabs(floor(value + 0.5) - value) > integerTolerance) { assert(downLocks_[i] == 0 || upLocks_[i] == 0); double obj = objective[iColumn]; if (downLocks_[i] == 0 && upLocks_[i] == 0) { if (direction * obj >= 0.0) delta += (floor(value) - value) * obj; else delta += (ceil(value) - value) * obj; } else if (downLocks_[i] == 0) delta += (floor(value) - value) * obj; else delta += (ceil(value) - value) * obj; } } if (direction*(solver->getObjValue() + delta) < solutionValue) { #ifdef DIVE_DEBUG nRoundFeasible++; #endif if (!nodes||bestColumn<0) { // Round all the fractional variables for (int i = 0; i < numberIntegers; i++) { int iColumn = integerVariable[i]; double value = newSolution[iColumn]; if (fabs(floor(value + 0.5) - value) > integerTolerance) { assert(downLocks_[i] == 0 || upLocks_[i] == 0); if (downLocks_[i] == 0 && upLocks_[i] == 0) { if (direction * objective[iColumn] >= 0.0) newSolution[iColumn] = floor(value); else newSolution[iColumn] = ceil(value); } else if (downLocks_[i] == 0) newSolution[iColumn] = floor(value); else newSolution[iColumn] = ceil(value); } } break; } else { // can't round if going to use in branching int i; for (i = 0; i < numberIntegers; i++) { int iColumn = integerVariable[i]; double value = newSolution[bestColumn]; if (fabs(floor(value + 0.5) - value) > integerTolerance) { if (iColumn==bestColumn) { assert(downLocks_[i] == 0 || upLocks_[i] == 0); double obj = objective[bestColumn]; if (downLocks_[i] == 0 && upLocks_[i] == 0) { if (direction * obj >= 0.0) bestRound=-1; else bestRound=1; } else if (downLocks_[i] == 0) bestRound=-1; else bestRound=1; break; } } } } } #ifdef DIVE_DEBUG else nRoundInfeasible++; #endif } // do reduced cost fixing #ifdef DIVE_DEBUG int numberFixed = reducedCostFix(solver); std::cout << "numberReducedCostFixed = " << numberFixed << std::endl; #else reducedCostFix(solver); #endif int numberAtBoundFixed = 0; #ifdef DIVE_FIX_BINARY_VARIABLES // fix binary variables based on pseudo reduced cost if (binVarIndex_.size()) { int cnt = 0; int n = static_cast<int>(binVarIndex_.size()); for (int j = 0; j < n; j++) { int iColumn1 = binVarIndex_[j]; double value = newSolution[iColumn1]; if (fabs(value) <= integerTolerance && lower[iColumn1] != upper[iColumn1]) { double maxPseudoReducedCost = 0.0; #ifdef DIVE_DEBUG std::cout << "iColumn1 = " << iColumn1 << ", value = " << value << std::endl; #endif int iRow = vbRowIndex_[j]; double chosenValue = 0.0; for (int k = rowStart[iRow]; k < rowStart[iRow] + rowLength[iRow]; k++) { int iColumn2 = column[k]; #ifdef DIVE_DEBUG std::cout << "iColumn2 = " << iColumn2 << std::endl; #endif if (iColumn1 != iColumn2) { double pseudoReducedCost = fabs(reducedCost[iColumn2] * elementByRow[k]); #ifdef DIVE_DEBUG int k2; for (k2 = rowStart[iRow]; k2 < rowStart[iRow] + rowLength[iRow]; k2++) { if (column[k2] == iColumn1) break; } std::cout << "reducedCost[" << iColumn2 << "] = " << reducedCost[iColumn2] << ", elementByRow[" << iColumn2 << "] = " << elementByRow[k] << ", elementByRow[" << iColumn1 << "] = " << elementByRow[k2] << ", pseudoRedCost = " << pseudoReducedCost << std::endl; #endif if (pseudoReducedCost > maxPseudoReducedCost) maxPseudoReducedCost = pseudoReducedCost; } else { // save value chosenValue = fabs(elementByRow[k]); } } assert (chosenValue); maxPseudoReducedCost /= chosenValue; #ifdef DIVE_DEBUG std::cout << ", maxPseudoRedCost = " << maxPseudoReducedCost << std::endl; #endif candidate[cnt].var = iColumn1; candidate[cnt++].pseudoRedCost = maxPseudoReducedCost; } } #ifdef DIVE_DEBUG std::cout << "candidates for rounding = " << cnt << std::endl; #endif std::sort(candidate, candidate + cnt, compareBinaryVars); for (int i = 0; i < cnt; i++) { int iColumn = candidate[i].var; if (numberAtBoundFixed < maxNumberAtBoundToFix) { columnFixed[numberAtBoundFixed] = iColumn; originalBound[numberAtBoundFixed] = upper[iColumn]; fixedAtLowerBound[numberAtBoundFixed] = true; solver->setColUpper(iColumn, lower[iColumn]); numberAtBoundFixed++; if (numberAtBoundFixed == maxNumberAtBoundToFix) break; } } } #endif // fix other integer variables that are at their bounds int cnt = 0; #ifdef GAP double gap = 1.0e30; #endif if (reducedCost && true) { #ifndef JJF_ONE cnt = fixOtherVariables(solver, solution, candidate, random); #else #ifdef GAP double cutoff = model_->getCutoff() ; if (cutoff < 1.0e20 && false) { double direction = solver->getObjSense() ; gap = cutoff - solver->getObjValue() * direction ; gap *= 0.1; // Fix more if plausible double tolerance; solver->getDblParam(OsiDualTolerance, tolerance) ; if (gap <= 0.0) gap = tolerance; gap += 100.0 * tolerance; } int nOverGap = 0; #endif int numberFree = 0; int numberFixed = 0; for (int i = 0; i < numberIntegers; i++) { int iColumn = integerVariable[i]; if (upper[iColumn] > lower[iColumn]) { numberFree++; double value = newSolution[iColumn]; if (fabs(floor(value + 0.5) - value) <= integerTolerance) { candidate[cnt].var = iColumn; candidate[cnt++].pseudoRedCost = fabs(reducedCost[iColumn] * random[i]); #ifdef GAP if (fabs(reducedCost[iColumn]) > gap) nOverGap++; #endif } } else { numberFixed++; } } #ifdef GAP int nLeft = maxNumberAtBoundToFix - numberAtBoundFixed; #ifdef CLP_INVESTIGATE4 printf("cutoff %g obj %g nover %d - %d free, %d fixed\n", cutoff, solver->getObjValue(), nOverGap, numberFree, numberFixed); #endif if (nOverGap > nLeft && true) { nOverGap = CoinMin(nOverGap, nLeft + maxNumberAtBoundToFix / 2); maxNumberAtBoundToFix += nOverGap - nLeft; } #else #ifdef CLP_INVESTIGATE4 printf("cutoff %g obj %g - %d free, %d fixed\n", model_->getCutoff(), solver->getObjValue(), numberFree, numberFixed); #endif #endif #endif } else { for (int i = 0; i < numberIntegers; i++) { int iColumn = integerVariable[i]; if (upper[iColumn] > lower[iColumn]) { double value = newSolution[iColumn]; if (fabs(floor(value + 0.5) - value) <= integerTolerance) { candidate[cnt].var = iColumn; candidate[cnt++].pseudoRedCost = numberIntegers - i; } } } } std::sort(candidate, candidate + cnt, compareBinaryVars); for (int i = 0; i < cnt; i++) { int iColumn = candidate[i].var; if (upper[iColumn] > lower[iColumn]) { double value = newSolution[iColumn]; if (fabs(floor(value + 0.5) - value) <= integerTolerance && numberAtBoundFixed < maxNumberAtBoundToFix) { // fix the variable at one of its bounds if (fabs(lower[iColumn] - value) <= integerTolerance) { columnFixed[numberAtBoundFixed] = iColumn; originalBound[numberAtBoundFixed] = upper[iColumn]; fixedAtLowerBound[numberAtBoundFixed] = true; solver->setColUpper(iColumn, lower[iColumn]); numberAtBoundFixed++; } else if (fabs(upper[iColumn] - value) <= integerTolerance) { columnFixed[numberAtBoundFixed] = iColumn; originalBound[numberAtBoundFixed] = lower[iColumn]; fixedAtLowerBound[numberAtBoundFixed] = false; solver->setColLower(iColumn, upper[iColumn]); numberAtBoundFixed++; } if (numberAtBoundFixed == maxNumberAtBoundToFix) break; } } } #ifdef DIVE_DEBUG std::cout << "numberAtBoundFixed = " << numberAtBoundFixed << std::endl; #endif double originalBoundBestColumn; double bestColumnValue; int whichWay; if (bestColumn >= 0) { bestColumnValue = newSolution[bestColumn]; if (bestRound < 0) { originalBoundBestColumn = upper[bestColumn]; solver->setColUpper(bestColumn, floor(bestColumnValue)); whichWay=0; } else { originalBoundBestColumn = lower[bestColumn]; solver->setColLower(bestColumn, ceil(bestColumnValue)); whichWay=1; } } else { break; } int originalBestRound = bestRound; int saveModelOptions = model_->specialOptions(); while (1) { model_->setSpecialOptions(saveModelOptions | 2048); solver->resolve(); model_->setSpecialOptions(saveModelOptions); if (!solver->isAbandoned()&&!solver->isIterationLimitReached()) { numberSimplexIterations += solver->getIterationCount(); } else { numberSimplexIterations = maxSimplexIterations + 1; reasonToStop += 100; break; } if (!solver->isProvenOptimal()) { if (nodes) { if (solver->isProvenPrimalInfeasible()) { if (maxSimplexIterationsAtRoot_!=COIN_INT_MAX) { // stop now printf("stopping on first infeasibility\n"); break; } else if (cuts) { // can do conflict cut printf("could do intermediate conflict cut\n"); bool localCut; OsiRowCut * cut = model_->conflictCut(solver,localCut); if (cut) { if (!localCut) { model_->makePartialCut(cut,solver); cuts[numberCuts++]=cut; } else { delete cut; } } } } else { reasonToStop += 10; break; } } if (numberAtBoundFixed > 0) { // Remove the bound fix for variables that were at bounds for (int i = 0; i < numberAtBoundFixed; i++) { int iColFixed = columnFixed[i]; if (fixedAtLowerBound[i]) solver->setColUpper(iColFixed, originalBound[i]); else solver->setColLower(iColFixed, originalBound[i]); } numberAtBoundFixed = 0; } else if (bestRound == originalBestRound) { bestRound *= (-1); whichWay |=2; if (bestRound < 0) { solver->setColLower(bestColumn, originalBoundBestColumn); solver->setColUpper(bestColumn, floor(bestColumnValue)); } else { solver->setColLower(bestColumn, ceil(bestColumnValue)); solver->setColUpper(bestColumn, originalBoundBestColumn); } } else break; } else break; } if (!solver->isProvenOptimal() || direction*solver->getObjValue() >= solutionValue) { reasonToStop += 1; } else if (iteration > maxIterations_) { reasonToStop += 2; } else if (CoinCpuTime() - time1 > maxTime_) { reasonToStop += 3; } else if (numberSimplexIterations > maxSimplexIterations) { reasonToStop += 4; // also switch off #ifdef CLP_INVESTIGATE printf("switching off diving as too many iterations %d, %d allowed\n", numberSimplexIterations, maxSimplexIterations); #endif when_ = 0; } else if (solver->getIterationCount() > 1000 && iteration > 3 && !nodes) { reasonToStop += 5; // also switch off #ifdef CLP_INVESTIGATE printf("switching off diving one iteration took %d iterations (total %d)\n", solver->getIterationCount(), numberSimplexIterations); #endif when_ = 0; } memcpy(newSolution, solution, numberColumns*sizeof(double)); numberFractionalVariables = 0; double sumFractionalVariables=0.0; for (int i = 0; i < numberIntegers; i++) { int iColumn = integerVariable[i]; double value = newSolution[iColumn]; double away = fabs(floor(value + 0.5) - value); if (away > integerTolerance) { numberFractionalVariables++; sumFractionalVariables += away; } } if (nodes) { // save information //branchValues[numberNodes]=bestColumnValue; //statuses[numberNodes]=whichWay+(bestColumn<<2); //bases[numberNodes]=solver->getWarmStart(); ClpSimplex * simplex = clpSolver->getModelPtr(); CbcSubProblem * sub = new CbcSubProblem(clpSolver,lowerBefore,upperBefore, simplex->statusArray(),numberNodes); nodes[numberNodes]=sub; // other stuff sub->branchValue_=bestColumnValue; sub->problemStatus_=whichWay; sub->branchVariable_=bestColumn; sub->objectiveValue_ = simplex->objectiveValue(); sub->sumInfeasibilities_ = sumFractionalVariables; sub->numberInfeasibilities_ = numberFractionalVariables; printf("DiveNode %d column %d way %d bvalue %g obj %g\n", numberNodes,sub->branchVariable_,sub->problemStatus_, sub->branchValue_,sub->objectiveValue_); numberNodes++; if (solver->isProvenOptimal()) { memcpy(lastDjs,solver->getReducedCost(),numberColumns*sizeof(double)); memcpy(lowerBefore,lower,numberColumns*sizeof(double)); memcpy(upperBefore,upper,numberColumns*sizeof(double)); } } if (!numberFractionalVariables||reasonToStop) break; } if (nodes) { printf("Exiting dive for reason %d\n",reasonToStop); if (reasonToStop>1) { printf("problems in diving\n"); int whichWay=nodes[numberNodes-1]->problemStatus_; CbcSubProblem * sub; if ((whichWay&2)==0) { // leave both ways sub = new CbcSubProblem(*nodes[numberNodes-1]); nodes[numberNodes++]=sub; } else { sub = nodes[numberNodes-1]; } if ((whichWay&1)==0) sub->problemStatus_=whichWay|1; else sub->problemStatus_=whichWay&~1; } if (!numberNodes) { // was good at start! - create fake clpSolver->resolve(); ClpSimplex * simplex = clpSolver->getModelPtr(); CbcSubProblem * sub = new CbcSubProblem(clpSolver,lowerBefore,upperBefore, simplex->statusArray(),numberNodes); nodes[numberNodes]=sub; // other stuff sub->branchValue_=0.0; sub->problemStatus_=0; sub->branchVariable_=-1; sub->objectiveValue_ = simplex->objectiveValue(); sub->sumInfeasibilities_ = 0.0; sub->numberInfeasibilities_ = 0; printf("DiveNode %d column %d way %d bvalue %g obj %g\n", numberNodes,sub->branchVariable_,sub->problemStatus_, sub->branchValue_,sub->objectiveValue_); numberNodes++; assert (solver->isProvenOptimal()); } nodes[numberNodes-1]->problemStatus_ |= 256*reasonToStop; // use djs as well if (solver->isProvenPrimalInfeasible()&&cuts) { // can do conflict cut and re-order printf("could do final conflict cut\n"); bool localCut; OsiRowCut * cut = model_->conflictCut(solver,localCut); if (cut) { printf("cut - need to use conflict and previous djs\n"); if (!localCut) { model_->makePartialCut(cut,solver); cuts[numberCuts++]=cut; } else { delete cut; } } else { printf("bad conflict - just use previous djs\n"); } } } // re-compute new solution value double objOffset = 0.0; solver->getDblParam(OsiObjOffset, objOffset); newSolutionValue = -objOffset; for (int i = 0 ; i < numberColumns ; i++ ) newSolutionValue += objective[i] * newSolution[i]; newSolutionValue *= direction; //printf("new solution value %g %g\n",newSolutionValue,solutionValue); if (newSolutionValue < solutionValue && !reasonToStop) { double * rowActivity = new double[numberRows]; memset(rowActivity, 0, numberRows*sizeof(double)); // paranoid check memset(rowActivity, 0, numberRows*sizeof(double)); for (int i = 0; i < numberColumns; i++) { int j; double value = newSolution[i]; if (value) { for (j = columnStart[i]; j < columnStart[i] + columnLength[i]; j++) { int iRow = row[j]; rowActivity[iRow] += value * element[j]; } } } // check was approximately feasible bool feasible = true; for (int i = 0; i < numberRows; i++) { if (rowActivity[i] < rowLower[i]) { if (rowActivity[i] < rowLower[i] - 1000.0*primalTolerance) feasible = false; } else if (rowActivity[i] > rowUpper[i]) { if (rowActivity[i] > rowUpper[i] + 1000.0*primalTolerance) feasible = false; } } for (int i = 0; i < numberIntegers; i++) { int iColumn = integerVariable[i]; double value = newSolution[iColumn]; if (fabs(floor(value + 0.5) - value) > integerTolerance) { feasible = false; break; } } if (feasible) { // new solution solutionValue = newSolutionValue; //printf("** Solution of %g found by CbcHeuristicDive\n",newSolutionValue); //if (cuts) //clpSolver->getModelPtr()->writeMps("good8.mps", 2); returnCode = 1; } else { // Can easily happen //printf("Debug CbcHeuristicDive giving bad solution\n"); } delete [] rowActivity; } #ifdef DIVE_DEBUG std::cout << "nRoundInfeasible = " << nRoundInfeasible << ", nRoundFeasible = " << nRoundFeasible << ", returnCode = " << returnCode << ", reasonToStop = " << reasonToStop << ", simplexIts = " << numberSimplexIterations << ", iterations = " << iteration << std::endl; #endif delete [] columnFixed; delete [] originalBound; delete [] fixedAtLowerBound; delete [] candidate; delete [] random; delete [] downArray_; downArray_ = NULL; delete [] upArray_; upArray_ = NULL; delete solver; return returnCode; }
// Fix other variables at bounds int CbcHeuristicDive::fixOtherVariables(OsiSolverInterface * solver, const double * solution, PseudoReducedCost * candidate, const double * random) { const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); double primalTolerance; solver->getDblParam(OsiPrimalTolerance, primalTolerance); int numberIntegers = model_->numberIntegers(); const int * integerVariable = model_->integerVariable(); const double* reducedCost = solver->getReducedCost(); // fix other integer variables that are at their bounds int cnt = 0; #ifdef GAP double direction = solver->getObjSense(); // 1 for min, -1 for max double gap = 1.0e30; #endif #ifdef GAP double cutoff = model_->getCutoff() ; if (cutoff < 1.0e20 && false) { double direction = solver->getObjSense() ; gap = cutoff - solver->getObjValue() * direction ; gap *= 0.1; // Fix more if plausible double tolerance; solver->getDblParam(OsiDualTolerance, tolerance) ; if (gap <= 0.0) gap = tolerance; gap += 100.0 * tolerance; } int nOverGap = 0; #endif int numberFree = 0; int numberFixedAlready = 0; for (int i = 0; i < numberIntegers; i++) { int iColumn = integerVariable[i]; if (upper[iColumn] > lower[iColumn]) { numberFree++; double value = solution[iColumn]; if (fabs(floor(value + 0.5) - value) <= integerTolerance) { candidate[cnt].var = iColumn; candidate[cnt++].pseudoRedCost = fabs(reducedCost[iColumn] * random[i]); #ifdef GAP if (fabs(reducedCost[iColumn]) > gap) nOverGap++; #endif } } else { numberFixedAlready++; } } #ifdef GAP int nLeft = maxNumberToFix - numberFixedAlready; #ifdef CLP_INVESTIGATE4 printf("cutoff %g obj %g nover %d - %d free, %d fixed\n", cutoff, solver->getObjValue(), nOverGap, numberFree, numberFixedAlready); #endif if (nOverGap > nLeft && true) { nOverGap = CoinMin(nOverGap, nLeft + maxNumberToFix / 2); maxNumberToFix += nOverGap - nLeft; } #else #ifdef CLP_INVESTIGATE4 printf("cutoff %g obj %g - %d free, %d fixed\n", model_->getCutoff(), solver->getObjValue(), numberFree, numberFixedAlready); #endif #endif return cnt; }