數學模型作業(2) 中計算歐式距離有誤,已更改。內容(程式碼)接數學模型作業(2) 初學python和數學模型,不足之處請大佬指出。 給個贊(上次已經騙過關注了~~~)
匯入相關引數
shape = 613
alpha1 = 25
alpha2 = 15
beta1 = 20
beta2 = 25
theta = 30
delta = 0.001
建立 List 格式的垂直校正點集合 V 和水平校正點集合 H,儲存在一個 txt檔案中。
V = [ ]
H = [ ]
VH = [ ]
for i in range ( 0 , shape) :
if prop[ i] == 1 :
V. append( i)
if prop[ i] == 0 :
H. append( i)
VH = V + H
file = open ( '校正點集合.txt' , 'w' )
for i in range ( len ( VH) ) :
s = str ( VH[ i] ) + '\n'
file . write( s)
file . close( )
用 python 輸出減少邊之後最短路模型的鄰接矩陣,存放在 excel 檔案中。
分析: (1) 剪枝可以剪去任意點為起點到垂直校正點不符合垂直校正條件的枝 (2)剪枝可以剪去任意點為起點到水平校正點不符合水平校正條件的枝 (3) 剪枝可以剪去以B點為終點,距離超過
θ
/
δ
\theta/\delta
θ / δ 的枝 安裝gurobi,參考另一篇(Gurobi9.0.3安裝 ) 將距離矩陣dist轉換為tupledict型別,用dict_dist存放鄰接矩陣(要呼叫gurobi庫:from gurobipy import *)
dict_dist = { }
for i in range ( shape) :
for j in range ( shape) :
dict_dist[ i, j] = dist[ i] [ j]
dict_dist = tupledict( dict_dist)
for i in range ( 1 , shape- 1 ) :
for j in V:
if dist[ i] [ j] > min ( alpha1, alpha2) / delta:
dict_dist[ i, j] = 0
for i in range ( 1 , shape- 1 ) :
for j in H:
if dist[ i] [ j] > min ( beta1, beta2) / delta:
dict_dist[ i, j] = 0
for i in range ( shape- 1 ) :
if dist[ i] [ shape- 1 ] > theta / delta:
dict_dist[ i, shape- 1 ] = 0
edge = [ ]
for i in range ( shape) :
for j in range ( shape) :
if dict_dist[ i, j] != 0 :
edge. append( ( i, j) )
print ( "剪枝之後的邊數:" , len ( edge) )
dict_dist_save = np. zeros( shape= ( shape, shape) )
for i in range ( shape) :
for j in range ( shape) :
dict_dist_save[ i] [ j] = dict_dist[ i, j]
dict_dist_save = pd. DataFrame( dict_dist_save)
writer = pd. ExcelWriter( 'dict_dist.xlsx' )
dict_dist_save. to_excel( writer, 'sheet1' )
writer. save( )
結果