📄 sga_main.asv
字号:
function maxgen_Callback(hObject, eventdata, handles)
% hObject handle to maxgen (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% maxGen=str2Num(get(handles.maxgen,'string'));
% Hints: get(hObject,'String') returns contents of maxgen as text
% str2double(get(hObject,'String')) returns contents of maxgen as a double
% --- Executes during object creation, after setting all properties.
function maxgen_CreateFcn(hObject, eventdata, handles)
% hObject handle to maxgen (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% --- Executes on button press in calculate.
function calculate_Callback(hObject, eventdata, handles)
% hObject handle to calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
tic;
fitFunc=get(handles.func,'string');
popSize=str2num(get(handles.popsize,'string'));
bounds=str2num(get(handles.lchrom,'string'));
pCross=str2num(get(handles.pcross,'string'));
pMutation=str2num(get(handles.pmutation,'string'));
maxGen=str2num(get(handles.maxgen,'string'));
maxRuns=str2num(get(handles.maxruns,'string'));
evalFN=fitFunc; %'gademo1eval'
%selectFN=['select_Roulette'];
%mutFNs=['binaryMutation'];
% xOverFNs=['simpleCross'];
opts=[1e-6 pCross pMutation maxGen];
%startPop=initpop(popSize,bounds,evalFN);
PRECI=20;GGAP=0.9;
FieldD=[20;bounds(1);bounds(2);1;0;1;1];
% FieldD = [rep([PRECI],[1, NVAR]); rep(bounds,[1, NVAR]);...
% rep([1; 0; 1 ;1], [1, NVAR])];
[x,y,trace] = ga_other(popSize, bounds, evalFN, maxGen,...
PRECI, GGAP,FieldD);
bestHandle=findobj('Tag','bch');
set(bestHandle,'string', x);
maxfitHandle=findobj('Tag','max');
set(maxfitHandle,'string', y);
minfitHandle=findobj('Tag','min');
set(minfitHandle,'string',min(trace(3,:)));
meanfitHandle=findobj('Tag','mean');
set(meanfitHandle,'string',mean(trace(2,:)));
set(findobj('Tag','time'),'string',toc);
% --- Executes on button press in reset.
function reset_Callback(hObject, eventdata, handles)
% hObject handle to reset (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cla reset
set(findobj('Tag','bch'),'string', 0);
set(findobj('Tag','max'),'string', 0);
set(findobj('Tag','min'),'string', 0);
set(findobj('Tag','mean'),'string', 0);
set(findobj('Tag','time'),'string', 0);
% --- Executes on button press in FNcLine.
function FNcLine_Callback(hObject, eventdata, handles)
% hObject handle to FNcLine (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fitFunc=get(handles.func,'string');
bounds=str2num(get(handles.lchrom,'string'));
figure(1);
hold on, grid;
fplot(fitFunc,bounds);
xlabel('Variable'); ylabel('Fittness');
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
tic;
fitFunc=get(handles.func,'string');
popSize=str2num(get(handles.popsize,'string'));
bounds=str2num(get(handles.lchrom,'string'));
pCross=str2num(get(handles.pcross,'string'));
pMutation=str2num(get(handles.pmutation,'string'));
maxGen=str2num(get(handles.maxgen,'string'));
maxRuns=str2num(get(handles.maxruns,'string'));
evalFN=fitFunc; %'gademo1eval'
selectFN=['select_Roulette'];
mutFNs=['binaryMutation'];
xOverFNs=['simpleCross'];
opts=[1e-6 pCross pMutation maxGen];
startPop=initpop(popSize,bounds,evalFN);
[x,bPop,traceInfo]=sga(popSize, bounds, evalFN, startPop, selectFN, xOverFNs, mutFNs, opts);
bestHandle=findobj('Tag','bch');
set(bestHandle,'string', x);
maxfitHandle=findobj('Tag','max');
set(maxfitHandle,'string', max(traceInfo(:,2)));
minfitHandle=findobj('Tag','min');
set(minfitHandle,'string',min(traceInfo(:,4)));
meanfitHandle=findobj('Tag','mean');
set(meanfitHandle,'string',mean(traceInfo(:,3)));
hold on, grid;
plot(traceInfo(:,2));
hold on, grid;
plot(traceInfo(:,3),'r-.');
hold on, grid;
plot(traceInfo(:,4),':');
legend('解的变化','种群均值的变化','种群最小值的变化')
xlabel('Generation'); ylabel('Fittness');
set(findobj('Tag','time'),'string',toc);
% --------------------------------------------------------------------
function Othersolve_Callback(hObject, eventdata, handles)
% hObject handle to Othersolve (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
tic
fitFunc=get(handles.func,'string');
popSize=str2num(get(handles.popsize,'string'));
bounds=str2num(get(handles.lchrom,'string'));
pCross=str2num(get(handles.pcross,'string'));
pMutation=str2num(get(handles.pmutation,'string'));
maxGen=str2num(get(handles.maxgen,'string'));
maxRuns=str2num(get(handles.maxruns,'string'));
evalFN=fitFunc; %'gademo1eval'
selectFN=['select_Roulette'];
mutFNs=['binaryMutation'];
xOverFNs=['simpleCross'];
opts=[1e-6 pCross pMutation maxGen];
startPop=initpop(popSize,bounds,evalFN);
[x,bPop,traceInfo]=sga(popSize, bounds, evalFN, startPop, selectFN, xOverFNs, mutFNs, opts);
bestHandle=findobj('Tag','bch');
set(bestHandle,'string', x);
maxfitHandle=findobj('Tag','max');
set(maxfitHandle,'string', max(traceInfo(:,2)));
minfitHandle=findobj('Tag','min');
set(minfitHandle,'string',min(traceInfo(:,4)));
meanfitHandle=findobj('Tag','mean');
set(meanfitHandle,'string',mean(traceInfo(:,3)));
hold on, grid;
plot(traceInfo(:,2));
hold on, grid;
plot(traceInfo(:,3),'r-.');
hold on, grid;
plot(traceInfo(:,4),':');
legend('解的变化','种群均值的变化','种群最小值的变化')
xlabel('Generation'); ylabel('Fittness');
set(findobj('Tag','time'),'string',toc);
% --------------------------------------------------------------------
function calcul_Callback(hObject, eventdata, handles)
% hObject handle to calcul (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
tic
fitFunc=get(handles.func,'string');
popSize=str2num(get(handles.popsize,'string'));
bounds=str2num(get(handles.lchrom,'string'));
pCross=str2num(get(handles.pcross,'string'));
pMutation=str2num(get(handles.pmutation,'string'));
maxGen=str2num(get(handles.maxgen,'string'));
maxRuns=str2num(get(handles.maxruns,'string'));
evalFN=fitFunc; %'gademo1eval'
%selectFN=['select_Roulette'];
%mutFNs=['binaryMutation'];
% xOverFNs=['simpleCross'];
opts=[1e-6 pCross pMutation maxGen];
%startPop=initpop(popSize,bounds,evalFN);
PRECI=20;GGAP=0.9;
FieldD=[20;bounds(1);bounds(2);1;0;1;1];
% FieldD = [rep([PRECI],[1, NVAR]); rep(bounds,[1, NVAR]);...
% rep([1; 0; 1 ;1], [1, NVAR])];
[x,y,trace] = ga_simple(popSize, bounds, evalFN, maxGen,...
PRECI, GGAP,FieldD);
bestHandle=findobj('Tag','bch');
set(bestHandle,'string', x);
maxfitHandle=findobj('Tag','max');
set(maxfitHandle,'string', y);
minfitHandle=findobj('Tag','min');
set(minfitHandle,'string',min(trace(3,:)));
meanfitHandle=findobj('Tag','mean');
set(meanfitHandle,'string',mean(trace(2,:)));
set(findobj('Tag','time'),'string',toc);
% --------------------------------------------------------------------
function resets_Callback(hObject, eventdata, handles)
% hObject handle to resets (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cla reset
set(findobj('Tag','bch'),'string', 0);
set(findobj('Tag','max'),'string', 0);
set(findobj('Tag','min'),'string', 0);
set(findobj('Tag','mean'),'string', 0);
set(findobj('Tag','time'),'string', 0);
% --------------------------------------------------------------------
function Option_Callback(hObject, eventdata, handles)
% hObject handle to Option (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -