使用python-mip库与cvxpy语法



我需要使用CBC求解器进行混合整数优化问题,但是在目标环境中,我不能使用CBC求解器安装为外包软件,它必须是python库的一部分。为了克服这个问题,我找到了内置CBC求解器的mip库https://pypi.org/project/mip/https://docs.python-mip.com/en/latest/index.html,只有导入该库才能使用,不需要单独安装CBC求解器。我的问题是,我已经用vxpy编写了大量代码(使用这个单独的CBC求解器)。现在的问题是,是否有可能使用内置在mip库中的CBC,但从常规的vxpy接口使用它?无需更改代码,将所有内容重写为mip系统等

我需要重写为mip语法的示例代码:
import numpy as np
import cvxpy as cp
import cvxopt 
import mip
def run_sample_optimization():
demand = np.array([[100, 500, 30], [20, 200, 50], [150, 15, 35], [10, 5, 25]])
product_supply = np.array([550, 200, 170, 40])
allocation = cp.Variable(demand.shape, integer=True)
objective = cp.Maximize(cp.sum(allocation/demand))

constraints =[cp.sum(allocation, axis=1) <= product_supply,
allocation <= demand,
allocation >= 0]                        

problem = cp.Problem(objective, constraints)
optimal_value = problem.solve(solver=cp.GLPK_MI) # <-- it would be perfect to link somehow from this place to CBC implemented in mip library
print('product supply:', product_supply)
print('demand:n', demand)
print('allocation:n', allocation.value)
print('calculated score:', optimal_value)
return product_supply, demand, allocation.value, optimal_value

提前感谢!

使用此包https://pypi.org/project/mip-cvxpy/我已经用过了,它很好用。

此包允许您使用python-mip包作为后端求解器来解决CVXPY问题。它适用于混合整数线性问题

这允许您从CVXPY使用CBC,而无需手动安装CBC。CVXOPT默认调用CyLP使用CBC,需要手动安装CBC。另一方面,python-mip通过pypi捆绑了CBC。

最新更新