0

I'm trying to solve a system of linear equations (5 variables on 11 different equations/restrictions).

The problem I'm having is that the solution I'm looking for can't be only equal since I'm trying to optimize for a budget, it has to be >= or <= but I'm not sure how write that in numpy.

The equations are as follow (only a few to simplify):

  • X/.07 >= 1728
  • Y/.02 >= 4431
  • Z/.05 >= 5409
  • A/.005 >= 7138
  • B/.01 >= 4897
  • A + B + X + Y + Z >= 100781

Ideally I'd like to include others like:

  • 9000 <= X <= 60000
  • 9000 <= Y <= 60000
  • 9000 <= Z <= 60000
  • 9000 <= A <= 60000
  • 9000 <= B <= 60000

But I'm not sure how to include them.

So far I Have

a = np.array([[1,0,0,0,0],
          [0,1,0,0,0],
          [0,0,1,0,0],
          [0,0,0,1,0],
          [0,0,0,0,1],
          [1,1,1,1,1]])

b = np.array([121204, 81069, 263260, 23541, 42964, 100781])

print(np.linalg.lstsq(a,b,rcond=None))

But clearly that's not what im looking for. (Notice b is simply the result of multiplying the right side by the denominator of the left hand)

Thank you!

Gorlomi
  • 515
  • 2
  • 11

2 Answers2

1

As @bg2b mentioned, you can use scipy.optimize.linprog. A very similar answer was given here Solve a system of linear equations and linear inequalities , with a step-by-step walkthrough for the function.

FBruzzesi
  • 6,385
  • 3
  • 15
  • 37
  • That's great but I don't see how to work that with 5 variables, any idea? – Gorlomi Dec 19 '19 at 15:04
  • Which variable are you optimizing for? I mean, you are looking for max or min? Is there any specific order (e.g. find the max for X, given that, the max for Y, etc..)? – FBruzzesi Dec 19 '19 at 16:53
  • this is for budget planning so ideally is maximizing all variables if possible, hence the 9000 <= # <= 60000 – Gorlomi Dec 20 '19 at 16:49
  • @Gorlomi I am sorry but this means nothing. Let's simplify the problem to see what I mean. Assume we have only 2 variables X and Y, with constraints 0.5 < X, Y < 1. And we also want X+Y < 1.2. What does it mean to maximize both X and Y? We can maximize their sum and get X+Y = 1.2, but this is still a line, hence there is no unique point to achieve that. On the other hand, let's say we want to maximize for Y, then we would get Y=0.7 and X=0.5 (otherwise they would not satisfy 0.5 < X, Y < 1). I hope the point and the reason for the previous comment is clear now. – FBruzzesi Dec 20 '19 at 19:13
0

It sounds like you may want linear programming (optimize a linear function subject to linear equality and inequality constraints), not a least squares solver.

See scipy.optimize.linprog: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linprog.html

bg2b
  • 1,939
  • 2
  • 11
  • 18
  • interesting! but how do I include 5 variables? – Gorlomi Dec 18 '19 at 18:44
  • 1
    Well, where the documentation there refers to x, imagine "a vector of 5 values representing [A, B, X, Y, Z]", basically the same idea as when expressing a simple system of linear equations in matrix form. As a simple example, the A_ub and b_ub mentioned there would be -a and -b from your little code snippet (negative because the optimizer constrains <= and you're wanting >=). Then you have to pick something to minimize (because there are infinitely many solutions). You might minimize A+B+X+Y+Z, in which case c=[1, 1, 1, 1, 1] – bg2b Dec 18 '19 at 18:57