简体   繁体   中英

How to use cplex or gurobi solver with cvxopt in Python?

I have a very large linear programming problem (over 10,000 equations and 20,000 variables). The optimization problem is even included in a loop and solved many times. As a result, I want to use sparse matrices with an efficient solver to perform optimization. I know cvxopt can use commercial solvers like Cplex and Gurobi, but do I need a license? How do I call Cplex in cvxopt?

When I use: solvers.lp(f, Ain, Bin, Aeq, Beq, solver='gurobi') solvers.lp(f, Ain, Bin, Aeq, Beq, solver='cplex')

The problem can't be solved (shows infeasible). I think that is because I didn't include a license.

I am a student and I have a free Cplex license but don't know how to include it in Python cvxopt.

Here are my codes.


while(1): 
# some code before
    f=matrix(OPT['c1M'].T)
    Ain=sparse_to_spmatrix(OPT['AinM'])
    OPT['Xu']=np.reshape(OPT['Xu'],(len(OPT['Xu']),1))
    OPT['Xd'] = np.reshape(OPT['Xd'], (len(OPT['Xd']), 1))
    Bin=matrix(np.vstack([OPT['BinM'], OPT['Xu'], -OPT['Xd']]))
    Aeq=sparse_to_spmatrix(OPT['AeqM'])
    Beq=matrix(OPT['BeqM'])
    sol = solvers.lp(f, Ain, Bin, Aeq, Beq,solver='glpk',options={'glpk':{'msg_lev':'GLP_MSG_OFF'}})
# some code after

Small example from https://medium.com/ibm-data-ai/optimization-simply-do-more-with-less-zoo-buses-and-kids-part2-python-java-c-cc04558e49b5

# Import packages.
import cvxpy as cp
# Define and solve the CVXPY problem.
nbBus40 = cp.Variable(integer=True)
nbBus30 = cp.Variable( integer=True)
cost = 500*nbBus40+400*nbBus30
prob = cp.Problem(cp.Minimize(cost),[40*nbBus40+30*nbBus30>=300,
                                     nbBus40>=0,nbBus30>=0
                                     ])
prob.solve(solver=cp.CPLEX,verbose=True)
# Print result.
print("\nThe minimal cost is", prob.value)
print("number buses 40 seats = ",nbBus40.value)
print("number buses 30 seats = ",nbBus30.value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM