![](/img/trans.png)
[英]How to use CPLEX or GUROBI in PySCIPOpt (Scip in Python)
[英]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
# 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.