Python solves linear programming problems, including examples using scipy.linprog

from scipy.optimize import linprog

scipy official documentation for linprog function

scipy.optimize.linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None, method='highs', callback=None, options=None, x0=None, integrality=None)

Linear programming solves problems of the following form:

Parameters:

c:1-D array

A_ub:2-D array, optional

b_ub:1-D array, optional

A_eq: 2-D array, optional

b_eq: 1-D array, optional

boundssequence, optional

x: 1-D array

Below we use an example to try to use this function to solve linear programming problems.

image-20230831142625576

image-20230831142633450

c=[-70, -50, -60] 
A_ub = [[2, 4, 3], [3, 1, 5], [7, 3, 5]] 
b_ub = [150, 160, 200]
result=linprog(c,A_ub=A_ub,b_ub=b_ub,bounds=[(0,None),(0,None),(0,None)],method='highs')
result

        message: Optimization terminated successfully. (HiGHS Status 7: Optimal)
        success: True
         status: 0
            fun: -2590.909090909091
              x: [ 2.121e+00  1.576e+01  2.758e+01]
            nit: 3
          lower:  residual: [ 2.121e+00  1.576e+01  2.758e+01]
                 marginals: [ 0.000e+00  0.000e+00  0.000e+00]
          upper:  residual: [       inf        inf        inf]
                 marginals: [ 0.000e+00  0.000e+00  0.000e+00]
          eqlin:  residual: []
                 marginals: []
        ineqlin:  residual: [ 0.000e+00  0.000e+00  0.000e+00]
                 marginals: [-6.364e+00 -0.000e+00 -8.182e+00]
 mip_node_count: 0
 mip_dual_bound: 0.0
        mip_gap: 0.0

The target solution obtained is consistent with the answer, but the value of the optimal solution of x is different, and there may be multiple feasible solutions.

Guess you like

Origin blog.csdn.net/weixin_61720360/article/details/132602539