matlab神經網路編程
1. MATLAB線性神經網路的程序,跪求。。
美國Michigan 大學的 Holland 教授提出的遺傳演算法(GeneticAlgorithm, GA)是求解復雜的組合優化問題的有效方法 ,其思想來自於達爾文進化論和門德爾松遺傳學說 ,它模擬生物進化過程來從龐大的搜索空間中篩選出較優秀的解,是戚鎮一種高效而且具有強魯棒性方法。所以,遺傳演算法在求解TSP和 MTSP問題中得到了廣泛的應用。
matlab程序如下:
function[opt_rte,opt_brk,min_dist] =mtspf_ga(xy,dmat,salesmen,min_tour,pop_size,num_iter)
%%
%實例
% n = 20;%城市個數
% xy = 10*rand(n,2);%城市坐標 隨機產生,也可以自己設定
% salesmen = 5;%旅行商個數
% min_tour = 3;%每個旅行商最少訪問的城市數
% pop_size = 80;%種群個數
% num_iter = 200;%迭代次數
% a = meshgrid(1:n);
% dmat =reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),n,n);
% [opt_rte,opt_brk,min_dist] = mtspf_ga(xy,dmat,salesmen,min_tour,...
% pop_size,num_iter);%函數
%%
[N,dims]= size(xy); %城市矩陣大小
[nr,nc]= size(dmat); %城市距離矩陣大小
n = N -1;% 除去起始的城市後剩餘的城市的數
% 初始化路線、斷點的選擇
num_brks= salesmen-1;
dof = n- min_tour*salesmen; %初始化路線、斷點的選擇
addto =ones(1,dof+1);
for k =2:num_brks
addto = cumsum(addto);
end
cum_prob= cumsum(addto)/sum(addto);
%% 初始化種群
pop_rte= zeros(pop_size,n); % 種群路徑
pop_brk= zeros(pop_size,num_brks); % 斷點集合的種群
for k =1:pop_size
pop_rte(k,:) = randperm(n)+1;
pop_brk(k,:) = randbreaks();
end
% 畫圖路徑曲線顏色
clr =[1 0 0; 0 0 1; 0.67 0 1; 0 1 0; 1 0.5 0];
ifsalesmen > 5
clr = hsv(salesmen);
end
%%
% 基於遺傳演算法的MTSP
global_min= Inf; %初始化最短路徑
total_dist= zeros(1,pop_size);
dist_history= zeros(1,num_iter);
tmp_pop_rte= zeros(8,n);%當前的路徑設置
tmp_pop_brk= zeros(8,num_brks); %當前的斷點設置
new_pop_rte= zeros(pop_size,n);%更新的路徑設置
new_pop_brk= zeros(pop_size,num_brks);%更新的斷點設置
foriter = 1:num_iter
% 計算適應值
for p = 1:pop_size
d = 0;
p_rte = pop_rte(p,:);
p_brk = pop_brk(p,:);
rng = [[1 p_brk+1];[p_brk n]]';
for s = 1:salesmen
d = d + dmat(1,p_rte(rng(s,1)));% 添加開始的路徑
for k = rng(s,1):rng(s,2)-1
d = d + dmat(p_rte(k),p_rte(k+1));
end
滲旁 d = d + dmat(p_rte(rng(s,2)),1); % 添加結束的的路徑
end
叢仔橡 total_dist(p) = d;
end
% 找到種群中最優路徑
[min_dist,index] = min(total_dist);
dist_history(iter) = min_dist;
if min_dist < global_min
global_min = min_dist;
opt_rte = pop_rte(index,:); %最優的最短路徑
opt_brk = pop_brk(index,:);%最優的斷點設置
rng = [[1 opt_brk+1];[opt_brk n]]';%設置記錄斷點的方法
figure(1);
for s = 1:salesmen
rte = [1 opt_rte(rng(s,1):rng(s,2))1];
plot(xy(rte,1),xy(rte,2),'.-','Color',clr(s,:));
title(sprintf('城市數目為 = %d,旅行商數目為 = %d,總路程 = %1.4f, 迭代次數 =%d',n+1,salesmen,min_dist,iter));
hold on
grid on
end
plot(xy(1,1),xy(1,2),'ko');
hold off
end
% 遺傳操作
rand_grouping = randperm(pop_size);
for p = 8:8:pop_size
rtes = pop_rte(rand_grouping(p-7:p),:);
brks = pop_brk(rand_grouping(p-7:p),:);
dists =total_dist(rand_grouping(p-7:p));
[ignore,idx] = min(dists);
best_of_8_rte = rtes(idx,:);
best_of_8_brk = brks(idx,:);
rte_ins_pts = sort(ceil(n*rand(1,2)));
I = rte_ins_pts(1);
J = rte_ins_pts(2);
for k = 1:8 %產生新種群
tmp_pop_rte(k,:) = best_of_8_rte;
tmp_pop_brk(k,:) = best_of_8_brk;
switch k
case 2% 倒置操作
tmp_pop_rte(k,I:J) =fliplr(tmp_pop_rte(k,I:J));
case 3 % 互換操作
tmp_pop_rte(k,[I J]) =tmp_pop_rte(k,[J I]);
case 4 % 滑動平移操作
tmp_pop_rte(k,I:J) =tmp_pop_rte(k,[I+1:J I]);
case 5% 更新斷點
tmp_pop_brk(k,:) = randbreaks();
case 6 % 倒置並更新斷點
tmp_pop_rte(k,I:J) =fliplr(tmp_pop_rte(k,I:J));
tmp_pop_brk(k,:) =randbreaks();
case 7 % 互換並更新斷點
tmp_pop_rte(k,[I J]) =tmp_pop_rte(k,[J I]);
tmp_pop_brk(k,:) =randbreaks();
case 8 % 評議並更新斷點
tmp_pop_rte(k,I:J) =tmp_pop_rte(k,[I+1:J I]);
tmp_pop_brk(k,:) =randbreaks();
otherwise
end
end
new_pop_rte(p-7:p,:) = tmp_pop_rte;
new_pop_brk(p-7:p,:) = tmp_pop_brk;
end
pop_rte = new_pop_rte;
pop_brk = new_pop_brk;
end
figure(2)
plot(dist_history,'b','LineWidth',2);
title('歷史最優解');
xlabel('迭代次數')
ylabel('最優路程')
% 隨機產生一套斷點 的集合
function breaks = randbreaks()
if min_tour == 1 % 一個旅行商時,沒有斷點的設置
tmp_brks = randperm(n-1);
breaks =sort(tmp_brks(1:num_brks));
else % 強制斷點至少找到最短的履行長度
num_adjust = find(rand <cum_prob,1)-1;
spaces =ceil(num_brks*rand(1,num_adjust));
adjust = zeros(1,num_brks);
for kk = 1:num_brks
adjust(kk) = sum(spaces == kk);
end
breaks = min_tour*(1:num_brks) +cumsum(adjust);
end
end
disp('最優路徑為:/n')
disp(opt_rte);
disp('其中斷點為為:/n')
disp(opt_brk);
end
2. 如何用MATLAB的神經網路工具箱實現三層BP網路
使用神經網路工具箱可以非常簡便地實現網路建立和訓練,實例代碼如下:
%%BP演算法
functionOut=bpnet(p,t,p_test)
%p,t為樣本需要提前組織好
globalS1
net=newff(minmax(p),[S1,8],{'tansig','purelin'},'trainlm');%trainlm訓練函數最有效
%net=newff(P,T,31,{'tansig','purelin'},'trainlm');%新版用法
net.trainParam.epochs=1000;
net.trainParam.goal=0.00001;
net.trainParam.lr=0.01;
net.trainParam.showWindow=false;%阻止訓練窗口的彈出
net.trainParam.showCommandLine=false;%阻止訓練窗口的彈出
net=train(net,p,t);
Out=sim(net,p_test);
end
上面的代碼不完整,完整的帶訓練樣本數據的程序見附件。