Skip to content

Commit 6f6333c

Browse files
committed
Moved isKnapsack to HighsMipSolverData::mipIsKnapsack
1 parent b2e3f1d commit 6f6333c

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

highs/lp_data/HighsLp.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,6 @@ bool HighsLp::isBip() const {
3838
return true;
3939
}
4040

41-
bool HighsLp::isKnapsack(HighsInt& capacity) const {
42-
// Has to kave one constraint
43-
if (this->num_row_ != 1) return false;
44-
// Has to be a binary integer programming problem
45-
if (!this->isBip()) return false;
46-
// Must be one-sided constraint
47-
if (this->row_lower_[0] > -kHighsInf && this->row_upper_[0] < kHighsInf)
48-
return false;
49-
const bool upper = this->row_upper_[0] < kHighsInf;
50-
const HighsInt constraint_sign = upper ? 1 : -1;
51-
// Now check that all the (signed) coefficients are integer and non-negative
52-
for (HighsInt iEl = 0; iEl < this->a_matrix_.numNz(); iEl++) {
53-
double coeff = constraint_sign * this->a_matrix_.value_[iEl];
54-
if (coeff < 0) return false;
55-
if (fractionality(coeff) > 0) return false;
56-
}
57-
// Capacity must be integer, but OK to round down any fractional
58-
// values since activity of constraint is integer
59-
double double_capacity =
60-
upper ? this->row_upper_[0] : constraint_sign * this->row_lower_[0];
61-
const double capacity_margin = 1e-6;
62-
capacity = std::floor(double_capacity + capacity_margin);
63-
// Problem is knapsack!
64-
return true;
65-
}
66-
6741
bool HighsLp::hasInfiniteCost(const double infinite_cost) const {
6842
for (HighsInt iCol = 0; iCol < this->num_col_; iCol++) {
6943
if (this->col_cost_[iCol] >= infinite_cost) return true;

highs/lp_data/HighsLp.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class HighsLp {
6666
bool equalScaling(const HighsLp& lp) const;
6767
bool isMip() const;
6868
bool isBip() const;
69-
bool isKnapsack(HighsInt& capacity) const;
7069
bool hasSemiVariables() const;
7170
bool hasInfiniteCost(const double infinite_cost) const;
7271
bool hasMods() const;

highs/mip/HighsMipSolver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void HighsMipSolver::run() {
9191
// Determine whether this is a knapsack problem and, if so, at least
9292
// update the data on knapsack sub-MIPs
9393
HighsInt capacity = 0;
94-
if (orig_model_->isKnapsack(capacity)) {
94+
if (mipdata_->mipIsKnapsack(capacity)) {
9595
mipdata_->knapsack_data_.num_problem++;
9696
mipdata_->knapsack_data_.sum_variables += orig_model_->num_col_;
9797
mipdata_->knapsack_data_.sum_capacity += capacity;

highs/mip/HighsMipSolverData.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,6 +2651,33 @@ void HighsMipSolverData::callbackUserSolution(
26512651
}
26522652
}
26532653

2654+
bool HighsMipSolverData::mipIsKnapsack(HighsInt& capacity) {
2655+
const HighsLp& lp = *(mipsolver.model_);
2656+
// Has to have one constraint
2657+
if (lp.num_row_ != 1) return false;
2658+
// Has to be a binary integer programming problem
2659+
if (!lp.isBip()) return false;
2660+
// Must be one-sided constraint
2661+
if (lp.row_lower_[0] > -kHighsInf && lp.row_upper_[0] < kHighsInf)
2662+
return false;
2663+
const bool upper = lp.row_upper_[0] < kHighsInf;
2664+
const HighsInt constraint_sign = upper ? 1 : -1;
2665+
// Now check that all the (signed) coefficients are integer and non-negative
2666+
for (HighsInt iEl = 0; iEl < lp.a_matrix_.numNz(); iEl++) {
2667+
double coeff = constraint_sign * lp.a_matrix_.value_[iEl];
2668+
if (coeff < 0) return false;
2669+
if (fractionality(coeff) > 0) return false;
2670+
}
2671+
// Capacity must be integer, but OK to round down any fractional
2672+
// values since activity of constraint is integer
2673+
double double_capacity =
2674+
upper ? lp.row_upper_[0] : constraint_sign * lp.row_lower_[0];
2675+
const double capacity_margin = 1e-6;
2676+
capacity = std::floor(double_capacity + capacity_margin);
2677+
// Problem is knapsack!
2678+
return true;
2679+
}
2680+
26542681
static double possInfRelDiff(const double v0, const double v1,
26552682
const double den) {
26562683
double rel_diff;

highs/mip/HighsMipSolverData.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ struct HighsMipSolverData {
309309
void callbackUserSolution(
310310
const double mipsolver_objective_value,
311311
const userMipSolutionCallbackOrigin user_solution_callback_origin);
312+
bool mipIsKnapsack(HighsInt& capacity);
312313
};
313314

314315
#endif

highs/mip/HighsPrimalHeuristics.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,11 +1787,10 @@ HighsStatus HighsPrimalHeuristics::solveMipKnapsackReturn(
17871787
}
17881788

17891789
HighsStatus HighsPrimalHeuristics::solveMipKnapsack() {
1790-
HighsLp lp = *(mipsolver.model_);
1791-
// const HighsLp& lp = mipsolver.mipdata_->model_;
1790+
const HighsLp& lp = *(mipsolver.model_);
17921791
const HighsLogOptions& log_options = mipsolver.options_mip_->log_options;
17931792
HighsInt capacity_;
1794-
assert(lp.isKnapsack(capacity_));
1793+
assert(mipsolver.mipdata_->mipIsKnapsack(capacity_));
17951794

17961795
const bool upper = lp.row_upper_[0] < kHighsInf;
17971796
const HighsInt constraint_sign = upper ? 1 : -1;

0 commit comments

Comments
 (0)