简体   繁体   中英

How to get the result array of scipy.optimize.linprog?

I am using scipy.optimize.linprog to solve a network flow problems and I've got the result. But I don't know how to get the result array which is shown as x:array in the result. I have to modify this array to make it easier to read.

res = linprog(c=C, A_eq=A, b_eq=B)
print(res)

And this is result:

    fun: 67538.0
 message: 'Optimization terminated successfully.'
     nit: 36
   slack: array([], dtype=float64)
  status: 0
 success: True
       x: array([    0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,  5415.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,  7732.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,  4045.,
        3167.,     0.,  2420.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,  3257.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0., 12230.,     0.,     0.,
        1574.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0., 13407.,  4708.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,  4601.,     0.,  4982.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.])

So, I want know how to get x:array. I have to use non-zero numbers in this array.

As per scipy.optimize.linprog documentation this function returns OptimizeResult where x is one of properties (instance variables).

You can access it directly as any other object property in Python using res.x notation.

Note that x is NumPy array here so especially if your array is going to be big rather than converting to Python native list and removing zeroes manually you might want to use nonzero_x = numpy.trim_zeros(res.x) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM