⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gene.m

📁 BP网络的图形用户界面 另附BP和遗传算法的功能,不过不好用
💻 M
📖 第 1 页 / 共 2 页
字号:
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')));%精英保留个数
options = gaoptimset(options,'CrossoverFraction' ,...
    str2num(get(handles.edit7,'String')));%交叉率
options = gaoptimset(options,'Generations' ,...
    str2num(get(handles.edit10,'String')));%代数
options = gaoptimset(options,'TimeLimit' ,...
    str2num(get(handles.edit11,'String')));%时间限制
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);% 拖尾代数
options = gaoptimset(options,'StallTimeLimit' ,25);% 拖尾时间
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('请确保样本数据设置正确!');
        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',['训练完成 ;','网络已经存为 ',get(handles.edit15,'String'),' ,请在工作空间中查看']);
% --- 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('请先选择待仿真数据 '));
    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',['计算结果已经存为 ',get(handles.edit13,'String'),' ,请在工作空间中查看']);
else msgbox('请先输入存储结果的变量名');
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','创建并训练一个BP网络');
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','创建一个BP网络,用遗传算法优化权值和阈值,再进行训练');
function getvar(hObject, eventdata, handles)
v = evalin('base', 'who');
len = length(v);
str = {'来自工作空间'};
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({'     请确保BP网络的输入和';'        输出数据设置正确!'});
        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','已经根据所选参数创建了一个BP网络.等待优化权值.');
set(handles.pushbutton1,'Enable','on');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -