【优化选址】基于matlab蚁群算法求解电动汽车充电站与换电站选址优化问题【含Matlab源码 1182期】
一、蚁群算法简介
1 概要
模拟蚂蚁觅食行为(最短路径原理)设计的算法。讲蚂蚁群觅食的特点抽象出来转化成数学描述。
• 蚁群算法(Ant Colony Algorithm, ACA)由Marco Dorigo于1992年在他的博士论文中首次提出。
• 蚂蚁在寻找食物源时,会在其经过的路径上释放一种信息素,并能够感知其它蚂蚁释放的信息素。信息素浓度的大小表征路径的远近,信息素浓度越高,表示对应的路径距离越短。
• 通常,蚂蚁会以较大的概率优先选择信息素浓度较高的路径,并释放 一定量的信息素,以增强该条路径上的信息素浓度,这样,会形成一个正反馈。最终,蚂蚁能够找到一条从巢穴到食物源的最佳路径,即距离最短。
• 生物学家同时发现,路径上的信息素浓度会随着时间的推进而逐渐衰减。
• 将蚁群算法应用于解决优化问题,其基本思路为:用蚂蚁的行走路径表示待优化问题的可行解,整个蚂蚁群体的所有路径构成待优化问题的解空间。路径较短的蚂蚁释放的信息素量较多,随着时间的推进,较短 的路径上累积的信息素浓度逐渐增高,选择该路径的蚂蚁个数也愈来愈多。最终,整个蚂蚁会在正反馈的作用下集中到最佳的路径上,此时对应的便是待优化问题的最优解。
类比GA(遗传算法)的交叉、选择、变异,PSO(粒子群算法)的个体、群体极值优化,蚁群算法也有自己的优化策略:正反馈的信息机制、信息素浓度的更新、蚂蚁对能够访问的路径的筛选。
2 基本思想
蚁群算法的基本原理来源于自然界蚂蚁觅食的最短路径原理。根据昆虫学家的观察,发现自然界的蚂蚁虽然视觉不发达,但它可以在没有任何提示的情况下找到从食物源到巢穴的最短路径,并且能在环境发生变化(如原有路径上有了障碍物)后,自适应地搜索新的最佳路径。蚂蚁是如何做到这一点的呢?
原来,蚂蚁在寻找食物源时,能在其走过的路径上放一种蚂蚁特有的分泌物-信息 素,也可称为信息素,使得一定范围内的其他蚂蚁能够察觉到并由此影响它们以后的行为。当一些路径上通过的蚂蚁越来越多时,其留下的信息素也越来越多,以致信息素强度增大(当然,随时间的推移会逐渐减弱,所以蚂蚁选择该路径的概率也越高,从而更增加了该路径的信息素强度,这种选择过程被称之为蚂蚁的自催化行为。 由于其原理是一种正反馈机制,因此,也可将蚂蚁王国理解为所谓的增强型学习系统。 这里我们用一个图来说明蚂蚁觅食的最短路径选择原理,如图所示。在中,假设A点是蚂蚁的巢穴,B点是食物,A、B两点间有一个障碍物,那么此时从A点到B点 的蚂蚁就必须决定往左走还是往右走,而从B点到A点的蚂蚁也必须决定选择走哪条路径。 这种决定会受到各条路径上以往蚂蚁留下的信息素浓度(即残留信息素浓度)的影响。 如果往 右走的路径上的信息素浓度比较大,那么右边的路径被蚂蚁选中的可能性也就大一些。但对于第一批探路的蚂蚁,因为没有信息素的影响或影响比较小,所以它们选择向左或者向右的可能性是一样的,正如图所示的那样。随着觅食过程的进行,各条道路上信息 素的强度开始出现变化,有的线路强,有的 线路弱。现以从A点到B点的蚂蚁为例说明(从B点到 A点的蚂蚁,过程也基本是一样的)随后过程的变化。由于路径 ADB比路径ACB要短,因此选择ADB路径的第一只蚂蚁要比选择ACB的第一只蚂蚁早到达B点。此时,从B点向 A点看,路径BDA A上的信息素浓度要比路径BCA上的信息素浓度大。因此从下一时刻开始,从B点到A 点的蚂蚁,它们选择 BDA路径的可能性要比选择BCA路径的可能性就大些,从而使ABDA路线上的信息素进一步增强,于是依D赖信息素强度选择路径的蚂蚁逐渐偏向于选择路径 ADB,如图所示。随着时间的推移,几乎所有的蚂蚁都会选择路径 ADB(或 BDA)搬运食物,而我们同时也会发现:ADB路径也正是事实上的最短路径。这种蚁群寻径的原理可简单理解为:对于单个的蚂蚁,它并没有要寻找最短路径的主观上的故意;但对于整个蚁群系统,它们又确实达到了寻找到最短路径的观上的效果。 在自然界中,蚁群的这种寻找路径的过程表现为一种正反馈的过程,“蚁群算法”就是模拟 生物学上蚂蚁群觅食寻找最短路径的原理衍生出来的。例如,我们把只具备了简单功能的工 作单元视为“蚂蚁”,那么上述寻找路径的过程可以用于解释蚁群算法中人工蚁群的寻优过程。 这也就是蚁群算法的基本思想。
3 算法设计的流程
这里以TSP问题为例,算法设计的流程如下:
步骤1:对相关参数进行初始化,包括蚁群规模、信息素因子、启发函数因子、信息素挥发因子、信息素常数、最大迭代次数等,以及将数据读入程序,并进行预处理:比如将城市的坐标信息转换为城市间的距离矩阵。
步骤2:随机将蚂蚁放于不同出发点,对每个蚂蚁计算其下个访问城市,直到有蚂蚁访问完所有城市。
步骤3:计算各蚂蚁经过的路径长度Lk,记录当前迭代次数最优解,同时对路径上的信息素浓度进行更新。
步骤4:判断是否达到最大迭代次数,若否,返回步骤2;是,结束程序。
步骤5:输出结果,并根据需要输出寻优过程中的相关指标,如运行时间、收敛迭代次数等。
4 数学模型
二、部分源代码
function varargout = antinface(varargin)
% ANTINFACE M-file for antinface.fig
% ANTINFACE, by itself, creates a new ANTINFACE or raises the existing
% singleton*.
%
% H = ANTINFACE returns the handle to a new ANTINFACE or the handle to
% the existing singleton*.
%
% ANTINFACE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ANTINFACE.M with the given input arguments.
%
% ANTINFACE('Property','Value',...) creates a new ANTINFACE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before antinface_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to antinface_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 antinface
% Last Modified by GUIDE v2.5 02-Jun-2021 10:29:35
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @antinface_OpeningFcn, ...
'gui_OutputFcn', @antinface_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 antinface is made visible.
function antinface_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 state (see GUIDATA)
% varargin command line arguments to antinface (see VARARGIN)
% Choose default command line output for antinface
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
h=waitbar(0,'请等待......');
for i=1:100
waitbar(i/100);
end
delete(h);
% UIWAIT makes antinface wait for user response (see UIRESUME)
% uiwait(handles.handles);
% --- Outputs from this function are returned to the command line.
function varargout = antinface_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 state (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function edit_initao_Callback(hObject, eventdata, handles)
% hObject handle to edit_initao (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user state (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_initao as text
% str2double(get(hObject,'String')) returns contents of edit_initao as a double
% --- Executes during object creation, after setting all properties.
function edit_initao_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_initao (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 edit_q_Callback(hObject, eventdata, handles)
% hObject handle to edit_q (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user state (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_q as text
% str2double(get(hObject,'String')) returns contents of edit_q as a double
% --- Executes during object creation, after setting all properties.
function edit_q_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_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 edit_alpha_Callback(hObject, eventdata, handles)
% hObject handle to edit_alpha (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user state (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_alpha as text
% str2double(get(hObject,'String')) returns contents of edit_alpha as a double
% --- Executes during object creation, after setting all properties.
function edit_alpha_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_alpha (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 edit_rou_Callback(hObject, eventdata, handles)
% hObject handle to edit_rou (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user state (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_rou as text
% str2double(get(hObject,'String')) returns contents of edit_rou as a double
% --- Executes during object creation, after setting all properties.
function edit_rou_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_rou (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 edit_beta_Callback(hObject, eventdata, handles)
% hObject handle to edit_beta (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user state (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_beta as text
% str2double(get(hObject,'String')) returns contents of edit_beta as a double
% --- Executes during object creation, after setting all properties.
function edit_beta_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_beta (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 edit_ncmax_Callback(hObject, eventdata, handles)
% hObject handle to edit_ncmax (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user state (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_ncmax as text
% str2double(get(hObject,'String')) returns contents of edit_ncmax as a double
% --- Executes during object creation, after setting all properties.
function edit_ncmax_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_ncmax (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 edit_antsum_Callback(hObject, eventdata, handles)
% hObject handle to edit_antsum (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user state (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_antsum as text
% str2double(get(hObject,'String')) returns contents of edit_antsum as a double
% --- Executes during object creation, after setting all properties.
function edit_antsum_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_antsum (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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
三、运行结果
四、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016.
[2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017.
文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。
原文链接:qq912100926.blog.csdn.net/article/details/119376599
- 点赞
- 收藏
- 关注作者
评论(0)