Mathematical modeling__Linear programming Python implementation

I am using the python library scipy.

Insert image description here

'''
线性规划
'''

#目标函数的系数
# min z = 2x1+3x2-5x3
c = np.array([-2,-3,5])

#不等式限制条件的系数,转化为小于等于
# 2x1-5x2+x3 <= 10, x1+3x2+x3<=12
Aup = np.array([[-2,5,-1],[-1,-3,-1]])  #必须是二维
#右侧系数
bup = np.array([-10, 12])


#等式条件,左侧系数
# x1+x2+x3=7
Aeq = np.array([[1,1,1]]) #必须是二维
beq = np.array([7])

#x取值范围
x_bounds = [(0, None), (0, None), (0, None)]

res = optimize.linprog(c, A_ub=Aup, b_ub=bup, A_eq=Aeq, b_eq=beq, bounds=x_bounds)

If it is an integer planning, you only need to modify the parameters of the linprog function. (0-1 planning is a special integer planning, limiting the value range of x to 0,1). It seems that there are no equations in integer planning?

res = optimize.linprog(c, A_ub=Aup, b_ub=bup, bounds=x_bounds, method='simplex', 
              options={
    
    'disp': True, 'presolve': True})

Guess you like

Origin blog.csdn.net/xdg15294969271/article/details/132896518