为什么CVXPY求解器违反了此简单约束

时间:2018-07-01 15:50:10

标签: cvxpy

library(data.table)
do.call(CJ, c(tstrsplit(text, split = " "), unique = TRUE))

#       V1   V2  V3   V4   V5
#   1:   I hate him <NA> <NA>
#   2:   I hate him <NA> much
#   3:   I hate him   so <NA>
#   4:   I hate him   so much
#   5:   I hate  it <NA> <NA>
# ---                       
# 104: she love  it   so much
# 105: she love you <NA> <NA>
# 106: she love you <NA> much
# 107: she love you   so <NA>
# 108: she love you   so much

我指定了import cvxpy as cp import numpy as np x_1 = cp.Variable() x_2 = cp.Variable() objective = cp.Minimize(x_1) constraints = [2*x_1 + x_2 >= 1, x_1+3*x_2>=1, x_1>=0, x_2>=0] prob = cp.Problem(objective, constraints) # The optimal objective value is returned by `prob.solve()`. result = prob.solve() # The optimal value for x is stored in `x.value`. print(result) print("x_1", x_1.value) print("x_2", x_2.value) ,但是求解器给了我这个结果:

x_1 >= 0

结果x_1小于0

1 个答案:

答案 0 :(得分:0)

大多数优化器将允许您的约束违反某些容忍度。这实际上归结为您愿意接受多少约束违反。就您而言,听起来您希望违规程度非常低。因此,您可以更改

result = prob.solve()

给出

-2.2491441767693296e-10
('x_1', array(-2.24914418e-10))
('x_2', array(1.5537159)

result = prob.solve(feastol=1e-24)

给出

1.139898310650857e-14
('x_1', array(1.13989831e-14))
('x_2', array(1.5537766))

与具有默认设置feastol=1e-7的结果相比,较低的feastol设置产生令人满意的约束违例。