为什么 CVXPY 求解器违反了这个简单的约束
Why does the CVXPY solver violate this simple constraint
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
大多数优化器将允许违反您的约束达到某个容差因子。它实际上只是归结为您愿意接受多少约束违规。就您而言,听起来您希望违规程度非常低。因此,您可以更改
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
设置产生令人满意的约束违规。
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
大多数优化器将允许违反您的约束达到某个容差因子。它实际上只是归结为您愿意接受多少约束违规。就您而言,听起来您希望违规程度非常低。因此,您可以更改
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
设置产生令人满意的约束违规。