?? gene.m
字號(hào):
Aineq = [];
Bineq = [];
Aeq = [];
Beq = [];
LB = [];
UB = [];
nonlconFunction = [];
options = gaoptimset;
%%Modify some parameters
options = gaoptimset(options,'PopulationSize' ,...
str2num(get(handles.edit6,'String')));%種群大小
options = gaoptimset(options,'EliteCount' ,...
str2num(get(handles.edit9,'String')));%精英保留個(gè)數(shù)
options = gaoptimset(options,'CrossoverFraction' ,...
str2num(get(handles.edit7,'String')));%交叉率
options = gaoptimset(options,'Generations' ,...
str2num(get(handles.edit10,'String')));%代數(shù)
options = gaoptimset(options,'TimeLimit' ,...
str2num(get(handles.edit11,'String')));%時(shí)間限制
options = gaoptimset(options,'SelectionFcn' ,@selectionroulette);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if get(handles.popupmenu11,'Value')==2
options = gaoptimset(options,'CrossoverFcn' ,@crossoversinglepoint);
elseif get(handles.popupmenu11,'Value')==1
options = gaoptimset(options,'CrossoverFcn' ,@crossovertwopoint);
end
if get(handles.popupmenu10,'Value')==2
options = gaoptimset(options,'PopulationType' ,'bitString');
end
options = gaoptimset(options,'MutationFcn' ,{ @mutationuniform 0.02 }); %變異%%%%%%%%%%%%%%%%%%%%%%%%
options = gaoptimset(options,'StallGenLimit' ,55);% 拖尾代數(shù)
options = gaoptimset(options,'StallTimeLimit' ,25);% 拖尾時(shí)間
options = gaoptimset(options,'TolFun' ,0.0000001);
if get(handles.radiobutton4,'Value')
options = gaoptimset(options,'PlotFcns' ,{ @gaplotbestf });
end
[x,FVAL,REASON,OUTPUT,POPULATION,SCORES] = ga(@(x)f(x,handles),nvars,Aineq,Bineq,Aeq,Beq,LB,UB,nonlconFunction,options);
set(handles.edit5,'String',REASON);
f(x,handles);
set(handles.trainBP,'UserData',get(handles.pushbutton4,'UserData'));
set(handles.trainBP,'Enable','on');
set(handles.computBP,'Enable','on');
function y = f(x,handles)
net = get(handles.pushbutton4,'UserData');
layer = get(handles.popupmenu6,'UserData');
layer = layer(:,1);
num = get(handles.text14,'UserData');
iw = x(1:num*layer(1));
net.IW{1} = reshape(iw,layer(1),num);
last = num*layer(1);
ceng = length(layer);
% net.LW = cell(ceng);
for i = 1:ceng-1
lw = x(last+[1:(layer(i)*layer(i+1))]);
net.LW{i+1,i} = reshape(lw,layer(i+1),layer(i));
last = last+layer(i)*layer(i+1);
end
for i = 1:ceng
net.b{i} = x(last+[1:layer(i)])';
last = last+layer(i);
end
PValue = get(handles.in,'Value');
Pdata = get(handles.in,'UserData');
Y = sim(net,Pdata{PValue});
PVbefore = get(handles.out,'Value');
Pdbefore = get(handles.out,'UserData');
y = sum((Pdbefore{PVbefore}-Y).^2)/length(x);
function y = prodY(x)
len =length(x);
y = 0;
for i = 1:len-1
y = y + x(i)*x(i+1);
end
% --- Executes on button press in trainBP.
function trainBP_Callback(hObject, eventdata, handles)
if get(handles.radiobutton1,'Value')
if get(handles.in,'Value')==1 | get(handles.out,'Value')==1
msgbox('請(qǐng)確保樣本數(shù)據(jù)設(shè)置正確!');
return;
end
PR = eval(get(handles.edit2,'String'));
data = get(handles.popupmenu6,'UserData');
S = data(:,1)';
TFindex = data(:,2);
str = get(handles.popupmenu7,'String');
TF = str(TFindex)';
str = get(handles.popupmenu2,'String');
index = get(handles.popupmenu2,'Value');
BTF = str(index);
str = get(handles.popupmenu3,'String');
index = get(handles.popupmenu3,'Value');
BLF = str(index);
str = get(handles.popupmenu4,'String');
index = get(handles.popupmenu4,'Value');
PF = str(index);
net = newff(PR,S,TF,BTF{1},BLF{1},PF{1});
net.trainParam.min_grad = 0;
inValue = get(handles.in,'Value');
outValue = get(handles.out,'Value');
indata = get(handles.in,'UserData');
outdata = get(handles.out,'UserData');
net.trainParam.epochs = str2num(get(handles.edit14,'String'));
net = train(net,indata{inValue},outdata{outValue});
evalin('base', [get(handles.edit15,'String'),'=evalin(''caller'', ''net'');']);
set(handles.computBP,'Enable','on');
else net = get(hObject,'UserData');
inValue = get(handles.in,'Value');
outValue = get(handles.out,'Value');
indata = get(handles.in,'UserData');
outdata = get(handles.out,'UserData');
net.trainParam.epochs = str2num(get(handles.edit14,'String'));
net = train(net,indata{inValue},outdata{outValue});
evalin('base', [get(handles.edit15,'String'),'=evalin(''caller'', ''net'');']);
set(handles.computBP,'Enable','on');
end
set(hObject,'UserData',net);
set(handles.edit5,'String',['訓(xùn)練完成 ;','網(wǎng)絡(luò)已經(jīng)存為 ',get(handles.edit15,'String'),' ,請(qǐng)?jiān)诠ぷ骺臻g中查看']);
% --- Executes on button press in computBP.
function computBP_Callback(hObject, eventdata, handles)
PValue = get(handles.test_in,'Value');
if PValue == 1
set(handles.edit5,'String',sprintf('請(qǐng)先選擇待仿真數(shù)據(jù) '));
return;
end
Pdata = get(handles.test_in,'UserData');
net = get(handles.trainBP,'UserData');
Y = sim(net,Pdata{PValue});
set(hObject,'UserData',Y);
if ~isempty(get(handles.edit13,'String'))
evalin('base', [get(handles.edit13,'String'),'=evalin(''caller'', ''Y'');']);
set(handles.edit5,'String',['計(jì)算結(jié)果已經(jīng)存為 ',get(handles.edit13,'String'),' ,請(qǐng)?jiān)诠ぷ骺臻g中查看']);
else msgbox('請(qǐng)先輸入存儲(chǔ)結(jié)果的變量名');
end
% --- Executes on selection change in test_in.
function test_in_Callback(hObject, eventdata, handles)
getvar(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function test_in_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
getvar(hObject, eventdata, handles);
function radiobutton1_Callback(hObject, eventdata, handles)
set(hObject,'Value',1);
set(handles.pushbutton1,'Enable','off');
set(handles.trainBP,'Enable','on');
h = get(handles.uipanel8,'Children');
h =[h(1:end-1); get(h(end),'Children')];
set(h,'Enable','off');
set(handles.pushbutton4,'Visible','off');
set(handles.text36,'Visible','off');
set(handles.edit5,'String','創(chuàng)建并訓(xùn)練一個(gè)BP網(wǎng)絡(luò)');
function radiobutton2_Callback(hObject, eventdata, handles)
set(hObject,'Value',1);
% set(handles.pushbutton1,'Enable','on');
set(handles.trainBP,'Enable','off');
h = get(handles.uipanel8,'Children');
h =[h(1:end-1); get(h(end),'Children')];
set(h,'Enable','on');
set(handles.pushbutton4,'Visible','on');
set(handles.text36,'Visible','on');
set(handles.edit5,'String','創(chuàng)建一個(gè)BP網(wǎng)絡(luò),用遺傳算法優(yōu)化權(quán)值和閾值,再進(jìn)行訓(xùn)練');
function getvar(hObject, eventdata, handles)
v = evalin('base', 'who');
len = length(v);
str = {'來(lái)自工作空間'};
num = {0};
for i = 1:len
m = evalin('base', v{i});
if issparse(m) | islogical(m) | isnumeric(m)
str = [str;v{i}];
num = [num;m];
end
end
set(hObject,'String',str,'UserData',num);
function edit13_Callback(hObject, eventdata, handles)
function edit13_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit14_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function edit14_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit15_Callback(hObject, eventdata, handles)
function edit15_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
if get(handles.radiobutton2,'Value')
if get(handles.in,'Value')==1 | get(handles.out,'Value')==1
msgbox({' 請(qǐng)確保BP網(wǎng)絡(luò)的輸入和';' 輸出數(shù)據(jù)設(shè)置正確!'});
return;
end
PR = eval(get(handles.edit2,'String'));
data = get(handles.popupmenu6,'UserData');
S = data(:,1)';
TFindex = data(:,2);
str = get(handles.popupmenu7,'String');
TF = str(TFindex)';
str = get(handles.popupmenu2,'String');
index = get(handles.popupmenu2,'Value');
BTF = str(index);
str = get(handles.popupmenu3,'String');
index = get(handles.popupmenu3,'Value');
BLF = str(index);
str = get(handles.popupmenu4,'String');
index = get(handles.popupmenu4,'Value');
PF = str(index);
net = newff(PR,S,TF,BTF{1},BLF{1},PF{1});
net.trainParam.min_grad = 1.0000e-016;
end
set(hObject,'UserData',net);
set(handles.edit5,'String','已經(jīng)根據(jù)所選參數(shù)創(chuàng)建了一個(gè)BP網(wǎng)絡(luò).等待優(yōu)化權(quán)值.');
set(handles.pushbutton1,'Enable','on');
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -