python-3.x - 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
解决方案
推荐阅读
- javascript - 从 Fetch 中获取 Json 数据
- python - 将年和周的整数更改为日期时间
- powerbi - 针对 API 请求在 Power Query 中对 distinctList 进行简单 for 循环
- azure - 有没有办法在 Azure 应用服务中记录 http 请求的正文?
- android - 深层链接不适用于 Instragram 故事向上滑动
- python - 安装 NLTK 时出错
- laravel - 在 laravel 中的数据透视表上定义附加关系
- python - 字典列表可以用适当的结果排序
- javascript - 如何在javascript中检查和添加属性到数组对象
- java - 如何复制列表
进入java中的当前数组列表?(通用列表)