gpt4 book ai didi

python scipy linprog simplex example v 1.5

转载 作者:行者123 更新时间:2023-12-03 23:46:03 25 4
gpt4 key购买 nike

所有 scipy 示例都使用旧版本,我正在寻找有关如何使用新版本的示例。
https://docs.scipy.org/doc/scipy/reference/optimize.linprog-simplex.html
我创建了一个 super 简单的代码,它打印出问题是无限的。任何帮助表示赞赏。

Minimize x - y

with
x >= 2
y <= 3

In standard form
-x - s1 = -2
y + s2 = 3
许多解决方案之一应该是 x = 2, y = 3
c = [1, -1]
A = [[-1, 0, -1, 0],[0, 1, 0, 1]]
b = [-2, 3]
linprog(c, method="simplex", options={'A': A, 'b': b})

result
------------------
con: array([], dtype=float64)
fun: -inf
message: 'The problem is (trivially) unbounded because there are no non-trivial constraints and a) at least one decision variable is unbounded above and its corresponding cost is negative, or b) at least one decision variable is unbounded below and its corresponding cost is positive. '
nit: 0
slack: array([], dtype=float64)
status: 3
success: False
x: array([ 0., inf])

最佳答案

scipy.optimize.linprog(c, method='simplex', callback=None, options={'c0': None, 'A': None, 'b': None, 'postsolve_args': None, 'maxiter': 1000, 'tol': 1e-09, 'disp': False, 'bland': False}, x0=None)

Minimize a linear objective function subject to linear equality and non-negativity constraints


这意味着您在使用此界面时无法解决您的问题。阅读文档中的注释。
您应该使用顶级 linprog取而代之的是模块。由于您没有等式和不等式约束,您的 A_ub, b_ub, A_eq and b_eq矩阵是 None你只需要定义 bounds .
c = [1, -1]
x_bounds = [2, None]
y_bounds = [None, 3]
res = linprog(c, bounds=[x_bounds, y_bounds], method='simplex')

res
Out[]:
fun: -1.0
message: 'Optimization terminated successfully.'
nit: 2
slack: array([0., 0., 0., 0.])
status: 0
success: True
x: array([ 2., 3.])

关于python scipy linprog simplex example v 1.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62669305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com