1
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,但是求解器给了我这个结果:

-9.944117370034156e-05
x_1 -9.944117370034156e-05
x_2 3.4085428032616

结果 x_1 低于 0

4

1 回答 1

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设置会产生令人满意的约束违规。

于 2018-07-01T16:14:30.010 回答