Decode Chromosome for Vehicle Routing Optimization
% This function is used to decode the chromosome and generate the routes for each vehicle % Input: % chrom - the chromosome to be decoded % customer_number - the number of customers % a - the earliest start time for each customer % b - the latest end time for each customer % L - the maximum duration time for each customer % service_time - the service time for each customer % dist - the distance matrix between customers and depot % Output: % VC - a cell array containing the routes for each vehicle % NV - the number of vehicles used in the solution % TD - the total travel distance for all vehicles % violate_num - the number of violated routes % violate_cus - the number of violated customers
function [VC, NV, TD, violate_num, violate_cus] = decode(chrom, customer_number, a, b, L, service_time, dist)
violate_num = 0;
violate_cus = 0;
VC = cell (customer_number, 1);
count = 1;
% find the last customer visited in the chromosome, which is added at the end in change.m
location0 = find(chrom > customer_number); % find the location of the last customer visited
for i = 1:length(location0)
if i == 1
% create a new route, including the number of depot
route = chrom(1:location0(i));
% delete the number of depot at the end
route(route == chrom(location0(i))) = [];
else
route = chrom(location0(i - 1):location0(i));
route(route == chrom(location0(i - 1))) = [];
route(route == chrom(location0(i))) = [];
end
VC{count} = route; % add the route to set
count = count + 1;
end
% for the last route
if isempty(location0)
route = chrom;
else
route = chrom(location0(end) : end);
route(route == chrom(location0(end))) = [];
end
VC{count} = route;
% delete empty routes, and get the number of vehicles
[VC,NV]=deleteEmptyRoutes(VC);
for j=1:NV
route = cell(1,1);
route{1} = VC{j}; % pick up one route
% check the violated
flag = Judge(route,a,b,L,service_time,dist);
if flag == 1
% if one customer in this route is violated, then all customers in
% this route are violated
violate_cus = violate_cus + length(route{1});
violate_num = violate_num + 1;
end
end
TD=travel_distance(VC,dist);
end
原文地址: https://www.cveoy.top/t/topic/nN7I 著作权归作者所有。请勿转载和采集!