禁忌搜索算法实现经典VRP问题
Posted fpga&matlab
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了禁忌搜索算法实现经典VRP问题相关的知识,希望对你有一定的参考价值。
1.问题描述:
人工智能算法解决VRP问题。。
用禁忌搜寻算法实现VRP问题 或者用启发式算法实现VRP问题 只要不是GA算法 约束任意 只要是VRP问题。
这里,一开始客服和你说的是禁忌搜寻算法,但是禁忌搜寻算法貌似解决VRP问题不太行,一般只用于解决简单的TSP问题,所以这里我研究了一下,考虑到你不要GA遗传算法,其他算法还有蚁群算法和PSO算法,这里我们使用的是蚁群算法。
2.部分程序:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
PNUM = str2num(get(handles.edit1,'String'));
Ant_Num = str2num(get(handles.edit2,'String'));
Itertions = str2num(get(handles.edit3,'String'));
Alpha = str2num(get(handles.edit4,'String'));
Beta = str2num(get(handles.edit5,'String'));
Rho = str2num(get(handles.edit6,'String'));
Car_Load = str2num(get(handles.edit7,'String'));
Q = str2num(get(handles.edit8,'String'));
gama = str2num(get(handles.edit9,'String'));
see = str2num(get(handles.edit10,'String'));
SEEK1 = str2num(get(handles.edit11,'String'));
SEEK2 = str2num(get(handles.edit12,'String'));
rng('default');
rng(SEEK1);
Position = 100*rand(PNUM,2);
rng(SEEK2);
Times = 4*rand(PNUM,1);
D = zeros(PNUM,PNUM);
for i=1:PNUM
for j=1:PNUM
if i~=j
D(i,j) = sqrt((Position(i,1)-Position(j,1))^2+(Position(i,2)-Position(j,2))^2);
else
D(i,j) = 1e-30;
end
D(j,i) = D(i,j);
end
end
U = zeros(PNUM,PNUM);
for i=1:PNUM
for j=1:PNUM
if i~=j
U(i,j) = D(i,1)+D(j,1)-D(i,j);
else
U(i,j) = 1e-30;
end
U(j,i) = U(i,j);
end
end
Carrier = 0;
ED = 1./D;
Infor_cube = ones(PNUM,PNUM);
%存储并记录路径的生成
Infor_Tabu = zeros(Ant_Num,PNUM+20);
COUNT = 1;
%各代最佳路线
Best_Road = [Itertions,PNUM+20];
%各代最佳路线的长度
Best_Length= inf.*ones(Itertions,1);
Avg_Length = zeros(Itertions,1);
%开始循环
while COUNT<=Itertions
COUNT
Infor_Tabu(:,1) = randint(Ant_Num,1,[1,1]);
for i=1:Ant_Num
VISITDAT = Infor_Tabu(i,:);
VISITDAT = VISITDAT(VISITDAT>0);
VISITDAT_ = setdiff(1:PNUM,VISITDAT);
VISIT_Tmp = length(VISITDAT_);
j = 1;
while j <= PNUM
if ~isempty(VISITDAT_)
for k=1:length(VISITDAT_)
x(k) = (Infor_cube(VISITDAT(end),VISITDAT_(k))^Alpha)*(ED(VISITDAT(end),VISITDAT_(k))^Beta)*(U(VISITDAT(end),VISITDAT_(k))^gama);
end
Weights=rand(1,1);
if Weights < see
Select = find(max(x));
else
x = x/(sum(x));
%按累积概论进行判断
Xadd = cumsum(x);
Select = find(Xadd>=rand);
end
if isempty(Select)==1
Select = 1;
Carrier = Carrier + Times(Select);
else
Carrier = Carrier + Times(VISITDAT_(Select(1)));
end
if Carrier>Car_Load
Select = 1;
j = j-1;
Carrier = 0;
Infor_Tabu(i,length(VISITDAT)+1) = Select(1);
else
Infor_Tabu(i,length(VISITDAT)+1) = VISITDAT_(Select(1));
end
end
VISITDAT = Infor_Tabu(i,:);
VISITDAT = VISITDAT(VISITDAT>0);
VISITDAT_ = setdiff(1:PNUM,VISITDAT);
x = [];
if VISITDAT(end) ~= 1
Infor_Tabu(i,1:(length(VISITDAT)+1))=[VISITDAT,1];
end
j = j + 1;
end
Carrier=0;
end
%记录本代各种参数
L = zeros(Ant_Num,1);
for i=1:Ant_Num
Infor_Tabu_tmps = Infor_Tabu(i,:);
R = Infor_Tabu_tmps(Infor_Tabu_tmps>0);
for j=1:(length(R)-1)
L(i) = L(i) + D(R(j),R(j+1));
end
end
Best_Length(COUNT) = min(L);
pos = find(L==Best_Length(COUNT));
Best_Road(COUNT,1:length(Infor_Tabu(pos(1),:)))=Infor_Tabu(pos(1),:);
%最优解进行更新
select = find(Best_Road(COUNT,:)==1);
FF = [];
FF2 = 0;
for I1 = 1:(length(select)-1)
Best_Road2 = Best_Road(COUNT,select(I1):select(I1+1));
Best_Road_len = length(Best_Road2);
T = zeros((length(select)-1),1);
for I4=1:(Best_Road_len-1)
T(I1) = T(I1) + D(Best_Road2(I4),Best_Road2(I4+1));
end
for I2 = 2:(Best_Road_len-1)
for I3=(I2+1):(Best_Road_len-1)
Best_Road3 = Best_Road2;
Best_Road31 = Best_Road3(I2);
Best_Road32 = Best_Road3(I3);
Best_Road3(I2) = Best_Road32;
Best_Road3(I3) = Best_Road31;
TT = zeros(1);
for I4=1:(Best_Road_len-1)
TT = TT + D(Best_Road3(I4),Best_Road3(I4+1));
end
if TT<T(I1)
T(I1) = TT;
Best_Road2 = Best_Road3;
end
end
end
if I1 >= 2
Best_Road2=Best_Road2(2:Best_Road_len);
end
FF = [FF,Best_Road2];
FF2 = FF2+T(I1);
end
Best_Length(COUNT) = FF2;
Best_Road(COUNT,1:length(FF)) = FF;
FF = [];
FF2 = 0;
Avg_Length(COUNT) = mean(L);
COUNT = COUNT+1;
%更新信息素
Delta_Infor = zeros(PNUM,PNUM);
for i=1:Ant_Num
Infor_Tabu_tmps = Infor_Tabu(i,:);
R = Infor_Tabu_tmps(Infor_Tabu_tmps>0);
for j=1:(length(R)-1)
Delta_Infor(R(j),R(j+1))=Delta_Infor(R(j),R(j+1))+Q/L(i);
end
end
Infor_cube = (1-Rho).*Infor_cube+Delta_Infor;
%禁忌表清零
Infor_Tabu = zeros(Ant_Num,PNUM);
Carrier = 0;
end
Pos = find(Best_Length==min(Best_Length));
best_route = Best_Road(Pos(1),:);
best_length = Best_Length(Pos(1));
best_route = best_route(best_route>0);
best_route
best_length
axes(handles.axes1);
plot([Position(best_route,1)],[Position(best_route,2)],'ro');
axis square;
axes(handles.axes2);
plot([Position(best_route,1)],[Position(best_route,2)],'rs');
hold on
plot([Position(best_route,1)],[Position(best_route,2)],'b-');
axis square;
axes(handles.axes3);
plot(Best_Length,'b-o');
hold on
plot(Avg_Length,'r-o');
grid on;
legend('最短距离','平均距离');
3.仿真结论:
A06-12
以上是关于禁忌搜索算法实现经典VRP问题的主要内容,如果未能解决你的问题,请参考以下文章