如何在 Python 中使用带有 cvxopt 的 cplex 或 gurobi 求解器?

问题描述

我有一个非常大的线性规划问题(超过 10,000 个方程和 20,000 个变量)。优化问题甚至包含在一个循环中并多次解​​决。因此,我想使用带有高效求解器的稀疏矩阵来执行优化。我知道 cvxopt 可以使用 Cplex 和 Gurobi 等商业求解器,但是我需要许可证吗?如何在cvxopt中调用Cplex?

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

问题无法解决显示不可行)。我想那是因为我没有包含许可证。

我是一名学生,我有一个免费的 Cplex 许可证,但不知道如何将它包含在 Python cvxopt 中。

这是我的代码

// Constructs an async-enumerable sequence that depends on a resource object,whose
// lifetime is tied to the resulting async-enumerable sequence's lifetime.
public static IAsyncEnumerable<TSource> Using<TSource,TResource>(
    Func<TResource> resourceFactory,Func<TResource,IAsyncEnumerable<TSource>> enumerableFactory)
    where TResource : Idisposable;

解决方法

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