四灰色**
五遺傳演算法
六神經網路
先等等再學這個。。。
%主程式: 用遺傳演算法求解y = 200 * exp(-0.05 * x) .* sin(x) 再[-2 2]上的最大值
clc;
clear all;
close all;
global bitlength;
global st;
global ed;
bounds = [-2
2]; %一維變數的取值範圍
precision = 0.0001; %運算精度
st = bounds(:, 1);
ed = bounds(:, 2);
%計算需要多長的染色體
bitlength = ceil(log2((ed - st)' ./precision));
popsize = 50; %初始種群大小
generationmax = 12; %最大代數
procro = 0.90; %交配概率
promut = 0.09; %變異概率
%產生初始種群
population = round(rand(popsize, bitlength));
%計算適應度, 返回適應度fitval和累積概率prosum;
[fitval, prosum] = fitnessfun(population);
cur = 1;
while(cur < generationmax +1)
for j = 1 : 2 : popsize
%選擇操作
sel = selection(population, prosum);
%交叉操作
cro = cross(population, sel, procro);
temp(j, :) = cro(1, :);
temp(j + 1, :) = cro(2, :);
%變異操作
new(j, :) = mutation(temp(j, :), promut);
new(j + 1, :) = mytation(temp(j + 1, :), promut);
endpopulation = new; %新種群
%計算新種群的最好的適應度和平均適應度
[fitval, prosum] = fitnessfun(popalation);
[fmax, nmax] = max(fitval);
fmean = mean(fitval);
ymax(cur) = fmax;
ymean(cur) = fmean;
%記錄當前代最佳染色體個體
x = tr2to10(population(nmax, :));
%自變數的取值範圍是[-2 2], 需要把經過遺傳運算的最佳染色體整合到對應區間
xx = st + x * (ed - st) / (power(2, bitlength) - 1);
xmax(cur) = xx;
cur = cur + 1;
endcur = cur - 1;
bestpopulation = xx;
besettargetfunval = targetfun(xx);
%繪製曲線
figure(1);
hand1 = plot(1 : cur, ymax);
set(head1, '
linestyle', '-', 'linewidth', 1.8, 'marker', '*', 'markersize', 6);
hold on;
hand2 = plot(1 : cur, ymean);
set(hand1, 'color', 'r', 'linestyle', '-', 'linewidth', 1.8, 'marker', 'h', 'markersize', 6);
xlabel('進化代數'); ylabel('最大\平均適應度');
xlim([1 generationmax]);
legend('最大適應度', '平均適應度');
box off;
hold off;
%交叉操作
function
temp = cross
(population, sel, pro);
bitlength = size(population, 2);
pcc = calpro(pro);
if pcc == 1
pos = round(rand * (bitlength - 2)) + 1; %在[1, bitlength - 1]範圍內隨機pos
temp(1, :) = [population(sel(1), 1 : pos) population(sel(2), pos + 1 : bitlength)];
temp(2, :) = [populayion(sel(2), 1 : pos) population(sel(1), pos + 1 : bitlength)];
else
temp(1, :) = population(sel(1), :);
temp(2, :) = population(sel(2), :);
end%計算適應度函式
function
[fitval, prosum] = fitnessfun
(population);
global bitlength;
global st;
global ed;
popsize = size(population, 1);
fori = 1 : popsize
x = tr2to10(population(i, :));
xx = st + x * (ed - st) / (power(2, bitlength) - 1);
fitval(i) = targetfun(xx);
endfitval = fitval' +233;
%計算選擇概率
fsum = sum(fitval);
temp = fitval / fsum;
%計算累積概率
prosum(1) = temp(1);
fori = 2 : popsize
prosum(i) = prosum(i - 1) + temp(i);
endprosum = prosum';
end%新種群變異操作
function
new = mutation
(temp, promut)
bitlength = size(temp, 2);
new = temp;
pmm = calpro(promut);
if(pmm == 1)
pos = round(rand * (bitlength - 1)) + 1;
new(pos) = abs(temp(pos) - 1);
endend
%計算概率
function
pcc = calpro
(pc)
temp(1 : 100) = 0;
cnt = round(100 * pc);
test(1 : cnt) = 1;
pos = round(rand * 99) + 1;
pcc = test(pos);
end%選擇操作
function
sel = selection
(population, prosum)
fori = 1 : 2
r = rand;
prand = prosum - r;
j = 1;
while prand(j) < 0
j = j + 1;
endsel(i) = j;
endend
%進製轉換
function
x = tr2to10
(population)
bitlength = size(population, 2);
x = population(bitlength);
fori = 1 : bitlength - 1
x = x + population(bitlength - i)* power(2, i);
endend
%目標函式
function
y = targetfun
(x)y = 200 * exp(-0.05 * x) .* sin(x);
endend
MATLAB基礎學習筆記(二)
基本步驟 三種基本結構 順序 選擇 迴圈 程式檔案拓展名 m吧 指令碼檔案 也叫命令檔案 函式檔案 指令碼檔案 呼叫 在命令列視窗輸入檔名 函式檔案 呼叫 直接在命令列視窗呼叫函式,函式檔名與函式名要一致 工作的目錄必須一致 cd命令查詢目前所在資料夾,cd e 資料夾修改所在資料夾 檔案的建立 順...
Matlab基礎學習筆記 二 2021 1 26
三角陣矩陣轉置 矩陣旋轉 矩陣翻轉 矩陣求逆 矩陣求值 特徵值與特徵向量 稀疏矩陣 diag a diag a,k 提取矩陣a的對角線元素 提取矩陣a的第k條對角線元素 產生乙個列向量 diag v diag v,k 以向量v為主對角線元素,產生對角矩陣 一向量v為第k條對角線元素,產生對角矩陣 例...
matlab學習筆記
一致化矩陣 z1 1 2 4 3 4 1 z2 1 2 2 5 6 1 b 0 1 q 3 z concur b,q 向量之和 x1 netsum z1,z2 向量之積 x2 netprod z1,z2 感知器神經網路,完成 或 的計算 err goal 0.001 max epoch 500 x ...