基于Dijkstra算法的赣州市交通路径导航系统GUI设计matlab程序
时间: 2023-12-18 20:04:47 浏览: 164
好的,下面给出基于Dijkstra算法的赣州市交通路径导航系统GUI设计的Matlab程序。
程序实现步骤:
1. 构建图的数据结构:使用Matlab中的graph数据类型来表示图的结构,其中节点和边都可以用结构体来表示。
2. 构建邻接矩阵:邻接矩阵是一个N x N的矩阵,其中N是节点的个数,矩阵中的元素表示节点之间的连通关系。如果节点i和节点j之间有一条边,则邻接矩阵中第i行第j列的元素为1,否则为0。
3. 实现Dijkstra算法:使用Matlab中的堆栈数据类型来实现Dijkstra算法。
4. GUI界面设计:使用Matlab中的GUIDE工具进行界面设计,添加输入框、按钮等控件。
5. 输出最短路径:最后,输出任意两点之间的最短路径。
具体实现代码如下:
```
function varargout = navigation_system(varargin)
% NAVIGATION_SYSTEM MATLAB code for navigation_system.fig
% NAVIGATION_SYSTEM, by itself, creates a new NAVIGATION_SYSTEM or raises the existing
% singleton*.
%
% H = NAVIGATION_SYSTEM returns the handle to a new NAVIGATION_SYSTEM or the handle to
% the existing singleton*.
%
% NAVIGATION_SYSTEM('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in NAVIGATION_SYSTEM.M with the given input arguments.
%
% NAVIGATION_SYSTEM('Property','Value',...) creates a new NAVIGATION_SYSTEM or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before navigation_system_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to navigation_system_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help navigation_system
% Last Modified by GUIDE v2.5 30-Jan-2022 17:03:08
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @navigation_system_OpeningFcn, ...
'gui_OutputFcn', @navigation_system_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before navigation_system is made visible.
function navigation_system_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to navigation_system (see VARARGIN)
% Choose default command line output for navigation_system
handles.output = hObject;
% 构建邻接矩阵
N = 10; % 节点个数
adjMatrix = zeros(N,N); % 初始化邻接矩阵
% 假设有8条道路,每条道路连接两个节点
adjMatrix(1,2) = 1; adjMatrix(2,1) = 1;
adjMatrix(1,3) = 1; adjMatrix(3,1) = 1;
adjMatrix(2,4) = 1; adjMatrix(4,2) = 1;
adjMatrix(2,5) = 1; adjMatrix(5,2) = 1;
adjMatrix(3,6) = 1; adjMatrix(6,3) = 1;
adjMatrix(4,7) = 1; adjMatrix(7,4) = 1;
adjMatrix(5,7) = 1; adjMatrix(7,5) = 1;
adjMatrix(6,8) = 1; adjMatrix(8,6) = 1;
adjMatrix(7,9) = 1; adjMatrix(9,7) = 1;
adjMatrix(8,10) = 1; adjMatrix(10,8) = 1;
adjMatrix(9,10) = 1; adjMatrix(10,9) = 1;
% 构建图的数据结构
handles.g = graph(adjMatrix);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes navigation_system wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = navigation_system_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in find_path_button.
function find_path_button_Callback(hObject, eventdata, handles)
% hObject handle to find_path_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 获取起点和终点
source = str2double(get(handles.source_edit, 'String'));
target = str2double(get(handles.target_edit, 'String'));
% 初始化距离数组
dist = inf(1,numnodes(handles.g));
dist(source) = 0;
% 定义堆栈
stack = [source];
% Dijkstra算法
while ~isempty(stack)
% 取出堆栈中距离源节点最近的节点
[minDist, index] = min(dist(stack));
currentNode = stack(index);
stack(index) = []; % 将节点从堆栈中移除
% 遍历与当前节点相邻的所有节点
neighbors = neighbors(handles.g,currentNode);
for i = 1:length(neighbors)
neighbor = neighbors(i);
% 计算从源节点到相邻节点的距离
newDist = dist(currentNode) + handles.g.Edges.Weight(findedge(handles.g,currentNode,neighbor));
% 更新距离数组
if newDist < dist(neighbor)
dist(neighbor) = newDist;
stack = [stack neighbor];
end
end
end
% 输出任意两点之间的最短路径
path = shortestpath(handles.g,source,target);
set(handles.result_text, 'String', ['从节点' num2str(source) '到节点' num2str(target) '的最短路径为:']);
for i = 1:length(path)
if i == length(path)
set(handles.result_text, 'String', [get(handles.result_text, 'String') num2str(path(i))]);
else
set(handles.result_text, 'String', [get(handles.result_text, 'String') ' -> ' num2str(path(i))]);
end
end
function source_edit_Callback(hObject, eventdata, handles)
% hObject handle to source_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function source_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to source_edit (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 target_edit_Callback(hObject, eventdata, handles)
% hObject handle to target_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function target_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to target_edit (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 clear_button.
function clear_button_Callback(hObject, eventdata, handles)
% hObject handle to clear_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 清空输入框和结果框
set(handles.source_edit, 'String', '');
set(handles.target_edit, 'String', '');
set(handles.result_text, 'String', '结果:');
% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 清空图数据结构
handles.g = [];
guidata(hObject, handles);
```
注意:这里的权重为道路的长度,可以根据实际情况进行修改。另外,为了方便起见,这里的节点编号从1开始,实际使用中可能需要进行调整。
阅读全文