錦標法

9
錦錦錦 錦錦錦錦錦錦錦錦錦錦錦 錦錦錦錦錦錦錦錦錦錦錦錦錦錦錦 ,。 錦錦錦錦錦錦錦錦錦錦錦錦 : 錦錦錦錦錦錦錦錦錦錦錦錦錦錦 錦錦錦錦錦錦錦錦錦錦錦錦錦錦錦錦 ,, 錦錦錦錦錦錦錦錦錦錦 錦錦 錦錦錦錦錦錦錦錦錦錦 ,一。 錦錦錦錦錦錦錦錦錦 錦錦錦錦錦錦錦錦錦錦錦 錦錦錦錦錦錦錦錦 錦錦錦錦 錦錦錦錦錦錦錦錦錦錦錦 ,,,, 錦錦錦 錦錦錦錦錦錦錦錦錦錦錦錦錦錦錦錦 錦 錦錦錦錦錦錦錦錦錦錦錦錦錦錦錦 ,,,。 ( 錦錦 ) ( 錦錦 ) tselect

Upload: tivona

Post on 05-Jan-2016

21 views

Category:

Documents


1 download

DESCRIPTION

tselect. 錦標法. 程式設計產生亂數的方 法,大致上可分為輪盤法以及錦標法。. 使用輪盤法設計程式的缺點 : 輪盤法是將資料劃分為區域長度,再由指標從中隨機挑選勝出的資料,當區域長度比值相同時,每一資料的勝出率也相等。. ( 指標 ). 當區域長度不相等時,則較高比值的區域資料,勝出率也比較高,相反的,比值較低的區域資料勝出率也低,因此資料亂數產生的機率並不平均,,錦標法改善了輪盤法的這個缺點。. ( 指標 ). 錦標法 : 每一資料都有各自的位址,每一位址存放著各自的資料,位址順序不變,將每一位址中的資料做交換,也就是打亂的動作. 位址. 資料. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 錦標法

錦標法程式設計產生亂數的方法,大致上可分為輪盤法以及錦標法。

使用輪盤法設計程式的缺點 : 輪盤法是將資料劃分為區域長度,再由指標從中隨機挑選勝出的資料,當區域長度比值相同時,每一資料的勝出率也相等。

當區域長度不相等時,則較高比值的區域資料,勝出率也比較高,相反的,比值較低的區域資料勝出率也低,因此資料亂數產生的機率並不平均,,錦標法改善了輪盤法的這個缺點。

( 指標 )

( 指標 )

tselect

Page 2: 錦標法

錦標法 : 每一資料都有各自的位址,每一位址存放著各自的資料,位址順序不變,將每一位址中的資料做交換,也就是打亂的動作

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

2 4 5 1 3

而後再由指標選出勝出的資料,錦標法的亂數產生,每一比資料的勝出率較為平均。

第 1 次交換 第 2 次交換 第 3 次交換

資料

位址

位址

資料

指標 交換後

Page 3: 錦標法

流程步驟 開始

利用 rand1 與 rand2做資料的打亂

由 tourneypos 選取勝出資料

比較 popsize 減 tourneypos是否小於 tourneysize

將勝出資料做比較挑取較小的資料

結束

Page 4: 錦標法

reset() { int i, rand1, rand2, temp; for(i=0; i<popsize; i++) tourneylist[i] = i; for(i=0; i < popsize; i++) { rand1=rnd(i,popsize-1);

rand2=rnd(i,popsize-1);

temp = tourneylist[rand1];

tourneylist[rand1]=tourneylist[rand2];

tourneylist[rand2]=temp; }}

整數變數 i, rand1, rand2, temp

製造資料陣列 tourneylist [i] ,其陣列資料數有 popsize 個。

程式迴圈執行 popsize 次。

從 0 到 popsize-1 隨機選一數值 , 將其值丟至 rand1 。

從 0 到 popsize-1 隨機選一數值 , 將其值丟至 rand2 。

陣列 tourneylist [i] , i 帶入 rand1 得 tourneylist [rand1] ,將此資料丟至 temp 中。

將 tourneylist[rand2] 的資料丟至 tourneylist[rand1] 中。

將 temp 中的資料丟至 tourneylist[rand2] 中。

restc 函式

程式 : 註解 :

Page 5: 錦標法

for(i=0; i<popsize; i++) tourneylist[i] = i;

整數資料數量 i 的範圍 , 從 0 到 popsize 減 1 個整數資料 , popsize 為一整數值 , 其值由使用者給定。 ( 迴圈中的最大值等於 popsize 減 1)

將 i 用至 tourneylist 陣列中可得 tourneylist[i] 整數資料 , 陣列資料從 0 開始不斷的加 1, 直到 popsize 減 1 停止。

迴圈 : popsize 為迴圈所執行的次數 , 迴圈每執行一次 , 就會產生一個整數資料。

範圍 = 0~popsize-1

tourneylist[0]

tourneylist[1]

tourneylist[2]

0 1 2

舉例 : 設 popsize=3; 迴圈執行 3 次。for(i=0; i<3; i++) tourneylist[i] = i;

產生 3 個陣列整數資料,資料內容由 0 到 2 。

tourneylist[0] tourneylist[~] tourneylist[popsize-1]

0 ~ popsize-1

Page 6: 錦標法

tourneylist [i] , i 帶入 rand1 ,既可得到 tourneylist[rand1] 這個資料,再將此資料丟入 temp 中。 (i 帶入 rand2 既可得 tourneylist[rand2])

temp

1接著把 tourneylist[rand2] 的資料丟到 tourneylist[rand1] ,最後再把 temp 的資料放置到 tourneylist[rand2] 。

從 popsize 中,隨機挑選一個數存放至 rand1 ,再隨機挑選一個數,存至rand2 。 ( 隨機挑選的兩個數有可能相同 )

temp

rand1 rand2

1 2

rand1=rnd(i,popsize-1);rand2=rnd(i,popsize-1);

tourneylist[rand1] tourneylist[rand2]

2temp = tourneylist[rand1];

tourneylist[rand1]=tourneylist[rand2]; tourneylist[rand2]=temp;

利用 rand1 與 rand2 做資料的打亂

temp

tourneylist[rand1] tourneylist[rand2]

2 1

Page 7: 錦標法

preselect(){ reset(); tourneypos = 0;}int selecting(){ int pick, winner, i; if((popsize - tourneypos) < tourneysize){ reset(); tourneypos = 0;} winner=tourneylist[tourneypos]; for(i=1; i<tourneysize; i++){ pick=tourneylist[i+tourneypos]; if(oldpop[pick].fitness <= oldpop[winner].fitness) winner=pick;}

tourneypos += tourneysize; return(winner);}

呼叫 reset() 這個函式,並將 tourneypos 歸零。

如果 popsize 減 tourneypos 小於 tourneysize 成立,程式執行 reset() 函式,並將 tourneypos 歸零。

從 tourneylist[i] 陣列中選出勝出者 winner, winner 等於 tourneylist[tourneypos] 這個陣列資料 。pick 挑選 tourneylist[tourneypos] 以下 tourneysize 減 1 個陣列資料 , 一個一個和 winner做比較 , 如果 oldpop[pick].fitness 小於或等於 oldpop[winner].fitness 成立 , 程式便將 pick 裡的資料 , 取代 winner 中的資料。

更新 tourneypos, tourneypos 等於 tourneypos 加 tourneysize 。程式返回 winner 。

程式 : 註解 :

Page 8: 錦標法

if((popsize - tourneypos) < tourneysize){ reset(); tourneypos = 0; }

winner=tourneylist[tourneypos]; for(i=1; i<tourneysize; i++) { pick=tourneylist[i+tourneypos]; if(oldpop[pick].fitness <= oldpop[winner].fitness) winner=pick; }

由指標選出勝出者,範圍從指標以下到tourneysize 減 1 個資料,並將較小值挑選出來。

亂數資料

( 範圍 :tourneysize 個資料 )

亂數資料

tourneypos

( 範圍 :tourneysize 個資料 )空白

如果 popsize 減 tourneypos 小於 tourneysize ,

那麼 tourneysize 內的資料,會有一部分是空白資料,因此當這樣的情況發生時,程式便執行 reset 這個函式並將 tourneypos 歸零。

popsize

popsize

tourneypos

Page 9: 錦標法

for(i=1; i<tourneysize; i++) { pick=tourneylist[i+tourneypos]; if(oldpop[pick].fitness <= oldpop[winner].fitness) winner=pick; }

由指標選出勝出者,範圍從指標以下到tourneysize 減 1 個資料,並將較小值挑選出來。

( 範圍 :tourneysize 個資料 )

popsize

tourneypos

winner=tourneylist[tourneypos];

tourneylist[i]

迴圈 : 程式的執行次數由 i 決定 ,i 從 0 到 popsize 減 1, 因此程式會執行 popsize 次。