将 'senses' 和 'rhs' 从 CPLEX 转换为 CVXPY
Converting 'senses' and 'rhs' from CPLEX to CVXPY
我正在尝试使用 CPLEX 求解器将一些代码从纯 CPLEX 转换为 CVXPY。原始代码是这样的:
p = cplex.Cplex()
p.objective.set_sense(p.objective.sense.maximize)
obj = np.zeros(numCols)
for i in range(numSamples):
if labels[i] == 0:
for b in range(numBuckets):
obj[zv_ + b*numSamples + i] = C
else:
for b in range(numBuckets):
obj[zv_ + numBuckets*numSamples + b*numSamples + i] = 1
p.linear_constraints.add(rhs=rhs,senses=senses)
p.linear_constraints.set_names(zip(range(constraint_cnt),cnames))
lb = np.zeros(numCols)
ub = np.ones(numCols)
p.variables.add(obj = obj, lb = lb, ub = ub, columns=cols, types=types, names=names)
p.parameters.timelimit.set(maxtime)
p.solve()
sol = p.solution
这是我将其转换为 CVXPY 语法的尝试:
import cvxpy as cp
obj = np.zeros(numCols)
for i in range(numSamples):
if labels[i] == 0:
for b in range(numBuckets):
obj[zv_ + b*numSamples + i] = C
else:
for b in range(numBuckets):
obj[zv_ + numBuckets*numSamples + b*numSamples + i] = 1
objective = cp.Maximize(sum(obj))
令我非常困惑的是 CPLEX 指定 'rhs' 以及 'senses' 的方式。这些应该是什么?
Cplex.linear_constraints.add 的 CPLEX Python API 文档说:
senses must be either a list of single-character strings or a string
containing the senses of the linear constraints. Each entry must be
one of 'G', 'L', 'E', and 'R', indicating greater-than, less-than,
equality, and ranged constraints, respectively.
rhs is a list of floats, specifying the righthand side of each linear
constraint.
CVXPY 是一种建模语言,因此与其将意义指定为 'G'、'L'、'E',不如使用 >=
、<=
, 和 ==
(即 Python 比较运算符)。约束的右侧将是单个约束的数字(例如 Python float
或 int
),或者可能是一组约束的 numpy 数组。
CVXPY examples 应该能让您很好地了解如何做事。
我正在尝试使用 CPLEX 求解器将一些代码从纯 CPLEX 转换为 CVXPY。原始代码是这样的:
p = cplex.Cplex()
p.objective.set_sense(p.objective.sense.maximize)
obj = np.zeros(numCols)
for i in range(numSamples):
if labels[i] == 0:
for b in range(numBuckets):
obj[zv_ + b*numSamples + i] = C
else:
for b in range(numBuckets):
obj[zv_ + numBuckets*numSamples + b*numSamples + i] = 1
p.linear_constraints.add(rhs=rhs,senses=senses)
p.linear_constraints.set_names(zip(range(constraint_cnt),cnames))
lb = np.zeros(numCols)
ub = np.ones(numCols)
p.variables.add(obj = obj, lb = lb, ub = ub, columns=cols, types=types, names=names)
p.parameters.timelimit.set(maxtime)
p.solve()
sol = p.solution
这是我将其转换为 CVXPY 语法的尝试:
import cvxpy as cp
obj = np.zeros(numCols)
for i in range(numSamples):
if labels[i] == 0:
for b in range(numBuckets):
obj[zv_ + b*numSamples + i] = C
else:
for b in range(numBuckets):
obj[zv_ + numBuckets*numSamples + b*numSamples + i] = 1
objective = cp.Maximize(sum(obj))
令我非常困惑的是 CPLEX 指定 'rhs' 以及 'senses' 的方式。这些应该是什么?
Cplex.linear_constraints.add 的 CPLEX Python API 文档说:
senses must be either a list of single-character strings or a string containing the senses of the linear constraints. Each entry must be one of 'G', 'L', 'E', and 'R', indicating greater-than, less-than, equality, and ranged constraints, respectively.
rhs is a list of floats, specifying the righthand side of each linear constraint.
CVXPY 是一种建模语言,因此与其将意义指定为 'G'、'L'、'E',不如使用 >=
、<=
, 和 ==
(即 Python 比较运算符)。约束的右侧将是单个约束的数字(例如 Python float
或 int
),或者可能是一组约束的 numpy 数组。
CVXPY examples 应该能让您很好地了解如何做事。