【TWVRP】基于matlab蚁群算法求解带时间窗的多中心车辆路径规划问题【含Matlab源码 113期】

✅博主简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,Matlab项目合作可私信。
🍎个人主页:海神之光
🏆代码获取方式:
海神之光Matlab王者学习之路—代码获取方式
⛳️座右铭:行百里者,半于九十。

更多Matlab仿真内容点击👇
Matlab图像处理(进阶版)
路径规划(Matlab)
神经网络预测与分类(Matlab)
优化求解(Matlab)
语音处理(Matlab)
信号处理(Matlab)
车间调度(Matlab)

⛄一、VRP简介

1 VRP基本原理
车辆路径规划问题(Vehicle Routing Problem,VRP)是运筹学里重要的研究问题之一。VRP关注有一个供货商与K个销售点的路径规划的情况,可以简述为:对一系列发货点和收货点,组织调用一定的车辆,安排适当的行车路线,使车辆有序地通过它们,在满足指定的约束条件下(例如:货物的需求量与发货量,交发货时间,车辆容量限制,行驶里程限制,行驶时间限制等),力争实现一定的目标(如车辆空驶总里程最短,运输总费用最低,车辆按一定时间到达,使用的车辆数最小等)。
VRP的图例如下所示:
在这里插入图片描述
2 问题属性与常见问题
车辆路径问题的特性比较复杂,总的来说包含四个方面的属性:
(1)地址特性包括:车场数目、需求类型、作业要求。
(2)车辆特性包括:车辆数量、载重量约束、可运载品种约束、运行路线约束、工作时间约束。
(3)问题的其他特性。
(4)目标函数可能是总成本极小化,或者极小化最大作业成本,或者最大化准时作业。

3 常见问题有以下几类:
(1)旅行商问题
(2)带容量约束的车辆路线问题(CVRP)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
该模型很难拓展到VRP的其他场景,并且不知道具体车辆的执行路径,因此对其模型继续改进。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(3)带时间窗的车辆路线问题
由于VRP问题的持续发展,考虑需求点对于车辆到达的时间有所要求之下,在车辆途程问题之中加入时窗的限制,便成为带时间窗车辆路径问题(VRP with Time Windows, VRPTW)。带时间窗车辆路径问题(VRPTW)是在VRP上加上了客户的被访问的时间窗约束。在VRPTW问题中,除了行驶成本之外, 成本函数还要包括由于早到某个客户而引起的等待时间和客户需要的服务时间。在VRPTW中,车辆除了要满足VRP问题的限制之外,还必须要满足需求点的时窗限制,而需求点的时窗限制可以分为两种,一种是硬时窗(Hard Time Window),硬时窗要求车辆必须要在时窗内到达,早到必须等待,而迟到则拒收;另一种是软时窗(Soft Time Window),不一定要在时窗内到达,但是在时窗之外到达必须要处罚,以处罚替代等待与拒收是软时窗与硬时窗最大的不同。
在这里插入图片描述
在这里插入图片描述
模型2(参考2017 A generalized formulation for vehicle routing problems):
该模型为2维决策变量
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(4)收集和分发问题
(5)多车场车辆路线问题
参考(2005 lim,多车场车辆路径问题的遗传算法_邹彤, 1996 renaud)
在这里插入图片描述
由于车辆是同质的,这里的建模在变量中没有加入车辆的维度。
在这里插入图片描述
在这里插入图片描述
(6)优先约束车辆路线问题
(7)相容性约束车辆路线问题
(8)随机需求车辆路线问题

4 解决方案
(1)数学解析法
(2)人机交互法
(3)先分组再排路线法
(4)先排路线再分组法
(5)节省或插入法
(6)改善或交换法
(7)数学规划近似法
(8)启发式算法

5 VRP与VRPTW对比
在这里插入图片描述

⛄二、蚁群算法简介

1 蚁群算法(ant colony algorithm,ACA)起源和发展历程
Marco Dorigo等人在研究新型算法的过程中,发现蚁群在寻找食物时,通过分泌一种称为信息素的生物激素交流觅食信息从而能快速的找到目标,于是在1991年在其博士论文中首次系统地提出一种基于蚂蚁种群的新型智能优化算法“蚂蚁系统(Ant system,简称AS)”,后来,提出者及许多研究者对该算法作了各种改进,将其应用于更为广泛的领域,如图着色问题、二次分配问题、工件排序问题、车辆路径问题、车间作业调度问题、网络路由问题、大规模集成电路设计等。近些年来,M.Dorigo等人把蚂蚁算法进一步发展成一种通用的优化技术“蚁群优化(Ant Colony Optimization,简称ACO)”,并将所有符合ACO框架的算法称为“蚁群优化算法(ACO algorithm)”。

在这里插入图片描述
具体来说,各个蚂蚁在没有事先告知食物在什么地方的前提下开始寻找食物。当一只找到食物以后,它会向环境释放一种挥发性分泌物pheromone (称为信息素,该物质随着时间的推移会逐渐挥发消失,信息素浓度的大小表征路径的远近)信息素能够让其他蚂蚁感知从而起到一个引导的作用。通常多个路径上均有信息素时,蚂蚁会优先选择信息素浓度高的路径,从而使浓度高的路径信息素浓度更高,形成一个正反馈。有些蚂蚁并没有像其它蚂蚁一样总重复同样的路,他们会另辟蹊径,如果另开辟的道路比原来的其他道路更短,那么,渐渐地,更多的蚂蚁被吸引到这条较短的路上来。最后,经过一段时间运行,可能会出现一条最短的路径被大多数蚂蚁重复着。最终,信息素浓度最高的路径即是最终被蚂蚁选中的最优路径。
与其他算法相比,蚁群算法是一种比较年轻的算法,具有分布式计算、无中心控制、个体之间异步间接通信等特点,并且易于与其他优化算法相结合,经过不少仁人志士的不断探索,到今天已经发展出了各式各样的改进蚁群算法,不过蚁群算法的原理仍是主干。

2 蚁群算法的求解原理
基于上述对蚁群觅食行为的描述,该算法主要对觅食行为进行以下几个方面模拟:
(1)模拟的图场景中包含了两种信息素,一种表示家,一种表示食物的地点,并且这两种信息素都在以一定的速率进行挥发。
(2)每个蚂蚁只能感知它周围的小部分地方的信息。蚂蚁在寻找食物的时候,如果在感知范围内,就可以直接过去,如果不在感知范围内,就要朝着信息素多的地方走,蚂蚁可以有一个小概率不往信息素多的地方走,而另辟蹊径,这个小概率事件很重要,代表了一种找路的创新,对于找到更优的解很重要。
(3)蚂蚁回窝的规则与找食物的规则相同。
(4)蚂蚁在移动时候首先会根据信息素的指引,如果没有信息素的指引,会按照自己的移动方向惯性走下去,但也有一定的机率改变方向,蚂蚁还可以记住已经走过的路,避免重复走一个地方。
(5)蚂蚁在找到食物时留下的信息素最多,然后距离食物越远的地方留下的信息素越少。找到窝的信息素留下的量的规则跟食物相同。蚁群算法有以下几个特点:正反馈算法、并发性算法、较强的鲁棒性、概率型全局搜索、不依赖严格的数学性质、搜索时间长,易出现停止现象。
蚂蚁转移概率公式:
在这里插入图片描述
公式中:是蚂蚁k从城市i转移到j的概率;α,β分别为信息素和启发式因子的相对重要程度;为边(i,j)上的信息素量;为启发式因子;为蚂蚁k下步允许选择的城市。上述公式即为蚂蚁系统中的信息素更新公式,是边(i,j)上的信息素量;ρ是信息素蒸发系数,0<ρ<1;为第k只蚂蚁在本次迭代中留在边(i,j)上的信息素量;Q为一正常系数;为第k只蚂蚁在本次周游中的路径长度。
在蚂蚁系统中,信息素更新公式为:
在这里插入图片描述
3 蚁群算法的求解步骤:
(1)初始化参数在计算之初,需要对相关参数进行初始化,如蚁群规模(蚂蚁数量)m、信息素重要程度因子α、启发函数重要程度因子β、信息素会发银子ρ、信息素释放总量Q、最大迭代次数iter_max、迭代次数初值iter=1。
(2)构建解空间将各个蚂蚁随机地置于不同的出发点,对每个蚂蚁k(k=1,2,3…m),按照(2-1)计算其下一个待访问城市,直到所有蚂蚁访问完所有城市。
(3)更新信息苏计算每个蚂蚁经过路径长度Lk(k=1,2,…,m),记录当前迭代次数中的最优解(最短路径)。同时,根据式(2-2)和(2-3)对各个城市连接路径上信息素浓度进行更新。
(4) 判断是否终止若iter<iter_max,则令iter=iter+1,清空蚂蚁经过路径的记录表,并返回步骤2;否则,终止计算,输出最优解。
(5)判断是否终止若iter<iter_max,则令iter=iter+1,清空蚂蚁经过路径的记录表,并返回步骤2;否则,终止计算,输出最优解。3. 判断是否终止若iter<iter_max,则令iter=iter+1,清空蚂蚁经过路径的记录表,并返回步骤2;否则,终止计算,输出最优解。

在这里插入图片描述

⛄三、部分源代码

function varargout = AntColonyVRPGUI(varargin)
% ANTCOLONYVRPGUI M-file for AntColonyVRPGUI.fig
% ANTCOLONYVRPGUI, by itself, creates a new ANTCOLONYVRPGUI or raises the existing
% singleton*.
%
% H = ANTCOLONYVRPGUI returns the handle to a new ANTCOLONYVRPGUI or the handle to
% the existing singleton*.
%
% ANTCOLONYVRPGUI(‘CALLBACK’,hObject,eventData,handles,…) calls the local
% function named CALLBACK in ANTCOLONYVRPGUI.M with the given input arguments.
%
% ANTCOLONYVRPGUI(‘Property’,‘Value’,…) creates a new ANTCOLONYVRPGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before AntColonyVRPGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to AntColonyVRPGUI_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 AntColonyVRPGUI

% Last Modified by GUIDE v2.5 11-Jun-2015 00:13:48

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(‘gui_Name’, mfilename, …
‘gui_Singleton’, gui_Singleton, …
‘gui_OpeningFcn’, @AntColonyVRPGUI_OpeningFcn, …
‘gui_OutputFcn’, @AntColonyVRPGUI_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 AntColonyVRPGUI is made visible.
function AntColonyVRPGUI_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 AntColonyVRPGUI (see VARARGIN)
% Choose default command line output for AntColonyVRPGUI
handles.output = hObject;

%% change !!!
handles.ismdvrp = true; %!!!
%%
if handles.ismdvrp == true
[handles.initClusters, handles.coordinates] = INIT;
handles.clusters = handles.initClusters;

set(handles.uipanelBases,'Visible','on');
create_init_plot_MDVRP(handles.coordinates, handles.clusters);

else
%initialization
[vehicles,demands,dist_stations,dist_bases] = INIT_VRP;
handles.vehicles = vehicles;
handles.demands = demands;
handles.distances_stations = dist_stations;
handles.distances_bases = dist_bases;

set(handles.uipanelBases,'Visible','off');

end
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes AntColonyVRPGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% — Outputs from this function are returned to the command line.
function varargout = AntColonyVRPGUI_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 selection change in algorithmPopupmenu.
function algorithmPopupmenu_Callback(hObject, eventdata, handles)
% hObject handle to algorithmPopupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,‘String’)) returns algorithmPopupmenu contents as cell array
% contents{get(hObject,‘Value’)} returns selected item from algorithmPopupmenu

%set invisible error messages
set(handles.errorMandatoryEditFields,‘Visible’,‘off’);
%erase text in edit fields
set(handles.evaporationPheromoneEdit,‘String’,‘’);
set(handles.attractStationEdit,‘String’,‘’);
set(handles.increasePheromoneEdit,‘String’,‘’);
set(handles.amountPheromoneEdit,‘String’,‘’);
set(handles.eliteAntsEdit,‘String’,‘’);
%set disabled all edit fields
set(handles.evaporationPheromoneEdit,‘Enable’,‘off’);
set(handles.attractStationEdit,‘Enable’,‘off’);
set(handles.increasePheromoneEdit,‘Enable’,‘off’);
set(handles.amountPheromoneEdit,‘Enable’,‘off’);
set(handles.eliteAntsEdit,‘Enable’,‘off’);
%set disabled run button
set(handles.runButton,‘Enable’,‘off’);
%set to 0 all results
set(handles.lengthWayText,‘String’,‘-’);
set(handles.subroutesNText,‘String’,‘-’);
set(handles.timeSpentText,‘String’,‘-’);
if handles.ismdvrp == true
%set invisible bases’ listbox
set(handles.listboxBases,‘Visible’,‘off’);
set(handles.totalLengthText,‘Visible’,‘off’);
set(handles.totalLengthText,‘String’,‘-’);
set(handles.totalLengthLabel,‘Visible’,‘off’);

%clear plot
cla;
create_init_plot_MDVRP(handles.coordinates, handles.clusters);

end
%clear plot
%cla;
%clear legend
legend(‘off’);
val = get(hObject,‘Value’);
if val ~= 1 %not placeholder’s text (not be executed)
if val ~= 2 %not Clark-Wright algorithm
%enable edit fields
if val == 5 %AntAlg with elite ants
set(handles.eliteAntsEdit,‘Enable’,‘on’);
end
set(handles.evaporationPheromoneEdit,‘Enable’,‘on’);
set(handles.attractStationEdit,‘Enable’,‘on’);
set(handles.increasePheromoneEdit,‘Enable’,‘on’);
set(handles.amountPheromoneEdit,‘Enable’,‘on’);
end
%enable run button
set(handles.runButton,‘Enable’,‘on’);
end

% — Executes during object creation, after setting all properties.
function algorithmPopupmenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to algorithmPopupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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 evaporationPheromoneEdit_Callback(hObject, eventdata, handles)
% hObject handle to evaporationPheromoneEdit (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 evaporationPheromoneEdit as text
% str2double(get(hObject,‘String’)) returns contents of evaporationPheromoneEdit as a double

% — Executes during object creation, after setting all properties.
function evaporationPheromoneEdit_CreateFcn(hObject, eventdata, handles)
% hObject handle to evaporationPheromoneEdit (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 attractStationEdit_Callback(hObject, eventdata, handles)
% hObject handle to attractStationEdit (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 attractStationEdit as text
% str2double(get(hObject,‘String’)) returns contents of attractStationEdit as a double

% — Executes during object creation, after setting all properties.
function attractStationEdit_CreateFcn(hObject, eventdata, handles)
% hObject handle to attractStationEdit (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 increasePheromoneEdit_Callback(hObject, eventdata, handles)
% hObject handle to increasePheromoneEdit (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 increasePheromoneEdit as text
% str2double(get(hObject,‘String’)) returns contents of increasePheromoneEdit as a double

% — Executes during object creation, after setting all properties.
function increasePheromoneEdit_CreateFcn(hObject, eventdata, handles)
% hObject handle to increasePheromoneEdit (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 amountPheromoneEdit_Callback(hObject, eventdata, handles)
% hObject handle to amountPheromoneEdit (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 amountPheromoneEdit as text
% str2double(get(hObject,‘String’)) returns contents of amountPheromoneEdit as a double

% — Executes during object creation, after setting all properties.
function amountPheromoneEdit_CreateFcn(hObject, eventdata, handles)
% hObject handle to amountPheromoneEdit (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 eliteAntsEdit_Callback(hObject, eventdata, handles)
% hObject handle to eliteAntsEdit (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 eliteAntsEdit as text
% str2double(get(hObject,‘String’)) returns contents of eliteAntsEdit as a double

% — Executes during object creation, after setting all properties.
function eliteAntsEdit_CreateFcn(hObject, eventdata, handles)
% hObject handle to eliteAntsEdit (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 runButton.
function runButton_Callback(hObject, eventdata, handles)
% hObject handle to runButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

%clear legend
legend(‘off’);
%clear plot
%cla;
%set invisible error messages
set(handles.errorMandatoryEditFields,‘Visible’,‘off’);

indexAlg = get(handles.algorithmPopupmenu,‘Value’);
if indexAlg ~= 1 %not placeholder’s text (not be executed)
if indexAlg ~= 2 %not Clark-Wright algorithm
if indexAlg == 5 %AntAlg with elite ants
if isempty(get(handles.eliteAntsEdit,‘String’))
set(handles.errorMandatoryEditFields,‘Visible’,‘on’);
return;
end
end
if ( isempty(get(handles.evaporationPheromoneEdit,‘String’)) || …
isempty(get(handles.attractStationEdit,‘String’)) || …
isempty(get(handles.increasePheromoneEdit,‘String’)) || …
isempty(get(handles.amountPheromoneEdit,‘String’)) )

        set(handles.errorMandatoryEditFields,'Visible','on');
        return;
    end
end
%set disabled run button
set(handles.runButton,'Enable','off');

if handles.ismdvrp == true
    clusters = runAlgorithmMDVRP(indexAlg, handles);
    handles.clusters = clusters;
    
    listbox=findall(gcf,'tag','listboxBases');
    guidata(listbox,handles);
    %select first value in bases' listbox 
    set(handles.listboxBases,'Value',1);
    listboxBases_Callback(listbox, eventdata, handles);
else
    runAlgorithmVRP(indexAlg, handles);    
end

%set enabled run button
set(handles.runButton,'Enable','on');
%set enabled zoom tools
set(handles.toolZoomIn,'Enable','on');
set(handles.toolZoomOut,'Enable','on');
%set enabled pan tool
set(handles.toolPan,'Enable','on');
zoom reset;

end

function [clusters] = runAlgorithmMDVRP(index, handles)
if index ~= 1
if index ~= 2
e = str2double(get(handles.evaporationPheromoneEdit,‘String’));
alpha = str2double(get(handles.attractStationEdit,‘String’));
beta = str2double(get(handles.increasePheromoneEdit,‘String’));
tau0 = str2double(get(handles.amountPheromoneEdit,‘String’));
E = str2double(get(handles.eliteAntsEdit,‘String’));
end

    try
        tStart = tic; %start spent time
        clusters = handles.initClusters;
        handles.clusters = clusters;
        basesName = cell(1,length(clusters)+1);
        basesName{1} = 'Full view';
        for c = 1:length(clusters)
            switch index
                case 2 %Clark-Wright
                    [ Route, RouteLength, vehicles ] = Clark_Wright_VRP( ...
                        clusters(c).demands, clusters(c).diststations, ...
                        clusters(c).distbases, clusters(c).vehicles_capacity );
                case 3 %Ant-minpath
                    [Route, RouteLength, vehicles] = ANT_colony_algorithm_VRP_minpath( ...
                        clusters(c).diststations, clusters(c).distbases, ...
                        clusters(c).demands, [e alpha beta tau0], clusters(c).vehicles_capacity );
                case 4 %Ant-partition
                    [Route, RouteLength, vehicles] = ANT_colony_algorithm_VRP( ...
                        clusters(c).diststations, clusters(c).distbases, ...
                        clusters(c).demands, [e alpha beta tau0], clusters(c).vehicles_capacity );
                case 5 %Ant-elite ants
                    [Route, RouteLength, vehicles] = ANT_colony_algorithm_VRP_with_elite_ants( ...
                        clusters(c).diststations, clusters(c).distbases, ...
                        clusters(c).demands, [e alpha beta tau0 E], clusters(c).vehicles_capacity );
            end
            handles.clusters(c).mdvrp = changeStations(clusters, vehicles, RouteLength, Route, c);
            basesName{c + 1} = sprintf('Base #%d',c); %saving bases that need to be added to listbox
        end
        tElapsed = toc(tStart); %end spent time
    catch ME
        msgbox(strcat('Error occured: ',ME.message),'Error','error');
    end
    clusters = handles.clusters;
    
    %clear plot
    cla;
    create_plot_route_with_vehicles_MDVRP(handles.coordinates, handles.clusters);
    
    set(handles.timeSpentText,'String',sprintf('%fs',tElapsed));

    set(handles.listboxBases, 'String', basesName);
    set(handles.listboxBases, 'Visible','on');
    set(handles.totalLengthText,'String',num2str(getTotalLength(handles.clusters)));
    set(handles.totalLengthText,'Visible','on');
    set(handles.totalLengthLabel,'Visible','on');
end

function [mdvrp] = changeStations(clusters, vehicles, LR, R, clusterN)
mdvrp = struct(‘length_route’,[],‘vehicles’,[],‘simpleVehicles’,[],‘num_of_subroutes’,[]);
len = length(vehicles);
mdvrp.simpleVehicles = vehicles;
for v = 1:len
vehicles(v).route(vehicles(v).route == 1) = 0;
len_route = length(vehicles(v).route);
for vr = 1:len_route
if vehicles(v).route(vr) ~= 0 %羼腓 礤 徉玎
vehicles(v).route(vr) = clusters(clusterN).stations(vehicles(v).route(vr)-1);
end
end
end
mdvrp.num_of_subroutes = number_of_subroutes®;
mdvrp.length_route = LR;
mdvrp.vehicles = vehicles;

function [tlength] = getTotalLength(clusters)
tlength = 0;
clen = length(clusters);
for c = 1:clen
tlength = tlength + clusters©.mdvrp.length_route;
end

function runAlgorithmVRP(index, handles)
if index ~= 1
if index ~= 2
e = str2double(get(handles.evaporationPheromoneEdit,‘String’));
alpha = str2double(get(handles.attractStationEdit,‘String’));
beta = str2double(get(handles.increasePheromoneEdit,‘String’));
tau0 = str2double(get(handles.amountPheromoneEdit,‘String’));
E = str2double(get(handles.eliteAntsEdit,‘String’));
end

    try
        tStart = tic; %start spent time
        switch index
            case 2 %Clark-Wright
                [Route,RouteLength,vehicles] = Clark_Wright_VRP( handles.demands, ...
                    handles.distances_stations, handles.distances_bases, handles.vehicles );
            case 3 %Ant-minpath
                [Route,RouteLength,vehicles] = ANT_colony_algorithm_VRP_minpath( handles.distances_stations,...
                    handles.distances_bases, handles.demands, [e alpha beta tau0], handles.vehicles);
            case 4 %Ant-partition
                [Route,RouteLength,vehicles] = ANT_colony_algorithm_VRP( handles.distances_stations, ...
                    handles.distances_bases, handles.demands, [e alpha beta tau0], handles.vehicles);
            case 5 %Ant-elite ants
                [Route,RouteLength,vehicles] = ANT_colony_algorithm_VRP_with_elite_ants( ...
                    handles.distances_stations, handles.distances_bases, handles.demands, ...
                    [e alpha beta tau0 E], handles.vehicles);
        end
        tElapsed = toc(tStart); %end spent time
        create_plot_route_with_vehicles( add_bases_to_distances(handles.distances_stations, ...
            handles.distances_bases), vehicles, [0 handles.demands] );
    catch ME
        msgbox(strcat('Error occured: ',ME.message),'Error','error');
    end
    
    set(handles.lengthWayText,'String',num2str(RouteLength));
    set(handles.subroutesNText,'String',num2str(number_of_subroutes(Route)));
    set(handles.timeSpentText,'String',sprintf('%fs',tElapsed));
end

% --------------------------------------------------------------------
function dataTooltip_ClickedCallback(hObject, eventdata, handles)
% hObject handle to dataTooltip (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------
function algorithmTooltip_ClickedCallback(hObject, eventdata, handles)
% hObject handle to algorithmTooltip (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% — Executes on selection change in listboxBases.
function listboxBases_Callback(hObject, eventdata, handles)
% hObject handle to listboxBases (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,‘String’)) returns listboxBases contents as cell array
% contents{get(hObject,‘Value’)} returns selected item from listboxBases

%clear legend
legend(‘off’);
%clear plot
cla;

index = get(handles.listboxBases,‘value’) - 1;
if index == 0 %if user want to see the full graph of routes
create_plot_route_with_vehicles_MDVRP(handles.coordinates, handles.clusters);
set(handles.lengthWayText,‘String’,‘-’);
set(handles.subroutesNText,‘String’,‘-’);
else
create_plot_of_base_MDVRP(handles.coordinates, handles.clusters, index);
set(handles.lengthWayText,‘String’,num2str(handles.clusters(index).mdvrp.length_route));
set(handles.subroutesNText,‘String’,num2str(handles.clusters(index).mdvrp.num_of_subroutes));
end

% — Executes during object creation, after setting all properties.
function listboxBases_CreateFcn(hObject, eventdata, handles)
% hObject handle to listboxBases (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: listbox 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 aboutMenu_Callback(hObject, eventdata, handles)
% hObject handle to aboutMenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

About %call gui About.m

% --------------------------------------------------------------------
function fileMenu_Callback(hObject, eventdata, handles)
% hObject handle to fileMenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------
function anotherVRPSubmenu_Callback(hObject, eventdata, handles)
% hObject handle to anotherVRPSubmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------
function newDataSubmenu_Callback(hObject, eventdata, handles)
% hObject handle to newDataSubmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

⛄四、运行结果

在这里插入图片描述

⛄五、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1]雷金羡,孙宇,朱洪杰.改进蚁群算法在带时间窗车辆路径规划问题中的应用[J].计算机集成制造系统. 2022,28(11)

3 备注
简介此部分摘自互联网,仅供参考,若侵权,联系删除

🍅 仿真咨询
1 各类智能优化算法改进及应用

生产调度、经济调度、装配线调度、充电优化、车间调度、发车优化、水库调度、三维装箱、物流选址、货位优化、公交排班优化、充电桩布局优化、车间布局优化、集装箱船配载优化、水泵组合优化、解医疗资源分配优化、设施布局优化、可视域基站和无人机选址优化

2 机器学习和深度学习方面
卷积神经网络(CNN)、LSTM、支持向量机(SVM)、最小二乘支持向量机(LSSVM)、极限学习机(ELM)、核极限学习机(KELM)、BP、RBF、宽度学习、DBN、RF、RBF、DELM、XGBOOST、TCN实现风电预测、光伏预测、电池寿命预测、辐射源识别、交通流预测、负荷预测、股价预测、PM2.5浓度预测、电池健康状态预测、水体光学参数反演、NLOS信号识别、地铁停车精准预测、变压器故障诊断

3 图像处理方面
图像识别、图像分割、图像检测、图像隐藏、图像配准、图像拼接、图像融合、图像增强、图像压缩感知

4 路径规划方面
旅行商问题(TSP)、车辆路径问题(VRP、MVRP、CVRP、VRPTW等)、无人机三维路径规划、无人机协同、无人机编队、机器人路径规划、栅格地图路径规划、多式联运运输问题、车辆协同无人机路径规划、天线线性阵列分布优化、车间布局优化

5 无人机应用方面
无人机路径规划、无人机控制、无人机编队、无人机协同、无人机任务分配

6 无线传感器定位及布局方面
传感器部署优化、通信协议优化、路由优化、目标定位优化、Dv-Hop定位优化、Leach协议优化、WSN覆盖优化、组播优化、RSSI定位优化

7 信号处理方面
信号识别、信号加密、信号去噪、信号增强、雷达信号处理、信号水印嵌入提取、肌电信号、脑电信号、信号配时优化

8 电力系统方面
微电网优化、无功优化、配电网重构、储能配置

9 元胞自动机方面
交通流 人群疏散 病毒扩散 晶体生长

10 雷达方面
卡尔曼滤波跟踪、航迹关联、航迹融合

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
鲸鱼算法是一种模拟鲸鱼集群捕食行为的优化算法,具有全局搜索、性能稳定等优点。而在车辆路径问题中,时间的开放式问题更加复杂,需要考虑时间限制以及车辆的容量等多个约束条件。 基于matlab鲸鱼算法求解时间开放式车辆路径问题,首先需要确定问题的目标函数以及各个约束条件。目标函数可以设定为最小化总路程或最小化总时间等,约束条件包括时间、容量、出发点和到达点等。 然后,可以利用matlab编写求解程序,采用鲸鱼算法进行全局搜索。具体来说,可以将路线规划问题转化为一个优化问题,使用遗传算法或粒子群算法等优化算法进行求解,同时考虑各个约束条件。 在程序中,可以使用矩阵存储车辆的容量、位置、时间等信息,采用突变、选择、交叉等操作进行遗传变异。在每次迭代中,根据当前种群中每个个体的适应度值对其进行排序,以选择较优的个体进行交叉和变异,从而逐渐优化解决方案。同时,可以设置停止迭代的条件,以保证程序的效率。 最后,需要对求解结果进行评估,并进行可视化展示。评估可以使用各种准则进行,如各辆车的路程、总路程、服务时间等指标。可视化可以使用matlab中的绘图工具进行展示,包括路线图、车辆调度图等。 总之,基于matlab鲸鱼算法求解时间开放式车辆路径问题,需要深刻理解问题本质,熟练掌握编程技能,对算法进行适当优化,并进行评估和可视化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海神之光

有机会获得赠送范围1份代码

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值