问题
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
回答1:
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.
回答2:
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
.
回答3:
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.
回答4:
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.
回答5:
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)
来源:https://stackoverflow.com/questions/39868762/minimizing-the-sum-of-3-variables-subject-to-equality-and-integrality-constraint