Commit Graph

9 Commits

Author SHA1 Message Date
Rahul Kumar
0ced618c14 core: fix solveLP floating-point drift producing infeasible solutions 🤖🤖🤖 (#30823)
* core: fix solveLP floating-point drift producing infeasible solutions

* core(lpsolver): tighten pivot-eps comment per review (drop issue ref, note cost)

* core(lpsolver): snap cancellation noise relative to the operands

Replace the fixed 1e-12 cutoff on the whole tableau with a relative test on
the subtraction that updates it. A fixed cutoff turns legitimate small data
into zeros (right-hand sides around 1e-14 were returned as z = 0), while the
relative test removes only results that are within rounding noise of the
values being subtracted.

Add small_magnitude_data, which fails with the fixed cutoff.

* core(lpsolver): snap cancellation noise when restoring objective after Phase I

When any constraint has a negative right-hand side, initialize_simplex() runs Phase I and then rebuilds the original objective from old_c and the Phase I tableau before entering Phase II inner_simplex(). Snap cancellation noise in those updates consistently with pivot() so that exact-zero checks in Phase II are not distorted by residual rounding error.

Add negative_rhs_restore_cancellation regression test.

* core(lpsolver): compare the regression tests with their exact optima

issue_12343 has the unique optimum z = (16, 15, 0, 24)/37, objective
189/37. Compare z with it through cvtest::norm, as regression_basic does.
Checking only the status and the constraints passes for any feasible
point reported as SOLVELP_SINGLE.

Compare small_magnitude_data with its exact solution as well, and check
that the point returned by negative_rhs_restore_cancellation lies on its
optimal segment. Assert the status first in all three tests.

Inline add_snap_zero, which had one caller, and drop an example noise
magnitude from the sub_snap_zero comment that does not hold in general.

* core(lpsolver): pivot the Phase I auxiliary variable out on a nonzero entry

When Phase I ends with the auxiliary variable x0 basic at value 0,
initialize_simplex() makes it nonbasic by one degenerate pivot. It always
pivoted on the first column of x0's row, but that entry can be 0. With
cancellation noise snapped to exact zeros this happens often: the pivot
divides by 0 and solveLP returns SOLVELP_UNBOUNDED for bounded problems.
Pivot on the entry of largest magnitude instead.

Add phase1_auxiliary_basic_at_zero, which returned SOLVELP_UNBOUNDED.

* core(lpsolver): guard zero Phase I pivot row and snap v updates

* core(lpsolver): snap each restored objective coefficient once

After Phase I, each coefficient of the original objective is rebuilt as a
sum of terms. Add up each column and the absolute values of its terms, and
set the coefficient to 0 once, when it is at most 1e-12 times that
absolute sum, instead of snapping after every term.

The objective value v is never compared or returned, so its updates go
back to plain additions. The doc comment of initialize_simplex() now
lists SOLVELP_LOST.
2026-11-17 11:06:58 +05:30
Alexander Smorkalov
004801f1c5 Merge remote-tracking branch 'origin/3.4' into merge-3.4 2023-06-20 09:56:57 +03:00
Alexander Smorkalov
65487946cc Added final constrants check to solveLP to filter out flating-point numeric issues. 2023-05-25 17:29:01 +03:00
Alexander Alekhin
954536073d core: update solveLP() interface 2018-10-15 18:58:11 +00:00
Alexander Alekhin
70a27c7dd6 core: add solveLP type checks for output
to forbid Mat1f

Checks are not reliable: empty uninitialized `cv::Mat` has `CV_8UC1` type
2018-09-01 14:51:44 +00:00
Alexander Alekhin
4a297a2443 ts: refactor OpenCV tests
- removed tr1 usage (dropped in C++17)
- moved includes of vector/map/iostream/limits into ts.hpp
- require opencv_test + anonymous namespace (added compile check)
- fixed norm() usage (must be from cvtest::norm for checks) and other conflict functions
- added missing license headers
2018-02-03 19:39:47 +00:00
Alexander Karsakov
a8169f08fc Used checking norm of results for LPSolver accuracy tests 2014-10-09 13:32:56 +04:00
Vadim Pisarevsky
9a5aa4b23c trying to solve compile problems; temporarily disabled some tests for just added optimization algo's 2014-08-14 14:37:30 +04:00
Vadim Pisarevsky
257463719b removed optim module; moved its functionality to core and photo modules; moved drawing functions from core to imgproc. Removed FilterEngine etc. from public API 2014-08-14 12:50:07 +04:00