3

I have a system of linear equations that I already reduced to a row echelon matrix using Gauss-Jordan elimination. My system with n variables Xn (where Xn is in N0 (=positive integers)) has multiple solutions and I want to find the solution for witch the Sum of all Xn is minimal.

How could I do that programmatically?

For example consider this system of linear equations:

x1 +              + x5 + x6 = 2
     x2           + x5      = 1       
          x3           + x6 = 1
               x4 + x5 + x6 = 1

one of the minimal solution I want to obtain is:

x3 = x4 = x5 = 0
x1 = x2 = x6 = 1 

another one would be

  x2 = x4 = x6 = 0
  x1 = x3 = x5 = 1

But I don't want

 x1 = 2
 x2 = x3 = x4 = 1
 x5 = x6 = 0

which is also a solution of this system, but not a minimal one according to my criteria as x1 + x2 + x3 + x4 + x5 + x6 = 5 (whereas it is only 3 for the 2 first solutions)

In case of multiple minimal solutions (like here, where solutions 1 and 2 are both minimal), I don't care about the minimal solution that is returned as long as it is one of the minimal ones

Thomas Bernard
  • 333
  • 3
  • 10
  • Are the variable required to be nonnegative? Since there are solutions with different sums, it follows that the achievable sums are not bounded below. – David Eisenstat Feb 07 '17 at 21:35
  • Yes the variables are non negatives. They belong to the set of positive integers {0,1,2,...} – Thomas Bernard Feb 07 '17 at 21:37
  • 1
    So how many variables are there in your instances? How many equations? – sascha Feb 07 '17 at 22:04
  • up to a hundred variables with as many equations (but some of these equations might be identical so I may have slightly less equations than variables after the Gauss-Jordan elimination procedure) – Thomas Bernard Feb 08 '17 at 05:06

1 Answers1

1

Since the variables are all nonnegative, this problem is essentially equivalent to integer programming. Use an off-the-shelf integer program solver and formulate like

minimize x1 + x2 + x3 + x4 + x5 + x6
subject to
x1                + x5 + x6 = 2
     x2           + x5      = 1
          x3           + x6 = 1
               x4 + x5 + x6 = 1
integers
x1, x2, x3, x4, x5, x6 >= 0

(exact syntax depends on the tool).

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120
  • Well I don't want to use an external tool. I need to program this solver myself (to integrate it into a game I am programming). That was the goal of my question... – Thomas Bernard Feb 07 '17 at 21:50
  • 2
    @ThomasBernard: There are linear programming packages for many languages: you could integrate one into your game. If you really want to program it yourself, search for [simplex method or simplex algorithm](https://en.wikipedia.org/wiki/Simplex_algorithm) and implement it. – Rory Daulton Feb 07 '17 at 21:52
  • @RoryDaulton Simplex is not enough and Simplex is one of the thoughest programming-tasks there is (if performance is important; and we don't know the size of his instances). – sascha Feb 07 '17 at 21:54
  • @ThomasBernard Simplex + depth-first search with best-first backtracking can be done in a few hundred lines, but the performance will be much worse than a well-implemented library. Perhaps your instances will be small enough that it doesn't matter. – David Eisenstat Feb 07 '17 at 21:56
  • My game is written in Haxe langage and I doubt there is any existing simplex solver library in Haxe... As for my instances, there should be a hundred variables at most. – Thomas Bernard Feb 07 '17 at 23:19