單純形法python實現

2021-09-28 17:20:50 字數 4061 閱讀 7785

# coding=utf-8

# 單純形法的實現,只支援最簡單的實現方法

# 且我們假設約束矩陣a的最後m列是可逆的

# 這樣就必須滿足a是行滿秩的(m*n的矩陣)

import numpy as np

class

******x

(object):

def__init__

(self, c, a, b)

:# 形式 minf(x)=c.tx

# s.t. ax=b

self.c = c

self.a = a

self.b = b

defrun(self)

: c_shape = self.c.shape

a_shape = self.a.shape

b_shape = self.b.shape

assert c_shape[0]

== a_shape[1]

,"not aligned a with c shape"

assert b_shape[0]

== a_shape[0]

,"not aligned a with b shape"

# 找到初始的b,n等值

end_index = a_shape[1]

- a_shape[0]

n = self.a[:,

0:end_index]

n_columns = np.arange(

0, end_index)

c_n = self.c[n_columns,:]

# 第乙個b必須是可逆的矩陣,其實這裡應該用演算法尋找,但此處省略

b = self.a[

:, end_index:

] b_columns = np.arange(end_index, a_shape[1]

) c_b = self.c[b_columns,:]

steps =

0while

true

: steps +=

1print

("steps is {}"

.format

(steps)

) is_optim, b_columns, n_columns = self.main_******x(b, n, c_b, c_n, self.b, b_columns, n_columns)

if is_optim:

break

else

: b = self.a[

:, b_columns]

n = self.a[

:, n_columns]

c_b = self.c[b_columns,:]

c_n = self.c[n_columns,:]

defmain_******x

(self, b, n, c_b, c_n, b, b_columns, n_columns)

: b_inverse = np.linalg.inv(b)

p =(c_n.t - np.matmul(np.matmul(c_b.t, b_inverse)

, n)

).flatten(

)if p.

min(

)>=0:

is_optim =

true

print

("reach optimization."

)print

("b_columns is {}"

.format

(b_columns)

)print

("n_columns is {}"

.format

(sorted

(n_columns)))

best_solution_point = np.matmul(b_inverse, b)

print

("best solution point is {}"

.format

(best_solution_point.flatten())

)print

("best value is {}"

.format

(np.matmul(c_b.t, best_solution_point)

.flatten()[

0]))

print

("\n"

)return is_optim, b_columns, n_columns

else

:# 入基

n_i_in = np.argmin(p)

n_i = n[

:, n_i_in]

.reshape(-1

,1)# by=ni, 求出基

y = np.matmul(b_inverse, n_i)

x_b = np.matmul(b_inverse, b)

n_i_out = self.find_out_base(y, x_b)

tmp = n_columns[n_i_in]

n_columns[n_i_in]

= b_columns[n_i_out]

b_columns[n_i_out]

= tmp

is_optim =

false

print

("not reach optimization"

)print

("in base is {}"

.format

(tmp)

)print

("out base is {}"

.format

(n_columns[n_i_in]))

# 此時已經被換過去了

print

("b_columns is {}"

.format

(sorted

(b_columns)))

print

("n_columns is {}"

.format

(sorted

(n_columns)))

print

("\n"

)return is_optim, b_columns, n_columns

deffind_out_base

(self, y, x_b)

:# 找到x_b/y最小且y>0的位置

index =

min_value =

for i, value in

enumerate

(y):

if value <=0:

continue

else:/

float

(value)

) actual_index = index[np.argmin(min_value)

]return actual_index

if __name__ ==

"__main__"

:'''

c = np.array([-20, -30, 0, 0]).reshape(-1, 1)

a = np.array([[1, 1, 1, 0], [0.1, 0.2, 0, 1]])

b = np.array([100, 14]).reshape(-1, 1)

'''c = np.array([-

4,-1

,0,0

,0])

.reshape(-1

,1) a = np.array([[

-1,2

,1,0

,0],

[2,3

,0,1

,0],

[1,-

1,0,

0,1]

])b = np.array([4

,12,3

]).reshape(-1

,1) ******x = ******x(c, a, b)

******x.run(

)

C語言實現單純形法與對偶單純形法

某次為了完成課程要求所做 單純形法 如下,使用方法修改二位陣列a ip jp include define ip 3 define jp 7 int i,j,m,n,flag 1 float max 1 min 1000 guiyi float c jp 1 float a ip jp float ...

單純形法簡介

考慮目標函式,ma xz 3 x1 4 x2增加x1 和x2 的值都將改進 z 的值,單純形法的設計要求每次都選擇使 z值有最大改善的那個變數。意味著在上述目標函式中,首先選擇增加x2 的值。通過對問題約束施加以下兩項要求來方便單純形法的計算 1.所有的約束都是等式,並且具有非負右端項 2.所有變數...

單純形法中大m法 單純形法(大M法)

function x,fval,flag,iteration original m c,a,b 原始單純形法 大m法,無需給出初始基變數 programmed by liyang faruto s studio bnu math last modified 2008.4.27 求解標準型線性規劃 m...