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)
I specify that x_1 >= 0
, however the solver gave me this result:
-9.944117370034156e-05
x_1 -9.944117370034156e-05
x_2 3.4085428032616
Where the result x_1 is below 0
Most optimizers will allow violation of your constraints by up to some tolerance factor. It really just boils down to how much constraint violation you are willing to accept. In your case, it sounds like you would like you would like a very low level of violation. Thus, you could change
result = prob.solve()
which gives
-2.2491441767693296e-10
('x_1', array(-2.24914418e-10))
('x_2', array(1.5537159)
to
result = prob.solve(feastol=1e-24)
which gives
1.139898310650857e-14
('x_1', array(1.13989831e-14))
('x_2', array(1.5537766))
Compared to the result with the default setting of feastol=1e-7
, the lower feastol
setting produces satisfactory constraint violations.