[TWVRP] Genetic algorithm for vehicle routing problem with time window [phase 002 with Matlab source code]

Posted by patrickmvi on Sun, 30 Jan 2022 02:19:53 +0100

1. Introduction

1 VRP Problem
Vehicle Routing Problem (VRP) generally refers to: for a series of shipping and receiving points, the organization calls certain vehicles, arranges appropriate driving routes, and makes the vehicles pass through them in an orderly manner. Under the specified constraints (e.g. quantity of goods needed and shipped, delivery time, vehicle capacity limit, driving mileage limit, driving time limit, etc.), strive to achieve certain goals (e.g., the shortest total mileage of empty vehicles, the lowest total transportation cost, the arrival of vehicles at a certain time, the smallest number of vehicles used, etc.).

2 VRPTW Problem
Vehicle Routing Problem with Time Window (VRPTW) is a new problem caused by adding delivery time constraints to VRP. In such problems, given the earliest and latest time for a vehicle to arrive at the destination, the vehicle is required to arrive within a specified time window, with additional penalties incurred for arriving earlier or later than the earliest time. At this point, it is decided how to plan and schedule the vehicles to minimize the total cost of distribution.

3 VRP versus VRPTW:

4 Problem Description
The questions to be studied in this article are referred to in the article Fruit and Vegetable Agricultural Products Logistics Transport Routing Optimization - A Case Study of Qingdao blueberries distribution.

A fruit and vegetable agricultural product transportation and distribution center C0 (Center). The distribution center has sufficient capacity to meet all customer requirements for the quantity of fruits, vegetables and agricultural products. At the same time, the distribution center has enough and identical vehicles J to complete the distribution activities. The maximum capacity of the transportation vehicles is V(Volume). The distribution vehicles can arrive at one time during the distribution activities, without any obstacles or special circumstances. C={C0, C1, C2...Cn}. C0 represents the distribution center. Ci (i=1,2,..., n)(Consumer) indicates the number of customers i n demand; N denotes the number of customers in demand. Dik (Distance): Indicates the distance from customer Ci to customer Ck (where I is not equal to k); Qdi (Quantity Demanded): Indicates the demand for customer Ci; Qg (Quality good): Indicates the quality of fruits and vegetables when they are just harvested and intact. [ETi, LTi] Represents a customer Ci time window constraint for a product.

When the above conditions are known, the optimal route can be reasonably arranged to minimize the sum of each cost when all conditions are met in the distribution process.

5 Mathematical model
(Refer to the specific model (3) Chinese literature)




6 algorithm design
Individual Coding
There are many encoding methods in the literature of genetic algorithm solving VRPTW. Here natural number encoding is used for individuals, representing distribution centers and customers. The delivery routes of different vehicles are separated by 0 (that is, each vehicle starts from the warehouse); For a customer who has a VRP problem with a car, the chromosome length is.

For example, a distribution center has three cars serving eight customers and one possible chromosome is as follows:
0, 7, 0, 1, 2, 3, 5, 0, 8, 4, 6, 0
This chromosome represents the route of three vehicles:
First car: 0-7-0
Second car: 0-1-2-3-5-0
Third car: 0-8-4-6-0

punishment
In the offspring of crossover and mutation, there may be two forms of constraint violation:

Vehicle overload
Cannot reach the specified customer within the latest time given by the time window constraint
Static penalties are applied for these two violations, which impose a larger penalty factor considering that time windows are more likely to be violated. The penalty factors for violations of these two constraints are set to 10 and 500, respectively.

overlapping
Here refers to the crossover operation given in the Study of Route Optimization with Time Window Based on Electric Vehicles

mutation
Optimize each subroute of the selected individual with a 2-opt algorithm

Choice
Breeding Selection: binary Championship Selection
Environmental Selection: Select individuals of the same size as the ethnic group by employing an elite retention strategy that combines offspring with parents
%

Source Code

clear
clc
close all
tic
%% use importdata This function reads files
c101=importdata('c101.txt');
vehicle_info = [2 4	350	50;
3	6	350	50;
6	15	450	100;
10	17	550	110;
13	30	650	140;
15	40	650	140;
25	50	850	180];
cap=200;                                                        %Maximum Vehicle Load
%% Extracting data information
E=c101(1,5);                                                    %Distribution Center Time Window Start Time
L=c101(1,6);                                                    %Distribution Center Time Window End Time
vertexs=c101(:,2:3);                                            %Coordinates of all points x and y
customer=vertexs(2:end,:);                                       %Customer coordinates
cusnum=size(customer,1);                                         %Number of customers
v_num=21;                                                        %Maximum number of vehicles used
demands=c101(2:end,4);                                          %Requirement
a=c101(2:end,5);                                                %Customer time window start time[a[i],b[i]]
b=c101(2:end,6);                                                %Customer time window end time[a[i],b[i]]
s=c101(2:end,7);                                                %Service time at customer point
h=pdist(vertexs);
dist=squareform(h);                                             %Distance matrix, satisfying triangular relationships, temporary distance represents cost c[i][j]=dist[i][j]
%% Genetic algorithm parameter settings
alpha=10;                                                       %Penalty function coefficients for violating capacity constraints
belta=100;                                                      %Penalty Function Factor Violating Time Window Constraints
NIND=100;                                                       %Population size
MAXGEN=100;                                                     %Number of iterations
Pc=0.9;                                                         %Crossover probability
Pm=0.05;                                                        %Probability of Variation
GGAP=0.9;                                                       %Generation gap(Generation gap)
N=cusnum+v_num-1;                                               %Chromosome Length=Number of customers+Maximum number of vehicles used-1
%% Initialize Population
init_vc=init(cusnum,a,demands,cap);                             %Construct initial solution
Chrom=InitPopCW(NIND,N,cusnum,init_vc);
%% Routes and total distances for outputting random solutions
disp('A random value in the initial population:')
[VC,NV,TD,violate_num,violate_cus]=decode(Chrom(1,:),cusnum,cap,demands,a,b,L,s,dist);
% disp(['Total Distance:',num2str(TD)]);
disp(['Number of vehicles used:',num2str(NV),',Total Vehicle Distance:',num2str(TD),',Number of constraint violation paths:',num2str(violate_num),',Number of Customers Violating Constraints:',num2str(violate_cus)]);
disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
%% optimization
gen=1;
figure;
hold on;box on
xlim([0,MAXGEN])
title('Optimization process')
xlabel('Algebra')
ylabel('optimal value')
ObjV=calObj(Chrom,cusnum,cap,demands,a,b,L,s,dist,alpha,belta);             %Calculating population objective function values
preObjV=min(ObjV);
while gen<=MAXGEN
    %% Calculate Fitness
    ObjV=calObj(Chrom,cusnum,cap,demands,a,b,L,s,dist,alpha,belta);             %Calculating population objective function values
    line([gen-1,gen],[preObjV,min(ObjV)]);pause(0.0001)
    preObjV=min(ObjV);
    FitnV=Fitness(ObjV);
    %% Choice
    SelCh=Select(Chrom,FitnV,GGAP);
    %% OX Cross operation
    SelCh=Recombin(SelCh,Pc);
    %% variation
    SelCh=Mutate(SelCh,Pm);
    %% Local search operation
    SelCh=LocalSearch(SelCh,cusnum,cap,demands,a,b,L,s,dist,alpha,belta);
    %% New Population of Reinserted Offspring
    Chrom=Reins(Chrom,SelCh,ObjV);
    %% Delete duplicate individuals from the population and complete the deleted individuals
    Chrom=deal_Repeat(Chrom);
    %% Print the current optimal solution
    ObjV=calObj(Chrom,cusnum,cap,demands,a,b,L,s,dist,alpha,belta);             %Calculating population objective function values
    [minObjV,minInd]=min(ObjV);
    disp(['No.',num2str(gen),'Generation optimal solution:'])
    [bestVC,bestNV,bestTD,best_vionum,best_viocus]=decode(Chrom(minInd(1),:),cusnum,cap,demands,a,b,L,s,dist);
    disp(['Number of vehicles used:',num2str(bestNV),',Total Vehicle Distance:',num2str(bestTD),',Number of constraint violation paths:',num2str(best_vionum),',Number of Customers Violating Constraints:',num2str(best_viocus)]);
    fprintf('\n')
    %% Number of update iterations
    gen=gen+1 ;
end
%% Map the best solution
ObjV=calObj(Chrom,cusnum,cap,demands,a,b,L,s,dist,alpha,belta);             %Calculating population objective function values
[minObjV,minInd]=min(ObjV);
%% Routes and total distances for output optimal solutions
disp('optimum solution:')
bestChrom=Chrom(minInd(1),:);
[bestVC,bestNV,bestTD,best_vionum,best_viocus]=decode(bestChrom,cusnum,cap,demands,a,b,L,s,dist);
disp(['Number of vehicles used:',num2str(bestNV),',Total Vehicle Distance:',num2str(bestTD),',Number of constraint violation paths:',num2str(best_vionum),',Number of Customers Violating Constraints:',num2str(best_viocus)]);
disp('-------------------------------------------------------------')
%% Determines whether the optimal solution satisfies the time window and load constraints, 0 indicates violation of constraints, 1 indicates all constraints
flag=Judge(bestVC,cap,demands,a,b,L,s,dist);
%% Check for missing elements, missing elements, or null elements in the optimal solution
DEL=Judge_Del(bestVC);
%% Draw the final Roadmap
draw_Best(bestVC,vertexs);
save c101.mat
toc

3. Operation results

4. Remarks

Version: 2014 a

Topics: MATLAB