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.
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
The printout from that code is
Minimum value of y = 43.3333333333
Maximum value of y = 51.1111111111
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]
.
y
. Do a search on linear programming in Python, and if you have a specific problem implementing it let us know. You could start with scipy.optimize.linprog. Did you look into that when you looked at scipy? – Allanadale