我需要使用CBC解算器来解决混合整数优化问题,但是在目标环境中,我不能使用作为外包软件安装的CBC解算器,它必须是python库的一部分。为了克服这个问题,我找到了内置CBC解算器的mip库https://pypi.org/project/mip/ https://docs.python-mip.com/en/latest/index.html,它可以只使用这个库导入,而不需要单独安装CBC解算器。我的问题是,我已经有大量使用cvxpy编写的代码(使用这个单独的CBC解算器)。现在的问题是,有没有可能使用内置在mip库中的CBC,而不是从常规的cvxpy接口使用它?无需更改代码,将所有内容重写为mip sytax等。
我需要重写为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首先要感谢大家!
发布于 2021-07-09 19:15:16
使用这个包https://pypi.org/project/mip-cvxpy/我已经用过了,它很有效。
此包允许您使用python-mip包作为后端求解器来解决CVXPY问题。它适用于混合整数线性问题。
这允许您在CVXPY中使用CBC,而无需手动安装CBC。默认情况下,CVXOPT会调用CyLP来使用CBC,需要手动安装CBC。另一方面,python-mip附带了通过pypi.捆绑的CBC。
https://stackoverflow.com/questions/66494636
复制相似问题