Skip to content

Commit

Permalink
fix: use assertions rather than exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mimizh2418 committed Nov 1, 2024
1 parent 541877d commit 3235d5f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
8 changes: 2 additions & 6 deletions src/LinearProblem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ LinearProblem::LinearProblem(const Ref<const VectorXd>& objective_coeffs, const
: is_minimization(is_minimization),
objective_coeffs((is_minimization ? -1 : 1) * objective_coeffs),
num_decision_vars(objective_coeffs.size()) {
if (objective_coeffs.size() < 1) {
throw std::invalid_argument("Objective function must have at least one coefficient");
}
Expects(objective_coeffs.size() > 0);
}

LinearProblem LinearProblem::maximizationProblem(const Ref<const VectorXd>& objective_coeffs) {
Expand All @@ -45,9 +43,7 @@ void LinearProblem::addEqualityConstraint(const Ref<const VectorXd>& constraint_
}

void LinearProblem::addConstraintImpl(const Ref<const VectorXd>& constraint_coeffs, const double rhs, const int constraint_type) {
if (constraint_coeffs.size() != num_decision_vars) {
throw std::invalid_argument("Constraint coefficients must have same dimension as decision variables");
}
Expects(constraint_coeffs.size() == num_decision_vars);

VectorXd new_constraint(constraint_coeffs.size() + 1);
new_constraint.head(num_decision_vars) = constraint_coeffs;
Expand Down
2 changes: 2 additions & 0 deletions src/solvers/linear/simplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ SolverExitStatus solveTableau(MatrixXd& tableau, VectorX<Index>& basic_vars, Sol

SolverExitStatus suboptimal::solveSimplex(const LinearProblem& problem, Ref<VectorXd> solution, double& objective_value,
const SimplexSolverConfig& config) {
Expects(solution.size() == problem.numDecisionVars());

MatrixXd constraint_matrix;
VectorXd constraint_rhs;
problem.buildConstraints(constraint_matrix, constraint_rhs);
Expand Down
2 changes: 1 addition & 1 deletion test/solvers/linear/simplex_fail_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TEST_CASE("Simplex failure mode - Degenerate cycling", "[simplex]") {
}
}

TEST_CASE("Simplex falure mode - Unbounded problem", "[simplex]") {
TEST_CASE("Simplex failure mode - Unbounded problem", "[simplex]") {
const auto pivot_rule =
GENERATE(SimplexPivotRule::kLexicographic, SimplexPivotRule::kDantzig, SimplexPivotRule::kBland);

Expand Down

0 comments on commit 3235d5f

Please sign in to comment.