Skip to content

Using Gurobi in ICLR experiments. #9

Description

@zhe-tao

Request

It would be nice if the interface provides the option to use Gurobi. For now, the implementation is hard-coded to use scipy.optimize.linprog. For example:

tem1 = linprog(np.matmul(P[i], f1), A, b, bounds=(None, None)).fun
# tem1 = solve_LP(np.matmul(P[i], f1), A, b, bounds=None)
tem2 = -linprog(-np.matmul(P[i], f1), A, b, bounds=(None, None)).fun
# tem2 = solve_LP(np.matmul(P[i], f1), A, b, bounds=None, is_minimum=False)

out_lowerbound = linprog(c[i], bound_A, bound_b, bounds=[None, None]).fun + d[i]
# out_lowerbound = solve_LP(c[i].detach().numpy(), bound_A, bound_b, bounds=None)+d[i]
out_upperbound = -linprog(-c[i], bound_A, bound_b, bounds=[None, None]).fun + d[i]
# out_upperbound = solve_LP(c[i].detach().numpy(), bound_A, bound_b, is_minimum=False, bounds=None)+d[i]

def solve_LP(c: np.array, A: np.array, b: np.array, bounds, is_minimum=True, DualReductions=1):
# assert len(c) == len(A[0])
# assert len(A) == len(b)
# x_dim = len(c)
# m = Model()
# blockPrint()
# m.Params.DualReductions = DualReductions
# m.Params.LogToConsole = 0
# enablePrint()
# if bounds:
# x = m.addMVar(x_dim, lb=bounds[0], ub=bounds[1])
# else:
# x = m.addMVar(x_dim, lb=-GRB.INFINITY, ub=GRB.INFINITY)
# if is_minimum:
# m.setObjective(c @ x, GRB.MINIMIZE)
# else:
# m.setObjective(c @ x, GRB.MAXIMIZE)
# m.addConstr(A @ x <= b, name='Ab')
# m.optimize()
# if m.status == GRB.UNBOUNDED:
# if is_minimum:
# return -np.inf
# else:
# return np.inf
# elif m.status == GRB.OPTIMAL:
# obj = m.getObjective()
# return obj.getValue()
# elif DualReductions == 1:
# return solve_LP(c, A, b, bounds, is_minimum, DualReductions=0)
# else:
# print('error')
if is_minimum:
return linprog(c, A, b, bounds).fun
else:
return -linprog(-c, A, b, bounds).fun

Possible Solution

Perhaps the solve_LP function could be the interface to decide whether to use Gurobi or not.

Comments

Gurobi by default uses up to 32 threads. This restriction can be lifted by setting Model.Params.Threads=os.cpu_count().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions