1 / 9

錦標法

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

tivona
Télécharger la présentation

錦標法

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. tselect 錦標法 程式設計產生亂數的方法,大致上可分為輪盤法以及錦標法。 使用輪盤法設計程式的缺點:輪盤法是將資料劃分為區域長度,再由指標從中隨機挑選勝出的資料,當區域長度比值相同時,每一資料的勝出率也相等。 (指標) 當區域長度不相等時,則較高比值的區域資料,勝出率也比較高,相反的,比值較低的區域資料勝出率也低,因此資料亂數產生的機率並不平均,,錦標法改善了輪盤法的這個缺點。 (指標)

  2. 錦標法:每一資料都有各自的位址,每一位址存放著各自的資料,位址順序不變,將每一位址中的資料做交換,也就是打亂的動作錦標法:每一資料都有各自的位址,每一位址存放著各自的資料,位址順序不變,將每一位址中的資料做交換,也就是打亂的動作 位址 資料 第1次交換 第2次交換 第3次交換 而後再由指標選出勝出的資料,錦標法的亂數產生,每一比資料的勝出率較為平均。 位址 資料 指標 交換後

  3. 流程步驟 開始 利用rand1與rand2 做資料的打亂 由tourneypos選取勝出資料 是 比較popsize 減 tourneypos 是否小於tourneysize 否 將勝出資料做比較 挑取較小的資料 結束

  4. 程式: 註解: restc函式 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]中。

  5. for(i=0; i<popsize; i++) tourneylist[i] = i; 迴圈: popsize為迴圈所執行的次數,迴圈每執行一次,就會產生一個整數資料。 整數資料數量 i 的範圍,從0到popsize減1個整數資料, popsize為一整數值,其值由使用者給定。(迴圈中的最大值等於popsize減1) 將 i 用至tourneylist陣列中可得tourneylist[i] 整數資料,陣列資料從0開始不斷的加1,直到popsize減1停止。 範圍= 0~popsize-1 舉例:設popsize=3; for(i=0; i<3; i++) tourneylist[i] = i; 迴圈執行3次。 產生3個陣列整數資料,資料內容由0到2。

  6. 利用rand1與rand2做資料的打亂 從 popsize 中,隨機挑選一個數存放至rand1,再隨機挑選一個數,存至rand2。(隨機挑選的兩個數有可能相同) rand1=rnd(i,popsize-1); rand2=rnd(i,popsize-1); tourneylist [i],i帶入rand1,既可得到 tourneylist[rand1]這個資料,再將此資料丟入temp中。(i帶入rand2既可得tourneylist[rand2]) temp = tourneylist[rand1]; 接著把tourneylist[rand2]的資料丟到tourneylist[rand1],最後再把temp的資料放置到tourneylist[rand2]。 tourneylist[rand1]=tourneylist[rand2]; tourneylist[rand2]=temp;

  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。

  8. winner=tourneylist[tourneypos]; for(i=1; i<tourneysize; i++) { pick=tourneylist[i+tourneypos]; if(oldpop[pick].fitness <= oldpop[winner].fitness) winner=pick; } 亂 數 資 料 tourneypos (範圍:tourneysize個資料) popsize 由指標選出勝出者,範圍從指標以下到tourneysize減1個資料,並將較小值挑選出來。 if((popsize - tourneypos) < tourneysize) { reset(); tourneypos = 0; } 亂 數 資 料 popsize 如果popsize減tourneypos小於tourneysize, 那麼tourneysize內的資料,會有一部分是空白資料,因此當這樣的情況發生時,程式便執行reset這個函式並將tourneypos歸零。 tourneypos (範圍:tourneysize個資料) 空白

  9. winner=tourneylist[tourneypos]; for(i=1; i<tourneysize; i++) { pick=tourneylist[i+tourneypos]; if(oldpop[pick].fitness <= oldpop[winner].fitness) winner=pick; } tourneypos tourneylist[i] (範圍:tourneysize個資料) 由指標選出勝出者,範圍從指標以下到tourneysize減1個資料,並將較小值挑選出來。 popsize 迴圈:程式的執行次數由i決定,i 從0到popsize減1,因此程式會執行popsize次。

More Related