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!
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