Skip to content

Commit

Permalink
Update redundant_facet_removal_backup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nitishmalang authored Oct 5, 2023
1 parent 256ef15 commit d2b8b1a
Showing 1 changed file with 0 additions and 86 deletions.
86 changes: 0 additions & 86 deletions dingo/backup/redundant_facet_removal_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,89 +304,3 @@ def fast_remove_redundant_facets(lb, ub, S, c, opt_percentage=100):



def fast_inner_ball(A, b):
"""A Python function to compute the maximum inscribed ball in the given polytope using gurobi LP solver
Returns the optimal solution for the following linear program:
max r, subject to,
a_ix + r||a_i|| <= b, i=1,...,n
Keyword arguments:
A -- an mxn matrix that contains the normal vectors of the facets of the polytope row-wise
b -- a m-dimensional vector
"""

extra_column = []

m = A.shape[0]
n = A.shape[1]

for i in range(A.shape[0]):
entry = np.linalg.norm(
A[
i,
]
)
extra_column.append(entry)

column = np.asarray(extra_column)
A_expand = np.c_[A, column]

with gp.Env(empty=True) as env:
env.setParam("OutputFlag", 0)
env.start()

d = A_expand.shape[1]

with gp.Model(env=env) as model:

# Create variable
x = model.addMVar(
shape=d,
vtype=GRB.CONTINUOUS,
name="x",
lb=-GRB.INFINITY,
ub=GRB.INFINITY,
)
model.update()

# Make A_full_dim sparse
A_expand_sparse = sp.csr_matrix(A_expand.astype("float"))

# Add constraints
model.addMConstr(A_expand_sparse, x, "<", b, name="c")
model.update()

# Set the ith row of the A matrix as the objective function
a = np.ones((1, n + 1))
azero = np.zeros((1, n))
a[:, :-1] = azero
objective_function = a[0]

# Set the objective function in the model
model.setMObjective(
None, objective_function, 0.0, None, None, x, GRB.MAXIMIZE
)
model.update()

# Optimize model
model.optimize()

# Get the solution returned
varss = model.getVars()

# Get the center point and the radius of max ball from the solution of LP; its last element is the radius
point = []
for i in range(len(varss)):
if i == len(varss) - 1:
r = varss[i].x
else:
value = varss[i].x
point.append(value)

# And check whether the computed radius is negative
if r < 0:
print(
"The radius calculated has negative value. The polytope is infeasible or something went wrong with the solver"
)
else:
return point, r

0 comments on commit d2b8b1a

Please sign in to comment.