2

I have a large system of linear inequalities I am trying to solve for in python, ideally with non-negative integers.

Eg: enter image description here

I have a matrix A of size (1000*700) and matrix b of size (700) where each line in the system is set up such that A*C <= b. I am trying to solve for C. Multiple solutions online involve Scipy optimization module, however this requires an optimization function. Trying to put all 0's for the coefficients in the optimization function and inputting the constraints as the system of inequalities gives me a solution which is wrong. I have put down a part of my code below.

    from scipy.optimize import linprog
    coefficients_min_y = [0] * len(A[0])
    res = linprog(coefficients_min_y, A_ub=A, b_ub=b, bounds=(0, None))
    if res.success:
        coefficients = res.x
    else:
        print("Solution is infeasible")
    matrixMult = np.dot(A, coefficients)

Given that linprog outputs a feasible solution, I use matrixMult to check if the solution satisfies the conditions. However it is not solving correctly. Is this because of the optimization function or am I doing something else wrong.

Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32
  • 1
    If I understand correctly - you're assuming (or knowing) that there is a solution to the system of inequalities, and you want to find it? And what wrong result did you get? – Itamar Mushkin Mar 08 '20 at 07:01
  • 1
    Is it possible to provide a failing example? – Itamar Mushkin Mar 08 '20 at 07:49
  • 1
    The idea to use an LP solver looks good to me. I guess you have a bug, or may be you interpret results wrong. Non-negative integers would require a MIP solver. SciPy does not have one. – Erwin Kalvelagen Mar 08 '20 at 18:10
  • When I take the dot product between A and the possible solution, I get a vector which does not match the inequalities I put in. The original matrix is too big, however, my second inequality is: A[1] *c[1]>= 343, however in terms of the solver I put -A[1]*c[1]<=-343 and when multiplying it out, I have an answer of (when multiplying A[1] * c[1]: where c[1] is provided by the solver) -172814.63517024496. Writing this out, I might be misinterpreting the answer and need to the flip the sign. But if that is not the case, then isn't the wrong answer? – indispinablenorm Mar 08 '20 at 21:03
  • 1
    The best thing to do is to show a reproducible example. Without that, answering is largely guesswork. – Erwin Kalvelagen Mar 09 '20 at 09:07
  • Hey, I figured it out. I flipped the sign on the inequalities that were originally > but I had to change to <= in order to make it work with Scipy Optimization module. Again, flipping the sign in the matrix multiplication solution made it work. Thanks guys! – indispinablenorm Mar 09 '20 at 15:40
  • Hi, I dont know if this should be seperated to another post, but I wanted to know if there was a way to find the optimal minimization of solution coefficients that solve the problem. What I mean by this is given there are multiple solutions to the inequality, I want to find solution vector whose sum is the smallest . Ie: x0+x1+...xN -> is smallest. Is this possible to do with linprog? Is that what linprog already returns? – indispinablenorm Mar 25 '20 at 19:10
  • Setting `coefficients_min_y = [1] * len(A[0])` will give you`exactly that: a feasible solution where the sum x0+...+xN is minimized. – David Jun 05 '20 at 10:07

0 Answers0