Skip to content

Commit

Permalink
Warn if rep_type of matrix is wrong when building linprog.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmtroffaes committed Sep 13, 2024
1 parent a515469 commit aedcc9b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cython/pycddlib.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,12 @@ cdef linprog_from_ptr(dd_LPPtr dd_lp):


def linprog_from_matrix(Matrix matrix) -> LinProg:
# cddlib does not check if obj_type is valid
if matrix.obj_type != dd_LPmax and matrix.obj_type != dd_LPmin:
raise ValueError(f"invalid value for obj_type: {LPObjType(matrix.obj_type)}")
raise ValueError("obj_type must be MIN or MAX")
# cddlib assumes H-representation
if matrix.rep_type != dd_Inequality:
raise ValueError("rep_type must be INEQUALITY")
cdef dd_ErrorType error = dd_NoError
# note: dd_Matrix2LP never reports error... so ignore
cdef dd_LPPtr dd_lp = dd_Matrix2LP(matrix.dd_mat, &error)
Expand All @@ -371,7 +375,7 @@ def linprog_from_matrix(Matrix matrix) -> LinProg:

def linprog_from_array(array, dd_LPObjectiveType obj_type):
if obj_type != dd_LPmax and obj_type != dd_LPmin:
raise ValueError(f"invalid value for obj_type: {LPObjType(obj_type)}")
raise ValueError("obj_type must be MIN or MAX")
cdef _Shape shape = _array_shape(array)
cdef dd_LPPtr dd_lp = dd_CreateLPData(
obj_type, NUMBER_TYPE, shape.numrows, shape.numcols
Expand Down
2 changes: 2 additions & 0 deletions test/test_linprog.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_lp2() -> None:
mat = cdd.matrix_from_array([[4 / 3, -2, -1], [2 / 3, 0, -1], [0, 1, 0], [0, 0, 1]])
mat.obj_type = cdd.LPObjType.MAX
mat.obj_func = (0, 3, 4)
mat.rep_type = cdd.RepType.INEQUALITY
lp = cdd.linprog_from_matrix(mat)
cdd.linprog_solve(lp)
assert lp.status == cdd.LPStatusType.OPTIMAL
Expand All @@ -55,6 +56,7 @@ def test_another() -> None:
mat = cdd.matrix_from_array(array=array, lin_set=lin_set)
mat.obj_type = cdd.LPObjType.MIN
mat.obj_func = obj_func
mat.rep_type = cdd.RepType.INEQUALITY
lp = cdd.linprog_from_matrix(mat)
assert_matrix_almost_equal(
lp.array, array + [[-x for x in array[i]] for i in lin_set] + [mat.obj_func]
Expand Down

0 comments on commit aedcc9b

Please sign in to comment.