优化算法 | 单纯形法(附MATLAB代码)

hello,大家好。各位可点击左下方阅读原文,访问公众号官方店铺。谨防上当受骗,感谢各位支持!如果链接无法访问,公众号后台回复代码商店,即可获取官方店铺最新链接。

Hello,各位小伙伴。前一段时间二刷了方述诚老师的《线性规划》公开课,不由得再次感叹方老师讲得实在是太好了。

《线性规划》公开课视频链接为:https://b23.tv/h1uTSbZ

今天这期推文作为年前收官之作,主要是谈一下对单纯形法的理解。

我想绝大多数同学之前在学校上最优化或运筹学课程时,老师几乎都是教我们用单纯形表来求解一个问题,然后我们也就机械地按步骤去操作做题。但实际上,无论是单纯形法还是智能优化算法,我认为它们的内核都是一致的,那就是通过迭代更新位置的方式去搜索更优质的解

因此,在《线性规划》这门课中,方老师讲到了单纯形法的初始解构造方法位置更新公式最优解判别标准

因为在单纯形法中,找到一个初始解和从一个可行解找到最优解的难度几乎相同,所以在后续小节中,我们按照位置更新公式、最优解判别标准和初始解构造方法来讲解。


预备知识

线性规划(LP)标准形式

Min ⁡ c T x  s.t.  A x = b x ≥ 0 \begin{array}{r} \operatorname{Min} \mathbf{c}^T \mathbf{x} \\ \text { s.t. } \mathbf{A x}=\mathbf{b} \\ \mathbf{x} \geq 0 \end{array} MincTx s.t. Ax=bx0

其中,cost vector c = ( c 1 , c 2 , ⋯   , c n ) T \mathbf{c}=\left(c_1, c_2, \cdots, c_n\right)^T c=(c1,c2,,cn)T,solution vector x = ( x 1 , x 2 , ⋯   , x n ) T \mathbf{x}=\left(x_1, x_2, \cdots, x_n\right)^T x=(x1,x2,,xn)T,right-hand-side vector b = ( b 1 , b 2 , ⋯   , b n ) T \mathbf{b}=\left(b_1, b_2, \cdots, b_n\right)^{T} b=(b1,b2,,bn)T

constraint matrix A = ( a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ ⋮ ⋱ ⋮ a m 1 a m 2 ⋯ a m n ) \mathbf{A}=\left(\begin{array}{cccc} a_{11} & a_{12} & \cdots & a_{1 n} \\ a_{21} & a_{22} & \cdots & a_{2 n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m 1} & a_{m 2} & \cdots & a_{m n} \end{array}\right) A= a11a21am1a12a22am2a1na2namn

超平面(hyperplane)

标准形式LP中的每一项等式约束都视为解空间中的一个超平面。

【超平面定义】对于一个向量 a ∈ R n , a ≠ 0 \mathbf{a} \in \mathbf{R}^n, \mathbf{a} \neq 0 aRn,a=0,和一个实数 β ∈ R \beta \in \mathbf{R} βR,超平面 H = { x ∈ R n ∣ a T x = β } H=\left\{\mathrm{x} \in \mathbf{R}^n \mid \mathrm{a}^T \mathrm{x}=\beta\right\} H={xRnaTx=β}

其中 H U = { x  in  R n ∣ a T x ≥ β } H_U=\left\{x \text { in } R^n \mid a^T x \geq \beta\right\} HU={x in RnaTxβ} H L = { x  in  R n ∣ a T x ≤ β } H_L=\left\{x \text { in } R^n \mid a^T x \leq \beta\right\} HL={x in RnaTxβ}分别表示上、下半封闭空间, H U i = H U − H = { x  in  R n ∣ a T x > β } H_U^i=H_U-H=\left\{x \text { in } R^n \mid a^T x>\beta\right\} HUi=HUH={x in RnaTx>β} H L i = H L − H = { x  in  R n ∣ a T x < β } H_L^i=H_L-H=\left\{x \text { in } R^n \mid a^T x<\beta\right\} HLi=HLH={x in RnaTx<β}分别表示上、下半开放空间。

【性质1】正交向量 a a a垂直于超平面 H H H中的所有向量。

【性质2】正交向量指向上半个超平面,即指向目标函数值增加的方向。

多面体集合(polyhedral set)

一个多面体集合由有限个半封闭空间相交而成。

【性质3】标准形式LP的可行域 P = { x ∈ R n ∣ A x = b , x ≥ 0 } P=\left\{\mathrm{x} \in \mathrm{R}^n \mid \mathrm{Ax}=\mathrm{b}, \mathrm{x} \geq 0\right\} P={xRnAx=b,x0}为一个多面体集合。

线性/仿射/锥/凸组合

【线性组合定义】假设 x 1 , x 2 , … , x p ∈ R n \mathbf{x}^1, \mathbf{x}^2, \ldots, \mathbf{x}^p \in \mathbf{R}^n x1,x2,,xpRn λ 1 , λ 2 , … , λ p ∈ R \lambda_1, \lambda_2, \ldots, \lambda_p \in \mathbf{R} λ1,λ2,,λpR x = ∑ i = 1 p λ i x i = λ 1 x 1 + λ 2 x 2 + ⋯ + λ p x p \mathbf{x}=\sum_{i=1}^p \lambda_i \mathrm{x}^i=\lambda_1 \mathrm{x}^1+\lambda_2 \mathrm{x}^2+\cdots+\lambda_p \mathrm{x}^p x=i=1pλixi=λ1x1+λ2x2++λpxp,则称 x x x { x 1 , … , x p } \left\{\mathrm{x}^1, \ldots, \mathrm{x}^p\right\} {x1,,xp}的线性组合。

【仿射组合定义】基于线性组合假设前提,另如果 ∑ i = 1 p λ i = 1 \sum_{i=1}^p \lambda_i=1 i=1pλi=1,则称 x x x { x 1 , … , x p } \left\{\mathrm{x}^1, \ldots, \mathrm{x}^p\right\} {x1,,xp}的仿射组合。

【锥组合定义】基于线性组合假设前提,另如果 λ i ≥ 0 \lambda_i \geq 0 λi0,则称 x x x { x 1 , … , x p } \left\{\mathrm{x}^1, \ldots, \mathrm{x}^p\right\} {x1,,xp}的锥组合。

【凸组合定义】基于线性组合假设前提,另如果 ∑ i = 1 p λ i = 1 , λ i ≥ 0 \sum_{i=1}^p \lambda_i=1,\lambda_i \geq 0 i=1pλi=1,λi0,则称 x x x { x 1 , … , x p } \left\{\mathrm{x}^1, \ldots, \mathrm{x}^p\right\} {x1,,xp}的凸组合。

仿射集合/凸集合/锥

【仿射集合定义】假设 S S S是实数集 R n \mathbf{R}^n Rn的子集,如果 S S S中任意两点的仿射组合仍在 S S S中,则称 S S S为仿射集合。

【凸集合】假设 S S S是实数集 R n \mathbf{R}^n Rn的子集,如果 S S S中任意两点的凸组合仍在 S S S中,则称 S S S为凸集合。

【锥】假设 S S S是实数集 R n \mathbf{R}^n Rn的子集,如果 λ x ∈ S  for all  x ∈ S  and  λ ≥ 0 \lambda \mathrm{x} \in S \text { for all } \mathrm{x} \in S \text { and } \lambda \geq 0 λxS for all xS and λ0,则称 S S S为锥。

极点(extreme point,ep)

【极点定义】如果 x x x不能被凸集合 S S S内的其它点表示为凸组合的形式,那么 x x x是凸集合 S S S中的一个极点。

【定理1】假设 P P P是一个凸多面体,当且仅当 x x x P P P的一个极点时,那么 x ∈ P x \in P xP也是一个顶点

【定理2】当且仅当矩阵 A A A对应于 x x x为正数的列向量线性无关时, x ∈ P = { x ∈ R n ∣ A x = b , x ≥ 0 } \mathbf{x} \in P=\left\{\mathbf{x} \in \mathbf{R}^n \mid \mathbf{A} \mathbf{x}=b, \mathbf{x} \geq 0\right\} xP={xRnAx=b,x0} P P P的一个极点。

【定理3】对于一个标准形式LP,如果可行域 P P P非空,则在 P P P上的最优值 z = c T x \mathbf{z}=\mathbf{c}^T \mathbf{x} z=cTx或者无下界,或者至少在 P P P上的一个极点取到。

基/非基/基变量/非基变量/基解/基可行解

假设 A A A m m m n n n列的矩阵,并且 A A A存在 m m m个线性无关的列,则 A A A称为满秩矩阵。基于此,将矩阵 A A A和解向量 x x x进行重写,即将 A A A m m m个线性无关的列放在前 m m m列,剩余 n − m n-m nm列放在这 m m m列之后,则 x x x中元素也按照此顺序进行调整。
x = ( x B x N ) ← basic variables non-basic variables \mathrm{x}=\left(\begin{array}{l} \mathrm{x}_B \\ \mathrm{x}_N \end{array}\right) \leftarrow \begin{gathered} \text {basic variables} \\ \text {non-basic variables} \end{gathered} x=(xBxN)basic variablesnon-basic variables

A = ( B N ↑ ↑  basis  non-basis ) \mathbf{A}=\left(\begin{array}{c|c} \mathbf{B} & \mathbf{N} \\ \uparrow & \uparrow \\ \text { basis } & \text {non-basis} \end{array}\right) A= B basis Nnon-basis

因此, B B B N N N非基 x B \mathrm{x}_B xB基变量(basic variable, bv) x N \mathrm{x}_N xN非基变量(non-basic variable, nbv)

如果设 x N = 0 \mathrm{x}_N=0 xN=0,对于 A x = B x B = b \mathrm{Ax}=\mathrm{Bx}_B=\mathrm{b} Ax=BxB=b求解出 x B \mathrm{x}_B xB,则 x x x基解(basic solution,bs)。进一步来说,如果 x B > 0 \mathrm{x}_B >0 xB>0,则 x x x基可行解(basic feasible solution,bfs)

【引理1】当且仅当 x x x是对应于基 B B B的基可行解bfs,则 P P P中的一个点 x x x P P P中的一个极点。

【引理2】多面体集合 P P P包含有限个极点。

基础矩阵(fundamental matrix)

如果一个bfs x x x是一个非退化解,那么 x x x n n n个超平面所唯一确定。

A = [ B ∣ N ] \mathbf{A}=[\mathbf{B} \mid \mathbf{N}] A=[BN] x = [ x B x N ] = [ B − 1 b 0 ] \mathbf{x}=\left[\begin{array}{l} \mathbf{x}_B \\ \mathbf{x}_N \end{array}\right]=\left[\begin{array}{c} \mathbf{B}^{-1} \mathbf{b} \\ \mathbf{0} \end{array}\right] x=[xBxN]=[B1b0],令 M = [ B N 0 I ] M=\left[\begin{array}{cc} \mathrm{B} & \mathrm{N} \\ 0 & \mathrm{I} \end{array}\right] M=[B0NI],则 M M M是非奇异矩阵,且 M x = [ B N 0 I ] [ x B x N ] = [ b 0 ] \mathrm{Mx}=\left[\begin{array}{cc} \mathrm{B} & \mathrm{N} \\ 0 & \mathrm{I} \end{array}\right]\left[\begin{array}{l} \mathrm{x}_B \\ \mathrm{x}_N \end{array}\right]=\left[\begin{array}{l} \mathrm{b} \\ 0 \end{array}\right] Mx=[B0NI][xBxN]=[b0]。因此, x = [ x B x N ] = M − 1 [ b 0 ] \mathbf{x}=\left[\begin{array}{l} \mathbf{x}_B \\ \mathbf{x}_N \end{array}\right]=\mathbf{M}^{-1}\left[\begin{array}{l} \mathrm{b} \\ 0 \end{array}\right] x=[xBxN]=M1[b0] n n n个线性无关超平面所唯一确定。
M − 1 = [ B − 1 − B − 1 N 0 I ] \mathbf{M}^{-1}=\left[\begin{array}{cc} \mathrm{B}^{-1} & -\mathrm{B}^{-1} \mathbf{N} \\ \mathbf{0} & \mathbf{I} \end{array}\right] M1=[B10B1NI]
B − 1 \mathrm{B}^{-1} B1已知时,则 M − 1 \mathbf{M}^{-1} M1已知。

综上,称 M − 1 \mathbf{M}^{-1} M1 M \mathbf{M} M为LP的基础矩阵。


单纯形法整体框架

本次推文所阐述的单纯形法应用场景是非退化情形,针对退化场景的单纯形法可在方老师的课程中学习。

由【引理1】可知,在非退化情形下,一般来说一个极点对应一个bfs。但是在退化情形下,一个极点可能对应多个bfs。

※bfs表示基可行解※

Phase I:
Step 1: (Starting)
Find an initial basic feasible solution (bfs), or declare P is null.
Phase II:
Step 2: (Checking optimality)
If the current bfs is optimal, STOP!
Step 3: (Pivoting)
Move to a better bfs.
Return to Step 2.


位置更新公式

由预备知识中的【定理3】可以得知,标准形式LP问题的最优解一定在可行域的某个极点,也就是凸多面体的顶点。所以求解标准形式的LP问题,可以转化为找出“最好的”那个极点。

假设LP问题的可行域是一个凸多面体集合,那么单纯形法的思想就是沿着凸多面体的边从一个顶点移动到另一个顶点,也可以说从一个bfs/ep更新至另一个相邻的bfs/ep(针对非退化情形),进而找到最优解。

那么从一个bfs更新至另一个bfs的公式实际上和其它优化算法的位置更新公式思想一致,即方向 d q \mathbf{d}_q dq步长 α \alpha α
x ← x + α d q \mathbf{x} \leftarrow \mathbf{x}+\alpha \mathbf{d}_q xx+αdq

方向 d q \mathbf{d}_q dq

既然要从一个bfs/ep更新至另一个相邻的bfs/ep,那么首先需要明确bfs相邻的定义,其次需要明确如何从众多个相邻的bfs中选择下一个bfs作为更新后的位置。

bfs相邻定义】如果两个bfs有 m − 1 m-1 m1个相同的基变量(指的是变量,而不是数值),那么这两个bfs就是相邻的。

在非退化情形下,每个bfs/ep恰好有 n − m n-m nm个相邻的邻居。针对一个bfs,其每一个相邻的bfs都可以通过将一个nbv从0增加到正数(即入基),同时将一个bv从正数减小到0(即出基)的方式到达该bfs。

在了解一个bfs有 n − m n-m nm个相邻的邻居后,接下来,我们需要明确该朝哪个方向走前往下一个更好的bfs

这时候,我们可以用到前面所讲的fundamental matrix, M − 1 = [ B − 1 − B − 1 N 0 I ] \mathbf{M}^{-1}=\left[\begin{array}{cc} \mathrm{B}^{-1} & -\mathrm{B}^{-1} \mathbf{N} \\ \mathbf{0} & \mathbf{I} \end{array}\right] M1=[B10B1NI],这个矩阵中后 n − m n-m nm [ − B − 1 N I ] \left[\begin{array}{c} -\mathbf{B}^{-1} \mathbf{N} \\ \mathbf{I} \end{array}\right] [B1NI]实际上就表示可能的 n − m n-m nm个方向。

d q d_q dq M − 1 \mathbf{M}^{-1} M1中对应非基变量 x q x_q xq的列,即为一个可能的移动方向, d q = ( − B − 1 A q 0 ⋮ 0 1 0 ⋮ 0 ) \mathrm{d}_q=\left(\begin{array}{c} -\mathbf{B}^{-1} \mathbf{A}_q \\ 0 \\ \vdots \\ 0 \\ 1 \\ 0 \\ \vdots \\ 0 \end{array}\right) dq= B1Aq00100 ,其中 A = ( A 1 ∣ A 2 ∣ ⋯ ∣ A n ) \mathbf{A}=\left(\mathbf{A}_1\left|\mathbf{A}_2\right| \cdots \mid \mathbf{A}_n\right) A=(A1A2An)

这里的 d q d_q dq表示一个可能的移动方向,但是我们并不知道该方向是否可行

这里我们直接给出结论,在非退化情形下,每一个 d q d_q dq都是可行方向。但是在退化情形下,可能存在 d q d_q dq不是可行方向的情况。

既然每一个 d q d_q dq都是可行方向,那么究竟哪个 d q d_q dq是好方向呢

我们可以通过目标函数值进行判断,具体计算公式如下,其中 z ( x ( λ ) ) \mathbf{z}(\mathbf{x}(\lambda)) z(x(λ))表示新bfs的目标函数值, z ( x ) \mathbf{z}(\mathbf{x}) z(x)表示原bfs的目标函数值。
z ( x ( λ ) ) = c T x ( λ ) = c T ( x + λ d q ) = z ( x ) + λ [ c B T ∣ c N T ] ( − B − 1 A q e q ) = z ( x ) + λ [ c q − c B T B − 1 A q ] = z ( x ) + λ r q \begin{aligned} \mathbf{z}(\mathbf{x}(\lambda)) & =\mathbf{c}^T \mathbf{x}(\lambda) \\ & =\mathbf{c}^T\left(\mathbf{x}+\lambda \mathbf{d}_q\right) \\ & =\mathbf{z}(\mathbf{x})+\lambda\left[\mathbf{c}_B^T \mid \mathbf{c}_N^T\right]\left(\frac{-\mathbf{B}^{-1} \mathbf{A}_q}{e_q}\right) \\ & =\mathbf{z}(\mathbf{x})+\lambda\left[c_q-\mathbf{c}_B^T \mathbf{B}^{-1} \mathbf{A}_q\right] \\ & =\mathbf{z}(\mathbf{x})+\lambda r_q \end{aligned} z(x(λ))=cTx(λ)=cT(x+λdq)=z(x)+λ[cBTcNT](eqB1Aq)=z(x)+λ[cqcBTB1Aq]=z(x)+λrq
可以看出, r q = c T   d q = c q − c B T B − 1   A q < 0 r_q=\mathrm{c}^T \mathrm{~d}_q=c_q-\mathbf{c}_B^T B^{-1} \mathrm{~A}_q<0 rq=cT dq=cqcBTB1 Aq<0,即表示目标函数值可以继续减小,那么 d q d_q dq就是一个好方向

【reduced cost定义】 r q = c T   d q = c q − c B T B − 1   A q r_q=\mathrm{c}^T \mathrm{~d}_q=c_q-\mathbf{c}_B^T B^{-1} \mathrm{~A}_q rq=cT dq=cqcBTB1 Aq称为对应于变量 x q x_q xq的reduced costs。

既然 r q < 0 r_q<0 rq<0表示 d q d_q dq是一个好方向,那么如果在一个bfs上存在多个好方向,究竟该选择哪个好方向

有两种方案,方案一:在众多好方向中选择reduced cost最大的方向 d q d_q dq,即 min ⁡ j :  nonbasic  { c T d j ∥ d j ∥ } \min _{j: \text { nonbasic }}\left\{\frac{\mathbf{c}^T \mathbf{d}_j}{\left\|\mathbf{d}_j\right\|}\right\} minj: nonbasic {djcTdj}

方案二:在计算新方向过程中,选择第一个好方向即可,即 r q < 0 r_q<0 rq<0的方向 d q d_q dq

看起来方案一似乎更好一些,但目前并没有人严格地证明哪种方案效果更佳。

步长 α \alpha α

既然已经有了方向 d q d_q dq,接下来就需要确定沿着这个方向需要走多远,才能到达下一个bfs。

这里针对 d q d_q dq中元素的取值情况,步长的取值也分两种情况。

x ( α ) = x + α d q , α > 0 \mathbf{x}(\alpha)=\mathbf{x}+\alpha d_q,\alpha>0 x(α)=x+αdq,α>0且有 r q = c T d q = c q − c B T B − 1 A q < 0 r_q=\mathbf{c}^T \mathbf{d}_q=c_q-\mathbf{c}_B^T B^{-1} \mathbf{A}_q<0 rq=cTdq=cqcBTB1Aq<0。此外, A d q = 0 \mathbf{A d}_q=\mathbf{0} Adq=0可推出 A x ( α ) = A x = b \mathbf{A x}(\alpha)=\mathbf{A x}=\mathbf{b} Ax(α)=Ax=b

Case 1:如果 d q ≥ 0 d_q \geq 0 dq0,则 x ( α ) ≥ 0 , ∀ α \mathbf{x}(\alpha) \geq 0, \forall \alpha x(α)0,α ,因此 x ( α ) ∈ P , ∀ α ≥ 0 \mathbf{x}(\alpha) \in P, \quad \forall \alpha \geq 0 x(α)P,α0,且当 α ⟶ + ∞ \alpha \longrightarrow+\infty α+ c T x ( α ) = c T x + α c T d q ⟶ − ∞ \mathbf{c}^T \mathbf{x}(\alpha)=\mathbf{c}^T \mathbf{x}+\alpha \mathbf{c}^T \mathbf{d}_q \longrightarrow-\infty cTx(α)=cTx+αcTdq 。这种情况,说明LP是无界的。

Case 2:当 d q d_q dq中至少存在一个小于0的元素,为了保证 x ( α ) ≥ 0 \mathbf{x}(\alpha) \geq 0 x(α)0步长计算公式
α = min ⁡ i :  basic  { x i − d q i ∣   d q i < 0 } \alpha=\min _{i: \text { basic }}\left\{\left.\frac{x_i}{-d_{q i}} \right\rvert\, d_{q i}<0\right\} α=i: basic min{dqixi dqi<0}
Case 2中计算步长的方式称为Minimum ratio test


最优解判别标准

【定理】给定一个bfs x 0 = ( B − 1 b 0 ) \mathbf{x}^0=\left(\frac{\mathbf{B}^{-1} \mathbf{b}}{\mathbf{0}}\right) x0=(0B1b),其中基为 B B B,如果对于任意的非基变量 x q x_q xq都有 r q ≥ 0 r_q \geq 0 rq0,那么 x 0 \mathbf{x}^0 x0即为最优解。


初始解构造方法

初始解构造方法有2种,分别为两阶段法和大M法。这两种方法的思想很接近,即首先增加人工变量并初始为人工变量赋值,然后迫使人工变量从正数变为0,在这一过程中,原问题的变量就一定不能全部为0,所以会得到原问题的一个初始解。

两阶段法

Step 1:使右手边向量 b b b 变为非负向量。
 Min  c T x ( L P )  s.t.  A x = b ( ≥ 0 ) x ≥ 0 \begin{array}{lll} & \text { Min } & \mathbf{c}^T \mathbf{x} \\ (\mathrm{LP}) & \text { s.t. } & \mathbf{A x}=\mathbf{b}(\geq 0) \\ & & \mathbf{x} \geq 0 \end{array} (LP) Min  s.t. cTxAx=b(0)x0
Step 2:向原问题添加了 m m m 个人工变量 u = [ u 1 u 2 ⋮ u m ] \mathbf{u}=\left[\begin{array}{c} u_1 \\ u_2 \\ \vdots \\ u_m \end{array}\right] u= u1u2um ,则产生了第一阶段问题。
 Min  ∑ i = 1 m u i ( P h I )  s.t.  A x + I u = b ( ≥ 0 ) x , u ≥ 0 \begin{array}{lll} &\text { Min } \sum_{i=1}^m u_i \\ (\mathrm{PhI}) \text { s.t. } & \mathbf{A x}+\mathbf{I u}=\mathbf{b}(\geq 0) \\ & \mathbf{x}, \mathbf{u} \geq 0 \end{array} (PhI) s.t.  Min i=1muiAx+Iu=b(0)x,u0
由上述步骤可知:

  1. u = b , x = 0 \mathbf{u}=b, \mathbf{x}=0 u=b,x=0 ( P h I ) (\mathrm{PhI}) (PhI)的一个bfs。
  2. ( P h I ) (\mathrm{PhI}) (PhI)的下界为0。
  3. 当且仅当 ( P h I ) (\mathrm{PhI}) (PhI)的最有目标函数值 z P h I ∗ = 0 \mathbf{z}_{P h I}^*=0 zPhI=0,则 ( L P ) (\mathrm{LP}) (LP)存在可行解。
  4. 在非退化情形下,如果 z P h I ∗ = 0 \mathbf{z}_{P h I}^*=0 zPhI=0,那么 ( P h I ) (\mathrm{PhI}) (PhI)的最优解是 ( L P ) (\mathrm{LP}) (LP)的一个bfs。

大M法

给每一个新添加的人工变量施加很大的惩罚 M > 0 M>0 M>0,并将原问题与第一阶段问题相结合,形成如下问题:
 Min  ∑ j = 1 n c j x j + ∑ i = 1 m M u i s.t. A x + I u = b ( ≥ 0 ) x , u ≥ 0 \begin{array}{lll} \text { Min } & \sum_{j=1}^n c_j x_j+\sum_{i=1}^m M u_i \\ \text {s.t.} & \mathbf{A x}+I \mathbf{u}=\mathbf{b}(\geq 0) \\ & \mathbf{x}, \mathbf{u} \geq 0 \end{array}  Min s.t.j=1ncjxj+i=1mMuiAx+Iu=b(0)x,u0
分析可知:

  1. u = b , x = 0 \mathbf{u}=b, \mathbf{x}=0 u=b,x=0是一个bfs。
  2. 假设在最优解 ( x ∗ u ∗ ) \left(\begin{array}{l} \mathbf{x}^* \\ \mathbf{u}^* \end{array}\right) (xu)处, z ∗ \mathbf{z}^* z取值为有限大,如果 u ∗ = 0 \mathbf{u}^*=0 u=0,那么 x ∗ \mathbf{x}^* x是原问题的bfs。如果 u ∗ ≠ 0 u^* \neq 0 u=0,那么原问题无可行解。

单纯形法伪代码

Step1: Find a bfs x x x with A = [ B ∣ N ] A=[B|N] A=[BN].

Step2: Check for n.b.v’s r q = c T d q ( = c q − c B T B − 1 A q ) r_q=\mathbf{c}^T \mathbf{d}_q\left(=c_q-\mathbf{c}_B^T \mathbf{B}^{-1} \mathbf{A}_q\right) rq=cTdq(=cqcBTB1Aq)

​ If r q ≥ 0 , ∀  nonbasic  x q r_q \geq 0, \forall \text { nonbasic } x_q rq0, nonbasic xq, then the current bfs is optimal.

​ Otherwise, pick one r q < 0 r_q<0 rq<0. Go to next step.

Step3: If d q ≥ 0 d_q\geq 0 dq0, then LP is unbounded.

​ Otherwise, find α = min ⁡ i :  basic  { x i − d q i ∣   d q i < 0 } \alpha=\min _{i: \text { basic }}\left\{\left.\frac{x_i}{-d_{q_i}} \right\rvert\, d_{q_i}<0\right\} α=mini: basic {dqixi dqi<0}.

​ Then x ← x + α d q \mathbf{x} \leftarrow \mathbf{x}+\alpha \mathbf{d}_q xx+αdq is a new bfs.

​ Update B B B and N N N. Go to step 2.


MATLAB代码

源代码链接:https://www.mathworks.com/matlabcentral/fileexchange/26554-revised-simplex-method

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The function revised solves an LPP using revised simplex method. It uses
% big M method to solve an LPP when thre are >= or = constraints present.
%
% Input:-   1)c : The cost vector or the (row) vector containing co-eficients 
%               of deceision variables in the objective function. It is a
%               required parameter.
%           2)b : The (row) vector containing right hand side constant of
%               the constraints. It is a required parameter.
%           3)a : The coefficient matrix of the left hand side of the
%               constraints. it is a required parameter.
%           4)inq : A (row) vector indicating the type of constraints as 1
%               for >=, 0 for = and -1 for <= constraints. If inq is not
%               supplied then it is by default taken that all constraints
%               are of <= type. It is an optional parameter.
%           5)minimize : This parameter indicates whether the objective
%               function is to be minimized. minimized = 1 indicates
%               a mimization problem and minimization = 0 stands for a
%               maximization problem. By default it is taken as 0. It is an
%               optional parameter.
%
% Example : min     z=-3x1+x2+x3
%           s.t.    x1-2x2+x3<= 11
%               	-4x1+x2+2x3>=3
%                       -2x1+x3=1
%                   x1,x2,x3>=0.
% Solution : c=[-3 1 1];b=[11 3 1];a=[1 -2 1;-4 1 2;-2 0 1];inq=[-1 1 0].
% After supplying these inputs call revised(c,b,a,inq,1).
% 
% This function is able to detect almost all types of
% properties/characteristics present in an LPP such as unbounded solution,
% alternate optima, degenaracy/cycling and infeasibilty. It only fails to
% work when there are redundant constraints present in the problem. However,
% it is rare and can be easily avoided by the user by just
% checking/ensuring that rank(a) should not be less than the number of 
% constraints. As finding rank of big matrices has high complexity, this 
% check has not been given here and it is expected that user would take 
% care of such cases. In such cases usually it is easily seen that some 
% costarints are linearly dependent and hence can be eliminated. Rest of
% the cases show good results. For theory of Revised Simplex method and LPP
% one may see "Numerical Optimization with Applications, Chandra S., Jayadeva,
% Mehra A., Alpha Science Internatinal Ltd, 2009."
%
% This code has been written by Bapi Chatterjee as course assignment in the 
% course Numerical Optimization at Indian Institute of Technology Delhi, 
% New Delhi, India. The author shall be thankful for suggesting any
% error/improvment at bhaskerchatterjee@gmail.com.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function revised(c,b,a,inq,minimize)
if nargin<3||nargin>5
    fprintf('\nError:Number of input arguments are inappropriate!\n');
else
    n=length(c);m=length(b);j=max(abs(c));
    if nargin<4
        minimize=0;
        inq=-ones(m,1);
    elseif nargin<5
        minimize=0;
    end
    if ~isequal(size(a),[m,n])||m~=length(inq)
        fprintf('\nError: Dimension mismatch!\n');
    else
        if minimize==1
            c=-c;
        end
        count=n;nbv=1:n;bv=zeros(1,m);av=zeros(1,m);
        for i=1:m
            if b(i)<0
                a(i,:)=-a(i,:);
                b(i)=-b(i);
            end
            if inq(i)<0
                count=count+1;
                c(count)=0;
                a(i,count)=1;
                bv(i)=count;
            elseif inq(i)==0
                count=count+1;
                c(count)=-10*j;
                a(i,count)=1;
                bv(i)=count;
                av(i)=count;
            else
                count=count+1;
                c(count)=0;
                a(i,count)=-1;
                nbv=[nbv count];
                count=count+1;
                c(count)=-10*j;
                a(i,count)=1;
                av(i)=count;
                bv(i)=count;
            end
        end
        A=[-c;a];
        B_inv=eye(m+1,m+1);
        B_inv(1,2:m+1)=c(bv);
        x_b=B_inv*[0; b'];
        fprintf('\n.............The initial tablaue................\n')
        fprintf('\t z');disp(bv);
        fprintf('--------------------------------------------------\n')
        disp([B_inv x_b])
        flag=0;count=0;of_curr=0;
        while(flag~=1)
            [s,t]=min(B_inv(1,:)*A(:,nbv));
            y=B_inv*A(:,nbv(t));count=count+1;
            if(any(y(2:m+1)>0))
                fprintf('\n.............The %dth tablaue................\n',count)
                fprintf('\t z');disp(bv);
                fprintf('--------------------------------------------------\n')
                disp([B_inv x_b y])
                if count>1 && of_curr==x_b(1)
                    flag=1;
                    if minimize==1
                        x_b(1)=-x_b(1);
                    end
                    fprintf('\nThe given problem has degeneracy!\n');
                    fprintf('\nThe current objective function value=%d.\n',x_b(1));
                    fprintf('\nThe current solution is:\n');
                    for i=1:n
                        found=0;
                        for j=1:m
                            if bv(j)==i
                                fprintf('x%u = %d\n',i,x_b(1+j));found=1;
                            end
                        end
                        if found==0
                            fprintf('x%u = %d\n',i,0);
                        end
                    end
                else
                    of_curr=x_b(1);
                    if(s>=0)
                        flag=1;
                        for i=1:length(av)
                            for j=1:m
                                if av(i)==bv(j)
                                    fprintf('\nThe given LPP is infeasible!\n');
                                    return
                                end
                            end
                        end
                        if minimize==1
                            x_b(1)=-x_b(1);
                        end
                        fprintf('\nReqiured optimization has been achieved!\n');
                        fprintf('\nThe optimum objective function value=%d.\n',x_b(1));
                        fprintf('\nThe optimum solution is:\n');
                        for i=1:n
                            found=0;
                            for j=1:m
                                if bv(j)==i
                                    fprintf('x%u = %d\n',i,x_b(1+j));found=1;
                                end
                            end
                            if found==0
                                fprintf('x%u = %d\n',i,0);
                            end
                        end
                        if (s==0 && any(y(2:m+1)>0))
                            fprintf('\nThe given problem has alternate optima!\n');
                        end
                    else
                        u=10*j;
                        for i=2:m+1
                            if y(i)>0
                                if (x_b(i)/y(i))<u
                                    u=(x_b(i)/y(i));
                                    v=i-1;
                                end
                            end
                        end
                        temp=bv(v);bv(v)=nbv(t);
                        nbv(t)=temp;
                        E=eye(m+1,m+1);
                        E(:,1+v)=-y/y(1+v);
                        E(1+v,1+v)=1/y(1+v);
                        B_inv=E*B_inv;
                        x_b=B_inv*[0; b'];
                    end
                end
            else
                fprintf('\nThe given problem has unbounded solution\n')
                return
            end
        end
    end
end

例子
Min ⁡ z = − 4 x 1 − 5 x 2  s.t.  3 x 1 + x 2 + x 3 = 8 x 1 + 2 x 2 + x 4 = 9 x 1 , x 2 , x 3 , x 4 ≥ 0 \begin{gathered} \operatorname{Min} z=-4 x_1-5 x_2 \\ \text { s.t. } \quad 3 x_1+x_2+x_3=8 \\ x_1+2 x_2+x_4=9 \\ x_1, x_2, x_3, x_4 \geq 0 \end{gathered} Minz=4x15x2 s.t. 3x1+x2+x3=8x1+2x2+x4=9x1,x2,x3,x40

%% example
clear
clc
A=[3,1,1,0; 
   1,2,0,1]; 
b=[8,9]; 
c=[-4,-5,0,0]; 
inq=zeros(size(b)); 
%inq : A (row) vector indicating the type of constraints as 1 for >=, 
% 0 for = and -1 for <= constraints. If inq is not supplied then it is by default taken 
% that all constraints are of <= type. It is an optional parameter.
minimize=1;
%minimize : This parameter indicates whether the objective function is to be minimized. 
% minimized = 1 indicates a mimization problem and minimization = 0 stands for a  maximization problem. 
% By default it is taken as 0. It is an optional parameter.
revised(c,b,A,inq,minimize)

求解结果:

.............The initial tablaue................
	 z     5     6

--------------------------------------------------
     1   -50   -50  -850
     0     1     0     8
     0     0     1     9


.............The 1th tablaue................
	 z     5     6

--------------------------------------------------
     1   -50   -50  -850  -204
     0     1     0     8     3
     0     0     1     9     1


.............The 2th tablaue................
	 z     1     6

--------------------------------------------------
    1.0000   18.0000  -50.0000 -306.0000  -87.0000
         0    0.3333         0    2.6667    0.3333
         0   -0.3333    1.0000    6.3333    1.6667


.............The 3th tablaue................
	 z     1     2

--------------------------------------------------
    1.0000    0.6000    2.2000   24.6000    0.6000
         0    0.4000   -0.2000    1.4000    0.4000
         0   -0.2000    0.6000    3.8000   -0.2000


Reqiured optimization has been achieved!

The optimum objective function value=-2.460000e+01.

The optimum solution is:
x1 = 1.400000e+00
x2 = 3.800000e+00
x3 = 0
x4 = 0

参考

  1. 【线性规划-方述诚】 https://b23.tv/h1uTSbZ
  2. https://www.mathworks.com/matlabcentral/fileexchange/26554-revised-simplex-metho
  3. https://www.12000.org/my_notes/simplex/index.htm#x1-60002.1

想快速入门智能优化算法的小伙伴可以阅读我们的书籍,本书对算法的讲解详细易懂,对代码的注释也十分完备,想要入手此书的小伙伴可以抓紧入手哦!

京东自营购买链接:

https://item.jd.com/13422442.html

当当自营购买链接

http://product.dangdang.com/29301483.html

OK,今天就到这里啦。我们已经推出粉丝QQ交流群,各位小伙伴赶快加入吧!!!


咱们下期再见

近期你可能错过了的好文章

新书上架 | 《MATLAB智能优化算法:从写代码到算法思想》

算法自学 | 优化算法书籍+代码+网站+推文一站式合集(上)

优化算法 | 灰狼优化算法(文末有福利)

优化算法 | 鲸鱼优化算法

遗传算法(GA)求解带时间窗的车辆路径(VRPTW)问题MATLAB代码

粒子群优化算法(PSO)求解带时间窗的车辆路径问题(VRPTW)MATLAB代码

知乎 | bilibili | CSDN:随心390

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值