【问题标题】:(MatLab) Problem is unbounded when doing the linear programming in matlab(MatLab)在matlab中进行线性规划时问题是无限的
【发布时间】:2022-01-15 18:59:17
【问题描述】:
x + y ≤ 44,
2x + y ≤ 64,
9,000x + 5,000y ≤ 300,000

目标函数30,000x + 20,000y

我想在 Matlab

中找到最优解

但是有错误信息Problem is unbounded.

这是我的代码

A = [1 1;2 1;9 5];

b = [44 64 300];

f = [3 2];

x = linprog(f,A,b)

假设答案:x=20,y=24

参考 https://www.mathworks.com/help/optim/ug/linprog.html

【问题讨论】:

  • 因为 linprog 最小化而你想最大化。你应该否定 f。

标签: matlab linear-programming


【解决方案1】:

这不是您问题的直接答案,但您遇到困难的一个原因是您使用的是 Matlab。其他语言会提供更有表达力和更容易理解的方式来表达您的问题。

以 Python 为例,再加上 cvxpy 给我们:

#!/usr/bin/env python3

import cvxpy as cp

x = cp.Variable()
y = cp.Variable()

constraints = [
  x + y <= 44,
  2 * x + y <= 64
]

objective = cp.Maximize(9000*x + 5000*y)

prob = cp.Problem(objective, constraints)
N
optimum_value = prob.solve()

print("optimum value", optimum_value)
print("x", x.value)
print("y", y.value)

代码可读性强,模型很容易扩展到非线性区域,例如二次规划和二阶锥。

返回

optimum value 300000.00000076543
x 20.00000000075882
y 23.999999998787203

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    相关资源
    最近更新 更多