【路径规划】模拟退火之求解VRP问题【Matlab 025期】

一、源代码

clc;
clear;
close all;
 
T0 = 0.4 ; % initial temperature
r = 0.997 ;%0.997 ; % temperature damping rate
Ts = 0.001 ; % stop temperature
iter = 300;
 
model = initModel();
set(gcf,'unit','normalized','position',[0,0.35,1,0.7]);
 
flag = 0;
 
% initialization
 
load ini2.txt
route = ini2;
 
%route = randomSol(model);
while(1)
if(isFeasible(route,model)) 
    break; 
end
mode = randi([1 3]);
route = createNeibor(route,model,mode);
end
 
cost = calculateCost(route,model);
T = T0;
 
cnt = 1;
minCost = cost;
minRoute = route;
    
maxIterate = 2100;
costArray = zeros(maxIterate,1);
 
% SA
while(T > Ts)
    for k = 1:iter
    mode = randi([1 3]);
    newRoute = createNeibor(route,model,mode);
    newCost = calculateCost(newRoute,model);
    delta = newCost(4) - cost(4);
    
    if(delta < 0)
        cost = newCost;
        route = newRoute;
    else
        p=exp(-delta/T);
        if rand() <= p 
             cost = newCost;
             route = newRoute;
             
        end
    end
    end
    
    costArray(cnt) = cost(4);
    if cost(4)<minCost(4)
        minCost = cost;
        minRoute = route;
        flag = 1;
    end
    
    T = T*r; %  annealing
    disp(['Iteration ' num2str(cnt) ': BestCost = ' num2str(minCost) ': CurrentCost = ' num2str(cost) ' T = ' num2str(T)]);
    cnt = cnt+1;
    
   figure(1);
   if(flag == 1)
   plotSolution(minRoute,model);
    flag = 0;
   end
%    figure(2);
   subplot(1,2,2)
   plot(costArray);
 
   pause(0.0001);
end
 
plotSolution(minRoute,model);

二、备注

完整代码或者代写添加QQ912100926
往期回顾>>>>>>
【路径规划】粒子群优化算法之三维无人机路径规划【Matlab 012期】
【路径规划】遗传算法之多物流中心的开放式车辆路径规划【Matlab 013期】
【路径规划】粒子群算法之机器人栅格路径规划【Matlab 014期】
【路径规划】蚁群算法之求解最短路径【Matlab 015期】
【路径规划】免疫算法之物流中心选址【Matlab 016期】
【路径规划】人工蜂群之无人机三维路径规划【Matlab 017期】
【路径规划】遗传算法之基于栅格地图机器人路径规划【Matlab 018期】
【路径规划】蚁群算法之多无人机攻击调度【Matlab 019期】
【路径规划】遗传算法之基于栅格地图的机器人最优路径规划【Matlab 020期】
【路径规划】遗传算法之考虑分配次序的多无人机协同目标分配建模【Matlab 021期】
【路径规划】蚁群算法之多中心vrp问题【Matlab 022期】
【路径规划】蚁群算法之求解带时间窗的多中心VRP【Matlab 023期】
【路径规划】遗传算法之多中心VRP求解【Matlab 024期】

猜你喜欢

转载自blog.csdn.net/m0_54742769/article/details/113041715