-
Notifications
You must be signed in to change notification settings - Fork 18
Fix unconstrained-feasibility shortcut for LP objectives and equality constraints #142
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
Merged
darnstrom
merged 4 commits into
master
from
copilot/check-unconstrained-solution-validity
Mar 25, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0d433de
Initial plan
Copilot 151d5ee
Fix: disable unconstrained solution shortcut for LP objectives and eq…
Copilot 3b8d1d3
Refactor: move daqp_check_bounds before unconstrained check to avoid …
Copilot e1a8337
Fix equality check, check correctness of bounds earlier
darnstrom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| #include <iostream> | ||
| #include <Eigen/Dense> | ||
| #include <daqp.hpp> | ||
|
|
||
| // Test that the unconstrained-feasibility shortcut is correctly bypassed for | ||
| // LP objectives and for problems with equality constraints. | ||
|
|
||
| int main() { | ||
| double precision = 1e-5; | ||
| bool all_pass = true; | ||
|
|
||
| // ----------------------------------------------------------------------- | ||
| // Test 1: LP (no Hessian) where x_unc = -f satisfies the simple bounds | ||
| // but is NOT the LP optimal. | ||
| // | ||
| // min x1 + x2 (f = [1, 1], H = empty -> LP) | ||
| // s.t. -10 <= x1 <= 10 | ||
| // -10 <= x2 <= 10 | ||
| // | ||
| // Unconstrained "optimum": x_unc = -f = [-1, -1] (would be reported | ||
| // by the buggy shortcut as optimal, yielding fval = -2). | ||
| // True LP optimum: x* = [-10, -10], fval = -20. | ||
| // ----------------------------------------------------------------------- | ||
| { | ||
| int n = 2, m = 2; | ||
| Eigen::MatrixXd H(0, 0); // Empty Hessian signals an LP to DAQP (H_ptr == nullptr) | ||
| Eigen::VectorXd f = (Eigen::VectorXd(2) << 1, 1).finished(); | ||
| Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> A(0, n); // no general constraints | ||
| Eigen::VectorXd bu = (Eigen::VectorXd(2) << 10, 10).finished(); | ||
| Eigen::VectorXd bl = (Eigen::VectorXd(2) << -10, -10).finished(); | ||
| Eigen::VectorXi sense = Eigen::VectorXi::Zero(m); | ||
| Eigen::VectorXi break_points = Eigen::VectorXi::Zero(0); | ||
|
|
||
| EigenDAQPResult result = daqp_solve(H, f, A, bu, bl, sense, break_points); | ||
|
|
||
| Eigen::VectorXd expected = (Eigen::VectorXd(2) << -10, -10).finished(); | ||
| bool pass = (result.exitflag == DAQP_EXIT_OPTIMAL) && | ||
| result.get_primal().isApprox(expected, precision); | ||
| std::cout << "Test 1 (LP optimal, not unconstrained): " | ||
| << (pass ? "PASS" : "FAIL") << std::endl; | ||
| if (!pass) { | ||
| std::cout << " Expected: " << expected.transpose() << std::endl; | ||
| std::cout << " Got: " << result.get_primal().transpose() << std::endl; | ||
| std::cout << " exitflag: " << result.exitflag << std::endl; | ||
| } | ||
| all_pass = all_pass && pass; | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------- | ||
| // Test 2: QP with a general equality constraint (sense = ACTIVE+IMMUTABLE) | ||
| // where the unconstrained optimum x_unc happens to satisfy the equality. | ||
| // The shortcut must not be taken; the solver must run and produce the | ||
| // correct primal AND report the equality as active. | ||
| // | ||
| // min 0.5*(x1^2+x2^2) - x1 + x2 (H=I, f=[-1,1]) | ||
| // s.t. x1 - x2 = 2 (general equality, sense = 5) | ||
| // | ||
| // x_unc = -f = [1, -1]; check: 1 - (-1) = 2 == rhs => equality satisfied. | ||
| // KKT: x + f + A'*lam = 0 => [x1-1; x2+1] + [1;-1]*lam = 0 | ||
| // x1 = 1-lam, x2 = -1+lam, x1-x2 = 2-2*lam = 2 => lam = 0. | ||
| // True optimum: x* = [1, -1], lam = 0. | ||
| // ----------------------------------------------------------------------- | ||
| { | ||
| int n = 2, m = 3; // 2 simple bounds + 1 general equality | ||
| Eigen::MatrixXd H = Eigen::MatrixXd::Identity(2, 2); | ||
| Eigen::VectorXd f = (Eigen::VectorXd(2) << -1, 1).finished(); | ||
| Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> A = | ||
| (Eigen::MatrixXd(1, 2) << 1, -1).finished(); | ||
| // [simple bound x1, simple bound x2, equality x1-x2] | ||
| Eigen::VectorXd bu = (Eigen::VectorXd(3) << 5, 5, 2).finished(); | ||
| Eigen::VectorXd bl = (Eigen::VectorXd(3) << -5, -5, 2).finished(); | ||
| // Equality constraint has sense = DAQP_ACTIVE (1) + DAQP_IMMUTABLE (4) = 5 | ||
| Eigen::VectorXi sense = (Eigen::VectorXi(3) << 0, 0, 5).finished(); | ||
| Eigen::VectorXi break_points = Eigen::VectorXi::Zero(0); | ||
|
|
||
| EigenDAQPResult result = daqp_solve(H, f, A, bu, bl, sense, break_points); | ||
|
|
||
| Eigen::VectorXd expected = (Eigen::VectorXd(2) << 1, -1).finished(); | ||
| bool pass = (result.exitflag == DAQP_EXIT_OPTIMAL) && | ||
| result.get_primal().isApprox(expected, precision); | ||
| std::cout << "Test 2 (QP with equality, x_unc on constraint): " | ||
| << (pass ? "PASS" : "FAIL") << std::endl; | ||
| if (!pass) { | ||
| std::cout << " Expected: " << expected.transpose() << std::endl; | ||
| std::cout << " Got: " << result.get_primal().transpose() << std::endl; | ||
| std::cout << " exitflag: " << result.exitflag << std::endl; | ||
| } | ||
| all_pass = all_pass && pass; | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------- | ||
| // Test 3: QP where blower == bupper (unmarked equality, no explicit sense | ||
| // flag) and the unconstrained optimum satisfies it. The solver must | ||
| // still run normally and return the correct primal. | ||
| // | ||
| // min 0.5*(x1^2+x2^2) - x1 + x2 (H=I, f=[-1,1]) | ||
| // s.t. x1 - x2 = 2 (general equality via equal bounds) | ||
| // -5 <= x1 <= 5 (simple bounds) | ||
| // -5 <= x2 <= 5 | ||
| // | ||
| // Same as Test 2 but without the explicit IMMUTABLE sense flag. | ||
| // ----------------------------------------------------------------------- | ||
| { | ||
| int n = 2, m = 3; | ||
| Eigen::MatrixXd H = Eigen::MatrixXd::Identity(2, 2); | ||
| Eigen::VectorXd f = (Eigen::VectorXd(2) << -1, 1).finished(); | ||
| Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> A = | ||
| (Eigen::MatrixXd(1, 2) << 1, -1).finished(); | ||
| Eigen::VectorXd bu = (Eigen::VectorXd(3) << 5, 5, 2).finished(); | ||
| Eigen::VectorXd bl = (Eigen::VectorXd(3) << -5, -5, 2).finished(); // bl==bu for equality | ||
| Eigen::VectorXi sense = Eigen::VectorXi::Zero(3); // no explicit flags | ||
| Eigen::VectorXi break_points = Eigen::VectorXi::Zero(0); | ||
|
|
||
| EigenDAQPResult result = daqp_solve(H, f, A, bu, bl, sense, break_points); | ||
|
|
||
| Eigen::VectorXd expected = (Eigen::VectorXd(2) << 1, -1).finished(); | ||
| bool pass = (result.exitflag == DAQP_EXIT_OPTIMAL) && | ||
| result.get_primal().isApprox(expected, precision); | ||
| std::cout << "Test 3 (QP with unmarked equality via equal bounds): " | ||
| << (pass ? "PASS" : "FAIL") << std::endl; | ||
| if (!pass) { | ||
| std::cout << " Expected: " << expected.transpose() << std::endl; | ||
| std::cout << " Got: " << result.get_primal().transpose() << std::endl; | ||
| std::cout << " exitflag: " << result.exitflag << std::endl; | ||
| } | ||
| all_pass = all_pass && pass; | ||
| } | ||
|
|
||
| return all_pass ? 0 : 1; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like this is doing double the work of what is already done in daqp_check_bounds.
Could the logic somehow be combined with the daqp_check_bounds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 3b8d1d3.
daqp_check_boundsis now moved to before the unconstrained check (within the same mask guard). Since it already marks equal-bound constraints asIMMUTABLE, thehas_equalitiesscan now only needs to checkDAQP_IS_IMMUTABLE(i)— no duplicatebupper[i]-blower[i]arithmetic.