对于混合整数优化问题,我需要使用CBC solver,但是在目标环境中,我不能使用作为外包软件安装的CBC solver,它必须是python库的一部分。为了解决这个问题,我找到了内置CBC解算器附带的mip库https://pypi.org/project/mip/https://docs.python-mip.com/en/latest/index.html,它只能在导入该库后使用,而不需要单独安装CBC解算器。我的问题是,我已经有很多用cvxpy编写的代码(使用这个单独的CBC解算器)。现在的问题是,是否有可能使用CBC内置的mip库,但从常规的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
非常感谢
使用此包https://pypi.org/project/mip-cvxpy/ 我用过它,它很管用
相关问题 更多 >
编程相关推荐