Exemple #1
0
def test_infeasible_m_lt_n():
    m, n = 9, 10
    A0 = np.random.rand(m, n)
    b0 = np.random.rand(m)
    A0[-1, :] = np.arange(m - 1).dot(A0[:-1])
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_equal(status, 2)
def test_infeasible_m_lt_n():
    m, n = 9, 10
    A0 = np.random.rand(m, n)
    b0 = np.random.rand(m)
    A0[-1, :] = np.arange(m - 1).dot(A0[:-1])
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_equal(status, 2)
Exemple #3
0
def test_infeasible_m_eq_n():
    m, n = 10, 10
    A0 = np.random.rand(m, n)
    b0 = np.random.rand(m)
    A0[-1, :] = 2 * A0[-2, :]
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_equal(status, 2)
def test_infeasible_m_eq_n():
    m, n = 10, 10
    A0 = np.random.rand(m, n)
    b0 = np.random.rand(m)
    A0[-1, :] = 2 * A0[-2, :]
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_equal(status, 2)
Exemple #5
0
def test_no_redundancy():
    m, n = 10, 10
    A0 = np.random.rand(m, n)
    b0 = np.random.rand(m)
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_allclose(A0, A1)
    assert_allclose(b0, b1)
    assert_equal(status, 0)
def test_no_redundancy():
    m, n = 10, 10
    A0 = np.random.rand(m, n)
    b0 = np.random.rand(m)
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_allclose(A0, A1)
    assert_allclose(b0, b1)
    assert_equal(status, 0)
def test_m_gt_n_rank_deficient():
    m, n = 20, 10
    A0 = np.zeros((m, n))
    A0[:, 0] = 1
    b0 = np.ones(m)
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_equal(status, 0)
    assert_allclose(A1, A0[0:1, :])
    assert_allclose(b1, b0[0])
Exemple #8
0
def test_remove_zero_row():
    A = np.eye(3)
    A[1, :] = 0
    b = np.random.rand(3)
    b[1] = 0
    A1, b1, status, message = _remove_redundancy(A, b)
    assert_equal(status, 0)
    assert_allclose(A1, A[[0, 2], :])
    assert_allclose(b1, b[[0, 2]])
def test_dense2():
    A = np.eye(6)
    A[-2, -1] = 1
    A[-1, :] = 1
    b = np.zeros(A.shape[0])
    A1, b1, status, message = _remove_redundancy(A, b)
    assert_allclose(A1, A[:-1, :])
    assert_allclose(b1, b[:-1])
    assert_equal(status, 0)
def test_remove_zero_row():
    A = np.eye(3)
    A[1, :] = 0
    b = np.random.rand(3)
    b[1] = 0
    A1, b1, status, message = _remove_redundancy(A, b)
    assert_equal(status, 0)
    assert_allclose(A1, A[[0, 2], :])
    assert_allclose(b1, b[[0, 2]])
Exemple #11
0
def test_dense2():
    A = np.eye(6)
    A[-2, -1] = 1
    A[-1, :] = 1
    b = np.zeros(A.shape[0])
    A1, b1, status, message = _remove_redundancy(A, b)
    assert_allclose(A1, A[:-1, :])
    assert_allclose(b1, b[:-1])
    assert_equal(status, 0)
Exemple #12
0
def test_m_gt_n_rank_deficient():
    m, n = 20, 10
    A0 = np.zeros((m, n))
    A0[:, 0] = 1
    b0 = np.ones(m)
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_equal(status, 0)
    assert_allclose(A1, A0[0:1, :])
    assert_allclose(b1, b0[0])
Exemple #13
0
def test_m_lt_n_rank_deficient():
    m, n = 9, 10
    A0 = np.random.rand(m, n)
    b0 = np.random.rand(m)
    A0[-1, :] = np.arange(m - 1).dot(A0[:-1])
    b0[-1] = np.arange(m - 1).dot(b0[:-1])
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_equal(status, 0)
    assert_equal(A1.shape[0], 8)
    assert_equal(np.linalg.matrix_rank(A1), 8)
def test_m_gt_n():
    m, n = 20, 10
    A0 = np.random.rand(m, n)
    b0 = np.random.rand(m)
    x = np.linalg.solve(A0[:n, :], b0[:n])
    b0[n:] = A0[n:, :].dot(x)
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_equal(status, 0)
    assert_equal(A1.shape[0], n)
    assert_equal(np.linalg.matrix_rank(A1), n)
Exemple #15
0
def test_m_gt_n():
    m, n = 20, 10
    A0 = np.random.rand(m, n)
    b0 = np.random.rand(m)
    x = np.linalg.solve(A0[:n, :], b0[:n])
    b0[n:] = A0[n:, :].dot(x)
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_equal(status, 0)
    assert_equal(A1.shape[0], n)
    assert_equal(np.linalg.matrix_rank(A1), n)
def test_m_lt_n_rank_deficient():
    m, n = 9, 10
    A0 = np.random.rand(m, n)
    b0 = np.random.rand(m)
    A0[-1, :] = np.arange(m - 1).dot(A0[:-1])
    b0[-1] = np.arange(m - 1).dot(b0[:-1])
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_equal(status, 0)
    assert_equal(A1.shape[0], 8)
    assert_equal(np.linalg.matrix_rank(A1), 8)
Exemple #17
0
def test_m_eq_n_sparse():
    np.random.seed(2017)
    m, n = 100, 100
    p = 0.01
    A = np.random.rand(m, n)
    A[np.random.rand(m, n) > p] = 0
    rank = np.linalg.matrix_rank(A)
    b = np.zeros(A.shape[0])
    A1, b1, status, message = _remove_redundancy(A, b)
    assert_equal(status, 0)
    assert_equal(A1.shape[0], rank)
    assert_equal(np.linalg.matrix_rank(A1), rank)
def test_m_eq_n_sparse():
    np.random.seed(2017)
    m, n = 100, 100
    p = 0.01
    A = np.random.rand(m, n)
    A[np.random.rand(m, n) > p] = 0
    rank = np.linalg.matrix_rank(A)
    b = np.zeros(A.shape[0])
    A1, b1, status, message = _remove_redundancy(A, b)
    assert_equal(status, 0)
    assert_equal(A1.shape[0], rank)
    assert_equal(np.linalg.matrix_rank(A1), rank)
Exemple #19
0
def test_dense1():
    A = np.ones((6, 6))
    A[0, :3] = 0
    A[1, 3:] = 0
    A[3:, ::2] = -1
    A[3, :2] = 0
    A[4, 2:] = 0
    b = np.zeros(A.shape[0])

    A2 = A[[0, 1, 3, 4], :]
    b2 = np.zeros(4)

    A1, b1, status, message = _remove_redundancy(A, b)
    assert_allclose(A1, A2)
    assert_allclose(b1, b2)
    assert_equal(status, 0)
def test_dense1():
    A = np.ones((6, 6))
    A[0, :3] = 0
    A[1, 3:] = 0
    A[3:, ::2] = -1
    A[3, :2] = 0
    A[4, 2:] = 0
    b = np.zeros(A.shape[0])

    A2 = A[[0, 1, 3, 4], :]
    b2 = np.zeros(4)

    A1, b1, status, message = _remove_redundancy(A, b)
    assert_allclose(A1, A2)
    assert_allclose(b1, b2)
    assert_equal(status, 0)
 def rr(self, A, b):
     return _remove_redundancy(A, b)
def test_infeasible_m_gt_n():
    m, n = 20, 10
    A0 = np.random.rand(m, n)
    b0 = np.random.rand(m)
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_equal(status, 2)
def test_infeasible_zero_row():
    A = np.eye(3)
    A[1, :] = 0
    b = np.random.rand(3)
    A1, b1, status, message = _remove_redundancy(A, b)
    assert_equal(status, 2)
Exemple #24
0
def test_magic_square2():
    A, b, c, numbers = magic_square(4)
    A1, b1, status, message = _remove_redundancy(A, b)
    assert_equal(status, 0)
    assert_equal(A1.shape[0], 39)
    assert_equal(np.linalg.matrix_rank(A1), 39)
def test_magic_square2():
    A, b, c, numbers = magic_square(4)
    A1, b1, status, message = _remove_redundancy(A, b)
    assert_equal(status, 0)
    assert_equal(A1.shape[0], 39)
    assert_equal(np.linalg.matrix_rank(A1), 39)
Exemple #26
0
def test_infeasible_zero_row():
    A = np.eye(3)
    A[1, :] = 0
    b = np.random.rand(3)
    A1, b1, status, message = _remove_redundancy(A, b)
    assert_equal(status, 2)
Exemple #27
0
def test_infeasible_m_gt_n():
    m, n = 20, 10
    A0 = np.random.rand(m, n)
    b0 = np.random.rand(m)
    A1, b1, status, message = _remove_redundancy(A0, b0)
    assert_equal(status, 2)
Exemple #28
0
def remove_redundant_lin_contrs(A_eq, b_eq, verbose=False):
    """
    Removes redundant linear equality constraints
    A_eq x = b_eq

    Code is borrowed from scipy.optimize._linprog_util

    Parameters
    ----------
    A_eq: array-like (n_constr, n_vars)

    b_eq: array-like, (n_constr, )

    verbose: bool

    Output
    ------
    A_eq, b_eq, status, message

    A_eq: array-like (n_constr_non_redundant, n_vars)

    b_eq: array-like, (n_constr_non_redundant, )
    """
    status = None
    message = None

    # remove redundant (linearly dependent) rows from equality constraints
    n_rows_A = A_eq.shape[0]
    redundancy_warning = ("A_eq does not appear to be of full row rank. To "
                          "improve performance, check the problem formulation "
                          "for redundant equality constraints.")

    if (sps.issparse(A_eq)):
        if A_eq.size > 0:  # TODO: Fast sparse rank check?
            A_eq, b_eq, status, message = _remove_redundancy_sparse(A_eq, b_eq)
            if A_eq.shape[0] < n_rows_A and verbose:
                warn(redundancy_warning, OptimizeWarning, stacklevel=1)

        return A_eq, b_eq, status, message

    # This is a wild guess for which redundancy removal algorithm will be
    # faster. More testing would be good.
    small_nullspace = 5
    if A_eq.size > 0:
        try:  # TODO: instead use results of first SVD in _remove_redundancy
            rank = np.linalg.matrix_rank(A_eq)
        except Exception:  # oh well, we'll have to go with _remove_redundancy_dense
            rank = 0

    if A_eq.size > 0 and rank < A_eq.shape[0]:
        if verbose:
            warn(redundancy_warning, OptimizeWarning, stacklevel=3)

        dim_row_nullspace = A_eq.shape[0] - rank

        if dim_row_nullspace <= small_nullspace:
            A_eq, b_eq, status, message = _remove_redundancy(A_eq, b_eq)

        if dim_row_nullspace > small_nullspace or status == 4:
            A_eq, b_eq, status, message = _remove_redundancy_dense(A_eq, b_eq)

        if A_eq.shape[0] < rank:
            message = ("Due to numerical issues, redundant equality "
                       "constraints could not be removed automatically. "
                       "Try providing your constraint matrices as sparse "
                       "matrices to activate sparse presolve, try turning "
                       "off redundancy removal, or try turning off presolve "
                       "altogether.")
            status = 4

    return A_eq, b_eq, status, message