3

I am working on a programming (using Python) problem where I have to solve the following type of linear equation in 3 variables:

x, y, z are all integers.

Equation example: 2x + 5y + 8z = 14

Condition: Minimize x + y + z

I have been trying to search for an algorithm for finding a solution to this, in an optimum way. If anybody has any idea please guide me through algorithm or code-sources.

I am just curious, what can be done if this problem is extrapolated to n variables?

I don't want to use hit & trial loops to keep checking for values. Also, there may be a scenario that equation has no solution.

UPDATE

Adding lower bounds condition:

x, y, z >= 0
x, y, z are natural
Rodrigo de Azevedo
  • 1,097
  • 9
  • 17
n.nasa
  • 551
  • 2
  • 5
  • 21
  • 6
    As has been pointed out in the answer below that problem is unbounded unless you have bounds on your variables `x, y, z`. It is common in some problems to have a lower bound of zero for all variables, but you have not specified this. In general for linear programming problems in Python I would recommend [PuLP](https://pypi.python.org/pypi/PuLP). – kabdulla Oct 05 '16 at 08:28
  • Maybe it sounds non-sense, does (x,y,z) needed to be an integer solution? – shole Oct 05 '16 at 08:45
  • Yes, looking for integral solutions only. – n.nasa Oct 05 '16 at 08:48
  • I added Update2 above for what I found while finding a programming approach to algorithm. All the links which helped are shared, thanks all for helping out! – n.nasa Oct 07 '16 at 09:06

5 Answers5

3

Any triple (x, y, z), with z = (14 - 2x - 5y) / 8, satisfies your constraint.

Note that x + y + (14 - 2x - 5y) / 8 is unbounded from below. This function decreases when each of x and y decrease, with no finite minimum.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • Thanks, it looks like the best mathematical solution. But, I was actually looking to solve this problem programmatically. I found `coin-change algorithm` which I can use with such kinds of problems I guess. – n.nasa Oct 05 '16 at 13:56
3

You have an equality-constrained integer program (IP) in just 3 dimensions. The equality constraint 2 x + 5 y + 8 z = 14 defines a plane in 3-dimensional space. Parametrizing it,

x = 7 - 2.5 u - 4 v
y = u
z = v 

we obtain an unconstrained IP in 2 dimensions. Given the integrality constraints, we have u <- {0,2} and v <- {0,1}. Enumerating all four (u,v) pairs, we conclude that the minimum is 4 and that it is attained at (u,v) = (2,0) and (u,v) = (0,1), which correspond to (x,y,z) = (2,2,0) and (x,y,z) = (3,0,1), respectively.

Using PuLP to solve the integer program:

from pulp import *

# decision variables
x = LpVariable("x", 0, None, LpInteger)
y = LpVariable("y", 0, None, LpInteger)
z = LpVariable("z", 0, None, LpInteger)

# define integer program (IP)
prob = LpProblem("problem", LpMinimize)
prob += x+y+z                   # objective function
prob += 2*x + 5*y + 8*z == 14   # equality constraint

# solve IP
prob.solve()

# print results
print LpStatus[prob.status]
print value(x)
print value(y)
print value(z)

which produces x = 3, y = 0 and z = 1.

Rodrigo de Azevedo
  • 1,097
  • 9
  • 17
1

Another tool to solve this type of problems is SCIP. There is also an easy to use Python interface available on GitHub: PySCIPOpt.

In general (mixed) integer programming problems are very hard to solve (NP complexity) and often even simple looking instances with only a few variables and constraints can take hours to prove the optimal solution.

mattmilten
  • 6,242
  • 3
  • 35
  • 65
0

From your first equation:

x = (14 - 5y - 8x) / 2

so, you now only need to minimize

(14 - 5y - 8z) / 2 + y + z

which is

(14 - 3y - 6z) / 2

But we can ignore the ' / 2' part for minimization purposes.

Presumably, there must be some other constraints on your problem, since as described the solution is that both y and z may grow without bound.

Dave Branton
  • 494
  • 3
  • 11
0

I do not know any general fast solution for n variables, or not using hit & trail loops. But for the given specific equation 2x + 5y + 8z = 14, there maybe some shortcut based on observation.

Notice that the range is very small for any possible solutions:

0<=x<=7, 0<=y<=2, 0<=z<=1

Also other than x = 7, you have at least to use 2 variables. (x+y+z = 7 for this case)


Let's find what we got if using only 2 variables:

If you choose to use (x,z) or (y,z), as z can only be 1, x or y is trivial.

(x+y+z = 4 for (x,z), no solution for (y,z))

If you choose to use (x,y), as x's coefficient is even and y's coefficient is odd, you must choose even number of y to achieve an even R.H.S. (14). Which means y must be 2, x is then trivial.

(x+y+z = 4 for this case)


Let's find what we got if using all 3 variables:

Similarly, z must be 1, so basically it's using 2 variables (x,y) to achieve 14-8 = 6 which is even.

Again we use similar argument, so we must choose even number of y which is 2, however at this point 2y + 1z > 14 already, which means there is no solution using all 3 variables.


Therefore simply by logic, reduce the equation by using 1 or 2 variables, we can find that minimum x+y+z is 4 to achieve 14 (x=3,y=0,z=1 or x=2,y=2,z=0)

shole
  • 4,046
  • 2
  • 29
  • 69