You are on page 1of 28

Part 1: Representation and explanation of the given problem

This is a classical optimization problem in graphic thoery. Variable descriptions


and mathmatical model are given below:

Variables:
K: the number of vehicles;
Q: capacity of each vehichle (all vehicles have the same capacity;
!: inde" of each vehicle;
#
!
: length travelled by the !th vehicle;
$
!
: the number of customers assigned to the !th vehicle;
%: the number of all customers;
"
m!
: " coordination of the mth customer corresponding to the !th vehicle;
y
m!
: y coordination of the mth customer corresponding to the !th vehicle;
&
m!
: demand amount of the mth customer corresponding to the !th vehicle;
T
m!
: arriving time of the mth customer corresponding to the !th vehicle;
T
'
m!
: ready time of the mth customer corresponding to the !th vehicle;
T
(
m!
: due time of the mth customer corresponding to the !th vehicle;
T
!
: the time when the !th vehicle returns to the original starting location;
T
(
)
: the total consuming time limit;
t: duration of sevice time ( same in all locations ;
#: total distance of all vehicles;
n: the orignal inde" of the given location.
d
!
(i * +: distance between the ith and +th location for the !th vehicle;

Analysis:
The goal is to fulfill demands of all customers at the lowest sum of travelling
distances by all vehicles according to the problem re,uirement. -or operational sa!e*
all the customers have to be grouped into K sections. The !th section is supposed to
contain $
!
specific customers which are assigned to the !th vehicle.

.onstraints can be listed in two aspects:
'.

number and volumes of the vehicles;
(.

time constraints including: ready time and due date of each customer and the
total amount of time used by each vehicle. The latter one is mentioned in files as
due date of the )
th
location.


Mathematical model:

/b+ective:


in which* we give the general mathematical notation of the optimization
ob+ective and overall restraints. #ome detailed relations among these varialbes are
stated as following:

where d(m*m0' originally represents the distance between the mth and (m0'th
customer* yet the tas! description thin!s that this is ea,ual to the time consumed in
the +ounry between the two locations. 1s a result* we directly adopt d(m*m0' as the
time. t is the service time in the mth location for !th vehicle. The total time during the
+ouney of the !th vehicle T
!
can be seen as the sum of 2 parts: ' elasped time when
the !th vehicle arrives the last customer $
!
; ( sevice duration at the $
!
th location; 2
time used in the way from the $
!
th location to the starting point.

Part 2: Genetic Algorithm for VRP

The mathematical model only lists the general relations among all the variables
and give a overall description of the problem. 3owever* several critical issues call for
solutions:
'.

3ow to group these customers4
(.

-or a certain group* how to choose the e"act route4
5n fact* the two issues are mutually reliant. 5f the former one can be solved* then
V67 problem can be transformed into the T#7 (Travelling #alesman 7roblem.
&istances in certain routes will definitely influence the way to devide customers.

Chromosome generating:

The coding of V67 problem is ,uite different from norm problems. 1ll the
original chromosomes must obey all the constraints above* as a result* we are
confromted with some chromosome structure problem.

'. Time restraint of all customers. 8e build a directional graph spanned by all the
locations. 8e !now that the p
th
customer has a ready time T
'
p
and a due time T
(
p
* there
is a route from customer p to customer , only when

which describs that if there is a vehicle moves from customer p* with time
consumed on the way to ,
th
customer and service time at location p ta!en into
consideration* the time when the vehicle arrives at the ,
th
location must lie between
the ready time and due time of the the ,
th
customer.
9y this* we successfully transfer a complete graph into a directional graph whose
degree is substantially decreased* and many routes need not to be considered.
(. Time constraint of a route. 1s is stated above* the time of traversing all the
locations of a route and returning to the starting point must be less than the due time
of the starting point. 9y further e"amination of some given e"amples and routine
thin!ing we !now that if there is no violation of the time restraint of the last customer
of a route* the vehicle can return to the starting point on time.
2. .apacity limitation. 5n detailed operation* we will randomly choose a
customer as the first customer of a certain route* and then there only some ad+acent
points ne"t to it* from which we choose one as the second customer of the route* and
so on. 1t each location* we will compute the cumulative demand of the customers on
present route* a cease of the route when the cumulative demand e"ceeds the vehicle
capacity. Then choose another customer as the first point of a new route and e"cute
the same procedure. 5t must be addressed that each customer can only be contained in
one route.
:. .onstaint of the number of vehicles. 8e can e"cute the progress stated in #tep
2* if the chromosome uses more vehicles than allowed* regenerate a new one until it
satisfy all vehicle number constraint.


stimation f!nction:

#uppose there are % customers and K vehicles* there should be %0K0'
elements in the serial above. /ur ob+ective is to minimize the total distance* which in
the specific serial can be e"pressed as the sum of all distances between two
consecutive elements. 5 would li!e to choose such a calculation as fitness value.

where i is the inde" in the serial and d(i* i0' represents the distance between the
i
th
and (i0'
th
location.

Crossover and variation:

.rossover here are also barried by the structual problem (all the constraints* we
can not merely choose a crossover point and e"change haves of two chromosomes as
normal. 1s the problem is based on the directional graph* and then we need only thin!
over constraints of capacity limitation and vehicle number.
8e randomly choose two chromosomes from parental generation. 8e build a
new directional graph among customers only(no consideration of the starting point*
all the routes between points are selected from the two chromosomes. 5n fact* ,uite a
lot are repeated in two chromosomes* each verte" has no more than two inputs and
two outputs. %aybe some points are isolated (in two chromosomes the point is a
route. Then we can simplify the directional graph. 8e firstly choose a shorter input
of two;inputs point and desert the other one. Then choose a shorter output of two;
outputs and let the other abandoned. 8ith the redued graph* we furtherly generate the
vehicle routes as above* in which capacity are ta!en account.

5n each ieration* we randomly generate several chromosomes as variation.

Part": Calc!lation res!lt:

8e choose the data listed in file <.'=(='.t"t> as an e"ample.
?ach generation ') chromosomes are selected and 2 iterations are ta!en. 8e only
give the e"act chromosome with the best fitness.

5nitial generation:
5n
de"
' ( 2 : @ A B C D '
)
-i
tness
(
min
:
(D(.D
:
A)'.B
@
@)C.B
:
BD'.C
:
2D'.A
B
:2C.C
B
2A'.(
A
@A2.(
@
DB(.2
A
B)@.:
9est fitness: :(D(.D
9est chromosome:
5nde" 6oute
' ) () :' '( '(D '' A '(( D) AB 'B 2D )
( ) (' (2 'C( B@ 'A2 'D: ':@ 'D@ @( D( )
2 ) (: A' '2D )
: ) 2) '() 'D 'D( 'DA DB ': CD ')@ '@ @D )
@ ) 2( 'B' A@ CA ''@ D: @' '') 'A( )
A ) :@ '@@ )
B ) ':: ''D 'AA 2@ '(A B' D ' DD @2 )
C ) ')) A: 'BD ')D ':D @A )
D ) @B @ ') 'D2 :A '(C ')A 'AB 2: D@ '@C )
') ) '': '@D 2C '@) (( '@' 'A ':) 'CB ':( ''' A2 )
'' ) B2 ''A ':B 'A) :B D' B) )
'( ) BC 'B@ '2 :2 ( )
'2 ) 'D) C( )
': ) C@ C) 2' (@ 'B( BB )
'@ ) D2 @@ 'C @: 'C@ '2( B 'C' 'CC ')C )
'A ) ')' )
'B ) '22 :C (A '@( :) '@2 'AD DA '2) (C B: ')B )
'C ) ''C C2 ':2 'BA 2A 22 '(' 'A@ :D )
'D ) ':C ')2 'DB '(: ':' AD ()) 'B: '2A 'CD )
() ) 'A' '): )
(' ) 'A: AA )
(( ) 'B) '2: @) '@A ''( 'AC BD (D CB :( '(2 'DC )
(2 ) 'BB 2 CC C 'CA '(B DC '@B )
(: ) 'BC (B 'B2 '@: )
(@ ) '2@ @C 'C: 'DD 2B C' '2C '2B 'C2 )
(A ) 'C) C: 'D' '(@ : B( )
(B ) :: ')( ':A )
(C ) A) )
(D ) A( )
2) ) AC BA )
2' ) ''2 )
2( ) ''B )
22 ) '2' )

#econd generation:
5n
de"
' ( 2 : @ A B C D '
)
-i
tness
(
min
2
@@D.@
2
:').C
2
B'(.@
:
(:A.:
2
C@(.'
A
)B@.2
A
:D(.A
B
@2A.'
A
D().C
@
AB'.:
9est fitness: 2:').C
9est chromosome:
5nde" 6oute
' ) () :' C@ C) 2' (@ 'B( BB '') 'A( )
( ) (' (2 'C( B@ 'A2 'D: ':@ 'D@ @( D( )
2 ) 2) '() 'D 'D( 'DA DB ': DA '2) (C B: ':D )
: ) 2( 'B' A@ CA ''@ D: @' 'B: '2A 'CD )
@ ) :@ (B 'B2 '@: (: A' ')) A: 'BD ')D )
A ) 'A' '): 'C @: 'C@ '2( B 'C' ''B :D )
B ) ')' ':: ''D 'AA 2@ '(A B' D ' DD @2 )
C ) @) '@A ''( 'AC BD (D CB :( '(2 'DC @A )
D ) @B ''C C2 ':2 'BA 2A 22 '(' 'A@ 'CC ')C )
') ) '22 :C (A '@( :) '@2 'AD CD ')@ '@ @D )
'' ) A) C( 'C) C: 'D' '(@ : B( )
'( ) A( '2' :: ')( ':A AC BA )
'2 ) 2C '@) (( '@' 'A ':) 'CB ':( ''' A2 )
': ) D2 @@ '2@ )
'@ ) ''2 '@@ BC 'B@ '2 :2 ( D) AB 'B 2D ')B )
'A ) ''A '( '(D '' A '(( '2D )
'B ) ':C ')2 'DB '(: ':' AD ()) )
'C ) 'D) @ ') 'D2 :A '(C ')A 'AB 2: D@ '@C )
'D ) 'A: AA ':B 'A) :B D' B) )
() ) 'B) '2: )
(' ) 'BB 2 CC C 'CA '(B DC '@B )
(( ) 'BC )
(2 ) @C 'C: 'DD 2B C' '2C '2B 'C2 )
(: ) B2 )
(@ ) '': )
(A ) '@D )

Third generation:
5n
de"
' ( 2 : @ A B C D '
)
-i
tness
(
min
(
CBB.2
2
A)(.A
2
'AA.@
:
DC(.B
:
B('.A
A
(C(.B
B
)C:.D
A
B'A
A
:)(.'
A
D)B.A
9est fitness: (CBB.2
9est chromosome:
5nde" 6oute
' ) () :' C@ C) 2' (@ 'B( BB '') 'A( )
( ) (' (2 'C( B@ 'A2 'D: ':@ 'D@ @( D( )
2 ) 2) '() 'D 'D( 'DA DB ': CD ')@ '@ @D )
: ) 2( 'B' A@ CA ''@ D: @' 'B: '2A 'CD )
@ ) :@ (B 'B2 '@: (: A' ')) A: 'BD ')D ':D )
A ) 'A' '): 'C @: 'C@ '2( B 'C' ''B :D )
B ) ')' ':: ''D 'AA 2@ '(A B' D ' DD @2 )
C ) D2 @@ '2@ @C 'C: 'DD 2B C' '2C '2B 'C2 @A )
D ) @B ''C C2 ':2 'BA 2A 22 '(' 'A@ 'CC ')C )
') ) A) C( 'C) C: 'D' '(@ : B( )
'' ) A( '2' :: ')( ':A AC BA DA '2) (C B: )
'( ) '': '@D 2C '@) (( '@' 'A ':) 'CB ':( ''' A2 )
'2 ) 'A: AA ':B 'A) :B D' B) )
': ) B2 ''A '( '(D '' A '(( '2D )
'@ ) ''2 '@@ BC 'B@ '2 :2 ( D) AB 'B 2D ')B )
'A ) '22 :C (A '@( :) '@2 'AD )
'B ) ':C ')2 'DB '(: ':' AD ()) )
'C ) 'D) @ ') 'D2 :A '(C ')A 'AB 2: D@ '@C )
'D ) 'B) '2: @) '@A ''( 'AC BD (D CB :( '(2 'DC )
() ) 'BB 2 CC C 'CA '(B DC '@B )
(' ) 'BC )
The e"ample given above shows that performance improved rapidly with the
iteration of genetic algorithm. 9y perceptual !nowledge* we manage it by two ways:
(' 5n crossover progress* we cut off the longer edge of the two;input or two;output
verte" to !eep it connected to other verte"es by the remained one. 8e see that the
total ob+ective function if the distance of all vehicle routes* when each element of the
vehicle route becomes shorter* there must be an reduction in the total distace. (( ?ach
vehicle route has two e"tented cost of distance: the first customer and the last
customer of the vehicle route connected to the starting point. 1s the iteration going
on* the number of vehicles really used decreases* so the distance reduction in the
e"tended cost will ensure the total distance optimized.
This is a strictly constrainted problem* which ma!es it hard to variate. 8e
directly randomly select some new chromosomes* in which some new genes are
included. 5t has reached the rule of traditional rule of getting new genes. 8e thin! that
it ma!es sense.

The following gives the matlab codes:
Eou only need to e"cute :
[resultChrom,lastMin,fitAll,ddd]=geneticVRP(iter)
in the command window, where iter denotes the iteration of genetic
method.

% geneticVRP.m
function [resultChrom,lastMin,fitAll,ddd]=geneticVRP(iter)
clear
iter=!
% Assum"tions#
% ($) ser%ice time of all customers is the same
% (&) time and demand constraints can ne%er 'e %iolated
% (() ) as the num'er of %ehicles is onl* a u""er limit, +e need not use
% all the %ehicles
% () no location 'ut the start "oint can 'e re%isited.
%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,
% -ome "re"arations
%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,
%, "arameter descri"tion
origin.ata$$! % load data
[height,+idth]=si/e(origin.ata)!
M=height0$! % num'er of customers
)=,1! % num'er of %ehicles
ca"acit*=&11! % ca"acit* of each %ehicle
2C=,!
coord=origin.ata(&#M3$,&#()! % coordinations
tim4im=origin.ata(&#M3$,,#5)! % time limits
demand=origin.ata(&#M3$,)! % demand
ser6ime=origin.ata(&,7)! % ser%ice time
%, calculate distances of all "oints to start "oint
dis8rg=/eros($,M)!
for i=$#M
dis8rg(i)=s9rt((origin.ata(i3$,&)0origin.ata($,&)):&3(origin.ata(i3$,()0
origin.ata($,()):&)!
end
clear origin.ata!
%, 'uild directional gra"h
"t&=;<!
ctPt=/eros($,M)!
dis&=ones(M)=>nf!
for i=$#M0$
for ?=i3$#M
dis@et+een=s9rt((coord(i,$)0coord(?,$)):&3(coord(i,&)0coord(?,&)):&)3ser6ime!
% distance 'et+een t+o consecuti%e locations
if(tim4im(i,&)3dis@et+eenAtim4im(?,$)BBtim4im(i,$)3dis@et+eenCtim4im(?,&))
ctPt(i)=ctPt(i)3$!
"t&;i<(ctPt(i))=?!
dis&(i,?)=dis@et+een!
end
if(tim4im(?,&)3dis@et+eenAtim4im(i,$)BBtim4im(?,$)3dis@et+eenCtim4im(i,&))
ctPt(?)=ctPt(?)3$!
"t&;?<(ctPt(?))=i!
dis&(?,i)=dis@et+een!
end
end
end
%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,
% Denetic ?o'
%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,
% choose nc cou"les of chromosomes +hich fulfill all the constraints
% calculate the fitness of all the chromosomes
chrom=;<!
numVehi=/eros($,2C=&)!
fit=ones($,2C=&)!
for i=$#2C=&
+hile($)
[chrom;i<,numCus]=random-elect(M,),demand,dis8rg,ctPt,"t&,dis&,ca"acit*)!
if(numCus==M)
'reaE!
end
end
end
% chromosome crosso%er
% let the 2C cou"les of chromosomes randoml* unioned
% regenerate some chromosomes and then select the 'est &=2C chromosomes as
% the "arental chromosomes
% Det a discrete seria
chromChild=;<!
ddd=;<!
fitAll=/eros(iter,&=2C)!
lastMin=inf!
last>ndeF=1!
for ?=$#iter
indeF=rand"erm(&=2C)!
for i=$#2C
chromChild;i<=cross8%er(chrom;indeF(&=i0
$)<,chrom;indeF(&=i)<,dis&,M,),ca"acit*,demand)!
end
for i=$#2C
chrom;i<=chromChild;i<!
+hile($)
[chrom;i32C<,numCus]=random-elect(M,),demand,dis8rg,ctPt,"t&,dis&,ca"aci
t*)!
if(numCus==M)
'reaE!
end
end
fit(i)=fitness(chrom;i<,dis&0ser6ime,dis8rg)!
fit(i32C)=fitness(chrom;i32C<,dis&0ser6ime,dis8rg)!
end
[minChrom,min>ndeF]=min(fit)!
if(minChrom==lastMin)
'reaE!
end
lastMin=minChrom!
last>ndeF=min>ndeF!

lend=length(chrom;last>ndeF<)!
ddd;?<=[]!
for E=$#lend
ddd;?<=[ddd;?<,1,chrom;last>ndeF<;E<]!
end
fitAll(?,#)=fit!
end
resultChrom=chrom;last>ndeF<!
%original.ata$$.m
origin.ata=[1 71 71 1 1 $(,$ 1
$ (( 7G &1 7,1 G1H H1
& ,H ,& &1 ,,( 51& H1
( $1 $(7 (1 $7 &$H H1
&G $1 5$5 55$ H1
, &, &5 &1 $&G $7H H1
5 G5 (7 $1 7G ,($ H1
7 $ $1H $1 5$5 5G1 H1
G 5 $(, 1 (,$ (G5 H1
H (& 7H &1 5,, 7&$ H1
$1 & &5 &1 &$H &7$ H1
$$ G5 (5 &1 (G ( H1
$& H, (, $1 $H5 &,& H1
$( 5( ,1 $1 (5 $5 H1
$ $11 $15 $1 ,57 5&5 H1
$, HH $$& &1 G5 H$$ H1
$5 (5 $(, $1 ,HG 55H H1
$7 ,7 ,H $1 G& GH1 H1
$G G $& $1 &($ &HG H1
$H G, $15 &1 $G5 &,( H1
&1 $1( 5H (1 (( H( H1
&$ $1H $($ &1 7& $$ H1
&& ( $1 $1 $1 71 H1
&( $$, $( (1 $1 $HH H1
& HG 71 $1 57 ,$$ H1
&, $$& 57 $1 5& ,( H1
&5 $1& $1 &1 &1$ &,G H1
&7 H( 7, (1 $7G &(G H1
&G H1 $1 &1 G, H$1 H1
&H $&7 $1G $1 577 7( H1
(1 G HH &1 (& $1& H1
($ $$( 5H &1 (71 1 H1
(& $&H H $1 G $7 H1
(( $G (G (1 ,G& 5(G H1
( (1 &7 $1 77& G(, H1
(, &, G1 &1 (G1 1 H1
(5 $7 (7 (1 H( , H1
(7 (& $15 $1 51& 5,7 H1
(G ( $(, $1 &$, &H( H1
(H 5$ ,H &1 H($ H7$ H1
1 $1 $15 &1 (H1 ( H1
$ $1H 7$ &1 H, $5( H1
& $&$ $$1 (1 G71 H&( H1
( 5$ G &1 G ,$7 H1
7 HH &1 $75 &( H1
, GH 7( $1 $H H H1
5 &$ &, &1 1, ,( H1
7 HH &G (1 (H& & H1
G $1$ H5 $1 $1$ $51 H1
H H $$ &1 H1 H, H1
,1 $&$ $$& &1 &&$ &77 H1
,$ $(7 5 $1 5$$ 57G H1
,& $$G $($ &1 7G& G,7 H1
,( ( G& $1 H&G $111 H1
, $&, (1 ($H (HG H1
,, &5 $1, 1 $$, $7H H1
,5 (, $&( &1 $$(1 $$HG H1
,7 &$ G $1 ,( $&( H1
,G &$ $$, $1 (15 (75 H1
,H HH $1G &1 H,& HH& H1
51 $ ( &1 77 $(& H1
5$ H 71 &1 ,,5 5$1 H1
5& 7 H( &1 &( 55 H1
5( (& $&G &1 $17H $$&( H1
5 H 7( $1 7$ 7H$ H1
5, $(, ( &1 &,1 (11 H1
55 H7 &G &1 $$1 $71 H1
57 ,H ,5 &1 7, 7G1 H1
5G G1 H7 $1 ,7 ,$G H1
5H $( ($ &1 ,1G ,55 H1
71 $1( ( (1 ,77 5(5 H1
7$ (1 7H &1 ,5$ 5(& H1
7& (1 1 5H( 75G H1
7( G7 (H &1 (, H& H1
7 H1 $1$ $1 H(( $11G H1
7, $$H $(5 (1 (( (G H1
75 7G H& (1 ,,, 5$1 H1
77 $$1 55 $1 5,1 7$1 H1
7G 5& ,G &1 $7( &&H H1
7H $&7 $$& &1 ,H, 5(5 H1
G1 $$( 7$ $1 &G5 ($ H1
G$ ( $1 $1 5G( 751 H1
G& & &G $1 $G $HH H1
G( $7 ( $1 &1, &7H H1
G $ & $1 ((( (G, H1
G, $$& 7$ &1 $H( &,& H1
G5 $(, 1 $1 (($ 1, H1
G7 $&, $1G &1 77G G&, H1
GG G $(G (1 &( ($5 H1
GH $1( $$$ &1 55G 7$H H1
H1 ,7 ,( $1 5$ 5H7 H1
H$ HG ($ &1 G1 ,$ H1
H& $$5 $(& $1 G71 H,( H1
H( &G HH $1 ,$ H( H1
H $(7 ( &1 ,&& ,G$ H1
H, &H &G $1 G5, H&, H1
H5 H $1, $1 5,5 7&G H1
H7 H& $17 (1 5, ,(& H1
HG $1 $( $1 5&H 577 H1
HH ( 7H $1 G$ H1& H1
$11 H 7& &1 57 71( H1
$1$ (, 75 $1 (, H$ H1
$1& 75 HH &1 &7( ((1 H1
$1( $&7 &G (1 $&G $H, H1
$1 G $$H &1 $& $H7 H1
$1, $1& $$1 $1 7, G$ H1
$15 & & $1 ,G( 57 H1
$17 5 5$ &1 $1$$ $177 H1
$1G && (G &1 H,1 $11( H1
$1H GG 7& (1 H&, H7H H1
$$1 $17 5( (1 7G G11 H1
$$$ (( $($ (1 H7H $1(7 H1
$$& $&( $$& &1 1, ,H H1
$$( 57 ,, $1 $, 5& H1
$$ (H $&G $1 5, $(( H1
$$, $(5 1 (1 &5 H$ H1
$$5 H( (5 &1 HG $55 H1
$$7 7 $$, $1 G1$ G7& H1
$$G &$ &1 $$, $G1 H1
$$H &G 7 &1 $GG &51 H1
$&1 G5 $1& $1 H( $,H H1
$&$ $H (7 &1 55, 7(G H1
$&& G (5 $1 ,5G 5&5 H1
$&( $$G $1G $1 H7& $11G H1
$& $(& & $1 ($$ (G H1
$&, 5 &, $1 ,$5 ,7 H1
$&5 &G 7G $1 7H ,&H H1
$&7 & $(& $1 ,&5 ,G H1
$&G && && $1 H ,,1 H1
$&H GG (, &1 &G, (,G H1
$(1 H $1 $1 7, G$( H1
$($ 7 H5 (1 G1 $,( H1
$(& 1 $$7 (1 ,$, ,G, H1
$(( HH H5 $1 (G H( H1
$( $$H $$ &1 $&G $G, H1
$(, &G $17 &1 &15 &7, H1
$(5 $(( $& $1 7H7 G7 H1
$(7 (, $1& $1 G77 H(G H1
$(G (5 $1, &1 775 G,& H1
$(H G (H &1 5,H 7&1 H1
$1 ( $(G &1 71 7H H1
$$ $(5 &H $1 15 G& H1
$& (( $(( &1 GH1 H$ H1
$( $5 (H &1 ($ (,G H1
$ (& 77 &1 $11 $,7 H1
$, $&& $($ &1 51G 55$ H1
$5 77 $11 &1 (,5 &H H1
$7 H5 &5 $1 &1& &5& H1
$G $&7 &H &1 71 $$ H1
$H H$ H, &1 $1(5 $1H7 H1
$,1 & $(5 $1 ($1 (G$ H1
$,$ (5 $(& $1 ,1H ,7$ H1
$,& $1( $15 (1 &H5 (7 H1
$,( $17 $1, $1 7& ,(H H1
$, H5 7 $1 (,$ (7 H1
$,, 5( ,7 $1 7H $1 H1
$,5 $&& $$( &1 (17 (7 H1
$,7 $ $($ $1 7$& 7G H1
$,G (1 ($ &1 H51 $1$5 H1
$,H 1 $(& &1 $(7 $G& H1
$51 HH &5 1 &G7 (5( H1
$5$ $1 $$G $1 75 $ H1
$5& $1& 51 (1 G1 H11 H1
$5( $&1 $(5 $1 $7 G& H1
$5 H7 &H (1 H $$H H1
$5, $H (H $1 75 G&( H1
$55 &7 77 $1 &G (H H1
$57 (1 & &1 57( 7G H1
$5G $&5 $$& $1 ,17 ,( H1
$5H $1, $$1 &1 ,57 5( H1
$71 $$G $$ $1 5, $&5 H1
$7$ $(, 5 (1 $,( &$1 H1
$7& $$$ 55 &1 ,5$ 5$7 H1
$7( H, 77 $1 &7G (&, H1
$7 $(H $1 (1 7&( 7,5 H1
$7, 5$ , $1 &5& (&H H1
$75 $5 (G &1 (G7 55 H1
$77 $5 $(G $1 G5 $& H1
$7G H$ 7G $1 GG $$ H1
$7H H$ 7$ &1 G&H GH1 H1
$G1 1 &7 (1 &$ &H& H1
$G$ 5 $$( &1 7&1 75G H1
$G& $$H $(H &1 &( &HG H1
$G( (, HH &1 H75 $1&, H1
$G ($ $$& &1 1$ G$ H1
$G, , $$H &1 &, G H1
$G5 ( $(, &1 (( H$ H1
$G7 (1 $(5 $1 7H& G,1 H1
$GG &1 (H $1 G,, H$( H1
$GH $($ $$ $1 GH, H51 H1
$H1 &G (( &1 ,, $&G H1
$H$ &( $1 $ H$ H1
$H& G7 $1G &1 &7, (,1 H1
$H( &$ &5 &1 ($$ (5, H1
$H $&$ $( &1 ,$7 ,57 H1
$H, $$H $(1 &1 5H& 75, H1
$H5 H$ $1G $1 (G (1 H1
$H7 $(1 &7 (1 &&H &G1 H1
$HG $1$ $17 $1 $1(G $1H1 H1
$HH ( $1G $1 ,15 ,55 H1
&11 $($ ($ &1 51 5,7 H1]!
% radom-elect.m
function [serialInon,ctAll]=random-elect(M,),demand,dis8rg,ctPt,"t&,dis&,ca"acit*)
% random-elect is to choose an original route
% denotions of all in"uts are consistent +ith ones in m0file of gra"h@uild
%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,
%, randoml* generate routes
%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,
flagJm"=ones($,M)!
% Det a discrete serial
dd=rand"erm(M)!
flagRand=$!
is-uc=$!
serialInon=;<!
ca"Ve=/eros($,))!
ctAll=1! % "ut do+n customers arranged
for i=$#)
"t=dd(flagRand)! % -elect the first customer of a route
flagRand=flagRand3$!
+hile(KflagJm"("t)BflagRandC=M)
"t=dd(flagRand)!
flagRand=flagRand3$!
end
if(flagRandAM) % all customers has 'een arranged +hile some %ehicles are em"t*
'reaE!%%L===========================L
end
timeCur=dis8rg("t)! % initiali/ation of each %ehicle
demandCur=demand("t)!
count=$! % count the %ehicles used
serialInon;i<(count)="t!
ca"Ve(i)=demand("t)!
flagJm"("t)=1!
ctAll=ctAll3$!
%0000000 0000000 000000000 00000000 00000000 00000000 0000000 00000000
?udgeMlag=$!
+hile(?udgeMlag)
+hile(ctPt("t)A1) % find an unused location NneFtN
%indeF=ceil(rand($)=ctPt("t))!
[dsd,indeF]=min(dis&("t,"t&;"t<))!
if(KflagJm"("t&;"t<(indeF)))
"t&;"t<(indeF)="t&;"t<(ctPt("t))!
ctPt("t)=ctPt("t)0$!
continue!
else
'reaE!
end
end
is2eFt=ctPt("t)!
if(is2eFt) % %erif* the current "oint NneFtN
neFt="t&;"t<(indeF)!
timeCur=timeCur3dis&("t,neFt)!
demandCur=demandCur3demand(neFt)!
inMlag=demandCurC=ca"acit*!
if(inMlag)
count=count3$!
ca"Ve(i)=demandCur!
serialInon;i<(count)=neFt!
flagJm"(neFt)=1!
ctAll=ctAll3$!
"t=neFt!
else
'reaE!
end
else
'reaE!
end
end
end
% cross8%er.m
function serialInon=cross8%er("arent$,"arent&,dis&,M,),ca"acit*,demand)
% record all the routes used in the t+o chromosomes
in>ndeF=/eros(&,M)!
out>ndeF=/eros(&,M)!
len=length("arent$)!
for i=$#len
su'4en=length("arent$;i<)0$!
for ?=$#su'4en
in>ndeF($,"arent$;i<(?3$))="arent$;i<(?)!
out>ndeF($,"arent$;i<(?))="arent$;i<(?3$)!
end
end
len=length("arent&)!
for i=$#len
su'4en=length("arent&;i<)0$!
for ?=$#su'4en
in>ndeF(&,"arent&;i<(?3$))="arent&;i<(?)!
out>ndeF(&,"arent&;i<(?))="arent&;i<(?3$)!
end
end
in-um=sum(in>ndeFA1)!
out-um=sum(out>ndeFA1)!
for i=$#M
if(out-um(i)==&) % location is used for "t +ith no ending connectin to st
if(dis&(i,out>ndeF($,i))Adis&(i,out>ndeF(&,i)))
out>ndeF($,i)=1!
else
out>ndeF(&,i)=1!
end
end
if(in-um(i)==&) % location is used for "t +ith no start connectin to st
if(dis&(in>ndeF($,i),i)Adis&(in>ndeF(&,i),i))
in>ndeF($,i)=1!
else
in>ndeF(&,i)=1!
end
end
end
% firstl* find sth from one0side "oint, and find routes +ith such "t as one
% end
flagOsed=/eros($,M)!
ctAll=1!
ctVe=1!
outMlag=sum(out>ndeFA1)!
inMlag=sum(in>ndeFA1)!
out>ndeF=sum(out>ndeF)!
in>ndeF=sum(in>ndeF)!
serialInon=;<!
for i=$#M
if(inMlag(i)==1BKflagOsed(i)BoutMlag(i))
ctVe=ctVe3$!
count=$!
serialInon;ctVe<(count)=i!
ca"Ve(ctVe)=demand(i)!
"t=i!
flagOsed(i)=$!
ctAll=ctAll3$!
+hile(outMlag("t)BKflagOsed(out>ndeF("t)))
ca"=ca"Ve(ctVe)3demand(out>ndeF("t))!
if(ca"Aca"acit*)
'reaE!
end
ca"Ve(ctVe)=ca"!
ctAll=ctAll3$!
count=count3$!
serialInon;ctVe<(count)=out>ndeF("t)!
"t=out>ndeF("t)!
flagOsed("t)=$!
end
elseif(outMlag(i)==1BKflagOsed(i)BinMlag(i))
ctVe=ctVe3$!
count=$!
serialInon;ctVe<(count)=i!
ca"Ve(ctVe)=demand(i)!
"t=i!
flagOsed(i)=$!
ctAll=ctAll3$!
+hile(inMlag("t)BKflagOsed(in>ndeF("t)))
ca"=ca"Ve(ctVe)3demand(in>ndeF("t))!
if(ca"Aca"acit*)
'reaE!
end
ca"Ve(ctVe)=ca"!
ctAll=ctAll3$!
count=count3$!
serialInon;ctVe<(count)=in>ndeF("t)!
"t=in>ndeF("t)!
flagOsed("t)=$!
end
trans=serialInon;ctVe<!
for i=$#count
serialInon;ctVe<(i)=trans(count3$0i)!
end
end
end
% secondl* tr* to trsem'le all the "oints as long as "ossi'le
% +e can cut it to "ieces if it diso'e*s the ca"acit* constraint
for i=$#M
if(flagOsed(i))
continue!
end
"re=[]!
after=i!
"tPre=i!
"tAfter=i!
+hile(inMlag("tPre)BKflagOsed(in>ndeF("tPre)))
"re=["re,in>ndeF("tPre)]!
"tPre=in>ndeF("tPre)!
end
+hile(outMlag("tPre)BKflagOsed(out>ndeF("tAfter)))
after=[after,out>ndeF("tAfter)]!
"tAfter=out>ndeF("tAfter)!
end
len$=length("re)!
len&=length(after)!

ctVe=ctVe3$!
count=$!
ca"Ve(ctVe)=1!
for ?=len$#0$#$
ca"=ca"Ve(ctVe)3demand("re(?))!
if(ca"Aca"acit*)
'reaE!
end
serialInon;ctVe<(count)="re(?)!
ca"Ve(ctVe)=ca"!
ctAll=ctAll3$!
count=count3$!
flagOsed("re(?))=$!
end

for ?=$#len&
ca"=ca"Ve(ctVe)3demand(after(?))!
if(ca"Aca"acit*)
'reaE!
end
serialInon;ctVe<(count)=after(?)!
ca"Ve(ctVe)=ca"!
ctAll=ctAll3$!
count=count3$!
flagOsed(after(?))=$!
end
end
% fitness.m
function fit=fitness(serialInon,distance,dis8rg)
% all the constraints including time and demand ha%e 'een eFamned
% As a result, here +e onl* figure the total distances tra%eled '* all
% %ehicles, +hich is the %alue of our o'?ecti%e function
len=length(serialInon)!
fit=1!
for i=$#len
fit=fit3dis8rg(serialInon;i<($))3dis8rg(serialInon;i<(end))!
su'4en=length(serialInon;i<)0$!
for ?=$#su'4en
fit=fit3distance(serialInon;i<(?),serialInon;i<(?3$))!
end
end

You might also like