Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Scipy exceptions #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Highs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "lp_data/HighsRanging.h"
#include "lp_data/HighsSolutionDebug.h"
#include "model/HighsModel.h"
#include "util/HighsExceptions.h"
#include "presolve/ICrash.h"
#include "presolve/PresolveComponent.h"

Expand Down
8 changes: 7 additions & 1 deletion src/presolve/HPresolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <limits>

#include "Highs.h"
#include "HighsExceptions.h"
#include "io/HighsIO.h"
#include "lp_data/HConst.h"
#include "lp_data/HStruct.h"
Expand Down Expand Up @@ -3993,7 +3994,12 @@ HPresolve::Result HPresolve::presolve(HighsPostsolveStack& postsolve_stack) {
}
};

HPRESOLVE_CHECKED_CALL(initialRowAndColPresolve(postsolve_stack));
try {
HPRESOLVE_CHECKED_CALL(initialRowAndColPresolve(postsolve_stack));
} catch (const DataStackOverflow& e) {
// Here we re-throw the error
throw PresolveTooLarge(e.what());
}

HighsInt numParallelRowColCalls = 0;
#if ENABLE_SPARSIFY_FOR_LP
Expand Down
1 change: 1 addition & 0 deletions src/presolve/HPresolve.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "util/HighsHash.h"
#include "util/HighsLinearSumBounds.h"
#include "util/HighsMatrixSlice.h"
#include "util/HighsExceptions.h"

namespace presolve {

Expand Down
27 changes: 17 additions & 10 deletions 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,17 +368,23 @@ class HighsPostsolveStack {
template <typename ColStorageFormat>
void fixedColAtLower(HighsInt col, double fixValue, double colCost,
const HighsMatrixSlice<ColStorageFormat>& colVec) {
assert(std::isfinite(fixValue));
colValues.clear();
for (const HighsSliceNonzero& colVal : colVec)
colValues.emplace_back(origRowIndex[colVal.index()], colVal.value());

reductionValues.push(FixedCol{fixValue, colCost, origColIndex[col],
HighsBasisStatus::kLower});
reductionValues.push(colValues);
reductionAdded(ReductionType::kFixedCol);
try {
assert(std::isfinite(fixValue));
colValues.clear();
for (const HighsSliceNonzero& colVal : colVec)
colValues.emplace_back(origRowIndex[colVal.index()], colVal.value());

reductionValues.push(FixedCol{fixValue, colCost, origColIndex[col],
HighsBasisStatus::kLower});
reductionValues.push(colValues);
reductionAdded(ReductionType::kFixedCol);
} catch (const DataStackOverflow& e) {
std::cerr << "Memory allocation failed while processing fixedColAtLower: "
<< std::endl;
// Rethrow.
throw;
}
}

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 @@ -18,9 +18,11 @@
#define UTIL_HIGHS_DATA_STACK_H_

#include <cstring>
#include <string>
#include <type_traits>
#include <vector>

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

#if __GNUG__ && __GNUC__ < 5
Expand All @@ -39,11 +41,19 @@ class HighsDataStack {
template <typename T,
typename std::enable_if<IS_TRIVIALLY_COPYABLE(T), int>::type = 0>
void push(const T& r) {
std::size_t dataSize = data.size();
data.resize(dataSize + sizeof(T));
HighsInt dataSize = data.size();
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)) + "for "+
". 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
21 changes: 21 additions & 0 deletions src/util/HighsExceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <stdexcept>

class DataStackOverflow : public std::runtime_error {
public:
explicit DataStackOverflow(const std::string& msg)
: std::runtime_error(msg) {}
};

class ProblemTooLargeException : public std::runtime_error {
public:
explicit ProblemTooLargeException(const std::string& msg)
: std::runtime_error(msg) {}
};


class PresolveTooLarge : public std::runtime_error {
public:
explicit PresolveTooLarge(const std::string& msg)
: std::runtime_error(msg) {}
};