Java教程

【TSP问题】基于蚁群算法求解TSP问题matlab源码含GUI界面

本文主要是介绍【TSP问题】基于蚁群算法求解TSP问题matlab源码含GUI界面,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1 模型简介见这里

2 部分代码

​
​
function PhoromCoe_Callback(hObject, eventdata, handles)
% hObject    handle to PhoromCoe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of PhoromCoe as text
%        str2double(get(hObject,'String')) returns contents of PhoromCoe as a double
​
​
% --- Executes during object creation, after setting all properties.
function PhoromCoe_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PhoromCoe (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
function PhoromEvap_Callback(hObject, eventdata, handles)
% hObject    handle to PhoromEvap (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of PhoromEvap as text
%        str2double(get(hObject,'String')) returns contents of PhoromEvap as a double
​
​
% --- Executes during object creation, after setting all properties.
function PhoromEvap_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PhoromEvap (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
function Q_Callback(hObject, eventdata, handles)
% hObject    handle to Q (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of Q as text
%        str2double(get(hObject,'String')) returns contents of Q as a double
​
​
% --- Executes during object creation, after setting all properties.
function Q_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Q (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
function edit8_Callback(hObject, eventdata, handles)
% hObject    handle to edit8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double
​
​
% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit8 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
%-------------------------------------------------------------------------%
function endgeneration_Callback(hObject, eventdata, handles)
% hObject    handle to endgeneration (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of endgeneration as text
%        str2double(get(hObject,'String')) returns contents of endgeneration as a double
​
​
% --- Executes during object creation, after setting all properties.
function endgeneration_CreateFcn(hObject, eventdata, handles)
% hObject    handle to endgeneration (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
% --- Executes on button press in RefreshCitiesBtn.
function RefreshCitiesBtn_Callback(hObject, eventdata, handles)
% hObject    handle to RefreshCitiesBtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global AntsCod
global bContinue
global Change
bContinue = 0;
CityNum = str2num(get(handles.CityNumber,'String'));
AntsCod =round(100*rand(CityNum,2));
    cla(handles.CityMap);
    cla(handles.EffectFigure);
    plot(AntsCod(:,1),AntsCod(:,2),'mo','LineWidth',1.5,'MarkerEdgeColor','r','MarkerFaceColor','b','Parent',handles.CityMap);
    set(handles.AntNum,'Enable','on')
    set(handles.MaxIteration,'Enable','on')
    set(handles.PhoromCoe,'Enable','on')
    set(handles.Beta,'Enable','on')
    set(handles.PhoromEvap,'Enable','on')
    set(handles.Q,'Enable','on')
​
% --- Executes on button press in LoopBeginBtn.
function LoopBeginBtn_Callback(hObject, eventdata, handles)
% hObject    handle to LoopBeginBtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global AntsCod
global bContinue
global Change
EditEnable = 'on';
if bContinue ~=  1
    bContinue = 1;
    EditEnable = 'off';
else
    set(handles.LoopBeginBtn,'Enable','off')
    bContinue = 0;
    return;
end
set(handles.AntNum,'Enable',EditEnable)
set(handles.MaxIteration,'Enable',EditEnable)
set(handles.PhoromCoe,'Enable',EditEnable)
set(handles.Beta,'Enable',EditEnable)
set(handles.PhoromEvap,'Enable',EditEnable)
set(handles.Q,'Enable',EditEnable)
set(handles.RefreshCitiesBtn,'Enable',EditEnable)
if bContinue == 1
    Change
    CityNum = str2num(get(handles.CityNumber,'String'));
    AntsNum =  str2num(get(handles.AntNum,'String'));
    AntsColonyNum =str2num(get(handles.MaxIteration,'String'));
    PheromCoe = str2num(get(handles.PhoromCoe,'String'));
    DistCoe =  str2num(get(handles.Beta,'String'));
    PhoremEvapCoe =str2num(get(handles.PhoromEvap,'String'));
    PheromAddCoe =str2num(get(handles.Q,'String'));
    set(handles.LoopBeginBtn,'String','停止');
   if Change==1 %刘
    ACATSP(AntsCod,AntsColonyNum,AntsNum,DistCoe,PheromCoe,PhoremEvapCoe,PheromAddCoe,handles); 
   else %陈
    ACATSP2(AntsCod,AntsColonyNum,AntsNum,DistCoe,PheromCoe,PhoremEvapCoe,PheromAddCoe,handles); 
   end
    EditEnable = 'on';
    bContinue = 0;
    set(handles.AntNum,'Enable',EditEnable)
    set(handles.MaxIteration,'Enable',EditEnable)
    set(handles.PhoromCoe,'Enable',EditEnable)
    set(handles.Beta,'Enable',EditEnable)
    set(handles.PhoromEvap,'Enable',EditEnable)
    set(handles.Q,'Enable',EditEnable)
    set(handles.RefreshCitiesBtn,'Enable',EditEnable)
    set(handles.LoopBeginBtn,'String','开始')
    set(handles.LoopBeginBtn,'Enable','on')
end
​
​
function CurrentAntsColony_Callback(hObject, eventdata, handles)
% hObject    handle to CurrentAntsColony (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of CurrentAntsColony as text
%        str2double(get(hObject,'String')) returns contents of CurrentAntsColony as a double
​
​
% --- Executes during object creation, after setting all properties.
function CurrentAntsColony_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CurrentAntsColony (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
function CurrentMinDistance_Callback(hObject, eventdata, handles)
% hObject    handle to CurrentMinDistance (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of CurrentMinDistance as text
%        str2double(get(hObject,'String')) returns contents of CurrentMinDistance as a double
​
​
% --- Executes during object creation, after setting all properties.
function CurrentMinDistance_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CurrentMinDistance (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
function TotalTime_Callback(hObject, eventdata, handles)
% hObject    handle to TotalTime (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of TotalTime as text
%        str2double(get(hObject,'String')) returns contents of TotalTime as a double
​
​
% --- Executes during object creation, after setting all properties.
function TotalTime_CreateFcn(hObject, eventdata, handles)
% hObject    handle to TotalTime (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
function edit12_Callback(hObject, eventdata, handles)
% hObject    handle to edit12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of edit12 as text
%        str2double(get(hObject,'String')) returns contents of edit12 as a double
​
​
% --- Executes during object creation, after setting all properties.
function edit12_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit12 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
function AverageTime_Callback(hObject, eventdata, handles)
% hObject    handle to AverageTime (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of AverageTime as text
%        str2double(get(hObject,'String')) returns contents of AverageTime as a double
​
​
% --- Executes during object creation, after setting all properties.
function AverageTime_CreateFcn(hObject, eventdata, handles)
% hObject    handle to AverageTime (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
%%
​

3 仿真结果

4 参考文献

[1]吴华锋, 陈信强, 毛奇凰,等. 基于自然选择策略的蚁群算法求解TSP问题[J]. 通信学报, 2013, 000(004):165-170.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。

这篇关于【TSP问题】基于蚁群算法求解TSP问题matlab源码含GUI界面的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!