基因演算法 學習速成 南台科技大學電機系趙春棠講解

Similar documents
0 0 = 1 0 = 0 1 = = 1 1 = 0 0 = 1

Algorithms and Complexity

= lim(x + 1) lim x 1 x 1 (x 2 + 1) 2 (for the latter let y = x2 + 1) lim

Chapter 20 Cell Division Summary

Differential Equations (DE)

國立中正大學八十一學年度應用數學研究所 碩士班研究生招生考試試題

生物統計教育訓練 - 課程. Introduction to equivalence, superior, inferior studies in RCT 謝宗成副教授慈濟大學醫學科學研究所. TEL: ext 2015

Chapter 22 Lecture. Essential University Physics Richard Wolfson 2 nd Edition. Electric Potential 電位 Pearson Education, Inc.

2019 年第 51 屆國際化學奧林匹亞競賽 國內初選筆試 - 選擇題答案卷

授課大綱 課號課程名稱選別開課系級學分 結果預視

Chapter 6. Series-Parallel Circuits ISU EE. C.Y. Lee

Chapter 1 Linear Regression with One Predictor Variable

HKDSE Chemistry Paper 2 Q.1 & Q.3

tan θ(t) = 5 [3 points] And, we are given that d [1 points] Therefore, the velocity of the plane is dx [4 points] (km/min.) [2 points] (The other way)

Frequency Response (Bode Plot) with MATLAB

Elementary Number Theory An Algebraic Apporach

1 dx (5%) andˆ x dx converges. x2 +1 a

邏輯設計 Hw#6 請於 6/13( 五 ) 下課前繳交

Ch.9 Liquids and Solids

pseudo-code-2012.docx 2013/5/9

KWUN TONG GOVERNMENT SECONDARY SCHOOL 觀塘官立中學 (Office) Shun Lee Estate Kwun Tong, Kowloon 上學期測驗

Candidates Performance in Paper I (Q1-4, )

原子模型 Atomic Model 有了正確的原子模型, 才會發明了雷射

論文與專利寫作暨學術 倫理期末報告 班級 : 碩化一甲學號 :MA 姓名 : 林郡澤老師 : 黃常寧

台灣大學開放式課程 有機化學乙 蔡蘊明教授 本著作除另有註明, 作者皆為蔡蘊明教授, 所有內容皆採用創用 CC 姓名標示 - 非商業使用 - 相同方式分享 3.0 台灣授權條款釋出

Multiple sequence alignment (MSA)

適應控制與反覆控制應用在壓電致動器之研究 Adaptive and Repetitive Control of Piezoelectric Actuators

期中考前回顧 助教 : 王珊彗. Copyright 2009 Cengage Learning

Candidates Performance in Paper I (Q1-4, )

GSAS 安裝使用簡介 楊仲準中原大學物理系. Department of Physics, Chung Yuan Christian University

14-A Orthogonal and Dual Orthogonal Y = A X

相關分析. Scatter Diagram. Ch 13 線性迴歸與相關分析. Correlation Analysis. Correlation Analysis. Linear Regression And Correlation Analysis

EXPERMENT 9. To determination of Quinine by fluorescence spectroscopy. Introduction

Advanced Engineering Mathematics 長榮大學科工系 105 級

ApTutorGroup. SAT II Chemistry Guides: Test Basics Scoring, Timing, Number of Questions Points Minutes Questions (Multiple Choice)

Linear Regression. Applied Linear Regression Models (Kutner, Nachtsheim, Neter, Li) hsuhl (NUK) SDA Regression 1 / 34

Chapter 1 Linear Regression with One Predictor Variable

壓差式迴路式均熱片之研製 Fabrication of Pressure-Difference Loop Heat Spreader

Statistics and Econometrics I

國立成功大學 航空太空工程學系 碩士論文 研究生 : 柯宗良 指導教授 : 楊憲東

5.5 Using Entropy to Calculate the Natural Direction of a Process in an Isolated System

磁振影像原理與臨床研究應用 課程內容介紹 課程內容 參考書籍. Introduction of MRI course 磁振成像原理 ( 前 8 週 ) 射頻脈衝 組織對比 影像重建 脈衝波序 影像假影與安全 等

Chapter 1 Physics and Measurement

在雲層閃光放電之前就開始提前釋放出離子是非常重要的因素 所有 FOREND 放電式避雷針都有離子加速裝置支援離子產生器 在產品設計時, 為增加電場更大範圍, 使用電極支援大氣離子化,

2. Suppose that a consumer has the utility function

REAXYS NEW REAXYS. RAEXYS 教育訓練 PPT HOW YOU THINK HOW YOU WORK

Sparse Learning Under Regularization Framework

Earth System Science Programme. Academic Counseling 2018

Learning to Recommend with Location and Context

Chapter 8 Lecture. Essential University Physics Richard Wolfson 2 nd Edition. Gravity 重力 Pearson Education, Inc. Slide 8-1

行政院國家科學委員會補助專題研究計畫 成果報告 期中進度報告 ( 計畫名稱 )

FUNDAMENTALS OF FLUID MECHANICS. Chapter 8 Pipe Flow. Jyh-Cherng. Shieh Department of Bio-Industrial

PHI7470 Topics in Applied Philosophy: The Philosopher and Sociology

統計學 Spring 2011 授課教師 : 統計系余清祥日期 :2011 年 3 月 22 日第十三章 : 變異數分析與實驗設計

Chapter 7. The Quantum- Mechanical Model of the Atom. Chapter 7 Lecture Lecture Presentation. Sherril Soman Grand Valley State University

CH 5 More on the analysis of consumer behavior

Digital Integrated Circuits Lecture 5: Logical Effort

醫用磁振學 MRM 課程介紹與原理複習. Congratulations! Syllabus. From Basics to Bedside. You are HERE! License of Radiological Technologist 盧家鋒助理教授國立陽明大學生物醫學影像暨放射科學系

Boundary Influence On The Entropy Of A Lozi-Type Map. Cellular Neural Networks : Defect Patterns And Stability

Lecture Notes on Propensity Score Matching

允許學生個人 非營利性的圖書館或公立學校合理使用本基金會網站所提供之各項試題及其解答 可直接下載而不須申請. 重版 系統地複製或大量重製這些資料的任何部分, 必須獲得財團法人臺北市九章數學教育基金會的授權許可 申請此項授權請電郵

大原利明 算法点竄指南 点竄術 算 額 絵馬堂

雷射原理. The Principle of Laser. 授課教授 : 林彥勝博士 Contents

MRDFG 的周期界的計算的提升計畫編號 :NSC E 執行期限 : 94 年 8 月 1 日至 94 年 7 月 31 日主持人 : 趙玉政治大學資管系計畫參與人員 :

FUNDAMENTALS OF FLUID MECHANICS Chapter 3 Fluids in Motion - The Bernoulli Equation

A Direct Simulation Method for Continuous Variable Transmission with Component-wise Design Specifications

Ch2 Linear Transformations and Matrices

Revised Dynamic Optimal Training of Three-Layer Neural Network

Finite Interval( 有限區間 ) open interval ( a, closed interval [ ab, ] = { xa x b} half open( or half closed) interval. Infinite Interval( 無限區間 )

The dynamic N1-methyladenosine methylome in eukaryotic messenger RNA 报告人 : 沈胤

國立交通大學 電子工程學系電子研究所碩士班 碩士論文

ANSYS 17 應用於半導體設備和製程的應用技術

Earth System Science Programme 地球系統科學課程 Tel: Fax:

On the Quark model based on virtual spacetime and the origin of fractional charge

在破裂多孔介質中的情形 底下是我們考慮的抛物線微分方程式. is a domain and = f. in f. . Denote absolute permeability by. P in. k in. p in. and. and. , and external source by

Digital Image Processing

Regression Analysis. Institute of Statistics, National Tsing Hua University, Taiwan

ON FINITE DIMENSIONAL APPROXIMATION IN NONPARAMETRIC REGRESSION

氮化鋁鎵 / 氮化鎵異質結構的電性傳輸. Electrical transport in AlGaN/GaN heterostructures

Numbers and Fundamental Arithmetic

2012 AP Calculus BC 模拟试卷

課程名稱 : 電路學 (2) 授課教師 : 楊武智 期 :96 學年度第 2 學期

GRE 精确 完整 数学预测机经 发布适用 2015 年 10 月考试

Tutorial Courses

奈米微污染控制工作小組 協辦單位 台灣賽默飛世爾科技股份有限公司 報名方式 本參訪活動由郭啟文先生負責 報名信箱

INTERNATIONAL BIOLOGY OLYMPIAD

Hong Kong s temperature record: Is it in support of global warming? 香港的溫度記錄 : 全球暖化的證據?

號碼 (No.): KA/2011/10550 日期 (Date): 2011/01/20 頁數 (Page): 1 of 7

CHAPTER 2. Energy Bands and Carrier Concentration in Thermal Equilibrium

Ph.D. Qualified Examination

測試報告 Test Report 號碼 (No.) : CE/2013/A2425 日期 (Date) : 2013/10/18 頁數 (Page) : 1 of 10 鈞寶電子工業股份有限公司 KING CORE ELECTRONICS INC. 桃園縣平鎮市平鎮工業區南豐路 269 號 NO.

國立交通大學 電信工程學系 碩士論文 考慮晶片上互感及電阻 電感 電容雜訊之漏話 導向電路佈局. Crosstalk-Driven Placement with Considering On-Chip Mutual. Inductance and RLC Noise 研究生 : 邱震軒

頁數 (Page): 2 of 12 測試結果 (Test Results) 測試部位 (PART NAME)No.1 : 銅色片狀 (COPPER COLORED SHEET) 測試項目 (Test Items) 單位 (Unit) 測試方法 (Method) 方法偵測極限值 (MDL) 結果 (

請見下一頁 (Please refer to next pages).

測試報告 Test Report 號碼 (No.) : CE/2015/60145 日期 (Date) : 2015/06/04 鈞寶電子工業股份有限公司 KING CORE ELECTRONICS INC. 桃園市平鎮區平鎮工業區南豐路 269 號 NO. 269, NANFONG RD., PI

d) There is a Web page that includes links to both Web page A and Web page B.

Permutation Tests for Difference between Two Multivariate Allometric Patterns

Ph.D. Qualified Examination

Chapter 7 Propositional and Predicate Logic

Transcription:

基因演算法 學習速成 南台科技大學電機系趙春棠講解 % 以下程式作者 : 清大張智星教授, 摘自 Neuro-Fuzzy and Soft Computing, J.-S. R. Jang, C.-T. Sun, and E. Mizutani 讀者可自張教授網站下載該書籍中的所有 Matlab 程式 % 主程式 : go_ga.m % 這是書中的一個範例, 了解每一個程式指令後, 大概就對 基因演算法, 就能有一全面性的初步了解了! clear all; close all; clc; generation_n = 30; % Number of generations popusize = 20; % Population size xover_rate = 1.0; % Crossover rate mutate_rate = 0.01; % Mutation rate bit_n = 8; % Bit number for each input variable global OPT_METHOD % optimization method. OPT_METHOD = 'ga'; % This is used for display in peaksfcn figure; blackbg; % 執行 blackbg.m obj_fcn = 'peaksfcn'; % Objective function: 'peaksfcn' or 'wavefcn' if strcmp(obj_fcn, 'peaksfcn'),% objective function is 'peaksfcn' var_n = 2; % Number of input variables range = [-3, 3; -3, 3]; % Range of the input variables % Plot peaks function peaks; % 輸出如下 ( 這是本例所解的問題 : 兩個輸入變數的函數, 求極值 )

colormap((jet+white)/2); % Plot contours of peaks function figure; blackbg; [x, y, z] = peaks; pcolor(x,y,z); shading interp; hold on; contour(x, y, z, 20, 'r'); hold off; colormap((jet+white)/2); axis square; xlabel('x'); ylabel('y'); % 輸出如下 else % objective function is 'wavefcn' var_n = 1; % Number of input variables range = [0 pi]; % Range of the input variables x = linspace(range(1), range(2)); y = sin(10*x).*sin(x); plot(x, sin(10*x).*sin(x)); axis([-inf inf -inf inf]); % 輸出如下 ( 這是另一個求解的問題 : 單一輸入變數 )

% Initial random population popu = rand(popusize, bit_n*var_n) > 0.5; % 本例即 rand(20, 8*2) 某次輸出 ( 主程式 i=1):popu (20*16)= 1 0 0 0 0 0 1 0 1 1 1 0 0 1 0 0 0 0 0 1 0 1 1 1 0 0 1 0 0 0 1 0 0 1 0 0 1 1 1 0 1 0 1 0 1 0 1 0 1 1 0 0 1 1 0 1 1 0 0 0 1 1 1 0 upper = zeros(generation_n, 1); % 本例即 zeros(30, 1) average = zeros(generation_n, 1); % 本例即 zeros(30, 1) lower = zeros(generation_n, 1); %% 本例即 zeros(30, 1) % Main loop of GA for i = 1:generation_n; % 本例作 30 世代 % delete unnecessary objects delete(findobj(0, 'tag', 'member')); delete(findobj(0, 'tag', 'individual')); delete(findobj(0, 'tag', 'count')); % Evaluate objective function for each individual fcn_value = evalpopu(popu, bit_n, range, obj_fcn); 某次輸出 :( 主程式 i=1) fcn_value (20*1) = 0.3304

0.5162 0.5897-0.2983 * 將以上 popu 二進位碼, 計算出對應的數值, 代入函數中即得上述結果 % if (i==1), % fprintf('initial population.\n'); % fprintf('press any key to continue...\n'); % pause; % % Fill objective function matrices upper(i) = max(fcn_value); % upper average lower 在本例都是 size=30*1 喔! average(i) = mean(fcn_value); lower(i) = min(fcn_value); % display current best [best, index] = max(fcn_value); fprintf('generation %i: ', i); if strcmp(obj_fcn, 'peaksfcn'),% obj. function is 'peaksfcn' fprintf('f(%f, %f)=%f\n',... bit2num(popu(index, 1:bit_n), range(1,:)),... bit2num(popu(index, bit_n+1:2*bit_n), range(2,:)),... best); 例 : 某次最佳輸出 f(-0.223529, 1.000000)=3.628006 ( 主程式 i=1) [best, index] = max(fcn_value); % 其中 best = 3.6280 index = 19 (i=1) 說明 : popu(19,:) = 0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 bit2num([0 1 1 1 0 1 1 0], [-3, 3])= -0.2235 bit2num([1 0 1 0 1 0 1 0], [-3, 3])= 1 else % obj. function is 'wavefcn' fprintf('f(%f)=%f\n', bit2num(popu(index, :), range), best); % generate next population via selection, crossover and mutation popu = nextpopu(popu, fcn_value, xover_rate, mutate_rate); % if (i==5),

% fprintf('population after the 5th generation.\n'); % fprintf('press any key to continue...\n'); % pause; % % if (i==10), % fprintf('population after the 10th generation.\n'); % fprintf('press any key to continue...\n'); % pause; % figure; blackbg; x = (1:generation_n)'; plot(x, upper, 'o', x, average, 'x', x, lower, '*'); hold on; plot(x, [upper average lower]); hold off; leg('best', 'Average', 'Poorest'); xlabel('generations'); ylabel('fitness');

Generation 1: f(0.317647, 2.270588)=3.144607 Generation 2: f(1.470588, -0.152941)=3.279510 Generation 3: f(1.376471, -0.152941)=3.456410 Generation 30: f(0.058824, 1.494118)=7.941152 以下說明其他相關副程式 function blackbg % Change figure background to black % Issue this to change the background to black (V4 default) % Roger Jang, 981210 tmp = version; 註 : tmp = 7.0.0.19920 (R14) % 以 Matlab 第七版為例 tmp(1)= 7 tmp(2)=. tmp(3)= 0 if str2num(tmp(1))==5, clf; colordef(gcf, 'black'); 註 : clf % 清除畫面 colordef(gcf, 'black') % 背景設為黑色 function fitness = evalpopu(population, bit_n, range, fcn) %EVALPOPU Evaluation of the population's fitness values. % population: 0-1 matrix of popu_n by string_leng % bit_n: number of bits used to represent an input variable % range: range of input variables, a var_b by 2 matrix % fcn: objective function (a MATLAB string) global count pop_n = size(population, 1); fitness = zeros(pop_n, 1); for count = 1:pop_n, fitness(count) = evaleach(population(count, :), bit_n, range, fcn); function out = evaleach(string, bit_n, range, fcn) % EVALEACH Evaluation of each individual's fitness value. % bit_n: number of bits for each input variable % string: bit string representation of an individual % range: range of input variables, a ver_n by 2 matrix

% fcn: objective function (a MATLAB string) var_n = length(string)/bit_n; input = zeros(1, var_n); for i = 1:var_n, input(i) = bit2num(string((i-1)*bit_n+1:i*bit_n), range(i, :)); out = feval(fcn, input); function num = bit2num(bit, range) % BIT2NUM Conversion from bit string representations to decimal numbers. % BIT2NUM(BIT, RANGE) converts a bit string representation BIT ( a 0-1 % vector) to a decimal number, where RANGE is a two-element vector % specifying the range of the converted decimal number. % % For example: % % bit2num([1 1 0 1], [0, 15]) % bit2num([0 1 1 0 0 0 1], [0, 127]) % Roger Jang, 12-24-94 integer = polyval(bit, 2); num = integer*((range(2)-range(1))/(2^length(bit)-1)) + range(1); function ver = matlabv % MATLAB major version tmp = version; ver = str2num(tmp(1)); function z = peaksfcn(input) %PEAKSFCN The PEAKS function. % PEAKSFCN(INPUT) returns the value of the PEAKS function at the INPUT. % % See also PEAKS. % Roger Jang, 12-24-94.

global OPT_METHOD % optimization method global PREV_PT % previous data point, used by simplex x = input(1); y = input(2); % The following function should be the same as the one in PEAKS.M. z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2)... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2)... - 1/3*exp(-(x+1).^2 - y.^2); if matlabv==4, property='linestyle'; %elseif matlabv==5, else property='marker'; %else % error('unknown MATLAB version!'); % Plotting... if strcmp(opt_method, 'ga'), % plot each member; for GA line(x, y, property, 'o', 'markersize', 15,... 'clipping', 'off', 'erase', 'xor', 'color', 'w',... 'tag', 'member', 'linewidth', 2); else % plot input point for simplex method line(x, y, property, '.', 'markersize', 10,... 'clipping', 'off', 'erase', 'none', 'color', 'k',... 'tag', 'member'); if ~isempty(prev_pt), % plotting traj line([prev_pt(1) x], [PREV_PT(2) y], 'linewidth', 1,... 'clipping', 'off', 'erase', 'none',... 'color', 'k', 'tag', 'traj'); else % plotting starting point % line(x, y, property, 'o', 'markersize', 10,... % 'clipping', 'off', 'erase', 'none',... % 'color', 'w', 'tag', 'member', 'linewidth', 3); PREV_PT = [x y];

drawnow; function new_popu = nextpopu(popu, fitness, xover_rate, mut_rate) % generate next population via selection( 選擇 ), crossover( 交配 ) and mutation ( 突變 ) % 呼叫本副程式例 : popu = nextpopu(popu, fcn_value, xover_rate, mutate_rate); new_popu = popu; popu_s = size(popu, 1); string_leng = size(popu, 2); 本例說明 : >> size(popu) ans = 20 16 >> popu_s = size(popu, 1) popu_s = 20 >> string_leng = size(popu, 2) string_leng = 16 % ====== ELITISM: find the best two and keep them tmp_fitness = fitness; % fitness 即是 fcn_value [junk, index1] = max(tmp_fitness); % find the best 本例某次 : junk = 3.6280 index1 = 19 ( 主程式 i=1) tmp_fitness(index1) = min(tmp_fitness); % 本例某次 : tmp_fitness(19) = -3.0034 ( 主程式 i=1) [junk, index2] = max(tmp_fitness); % find the second best 本例某次 : junk = 2.8788 index2 = 3 ( 主程式 i=1) new_popu([1 2], :) = popu([index1 index2], :); 說明 : 本例某次 popu([index1 index2], :) 即是 popu([19 3], :) ans = 0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 1 0 1 1 0 1 1 1 1 0 0 0 0 0 以上最好的兩組, 將被保留 % rescaling the fitness fitness = fitness - min(fitness); % keep it positive 恆正,fitness(20*1 喔!) total = sum(fitness); % 本例某次 :total = 68.7807 ( 主程式 i=1) if total == 0, fprintf('=== Warning: converge to a single point ===\n');

fitness = ones(popu_s, 1)/popu_s; % sum is 1 else fitness = fitness/sum(fitness); % sum is 1 說明 : 本例某次 ( 主程式 i=1),after fitness scaling fitness = 0.0429 0.0429 0.0855. 0.0385 cum_prob = cumsum(fitness); 說明 : 本例某次 ( 主程式 i=1) cum_prob = 0.0429 0.0858 0.1713. 1.0000 % ====== SELECTION and CROSSOVER for i = 2:popu_s/2, % 本例 i = 2: 20/2 = 2:10 ( 共 9 次, 每次得兩個基因, 共 18 個, 加上之前保留最佳的兩個, 共 20 個 ) % === Select two parents based on their scaled fitness values tmp = find(cum_prob - rand > 0); 說明 : 本例某次 ( 主程式 i=1, 本副程式 i=1) tmp = 16 17 18 19 20 parent1 = popu(tmp(1), :); 說明 : 本例某次 ( 主程式 i=1, 本副程式 i=1) Parent1 = 0 1 0 0 1 0 0 0 1 1 0 0 0 1 0 1

tmp = find(cum_prob - rand > 0); 說明 : 本例某次 ( 主程式 i=1, 本副程式 i=1) tmp (19*1)= 2 3. 20 parent2 = popu(tmp(1), :); 說明 : 本例某次 ( 主程式 i=1, 本副程式 i=1) Parent2 = 0 0 0 0 1 1 1 0 1 0 1 0 1 0 1 1 % === Do crossover if rand < xover_rate, % 本例 xover_rate = 1.0; % Perform crossover operation xover_point = ceil(rand*(string_leng-1)); % 本例 string_leng = 16 說明 : 本例某次 ( 主程式 i=1, 本副程式 i=1) >> rand*(string_leng-1) ans = 1.7225 >> ceil(ans) ans = 2 ( 故 xover_point =2) 註 : CEIL(X) rounds the elements of X to the nearest integers towards infinity. new_popu(i*2-1, :) =... [parent1(1:xover_point) parent2(xover_point+1:string_leng)]; new_popu(i*2, :) =... [parent2(1:xover_point) parent1(xover_point+1:string_leng)]; 說明 : 本例某次 ( 主程式 i=1, 本副程式 i=1), 交配 結果整理 (xover_point =2) Parent1 = 0 1 0 0 1 0 0 0 1 1 0 0 0 1 0 1 Parent2 = 0 0 0 0 1 1 1 0 1 0 1 0 1 0 1 1 產生子代如下 : new_popu1 = 0 1 0 0 1 1 1 0 1 0 1 0 1 0 1 1 new_popu2 = 0 0 0 0 1 0 0 0 1 1 0 0 0 1 0 1 % fprintf('xover_point = %d\n', xover_point); % disp(parent1); % disp(parent2); % disp(new_popu(i*2-1, :));

% disp(new_popu(i*2, :)); % keyboard; % ====== MUTATION (elites are not subject to this.) mask = rand(popu_s, string_leng) < mut_rate; % 本例 popu_s = 20 string_leng = 16 mutate_rate = 0.01 說明 : 本例某次 ( 主程式 i=1, 本副程式 i=1) 結果 mask(20*16) = 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 new_popu = xor(new_popu, mask); % 上述 mask 表中, 若為 1 者, 表示對 應的該染色體會突變 ( 1 => 0, 0 => 1 ) % restore the elites new_popu([1 2], :) = popu([index1 index2], :); % 可見前面所保留最好的基 因, 並不允許突變 後記 晚學執行以上程式後, 明瞭了 基因演算法 的整個大概的流程, 不過對於 new_popu.m 程式中的 交配 過程, 感覺似乎並不是很妥當 以前述兩個母代 ( 如下 ) 交配為例 : Parent1 = 0 1 0 0 1 0 0 0 1 1 0 0 0 1 0 1 Parent2 = 0 0 0 0 1 1 1 0 1 0 1 0 1 0 1 1 其實我們知道在 Parent1 中, 前 8 個 bits 0 1 0 0 1 0 0 0 代表的是一個變 數 ; 後 8 個 bits 1 1 0 0 0 1 0 1 代表的是另一個變數, 彼此之間理論上應是 相 互獨立 的, 所以作 交配 時, 也應該分開作才合理 於是筆者更動部份程式 如下 : new_popu.m( 程式修改 ).( 略 ). string_leng = size(popu, 2); string_leng_hf = string_leng/2;.( 略 ). % 新增指令 % ====== SELECTION and CROSSOVER for i = 2:popu_s/2,

% === Select two parents based on their scaled fitness values tmp = find(cum_prob - rand > 0); parent1 = popu(tmp(1), :); parent1_hl = popu(tmp(1), 1:string_leng_hf); parent1_hr = popu(tmp(1), string_leng_hf+1:); % 新增指令 tmp = find(cum_prob - rand > 0); parent2_hl = popu(tmp(1), 1:string_leng_hf); parent2_hr = popu(tmp(1), string_leng_hf+1:); % 新增指令 parent2 = popu(tmp(1), :); % === Do crossover if rand < xover_rate, % Perform crossover operation % xover_point = ceil(rand*(string_leng-1)); 原指令 xover_point = ceil(rand*(string_leng_hf-1)); % 新指令 %new_popu(i*2-1, :) =... 原指令 % [parent1(1:xover_point) parent2(xover_point+1:string_leng) ]; % 原指令 new_popu(i*2-1, :) =... [parent1_hl(1:xover_point) parent2_hl(xover_point+1:string_leng_hf) parent1_hr(1:xover_point) parent2_hr(xover_point+1:string_leng_hf)]; %new_popu(i*2, :) =... 原指令 % [parent2(1:xover_point) parent1(xover_point+1:string_leng)]; % 原指令 new_popu(i*2, :) =... [parent2_hl(1:xover_point) parent1_hl(xover_point+1:string_leng_hf) parent2_hr(1:xover_point) parent1_hr(xover_point+1:string_leng_hf)]; 說明 : 本例某次 ( 主程式 i=1, 本副程式 i=1), 交配 結果整理(xover_point =2) Parent1 = 0 1 0 0 1 0 0 0(hl 部分 ) 1 1 0 0 0 1 0 1(hr 部分 ) Parent2 = 0 0 0 0 1 1 1 0(hl 部分 ) 1 0 1 0 1 0 1 1(hr 部分 ) 產生子代如下 : new_popu1 = 0 1 0 0 1 1 1 0 1 1 1 0 1 0 1 1 new_popu2 = 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 1

以下是某次執行的結果, 我們發現有時效果好許多喔! 僅作為讀者的參考! 此外, 無論是原作者的程式, 或是晚學修改後的程式, 讀者不妨多執行幾次看看, 會發現有時候會落入 local 的 極值 喔! 可見得這個程式仍然是有問題的 除了可以改變 mutation rate 試試以外, 本程式在 交配 時 母代 的選擇上, 似乎是完全隨機的, 此點似乎可改善,