customer_demand = test_date2end 4; 我已经读取了客户需求的数据根据以下遗传代码主函数加入容量约束:clear clc close all; input data test_date = importdatatestxlsx;test_date = importdataa1txt; tw1 means the earliest start time tw2 mea
以下是加入容量约束的主函数代码:
%clear, clc, close all;
%% input data % test_date = importdata('test.xlsx'); test_date = importdata('a1.txt'); % tw1 means the earliest start time, tw2 means the latest start time depot_time_window1 = test_date(1,5); depot_time_window2 = test_date(1,6); customer_demand = test_date(2:end, 4); time_window1 = test_date(2:end, 5); % tw, start time time_window2 = test_date(2:end, 6); % tw, end time service_time = test_date(2:end, 7); % service time vertexs = test_date(:, 2:3); customer_location = vertexs(2:end, :); customer_number = size(customer_location, 1);
vc_capacity = 1500; robot_number = 15; % number of vehicles 车辆数
% dist matrix distance_pair = pdist(vertexs); % pairwise distance distance_matrix = squareform(distance_pair);
%% GA parameters population = 20; %种群内个体数(一般20-100个) generation = 1; %开始种群代数 maximum_generation = 100; %终止进化代数(一般100-500代) probability_crossover = 0.9; %交叉概率(一般0.4-0.99) probability_mutation = 0.05; %变异概率(一般0.0001-0.1) rate_gap = 0.9; length_chrom = customer_number + robot_number - 1;
%% initialize population init_chrom = createInitChrom(customer_number, time_window1);
chroms = createInitPopulation(population, length_chrom, customer_number, init_chrom);
% display some values of the initial random population [VC,NV,TD,violate_num,violate_cus] = decode(chroms(1,:),customer_number,time_window1,time_window2,depot_time_window2,service_time,distance_matrix); disp('The initial population:'); disp(['Number of Robots: ', num2str(NV), ', Total Distance: ', num2str(TD), ', Number of violated Path: ', num2str(violate_num), ', Number of Violated Customer: ', num2str(violate_cus)]); fprintf('\n'); % calsulate the value of objective function ObjV = calObj(chroms,customer_number,time_window1,time_window2,depot_time_window2,service_time,distance_matrix, vc_capacity); % the minimal value pre_objective_value=min(ObjV);
%Display the change of the objective function value figure; hold on; box on; xlim([0, maximum_generation]); title('Optimization Process'); xlabel('Generation'); ylabel('The Optimal Value');
iter_time = []; last_dist = 0; stop_count = 0;
%% the loop while generation <= maximum_generation
tic;
% calculate the fitness value
ObjV = calObj(chroms,customer_number,time_window1,time_window2,depot_time_window2,service_time,distance_matrix, vc_capacity);
% draw the line
line([generation - 1, generation], [pre_objective_value, min(ObjV)]); pause(0.0001);
pre_objective_value = min(ObjV);
FitnV = Fitness(ObjV);
% select
SelCh = Select(chroms,FitnV,rate_gap);
% crossover
SelCh = Recombin(SelCh,probability_crossover);
% mutation
SelCh = Mutate(SelCh,probability_mutation);
% local search
SelCh = neighborhoodSearch(SelCh, customer_number, time_window1, time_window2, depot_time_window2, service_time, distance_matrix, vc_capacity);
%
chroms = Reins(chroms,SelCh,ObjV);
% Delete duplicate chromsomes
chroms = DealRepeat(chroms);
ObjV = calObj(chroms,customer_number,time_window1,time_window2,depot_time_window2,service_time,distance_matrix, vc_capacity);
[minObjV,minInd]=min(ObjV);
disp(['Generation:' num2str(generation)]);
[bestVC,bestNV,bestTD,best_vionum,best_viocus]=decode(chroms(minInd(1),:),customer_number,time_window1,time_window2,depot_time_window2,service_time,distance_matrix);
disp(['Number of Robots: ', num2str(bestNV), ', Total Distance: ', num2str(bestTD), ', Number of violated Path: ', num2str(best_vionum), ', Number of Violated Customer: ', num2str(best_viocus)]);
fprintf('\n');
generation = generation + 1;
iter_time(generation) = toc;
%以下为“收敛准则”,当最优适应度值连续 31 次未发生变化时,认为算法已经收敛,并提前结束迭代
if last_dist == bestTD
stop_count = stop_count + 1;
if stop_count > 30
break;
end
else
last_dist = bestTD;
stop_count = 0;
end
end ObjV = calObj(chroms,customer_number,time_window1,time_window2,depot_time_window2,service_time,distance_matrix, vc_capacity); [minObjV,minInd]=min(ObjV);
disp('Optimal Result:') bestChrom=chroms(minInd(1),:); [bestVC,bestNV,bestTD,best_vionum,best_viocus]=decode(bestChrom, customer_number, time_window1, time_window2, depot_time_window2, service_time, distance_matrix); disp(['Number of Robots: ', num2str(bestNV), ', Total Distance: ', num2str(bestTD), ', Number of violated Path: ', num2str(best_vionum), ', Number of Violated Customer: ', num2str(best_viocus)]);
flag = Judge(bestVC, time_window1, time_window2, depot_time_window2, service_time, distance_matrix);
% check the missing element in the final result missing_element = judgeFullElement(bestVC, customer_number);
drawMap(bestVC, vertexs);
total_time = sum(iter_time); disp(['Total Running Time: ', num2str(total_time), ' s']); average_time = total_time/generation; disp(['Average Running Time per Generation: ', num2str(average_time), ' s'])
原文地址: https://www.cveoy.top/t/topic/ehW1 著作权归作者所有。请勿转载和采集!