线性规划 by Python

 设生产A产品x吨,B产品y吨,总经济价值为Z万元。  

目标函数:Z = 0.7x + 1.2y  

约束条件:  

9x + 4y <= 360  

4x + 5y <= 200  

3x + 10y <= 300  

Scipy中线性规划标准型:

from scipy.optimize import linprog


# 建模
c = [-0.7, -1.2]
A = [[9, 4], [4, 5], [3, 10]]
b = [360, 200, 300]
x1 = [0, None]
x2 = [0, None]
result =linprog(c, A, b, bounds=(x1, x2))
print(result)
print('\n可创造的最大经济价值为:', -(result.fun), '万元')
print('此时生产产品A、B分别为{}吨。'.format(result.x))
输出>>
message: Optimization terminated successfully. (HiGHS Status 7: Optimal)
        success: True
         status: 0
            fun: -42.79999999999998
              x: [ 2.000e+01  2.400e+01]
            nit: 3
          lower:  residual: [ 2.000e+01  2.400e+01]
                 marginals: [ 0.000e+00  0.000e+00]
          upper:  residual: [       inf        inf]
                 marginals: [ 0.000e+00  0.000e+00]
          eqlin:  residual: []
                 marginals: []
        ineqlin:  residual: [ 8.400e+01  0.000e+00  0.000e+00]
                 marginals: [-0.000e+00 -1.360e-01 -5.200e-02]
 mip_node_count: 0
 mip_dual_bound: 0.0
        mip_gap: 0.0

可创造的最大经济价值为: 42.79999999999998 万元
此时生产产品A、B分别为[20. 24.]吨。

假设各种下料方式分别采取x1、x2、x3、x4、x5次,所剩尾料为Z米。  

目标函数:minimize Z = 0 * x1 + 0.1 * x2 + 0.2 * x3 + 0.3 * x4 + 0.8 * x5  

约束条件:  

         1 * x1 + 2 * x2 + 0 * x3 + 1 * x4 + 0 * x5 = 100  

         0 * x1 + 0 * x2 + 2 * x3 + 2 * x4 + 1 * x5 = 100  

         3 * x1 + 1 * x2 + 2 * x3 + 0 * x4 + 3 * x5 = 100  

# 建模
c = [0, 0.1, 0.2, 0.3, 0.8]
Aeq = [[1, 2, 0, 1, 0], [0, 0, 2, 2, 1], [3, 1, 2, 0, 3]]
beq = [100, 100, 100]
x1 = [0, None]
x2 = [0, None]
x3 = [0, None]
x4 = [0, None]
x5 = [0, None]

result = linprog(c, A_eq=Aeq, b_eq=beq, bounds=(x1, x2, x3, x4, x5))
print(result)
print('\n剩下尾料最少为:', (result.fun), '米')
print('此时各种下料方式次数分别为{}。'.format(result.x))
输出>>        
message: Optimization terminated successfully. (HiGHS Status 7: Optimal)
        success: True
         status: 0
            fun: 16.0
              x: [ 0.000e+00  4.000e+01  3.000e+01  2.000e+01  0.000e+00]
            nit: 4
          lower:  residual: [ 0.000e+00  4.000e+01  3.000e+01  2.000e+01
                              0.000e+00]
                 marginals: [ 0.000e+00  0.000e+00  0.000e+00  0.000e+00
                              7.400e-01]
          upper:  residual: [       inf        inf        inf        inf
                                    inf]
                 marginals: [ 0.000e+00  0.000e+00  0.000e+00  0.000e+00
                              0.000e+00]
          eqlin:  residual: [ 0.000e+00  0.000e+00  0.000e+00]
                 marginals: [ 6.000e-02  1.200e-01 -2.000e-02]
        ineqlin:  residual: []
                 marginals: []
 mip_node_count: 0
 mip_dual_bound: 0.0
        mip_gap: 0.0

剩下尾料最少为: 16.0 米
此时各种下料方式次数分别为[ 0. 40. 30. 20.  0.]。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值