Solving Optimization Problems

How to Solve Open Vehicle Routing Problem Using Genetic Algorithm

Hello everyone!

In this post, I’m going to show you-how to solve an open vehicle routing problem using Genetic Algorithm.

This Genetic Algorithm is coded in Matlab, and a Google map is used to visualize the optimal solution.

It is possible to download, and customize this Matlab code to solve new vehicle routing problems, as well as the famous travelling salesman problems.

Let’s see how it works:

For more videos like this, check my YouTube channel here.

Matlab code

population.m

function Y=population(n,nc)
% nc = number of cities
% n = pop size
B=zeros(n,nc);
for j=1:n
    A=1:1:nc;   
    [x y]=size(A);
    for i = 1:nc
        r = randi(y);
        B(j,i)=A(r);
        A(:,r) = [];
        [x y]=size(A);
    end
end
Y=B;

crossover.m

function YY = crossover(X,n)
% X = population
% n = Number of chromosomes to be crossed
[x1 y1] = size(X);
Y = zeros(n,y1);

for z = 1:n
    B = X(randi(x1),:); % select parent chromosome
    r1 = 1 + randi(y1-1);
    C = B(1,1:r1);
    B(:,1:r1) = [];  % cut
    [x3 y3] = size(B);
    B(1,y3+1:y1) = C;
    Y(z,:) = B;
end
YY = Y;

mutation.m

function Y = mutation(X,n)
% X = population
% n = number of chromosomes to be mutated
[x1 y1]=size(X);
Y=zeros(n,y1);

for z=1:n
    A=X(randi(x1),:); % select parent chromosome
    r1=1+randi(y1-1,1,2);
    while r1(1)==r1(2)
            r1=1+randi(y1-1,1,2);
    end
    B=A(1,r1(1));
    A(1,r1(1))=A(1,r1(2));
    A(1,r1(2))=B;
    Y(z,:)=A;
end
YY = Y;

evaluation.m

function YY=evaluation(P,Data)
% P = population size
% Data = city locations
[x0 y0]=size(P);

for i = 1:x0
    A=P(i,:); % one chromosome
    B=zeros(size(A));
    for j1 = 1:y0-1
        [x1 y1]=find(Data(:,1)==A(1,j1));
        [x2 y2]=find(Data(:,1)==A(1,j1+1));
        B(1,j1)=sqrt((Data(x1,2)-Data(x2,2))^2+(Data(x1,3)-Data(x2,3))^2);
.
.
.
Sorry! This is only a half of the Python code.   

Notice: It would take you from 1 to 3 hours to re-type the Matlab code yourself; or with just €2.99 (the cost of a cup of coffee), you can download/copy the whole Matlab code within 2 minutes. It’s your choice to make.

Original price is €5.99 but today it’s only €2.99 (save €3 today – available for a limited time only)

Download the whole Matlab code here (Membership Code ID: 030)

No need to build the Matlab code from scratch because it’s very time-consuming. My idol, Jim Rohn, once said: “Time is more value than money. You can get more money, but you cannot get more time”. If you think this code can be used in your research/teaching work, you should download it and then customize/modify/apply it to your work, without any obligation of citing the original source if you don’t want. However, redistribution (i.e., downloading the code/script here and then making it available on another site on the Internet) is strictly prohibited.

If you have any question or problem, please contact Dr. Panda by email: learnwithpanda2018@gmail.com

Thank you very much and good luck with your research!

Dr.Panda

View Comments

Recent Posts

Adaptive Re-Start Hybrid Genetic Algorithm in Matlab

Hello everyone! In this post, I am going to show you my innovative version of…

6 months ago

Test Your Understanding About Genetic Algorithm (Test 2)

Hello everyone. Let’s take a test to check your understanding about genetic algorithm, with multiple…

7 months ago

Adaptive Restart Hybrid Genetic Algorithm

Hello everyone! In this post, I am going to show you my innovative version of…

7 months ago

Adaptive Re-Start Hybrid Genetic Algorithm (Test the Performance in Case Studies)

Hello everyone. In this post, I am going to show you my innovative version of…

1 year ago

Adaptive Re-Start Hybrid Genetic Algorithm in Matlab

Hello everyone! Let’s see how my innovative version of Genetic Algorithm, called Adaptive Re-start Hybrid…

1 year ago

Crypto Quiz (Test Your Knowledge About Cryptocurrency)

Hello everyone! Let’s take a short quiz, to test your knowledge about crypto-currency, or crypto.…

1 year ago