Thuat Toan Va Giai Thuat - Smith.N Studio

  • View
    216

  • Download
    0

Embed Size (px)

Text of Thuat Toan Va Giai Thuat - Smith.N Studio

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    1/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 1

    CHNG 1 : THUT TON THUT GII

    I. KHI NIM THUT TON THUT GII

    II. THUT GII HEURISTIC

    III. CC PHNG PHP TM KIM HEURISTIC

    III.1. Cu trc chung ca bi ton tm kim

    III.2. Tm kim chiu su v tm kim chiu rng

    III.3. Tm kim leo i

    III.4. Tm kim u tin ti u (best-first search)

    III.5. Thut gii AT

    III.6. Thut gii AKT

    III.7. Thut gii A*

    III.8. V d minh ha hot ng ca thut gii A*

    III.9. Bn lun v A*

    III.10. ng dng A* gii bi ton Ta-canh

    III.11. Cc chin lc tm kim lai

    I. TNG QUAN THUT TON THUT GII

    Trong qu trnh nghin cu gii quyt cc vn bi ton, ngi ta a ranhng nhn xt nh sau:

    C nhiu bi ton cho n nay vn cha tm ra mt cch gii theo kiuthut ton v cng khng bit l c tn ti thut ton hay khng.

    C nhiu bi ton c thut ton gii nhng khng chp nhn c vthi gian gii theo thut ton qu ln hoc cc iu kin cho thut tonkh p ng.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    2/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 2

    C nhng bi ton c gii theo nhng cch gii vi phm thut tonnhng vn chp nhn c.

    T nhng nhn nh trn, ngi ta thy rng cn phi c nhng i mi cho khinim thut ton. Ngi ta mrng hai tiu chun ca thut ton: tnh xc nh

    v tnh ng n. Vic mrng tnh xc nh i vi thut ton c th hinqua cc gii thut quy v ngu nhin. Tnh ng ca thut ton by gikhngcn bt buc i vi mt s cch gii bi ton, nht l cc cch gii gn ng.Trong thc tin c nhiu trng hp ngi ta chp nhn cc cch gii thng chokt qu tt (nhng khng phi lc no cng tt) nhng t phc tp v hiu qu.Chng hn nu gii mt bi ton bng thut ton ti u i hi my tnh thc hinnhiu nm th chng ta c th sn lng chp nhn mt gii php gn ti u m chcn my tnh chy trong vi ngy hoc vi gi.

    Cc cch gii chp nhn c nhng khng hon ton p ng y cc tiu

    chun ca thut ton thng c gi l cc thut gii. Khi nim mrng nyca thut ton mca cho chng ta trong vic tm kim phng php giiquyt cc bi ton c t ra.

    Mt trong nhng thut gii thng c cp n v s dng trong khoa hc trtu nhn to l cc cch gii theo kiu Heuristic

    II. THUT GII HEURISTIC

    Thut gii Heuristic l mt s mrng khi nim thut ton. N th hin cch giibi ton vi cc c tnh sau:

    Thng tm c li gii tt (nhng khng chc l li gii tt nht)

    Gii bi ton theo thut gii Heuristic thng d dng v nhanhchng a ra kt qu hn so vi gii thut ti u, v vy chi ph thphn.

    Thut gii Heuristic thng th hin kh t nhin, gn gi vi cchsuy nghv hnh ng ca con ngi.

    C nhiu phng php xy dng mt thut gii Heuristic, trong ngi tathng da vo mt s nguyn l cbn nh sau:

    Nguyn l vt cn thng minh: Trong mt bi ton tm kim no ,khi khng gian tm kim ln, ta thng tm cch gii hn li khng gian tmkim hoc thc hin mt kiu d tm c bit da vo c th ca bi ton nhanh chng tm ra mc tiu.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    3/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 3

    Nguyn l tham lam (Greedy): Ly tiu chun ti u (trn phm vi toncc) ca bi ton lm tiu chun chn la hnh ng cho phm vi cc bca tng bc (hay tng giai on) trong qu trnh tm kim li gii.

    Nguyn l tht: Thc hin hnh ng da trn mt cu trc th t

    hp l ca khng gian kho st nhm nhanh chng t c mt li giitt.

    Hm Heuristic: Trong vic xy dng cc thut gii Heuristic, ngi tathng dng cc hm Heuristic. l cc hm nh gi th, gi tr cahm ph thuc vo trng thi hin ti ca bi ton ti mi bc gii. Nhgi tr ny, ta c th chn c cch hnh ng tng i hp l trong tngbc ca thut gii.

    Bi ton hnh trnh ngn nht ng dng nguyn l Greedy

    Bi ton: Hy tm mt hnh trnh cho mt ngi giao hng i qua n im khcnhau, mi im i qua mt ln v trvim xut pht sao cho tng chiu dion ng cn i l ngn nht. Gi s rng c con ng ni trc tip t gia haiim bt k.

    Tt nhin ta c th gii bi ton ny bng cch lit k tt c con ng c thi,tnh chiu di ca mi con ng ri tm con ng c chiu di ngn nht.Tuy nhin, cch gii ny li c phc tp 0(n!) (mt hnh trnh l mt hon vca n im, do , tng s hnh trnh l s lng hon v ca mt tp n phn t ln!). Do , khi si l tng th s con ng phi xt s tng ln rt nhanh.

    Mt cch gii n gin hn nhiu v thng cho kt qu tng i tt l dng mtthut gii Heuristic ng dng nguyn l Greedy. T tng ca thut gii nh sau:

    Tim khi u, ta lit k tt c qung ng tim xut pht cho nn i l ri chn i theo con ng ngn nht.

    Khi i n mt i l, chn i n i l k tip cng theo nguyn tctrn. Ngha l lit k tt c con ng ti l ta ang ng n nhng il cha i n. Chn con ng ngn nht. Lp li qu trnh ny cho n

    lc khng cn i l no i.

    Bn c th quan st hnh sau thy c qu trnh chn la. Theo nguyn lGreedy, ta ly tiu chun hnh trnh ngn nht ca bi ton lm tiu chun chochn la cc b. Ta hy vng rng, khi i trn n on ng ngn nht th cuicng ta sc mt hnh trnh ngn nht.iu ny khng phi lc no cng ng.Vi iu kin trong hnh tip theo th thut gii cho chng ta mt hnh trnh c

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    4/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 4

    chiu di l 14 trong khi hnh trnh ti u l 13. Kt qu ca thut gii Heuristictrong trng hp ny ch lch 1 n v so vi kt qu ti u. Trong khi , phc tp ca thut gii Heuristic ny ch l 0(n2).

    Hnh : Gii bi ton s dng nguyn l Greedy

    Tt nhin, thut gii theo kiu Heuristic i lc li a ra kt qu khng tt, thmch rt t nh trng hp hnh sau.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    5/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 5

    Bi ton phn vic ng dng ca nguyn l tht

    Mt cng ty nhn c hp ng gia cng m chi tit my J1, J2, Jm. Cng ty cn my gia cng ln lt l P1, P2, Pn. Mi chi tit u c thc gia cng trnbt k my no. Mt khi gia cng mt chi tit trn mt my, cng vic s tiptc cho n lc hon thnh, khng th b ct ngang. gia cng mt vic J1 trn

    mt my bt k ta cn dng mt thi gian tng ng l t1. Nhim v ca cng tyl phi lm sao gia cng xong ton b n chi tit trong thi gian sm nht.

    Chng ta xt bi ton trong trng hp c 3 my P1, P2, P3 v 6 cng vic vi thigian l t1=2, t2=5, t3=8, t4=1, t5=5, t6=1. ta c mt phng n phn cng (L) nhhnh sau:

    Theo hnh ny, ti thi im t=0, ta tin hnh gia cng chi tit J2 trn my P1, J5

    trn P2 v J1 ti P3. Ti thi im t=2, cng vic J1c hon thnh, trn my P3 tagia cng tip chi tit J4. Trong lc , hai my P1 v P2 vn ang thc hin cngvic u tin mnh S phn vic theo hnh trn c gi l lc GANTT. Theo lc ny, ta thy thi gian hon thnh ton b 6 cng vic l12. Nhn xt mt cch cm tnh ta thy rng phng n (L) va thc hin l mtphng n khng tt. Cc my P1 v P2 c qu nhiu thi gian rnh.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    6/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 6

    Thut ton tm phng n ti u L0 cho bi ton ny theo kiu vt cn c phctp cO(mn) (vi m l s my v n l s cng vic). By gita xt n mt thutgii Heuristic rt n gin ( phc tp O(n)) gii bi ton ny.

    Sp xp cc cng vic theo th t gim dn v thi gian gia cng.

    Ln lt sp xp cc vic theo th t vo my cn d nhiu thigian nht.

    Vi t tng nh vy, ta s c mt phng n L* nh sau:

    R rng phng n L* va thc hin cng chnh l phng n ti u ca trnghp ny v thi gian hon thnh l 8, ng bng thi gian ca cng vic J3. Ta hyvng rng mt gii Heuristic n gin nh vy s l mt thut gii ti u. Nhng

    tic thay, ta d dng a ra c mt trng hp m thut gii Heuristic khnga ra c kt qu ti u.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    7/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 7

    Nu gi T* l thi gian gia cng xong n chi tit my do thut gii Heuristic ara v T0 l thi gian ti u th ngi ta chng minh c rng

    , M l s my

    Vi kt qu ny, ta c th xc lp c sai s m chng ta phi gnh chu nudng Heuristic thay v tm mt li gii ti u. Chng hn vi s my l 2 (M=2) ta

    c , v chnh l sai s cc i m trng hp trn gnh chu. Theocng thc ny, s my cng ln th sai s cng ln.

    Trong trng hp M ln th t s 1/M xem nh bng 0 . Nh vy, sai s ti a mta phi chu l T* 4/3 T0, ngha l sai s ti a l 33%. Tuy nhin, kh tm rac nhng trng hp m sai sng bng gi tr cc i, d trong trng hpxu nht. Thut gii Heuristic trong trng hp ny r rng cho chng ta nhngli gii tng i tt.

    III. CC PHNG PHP TM KIM HEURISTIC

    Qua cc phn trc chng ta tm hiu tng quan v tng ca thut giiHeuristic (nguyn l Greedy v sp th t). Trong mc ny, chng ta si su votm hiu mt s k thut tm kim Heuristic mt lp bi ton rt quan trng vc nhiu ng dng trong thc t.

    III.1. Cu trc chung ca bi ton tm kim

    tin li cho vic trnh by, ta hy dnh cht thi gian lm r hn "itng" quan tm ca chng ta trong mc ny. Mt cch chung nht, nhiu vn -bi ton phc tp u c dng "tm ng i trong th" hay ni mt cch hnhthc hn l "xut pht tmtnh ca mt th, tm ng i hiu qu nhtnmtnh no ". Mt pht biu khc thng gp ca dng bi ton ny l :

    Cho trc hai trng thi T0 v TG hy xy dng chui trng thi T0, T1, T2, ..., Tn-1, Tn = TG sao cho :

    tha mn mt iu kin cho trc (thng l nh nht).

    Trong , Ti thuc tp hp S (gi l khng gian trng thi state space) bao gmtt c cc trng thi c th c ca bi ton v cost(Ti-1, Ti) l chi phbin i ttrng thi Ti-1 sang trng thi Ti. Dnhin, t mt trng thi Ti ta c nhiu cch

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    8/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 8

    bin i sang trng thi Ti+1. Khi ni n mt bin i c th t Ti-1 sang Ti ta sdng thut nghng i (vi ng ni v s la chn).

    Hnh : M hnh chung ca cc vn -bi ton phi gii quyt bng phng php tm kim ligii. Khng gian tm kim l mt tp hp trng thi - tp cc nt ca th. Chi ph cn thit

    chuyn t trng thi T ny sang trng thi Tkc biu din di dng cc con s nm trn cungni gia hai nt tng trng cho hai trng thi.

    a s cc bi ton thuc dng m chng ta ang m tu c thc biu dindi dng th. Trong , mt trng thi l mt nh ca th. Tp hp S baogm tt c cc trng thi chnh l tp hp bao gm tt cnh ca th. Vic bini t trng thi Ti-1 sang trng thi Ti l vic i tnh i din cho Ti-1 sang

    nh i din cho Titheo cung ni gia hai nh ny.III.2. Tm kim chiu su v tm kim chiu rng

    bn c c th hnh dung mt cch c th bn cht ca thut gii Heuristic,chng ta nht thit phi nm vng hai chin lc tm kim cbn l tm kim theochiu su (Depth First Search) v tm kim theo chiu rng (Breath First Search).Sdchng ta dng tchin lc m khng phi lphng php l bi v trongthc t, ngi ta hu nh chng bao givn dng mt trong hai kiu tm kim nymt cch trc tip m khng phi sa i g.

    III.2.1. Tm kim chiu su (Depth-First Search)

    Trong tm kim theo chiu su, ti trng thi (nh) hin hnh, ta chn mt trngthi k tip (trong tp cc trng thi c th bin i thnh t trng thi hin ti)lm trng thi hin hnh cho n lc trng thi hin hnh l trng thi ch. Trongtrng hp ti trng thi hin hnh, ta khng th bin i thnh trng thi k tipth ta s quay lui (back-tracking) li trng thi trc trng thi hin hnh (trng

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    9/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 9

    thi bin i thnh trng thi hin hnh) chn ng khc. Nu trng thitrc ny m cng khng th bin i c na th ta quay lui li trng thi trcna v c th. Nu quay lui n trng thi khi u m vn tht bi th kt lunl khng c li gii. Hnh nh sau minh ha hot ng ca tm kim theo chiusu.

    Hnh : Hnh nh ca tm kim chiu su. N ch lu "mrng" trng thi c chn m khng"mrng" cc trng thi khc (nt mu trng trong hnh v).

    III.2.2. Tm kim chiu rng (Breath-First Search)

    Ngc li vi tm kim theo kiu chiu su, tm kim chiu rng mang hnh nhca vt du loang. T trng thi ban u, ta xy dng tp hp S bao gm cc trngthi k tip (m t trng thi ban u c th bin i thnh). Sau , ng vi mitrng thi Tk trong tp S, ta xy dng tp Sk bao gm cc trng thi k tip ca Tkri ln lt b sung cc Sk vo S. Qu trnh ny c lp li cho n lc S c chatrng thi kt thc hoc S khng thay i sau khi b sung tt c Sk.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    10/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 10

    Hnh : Hnh nh ca tm kim chiu rng. Ti mt bc, mi trng thi u cmrng, khng b st trng thi no.

    Chiu su Chiu rng

    Tnh hiu qu Hiu qu khi li gii nm sutrong cy tm kim v c mtphng n chn hng i chnhxc. Hiu qu ca chin lc

    ph thuc vo phng n chnhng i. Phng n cng kmhiu qu th hiu qu ca chinlc cng gim. Thun li khimun tm ch mt li gii.

    Hiu qu khi li giinm gn gc ca cy tmkim. Hiu qu cachin lc ph thuc

    vo su ca li gii.Li gii cng xa gc thhiu qu ca chin lccng gim. Thun likhi mun tm nhiu ligii.

    Lng b nhs dng lu tr cc trngthi

    Ch lu li cc trng thi chaxt n.

    Phi lu ton b cctrng thi.

    Trng hp xu nht Vt cn ton b Vt cn ton b.

    Trng hp tt nht Phng n chn hng i tuyti chnh xc. Li gii c xcnh mt cch trc tip.

    Vt cn ton b.

    Tm kim chiu su v tm kim chiu rng u l cc phng php tm kim ch thng v chc chn tm ra li gii. Tuy nhin, do bn cht l vt cn nn vinhng bi ton c khng gian ln th ta khng th dng hai chin lc ny c.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    11/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 11

    Hn na, hai chin lc ny u c tnh cht "m qung" v chng khng ch n nhng thng tin (tri thc) trng thi hin thi v thng tin vch cn t ticng mi quan h gia chng. Cc tri thc ny v cng quan trng v rt c ngha thit k cc thut gii hiu qu hn m ta sp sa bn n.

    III.3. Tm kim leo i

    III.3.1. Leo i n gin

    Tm kim leo i theo ng ngha, ni chung, thc cht ch l mt trng hp cbit ca tm kim theo chiu su nhng khng th quay lui. Trong tm kim leoi, vic la chn trng thi tip theo c quyt nh da trn mt hm Heuristic.

    Hm Heuristic l g ?

    Thut ng "hm Heuristic" mun ni ln iu g? Chng c g gh gm. Bn quen vi n ri! n gin ch l mt c lng vkh nng dn n li giitnh t trng thi (khong cch gia trng thi hin ti v trng thi ch). Ta squy c gi hm ny l h trong sut gio trnh ny. i lc ta cng cp nchi

    ph ti u thc st mt trng thi dn n li gii. Thng thng, gi tr ny lkhng th tnh ton c (v tnh c ng ngha l bit con ng n ligii !) m ta ch dng n nh mt cs suy lun v mt l thuyt m thi !Hm h, ta quy c rng, lun tr ra kt qu l mt s khng m. bn c thcs nm c ngha ca hai hm ny, hy quan st hnh sau trong minh hachi ph ti u thc s v chi phc lng.

    Hnh Chi phc lng h = 6 v chi ph ti u thc s h = 4+5 = 9 (i theo ng 1-3-7)

    Bn ang trong mt thnh ph xa l m khng c bn trong tay v tamun i vo khu trung tm? Mt cch suy ngh n gin, chng ta s nhm vohngnhng ta cao c ca khu trung tm!

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    12/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 12

    Ttng

    1) Nu trng thi bt u cng l trng thi ch th thot v bo l tm c ligii. Ngc li, t trng thi hin hnh (Ti) l trng thi khi u (T0)

    2) Lp li cho n khi t n trng thi kt thc hoc cho n khi khng tn timt trng thi tip theo hp l (Tk) ca trng thi hin hnh :

    a. t Tk l mt trng thi tip theo hp l ca trng thi hin hnhTi.

    b. nh gi trng thi Tk mi :

    b.1. Nu l trng thi kt thc th tr v tr ny v thot.

    b.2. Nu khng phi l trng thi kt thc nhng tt hntrng thi hin hnh th cp nht n thnh trng thi hinhnh.

    b.3. Nu n khng tt hn trng thi hin hnh th tip tcvng lp.

    M gi

    Ti:= T0; Stop :=FALSE;

    WHILE Stop=FALSE DO BEGIN

    IF Ti TG THENBEGIN

    ; Stop:=TRUE;

    END;

    ELSE BEGIN

    Better:=FALSE;

    WHILE (Better=FALSE) AND (STOP=FALSE) DOBEGIN

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    13/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 13

    IF THENBEGIN

    ; Stop:=TRUE;END;

    ELSE BEGIN

    Tk := ;

    IF THEN BEGIN

    Ti :=Tk; Better:=TRUE;

    END;

    END;END; {WHILE}

    END; {ELSE}

    END;{WHILE}

    Mnh "h(Tk) tt hn h(Ti)" ngha l g? y l mt khi nim chung chung.Khi ci t thut gii, ta phi cung cp mt nh ngha tng minh vtt hn.Trong mt s trng hp, tt hn l nh hn : h(Tk) < h(Ti); mt s trng hp

    khc tt hn l ln hn h(Tk) > h(Ti)...Chng hn, i vi bi ton tm ng ingn nht gia hai im. Nu dng hm h l hm cho ra khong cch theo ngchim bay gia v tr hin ti (trng thi hin ti) v ch n (trng thi ch) th tthn ngha l nh hn.

    Vn cn lm r k tip l th no l ?Mt trngthi k tip hp l l trng thi cha c xt n. Gi s h ca trng thi hin tiTi c gi tr l h(Ti) = 1.23 v t Ti ta c th bin i sang mt trong 3 trng thik tip ln lt l Tk1, Tk2, Tk3 vi gi tr cc hm h tng ng l h(Tk1) = 1.67,h(Tk2) = 2.52, h(Tk3) = 1.04. u tin, Tk sc gn bng Tk1, nhng v h(Tk)

    = h(Tk1) > h(Ti) nn Tk khng c chn. K tip l Tk sc gn bng Tk2 vcng khng c chn. Cui cng th Tk3c chn. Nhng gi s h(Tk3) = 1.3th c Tk3 cng khng c chn v mnh s c gi tr TRUE. Gii thch ny c v hin nhin nhng c l cn thit trnh nhm ln cho bn c.

    thy r hot ng ca thut gii leo i. Ta hy xt mt bi ton minh ha sau.Cho 4 khi lp phng ging nhau A, B, C, D. Trong cc mt (M1), (M2),

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    14/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 14

    (M3), (M4), (M5), (M6) c thc t bng 1 trong 6 mu (1), (2), (3), (4), (5),(6). Ban u cc khi lp phng c xp vo mt hng. Mi mt bc, ta chc xoay mt khi lp phng quanh mt trc (X,Y,Z) 900 theo chiu bt k(ngha l ngc chiu hay thun chiu kim ng h cng c). Hy xc nh sbc quay t nht sao cho tt c cc mt ca khi lp phng trn 4 mt ca hng

    l c cng mu nh hnh v.

    Hnh : Bi ton 4 khi lp phng

    gii quyt vn , trc ht ta cn nh ngha mt hm G dng nh gi mttnh trng c th c phi l li gii hay khng? Bn c c th d dng a ra mtci t ca hm G nh sau :

    IF (Gtri + Gphi + Gtrn + Gdi + Gtrc + Gsau) = 16 THEN

    G:=TRUE

    ELSE

    G:=FALSE;

    Trong , Gphil slng cc mtc cng mu ca mt bn phi ca hng.Tng t cho Gtri, Gtrn, Ggia, Gtrc, Gsau. Tuy nhin, do cc khi lpphng A,B,C,D l hon ton tng t nhau nn tng quan gia cc mt ca mikhi l ging nhau. Do , nu c 2 mt khng i nhau trn hng ng mu th 4mt cn li ca hng cng ng mu. T ta ch cn hm G c nh ngha nhsau l :

    IF Gphi + Gdi = 8 THEN

    G:=TRUE

    ELSE

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    15/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 15

    G:=FALSE;

    Hm h (c lng kh nng dn n li gii ca mt trng thi) sc nh

    ngha nh sau :

    h = Gtri+ Gphi+ Gtrn+ Gdi

    Bi ton ny n gin thut gii leo i c th hot ng tt. Tuy nhin,khng phi lc no ta cng may mn nh th!

    n y, c th chng ta s ny sinh mt tng. Nu chn trng thi tt hnlm trng thi hin ti th ti sao khng chn trng thi tt nht? Nh vy, c ltas nhanh chng dn n li gii hn! Ta s bn lun v vn : "liu ci tin nyc thc s gip chng ta dn n li gii nhanh hn hay khng?" ngay sau khitrnh by xong thut gii leo i dc ng.

    III.3.2. Leo i dc ng

    V cbn, leo i dc ng cng ging nh leo i, ch khc im l leo idc ng s duyt tt c cc hng i c th v chn i theo trng thi tt nhttrong s cc trng thi k tip c th c (trong khi leo i ch chn i theo trngthi k tip u tin tt hn trng thi hin hnh m n tm thy).

    Ttng

    1) Nu trng thi bt u cng l trng thi ch th thot v bo l tm c li gii. Ngcli, t trng thi hin hnh (Ti) l trng thi khi u (T0)

    2) Lp li cho n khi t n trng thi kt thc hoc cho n khi (Ti) khng tn ti mt trngthi k tip (Tk) no tt hn trng thi hin ti (Ti)

    a) t S bng tp tt c trng thi k tip c th c ca Tiv tt hn Ti.

    b) Xc nh Tkmax l trng thi tt nht trong tp S

    t Ti = Tkmax

    M gi

    Ti:= T0;

    Stop :=FALSE;

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    16/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 16

    WHILE Stop=FALSE DO BEGIN

    IF Ti TG THEN BEGIN

    ;

    STOP :=TRUE;

    END;

    ELSE BEGIN

    Best:=h(Ti);

    Tmax:= Ti;

    WHILE DOBEGIN

    Tk := ;

    IF THEN BEGIN

    Best :=h(Tk);

    Tmax:= Tk;

    END;

    END;

    IF (Best>Ti) THEN

    Ti:= Tmax;

    ELSE BEGIN

    ;

    STOP:=TRUE;

    END;

    END; {ELSE IF}

    END;{WHILE STOP}

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    17/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 17

    III.3.3. nh gi

    So vi leo i n gin, leo i dc ng c u im l lun lun chn hng ctrin vng nht i. Liu iu ny c m bo leo i dc ng lun tt hn leoi n gin khng? Cu tr li l khng. Leo i dc ng ch tt hn leo i

    n gin trong mt s trng hp m thi. chn ra c hng i tt nht, leoi dc ng phi duyt qua tt c cc hng i c th c ti trng thi hin hnh.Trong khi , leo i n gin ch chn i theo trng thi u tin tt hn (so vitrng thi hin hnh) m n tm ra c. Do , thi gian cn thit leo i dcng chn c mt hng i s ln hn so vi leo i n gin. Tuy vy, do lcno cng chn hng i tt nht nn leo i dc ng thng s tm n li giisau mt s bc t hn so vi leo i n gin. Ni mt cch ngn gn, leo idc ng s tn nhiu thi gian hn cho mt bc nhng li i t bc hn; cnleo i n gin tn t thi gian hn cho mt bc i nhng li phi i nhiu bchn. y chnh l yu tc v mt gia hai thut gii nn ta phi cn nhc k

    lng khi la chn thut gii.

    C hai phng php leo ni n gin v leo ni dc ng u c kh nng tht bitrong vic tm li gii ca bi ton mc d li gii thc s hin hu. C hai giithut u c th kt thc khi t c mt trng thi m khng cn trng thi nott hn na c th pht sinh nhng trng thi ny khng phi l trng thi ch.iu ny s xy ra nu chng trnh t n mt im cc i a phng, mton n iu ngang.

    im cc i a phng (a local maximum) : l mt trng thi tt hn tt c lncn ca n nhng khng tt hn mt s trng thi khc xa hn. Ngha l ti mtim cc i a phng, mi trng thi trong mt ln cn ca trng thi hin tiuxu hn trng thi hin ti. Tuy c dng v ca li gii nhng cc cc i aphng khng phi l li gii thc s. Trong trng hp ny, chng c gi lnhng ngn i thp.

    on n iu ngang (a plateau) : l mt vng bng phng ca khng gian tmkim, trong , ton b cc trng thi ln cn u c cng gi tr.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    18/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 18

    Hnh : Cc tnh hung kh khn cho tm kim leo o.

    i ph vi cc cc im ny, ngi ta a ra mt s gii php. Ta s tmhiu 2 trong s cc gii php ny. Nhng gii ny, khng thc s gii quyt trn

    vn vn m ch l mt phng n cu nguy tm thi m thi.

    Phng n u tin l kt hp leo i v quay lui. Ta s quay lui li cc trng thitrc v thi theo hng khc. Thao tc ny hp l nu ti cc trng thitrc c mt hng i tt m ta b qua trc . y l mt cch kh hayi ph vi cc im cc i a phng. Tuy nhin, do c im ca leo i l"bc sau cao hn bc trc" nn phng n ny s tht bi khi ta xut pht tmt im qu cao hoc xut pht t mt nh i m n c li gii cn phii qua mt "thung lng" tht su nh trong hnh sau.

    Hnh : Mt trng hp tht bi ca leo o kt hp quay lui.

    Cch th hai l thc hin mt bc nhy vttheo hng no thn mtvng mi ca khng gian tm kim. Nm na l "bc" lin tc nhiu "bc"(chng hn 5,7,10, ) m tm thi "qun" i vic kim tra "bc sau cao hn

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    19/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 19

    bc trc". Tip cn c v hiu qu khi ta gp phi mt on n iu ngang.Tuy nhin, nhy vt cng c ngha l ta b qua chi tin n li gii thcs. Trong trng hp chng ta ang ng kh gn li gii, vic nhy vt sachng ta sang mt v tr hon ton xa l, m t, c th s dn chng ta n mtrc ri kiu khc. Hn na, s bc nhy l bao nhiu v nhy theo hng no l

    mt vn ph thuc rt nhiu vo c im khng gian tm kim ca bi ton.

    Hnh Mt trng hp kh khn cho phng n "nhy vt".

    Leo ni l mt phng php cc b bi v n quyt nh s lm g tip theo davo mt nh gi v trng thi hin ti v cc trng thi k tip c th c (tt hntrng thi hin ti, trng thi tt nhttt hn trng thi hin ti) thay v phi xemxt mt cch ton din trn tt c cc trng thi i qua. Thun li ca leo ni l

    t gp s bng n t hp hn so vi cc phng php ton cc. Nhng n cngging nh cc phng php cc b khc ch l khng chc chn tm ra li giitrong trng hp xu nht.

    Mt ln na, ta khng nh li vai tr quyt nh ca hm Heuristic trong qu trnhtm kim li gii. Vi cng mt thut gii (nh leo i chng hn), nu ta c mthm Heuristic tt hn th kt qu sc tm thy nhanh hn. Ta hy xt bi tonv cc khi c trnh by hnh sau. Ta c hai thao tc bin i l:

    + Ly mt khi nh mt ct bt k v t n ln mt ch trng to thnh

    mt ct mi. Lu l ch c th to ra ti a 2 ct mi.+ Ly mt khi nh mt ct v t n ln nh mt ct khc

    Hy xc nh s thao tc t nht bin i ct cho thnh ct kt qu.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    20/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 20

    Hnh : Trng thi khi u v trng thi kt thc

    Gi s ban u ta dng mt hm Heuristic n gin nh sau :

    H1 : Cng 1 im cho mi khi v trng so vi trng thi ch. Tr 1im cho mi khi t v tr sai so vi trng thi ch.

    Dng hm ny, trng thi kt thc s c gi tr l 8 v c 8 khi u c t vtrng. Trng thi khi u c gi tr l 4 (v n c 1 im cng cho cc khi C,D, E, F, G, H v 1 im tr cho cc khi A v B). Ch c th c mt di chuyn ttrng thi khi u, l dch chuyn khi A xung to thnh mt ct mi (T1).

    iu sinh ra mt trng thi vi sim l 6 (v v tr ca khi A by gisinh ra

    1 im cng hn l mt im tr). Th tc leo ni s chp nhn s dch chuyn. T trng thi mi T1, c ba di chuyn c th thc hin dn n ba trng thiTa, Tb, Tcc minh ha trong hnh di. Nhng trng thi ny c sim l :h(Ta)= 4; h(Tb) = 4 v h(Tc) = 4

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    21/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 21

    T1 TA TB TC

    Hnh Cc trng thi c tht c t T1

    Th tc leo ni s tm dng bi v tt c cc trng thi ny c sim thp hntrng thi hin hnh. Qu trnh tm kim ch dng li mt trng thi cc i aphng m khng phi l cc i ton cc.

    Chng ta c th li cho chnh gii thut leo i v tht bi do khng tmnhn tng qut tm ra li gii. Nhng chng ta cng c th li cho hm

    Heuristic v c gng sa i n. Gi s ta thay hm ban u bng hm Heuristicsau y :

    H2 : i vi mi khi ph trng (khi ph trl khi nm bn dikhi hin ti), cng 1 im, ngc li tr 1 im.

    Dng hm ny, trng thi kt thc c sim l 28 v B nm ng v tr v khngc khi ph trno, C ng v trc 1 im cng vi 1 im do khi ph trBnm ng v tr nn C c 2 im, D c 3 im, ....Trng thi khi u c sim l 28. Vic di chuyn A xung to thnh mt ct mi lm sinh ra mt trngthi vi sim l h(T1) = 21 v A khng cn 7 khi sai pha di n na. Batrng thi c th pht sinh tip theo by gic cc im s l : h(Ta)=28;h(Tb)=16 v h(Tc) = 15. Lc ny th tc leo ni dc ng s chn di chuynn trng thi Tc, c mt khi ng. Qua hm H2 ny ta rt ra mt nguyn tc: tt hn khng ch c ngha l c nhiu u im hn m cn phi t khuytimhn. Hn na, khuyt im khng c ngha ch l s sai bit ngay ti mt v tr mcn l s khc bit trong tng quan gia cc v tr. R rng l ng v mt ktqu, cng mt th tc leo i nhng hm H1 b tht bi (do ch bit nh gi u

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    22/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 22

    im) cn hm H2 mi ny li hot ng mt cch hon ho (do bit nh gi cu im v khuyt im).

    ng tic, khng phi lc no chng ta cng thit kc mt hm Heuristic honho nh th. V vic nh gi u im kh, vic nh gi khuyt im cng kh

    v tinh t hn. Chng hn, xt li vn mun i vo khu trung tm ca mt thnhphxa l. hm Heuristic hiu qu, ta cn phi a cc thng tin v cc ngmt chiu v cc ng ct, m trong trng hp mt thnh ph hon ton xa l thta kh hoc khng th bit c nhng thng tin ny.

    n y, chng ta hiu r bn cht ca hai thut gii tip cn theo chin lc tmkim chiu su. Hiu qu ca c hai thut gii leo i n gin v leo i dcng ph thuc vo :

    + Cht lng ca hm Heuristic.

    + c im ca khng gian trng thi.

    + Trng thi khi u.

    Sau y, chng ta s tm hiu mt tip cn theo mi, kt hp c sc mnh cac tm kim chiu su v tm kim chiu rng. Mt thut gii rt linh ng v cth ni l mt thut gii kinh in ca Heuristic.

    III.4. Tm kim u tin ti u (best-first search)

    u im ca tm kim theo chiu su l khng phi quan tm n s mrng catt c cc nhnh. u im ca tm kim chiu rng l khng b sa vo cc ngdn b tc (cc nhnh ct). Tm kim u tin ti u s kt hp 2 phng php trncho php ta i theo mt con ng duy nht ti mt thi im, nhng ng thivn "quan st" c nhng hng khc. Nu con ng ang i "c v" khngtrin vng bng nhng con ng ta ang "quan st" ta s chuyn sang i theo mttrong s cc con ng ny. tin li ta s dng ch vit tt BFS thay cho tngi tm kim u tin ti u.

    Mt cch c th, ti mi bc ca tm kim BFS, ta chn i theo trng thi c kh

    nng cao nht trong s cc trng thi c xt cho n thi im . (khc vileo i dc ng l ch chn trng thi c kh nng cao nht trong s cc trngthi k tip c thn c t trng thi hin ti). Nh vy, vi tip cn ny, ta su tin i vo nhng nhnh tm kim c kh nng nht (ging tm kim leo idc ng), nhng ta s khng b ln qun trong cc nhnh ny v nu cng i suvo mt hng m ta pht hin ra rng hng ny cng i th cng t, n mc nxu hn c nhng hng m ta cha i, th ta s khng i tip hng hin ti na

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    23/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 23

    m chn i theo mt hng tt nht trong s nhng hng cha i. l t tngcho ca tm kim BFS. hiu c t tng ny. Bn hy xem v d sau :

    Hnh Minh ha thut gii Best-First Search

    Khi u, ch c mt nt (trng thi) A nn n sc mrng to ra 3 nt miB,C v D. Cc con s di nt l gi tr cho bit tt ca nt. Con s cng nh,

    nt cng tt. Do D l nt c kh nng nht nn n sc mrng tip sau nt Av sinh ra 2 nt k tip l E v F. n y, ta li thy nt B c v c kh nng nht(trong cc nt B,C,E,F) nn ta s chn mrng nt B v to ra 2 nt G v H.Nhng li mt ln na, hai nt G, H ny c nh gi t kh nng hn E, v ths ch li trv E. E c mrng v cc nt c sinh ra t E l I v J. bc k tip, J sc mrng v n c kh nng nht. Qu trnh ny tip tc chon khi tm thy mt li gii.

    Lu rng tm kim ny rt ging vi tm kim leo i dc ng, vi 2 ngoi l.Trong leo ni, mt trng thi c chn v tt c cc trng thi khc b loi b,

    khng bao gichng c xem xt li. Cch x l dt khot ny l mt c trngca leo i. Trong BFS, ti mt bc, cng c mt di chuyn c chn nhngnhng ci khc vn c gi li, ta c th trli xt sau khi trng thi hinti trnn km kh nng hn nhng trng thi c lu tr. Hn na, ta chntrng thi tt nht m khng quan tm n n c tt hn hay khng cc trng thitrc . iu ny tng phn vi leo i v leo i s dng nu khng c trngthi tip theo no tt hn trng thi hin hnh.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    24/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 24

    ci t cc thut gii theo kiu tm kim BFS, ngi ta thng cn dng 2 tphp sau :

    OPEN : tp cha cc trng thi c sinh ra nhng cha c xt n (v ta chn mt trng thi khc). Thc ra, OPEN l mt loi hng i u tin (priority

    queue) m trong , phn t c u tin cao nht l phn ttt nht. Ngi tathng ci t hng i u tin bng Heap. Cc bn c th tham kho thm trongcc ti liu v Cu trc d liu v loi d liu ny.

    CLOSE : tp cha cc trng thi c xt n. Chng ta cn lu tr nhngtrng thi ny trong b nh phng trng hp khi mt trng thi mi cto ra li trng vi mt trng thi m ta xt n trc . Trong trng hpkhng gian tm kim c dng cy th khng cn dng tp ny.

    Thut gii BEST-FIRST SEARCH

    1. t OPEN cha trng thi khi u.

    2. Cho n khi tm c trng thi ch hoc khng cn nt no trong OPEN, thc hin :

    2.a. Chn trng thi tt nht (Tmax) trong OPEN (v xa Tmaxkhi OPEN)

    2.b. Nu Tmax l trng thi kt thc th thot.

    2.c. Ngc li, to ra cc trng thi k tip Tk c th c t trng thi Tmax. ivi mi trng thi k tip Tk thc hin :

    Tnh f(Tk); Thm Tk vo OPEN

    BFS kh n gin. Tuy vy, trn thc t, cng nh tm kim chiu su v chiurng, him khi ta dng BFS mt cch trc tip. Thng thng, ngi ta thngdng cc phin bn ca BFS l AT, AKT v A*

    Thng tin v qu khv tng lai

    Thng thng, trong cc phng n tm kim theo kiu BFS, tt fca mttrng thi c tnh da theo 2 hai gi tr m ta gi l l g v h. h chng ta bit, l mt c lng v chi ph t trng thi hin hnh cho n trng thi ch(thng tin tng lai). Cn g l "chiu di qung ng" i t trng thi ban ucho n trng thi hin ti (thng tin qu kh). Lu rng g l chi ph thc s(khng phi chi phc lng). d hiu, bn hy quan st hnh sau :

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    25/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 25

    Hnh 6.14 Phn bit khi nim g v h

    Kt hp g v h thnh f (f = g + h) s th hin mt c lng v "tng chi ph"cho con ng t trng thi bt u n trng thi kt thc dc theo con ng iqua trng thi hin hnh. thun tin cho thut gii, ta quy c l g v hukhng m v cng nh ngha l cng tt.

    III.5. Thut gii AT

    Thut gii ATl mt phng php tm kim theo kiu BFS vi tt ca nt lgi tr hm g tng chiu di con ng i t trng thi bt u n trng thihin ti.

    Thut gii AT

    1. t OPEN cha trng thi khi u.

    2. Cho n khi tm c trng thi ch hoc khng cn nt no trong OPEN, thc hin :

    2.a. Chn trng thi (Tmax) c gi tr g nh nht trong OPEN (v xa Tmaxkhi OPEN)

    2.b. Nu Tmax l trng thi kt thc th thot.

    2.c. Ngc li, to ra cc trng thi k tip Tk c th c t trng thi Tmax. ivi mi trng thi k tip Tk thc hin :

    g(Tk) = g(Tmax) + cost(Tmax, Tk);

    Thm Tk vo OPEN.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    26/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 26

    * V ch s dng hm g (m khng dng hm c lng h) fsnh gi tt ca mt trngthi nn ta cng c th xem AT ch l mt thut ton.

    III.6. Thut gii AKT

    (Algorithm for Knowlegeable Tree Search)

    Thut gii AKTmrng AT bng cch s dng thm thng tin c lng h. tt ca mt trng thi f l tng ca hai hm g v h.

    Thut gii AKT

    1. t OPEN cha trng thi khi u.

    2. Cho n khi tm c trng thi ch hoc khng cn nt no trong OPEN, thc hin :

    2.a. Chn trng thi (Tmax) c gi tr f nh nht trong OPEN (v xa Tmaxkhi OPEN)

    2.b. Nu Tmax l trng thi kt thc th thot.

    2.c. Ngc li, to ra cc trng thi k tip Tk c th c t trng thi Tmax. ivi mi trng thi k tip Tk thc hin :

    g(Tk) = g(Tmax) + cost(Tmax, Tk);

    Tnh h(Tk)

    f(Tk) = g(Tk) + h(Tk);

    Thm Tk vo OPEN.

    III.7. Thut gii A*

    A* l mt phin bn c bit ca AKT p dng cho trng hp th. Thut giiA* c s dng thm tp hp CLOSE lu tr nhng trng hp c xtn. A* mrng AKTbng cch b sung cch gii quyt trng hp khi "m"mt nt m nt ny c sn trong OPEN hoc CLOSE. Khi xt n mt trngthi Ti bn cnh vic lu tr 3 gi tr cbn g,h, f phn nh tt ca trngthi , A* cn lu tr thm hai thng s sau :

    1. Trng thi cha ca trng thi Ti (k hiu l Cha(Ti) : cho bit trng thi dnn trng thi Ti. Trong trng hp c nhiu trng thi dn n Ti

    th chnCha(Ti) sao cho chi phi t trng thi khi u n Ti l thp nht, ngha l :

    g(Ti) = g(Tcha) + cost(Tcha, Ti) l thp nht.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    27/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 27

    2.Danh sch cc trng thi ktip ca Ti: danh sch ny lu tr cc trng thi ktip Tk ca Ti sao cho chi phn Tk thng qua Ti t trng thi ban u l thpnht. Thc cht th danh sch ny c thc tnh ra t thuc tnh Cha ca cctrng thi c lu tr. Tuy nhin, vic tnh ton ny c th mt nhiu thi gian(khi tp OPEN, CLOSE c mrng) nn ngi ta thng lu tr ra mt danh

    sch ring. Trong thut ton sau y, chng ta s khng cp n vic lu trdanh sch ny. Sau khi hiu r thut ton, bn c c th d dng iu chnh lithut ton lu tr thm thuc tnh ny.

    1.t OPEN ch cha T0. t g(T0) = 0, h(T0) = 0 v f(T0) = 0.t CLOSE l tp hp rng.

    2. Lp li cc bc sau cho n khi gp iu kin dng.

    2.a. Nu OPEN rng : bi ton v nghim, thot.

    2.b. Ngc li, chn Tmax trong OPEN sao cho f(Tmax) l nh nht

    2.b.1. Ly Tmax ra khi OPEN v a Tmax vo CLOSE.

    2.b.2. Nu Tmaxchnh l TGth thot v thng bo li gii l Tmax.

    2.b.3. Nu Tmax khng phi l TG. To ra danh sch tt ccc trngthi k tip ca Tmax. Gi mt trng thi ny l Tk. Vi mi Tk, lm ccbc sau :

    2.b.3.1. Tnh g(Tk) = g(Tmax) + cost(Tmax, Tk).

    2.b.3.2. Nu tn ti Tk trong OPEN trng vi Tk.

    Nu g(Tk) < g(Tk) th

    t g(Tk) = g(Tk)

    Tnh li f(Tk)

    t Cha(Tk) = Tmax

    2.b.3.3. Nu tn ti Tk trong CLOSE trng vi Tk.

    Nu g(Tk) < g(Tk) th

    t g(Tk) = g(Tk)

    Tnh li f(Tk)

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    28/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 28

    t Cha(Tk) = Tmax

    Lan truyn s thay i gi tr g, f cho tt c cctrng thi k tip ca Ti (tt c cc cp) c lu tr trong CLOSE v OPEN.

    2.b.3.4. Nu Tkcha xut hin trong cOPEN ln CLOSE th :

    Thm Tk vo OPEN

    Tnh : f' (Tk) = g(Tk)+h(Tk).

    C mt sim cn gii thch trong thut gii ny. u tin l vic sau khi tmthy trng thi ch TG, lm sao xy dng li c "con ng" t T0n TG.Rt n gin, bn ch cn ln ngc theo thuc tnh Cha ca cc trng thi c lu tr trong CLOSE cho n khi t n T0. chnh l "con ng" tiu i t TG n T

    0(hay ni cch khc l t T

    0n TG).

    im th hai l thao tc cp nht li g(Tk) , f(Tk) v Cha(Tk) trong bc2.b.3.2 v 2.b.3.3. Cc thao tc ny th hin t tng : "lun chn con ng tiu nht". Nh chng ta bit, gi tr g(Tk) nhm lu tr chi ph ti u thc stnh t T0n Tk. Do , nu chng ta pht hin thy mt "con ng" khc tthn thng qua Tk (c chi ph nh hn) con ng hin ti c lu tr th ta phichn "con ng" mi tt hn ny. Trng hp 2.b.3.3 phc tp hn. V t Tknm trong tp CLOSE nn t Tk ta lu tr cc trng thi con k tip xut phtt Tk. Nhng g(Tk) thay i dn n gi tr g ca cc trng thi con ny cngphi thay i theo. V n lt cc trng thi con ny li c th c cc cc trng

    thi con tip theo ca chng v c th cho n khi mi nhnh kt thc vi mttrng thi trong OPEN (ngha l khng c trng thi con no na). thc hinqu trnh cp nht ny, ta hy thc hin qu trnh duyt theo chiu su vi imkhi u l Tk. Duyt n u, ta cp nht li g ca cc trng thi n ( dngcng thc g(T) = g(Cha(T)) +cost(Cha(T), T) ) v v th gi trf ca cc trngthi ny cng thay i theo.

    Mt ln na, xin nhc li rng, bn c th cho rng tp OPEN lu tr cc trngthi "sc xem xt n sau" cn tp CLOSE lu tr cc trng thi " c xtn ri".

    C th bn s cm thy kh lng tng trc mt thut gii di nh th. Vn cl s trnn sng sa hn khi bn quan st cc bc gii bi ton tm ng ingn nht trn th bng thut gii A* sau y.

    III.8. V d minh ha hot ng ca thut gii A*

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    29/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 29

    Chng ta s minh ha hot ng ca thut gii A* trong vic tm kim ng ingn nht t thnh phAradn thnh phBucharestca Romania. Bn ccthnh ph ca Romania c cho trong th sau. Trong mi nh ca thca l mt thnh ph, gia hai nh c cung ni ngha l c ng i gia haithnh ph tng ng. Trng s ca cung chnh l chiu di (tnh bng km) ca

    ng i ni hai thnh ph tng ng, chiu di theo ng chim bay mt thnhphn Bucharest c cho trong bng km theo.

    Hnh : Bng ca Romania vi khong cch ng tnh theo km

    Bng : Khong cch ng chim bay t mt thnh phn Bucharest.

    Chng ta s chn hm h chnh l khong cch ng chim bay cho trong bngtrn v hm chi phcost(Ti, Ti+1) chnh l chiu di con ng ni t thnh ph Tiv Ti+1.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    30/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 30

    Sau y l tng bc hot ng ca thut ton A* trong vic tm ng i ngnnht t Arad n Bucharest.

    Ban u :

    OPEN = {(Arad,g= 0,h= 0,f= 0)}

    CLOSE = {}

    Do trong OPEN ch cha mt thnh ph duy nht nn thnh ph ny s l thnhph tt nht. Ngha l Tmax = Arad.Ta ly Arad ra khi OPEN v a voCLOSE.

    OPEN = {}

    CLOSE = {(Arad,g= 0,h= 0,f= 0)}

    T Arad c thi n c 3 thnh ph l Sibiu, Timisoara v Zerind. Ta ln lttnh gi tr f, g v h ca 3 thnh ph ny. Do c 3 nt mi to ra ny cha c ntcha nn ban u nt cha ca chng u l Arad.

    h(Sibiu) = 253

    g(Sibiu) = g(Arad)+cost(Arad,Sibiu)

    = 0+140= 140

    f(Sibiu) = g(Sibiu)+h(Sibiu)

    = 140+253 = 393

    Cha(Sibiu) = Arad

    h(Timisoara) = 329

    g(Timisoara) = g(Arad)+cost(Arad, Timisoara)

    = 0+118= 118

    f(Timisoara) = g(Timisoara)+ h(Timisoara)

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    31/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 31

    = 118+329 = 447

    Cha(Timisoara) = Arad

    h(Zerind) = 374

    g(Zerind) = g(Arad)+cost(Arad, Zerind)

    = 0+75= 75

    f(Zerind) = g(Zerind)+h(Zerind)

    = 75+374 = 449

    Cha(Zerind) = AradDo c 3 nt Sibiu, Timisoara, Zerind u khng c trong c OPEN v CLOSE nnta b sung 3 nt ny vo OPEN.

    OPEN = {(Sibiu,g= 140,h= 253,f= 393,Cha= Arad)

    (Timisoara,g= 118,h= 329,f= 447,Cha= Arad)

    (Zerind,g= 75,h= 374,f= 449,Cha= Arad)}

    CLOSE = {(Arad,g= 0,h= 0,f= 0)}

    Hnh : Bc 1, nt c ng ngoc vung (nh [Arad]) l nt trong tp CLOSE,ngc li l trong tp OPEN.

    Trong tp OPEN, nt Sibiu l nt c gi tr f nh nht nn ta s chn Tmax =Sibiu. Ta ly Sibiu ra khi OPEN v a vo CLOSE.

    OPEN = {(Timisoara,g= 118,h= 329,f= 447,Cha= Arad)

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    32/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 32

    (Zerind,g= 75,h= 374,f= 449,Cha= Arad)}

    CLOSE = {(Arad,g= 0,h= 0,f= 0)

    (Sibiu,g= 140,h= 253,f= 393,Cha= Arad)}

    T Sibiu c thi n c 4 thnh ph l : Arad, Fagaras, Oradea, Rimnicu. Taln lt tnh cc gi tr g, h, f cho cc nt ny.

    h(Arad) = 366

    g(Arad) = g(Sibiu)+cost(Sibiu,Arad)

    = 140+140= 280

    f(Arad) = g(Arad)+h(Arad)

    = 280+366 = 646

    h(Fagaras) = 178

    g(Fagaras) = g(Sibiu)+cost(Sibiu, Fagaras) = 140+99= 239

    f(Fagaras) = g(Fagaras)+ h(Fagaras)

    = 239+178= 417

    h(Oradea) = 380

    g(Oradea) = g(Sibiu)+cost(Sibiu, Oradea)

    = 140+151 = 291

    f(Oradea) = g(Oradea)+ h(Oradea)

    = 291+380 = 671

    h(R.Vilcea) = 193

    g(R.Vilcea) = g(Sibiu)+cost(Sibiu, R.Vilcea)

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    33/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 33

    = 140+80 = 220

    f(R.Vilcea) = g(R.Vilcea)+ h(R.Vilcea)

    = 220+193 = 413

    Nt Arad c trong CLOSE. Tuy nhin, do g(Arad) mi c to ra (c gi tr280) ln hn g(Arad) lu trong CLOSE (c gi tr 0) nn ta s khng cp nht ligi tr g v f ca Arad lu trong CLOSE. 3 nt cn li : Fagaras, Oradea, Rimnicuu khng c trong c OPEN v CLOSE nn ta sa 3 nt ny vo OPEN, tcha ca chng l Sibiu. Nh vy, n bc ny OPEN cha tng cng 5 thnhph.

    OPEN = {(Timisoara,g= 118,h= 329,f=447,Cha= Arad)(Zerind,g= 75,h= 374,f=449,Cha= Arad)

    (Fagaras,g= 239,h= 178,f=417,Cha= Sibiu)

    (Oradea,g= 291,h= 380,f=617,Cha= Sibiu)

    (R.Vilcea,g= 220,h= 193,f=413,Cha= Sibiu)}

    CLOSE = {(Arad,g= 0,h= 0,f= 0)

    (Sibiu,g= 140,h= 253,f= 393,Cha= Arad)}

    Trong tp OPEN, nt R.Vilcea l nt c gi tr f nh nht. Ta chn Tmax =R.Vilcea. Chuyn R.Vilcea t OPEN sang CLOSE. T R.Vilcea c thi n

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    34/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 34

    c 3 thnh ph l Craiova, Pitesti v Sibiu. Ta ln lt tnh gi tr f, g v h ca3 thnh ph ny.

    h(Sibiu) = 253

    g(Sibiu) = g(R.Vilcea)+ cost(R.Vilcea,Sibiu)

    = 220+80= 300

    f(Sibiu) = g(Sibiu)+h(Sibiu)

    = 300+253 = 553

    h(Craiova) = 160

    g(Craiova) = g(R.Vilcea)+ cost(R.Vilcea, Craiova)

    = 220+146= 366

    f(Craiova) = g(Fagaras)+h(Fagaras)

    = 366+160= 526

    h(Pitesti) = 98

    g(Pitesti) = g(R.Vilcea)+ cost(R.Vilcea, Pitesti)

    = 220+97 = 317

    f(Pitesti) = g(Oradea)+h(Oradea)

    = 317+98 = 415

    Sibiu c trong tp CLOSE. Tuy nhin, do g(Sibiu) mi (c gi tr l 553) ln

    hn g(Sibiu) (c gi tr l 393) nn ta s khng cp nht li cc gi tr ca Sibiuc lu trong CLOSE. Cn li 2 thnh ph l Pitesti v Craiova u khng ctrong c OPEN v CLOSE nn ta sa n vo OPEN v t cha ca chng lR.Vilcea.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    35/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 35

    OPEN = {(Timisoara,g= 118,h= 329,f=447,Cha= Arad)

    (Zerind,g= 75,h= 374,f=449,Cha= Arad) (Fagaras,g=239,h= 178,f=417,Cha= Sibiu)

    (Oradea,g= 291,h= 380,f=617,Cha= Sibiu)(Craiova,g= 366,h= 160,f=526,Cha= R.Vilcea)

    (Pitesti,g= 317,h= 98,f=415,Cha= R.Vilcea) }CLOSE = {(Arad,g= 0,h= 0,f= 0)

    (Sibiu,g= 140,h= 253,f=393,Cha= Arad)

    (R.Vilcea,g= 220,h= 193,f=413,Cha= Sibiu) }

    n y, trong tp OPEN, nt tt nht l Pitesti, t Pitesti ta c thi nc R.Vilcea, Bucharest v Craiova. Ly Pitesti ra khi OPEN v t n

    vo CLOSE. Thc hin tip theo tng t nh trn, ta s khng cp nhtgi tr f, g ca R.Vilcea v Craiova lu trong CLOSE. Sau khi tnh ton f,g ca Bucharest, ta sa Bucharest vo tp OPEN, t Cha(Bucharest) =Pitesti.

    h(Bucharest) = 0

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    36/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 36

    g(Bucharest) = g(Pitesti)+cost(Pitesti, Bucharest)

    = 317+100= 418

    f(Bucharest) = g(Fagaras)+h(Fagaras)

    = 417+0= 417

    bc k tip, ta s chn c Tmax = Bucharest. V nh vy thut ton kt thc(thc ra th ti bc ny, c hai ng c vin l Bucharest v Fagaras v u cngc f= 417 , nhng v Bucharest l ch nn ta su tin chn hn).

    xy dng li con ng i t Arad n Bucharest ta ln theo gi tr Cha clu tr km vi f, g v h cho n lc n Arad.

    Cha(Bucharest) = Pitesti

    Cha(R.Vilcea) = Sibiu

    Cha(Sibiu) = Arad

    Vy con ng i ngn nht t Arad n Bucharest l Arad, Sibiu, R.Vilcea,Pitesti, Bucharest.

    Trong v d minh ha ny, hm h c cht lng kh tt v cu trc th kh ngin nn ta gn nhi thng n ch m t phi kho st cc con ng khc.y l mt trng hp n gin, trong trng hp ny, thut gii c dng dp catm kim chiu su.

    n y, minh ha mt trng hp phc tp hn ca thut gii. Ta th sa ili cu trc th v quan st hot ng ca thut gii. Gi s ta c thm mtthnh ph tm gi l TP v con ng gia Sibiu v TP c chiu di 100, conng gia TP v Pitesti c chiu di 60. V khong cch ng chim bay t TPn Bucharest l 174. Nh vy r rng, con ng ti u n Bucharest khng cnl Arad, Sibiu, R.Vilcea, Pitesti, Bucharest na m l Arad, Sibiu, TP, Pitesti,

    Bucharest.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    37/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 37

    Trong trng hp ny, chng ta vn tin hnh bc 1 nhtrn. Sau khi thchin hin bc 2 (mrng Sibiu), chng ta c cy tm kim nh hnh sau. Lu l c thm nhnh TP.

    R.Vilcea vn c gi tr f thp nht. Nn ta mrng R.Vilcea nh trng hp utin.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    38/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 38

    Bc k tip ca trng hp n gin l mrng Pitesti c c kt qu. Tuy

    nhin, trong trng hp ny, TP c gi tr f thp hn. Do , ta chn mrng TP.T TP ta ch c 2 hng i, mt quay li Sibiu v mt n Pitesti. nhanhchng, ta s khng tnh ton gi tr ca Sibiu v bit chc n s ln hn gi trc lu tr trong CLOSE (v i ngc li).

    h(Pitesti) = 98

    g(Pitesti) = g(TP)+cost(TP, Pitesti)

    = 240+75= 315

    f(Pitesti) = g(TP)+h(Pitesti) = 315+98= 413

    Pistestti xut hin trong tp OPEN v g(Pitesti) mi (c gi tr l 315) thp hng(Pitesti) c (c gi tr 317) nn ta phi cp nht li gi tr ca f,g, Cha caPitesti lu trong OPEN. Sau khi cp nht xong, tp OPEN v CLOSE s nh sau :

    OPEN = {(Timisoara,g= 118,h= 329,f=447,Cha= Arad)

    (Zerind,g= 75,h= 374,f=449,Cha= Arad)

    (Fagaras,g= 239,h= 178,f=417,Cha= Sibiu)

    (Oradea,g= 291,h= 380,f=617,Cha= Sibiu)

    (Craiova,g= 366,h= 160,f=526,Cha= R.Vilcea)

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    39/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 39

    (Pitesti,g=315,h=98,f=413,Cha=TP) }

    CLOSE = {(Arad,g= 0,h= 0,f= 0)

    (Sibiu,g= 140,h= 253,f=393,Cha= Arad)

    (R.Vilcea,g= 220,h= 193,f=413,Cha= Sibiu)

    }

    n y ta thy rng, ban u thut gii chn ng i n Pitesti qua R.Vilcea.Tuy nhin, sau , thut gii pht hin ra con ng n Pitesti qua TP l tt hnnn n s s dng con ng ny. y chnh l trng hp 2.b.iii.2 trong thutgii.

    Bc sau, chng ta s chn mrng Pitesti nh bnh thng. Khi ln ngc theothuc tnh Cha, ta s c con ng ti u l Arad, Sibiu, TP, Pitesti, Bucharest.

    III.9. Bn lun v A*

    n y, c l bn hiu c thut gii ny. Ta c mt vi nhn xt kh th vv A*. u tin l vai tr ca g trong vic gip chng ta la chn ng i. Ncho chng ta kh nng la chn trng thi no mrng tip theo, khng ch datrn vic trng thi tt nh th no (th hin bi gi trh) m cn trn cscon ng t trng thi khi u n trng thi hin ti tt ra sao. iu ny s

    rt hu ch nu ta khng ch quan tm vic tm ra li gii hay khng m cn quantm n hiu qu ca con ng dn n li gii. Chng hn nh trong bi tontm ng i ngn nht gia hai im. Bn cnh vic tm ra ng i gia haiim, ta cn phi tm ra mt con ng ngn nht. Tuy nhin, nu ta ch quan tmn vic tm c li gii (m khng quan tm n hiu qu ca con ng nli gii), chng ta c tht g=0 mi trng thi. iu ny s gip ta lun chn itheo trng thi c v gn nht vi trng thi kt thc (v lc ny f ch ph thucvo h l hm c lng "khong cch" gn nht ti ch). Lc ny thut giic dng dp ca tm kim chiu su theo nguyn l hng ch kt hp vi lnngc.

    Ngc li, nu ta mun tm ra kt qu vi s bc t nht (t c trng thich vi s trng thi trung gian t nht), th ta t gi tri t mt trng thin cc trng thi con k tip ca n lun l hng s, thng l 1 Ngha tcost(Ti-1, Ti) = 1 (v vn dng mt hm c lng h nh bnh thng). Cnngc li, nu mun tm chi ph r nht th ta phi t gi tr hm cost chnh xc(phn nh ng ghi ph thc s).

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    40/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 40

    n y, chc bn c c th bt u cm nhn c rng thut gii A* khnghon ton l mt thut gii ti u tuyt i. Ni ng hn, A* ch l mt thut giilinh ng v cho chng ta kh nhiu ty chn. Ty theo bi ton m ta s c mtb thng s thch hp cho A* thut gii hot ng hiu qu nht.

    im quan tm th hai l v gi trh sc lng khong cch (chi ph) tmt trng thi n trng thi ch. Nu h chnh l h (nh gi tuyt i chnh xc)th A* si mt mch t trng thi u n trng thi kt thc m khng cn phithc hin bt k mt thao tc i hng no!. Dnhin, trn thc t, hu nhchng bao gita tm thy mt nh gi tuyt i chnh xc. Tuy nhin, iu ngquan tm y l hc c lng cng gn vi h, qu trnh tm kim cng t bsai st, t b r vo nhng nhnh ct hn. Hay ni ngn gn l cng nhanh chngtm thy li gii hn.

    Nu h lun bng 0 mi trng thi (trv thut gii AT) th qu trnh tm kim

    sc iu khin hon ton bi gi trg. Ngha l thut gii s chn i theonhng hng m s tn t chi ph/bc i nht (chi ph tnh t trng thi u tinn trng thi hin ang xt) bt chp vic i theo hng c kh nng dn nli gii hay khng. y chnh l hnh nh ca nguyn l tham lam (Greedy).

    Nu chi ph t trng thi sang trng thi khc lun l hng s (dnhin lc ny hlun bng 0) th thut gii A* trthnh thut gii tm kim theo chiu rng! L dol v tt c nhng trng thi cch trng thi khi u n bc u c cng gi trgv v thu c cng f v gi tr ny s nh hn tt c cc trng thi cch trngthi khi u n+1 bc. V nu g lun bng 0 v h cng lun bng 0, mi trngthi ang xt u tng ng nhau. Ta ch c th chn bng trng thi k tipbng ngu nhin !

    Cn nu nhh khng th tuyt i chnh xc (ngha l khng bng ng h) vcng khng lun bng 0 th sao? C iu g th v v cch x l ca qu trnh tmkim hay khng? Cu tr li l c. Nu nh bng mt cch no , ta c th chcchn rng, c lng h lun nh hn h (i vi mi trng thi) th thut gii A*s thng tm ra con ng ti u (xc nh bi g) i n ch, nu ng dn tn ti v qu trnh tm kim s t khi b sa ly vo nhng con ng qu d.Cn nu v mt l do no , c lng h li ln hn h th thut gii s d dngb vng vo nhng hng tm kim v ch. Thm ch n li c khuynh hng tm

    kim nhng hng i v ch trc! iu ny c th thy mt cch d dng t viv d.

    Xt trng hp c trnh by trong hnh sau. Gi s rng tt c cc cung u cgi tr 1. G l trng thi ch. Khi u, OPEN ch cha A, sau A c mrng nn B, C, D sc a vo OPEN (hnh v m t trng thi 2 bc sau ,khi B v E c mrng). i vi mi nt, con su tin l gi trh, con s

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    41/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 41

    k tip l g. Trong v d ny, nt B c f thp nht l 4 = h+g = 3 + 1 , v th nc mrng trc tin. Gi s n ch c mt nt con tip theo l E v h(E) = 3,do E cc A hai cung nn g(E) = 2 suy ra f(E) = 5, ging nh f(C). Ta chn mrng E k tip. Gi s n cng ch c duy nht mt con k tip l F v h(F) cngbng 3. R rng l chng ta ang di chuyn xung v khng pht trin rng.

    Nhng f(F) = 6 ln hn f(D). Do , chng ta s mrng C tip theo v t ntrng thi ch. Nh vy, ta thy rng do nh gi thp h(B) nn ta lng ph mts bc (E,F), nhng cui cng ta cng pht hin ra B khc xa vi iu ta mongi v quay li th mt ng dn khc.

    Hnh : h nh gi thp h

    By gihy xt trng hp hnh tip theo. Chng ta cng mrng B bc utin v E bc th hai. K tip l F v cui cng G, cho ng dn kt thc c di l 4. Nhng gi s c ng dn trc tip t D n mt li gii c di hthc sl 2 th chng ta skhng bao gitm c ng dn ny (tuy rng ta cth tm thy li gii). Bi v vic nh gi qu cao h(D), chng ta s lm cho Dtrng dn ni m ta phi tm mt ng i khc n mt li gii t hn - mkhng bao ginghn vic mrng D. Ni chung, nu hnh gi cao h th A*s c th khng th tm ra ng dn ti u n li gii (nu nh c nhiu ngdn n li gii). Mt cu hi th v l "Liu c mt nguyn tc chung no gipchng ta a ra mt cch c lng h khng bao ginh gi cao h haykhng?". Cu tr li l "hu nh khng", bi v i vi hu ht cc vn thc tau khng bit h. Tuy nhin, cch duy nht bo m h khng bao ginh gicao h l t h bng 0 !

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    42/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 42

    Hnh : h nh gi cao h

    n y chng ta kt thc vic bn lun v thut gii A*, mt thut gii linhng, tng qut, trong hm cha c tm kim chiu su, tm kim chiu rng vnhng nguyn l Heuristic khc. Chnh v th m ngi ta thng ni, A* chnh lthut gii tiu biu cho Heuristic.

    A* rt linh ng nhng vn gp mt khuyt im cbn ging nh chin lctm kim chiu rng l tn kh nhiu b nh lu li nhng trng thi iqua nu chng ta mun n chc chn tm thy li gii ti u. Vi nhng khnggian tm kim ln nh th y khng phi l mt im ng quan tm. Tuy nhin,vi nhng khng gian tm kim khng l (chng hn tm ng i trn mt matrn kch thc c106 x 106) th khng gian lu tr l c mt vn hc ba. Ccnh nghin cu a ra kh nhiu cc hng tip cn lai gii quyt vn ny. Chng ta s tm hiu mt s phng n nhng quan trng nht, ta cn phinm r v tr ca A* so vi nhng thut gii khc.

    III.10. ng dng A* gii bi ton Ta-canh

    Bi ton Ta-canh tng l mt tr chi kh ph bin, i lc ngi ta cn giy l bi ton 9-puzzle. Tr chi bao gm mt hnh vung kch thc 3x3 . C8 c s, mi c mt s t 1 n 8. Mt cn trng. Mi ln di chuyn ch

    c di chuyn mt nm cnh trng v pha trng. Vn l t mt trngthi ban u bt k, lm sao a c v trng thi cui l trng thi m cc c sp ln lt t 1 n 8 theo th t t tri sang phi, t trn xung di, cui dng l trng.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    43/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 43

    Cho n nay, ngoi tr 2 gii php vt cn v tm kim Heuristic, ngi ta vncha tm c mt thut ton chnh xc, ti u gii bi ton ny. Tuy nhin,cch gii theo thut gii A* li kh n gin v thng tm c li gii (nhngkhng phi lc no cng tm c li gii). Nhn xt rng: Ti mi thi im tach c ti a 4 c th di chuyn. Vn l ti thi im , ta s chn la dichuyn no? Chng hn hnh trn, ta nn di chuyn (1), (2), (6), hay (7) ? Biton ny hon ton c cu trc thch hp c th gii bng A* (tng s trng thic th c ca bn cl n2! vi n l kch thc bn cv mi trng thi l mt honv ca tp n2 con s).

    Ti mt trng thi ang xt Tk, t d(i,j)l s cn di chuyn a con s(i,j) vng v tr ca n trng thi ch.

    Hm c lng h ti trng thi Tk bt k bng tng ca cc d(i,j) sao cho v tr(i,j) khng phi l trng.

    Nh vy i vi trng thi hnh ban u, hm f(Tk) s c gi tr l

    Fk=2+1+3+1+0+1+2+2=12

    III.11. Cc chin lc tm kim lai

    Chng ta bit qua 4 kiu tm kim : leo o (L), tm theo chiu su (MC), tmtheo chiu rng (BR) v tm kim BFS. Bn kiu tm kim ny c thc xemnh 4 thi cc ca khng gian lin tc bao gm cc chin lc tm kim khcnhau. gii thch iu ny r hn, s tin hn cho chng ta nu nhn mt chinlc tm kim li gii di hai chiu sau :

    Chiu kh nng quay lui (R): l kh nng cho php quay li xem xtnhng trng thi xt n trc nu gp mt trng thi khng thi tip.

    Chiu phm vi ca snh gi (S): s cc trng thi xt n trong miquyt nh.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    44/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 44

    Hnh : Tng quan gia cc chin lc leo o, quay lui v tt nht

    Theo hng R, chng ta thy leo o nm mt thi cc (n khng cho phpquay li nhng trng thi cha c xt n), trong khi tm kim quay lui vBFS mt thi cc khc (cho php quay li tt c cc hng i cha xt n).Theo hng S chng ta thy leo o v ln ngc nm mt thi cc (ch tptrung vo mt phm vi hp trn tp cc trng thi mi to ra t trng thi hin ti)v BFS nm mt thi cc khc (trong khi BF xem xt ton b tp cc con ng c, bao gm c nhng con ng mi c to ra cng nh tt c nhng conng khng c xt ti trc y trc mi mt quyt nh).

    Nhng thi cc ny c trc quan ha bng hnh trn. Vng in m biu dinmt mt phng lin tc cc chin lc tm kim m n kt hp mt sc imca mt trong ba thi cc (leo o, chiu su, BFS) c c mt ha hp cc

    c tnh tnh ton ca chng.

    Nu chng ta khng b nhcn thit p dng thut ton BFS thun ty. Tac th kt hp BFS vi tm theo chiu su gim bt yu cu b nh. Dnhin,ci gi m ta phi tr l s lng cc trng thi c th xt n ti mt bc s nhi. Mt loi kt hp nh thc ch ra trong hnh di. Trong hnh ny, thutgii BFS c p dng ti nh ca th tm kim (biu din bng vng t tm)v tm kim theo chiu su c p dng ti y (biu din bi tam gic t nht).u tin ta p dng BFS vo trng thi ban u T0 mt cch bnh thng. BFS sthi hnh cho n mt lc no , s lng trng thi c lu tr chim dng mt

    khng gian b nhvt qu mt mc cho php no . n lc ny, ta s p dngtm kim chiu su xut pht t trng thi tt nht Tmax trong OPEN cho ti khiton b khng gian con pha "di" trng thi c duyt ht. Nu khng tmthy kt qu, trng thi Tmax ny c ghi nhn l khng dn n kt qu v ta lichn ra trng thi tt th hai trong OPEN v li p dng tm kim chiu su chocho phn khng gian pha "di" trng thi ny....

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    45/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 45

    Hnh : Chin lc lai BFS-MC trong , BFS p dng ti nh v MC ti y.

    Mt cch kt hp khc l dng tm kim chiu su ti nh khng gian tm kim vBFS c dng ti y. Chng ta p dng tm kim chiu su cho ti khi gp mttrng thi Tk m su (s trng thi trung gian) ca n vt qu mt ngng d0no . Ti im ny, thay v ln ngc trli, ta p dng kiu tm kim BFS chophn khng gian pha "di" bt u t Tk cho ti khi n tr v mt gii phphoc khng tm thy. Nu n khng tm thy kt qu, chng ta ln ngc trli vli dng BFS khi t su d0. Tham s d0 sc chn sao cho b nhdng chotm kim BFS trn khng gian "di" mc d0 s khng vt qu mt hng s chotrc. R rng ta ta khng d g xc nh c d0 (v ni chung, ta kh nh gic khng gian bi ton rng n mc no). Tuy nhin, kiu kt hp ny li cmt thun li. Phn y khng gian tm kim thng cha nhiu thng tin "bch" hn l phn nh. (Chng hn, tm ng i n khu trung tm ca thnh ph,khi cng n gn khu trung tm y th bn cng d dng tin n trung tm

    hn v c nhiu "du hiu" ca trung tm xut hin xung quanh bn!). Ngha l,cng tin v pha y ca khng gian tm kim, c lng h thng cng trnnchnh xc hn v do , cng d dn ta n kt qu hn.

    Hnh : Chin lc lai BFS-MC trong , MC p dng ti nh v BFS ti y.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    46/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 46

    Cn mt kiu kt hp phc tp hn na. Trong , BFS c thc hin cc b vchiu su c thc hin ton cc. Ta bt u tm kim theo BFS cho ti khi mts lng b nhxc nh M0c dng ht. Ti im ny, chng ta xem tt cnhng trng thi trong OPEN nh nhng trng thi con trc tip ca trng thi banu v chuyn giao chng cho tm kim chiu su. Tm kim chiu su s chn

    trng thi tt nht trong nhng trng thi con ny v "bnh trng" n dng BFS,ngha l n chuyn trng thi chn cho tm kim BFS cc b cho n khi mtlng b nhM0 li c dng ht v trng thi con mi trong OPEN li tip tcc xem nh nt con ca nt "bnh trng"...Nu vic "bnh trng" bng BFStht bi th ta quay lui li v chn nt con tt th hai ca tp OPEN trc , rili tip tc bnh trng bng BFS...

    Hnh : Chin lc lai BFS-MC trong , BFS c p dng cc b v chiu suc p dng ton cc.

    C mt cch phi hp ni ting khc c gi l tm kim theo giai on cthc hin nh sau. Thay v lu tr trong b nhton b cy tm kim c sinh rabi BFS, ta ch gi li cy con c trin vng nht. Khi mt lng b nhM0cdng ht, ta snh du mt tp con cc trng thi trong OPEN (nhng trng thic gi tr hm f thp nht) gi li; nhng ng i tt nht qua nhng trng thiny cng sc ghi nhv tt c phn cn li ca cy b loi b. Qu trnh tmkim sau s tip tc theo BFS cho ti khi mt lng b nhM0 li c dnght v c th. Chin lc ny c thc xem nh l mt s lai ghp gia BF vleo o. Trong , leo o thun ty loi b tt c nhng ch gi li phng n ttnht cn tm kim theo giai on loi b tt c nhng ch gi li tp cc phng

    n tt nht.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    47/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 47

    A. TNG QUAN TR TU NHN TO

    I. MU

    Ch to c nhng c my thng minh nh con ngi (thm ch thng minh hn

    con ngi) l mt c mchy bng ca loi ngi t hng ngn nm nay. Hnbn c cn nhn nh khoa hc Alan Turing cng nhng ng gp to ln cang trong lnh vc tr tu nhn to. Nng lc my tnh ngy cng mnh m l mtiu kin ht sc thun li cho tr tu nhn to. iu ny cho php nhng chngtrnh my tnh p dng cc thut gii tr tu nhn to c kh nng phn ng nhanhv hiu qu hn trc. S kin my tnh Deep Blue nh bi kin tng cvuath gii Casparov l mt minh chng hng hn cho mt bc tin di trong cngcuc nghin cu v tr tu nhn to. Tuyc thnh bi c Casparov nhngDeep Blue l mt c my chbit nh c! N thm ch khng c c tr thngminh sng ca mt a b bit ln ba nh nhn din c nhng ngi thn,

    kh nng quan st nhn bit th gii, tnh cm thng, ght, ... Ngnh tr tu nhnto c nhng bc tin ng k, nhng mt tr tu nhn to thc s vn ch ctrong nhng b phim khoa hc gi tng ca Hollywood. Vy th ti sao chng tavn nghin cu v tr tu nhn to? iu ny cng tng t nhc mch tovng ca cc nh gi kim thut thi Trung C, tuy cha thnh cng nhng chnhqu trnh nghin cu lm sng t nhiu vn .

    Mc d mc tiu ti thng ca ngnh TTNT l xy dng mt chic my c nnglc t duy tng t nh con ngi nhng kh nng hin ti ca tt c cc snphm TTNT vn cn rt khim tn so vi mc tiu ra. Tuy vy, ngnh khoa

    hc mi m ny vn ang tin b mi ngy v ang t ra ngy cng hu dngtrong mt s cng vic i hi tr thng minh ca con ngi. Hnh nh sau s gipbn hnh dung c tnh hnh ca ngnh tr tu nhn to.

    Trc khi bc vo tm hiu v tr tu nhn to, chng ta hy nhc li mt nhngha c nhiu nh khoa hc chp nhn.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    48/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 48

    Mc tiu ca ngnh khoa hc tr tu nhn to ?

    To ra nhng chic my tnh c kh nng nhn thc, suy lun v phn ng.

    Nhn thc c hiu l kh nng quan st, hc hi, hiu bit cng nh nhng kinh

    nghim v th gii xung quanh. Qu trnh nhn thc gip con ngi c tri thc.Suy lun l kh nng vn dng nhng tri thc sn c phn ng vi nhng tnhhung hay nhng vn - bi ton gp phi trong cuc sng. Nhn thc v suylun t a ra nhng phn ng thch hp l ba hnh vi c th ni l ctrng cho tr tu ca con ngi. (Dnhin cn mt yu t na l tnh cm. Nhngchng ta s khng cp n y!). Do , cng khng c g ngc nhin khimun to ra mt chic my tnh thng minh, ta cn phi trang b cho n nhng khnng ny. C ba kh nng ny u cn n mt yu t cbn l tri thc.

    Di gc nhn ca tp sch ny, xy dng tr tu nhn to l tm cch biu din

    tri thc, tm cch vn dng tri thc gii quyt vn v tm cch b sungtri thc bng cch "pht hin" tri thc t cc thng tin sn c (my hc).

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    49/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 49

    II. THNG TIN, DLIU V TRI THC

    Tri thc l mt khi nim rt tru tng. Do , chng ta s khng c gng a ramt nh ngha hnh thc chnh xc y. Thay vo , chng ta hy cng nhaucm nhn khi nim "tri thc" bng cch so snh n vi hai khi nim khc l

    thng tin v d liu.

    Nh bc hc ni ting Karan Sing tng ni rng "Chng ta ang ngp chmtrongbin thng tin nhng li angkht tri thc". Cu ni ny lm ni bt skhc bit v lng ln v cht gia hai khi nim thng tin v tri thc.

    Trong ng cnh ca ngnh khoa hc my tnh, ngi ta quan nim rng dliu lcc con s, ch ci, hnh nh, m thanh... m my tnh c th tip nhn v x l.Bn thn d liu thng khng c ngha i vi con ngi. Cn thng tin l ttc nhng g m con ngi c th cm nhn c mt cch trc tip thng qua cc

    gic quan ca mnh (khu gic, v gic, thnh gic, xc gic, th gic v gic quanth 6) hoc gin tip thng qua cc phng tin k thut nh tivi, radio, cassette,...Thng tin i vi con ngi lun c mt ngha nht nh no . Vi phngtin my tnh (m c th l cc thit bu ra), con ngi s tip thu c mtphn d liu c ngha i vi mnh. Nu so v lng, d liu thng nhiu hnthng tin.

    Cng c th quan nim thng tin l quan h gia cc d liu. Cc d liu c spxp theo mt th t hoc c tp hp li theo mt quan h no s cha ngthng tin. Nu nhng quan h ny c ch ra mt cch r rng th l cc tri

    thc. Chng hn :Trong ton hc :

    Bn thn tng con s ring l nh 1, 1, 3, 5, 2, 7, 11, ... l cc d liu. Tuy nhin,khi t chng li vi nhau theo trt t nh di y th gia chng bt u cmt mi lin h

    D liu : 1, 1, 2, 3, 5, 8, 13, 21, 34, ....

    Mi lin h ny c thc biu din bng cng thc sau : Un = Un-1 + Un-2.

    Cng thc nu trn chnh l tri thc.

    Trong vt l :

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    50/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 50

    Bn sau y cho chng ta bit so vin tr(R), in th (U) v cng dng in (I) trong mt mch in.

    I U R

    5 10 2

    2.5 20 8

    4 12 3

    7.3 14.6 2

    Bn thn nhng con s trong cc ct ca bn trn khng c my ngha nu tatch ri chng ta. Nhng khi t k nhau, chng cho thy c mt s lin h no

    . V mi lin h ny c thc din t bng cng thc n gin sau :

    Cng thc ny l tri thc.

    Trong cuc sng hng ngy :

    Hng ngy, ngi nng dn vn quan st thy cc hin tng nng, ma, rm vchun chun bay. Rt nhiu ln quan st, h c nhn xt nh sau :

    Chun chun bay thp th ma, bay cao th nng, bay va th rm.

    Li nhn xt trn l tri thc.

    C quan im trn cho rng ch nhng mi lin h tng minh(c th chngminh c) gia cc d liu mi c xem l tri thc. Cn nhng mi quan hkhng tng minhth khng c cng nhn. y, ta cng c th quan nim

    rng, mi mi lin hgia cc d liu u c th c xem l tri thc, bi v,nhng mi lin h ny thc s tn ti. im khc bit l chng ta cha phthin ra n m thi. R rng rng "d sao th tri t cng vn xoay quanh mttri" d tri thc ny c c Galil pht hin ra hay khng!

    Nh vy, so vi d liu th tri thc c s lng thn rt nhiu. Thut ngtykhng chn gin l mt du nh hn bnh thng m l skt tinh hoc cng li. Bn hy hnh dung d liu nh l nhng im trn mt phng cn tri

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    51/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 51

    thc chnh lphng trnh ca ng cong ni tt c nhng im ny li. Ch cnmtphng trnh ng cong ta c th biu din c v sim!. Cng vy,chng ta cn c nhng kinh nghim, nhn xt t hng ng s liu thng k, nukhng, chng ta sngp chm trong bin thng tin nh nh bc hc Karan Sing cnh bo!.

    Ngi ta thng phn loi tri thc ra lm cc dng nh sau :

    Tri thc skin : l cc khng nh v mt s kin, khi nim no (trongmt phm vi xc nh). Cc nh lut vt l, ton hc, ... thng c xp vo loiny. (Chng hn : mt tri mc ng ng, tam gic u c 3 gc 600, ...)

    Tri thc th tc : thng dng din t phng php, cc bc cn tin hnh,trnh t hay ngn gn l cch gii quyt mt vn . Thut ton, thut gii l mtdng ca tri thc th tc.

    Tri thc m t : cho bit mt i tng, s kin, vn , khi nim, ... cthy, cm nhn, cu to nh th no (mt ci bn thng c 4 chn, con ngi c2 tay, 2 mt,...)

    Tri thc Heuristic : l mt dng tri thc cm tnh. Cc tri thc thuc loi nythng c dng c lng, phng on, v thng c hnh thnh thng quakinh nghim.

    Trn thc t, rt him c mt tr tu m khng cn n tri thc (liu c th c mti kin tng cvua m khng bit nh choc khng bit cc th cquan

    trng khng?). Tuy tri thc khng quyt nh s thng minh (ngi bit nhiunh l ton hn cha chc gii ton gii hn!) nhng n l mt yu t cbncu thnh tr thng minh. Chnh v vy, mun xy dng mt tr thng minh nhnto, ta cn phi c yu t cbn ny. Ty t ra vn u tin l Ccphng php a tri thc vo my tnh c gi l biu din tri thc.

    III. THUT TON MT PHNG PHP BIU DIN TRITHC?

    Trc khi tr li cu hi trn, bn hy th nghxem, liu mt chng trnh gii

    phng trnh bc 2 c thc xem l mt chng trnh c tri thc hay khng? ...C ch ! Vy th tri thc nm u? Tri thc v gii phng trnh bc hai thccht c m ha di dng cc cu lnh if..then..else trong chng trnh. Mtcch tng qut, c th khng nh l tt c cc chng trnh my tnh t nhiu u c tri thc. chnh l tri thc ca lp trnh vin c chuyn thnh cc culnh ca chng trnh. Bn s thc mc "nhvy ti sao a tri thc vo my tnhli l mt vn ? (v ttrc ti gichng ta , ang v stip tc lm nhth

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    52/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 52

    m?)". ng nh th tht, nhng vn nm ch, cc tri thc trong nhngchng trnh truyn thng l nhng tri thc "cng", ngha l n khng thcthm vo hay iu chnh mt khi chng trnh c bin dch. Mun iuchnh th chng ta phi tin hnh sa li m ngun ca chng trnh (ri sau bin dch li). M thao tc sa chng trnh th ch c nhng lp trnh vin mi c

    th lm c. iu ny s lm gim kh nng ng dng chng trnh (v a sngi dng bnh thng u khng bit lp trnh).

    Bn th nghxem, vi mt chng trnh h trra quyt nh (nhu t c phiu,u t bt ng sn chng hn), liu ngi dng c cm thy thoi mi khng khimun a vo chng trnh nhng kin thc ca mnh th anh ta phi chn mttrong hai cch l (1) tsa li m chng trnh!? (2) tm tc gi ca chng trnhnhngi ny sa li!?. C hai thao tc trn u khng th chp nhn c ivi bt k ngi dng bnh thng no. H cn c mt cch no chnh h ctha tri thc vo my tnh mt cch d dng, thun tin ging nh hang i

    thoi vi mt con ngi.

    lm c iu ny, chng ta cn phi "mm" ha cc tri thc c biu dintrong my tnh. Xt cho cng, mi chng trnh my tnh u gm hai thnh phnl cc m lnh v d liu. M lnh c v nh l phn cng ca chng trnh cnd liu c xem l phn mm (v n c thc thay i bi ngi dng). Do, "mm" ha tri thc cng ng ngha vi vic tm cc phng php c thbiu din cc loi tri thc ca con ngi bng cc cu trc dliu m my tnh cth x l c. y cng chnh l ngha ca thut ng "biu din tri thc".

    Bn cn phi bit rng, t ra l cho n thi im bn ang c cun sch ny, conngi vn cha th tm ra mt kiu biu din tng qut cho mi loi tri thc!

    lm vn m chng ta ang bn lun trnn sng t hn. Chng ta hy xemxt mt s bi ton trong phn tip theo.

    IV. LM QUEN VI CCH GII QUYT VN BNGCCH CHUYN GIAO TRI THC CHO MY TNH

    Bi ton 1 : Cho hai bnh rng X v Y c th tch ln lt l VX v VY, hydng hai bnh ny ong ra z lt nc (z

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    53/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 53

    Hai bi ton u l hai bi ton kh tiu biu, thng c dng minh ha chont p ca phng php gii quyt vn bi ton bng cch chuyn giao tri thccho my tnh. Nu s dng thut ton thng thng, chng ta thng ch gii cmt s trng hp c th ca cc bi ton ny. Thm ch, nhiu ngi khi mi tipcn vi 2 bi ton ny cn khng tin l n c th hon ton c gii mt cch

    tng qut bi my tnh!. Bi ton s 3 l mt minh ha p mt cho k thut giiquyt vn "vm", ngha l ta ch cn m t cc bc gii quyt mc tngqut cho my tnh m khng cn i vo ci t c th.

    Bi ton 1 sc gii quyt bng cch s dng cc lut dn xut (lut sinh). Biton 2 sc gii quyt bng mng ng ngha v bi ton 3 s gii quyt bngcng c frame. y chng ta cng nhau tm hiu cch gii bi ton u tin. Haibi ton k tip sc gii quyt ln lt cc mc sau.

    Vi mt trng hp c th ca bi ton 1, nh VX = 5 v VY= 7 v z = 4. Sau

    mt thi gian tnh ton, bn c th sa ra mt quy trnh nc i loi nh :Mc y bnh 7

    Trt ht qua bnh 5 cho n khi 5 y.

    ht nc trong bnh 5

    ht nc cn li tbnh 7 sang bnh 5

    Mc y bnh 7

    Trt ht qua bnh 5 cho n khi bnh 5 y.

    Phn cn li chnh l snc cn ong.

    Tuy nhin, vi nhng s liu khc, bn phi "my m" li tu tm ra quytrnh nc. C th, mi mt trng hp s c mt cch nc hon ton khcnhau. Nh vy, nu c mt ai yu cu bn a ra mt cch lm tng qut thchnh bn cng s lng tng (dnhin, ngoi tr trng hp bn bit trc cchgii theo tri thc m chng ta sp sa tm hiu y!).

    n y, bn hy bnh tm kim li cch thc bn tm kim li gii cho mttrng hp c th. V cha tm ra mt quy tc c th no, bn s thc hin mtlot cc thao tc "cm tnh" nhong y mt bnh, trt mt bnh ny sang bnhkia, ht nc trong mt bnh ra... va lm va nhm tnh xem cch lm ny cthi n kt qu hay khng. Sau nhiu ln th nghim, rt c th bn s rt rac mt s kinh nghim nh"khi bnh 7y nc m bnh 5 cha y th hy n sang bnh 5 cho n khi bnh 5 y"... Vy th ti sao bn li khng th

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    54/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 54

    "truyn" nhng kinh nghim ny cho my tnh v cho my tnh "my m" tmcc thao tc cho chng ta? iu ny hon ton c li, v my tnh c kh nng"my m" hn hn chng ta! Nu nhng "kinh nghim" m chng ta cung cp chomy tnh khng gip chng ta tm c li gii, chng ta s thay th n bngnhng kinh nghim khc v li tip tc my tnh tm kim li gii!

    Chng ta hy pht biu li bi ton mt cch hnh thc hn.

    Khng lm mt tnh tng qut, ta lun c th gi s rng VX

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    55/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 55

    WHILE ( (x z) AND (yz) ) DO BEGIN

    IF (x = Vx) THEN x := 0;

    IF (y = 0) THEN (y:= Vy);

    IF (y > 0) THEN BEGIN

    k:= min(Vx - x, y);

    x := x + k;

    y := y - k;

    END;

    END;

    ...

    Th "chy" chng trnh trn vi s liu c th l :

    Vx = 3, Vy = 4 v z = 2

    Ban u : x = 0, y = 0

    Lut (L2) -> x = 0, y = 4

    Lut (L3) -> x = 3, y = 1

    Lut (L1) -> x = 0, y = 1

    Lut (L3) -> x = 1, y = 0

    Lut (L2) -> x = 1, y = 4

    Lut (L3) -> x = 3, y = 2

    3 lut m chng ta ci t trong chng trnh trn c gi l cstri thc.

    Cn cch thc tm kim li gii bng cch duyt tun t tng lut v p dng nc gi l ng csuy din. Chng ta snh ngha chnh xc hai thut ng nycui mc.

    Ngi ta chng minh c rng, bi ton ong nc ch c li gii khi s nc cn ong lmt bi s ca c s chung ln nht ca th tch hai bnh.

    z = n USCLN(VX, VY) (vi n nguyn dng)

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    56/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 56

    Cch gii quyt vn theo kiu ny khc so vi cch gii bng thut ton thngthng l chng ta khng a ra mt trnh tgii quyt vn c thm chara cc quy tc chung chung (di dng cc lut), my tnh s da vo (p dngcc lut) txy dng mt quy trnh gii quyt vn . iu ny cng ging nhvic chng ta gii ton bng cch a ra cc nh l, quy tc lin quan n bi ton

    m khng cn phi ch ra cch gii c th.

    Vy th im th v nm im no? Bn s c th cm thy rng chng ta vnang dng tri thc "cng" ! (v cc tri thc vn l cc cu lnh IF c ci sntrong chng trnh). Thc ra th chng trnh ca chng ta "mm" hn mt tri y. Nu khng tin, cc bn hy quan st phin bn k tip ca chng trnhny.

    FUNCTION DK(L INTEGER):BOOLEAN;

    BEGIN

    CASE L OF

    1 : DK := (x = Vx);

    2 : DK := (y = 0);

    3 : DK := (y>0);

    END;

    END;

    PROCEDURE ThiHanh(L INTEGER):BOOLEAN;

    BEGIN

    CASE L OF

    1 : x := 0;

    2: y := Vy;

    3 : BEGIN

    k := min(Vx-x,y);

    x := x+k;

    y := y-k;

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    57/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 57

    END;

    END;

    END;

    CONST SO_LUAT = 3;

    BEGIN

    WHILE (xz) AND (yz) DO BEGIN

    FOR i:=1 TO SO_LUAT DO

    IF DK(L) THEN ThiHanh(L);

    END;

    END.

    on chng trnh chnh cng thi hnh bng cch ln lt xt qua 3 lnh IF nhchng trnh u tin. Tuy nhin, y, biu thc iu kin c thay th bnghm DK v cc hnh ng ng vi iu kin c thay th bng th tcThiHanh. Tnh cht "mm" hn ca chng trnh ny th hin ch, nu mun bsung "tri thc", ta ch phi iu chnh li cc hm DK v ThiHanh m khng cnphi sa li chng trnh chnh.

    By gihy gi s rng ta c hm v th tc c bit sau :

    FUNCTION GiaTriBool(DK : String) : BOOLEAN;

    PROCEDURE ThucHien(ThaoTac : String) ;

    hm GiaTriBool nhn vo mt chuiiu kin, n s phn tch chui, tnh ton ritr ra gi tr BOOLEAN ca biu thc ny.

    V d : GiaTriBoolean(6

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    58/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 58

    Luat RECORD

    DK : String;

    ThiHanh : String;

    END;

    DSLuat ARRAY [1..SO_LUAT] OF Luat; 9;

    VAR

    CacLuat DSLuat;

    PROCEDURE KhoiDong;

    BEGIN

    CacLuat[1].DK := x = Vx;

    CacLuat[2].DK := y = 0;

    CacLuat[3].DK := y>0; 9;

    CacLuat[1].ThaoTac := x:=0;

    CacLuat[2].ThaoTac:= y:=Vy;

    CacLuat[3].ThaoTac:= k:=min(Vx-x,y), x:=x+k, y:=y-k;

    END;

    BEGIN

    WHILE (xz) AND (yz) DO BEGIN

    FOR i:=1 TO SO_LUAT DO

    IF GiaTriBoolean(CacLuat[i].DK)

    THEN ThucHien(CacLuat[i].ThaoTac);

    END;

    END.

    Chng ta tm cho rng trong qu trnh chng trnh thi hnh, ta c th d dngthay i s phn t mng CacLuat (cc ngn ng lp trnh sau ny nh Visual

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    59/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 59

    C++, Delphi u cho php iu ny). Vi chng trnh ny, khi mun sa i "trithc", bn ch cn thay i gi tr mng Luat l xong.

    Tuy nhin, ngi dng vn gp kh khn khi mun b sung hoc hiu chnh trithc. H cn phi nhp cc chui i loi nh x=0 hoc k:=min(Vx-x,y) ...Cc

    chui ny, tuy c ngha i vi chng trnh nhng vn cn kh xa li vingi dng bnh thng. Chng ta cn gim bt "khong cch" ny li bng ccha ra nhng chui iu kin hoc thao tc c ngha trc tipi vi ngidng. Chng trnh s c chuyn i li cc iu kin v thao tc ny sang dngph hp vi chng trnh.

    lm c iu trn. Chng ta cn phi lit k c cc trng thi v thao tc cbn ca bi ton ny. Sau y l mt s trng thi v thao tc cbn.

    Trng thi cbn :

    Bnh X y, Bnh X rng, Bnh X khng rng, Bnh X c n lt nc.

    Thao tc

    ht nc trong bnh, y nc trong bnh, nc t bnh A sang bnh Bcho n khi B y hoc A rng.

    Lu rng ta khng th c thao tc " n lt nc t A sang B" v bi ton ginhrng cc bnh u khng c vch chia, hn na nu ta bit cch n lt nc t A sang Bth li gii bi ton trthnh qu n gin.

    "Mc y X"

    " z lt nc t X sang Y"

    V y l mt bi ton n gin nn bn c th d nhn thy rng, cc trng thi cbnv thao tc chng c g khc so vi cc iu kin m chng ta a ra.

    K tip, ta s vit cc on chng trnh cho php ngi dng nhp vo cc lut(dng nu ... th ...) c hnh thnh t cc trng thi v iu kin cbn ny,ng thi tin hnh chuyn sang dng my tnh c th x l c nhv d trn.

    Chng ta s khng bn n vic ci t cc on chng trnh giao tip vi ngidng y.

    Nh vy, so vi chng trnh truyn thng (c cu to t hai "cht liu" cbnldliu vthut ton), chng trnh tr tu nhn to c cu to t hai thnhphn lcstri thc (knowledge base) v ng csuy din (inference engine).

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    60/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 60

    Cstri thc : l tp hp cc tri thc lin quan n vn m chng trnhquan tm gii quyt.

    ng csuy din : l phng php vn dng tri thc trong cstri thc gii quyt vn .

    Nu xt theo quan nim biu din tri thc m ta va bn lun trn th cstrithc ch l mt dng d liu c bit v ng csuy din cng ch l mt dng cathut ton c bit m thi. Tuy vy, c th ni rng, cstri thc v ng csuydin l mt bc tin ha mi ca d liu v thut ton ca chng trnh! Bn cth hnh dung ng csuy din ging nh mt loi ng ctng qut, c chunha c th dng vn hnh nhiu loi xe mykhc nhau v cstri thc chnhl loi nhin liu c bit vn hnh loi ng cny !

    Cstri thc cng gp phi nhng vn tng t nh nhng csd liu khcnh s trng lp, tha, mu thun. Khi xy dng cstri thc, ta cng phi ch n nhng yu t ny. Nh vy, bn cnh vn biu din tri thc, ta cn phi ra cc phng php loi b nhng tri thc trng lp, tha hoc mu thun.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    61/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 61

    Nhng thao tc ny sc thc hin trong qu trnh ghi nhn tri thc vo hthng. Chng ta s cp n nhng phng php ny trong phn tm hiu v cclut dn.

    Hnh nh trn tm tt cho chng ta thy cu trc chung nht ca mt chng trnh

    tr tu nhn to.

    B. CC PHNG PHP BIU DIN TRI THC TRN MYTNH

    V. LOGIC MNH

    y c l l kiu biu din tri thc n gin nht v gn gi nht i vi chng ta.Mnh l mt khng nh, mt pht biu m gi tr ca n ch c th hoc lng hoc l sai.

    V d :

    pht biu "1+1=2" c gi trng.

    pht biu "Mi loi c c th sng trn b" c gi tr sai.

    Gi tr ca mnh khng ch ph thuc vo bn thn mnh . C nhngmnh m gi tr ca n lun ng hoc sai bt chp thi gian nhng cng cnhng mnh m gi tr ca n li ph thuc vo thi gian, khng gian v nhiu

    yu t khc quan khc. Chng hn nh mnh : "Con ngi khng th nhy caohn 5m vi chn trn" l ng khi tri t , cn nhng hnh tinh c lc hpdn yu th c th sai.

    Ta k hiu mnh bng nhng ch ci la tinh nha, b, c, ...

    C 3 php ni cbn to ra nhng mnh mi t nhng mnh cslphp hi ( ), giao( ) v phnh ( )

    Bn c chn hn tng s dng logic mnh trong chng trnh rt nhiu ln (nhtrong cu trc lnh IF ... THEN ... ELSE) biu din cc tri thc "cng" trong my tnh

    !

    Bn cnh cc thao tc tnh ra gi tr cc mnh phc t gi tr nhng mnh con, chng ta c c mt cch suy din nh sau :

    Modus Ponens : Nu mnh A l ng v mnh A B l ng th gi trca B s l ng.

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    62/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 62

    Modus Tollens : Nu mnh A B l ng v mnh B l sai th gi tr caA s l sai.

    Cc php ton v suy lun trn mnh c cp nhiu n trong cc tiliu v ton nn chng ta s khng i vo chi tit y.

    VI. LOGIC V T

    Biu din tri thc bng mnh gp phi mt trngi cbn l ta khng th canthip vo cu trc ca mt mnh . Hay ni mt cch khc l mnh khng ccu trc . iu ny lm hn ch rt nhiu thao tc suy lun . Do , ngi ta a vo khi nim v t v lng t ( - vi mi, - tn ti) tng cng tnhcu trc ca mt mnh .

    Trong logic v t, mt mnh c cu to bi hai thnh phn l cc i tng

    tri thc v mi lin h gia chng (gi l v t). Cc mnh sc biu dindi dng :

    V t(, , , )

    Nh vy biu din v ca cc tri cy, cc mnh sc vit li thnh :

    Cam c v Ngt V (Cam, Ngt)

    Cam c mu Xanh Mu (Cam, Xanh)

    ...

    Kiu biu din ny c hnh thc tng t nh hm trong cc ngn nglp trnh, cc i tng tri thc chnh l cc tham s ca hm, gi trmnh chnh l kt qu ca hm (thuc kiu BOOLEAN).

    Vi v t, ta c th biu din cc tri thc di dng cc mnh tng qut, lnhng mnh m gi tr ca n c xc nh thng qua cc i tng tri thccu to nn n.

    Chng hn tri thc : "A l bca B nu B l anh hoc em ca mt ngi con caA" c thc biu din di dng v t nh sau :

    B (A, B) = Tn ti Z sao cho : B (A, Z) v (Anh(Z, B) hoc Anh(B,Z))

    Trong trng hp ny, mnh B(A,B) l mt mnh tng qut

    Nh vy nu ta c cc mnh csl :

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    63/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 63

    a) B ("An", "Bnh") c gi trng (Anh l b ca Bnh)

    b) Anh("T", "Bnh") c gi trng (T l anh ca Bnh)

    th mnh c) B ("An", "T") s c gi tr l ng. (An l b ca T).

    R rng l nu ch s dng logic mnh thng thng th ta s khng th tmc mt mi lin h no gia c v a,b bng cc php ni mnh , , . T, ta cng khng th tnh ra c gi tr ca mnh c. Sdnh vy v ta khngth th hin tng minh tri thc "(A l bca B) nu c Z sao cho (A l bca Z)v (Z anh hoc em C)" di dng cc mnh thng thng. Chnh c trng cav t cho php chng ta th hin c cc tri thc dng tng qut nh trn.

    Thm mt s v d na cc bn thy r hn kh nng ca v t :

    Cu cch ngn "Khng c vt g l ln nht v khng c vt g l b nht!" c th

    c biu din di dng v t nh sau :

    LnHn(x,y) = x>y

    NhHn(x,y) = x

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    64/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 64

    Vi cng c my tnh, bn c th cho rng ta s d dng chng minh c mi biton bng mt phng php "th bo" l lp bng chn tr . Tuy v l thuyt,phng php lp bng chn tr lun cho c kt qu cui cng nhng phctp ca phng php ny l qu ln, O(2n) vi n l s bin mnh . Sau ychng ta s nghin cu hai phng php chng minh mnh vi phc tp ch

    c O(n).

    VII.1. Thut gii Vng Ho

    B1 : Pht biu li gi thit v kt lun ca vn theo dng chun sau :

    GT1, GT2, ..., GTn KL1, KL2, ..., KLm

    Trong cc GTi v KLi l cc mnh c xy dng t cc bin mnh v 3php ni cbn : , ,

    B2 : Chuyn v cc GTi v KLi c dng phnh.

    V d :

    p q, (r s), g, p r s, p

    p q, p r, p (r s), g, s

    B3 : Nu GTi c php th thay th php bng du ","

    Nu KLi c php th thay th php bng du ","

    V d :

    p q, r ( p s) q, s

    p, q, r, p s q, s

    B4 : Nu GTi c php th tch thnh hai dng con.

    Nu KLi c php th tch thnh hai dng con.

    V d :

    p, p q q

    p, p q p, q q

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    65/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 65

    B5 : Mt dng c chng minh nu tn ti chung mt mnh c hai pha.

    V d :

    p, qqc chng minh

    p, p qp p, q

    B6 :

    a) Nu mt dng khng cn php ni hoc c hai v v 2 v khng cchung mt bin mnh th dng khng c chng minh.

    b) Mt vn c chng minh nu tt c dng dn xut t dng chun ban uu c chng minh.

    VII.2 Thut gii Robinson

    Thut gii ny hot ng da trn phng php chng minh phn chng.

    Phng php chng minh phn chng

    Chng minh php suy lun (a b) l ng (vi a l gi thit, b l kt lun).

    Phn chng : gi s b sai suy ra b l ng.

    Bi ton c chng minh nu a ng v b ng sinh ra mt mu thun.

    B1 : Pht biu li gi thit v kt lun ca vn di dng chun nh sau :

    GT1, GT2, ...,GTn KL1, KL2, .., KLm

    Trong : GTi v KLj c xy dng t cc bin mnh v cc php ton : , ,

    B2 : Nu GTi c php th thay bng du ","

    Nu KLi c php th thay bng du ","

    B3 : Bin i dng chun B1 v thnh danh sch mnh nh sau :

    { GT1, GT2, ..., GTn , KL1, KL2, ..., KLm }

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    66/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 66

    B4 : Nu trong danh sch mnh bc 2 c 2 mnh i ngu nhau th biton c chng minh. Ngc li th chuyn sang B4. (a v a gi l hai mnh i ngu nhau)

    B5 : Xy dng mt mnh mi bng cch tuyn mt cp mnh trong danh

    sch mnh bc 2. Nu mnh mi c cc bin mnh i ngu nhau thcc bin c loi b.

    V d : p q r s q

    Hai mnh q, q l i ngu nn sc loi b

    p r s

    B6 : Thay th hai mnh va tuyn trong danh sch mnh bng mnh mi.

    V d :

    { p q , r s q , w r, s q }

    { p r s , w r, s q }

    B7 : Nu khng xy dng c thm mt mnh mi no v trong danh schmnh khng c 2 mnh no i ngu nhau th vn khng c chngminh.

    V d : Chng minh rng

    p q, q r, r s, u s p, u

    B3: {p q, q r, r s, u s, p, u }

    B4 : C tt c 6 mnh nhng cha c mnh no i ngu nhau.

    B5 :tuyn mt cp mnh (chn hai mnh c bin i ngu). Chn haimnh u :

    p qq r p r

    Danh sch mnh thnh :

    { p r, r s, u s, p, u }

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    67/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 67

    Vn cha c mnh i ngu.

    Tuyn hai cp mnh u tin

    p r r s p s

    Danh sch mnh thnh { p s, u s, p, u }

    Vn cha c hai mnh i ngu

    Tuyn hai cp mnh u tin

    p s u s p u

    Danh sch mnh thnh : { p u, p, u }

    Vn cha c hai mnh i ngu

    Tuyn hai cp mnh :

    p u u p

    Danh sch mnh trthnh : { p, p }

    C hai mnh i ngu nn biu thc ban u c chng minh.

    VIII. BIU DIN TRI THC SDNG LUT DN XUT(LUT SINH)

    VIII.1. Khi nim

    Phng php biu din tri thc bng lut sinh c pht minh bi Newell vSimon trong lc hai ng ang c gng xy dng mt h gii bi ton tng qut.y l mt kiu biu din tri thc c cu trc. tng cbn l tri thc c thc cu trc bng mt cp iu kin hnh ng : "NU iu kin xy ra THhnh ng sc thi hnh". Chng hn : NU n giao thng l TH bn

    khng c i thng, NU my tnh mm khng khi ng c TH kimtra ngun in,

    Ngy nay, cc lut sinh trnn ph bin v c p dng rng ri trong nhiuh thng tr tu nhn to khc nhau. Lut sinh c th l mt cng c m t giiquyt cc vn thc t thay cho cc kiu phn tch vn truyn thng. Trongtrng hp ny, cc lut c dng nh l nhng ch dn (tuy c th khng hon

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    68/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 68

    chnh) nhng rt hu ch trgip cho cc quyt nh trong qu trnh tm kim,t lm gim khng gian tm kim. Mt v d khc l lut sinh c thc dng bt chc hnh vi ca nhng chuyn gia. Theo cch ny, lut sinh khng chn thun l mt kiu biu din tri thc trong my tnh m l mt kiu biu dincc hnh vi ca con ngi.

    Mt cch tng qut lut sinh c dng nh sau :

    P1 P2 ... Pn Q

    Ty vo cc vn ang quan tm m lut sinh c nhng ng ngha hay cu tokhc nhau :

    Trong logic v t : P1, P2, ..., Pn, Q l nhng biu thc logic.

    Trong ngn ng lp trnh, mi mt lut sinh l mt cu lnh.IF (P1 AND P2 AND .. AND Pn) THEN Q.

    Trong l thuyt hiu ngn ng t nhin, mi lut sinh l mt php dch :

    ONE mt.

    TWO hai.

    JANUARY thng mt

    biu din mt tp lut sinh, ngi ta thng phi ch r hai thnh phn chnhsau :

    (1) Tp cc s kin F(Facts)

    F = { f1, f2, ... fn}

    (2) Tp cc quy tc R (Rules) p dng trn cc s kin dng nh sau :

    f1 ^ f2 ^ ... ^ fi qTrong , cc fi , q u thuc F

    V d : Cho 1 cstri thc c xc nh nh sau :

    Cc s kin : A, B, C, D, E, F, G, H, K

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    69/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 69

    Tp cc quy tc hay lut sinh (rule)

    R1 : A E

    R2 : B D

    R3 : H A

    R4 : E G C

    R5 : E K B

    R6 : D E K C

    R7 : G K F A

    VIII.2. Cch suy lun trn cc lut sinh

    Suy din tin : l qu trnh suy lun xut pht t mt s s kin ban u, xcnh cc s kin c thc "sinh" ra t s kin ny.

    Skin ban u : H, K

    R3 : H A {A, H. K }

    R1 : A E { A, E, H, H }R5 : E K B { A, B, E, H, K }

    R2 : B D { A, B, D, E, H, K }

    R6 : D E K C { A, B, C, D, E, H, K }

    Suy din li : l qu trnh suy lun ngc xut pht t mt s s kin ban u,ta tm kim cc s kin "sinh" ra s kin ny. Mt v d thng gp trong thct l xut pht t cc tnh trng ca my tnh, chn on xem my tnh b hnghc u.

    V d :

    Tp cc s kin :

    cng l "hng" hay "hot ng bnh thng"

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    70/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 70

    Hng mn hnh. Lng cp mn hnh. Tnh trng n cng l "tt" hoc "sng" C m thanh c cng. Tnh trng n mn hnh "xanh" hoc "chp " Khng s dng c my tnh. in vo my tnh "c" hay "khng"

    Tp cc lut :

    R1. Nu ( ( cng "hng") hoc (cp mn hnh "lng")) th khng s dng cmy tnh.

    R2. Nu (in vo my l "c") v ( (m thanh c cng l "khng") hoc tnhtrng n cng l "tt")) th ( cng "hng").

    R3. Nu (in vo my l "c") v (tnh trng n mn hnh l "chp ") th (cpmn hnh "lng").

    xc nh c cc nguyn nhn gy ra s kin "khng s dng c my tnh",ta phi xy dng mt cu trc th gi l th AND/OR nh sau :

    Nh vy l xc nh c nguyn nhn gy ra hng hc l do cng hng haycp mn hnh lng, h thng phi ln lt i vo cc nhnh kim tra cc iukin nhin vo my "c", m thanh cng "khng"Ti mt bc, nu gi trcn xc nh khng thc suy ra t bt k mt lut no, h thng s yu cungi dng trc tip nhp vo. Chng hn nh bit my tnh c in khng, h

  • 8/3/2019 Thuat Toan Va Giai Thuat - Smith.N Studio

    71/110

    Nguyen Dinh Familys

    G-Unit Editor: Smith.N - 71

    thng s hin ra mn hnh cu hi "Bn kim tra xem c in vo my tnh khng(kim tra n ngun)? (C/K)". thc hin c cch suy lun li, ngi tathng s dng ngn xp ( ghi nhn li nhng nhnh cha kim tra).

    VIII.3. Vn ti u lut

    Tp cc lut trong mt cstri thc rt c kh nng tha, trng lp hoc muthun. Dnhin l h thng c thli cho ngi dng v vic a vo h thngnhng tri thc nh vy. Tuy vic ti u mt cstri thc v mt tng qut l mtthao tc kh (v gia cc tri thc thng c quan h khng tng minh), nhngtrong gii hn cstri thc di dng lut,