Skip to content

Commit 1b5ef6a

Browse files
committed
Now demanding integer knapsack weights, but rounding down any fractional capacity
1 parent da76f03 commit 1b5ef6a

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

highs/lp_data/HighsLp.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@ bool HighsLp::isKnapsack(HighsInt& capacity) const {
4646
if (this->row_lower_[0] > -kHighsInf && this->row_upper_[0] < kHighsInf) return false;
4747
const bool upper = this->row_upper_[0] < kHighsInf;
4848
const HighsInt constraint_sign = upper ? 1 : -1;
49-
// Now check that all the (signed) coefficients are non-negative
50-
for (HighsInt iEl = 0; iEl < this->a_matrix_.numNz(); iEl++)
51-
if (constraint_sign * this->a_matrix_.value_[iEl] < 0) return false;
49+
// Now check that all the (signed) coefficients are integer and non-negative
50+
for (HighsInt iEl = 0; iEl < this->a_matrix_.numNz(); iEl++) {
51+
double coeff = constraint_sign * this->a_matrix_.value_[iEl];
52+
if (coeff < 0) return false;
53+
if (fractionality(coeff) > 0) return false;
54+
}
55+
// Capacity must be integer, but OK to round down any fractional
56+
// values since activity of constraint is integer
57+
double double_capacity = upper ? this->row_upper_[0] : constraint_sign*this->row_lower_[0];
58+
const double capacity_margin = 1e-6;
59+
capacity = std::floor(double_capacity+capacity_margin);
5260
// Problem is knapsack!
53-
//
54-
// Get the capacity: if it is negative, then the problem is infeasible
55-
capacity = upper ? this->row_upper_[0] : constraint_sign*this->row_lower_[0];
56-
assert(capacity >= 0);
5761
return true;
5862
}
5963

highs/mip/HighsPrimalHeuristics.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1743,10 +1743,13 @@ HighsStatus HighsPrimalHeuristics::solveMipKnapsack() {
17431743
const HighsLogOptions& log_options = mipsolver.options_mip_->log_options;
17441744
HighsInt capacity_;
17451745
assert(lp.isKnapsack(capacity_));
1746-
const HighsInt capacity = capacity_;
17471746

17481747
const bool upper = lp.row_upper_[0] < kHighsInf;
17491748
const HighsInt constraint_sign = upper ? 1 : -1;
1749+
double double_capacity = upper ? lp.row_upper_[0] : constraint_sign * lp.row_lower_[0];
1750+
const double capacity_margin = 1e-6;
1751+
const HighsInt capacity = std::floor(double_capacity+capacity_margin);
1752+
17501753
if (capacity < 0) {
17511754
mipsolver.modelstatus_ = HighsModelStatus::kInfeasible;
17521755
return solveMipKnapsackReturn(HighsStatus::kOk);

0 commit comments

Comments
 (0)