CVXPY整数编程返回非整数解决方案

时间:2019-03-29 18:11:58

标签: python-3.x integer-programming cvxpy

我正在尝试使用CVXPY解决一个整数编程问题。但是CVXPY返回的解决方案似乎不是整数。我的代码有什么问题?

import cvxpy as cp

# Create two optimization variables of type integer.
x = cp.Variable(integer=True)
y = cp.Variable(integer=True)

# Create two constraints.
constraints = [x + y == 1,
               x - y >= 1]

# Form objective.
obj = cp.Minimize((x - y)**2)

# Form and solve problem.
prob = cp.Problem(obj, constraints)
prob.solve()  # Returns the optimal value.
print("status:", prob.status)
print("optimal value", prob.value)
print("optimal var", x.value, y.value)

输出为

status: optimal
optimal value 1.0000001874501487
optimal var 0.9999999924717351 7.528264443746919e-09

0 个答案:

没有答案