Python线性规划

标准形式

m i n i m i z e    c T x s u b j e c t   t o    A x = b    s u b j e c t   t o    x ⩾ 0 w h e r e   c ∈ R n   ,   b ∈ R m   ,   A ∈ R m × n \mathrm{minimize} \ \ \mathbf{c}^\mathsf{T} \mathbf{x} \\ \mathrm{subject \ to} \ \ \mathbf{Ax} = \mathbf{b} \\ \ \ \phantom{\mathrm{subject \ to} \ \ } \mathbf{x} \geqslant \mathbf{0} \\ where \ \mathbf{c} \in \mathbb{R}^n \ , \ \mathbf{b} \in \mathbb{R}^m \ , \ \mathbf{A} \in \mathbb{R}^{m \times n} minimize  cTxsubject to  Ax=b  subject to  x0where cRn , bRm , ARm×n

图解法

考虑问题:
max ⁡   c T x s . t .    A x ⩽ b    s . t .    x ⩾ 0 w h e r e   x = [ x 1 ,   x 2 ] T   a n d c T = [ 1 ,   5 ] , A = [ 5    6 3    2 ] ,    b = [ 30 12 ] \max \ \mathbf{c}^\mathsf{T} \mathbf{x} \\ \mathrm{s.t.} \ \ \mathbf{Ax} \leqslant \mathbf{b} \\ \ \ \phantom{\mathrm{s.t.} \ \ } \mathbf{x} \geqslant \mathbf{0} \\[8pt] where \ \mathbf{x} = [x_1 , \ x_2]^{\mathsf{T}} \ and \\[6pt] \mathbf{c}^{\mathsf{T}} = [1, \ 5] , \\[6pt] \mathbf{A} = \begin{bmatrix} 5 \ \ 6 \\ 3 \ \ 2 \end{bmatrix} , \ \ \mathbf{b} = \begin{bmatrix} 30 \\ 12 \end{bmatrix} max cTxs.t.  Axb  s.t.  x0where x=[x1, x2]T andcT=[1, 5],A=[5  63  2],  b=[3012]
可行域:
在这里插入图片描述绘图代码:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0.0, 8.0, 50)
y1 = (-5 / 6) * x + 5
y2 = (-3 / 2) * x + 6
y = np.minimum(y1, y2)

upper = (-1 / 5) * x + 5
lower = (-1 / 5) * x

plt.fill_between(x, 0, y, where=y >= 0, facecolor='blue', alpha=0.5)
plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--', label=r'$5x_1 + 6x_2 = 30$')
plt.plot(x, y2, color='orange', linewidth=1.0, linestyle='--', label=r'$3x_1 + 2x_2 = 12$')
plt.plot(x, upper, color='black', linewidth=1.5, label=r'$upper = x_1 + 5x_2 = 25$')
plt.plot(x, lower, color='black', linewidth=1.5, label=r'$lower = x_1 + 5x_2 = 0$')

ax = plt.gca()  # get the current Axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))

plt.xlim((-1.0, 6.0))
plt.ylim((-1.0, 8.0))
plt.xlabel(r'$x_1$')
plt.ylabel(r'$x_2$')
plt.legend(fontsize=14)
plt.show()

软件求解(scipy)

标准形式:

min ⁡   c T x s u c h   t h a t    A u b ⩽ b u b , s u c h   t h a t A e q x = b e q , s u c h   t h a t l ⩽ x ⩽ u \min \ \mathbf{c}^{\mathsf{T}} \mathbf{x} \\ \mathrm{such \ that} \ \ \mathbf{A}_{ub} \leqslant \mathbf{b}_{ub} , \\ \phantom{\mathrm{such \ that}} \mathbf{A}_{eq} \mathbf{x} = \mathbf{b}_{eq} , \\ \phantom{\mathrm{such \ that}} \mathbf{l} \leqslant \mathbf{x} \leqslant \mathbf{u} min cTxsuch that  Aubbub,such thatAeqx=beq,such thatlxu

x \mathbf{x} x 是决策变量的向量形式
c ,   b u b ,   b e q ,   l ,   u \mathbf{c}, \ \mathbf{b}_{ub}, \ \mathbf{b}_{eq}, \ \mathbf{l}, \ \mathbf{u} c, bub, beq, l, u 是向量
A u b ,   A e q \mathbf{A}_{ub}, \ \mathbf{A}_{eq} Aub, Aeq 是矩阵

例题:
max ⁡   z = 24 x 1 + 16 x 2 + 44 x 3 + 32 x 4 − 3 x

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值