解决线性方程组和线性不等式 [英] Solve a system of linear equations and linear inequalities

查看:297
本文介绍了解决线性方程组和线性不等式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须获取线性表达式的最小值和最大值y,但受python中一些线性不等式的限制.

I have to get the min and max y for a linear expression, restricted by some linear inequalities in python.

您可以在这里看到我输入Desmos的方程式和不等式:

You can see the equation and inequalities here that I have entered into Desmos:

3x+12y = 1000
x > 30
x < 160
y < 60
y > 10
x + y > 180

我可以通过画出并消除不等式来手工解决它们.但是我无法在Python中做到这一点. 到目前为止,我在Python中尝试过的是当x = 0时得到y = 83.33;当y = 0时x = 333.33; 得到最小和最大x,y后,我将不等式1乘以1.但是对于每个不等式,我都必须加上前面的不等式,并且还要检查x或y是否已超出一定范围并且到目前为止,它几乎是确定我会错过支票.

I can solve them by hand by drawing and crossing out the inequalities. But I cannot do that in Python. What I have tried so far in Python is to get y=83.33 when x=0; x=333.33 when y=0; After getting the min and max x,y I then apply the inequalities 1 by 1. But with every inequality I have to add the previous ones, and also do check if x or y has gone over certain range and so far and it is almost certain I will miss a check.

我查看了numpy和sympy,但无法弄清楚如何使用它们解决此问题.您能建议我什么/如何使用以获得白色箭头在图片上显示的范围?

I looked at numpy and sympy, but could not figure out how to solve this using them. Can you suggest what / how can I use in order to get the range which the white arrow shows on the picture?

推荐答案

在线性编程中,您是一个问题,其中等式和不等式是限制,您想最小化(然后最大化)表达式y.等式,不等式和表达式都是线性的,因此使其成为线性编程.使用scipy.optimize.linprog函数的scipy程序包可以执行这种线性编程.

Yours is a problem in linear programming, where your equality and inequalities are the limitations and you want to minimize (then later maximize) the expression y. The equality, inequalities, and expression are all linear, so that makes it linear programming. The scipy package, using the scipy.optimize.linprog function, can do this kind of linear programming.

以下是注释代码,可以执行您想要的操作.请注意,所有不等式都作了些微更改,以包括等式,这对于具有最大或最小值y是必需的.为了找到y的最大值,该代码改为找到-y的最小值,然后打印该值的加法逆,因为linprog最小化了目标函数.最后,在linprog中不等式限制必须小于或等于",因此我将不等式x + y > 180的两边乘以-1得到一个,即-x + -y <= -180.询问您是否有任何疑问.

Here is commented code to do what you want. Note that all the inequalities were slightly changed to include equality, which is necessary to have a maximum or minimum value of y. To find the maximum value of y the code instead finds the minimum value of -y then prints the additive inverse of that, since linprog minimizes the objective function. Finally, the inequality restrictions must be "less than or equal to" in linprog, so I multiplied both sides of your inequality x + y > 180 by -1 to get one, namely -x + -y <= -180. Ask if you have any questions.

from scipy.optimize import linprog

# Set up values relating to both minimum and maximum values of y
coefficients_inequalities = [[-1, -1]]  # require -1*x + -1*y <= -180
constants_inequalities = [-180]
coefficients_equalities = [[3, 12]]  # require 3*x + 12*y = 1000
constants_equalities = [1000]
bounds_x = (30, 160)  # require 30 <= x <= 160
bounds_y = (10, 60)  # require 10 <= y <= 60

# Find and print the minimal value of y
coefficients_min_y = [0, 1]  # minimize 0*x + 1*y
res = linprog(coefficients_min_y,
              A_ub=coefficients_inequalities,
              b_ub=constants_inequalities,
              A_eq=coefficients_equalities,
              b_eq=constants_equalities,
              bounds=(bounds_x, bounds_y))
print('Minimum value of y =', res.fun)

# Find and print the maximal value of y = minimal value of -y
coefficients_max_y = [0, -1]  # minimize 0*x + -1*y
res = linprog(coefficients_max_y,
              A_ub=coefficients_inequalities,
              b_ub=constants_inequalities,
              A_eq=coefficients_equalities,
              b_eq=constants_equalities,
              bounds=(bounds_x, bounds_y))
print('Maximum value of y =', -res.fun)  # opposite of value of -y

该代码的打印输出为

Minimum value of y = 43.3333333333
Maximum value of y = 51.1111111111

在浮点精度范围内是正确的.如果需要相应的x值,请参见res.x的值,该值是在所需点给出xy的值的数组-xres.x[0]yres.x[1].

which is correct within the precision of floating point. If you need the corresponding values of x, see the value of res.x which is an array that gives the values of both x and y at the desired point--x is res.x[0] and y is res.x[1].

这篇关于解决线性方程组和线性不等式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆