简体   繁体   English

以下LP代码在哪里出错?

[英]Where am I going wrong in the following LP code?

I am trying to solve an LP problem with two variables with two constraints where one is inequality and the other one is equality constraint in Scipy. 我试图用两个约束来解决两个变量的LP问题,其中一个是不等式,另一个是Scipy中的等式约束。 To convert the inequality in the constraint I have added another variable in it called A . 为了转换约束中的不等式,我在其中添加了另一个变量A

Min(z) = 80x + 60y

Constraints: 限制条件:

0.2x + 0.32y <= 0.25
x + y = 1
x, y <= 0

I have changed the inequality constraints by the following equations by adding an extra variable A 我通过添加额外的变量A通过以下方程式更改了不平等约束

0.2x + 0.32y + A = 0.25
Min(z) = 80x + 60y + 0A
X+ Y + 0A = 1

from scipy.optimize import linprog
import numpy as np

z = np.array([80, 60, 0])
C = np.array([
[0.2, 0.32, 1],
[1, 1, 0]
])
b = np.array([0.25, 1])
x1 = (0, None)
x2 = (0, None)
sol = linprog(-z, A_eq = C, b_eq = b, bounds = (x1, x2), method='simplex')

However, I am getting an error message 但是,我收到一条错误消息

Invalid input for linprog with method = 'simplex'. 方法='simplex'的linprog输入无效。 Length of bounds is inconsistent with the length of c 边界长度与c的长度不一致

How can I fix this? 我怎样才能解决这个问题?

The problem is that you do not provide bounds for A . 问题是您不为A提供边界。 If you eg run 如果你跑

linprog(-z, A_eq = C, b_eq = b, bounds = (x1, x2, (0, None)), method='simplex')

you will obtain: 您将获得:

     con: array([0., 0.])
     fun: -80.0
 message: 'Optimization terminated successfully.'
     nit: 3
   slack: array([], dtype=float64)
  status: 0
 success: True
       x: array([1.  , 0.  , 0.05])

As you can see, the constraints are met: 如您所见,满足了约束:

0.2 * 1 + 0.32 * 0.0 + 0.05 = 0.25  # (0.2x + 0.32y + A = 0.25)

and also 并且

1 + 0 + 0 = 1  # (X + Y + 0A = 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM