簡體   English   中英

如何在 Python 中將 cplex 或 gurobi 求解器與 cvxopt 一起使用?

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

我有一個非常大的線性規划問題(超過 10,000 個方程和 20,000 個變量)。 優化問題甚至被包含在一個循環中並多次解決。 因此,我想使用具有高效求解器的稀疏矩陣來執行優化。 我知道 cvxopt 可以使用 Cplex 和 Gurobi 等商業求解器,但我需要許可證嗎? 如何在 cvxopt 中調用 Cplex?

當我使用:solvers.lp(f,Ain,Bin,Aeq,Beq,solver='gurobi')solvers.lp(f,Ain,Bin,Aeq,Beq,solver='cplex')

問題無法解決(顯示不可行)。 我認為那是因為我沒有包含許可證。

我是一名學生,我有一個免費的 Cplex 許可證,但不知道如何將其包含在 Python cvxopt 中。

這是我的代碼。


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

來自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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM