首页 > 解决方案 > 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

标签: python-3.xinteger-programmingcvxpy

解决方案


推荐阅读