Solving vehicle scheduling problem with genetic algorithm

Keywords: Algorithm Deep Learning

preface:

Now let's assume that n factories send out demand at the same time every morning, that is, how many goods do you want from which factory. Then, it is better to carry out unified allocation. In the later stage, we will realize that a separate factory will issue orders, and then dispatch and solve them separately. At present, the code only considers the scheduling of all factories.

1, We have three input parameters:

1. The first is the distance between factories (i.e. nodes) (8 nodes in this example)

2. Then the quantity of goods desired by each factory (we thought about the demand of each factory for which factory, but this question is for the demand of the logistics center, that is, not for which specific factory)

3. The third parameter is the distance from the logistics center to each factory node

2, Main program

In each generation, he set up 80 samples, that is, 80 * 8 two-dimensional matrix
A total of 200 generations
The 80 samples of each generation must be cross mutated first
(I feel this place is a little violent)

1. First, the population initializes 80 × 8 matrix
2. Then while 200 layers of loops, the cross variogram is performed in the loop body of each layer, and the specific function is in the third module
3. After crossing, it is still 80 * 8, but after variation, it becomes 132 × 8, 132 samples
4. Then, through the size function and the variable is set to 1, the number of rows of the two-dimensional matrix is obtained, i.e. 132
5. Then, a for loop is performed on 132 lines. Each sample is decoded and the shortest distance is calculated. The calculation result is placed in Total_Dis, that is, the matrix is 132 × one
6. Then the key population is updated. He arranges the Total_Dis in ascending order (i.e. find the 80 samples with the shortest distance), then takes out the corresponding 80 samples according to the index, and then puts them into the next generation to continue the same operation
7. Finally, after 200 generations of continuous elimination, select the lowest sample in Total_Dis among the 80 children of the last generation, that is, the final result (which factory should all vehicles go to for delivery first and then which) (then allocate vehicles one by one according to this result - this idea is simple) (so the key to the procedure is to find an overall result)

The decoding function below also needs to be converted into a java program




main program

%Algorithm parameters
population_num=80;%Population size
Max_gen=200;%Number of iterations
Pc=0.9;%Crossover probability
Pm=0.09;%Variation probability
%%
%Problem parameters
%Number of vehicles Car_num=2
%Number of customers Customer_num=8
%Vehicle capacity capacity_max=8
%Travel distance distance_max=50
Car_num=2;
Customer_num=8;
capacity_max=8;
distance_max=50;
load Demand %Customer needs
load Distance %Distance between customers
load X %Distance from logistics center to customers
%%
%Population initialization
population=zeros(population_num,Customer_num);
for i=1:population_num
     population(i,:)=randperm(Customer_num);
end
%%
y=1;%Cycle counter
 while y<Max_gen
     %overlapping
     [new_pop_intercross]=Mating_pool(population_num,population,Pc);
     %variation
     [new_pop_mutation]=Mutation(new_pop_intercross,Pm);
     %Calculate objective function
     mutation_num=size(new_pop_mutation,1);
     Total_Dis=[];
     for k=1:mutation_num
     [Result]=decode(new_pop_mutation(k,:),distance_max,capacity_max);
     [Total_Dis(k,1)]=parameter(Result,Customer_num,Car_num);
     end
     %Renewal population
     new_pop_new=zeros(population_num,Customer_num);
     [Total_Dissort, index] = sort(Total_Dis);
     
     for k=1:population_num
         new_pop_new(k,:)=new_pop_mutation(index(k),:);
     end
     population=new_pop_new;
     %Number of iterations plus one
     y=y+1;
 end
 Dis_min1=min(Total_Dis);
for k=1:mutation_num
    if Total_Dis(k,1)==Dis_min1
       position1= k;
       break
    end
end
X_Best=new_pop_mutation(position1,:)
Y_Obj=Total_Dis(position1,1)
t=toc;

3, Cross Variogram

Cross function

function [new_pop_intercross]=Mating_pool(population_num,population,Pc)
%%
%input:population,population_num,Pc
%Output: 1.new_popopulation_intercross
%     2.c3,Pairing pool: random population population Pairing
%     3.pool
%%
pl=randperm(population_num);
num=population_num/2;
c3=zeros(2,num);
pool=[];
new_pop_intercross=population;
for kj=1:num
    c3(1,kj)=pl(2*kj-1);
    c3(2,kj)=pl(2*kj);
end%Generate pairing pool c3"

%%Judge "pairing pool" c3"Is the random number of each pair of individuals less than the crossover probability Pc
rd=rand(1,num);
for kj=1:num
    if rd(kj)<Pc
       pool=[pool,c3(:,kj)];
    end
end
%%Judge whether the random number of each pair of individuals in the pairing pool is less than the crossover probability Pc,If it is less than, save it to the "child pool" pool"

pool_num=size(pool,2);
for kj=1:pool_num
    c1=population(pool(1,kj),:);
    c2=population(pool(2,kj),:);
    [new_c1,new_c2]=cross(c1,c2);
    new_pop_intercross(pool(1,kj),:)=new_c1;
    new_pop_intercross(pool(2,kj),:)=new_c2;
end
end

    

Variogram

function [Mut_Pop]=Mutation(Cross_Pop,Pm)
Mut_Pop=Cross_Pop;
Cross_Pop_num=size(Cross_Pop,1);
for j=1:Cross_Pop_num
    A=Cross_Pop(j,:);
    A_1=A;
    n=size(A,2);
    r=rand(1,n);
    Pe=find(r<Pm);%Mutation probability can be introduced
    sum_Pe=size(Pe,2);
    for i=1:sum_Pe
        c=A(Pe(i));
        A_1(Pe(i))=A_1(find(r==max(r)));
        A_1(find(r==max(r)))=c;
        Mut_Pop=[Mut_Pop;A_1];
    end
end

4, Decoding function

decode.m

function [Result]=decode(T,distance_max,capacity_max)
%distance_max=50;
%capacity_max=8;
load Demand
load Distance
load X
WS=1;WT=1;
a1=0;b1=0;
for i=1:size(T,2)
    if WT==1
       a1=2*X(T(i));
       b1=Demand(T(i));
    elseif WT==2
        a1=X(T(i-1))+X(T(i))+Distance(T(i),T(i-1));
        b1=Demand(T(i-1))+Demand(T(i));
    else
        a1=0;b1=0;
        for j=i-WT+1:i-1
            a1=a1+Distance(T(j+1),T(j));
            b1=b1+Demand(T(j));
        end
        b1=b1+Demand(T(i));
        a1=a1+X(T(i-WT+1))+X(T(i));
    end
    if (a1>distance_max)|(b1>capacity_max)
        a1=2*X(T(i));
        b1=Demand(T(1));
        WS=WS+1;
        Result(i,:)=[T(i),WS,a1,b1];
        WT=2;
    else
        Result(i,:)=[T(i),WS,a1,b1];
        WT=WT+1;
    end
   
end


parameter.m

function [Total_Dis]=parameter(Result,Customer_num,Car_num)
%% decode
if Result(Customer_num,2)<=Car_num
Current_Workstation=1;
Total_Dis=0;
for i=1:Customer_num
    if Result(i,2)==Current_Workstation;
        continue
    else
        Total_Dis=Total_Dis+Result(i-1,3);
        Current_Workstation=Current_Workstation+1;
    end  
end
Total_Dis=Total_Dis+Result(Customer_num,3);
else
    Total_Dis=10000;%Introducing penalty function
end

Posted by smclay on Tue, 21 Sep 2021 20:16:08 -0700