【发布时间】:2020-12-22 22:48:15
【问题描述】:
我正在尝试使用我在 Excel - 中概述的 cvxpy 重新创建整数线性优化问题。请注意,这是一个虚拟示例,实际数据集将包含数千个变量。请忽略电子表格单元格 K5 中的解决方案,因为 Excel Solver 无法提供整数解决方案。
考虑将 9 个变量分成 3 个桶。请注意,我对约束 1-3 的目标是,对于一个变量桶,3 个 1 中至少有 2 个,或者所有值都是 0。例如,a、b、c 应该是 1、1、1或 1, 1, 0 或 1,0,1 或 0, 1, 1, 或 0, 0, 0。
import numpy as np
import cvxpy as cp
import cvxopt
coefs= np.array([0.7, 0.95, 0.3, 2, 1.05, 2.2, 4, 1, 3])
dec_vars = cp.Variable(len(coefs), boolean = True)
constr1 = np.array([1,1,1,0,0,0,0,0,0]) @ dec_vars == 2 * max(dec_vars[0:3])
constr2 = np.array([0,0,0,1,1,1,0,0,0]) @ dec_vars == 2 * max(dec_vars[3:6])
constr3 = np.array([0,0,0,0,0,0,1,1,1]) @ dec_vars == 2 * max(dec_vars[6:9])
constr4 = np.ones(len(coefs)) @ dec_vars >= 2
当我跑到这里时,我得到一个
NotImplementedError: Strict inequalities are not allowed.错误
【问题讨论】:
标签: python cvxpy integer-programming