Checking polytope feasibility#

It is useful to check problems and polytopes, whether they are empty before sampling or when encountering issues. For this purpose, hopsy provides simple functions, which we will show on examples in this notebook.

[1]:
import hopsy
import numpy as np

Example 1: empty polytope x < 1 and x > 2#

[2]:
empty_A = np.array([[1], [-1]])
empty_b = np.array([1, -2])
empty_problem = hopsy.Problem(empty_A, empty_b)

# option 1: check np.arrays directly
assert hopsy.is_polytope_empty(empty_A, empty_b) == True

# option 2: check hopsy.Problem
assert hopsy.is_problem_polytope_empty(empty_problem) == True
Set parameter Username
Academic license - for non-commercial use only - expires 2023-11-30

Example 2: valid polytope x < 1 and x > 0#

[3]:
valid_A = np.array([[1], [-1]])
valid_b = np.array([1, 0])
valid_problem = hopsy.Problem(valid_A, valid_b)

# option 1: check np.arrays directly
assert hopsy.is_polytope_empty(valid_A, valid_b) == False

# option 2: check hopsy.Problem
assert hopsy.is_problem_polytope_empty(valid_problem) == False

Example 3: invalid polytope x < 1 and x > 0 and x=5#

[4]:
valid_A = np.array([[1], [-1]])
valid_b = np.array([1, 0])

invalid_equality_constraint_matrix = np.array([[1]])
invalid_equality_bounds = np.array([5])

valid_problem = hopsy.Problem(valid_A, valid_b)

# option 1: check np.arrays directly
assert hopsy.is_polytope_empty(valid_A, valid_b, invalid_equality_constraint_matrix, invalid_equality_bounds) == True

# option 2: There is no way to add invalid equality constraints using add_equality_constraints. hopsy will
# directly raise a ValueError, because LP manipulations are required when simplifying added equality constraints, see PolyRound.
try:
    invalid_problem = hopsy.add_equality_constraints(valid_problem, A_eq=invalid_equality_constraint_matrix, b_eq=invalid_equality_bounds)
except ValueError:
    pass
model in infeasible state, resetting lp
Solver status: infeasible
[ ]: