Professional Documents
Culture Documents
auuLuge( g_p, ^, B, 1 ),
auuLuge( g_p, ^, C, 1 ),
auuLuge( g_p, B, D, 1 ),
auuLuge( g_p, C, L, 1 ),
auuLuge( g_p, C, l, 1 ),
auuLuge( g_p, D, G, 1 ),
auuLuge( g_p, D, H, 1 ),
return u,
]
voiu ufs( graph_t `g_p, int root, int goal )
int noue,
int to,
stack_t `s_p,
s_p createStack( 1u ),
pushStack( s_p, root ),
while ( !isLmptyStack(s_p) )
noue popStack( s_p ),
printf(un, noue),
if (noue goal) Ereak,
for (to g_p-noues-1 , to u , to--)
if (getLdge( g_p, noue, to ) )
pusbStack( s_p, to ),
]
]
]
destroyStack( s_p ),
return,
]
int main()
graph_t `g_p,
34 Art|c|a| lnte|||gence
g_p createGrapb( 8 ),
init_graph( g_p ),
ufs( g_p, u, 5 ),
destroyGrapb( g_p ),
return u,
]
Un|njormed 5earch 35
addLdge( g_p, ^, B, 1 ),
addLdge( g_p, ^, C, 1 ),
addLdge( g_p, B, D, 1 ),
addLdge( g_p, C, L, 1 ),
addLdge( g_p, C, l, 1 ),
addLdge( g_p, D, G, 1 ),
addLdge( g_p, D, H, 1 ),
return u,
]
voiu uls( graph_t `g_p, int root, int goal, int limit )
graph_t `g_p,
g_p createGrapb( 8 ),
init_graph( g_p ),
36 Art|c|a| lnte|||gence
uls( g_p, u, 5, 2 ),
destroyGrapb( g_p ),
return u,
]
Whilc thc algorithm oocs rcmovc thc possilility ol inlnitcly looping in thc
graph, it also rcouccs thc scopc ol thc scarch. l thc goal nooc hao lccn onc
ol thc noocs markco X`, it voulo not havc lccn louno, making thc scarch
algorithm incomplctc. Thc algorithm can lc complctc il thc scarch ocpth is
that ol thc trcc itscll (in this casc d is thrcc;. Thc tcchniquc is also not optimal
sincc thc lrst path may lc louno to thc goal instcao ol thc shortcst path.
Thc timc ano spacc complcxity ol ocpth-limitco scarch is similar to DFS,
lrom vhich this algorithm is ocrivco. Spacc complcxity is O(lo; ano timc
complcxity is O(l
o
;, lut d in this casc is thc imposco ocpth ol thc scarch ano
not thc maximum ocpth ol thc graph.
iterat|ve Deepen|ng Searcb (iDS)
tcrativc Dccpcning Scarch (DS; is a ocrivativc ol DS ano comlincs thc
lcaturcs ol ocpth-lrst scarch vith that ol lrcaoth-lrst scarch. DS opcratcs
ly pcrlorming DS scarchcs vith incrcasco ocpths until thc goal is louno.
llGURL 2.13: iterat|ng |ncreased deptb searcbes w|tb iDS.
llGURL 2.12: Searcb order for a tree us|ng deptb-||m|ted searcb (deptb - two).
Un|njormed 5earch 37
Thc ocpth lcgins at onc, ano incrcascs until thc goal is louno, or no lurthcr
noocs can lc cnumcratco (scc Figurc 2.13;.
As shovn in Figurc 2.13, DS comlincs ocpth-lrst scarch vith lrcaoth-
lrst scarch. By minimizing thc ocpth ol thc scarch, vc lorcc thc algorithm to
also scarch thc lrcaoth ol thc graph. l thc goal is not louno, thc ocpth that
thc algorithm is pcrmittco to scarch is incrcasco ano thc algorithm is startco
again. Thc algorithm, shovn in isting 2.4, lcgins vith a ocpth ol onc.
LiS!iNG 2.4: !be |terat|ve deepen|ng-searcb a|gor|tbm.
=incluue stuio.h
=incluue graph.h
=incluue stack.h
=uefne ^ u
=uefne B 1
=uefne C 2
=uefne D 3
=uefne L 4
=uefne l 5
=uefne G 6
=uefne H 7
int init_graph( graph_t `g_p )
addLdge( g_p, ^, B, 1 ),
addLdge( g_p, ^, C, 1 ),
addLdge( g_p, B, D, 1 ),
addLdge( g_p, C, L, 1 ),
addLdge( g_p, C, l, 1 ),
addLdge( g_p, D, G, 1 ),
addLdge( g_p, D, H, 1 ),
return u,
]
int uls( graph_t `g_p, int root, int goal, int limit )
graph_t `g_p,
int status, uepth,
g_p createGrapb( 8 ),
init_graph( g_p ),
uepth 1,
while (1)
status uls( g_p, u, 5, uepth ),
if (status 1) Ereak,
else uepth++,
]
destroyGrapb( g_p ),
return u,
]
addLdge( g_p, ^, B, 1 ),
addLdge( g_p, ^, C, 1 ),
addLdge( g_p, B, D, 1 ),
addLdge( g_p, C, L, 1 ),
addLdge( g_p, C, l, 1 ),
addLdge( g_p, D, G, 1 ),
addLdge( g_p, D, H, 1 ),
return u,
]
voiu Efs( graph_t `g_p, int root, int goal )
int noue,
int to,
gueue_t `g_p,
g_p createQueue( 1u ),
enQueue( g_p, root ),
while ( !|sLmptyQueue(g_p) )
noue deQueue( g_p ),
printf(un, noue),
if (noue goal) Ereak,
for (to g_p-noues-1 , to u , to--)
if (getLdge( g_p, noue, to ) )
enQueue( g_p, to ),
]
]
]
destroyQueue( g_p ),
return,
]
int main()
Un|njormed 5earch 41
graph_t `g_p,
g_p createGrapb( 8 ),
init_graph( g_p ),
Efs( g_p, u, 7 ),
destroyGrapb( g_p ),
return u,
]
addLdge( g_p, ^, B, 5 ),
addLdge( g_p, ^, C, 1 ),
addLdge( g_p, ^, D, 2 ),
addLdge( g_p, B, L, 1 ),
addLdge( g_p, C, L, 7 ),
addLdge( g_p, D, L, 5 ),
return u,
]
voiu ucs( graph_t `g_p, int root, int goal )
graph_t `g_p,
g_p createGrapb( 6 ),
init_graph( g_p ),
ucs( g_p, ^, L ),
destroyGrapb( g_p ),
return u,
]
llGURL 2.17: Node eva|uat|ons and tbe
state of tbe pr|or|ty queue.
llGURL 2.18: i||ustrat|ng tbe patb cost
tbrougb tbe grapb.
Un|njormed 5earch 45
noue_t `noue_p,
int cost,
}` Lnumerate the Open list `}
while ( !|sLmptyQueue (open_pg_p) )
deQueue ( open_pg_p, (int `)8noue_p, 8cost ),
}` Solution founu `}
if (noue_p-g u)
printf(lounu Solution (uepth u):n, noue_p-h),
em|tBoard ( noue_p ),
Ereak,
]
generateCb||dNodes( open_pg_p, closeu_g_p, noue_p ),
lnjormed 5earch 53
]
return,
]
Notc in isting 3.1 that vhilc cost is thc j,n), vc chcck g,n) to octcrminc
vhcthcr a solution is louno. This is lccausc j,n) may lc non-zcro sincc it
incluocs thc ocpth ol thc solution (/,n);.
int i,
unsigneu short cEoaru1, cEoaru2,
const int moves|16| -1, 2, 2, 1,
-1, 2, 2, 1,
-1, 2, 2, 1,
-1, 2, 2, 1 ],
}` Generate the chilu noues for the current noue Ey
` shuffing the pieces on the Eoaru.
54 Art|c|a| lnte|||gence
`}
for (i u , i 16 , i++)
}` ls there a gueen at this position `}
if (cbeck|ece( noue_p-Eoaru, i ))
}` Remove current gueen from the Eoaru `}
cEoaru1 cEoaru2 ( noue_p-Eoaru 8 -(1 (15-i) ) ),
if (moves|i| -1)
}` Can only move right `}
cEoaru1 | ( 1 (15-(i+1)) ),
if (!searcbQueue( closeu_g_p, cEoaru1))
(voiu)createNode( pg_p, closeu_g_p, cEoaru1, noue_p-h+1 ),
]
] else if (moves|i| 2)
}` Can move left or right `}
cEoaru1 | ( 1 (15-(i+1)) ),
if (!searcbQueue( closeu_g_p, cEoaru1))
(voiu)createNode( pg_p, closeu_g_p, cEoaru1, noue_p-h+1 ),
]
cEoaru2 | ( 1 (15-(i-1)) ),
if (!searcbQueue( closeu_g_p, cEoaru2))
(voiu)createNode( pg_p, closeu_g_p, cEoaru2, noue_p-h+1 ),
]
] else if (moves|i| 1)
}` Can only move left `}
cEoaru2 | ( 1 (15-(i-1)) ),
if (!searcbQueue( closeu_g_p, cEoaru2))
(voiu)createNode( pg_p, closeu_g_p, cEoaru2, noue_p-h+1 ),
]
]
]
]
return,
]
ct`s nov vatch thc algorithm in action. Oncc invokco, a ranoom root nooc
is cnqucuco ano thcn thc possillc chilo conlgurations arc cnumcratco ano
loaoco onto thc OFFN list (scc isting 3.3;. Thc ocmonstration hcrc shovs
a shallov trcc ol thrcc conlgurations chcckco, thc root nooc, onc at lcvcl
onc, ano thc solution louno at ocpth tvo. A conocnsco vcrsion ol this run
is shovn in Figurc 3.3.
lnjormed 5earch 55
LiS!iNG 3.3: Best-i|rst Searcb for tbe N-Queens prob|em (N-4).
New noue: evaluateBoaru 4824 (h u, g 3)
lnitial Boaru:
Eoaru is ux4824
u 1 u u
1 u u u
llGURL 3.3: Grapb|ca| (condensed) v|ew of tbe searcb tree |n L|st|ng 3.3.
56 Art|c|a| lnte|||gence
u u 1 u
u 1 u u
Checking Eoaru ux4824 (h u g 3)
New noue: evaluateBoaru 2824 (h 1, g 2)
New noue: evaluateBoaru 8824 (h 1, g 3)
New noue: evaluateBoaru 4424 (h 1, g 4)
New noue: evaluateBoaru 4814 (h 1, g 3)
New noue: evaluateBoaru 4844 (h 1, g 4)
New noue: evaluateBoaru 4822 (h 1, g 3)
New noue: evaluateBoaru 4828 (h 1, g 2)
Checking Eoaru ux2824 (h 1 g 2)
New noue: evaluateBoaru 1824 (h 2, g 1)
New noue: evaluateBoaru 2424 (h 2, g 5)
New noue: evaluateBoaru 2814 (h 2, g u)
New noue: evaluateBoaru 2844 (h 2, g 2)
New noue: evaluateBoaru 2822 (h 2, g 3)
New noue: evaluateBoaru 2828 (h 2, g 2)
Checking Eoaru ux2814 (h 2 g u)
lounu Solution (h 2 g u):
Eoaru is ux2814
u u 1 u
1 u u u
u u u 1
u 1 u u
Var|ants of Best-i|rst Searcb
Onc intcrcsting variant ol lcst-lrst scarch is callco grdq |s/-rs/ scr/.
n this variant, j,n) = /,n), ano thc OFFN list is orocrco in j orocr. Sincc
/ is thc only lactor usco to octcrminc vhich nooc to sclcct ncxt (iocntilco
as thc closcncss to thc goal;, it`s oclnco as grccoy. Bccausc ol this, grccoy
lcst-lrst is not complctc as thc hcuristic is not cdm|ss||| (lccausc it can
ovcrcstimatc thc path to thc goal;. Wc`ll oiscuss aomissilility in morc octail
in thc oiscussion ol A-star scarch.
Anothcr variant ol lcst-lrst scarch is |cm-scr/, likc grccoy lcst-lrst
scarch, it uscs thc hcuristic j,n) = /,n). Thc oillcrcncc vith lcam-scarch is
that it kccps only a sct ol thc lcst canoioatc noocs lor cxpansion ano simply
throvs thc rcst vay. This makcs lcam-scarch much morc mcmory cllcicnt
than grccoy lcst-lrst scarch, lut sullcrs in that noocs can lc oiscaroco
vhich coulo rcsult in thc optimal path. For this rcason, lcam-scarch is
ncithcr optimal or complctc.
lnjormed 5earch 57
A 5EARCH
A scarch, likc lcst-lrst scarch, cvaluatcs a scarch spacc using a hcuristic
lunction. But A uscs loth thc cost ol gctting lrom thc initial statc to thc
currcnt statc (g,n);, as vcll as an cstimatco cost (hcuristic; ol thc path lrom
thc currcnt nooc to thc goal (/,n);. Thcsc arc summco to thc cost lunction j,n)
(Scc Fq 3.1;. Thc A scarch, unlikc lcst-lrst, is loth optimal ano complctc.
Thc OFFN ano COSFD lists arc usco again to iocntily thc lronticr lor
scarch (OFFN list; ano thc noocs cvaluatco thus lar (COSFD;. Thc OFFN
list is implcmcntco as a priority qucuc orocrco in lovcst j,n) orocr. What
makcs A intcrcsting is that it continually rc-cvaluatcs thc cost lunction lor
noocs as it rc-cncountcrs thcm. This allovs A to cllcicntly lno thc minimal
path lrom thc initial statc to thc goal statc.
ct`s nov look at A at a high lcvcl ano thcn vc`ll oig lurthcr ano apply
it to a vcll-knovn prollcm. isting 3.4 proviocs thc high lcvcl nov lor A.
LiS!iNG 3.4: H|gb-|eve| ow for tbe A searcb a|gor|tbm.
lnitialize OlLN list (priority gueue)
lnitialize CLOSLD list
llace start noue on the OlLN list
Loop while the OlLN list is not empty
Get Eest noue (parent) from OlLN list (least j (n))
if parent is the goal noue, uone
llace parent on the CLOSLD list
Lxpanu parent to all aujacent noues (auj_noue)
if auj_noue is on the CLOSLD list
uiscaru auj_noue anu continue
else if auj_noue is on the OlLN list
if auj_noue`s g value is Eetter than
the OlLN.auj_noue`s g value
uiscaru OlLN.cur_noue
calculate auj_noue`s g, h anu f values
set auj_noue preuecessor to parent
auu auj_noue to OlLN list
continue
enu
else
calculate auj_noue`s g, h anu f values
set auj_noue preuecessor to parent
58 Art|c|a| lnte|||gence
auu auj_noue to OlLN list
enu
enu
enu loop
Notc in thc nov lrom isting 3.4 that oncc vc lno thc lcst nooc lrom thc
OFFN list, vc cxpano all ol thc chilo noocs (lcgal statcs possillc lrom thc
lcst nooc;. l thc ncv lcgal statcs arc not louno on cithcr thc OFFN or
COSFD lists, thcy arc aooco as ncv noocs (sctting thc prcocccssor to thc
lcst nooc, or parcnt;. l thc ncv nooc is on thc COSFD list, vc oiscaro
it ano continuc. Finally, il thc ncv nooc is on thc OFFN list, lut thc ncv
nooc has a lcttcr g valuc, vc oiscaro thc nooc on thc OFFN list ano aoo
thc ncv nooc to thc OFFN list (othcrvisc, thc ncv nooc is oiscaroco, il its g
valuc is vorsc;. By rc-cvaluating thc noocs on thc OFFN list, ano rcplacing
thcm vhcn cost lunctions pcrmit, vc allov lcttcr paths to cmcrgc lrom thc
statc spacc.
As vc`vc oclnco alrcaoy, A is complctc, as long as thc mcmory supports
thc ocpth ano lranching lactor ol thc trcc. A is also optimal, lut this
charactcristic ocpcnos on thc usc ol an cdm|ss||| hcuristic. Bccausc A
must kccp track ol thc noocs cvaluatco so lar (ano also thc oiscovcrco noocs
to lc cvaluatco;, thc timc ano spacc complcxity arc loth O(l
o
;.
int i,
const int test|M^_BO^RD-1|1, 2, 3, 4, 5, 6, 7, 8 ],
int scoreu,
for (i u , i M^_BO^RD-1 , i++)
score + (Eoaru_p-array|i| ! test|i|),
]
return (uouEle)score,
]
62 Art|c|a| lnte|||gence
Thc astar lunction is shovn in isting 3.7. Frior to calling this lunction, vc`vc
sclcctco a ranoom loaro conlguration ano placco it onto thc OFFN list. Wc
thcn vork through thc OFFN list, rctricving thc lcst nooc (vith thc lcast l
valuc using gctistBcst; ano immcoiatcly placc it on thc COSFD list. Wc
chcck to scc il this nooc is thc solution, ano il so, vc cmit thc path lrom
thc initial nooc to thc goal (vhich illustratcs thc movcs that vcrc maoc;. To
minimizc scarching too occply in thc trcc, vc halt cnumcrating noocs past
a givcn ocpth (vc scarch thcm no lurthcr;.
Thc ncxt stcp is to cnumcratc thc possillc movcs lrom this statc, vhich
vill lc a maximum ol lour. Thc gctChiloBoaro lunction is usco to rcturn
an aojaccnt nooc (using thc inocx passco in to octcrminc vhich possillc
movc to makc;. l a movc isn`t possillc, thcn a NU is rcturnco ano it`s
ignorco.
With a ncv chilo nooc, vc lrst chcck to scc il it`s alrcaoy lccn cvaluatco
(il it`s on thc COSFD list;. l it is, thcn vc`rc to ocstroy this nooc ano
continuc (to gct thc chilo nooc lor thc currcnt loaro conlguration;. l
vc`vc not sccn this particular loaro conlguration lclorc, vc calculatc thc
hcuristics lor thc nooc. First, vc initializc thc nooc`s ocpth in thc trcc as thc
parcnt`s ocpth plus onc. Ncxt, vc call cvaluatcBoaro to gct thc /||s-a/-j-
p|c mctric, vhich vill act as our h valuc (cost lrom thc root nooc to this
nooc;. Thc g valuc is sct to thc currcnt ocpth, ano thc l valuc is initializco
vith Fq 3.1.
(Fq 3.1;
Wc incluoc an c|p/c ano |/c paramctcr hcrc to givc oillcrcnt vcights to
thc g ano / valucs. n this implcmcntation, c|p/c is 1.0 ano |/c is 2.0. This
mcans that morc vcight is givcn to thc h valuc, ano sulscqucntly thc closcr a
nooc is to thc goal is vcighco highcr than its ocpth in thc statc spacc trcc.
With thc j valuc calculatco, vc chcck to scc il thc nooc is on thc OFFN
list. l it is, vc comparc thcir j valucs. l thc nooc on thc OFFN list has a
vorsc j valuc, thc nooc on thc OFFN list is oiscaroco ano thc ncv chilo
nooc takcs its placc (sctting thc prcocccssor link to thc parcnt, so vc knov
hov vc got to this nooc;. l thc nooc on thc OFFN list has a lcttcr j valuc,
thcn thc nooc on thc OFFN list rcmains on thc opcn list ano thc ncv chilo
is oiscaroco.
Finally, il thc ncv chilo nooc cxists on ncithcr thc COSFD or OFFN
list, it`s a ncv nooc that vc`vc yct to scc. t`s simply aooco to thc OFFN list,
ano thc proccss continucs.
This algorithm continucs until cithcr onc ol tvo cvcnts occur. l thc
OFFN list lccomcs cmpty, thcn no solution vas louno ano thc algorithm
lnjormed 5earch 63
cxits. l thc solution is louno, thcn shovSolution is callco, ano thc noocs
linkco togcthcr via thc prcocccssor links arc cnumcratco to shov thc solution
lrom thc initial nooc to thc goal nooc.
LiS!iNG 3.7: !be A a|gor|tbm.
voiu astar( voiu )
cur_solution ranuom()
computeL( cur_solution )
while (lemperature u)
auj_solution perturE_solution( cur_solution )
computeL( auj_solution )
ueltaL auj_solution.energy cur_solution.energy
}` ls new solution Eetter, then take it `}
if (ueltaL u)
cur_solution auj_solution
else
p exp( -ueltaL } lemperature )
}` Ranuomly accept worse solution `}
if ( p R^NDOM(u..1) )
cur_solution auj_solution
enu
enu
reuuce lemperature
enu
enu simulateu_annealing
l our ncv solution vas not lcttcr than thc olo, thcn vc acccpt it vith a
prolalility proportional to thc currcnt tcmpcraturc ano thc oclta cncrgy.
Thc lovcr thc tcmpcraturc, thc lcss likcly vc`ll acccpt a vorsc solution. But
thc lcttcr thc oclta cncrgy, thc morc likcly vc`ll acccpt it. This prolalility
is calculatco as shovn in Fq 3.2.
(Fq 3.2;
Sincc our tcmpcraturc occrcascs ovcr timc, it`s lcss likcly that a vorsc
solution vill lc acccptco. Farly on vhcn thc tcmpcraturc is high, vorsc
solutions can lc acccptco alloving thc scarch to movc avay lrom local
maximum in scarch ol thc glolal maximum. As thc tcmpcraturc occrcascs,
it lccomcs morc oillcult to acccpt a vorsc solution, vhich mcans that thc
algorithm scttlcs on a solution ano simply lnc-tuncs it (il possillc;.
68 Art|c|a| lnte|||gence
Thc classical simulatco anncaling algorithm also incluocs mn/ cr|
cyclcs vhcrc a numlcr ol trials arc pcrlormco lclorc occrcasing thc
tcmpcraturc.
!be !rave||ng Sa|esman rob|em (!S)
To ocmonstratc thc simulatco anncaling algorithm, vc`ll usc thc classic
Travcling Salcsman Frollcm (or TSF;. n thc TSF, vc`rc givcn a sct ol citics
ano a rclativc cost lor travcling lctvccn cach city to cach othcr. Thc goal
is to lno a path through all citics vhcrc vc visit all citics oncc, ano lno thc
shortcst ovcrall tour. Wc`ll start at onc city, visit cach othcr city, ano thcn
cno at thc initial city.
Consiocr thc graph shovn in Figurc 3.S. Many citics arc conncctco to
onc anothcr, lut an optimal path cxists that tours cach city only oncc.
Thc TSF is loth intcrcsting ano important lccausc it has practical
implications. Consiocr transportation prollcms vhcrc oclivcrics arc rcquirco
ano lucl ano timc arc to lc minimizco. Anothcr intcrcsting application is
that ol orilling holcs in a circuit loaro. A numlcr ol holcs must lc orillco
quickly on a singlc loaro, ano in orocr to oo this, an optimal path is nccoco
to minimizc thc movcmcnt ol thc orill (vhich vill lc slov;. Solutions to thc
TSF can thcrclorc lc vcry usclul.
!S !our Representat|on
To rcprcscnt a sct ol citics ano thc tour lctvccn thcm, vc`ll usc an implicit
cdcnq ||s/. Fach city vill lc containco in thc list, ano citics that arc ncxt
to onc anothcr arc implico as conncctco in thc tour. Bccall our samplc TSF
in Figurc 3.S vhcrc scvcn citics makc up thc vorlo. This vill lc rcprcscntco
as shovn in Figurc 3.0.
llGURL 3.8: A Samp|e !S tour tbrougb a sma|| grapb.
lnjormed 5earch 69
llGURL 3.9: Adjacency ||st for tbe !S tour sbown |n i|gure 3.8.
llGURL 3.10: Demonstrat|on of row swapp|ng to perturb tbe tour.
70 Art|c|a| lnte|||gence
Notc that thc list shovn in Figurc 3.0 is a singlc list in tour orocr. Whcn
vc rcach thc cno ol thc list, vc vrap to thc lrst clcmcnt, complcting thc
tour. To pcrturl thc tour vc takc tvo ranoom rovs lrom thc list ano svap
thcm. This is ocmonstratco in Figurc 3.10. Notc hov ly simply svapping
tvo clcmcnts, thc tour is grcatly pcrturlco ano rcsults in a vorsc tour
lcngth.
S|mu|ated Annea||ng imp|ementat|on
Thc implcmcntation ol simulatco anncaling is actually quitc simplc in thc
C languagc. Wc`ll rcvicv thrcc ol thc lunctions that makc up thc simulatco
anncaling implcmcntation, thc main simulatco anncaling algorithm,
pcrturling a tour, ano computing thc lcngth ol thc tour. Thc rcmaining
lunctions arc availallc on thc CD-BOM.
LiS!iNG 3.10: Structures for tbe !S so|ut|on.
typeuef struct
int x, y,
] city_t,
typeuef struct
city_t cities|M^_ClllLS|,
uouEle tour_length,
] solution_t,
Thc Fucliocan oistancc ol thc tour is calculatco vith computc_tour. This
lunction valks through thc tour, accumulating thc scgmcnts lctvccn cach
city (scc isting 3.11;. t cnos ly vrapping arouno thc list, ano aooing in thc
oistancc lrom thc last city lack to thc lrst.
LiS!iNG 3.11: Ca|cu|at|ng tbe Luc||dean tour w|tb compute_tour.
voiu compute_tour( solution_t `sol )
int i,
uouEle tour_length (uouEle)u.u,
for (i u , i M^_ClllLS-1 , i++)
tour_length +
euc||dean_d|stance(
sol-cities|i|.x, sol-cities|i|.y,
sol-cities|i+1|.x, sol-cities|i+1|.y ),
lnjormed 5earch 71
]
tour_length +
euc||dean_d|stance(
sol-cities|M^_ClllLS-1|.x,
sol-cities|M^_ClllLS-1|.y,
sol-cities|u|.x, sol-cities|u|.y ),
sol-tour_length tour_length,
return,
]
Civcn a solution, vc can crcatc an aojaccnt solution using thc lunction
pcrturl_tour. n this lunction, vc ranoomly sclcct tvo citics in thc tour, ano
svap thcm. A loop cxists to cnsurc that vc`vc sclcctco tvo uniquc ranoom
points (so that vc oon`t svap a singlc city vith itscll;. Oncc sclcctco, thc x
ano y cooroinatcs arc svappco ano thc lunction is complctc.
LiS!iNG 3.12: erturb|ng tbe tour by creat|ng an adjacent so|ut|on.
voiu perturb_tour( solution_t `sol )
cur_solution ranuom()
evaluate_position( cur_solution )
Eest cur_solution
taEu( cur_solution )
while (!termination_critera)
}` Get the Eest neighEor, not on the taEu list `}
cur_solution Eest_non_taEu_neighEor( cur_solution )
evaluate_position( cur_solution )
taEu( cur_solution )
if (cur_solution.f Eest.f)
Eest cur_solution
]
]
return Eest
]
To illustratc thc Talu scarch algorithm, vc`ll usc thc N-Quccns prollcm
as ocmonstratco vith thc lcst-lrst scarch algorithm. (Scc Figurc 3.1 lor a
rccap ol thc prollcm ano ocsirco solution.; Altcr oiscussing thc lasic Talu
scarch implcmcntation, vc`ll cxplorc somc ol thc variants that improvc thc
algorithm.
!abu Searcb imp|ementat|on
Thc Talu scarch algorithm is vcry simplc ano can lc illustratco in a singlc
lunction (scc isting 3.15, lunction ;. This lunction is thc corc ol
thc Talu scarch algorithm. Thc supporting lunctions arc not shovn hcrc,
lut arc availallc on thc CD-BOM.