0

Consider the following equations:

X > Y  

X + Y > 7  

Y <= 10  

X >= 0  

Y >= 0  

I want to find out if there exists a solution that fulfills all of them (natural numbers).

I don't care about the exact solution, I just want to know if there is a solution at all

I have read about Microsoft Solver Foundation or other linear programming libraries, but I'm not sure if they can solve problems like this.

Especially I'm not sure if the can solve equations with variables on each side, like

X > Y, or X + Y > Z  

most examples are of the form:

X * 10 + Y * 30 > constant  

I need it to be able to solve systems with maximum of 4-8 variables, all in range of 0-100

Another important constraint I have, the library needs to be fast. I need to be able to solve systems of like 7 equations in like 0,00001 seconds

Deduplicator
  • 44,692
  • 7
  • 66
  • 118

1 Answers1

0

Interesting question. Feels a lot like the integer-knapsack problem.

First of all, whether variables are on each side is irrelevant, since an equation like

X + Y > Z

can be rewritten to

X + Y - Z > 0

So let's assume that all constraints are of the format

(const1 * var1) + ... + (const8 * var8) > const

To support less variables, just use the value 0 for one of the constants.

The way to visualize this is to see the case of 2 variables as determining the convex hull of the 'lines' corresponding to the constraints. So each constraint can be drawn as a 2D line, and only values on one side of the line are allowed.

To visualize this for 3 variables, it's the same as whether the convex hull of 'planes' determined by the constraint have any grid points ('natural numbers') in them.

The trouble in this case is the fact that the solution should have only natural numbers: this makes normal linear algebra impossible, since a grid is imposed. I would not know of any library supporting such restrictions.

But it would not be too difficult to write a solution yourself: the idea is to find a solution by trying every number by pruning aggressively.

So in your example: test all X in the range 0 to 100. Now go to the next variable, and determine the valid range for the free variable based on the constraints. Worked out for x == 8: then the range for y would be:

  • 0 .. 7 because of constraint x > y
  • 0 .. 100 because of constraint x + y > 7 (since x is already 8)
  • 0 .. 9 because of constraint y < 10

...and we repeat this for all constraints. The final constraint for y is then 0 .. 7, because that is the most tight constraint. Now repeat this process for the left-over unbound variables, and you're done if you find at least one solution.

I expect this code to be about 100 lines with dynamic programming; computation time very much depends on the input and vary wildly.

For example, a set of equations which would take a long time:

A + B + C + D + E + F + G + H > 400.5
A + B + C + D + E + F + G + H < 400.6

As a human we can deduce that since we're requiring natural numbers, there is no solution to these equations. However, this solution is not prunable using the method described above, all combinations of A .. G will have to be tested before it will be concluded that there is no fitting H. Therefore it will look at about all possibilities. Not really pleasant, but unavoidable.

Rutger Nijlunsing
  • 4,861
  • 1
  • 21
  • 24
  • I tried brute force but sometimes there are up to 8 variables in the range 0-120... and brute force cant solve these problems.. woudl require 120 ^ 8 tries in the worst case – user1803928 May 23 '14 at 16:01
  • This problem feels like it is equivalent to 'integer knapsack' problem, which is a hard problem (see for example https://web.cs.ship.edu/~tbriggs/dynamic/index.html ). Therefore, worstcase will be the 120^8 tries, no way around it. With aggressive pruning (as suggested) you can only bring down the complexity in 'a lot of cases', but not all. Btw: do you have a complex example? – Rutger Nijlunsing May 24 '14 at 06:31
  • i work at a company that makes sports betting software. one problem is to find out the maximum possible winnings of a bet. for example you could bet on X wins 3:1, more than 2 goals, score of the first period 0:1, odd number of goals.. and so on. to solve for maximum winnings, you need to find out which bets can "win together". for example you can win both 2:1 and more than 2 goals, but you cant win 2:1 and more than 3 goals. our first idea was to solve it with sets, but for some sports with lot of periods, the sets just get way too big. – user1803928 May 25 '14 at 17:17
  • for example ice hockey has 3 periods, so that would be 6 variables, P1A,P1B,P2A,P2B,P3A,P3B (number of goals in the corresponding period). teamA wins would mean P1A+P2A+P3A > P1B+P2B+P3B – user1803928 May 25 '14 at 17:21
  • the 120^8 number was from basketball, you have 4 periods and in each periods there can be up to 120 points for a team (or even more). – user1803928 May 25 '14 at 17:41