【TSP问题】基于蚁群算法求解TSP问题matlab源码含GUI界面
1 算法介绍1.1 蚁群算法原理 蚁群算法是受到对真实蚂蚁群觅食行为研究的启发而提出。生物学研究表明:一群相互协作的蚂蚁能够找到食物和巢穴之间的最短路径,而单只蚂蚁则不能。生物学家经过大量细致观察研究发现,蚂蚁个体之间的行为是相互作用相互影响的。蚂蚁在运动过程中,能够在它所经过的路径上留下一种称之为信息素的物质,而此物质恰恰是蚂蚁个体之间信息传递交流的载体。蚂蚁在运动时能够感知这种物质,并且习惯于
·
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.
5 代码下载
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献2条内容
所有评论(0)