0

I am trying to solve a problem using scipy's linprog function, but running the program I get the following error:

File "C:\...\anaconda3\lib\site-packages\scipy\optimize\_linprog_util.py", line 339, in _clean_inputs elif (len(bounds) == 2 and np.isreal(bounds[0])

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

My doubt is that for the configuration of my matrices in MATLAB I do get results, so I don't know the origin of this error.

The line of code where I call the linprog function looks like this:

from scipy.optimize import linprog
res = linprog(c=f,A_ub=Aineq,b_ub=Bineq,A_eq=Aeq,b_eq=Beq,bounds=[lb,ub],method='interior-point')

I would appreciate any information you could give me about the possible cause of the error.

The all matrices of the optimization problem are shown below:

f= matrix([[7.8, 7.8, 6.4, 6.4, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]])

Aineq= matrix([[  0.        ,   0.        ,   0.        ,   0.        ,-6.66666667,   6.66666667,   0.        ,   0.        , 0.        ,   0.        ,   0.        ,   0.        , 0.        ,   0.        ,   0.        ,   0.        ],
           [  0.        ,   0.        ,   0.        ,   0.        ,-5.        ,   0.        ,   5.        ,   0.        , 0.        ,   0.        ,   0.        ,   0.        ,  0.        ,   0.        ,   0.        ,   0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        , 0.        ,  -4.        ,   4.        ,   0.        , 0.        ,   0.        ,   0.        ,   0.        , 0.        ,   0.        ,   0.        ,   0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,0.        ,   0.        ,   0.        ,  -6.66666667, 6.66666667,   0.        ,   0.        ,   0.        , 0.        ,   0.        ,   0.        ,   0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        , 0.        ,   0.        ,   0.        ,  -5.        ,0.        ,   5.        ,   0.        ,   0.        , 0.        ,   0.        ,   0.        ,   0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,0.        ,   0.        ,   0.        ,   0.        , -4.        ,   4.        ,   0.        ,   0.        ,0.        ,   0.        ,   0.        ,   0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        , 0.        ,   0.        , -13.33333333,   0.        , 0.        ,   0.        ,  13.33333333,   0.        , 0.        ,   0.        ,   0.        ,   0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,0.        ,   0.        ,   0.        ,   0.        ,0.        , -13.33333333,   0.        ,  13.33333333,0.        ,   0.        ,   0.        ,   0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,   0.        ,   0.        ,   0.        ,   0.        ,0.        ,   0.        , -13.33333333,   0.        ,13.33333333,   0.        ,   0.        ,   0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,0.        ,   0.        ,   0.        ,   0.        , 0.        ,   0.        ,   0.        , -13.33333333, 0.        ,  13.33333333,   0.        ,   0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,0.        ,   0.        ,   0.        ,   0.        , 0.        ,   0.        ,   0.        ,   0.        , 0.        ,   0.        , -15.98721023,  15.98721023],
            [  0.        ,   0.        ,   0.        ,   0.        ,6.66666667,  -6.66666667,  -0.        ,  -0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,5.        ,  -0.        ,  -5.        ,  -0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        , -0.        ,   4.        ,  -4.        ,  -0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,-0.        ,  -0.        ,  -0.        ,   6.66666667,-6.66666667,  -0.        ,  -0.        ,  -0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,-0.        ,  -0.        ,  -0.        ,   5.        ,-0.        ,  -5.        ,  -0.        ,  -0.        ,  -0.        ,  -0.        ,  -0.        ,  -0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        ,  4.        ,  -4.        ,  -0.        ,  -0.        , -0.        ,  -0.        ,  -0.        ,  -0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,-0.        ,  -0.        ,  13.33333333,  -0.        , -0.        ,  -0.        , -13.33333333,  -0.        , -0.        ,  -0.        ,  -0.        ,  -0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        , -0.        ,  -0.        ,  -0.        ,  -0.        ,  -0.        ,  13.33333333,  -0.        , -13.33333333, -0.        ,  -0.        ,  -0.        ,  -0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        ,-0.        ,  -0.        ,  13.33333333,  -0.        ,-13.33333333,  -0.        ,  -0.        ,  -0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        , -0.        ,  -0.        ,  -0.        ,  -0.        ,-0.        ,  -0.        ,  -0.        ,  13.33333333, -0.        , -13.33333333,  -0.        ,  -0.        ],
            [  0.        ,   0.        ,   0.        ,   0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        ,-0.        ,  -0.        ,  15.98721023, -15.98721023]])
  
 
Bineq= matrix([[2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5,2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5]])

Aeq= matrix([[  1.        ,   0.        ,   0.        ,   0.        , 11.66666667,  -6.66666667,  -5.        ,  -0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        , -0.        ,  -0.        ,   0.        ,   0.        ],
          [  0.        ,   1.        ,   0.        ,   0.        , -6.66666667,  10.66666667,  -4.        ,  -0.        , -0.        ,  -0.        ,  -0.        ,  -0.        ,  -0.        ,  -0.        ,   0.        ,   0.        ],
          [  0.        ,   0.        ,   0.        ,   0.        ,  -5.        ,  -4.        ,  22.33333333,  -0.        ,  -0.        ,  -0.        , -13.33333333,  -0.        ,  -0.        ,  -0.        ,   0.        ,   0.        ],
          [  0.        ,   0.        ,   1.        ,   0.        , -0.        ,  -0.        ,  -0.        ,  11.66666667, -6.66666667,  -5.        ,  -0.        ,  -0.        , -0.        ,  -0.        ,   0.        ,   0.        ],
          [  0.        ,   0.        ,   0.        ,   1.        , -0.        ,  -0.        ,  -0.        ,  -6.66666667, 10.66666667,  -4.        ,  -0.        ,  -0.        , -0.        ,  -0.        ,   0.        ,   0.        ],
          [  0.        ,   0.        ,   0.        ,   0.        ,-0.        ,  -0.        ,  -0.        ,  -5.        , -4.        ,  22.33333333,  -0.        , -13.33333333, -0.        ,  -0.        ,   0.        ,   0.        ],
           [  0.        ,   0.        ,   0.        ,   0.        ,  -0.        ,  -0.        , -13.33333333,  -0.        ,  -0.        ,  -0.        ,  26.66666667,  -0.        , -13.33333333,  -0.        ,   0.        ,   0.        ],
           [  0.        ,   0.        ,   0.        ,   0.        , -0.        ,  -0.        ,  -0.        ,  -0.        , -0.        , -13.33333333,  -0.        ,  26.66666667, -0.        , -13.33333333,   0.        ,   0.        ],
           [  0.        ,   0.        ,   0.        ,   0.        , -0.        ,  -0.        ,  -0.        ,  -0.        , -0.        ,  -0.        , -13.33333333,  -0.        , 13.33333333,  -0.        ,  15.98721023, -15.98721023],
           [  0.        ,   0.        ,   0.        ,   0.        ,-0.        ,  -0.        ,  -0.        ,  -0.        , -0.        ,  -0.        ,  -0.        , -13.33333333,  -0.        ,  13.33333333, -15.98721023,  15.98721023],
           [  0.        ,   0.        ,   0.        ,   0.        ,0.        ,   0.        ,   0.        ,   0.        , 0.        ,   0.        ,   0.        ,  13.33333333, 0.        , -13.33333333,   0.        ,   0.        ]])

Beq= matrix([[ 0. ,  0. ,  1.5,  0. ,  0. ,  1.5,  0. ,  0. ,  0. ,  0. ,-1. ]])

lb= matrix([[ 0.1       ,  0.1       ,  0.1       ,  0.1       ,  0.        ,-6.28318531, -6.28318531,  0.        , -6.28318531, -6.28318531, -6.28318531, -6.28318531, -6.28318531, -6.28318531,  2.        ,0.        ]]
 
ub= matrix([[2.5       , 2.5       , 2.5       , 2.5       , 0.        ,6.28318531, 6.28318531, 0.        , 6.28318531, 6.28318531, 6.28318531, 6.28318531, 6.28318531, 6.28318531, 2.        ,inf]])
LEO HDZ
  • 21
  • 4
  • 1
    Please read this short [note](https://numpy.org/doc/1.18/reference/generated/numpy.matrix.html#numpy.matrix): *don't use np.matrix* and then [linprog docs](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linprog.html) which say, among other things, that `Bineq` is 1d. Maybe also consult [NumPy for Matlab users](https://numpy.org/doc/stable/user/numpy-for-matlab-users.html). There are tons of differences and you won't see much care in regards to column vs. row-vectors in numpy-code due to broadcasting rules. – sascha Jul 08 '20 at 09:49
  • Does this answer your question? [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – Trenton McKinney Aug 11 '20 at 18:06

0 Answers0