Genetic Algorithm Pseudo code:
Input CCN Topology, Traveling Time, Profit SL=null /*put the Scheduling Lists*/
Temp_SL=null n=V.length()
while (SL.length<k) do
temp_list=generate a scheduling list which is length n from V SL.add(temp_list)
end while
while (generation<X) do
for each scheduling_list in SL do
Caculate the total_profit of scheduling_list Caculate the total_time of scheduling_list end for
Choose scheduling list which have the bigger profit from SL /* selection */
Delete the other scheduling list which have the smaller profit from SL for each scheduling_list in SL do /*reproduction*/
PMC(scheduling_list_a, scheduling_list_b)
end for
for each scheduling_list in SL do /*mutation*/
rp=math.random()*n rv=math.random()*n
if scheduling_list[rp]!=rv
exchange(scheduling_list[rp],rv)
end if
end for
generation=generation+1
end while
圖 4.26、演算法 Genetic Algorithm 之 Pseudo Code
Genetic Algorithm 演算法範例
我們以一個簡單的範例說明 Genetic Algorithm 演算法,由於此演算法在演算過程中並不 單一考慮各基地台的救災效益、路網成本及 CCN 樹狀拓樸,而是著重在基本遺傳的選 擇(selection)、交叉(crossover)、變異(mutation),將優良的基因會不斷遺傳下去,把好的 解不斷分裂再結合,每一階段基因演化結束才參照路網成本及救災效益時間總表進行救
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
86
災效益的計算。因此在此以每階段 Genetic Algorithm 中基因演化步驟如何進行講述。
如圖 4.27,第一步驟單純只看輸入資訊 CCN 樹狀拓樸中的基地台,即點的集合 A、
B、C、D、E、F,由這些點隨機產生 K 組的排列組合,這些排列組合各自對應到不同 的 CCN 建構排程順序,此即為初始的染色體。
圖 4.27、Genetic Algorithm 演算法步驟一
第二步驟,由於一組排列組合即對應到一組建構排程的順序,在此步驟根據 CCN 樹狀 拓樸、路徑成本以及救災效益時間總表計算 K 組建構排程各自的 total profit 及 total time。
如圖 4.28 所示,第 2 組建構排程所計算出來的 total profit 是 37。
圖 4.28、Genetic Algorithm 演算法步驟二
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
87
第三步驟,由於第二步驟已將 K 組建構排程的 total profit 都計算出來,此步驟將 K 組 total profit 互相比較,選擇前二分之一較高的 total profit 保存下來,也就是將表現較好的染色 體留下來,將表現差的染色體丟棄,如圖 4.29 所示,最後僅留下了 K/2 組 total profit 較 高的建構排程。
圖 4.29、Genetic Algorithm 演算法步驟三
如圖 4.30,第四步驟將第三步驟篩選過後所留下的建構排程,每一組都複製一份,建構 排程的解由 K/2 組恢復到 K 組
圖 4.30、Genetic Algorithm 演算法步驟四
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
88
第五步驟,為了產生出更好的解,將這些已篩選過的建構排程進行交叉(crossover)的動 作,如圖 4.31 所示,當兩組建構排程進行交叉動作時,演算法會隨機產生一個值 Ri,
代表即將產生交叉的建構排程位置,圖中的例子 Ri=5,因此 Scheduling 1 位置 5 的 E 要 轉換成 C,而 Scheduling 2 位置 5 的 C 要轉換成 E,但 CCN 建構排程在此具有不重複建 構的特性,因此將 Scheduling 1 中原來的值為 C 的位置 4 改成 E,將 Scheduling 2 中原 來的值為 E 的位置 3 改成 C,完成交叉的動作。
圖 4.31、Genetic Algorithm 演算法步驟五
第六步驟是突變(mutation),隨機產生兩個值 RP 代表需變異的位置,RV 代表變異後的 值的位置,因為不可重複的特性,在此可視為是單一建構排程內的基地台建構順序交換,
如圖 4.32,位置 5 的值為 E,位置 1 的值為 D,經過突變動作後,位置 1 的值變成了 E 而位置 5 的值變成了 D。
圖 4.32、Genetic Algorithm 演算法步驟六
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
89
重複第二步驟到第六步驟直到已經基因演化 X 代,演算法才算結束。以上即為 Genetic