Skip to content

Commit

Permalink
MAINT: Use exceptions to propagate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoZeke committed Jul 30, 2023
1 parent 49a27c5 commit dc45be1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
21 changes: 16 additions & 5 deletions src/presolve/HPresolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3817,11 +3817,22 @@ HPresolve::Result HPresolve::initialRowAndColPresolve(
// arrays are not initialized, also unset changedRowFlag so that the row will
// be added to the changed row vector when it is changed after it was
// processed
for (HighsInt row = 0; row != model->num_row_; ++row) {
if (rowDeleted[row]) continue;
HPRESOLVE_CHECKED_CALL(rowPresolve(postsolve_stack, row));
changedRowFlag[row] = false;
}
try {
for (HighsInt row = 0; row != model->num_row_; ++row) {
if (rowDeleted[row]) continue;
HPRESOLVE_CHECKED_CALL(rowPresolve(postsolve_stack, row));
changedRowFlag[row] = false;
}
} catch(const ProblemTooLargeException& e) {
std::cerr << "The problem is too large to be presolved with the current resources.\n";
std::cerr << "A problem occurred during execution: " << e.what() << std::endl;
std::cerr << "Model details: \n";
std::cerr << "Number of rows: " << model->num_row_ << "\n";
// Add additional model details as needed
// Once the necessary information is printed, you could decide whether to terminate or propagate the error further.
// Here we re-throw the error to allow higher level code to decide on next steps
throw;
}

// same for the columns
for (HighsInt col = 0; col != model->num_col_; ++col) {
Expand Down
1 change: 1 addition & 0 deletions src/presolve/HPresolve.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "util/HighsHash.h"
#include "util/HighsLinearSumBounds.h"
#include "util/HighsMatrixSlice.h"
#include "util/HighsExceptions.h"

namespace presolve {

Expand Down
8 changes: 7 additions & 1 deletion src/presolve/HighsPostsolveStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <tuple>
#include <vector>

#include "HighsExceptions.h"
#include "lp_data/HConst.h"
#include "lp_data/HStruct.h"
#include "lp_data/HighsOptions.h"
Expand Down Expand Up @@ -367,6 +368,7 @@ class HighsPostsolveStack {
template <typename ColStorageFormat>
void fixedColAtLower(HighsInt col, double fixValue, double colCost,
const HighsMatrixSlice<ColStorageFormat>& colVec) {
try {
assert(std::isfinite(fixValue));
colValues.clear();
for (const HighsSliceNonzero& colVal : colVec)
Expand All @@ -376,8 +378,12 @@ class HighsPostsolveStack {
HighsBasisStatus::kLower});
reductionValues.push(colValues);
reductionAdded(ReductionType::kFixedCol);
} catch(const DataStackOverflow& e) {
std::cerr << "Memory allocation failed while processing fixedColAtLower: " << e.what() << std::endl;
// Throw another exception.
throw PresolveTooLarge("The problem cannot be pre-solved, either lower the size or try without presolve");
}

}
template <typename ColStorageFormat>
void fixedColAtUpper(HighsInt col, double fixValue, double colCost,
const HighsMatrixSlice<ColStorageFormat>& colVec) {
Expand Down
16 changes: 13 additions & 3 deletions src/util/HighsDataStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
#include <cstring>
#include <type_traits>
#include <vector>
#include <string>

#include "util/HighsInt.h"
#include "util/HighsExceptions.h"

#if __GNUG__ && __GNUC__ < 5
#define IS_TRIVIALLY_COPYABLE(T) __has_trivial_copy(T)
Expand All @@ -40,10 +42,18 @@ class HighsDataStack {
typename std::enable_if<IS_TRIVIALLY_COPYABLE(T), int>::type = 0>
void push(const T& r) {
HighsInt dataSize = data.size();
data.resize(dataSize + sizeof(T));
std::memcpy(data.data() + dataSize, &r, sizeof(T));
HighsInt newSize = dataSize + sizeof(T);
try {
data.resize(newSize);
} catch (const std::length_error& e) {
throw DataStackOverflow(
"Failed to resize the vector. Requested new size: " +
std::to_string(newSize) +
". Size to add is "+std::to_string(sizeof(T))+"."+
". Current size: " + std::to_string(data.size()) + ".");
}

std::memcpy(data.data() + dataSize, &r, sizeof(T));
}
template <typename T,
typename std::enable_if<IS_TRIVIALLY_COPYABLE(T), int>::type = 0>
void pop(T& r) {
Expand Down

0 comments on commit dc45be1

Please sign in to comment.