xPp1dZ - 0985930802 PDF
xPp1dZ - 0985930802 PDF
xPp1dZ - 0985930802 PDF
T H E N A T U R E O F C O D E
S I M U L A T I N G N A T U R A L S Y S T E M S W I T H P R O C E S S I N G
D A N I E L S H I F F M A N
T
H
E
N
A
T
U
R
E
O
F
C
O
D
E
D
A
N
I
E
L
S
H
I
F
F
M
A
N
How can we capture the unpredictable
evolutionary and emergent properties of
nature in software?
How can understanding the mathematical
principles behind our physical world help us
to create digital worlds?
This book focuses on a range of
programming strategies and techniques
behind computer simulations of natural
systems, from elementary concepts in
mathematics and physics to more advanced
algorithms that enable sophisticated visual
results. Using the open-source language
Processing, readers will progress from
building a basic physics engine to creating
intelligent moving objects and complex
systems, setting the foundation for further
experiments in generative design.
http://natureofcode.com
The Nature of Code The Nature of Code
?V $>KFBI 3EFCCJ>K
QEB MR?IFPEBO TLRIA DL EBOB, ?RQ QEBOB FP KLKB, FQfP LKIV JB.
SBOPFLK 0005, DBKBO>QBA $B@BJ?BO 4, 2012
/<=G =G H<9 :=B5@ ," F9@95G9 69:CF9 H<9 6CC? ;C9G HC DF=BH. ,@95G9 DCGH 5BM 7CFF97H=CBG HC
#=H$I6 (<HHD://;=H<I6.7CA/G<=::A5B//<9-*5HIF9-C:-CC89/=GGI9G) 6M 11:59 ,) (!./) 979A69F
5, 2012.
F
Dedication Dedication
&LO JV DO>KAJLQEBO, "BII> ->KBI 'OBBKCFBIA (/@QL?BO 13, 1915 - !MOFI 3, 2010)
"BII> ->KBI T>P ?LOK FK .BT 9LOH #FQV. ! MFLKBBOFKD TLJ>K FK J>QEBJ>QF@P, PEB B>OKBA EBO
0E$ FK 1939 COLJ .BT 9LOH 5KFSBOPFQV RKABO QEB PRMBOSFPFLK LC 2F@E>OA #LRO>KQ. 3EB TLOHBA
CLO 2>JL-7LLIAOFADB (KLT 427) >KA >Q QEB 2>KA #LOMLO>QFLK TFQE 2F@E>OA "BIIJ>K. ,>QBO, PEB
Q>RDEQ J>QEBJ>QF@P >Q QEB #LIIBDB LC .LQOB $>JB AB .>JRO 5KFSBOPFQV FK "BIJLKQ, #>IFCLOKF>,
>KA >Q 5#,!. 4EB "BII> ->KBI 0OFWB CLO LRQPQ>KAFKD DO>AR>QB TLOH ?V > TLJ>K LO JFKLOFQV
T>P BPQ>?IFPEBA >Q .95fP #LRO>KQ )KPQFQRQB FK 1995.
4EB .>QROB LC #LAB (S005)
FF
%AFQLO %AFQLO 3E>KKLK &OV
)IIRPQO>QFLKP )IIRPQO>QFLKP :>KK>E ->OPE
#LSBO $BPFDK #LSBO $BPFDK $>SFA 7FIPLK
)KQBOFLO $BPFDK )KQBOFLO $BPFDK $>SFA 7FIPLK
7B? 3FQB $BPFDK 7B? 3FQB $BPFDK 3QBSB +IFPB
%AFQLOF>I >KA $BPFDK !PPFPQ>KQ %AFQLOF>I >KA $BPFDK !PPFPQ>KQ %S>K %JLIL
->DF@ "LLH ,B>A $BSBILMBOP ->DF@ "LLH ,B>A $BSBILMBOP 2RKB ->APBK, 3QBSB +IFPB
->DF@ "LLH 2BPB>O@EBOP ->DF@ "LLH 2BPB>O@EBOP %S>K %JLIL, -FDRBI "BOJRABW, ,RFP> 0BFOBO> (LOP
)KABU )KABU 7LOA#L )KABUFKD 3BOSF@BP
#LMVOFDEQ 2012 ?V $>KFBI 3EFCCJ>K
)3".-13: 978-0985930806
)3".-10: 0985930802
4EFP TLOH FP IF@BKPBA RKABO QEB #OB>QFSB #LJJLKP !QQOF?RQFLK-.LK#LJJBO@F>I 3.0
5KMLOQBA ,F@BKPB. 4L SFBT > @LMV LC QEFP IF@BKPB, SFPFQ @OB>QFSB@LJJLKP.LOD
(EQQM://@OB>QFSB@LJJLKP.LOD/IF@BKPBP/?V-K@/3.0/) LO PBKA > IBQQBO QL #OB>QFSB #LJJLKP, 444
#>PQOL 3QOBBQ, 3RFQB 900, -LRKQ>FK 6FBT, #>IFCLOKF> 94041, 53!.
!II LC QEB ?LLHfP PLRO@B @LAB FP IF@BKPBA RKABO QEB '.5 ,BPPBO 'BKBO>I 0R?IF@ ,F@BKPB
(EQQM://@OB>QFSB@LJJLKP.LOD/IF@BKPBP/,'0,/2.1/) >P MR?IFPEBA ?V QEB &OBB 3LCQT>OB
&LRKA>QFLK; BFQEBO SBOPFLK 2.1 LC QEB ,F@BKPB, LO (>Q VLRO LMQFLK) >KV I>QBO SBOPFLK.
4EFP ?LLH T>P DBKBO>QBA ?V QEB ->DF@ "LLH 0OLGB@Q (EQQM://TTT.J>DF@?LLHMOLGB@Q.@LJ).
$BAF@>QFLK
FFF
Acknowledgments Acknowledgments
"*52 D<?91 .?<B;1 B@ :<C2@ 6; 0<:=960.A21 .;1 D<;12?3B9 D.F@. ,2
@=2;1 A52 2.?962? =.?A@ <3 <B? 96C2@ 92.?;6;4 ./<BA <B? 2;C6?<;:2;A
A5?<B45 =2?02=A6<; .;1 6;A2?.0A6<;. ,2 2E=20A A52 =5F@60.9 D<?91 .?<B;1
B@ A< /25.C2 0<;@6@A2;A9F D6A5 <B? =2?02=AB.9 :2:<?F, 2.4. 63 D2 1?<= .
?<08 6A D699 3.99 1B2 A< 4?.C6AF, 63 . 4B@A <3 D6;1 /9<D@, 9645A2? </720A@ D699
/2 A<@@21 /F A52 D6;1 3B?A52?. *56@ 09.@@ 3<0B@2@ <; B;12?@A.;16;4,
@6:B9.A6;4, .;1 6;0<?=<?.A6;4 :<A6<;-/.@21 292:2;A@ <3 <B? =5F@60.9
D<?91 6;A< A52 1646A.9 D<?91@ A5.A D2 0?2.A2. &B? 5<=2 6@ A< 0?2.A2
6;AB6A6C2, ?605, .;1 :<?2 @.A6@3F6;4 2E=2?62;02@ /F 1?.D6;4 3?<: A52
=2?02=AB.9 :2:<?62@ <3 <B? B@2?@."
James Tu Dynamic Bodies course description, Spring 2003, ITP
4EB .>QROB LC #LAB (S005)
FS
A.1 A little bit of history A.1 A little bit of history
)K 2003, >P > DO>AR>QB PQRABKQ >Q QEB )KQBO>@QFSB 4BIB@LJJRKF@>QFLKP 0OLDO>J ()40) FK QEB
4FP@E 3@ELLI LC QEB !OQP >Q .BT 9LOH 5KFSBOPFQV, ) BKOLIIBA FK > @LROPB @>IIBA $VK>JF@
"LAFBP. 4EB @LROPB T>P Q>RDEQ ?V FKQBO>@QFLK ABPFDKBO >KA )40 >AGRK@Q MOLCBPPLO *>JBP 4R.
!Q QEB QFJB, JV TLOH T>P CL@RPBA LK > PBOFBP LC PLCQT>OB BUMBOFJBKQP QE>Q DBKBO>QBA OB>I-
QFJB gKLK-MELQLOB>IFPQF@h FJ>DBOV. 4EB >MMIF@>QFLKP FKSLISBA @>MQROFKD FJ>DBP COLJ > IFSB
PLRO@B >KA gM>FKQFKDh QEB @LILOP TFQE BIBJBKQP QE>Q JLSBA >?LRQ QEB P@OBBK >@@LOAFKD QL
S>OFLRP ORIBP. 4EB $VK>JF@ "LAFBP @LROPBdTEF@E @LSBOBA SB@QLOP, CLO@BP, LP@FII>QFLKP,
M>OQF@IB PVPQBJP, OB@ROPFLK, PQBBOFKD, >KA PMOFKDPd>IFDKBA MBOCB@QIV TFQE JV TLOH.
) E>A ?BBK RPFKD QEBPB @LK@BMQP FKCLOJ>IIV FK JV LTK MOLGB@QP, ?RQ E>A KBSBO Q>HBK QEB
QFJB QL @ILPBIV BU>JFKB QEB P@FBK@B ?BEFKA QEB >IDLOFQEJP LO IB>OK L?GB@Q-LOFBKQBA
QB@EKFNRBP QL CLOJ>IFWB QEBFO FJMIBJBKQ>QFLK. 4E>Q SBOV PBJBPQBO, ) >IPL BKOLIIBA FK
&LRKA>QFLKP LC 'BKBO>QFSB !OQ 3VPQBJP, > @LROPB Q>RDEQ ?V 0EFIFM '>I>KQBO, QE>Q CL@RPBA LK
QEB QEBLOV >KA MO>@QF@B LC DBKBO>QFSB >OQ, @LSBOFKD QLMF@P PR@E >P @E>LP, @BIIRI>O >RQLJ>Q>,
DBKBQF@ >IDLOFQEJP, KBRO>I KBQTLOHP, >KA CO>@Q>IP. "LQE 4RfP @LROPB >KA '>I>KQBOfP @LROPB
LMBKBA JV BVBP QL > TLOIA LC PFJRI>QFLK >IDLOFQEJP >KA QB@EKFNRBP QE>Q @>OOFBA JB QEOLRDE
QEB KBUQ PBSBO>I VB>OP LC TLOH >KA QB>@EFKD, >KA PBOSBA >P QEB CLRKA>QFLK >KA FKPMFO>QFLK
CLO QEFP ?LLH.
"RQ QEBOBfP >KLQEBO MFB@B LC QEB MRWWIB JFPPFKD COLJ QEFP PQLOV.
'>I>KQBOfP @LROPB T>P JLPQIV QEBLOV-?>PBA, TEFIB 4RfP T>P Q>RDEQ RPFKD ->@OLJBAF>
$FOB@QLO >KA QEB ,FKDL MOLDO>JJFKD I>KDR>DB. 4E>Q PBJBPQBO, ) IB>OKBA J>KV LC QEB
>IDLOFQEJP ?V QO>KPI>QFKD QEBJ FKQL #++ (QEB I>KDR>DB ) T>P RPFKD NRFQB >THT>OAIV >Q QEB
QFJB, TBII ?BCLOB #++ @OB>QFSB @LAFKD BKSFOLKJBKQP IFHB LMBK&O>JBTLOHP >KA #FKABO E>A
>OOFSBA). 4LT>OAP QEB BKA LC QEB PBJBPQBO, ) AFP@LSBOBA PLJBQEFKD @>IIBA 0OL@BPPFKD
(EQQM://TTT.MOL@BPPFKD.LOD). 0OL@BPPFKD T>P FK >IME> QEBK (SBOPFLK 0055) >KA, E>SFKD E>A
PLJB BUMBOFBK@B TFQE *>S>, FQ FKQOFDRBA JB BKLRDE QL >PH QEB NRBPQFLK: #LRIA QEFP LMBK-
PLRO@B, >OQFPQ-COFBKAIV MOLDO>JJFKD I>KDR>DB >KA BKSFOLKJBKQ ?B QEB OFDEQ MI>@B QL ABSBILM
> PRFQB LC QRQLOF>IP >KA BU>JMIBP >?LRQ MOLDO>JJFKD >KA PFJRI>QFLK? 7FQE QEB PRMMLOQ LC
QEB )40 >KA 0OL@BPPFKD @LJJRKFQFBP, ) BJ?>OHBA LK TE>Q E>P KLT ?BBK >K >IJLPQ BFDEQ-
VB>O GLROKBV LC QB>@EFKD > S>OFBQV LC MOLDO>JJFKD @LK@BMQP >KA QEBFO >MMIF@>QFLKP RPFKD
0OL@BPPFKD.
)fA IFHB QL CFOPQ QE>KH 2BA "ROKP, )40fP CLRKABO, TEL E>P PRMMLOQBA >KA BK@LRO>DBA JB FK JV
TLOH CLO LSBO QBK VB>OP. $>K /f3RIIFS>K, QEB @E>FO LC )40, E>P ?BBK JV QB>@EFKD JBKQLO >KA
T>P QEB CFOPQ QL PRDDBPQ QE>Q ) QOV QB>@EFKD > @LROPB LK 0OL@BPPFKD, DFSFKD JB > OB>PLK QL
PQ>OQ >PPBJ?IFKD MOLDO>JJFKD QRQLOF>IP FK QEB CFOPQ MI>@B. 3E>TK 6>K %SBOV, ABSBILMBO
BUQO>LOAFK>FOB >KA >RQELO LC ,FC AB8FC=8 )98=5, E>P >IPL ?BBK > OF@E PLRO@B LC EBIM >KA
FKPMFO>QFLK >Q )40 LSBO QEB VB>OP. )40 C>@RIQV JBJ?BOP #I>V 3EFOHV, $>KKV 2LWFK, +>QEBOFKB
$FIILK, ->OF>KKB 0BQFQ, ->OFK> :ROHLT, >KA 4LJ )DLB E>SB MOLSFABA > DOB>Q AB>I LC PRMMLOQ
>KA CBBA?>@H QEOLRDELRQ QEB TOFQFKD LC QEFP ?LLH. 4EB OBPQ LC QEB C>@RIQV >KA PQ>CC >Q )40
E>SB >IPL J>AB QEFP MLPPF?IB: "OF>K +FJ, %AT>OA 'LOALK, 'BLODB !DRALT, *LEK $R>KB,
->OILK %S>KP, ->QQ "BODBO, -BD>K $BJ>OBPQ, -FALOF 9>PRA>, >KA 2L? 2V>K.
!@HKLTIBADJBKQP
S
4EB PQRABKQP LC )40, QLL KRJBOLRP QL JBKQFLK, E>SB ?BBK >K >J>WFKD PLRO@B LC CBBA?>@H
QEOLRDELRQ QEFP MOL@BPP. -R@E LC QEB J>QBOF>I FK QEFP ?LLH @LJBP COLJ JV @LROPB LC QEB P>JB
QFQIB, TEF@E )fSB KLT Q>RDEQ CLO CFSB VB>OP. ) E>SB PQ>@HP LC AO>CQ MOFKQLRQP LC QEB ?LLH TFQE
KLQBP P@O>TIBA >ILKD QEB J>ODFKP >P TBII >P > S>PQ >O@EFSB LC PQRABKQ BJ>FIP TFQE @LOOB@QFLKP,
@LJJBKQP, >KA DBKBOLRP TLOAP LC BK@LRO>DBJBKQ.
) >J >IPL FKAB?QBA QL QEB BKBODBQF@ >KA PRMMLOQFSB @LJJRKFQV LC 0OL@BPPFKD MOLDO>JJBOP
>KA >OQFPQP. ) TLRIAKfQ ?B TOFQFKD QEFP ?LLH FC FQ TBOBKfQ CLO #>PBV 2B>P >KA "BK &OV, TEL
@OB>QBA 0OL@BPPFKD. )fSB IB>OKBA E>IC LC TE>Q ) HKLT PFJMIV COLJ OB>AFKD QEOLRDE QEB
0OL@BPPFKD PLRO@B @LAB; QEB BIBD>KQ PFJMIF@FQV LC QEB 0OL@BPPFKD I>KDR>DB, TB?PFQB, >KA )$%
E>P J>AB MOLDO>JJFKD >@@BPPF?IB >KA CRK CLO >II LC JV PQRABKQP. )fSB OB@BFSBA >ASF@B >KA
FKPMFO>QFLK COLJ J>KV 0OL@BPPFKD MOLDO>JJBOP FK@IRAFKD !KAOZP #LIR?OF, *BO 4ELOM, ->OFRP
7>QW, +>OPQBK 3@EJFAQ, 2L?BOQ (LADFK, 3B?-,BB $BIFPIB, >KA )O> 'OBBK?BOD. (B>QEBO $BTBV-
(>D?LOD MOLSFABA > DOB>Q AB>I LC BU@BIIBKQ CBBA?>@H LK #E>MQBO 10 (.BRO>I .BQTLOHP). 3@LQQ
-ROO>V MOLSFABA PLJB OB>IIV EBIMCRI >ASF@B >?LRQ FKIFKB 36'P LSBO B-J>FI. ->KV LC QEB QFQIBP FK
QEB &ROQEBO 2B>AFKD PB@QFLK TBOB PRDDBPQBA ?V 'LI>K ,BSFK.
) >J FKAB?QBA QL 3E>KKLK &OV, TEL BAFQBA QEFP ?LLH BSBOV PQBM LC QEB T>V. 4EB HKLTIBADB
QE>Q ) TLRIA >IT>VP E>SB EBO @>OBCRI >KA QELRDEQCRI CBBA?>@H LK JV TOFQFKD >IILTBA JB QL
MILT >EB>A, >T>OB QE>Q BSBOVQEFKD TLRIA @LJB LRQ PLRKAFKD ?BQQBO >CQBO PEB DLQ EBO E>KAP
LK JV @E>MQBOP.
! PMB@F>I JBKQFLK DLBP QL :>KK>E ->OPE TEL TLOHBA QFOBIBPPIV QL @OB>QB LSBO > ERKAOBA
FIIRPQO>QFLKP CLO QEFP ?LLH, ABSBILMFKD > COFBKAIV >KA FKCLOJ>I ILLH. ) BPMB@F>IIV T>KQ QL QE>KH
EBO CLO EBO M>QFBK@B >KA TFIIFKDKBPP QL DL TFQE QEB CILT >P TB @E>KDBA QEB FIIRPQO>QFLK
OBNRFOBJBKQP PBSBO>I QFJBP. ) >IPL T>KQ QL QE>KH $>SFA 7FIPLK, TEL @>JB QL JV OBP@RB >Q QEB
I>PQ JFKRQB >KA ABPFDKBA QEB FKQBOFLO I>VLRQ >KA @LSBO CLO QEB ?LLH. ) >J M>OQF@RI>OIV DO>QBCRI
QL 3QBSB +IFPB, TEL ABPFDKBA >KA ?RFIQ QEB ?LLHfP TB? PFQB, EBIMFKD JB QL ABSBILM > "M>V TE>Q
VLR T>KQ" JLABI CLO QEB AFDFQ>I 0$&.
!P )fII BUMI>FK > ?FQ JLOB FK QEB MOBC>@B, QEFP ?LLH T>P DBKBO>QBA TFQE > KBT LMBK-PLRO@B
PVPQBJ CLO MR?IFPEFKD @>IIBA g4EB ->DF@ "LLH.h ! @O>@H QB>J LC )40 MOLDO>JJBOP, ABPFDKBOP,
>KA >OQFPQP TLOHBA LSBO QEB @LROPB LC JLOB QE>K > VB>O QL ABSBILM QEFP PVPQBJ, TEF@E
DBKBO>QBP > ?LLH FK > S>OFBQV LC CLOJ>QP (0$&, (4-,, >KA JLOB) COLJ LKB PFKDIB !3#))$/#
CFIB, >II ABPFDKBA TFQE #33 I>VLRQ. 2RKB ->APBK ?BD>K QEB MOLGB@Q >KA ABSBILMBA QEB LOFDFK>I
2R?V / 3FK>QO> CO>JBTLOH. ) >J MOBQQV PROB )fA ?B PQORDDIFKD TFQE MRQQFKD QEB MFB@BP LC QEB ?LLH
QLDBQEBO TBII FKQL 2013 FC FQ T>PKfQ CLO 2RKBfP ABAF@>QFLK QL PBBFKD QEB MOLGB@Q QEOLRDE QL QEB
BKA. 3QBSB +IFPB @LKQOF?RQBA @LRKQIBPP ?RD CFUBP >KA BKDFKBBOBA QEB PVPQBJ QE>Q >IILTP RP QL
OBPQVIB @LAB @LJJBKQP QL QEB PFAB LC QEB @LAB ?IL@HP QEBJPBISBP. -FDRBI "BOJRABW, %S>K
%JLIL, >KA ,RFP> 0BOBFO> (LOP @LKQOF?RQBA FK J>KV T>VP, IB>OKFKD QEB FKP >KA LRQP LC
!3#))$/# >P TBII >P #33 0>DBA -BAF>. )40 OBPB>O@EBO 'OBD "LOBKPQBFK MOLSFABA >
QOBJBKALRP >JLRKQ LC >ASF@B >KA PRMMLOQ >ILKD QEB T>V OBD>OAFKD QEB >OB>P LC MR?IFPEFKD CLO
QEB TB? >KA MOFKQ. 0OFK@B (EQQM://MOFK@BUJI) FP QEB BKDFKB QEB ->DF@ "LLH RPBP QL DBKBO>QB >
0$& COLJ >K (4-, AL@RJBKQ >KA )fA IFHB QL QE>KH -F@E>BI $>V, #%/ LC 0OFK@B8-,, TEL T>P
>KPTBOBA J>KV LC LRO NRBPQFLKP (>Q IFDEQKFKD PMBBA) >ILKD QEB T>V.
4EB .>QROB LC #LAB (S005)
SF
&FK>IIV )fA IFHB QL QE>KH JV C>JFIV: JV TFCB, !IFHF #>ILVBO>P, TEL PRMMLOQBA QEFP MOLGB@Q
QEOLRDELRQ TEFIB E>SFKD EBO LTK DF>KQ QLJB QL TOFQB, >KA JV @EFIAOBK, %IF>P >KA /IVJMF>,
JLQFS>QFLK CLO CFKFPEFKD QEFP RM PL QE>Q ) @LRIA PMBKA JLOB QFJB E>KDFKD LRQ TFQE QEBJ. )fA
>IPL IFHB QL QE>KH JV C>QEBO, "BOK>OA 3EFCCJ>K, TEL DBKBOLRPIV IBKQ EFP J>QEBJ>QF@>I
BUMBOQFPB >KA MOLSFABA CBBA?>@H >ILKD QEB T>V, >P TBII >P JV JLQEBO, $LOFP 9>CCB 3EFCCJ>K,
>KA ?OLQEBO, *LK>QE>K 3EFCCJ>K, TEL TBOB >IT>VP QOBJBKALRPIV PRMMLOQFSB FK >PHFKD QEB
NRBPQFLK: g(LT FP QEB ?LLH @LJFKD >ILKD?h
A.2 Kickstarter A.2 Kickstarter
4EBOB FP >KLQEBO LOD>KFW>QFLK >KA @LJJRKFQV QE>Q E>P J>AB QEFP ?LLH MLPPF?IB: +F@HPQ>OQBO.
)K 2008, ) @LJMIBQBA TLOH LK JV CFOPQ ?LLH, (95FB=B; ,FC79GG=B;, MR?IFPEBA ?V -LOD>K
+>RCJ>KK/%IPBSFBO. (95FB=B; ,FC79GG=B; QLLH >IJLPQ QEOBB VB>OP QL CFKFPE. ) AFAKfQ Q>HB > ILQ
LC @>OB FK @ELLPFKD > MR?IFPEBO LO QEFKHFKD >?LRQ QEB QBOJP. ) GRPQ QELRDEQ d g2B>IIV? 9LR
T>KQ QL MR?IFPE > ?LLH ?V JB? /+, )fII AL FQ.h 5KCLOQRK>QBIV, JV BUMBOFBK@B T>P KLQ BKQFOBIV
MLPFQFSB. ) E>A CFSB AFCCBOBKQ BAFQLOP >PPFDKBA QL JB QEOLRDELRQ QEB MOL@BPP, >KA ) OB@BFSBA
IFQQIB QL KL CBBA?>@H LK QEB @LKQBKQ FQPBIC. 4EB MR?IFPEBO LRQPLRO@BA QEB QVMBPBQQFKD, TEF@E
OBPRIQBA FK > DOB>Q AB>I LC JFPQ>HBP >KA FK@LKPFPQBK@FBP FK MOLAR@QFLK. )K >AAFQFLK, ) CLRKA
QEB MOF@FKD LC QEB ?LLH QL ?B LCC QEB J>OH. -V DL>I T>P QL TOFQB > COFBKAIV, FKBUMBKPFSB
(?I>@H >KA TEFQB), M>MBO?>@H FKQOLAR@QFLK QL MOLDO>JJFKD FK 0OL@BPPFKD, >KA QEB ?LLH
BKABA RM OBQ>FIFKD CLO > "QBUQ?LLH" MOF@B LC $50.
.LT, ) T>KQ QL BJME>PFWB QE>Q JV MR?IFPEBO E>A DLLA FKQBKQFLKP. 4EBV ELKBPQIV T>KQBA QL
MOLAR@B QEB ?BPQ ?LLH MLPPF?IB, LKB QE>Q ) TLRIA ?B E>MMV TFQE, QE>Q QEBV TLRIA ?B E>MMV
TFQE, >KA QE>Q OB>ABOP TLRIA BKGLV. !KA QEBV TLOHBA E>OA QL J>HB QEFP E>MMBK.
5KCLOQRK>QBIV, QEBV E>A QL TLOH TFQEFK > SBOV QFDEQ ?RADBQ, >KA >P > OBPRIQ TBOB PQOBQ@EBA
BUQOBJBIV QEFK. )K >AAFQFLK, ) ALKfQ QEFKH QEBV TBOB QBOOF?IV C>JFIF>O TFQE QEB TLOIA LC LMBK-
PLRO@B g@OB>QFSBh @LAFKD BKSFOLKJBKQP IFHB 0OL@BPPFKD; QEBFO TLOIA FP @LJMRQBO P@FBK@B
QBUQ?LLHP.
!P > OBPRIQ, CLO QEFP .>QROB LC #LAB ?LLH, ) CBIQ FQ T>P FJMLOQ>KQ QL QOV PBIC-MR?IFPEFKD. 3FK@B )
AFAKfQ DBQ BAFQFKD PRMMLOQ COLJ QEB MR?IFPEBO, TEV KLQ EFOB >K BAFQLO? ) T>PKfQ E>MMV TFQE QEB
MOF@FKD, PL TEV KLQ PBQ QEB MOF@B JVPBIC (LO, FK QEB @>PB LC QEB 0$&, IBQ QEB ?RVBO PBQ QEB
MOF@B)? 4EBK QEBOBfP QEB NRBPQFLK LC J>OHBQFKD d ALBP > MR?IFPEBO >AA S>IRB >KA EBIM VLR
OB>@E >K >RAFBK@B? )K PLJB @>PBP, QEB >KPTBO FP VBP. 4EB /f2BFIIV g->HBh PBOFBP, CLO
BU>JMIB, ALBP > TLKABOCRI GL? LC @OB>QFKD > @LJJRKFQV >OLRKA QEBFO ?LLHP >KA MOLAR@QP.
3QFII, FK QEB @>PB LC IB>OKFKD QL MOLDO>J FK 0OL@BPPFKD, OB>@EFKD QEB >RAFBK@B FP >P PFJMIB >P
LKB 52, d MOL@BPPFKD.LOD.
5KCLOQRK>QBIV, ) NRF@HIV AFP@LSBOBA QE>Q QEBOB FP LKB QEFKD > MR?IFPEBO LCCBOP QE>Q ) T>P KLQ
DBQQFKD COLJ JV PBIC-MR?IFPEFKD M>QE. /KB SBOV FJMLOQ>KQ, EFDEIV @OR@F>I ABQ>FI d > AB>AIFKB.
/K JV LTK, ) CILRKABOBA CLO QTL VB>OP, P>VFKD ) T>P DLFKD QL TOFQB QEB .>QROB LC #LAB ?LLH
?RQ LKIV AO>CQFKD > IFQQIB ?FQ EBOB >KA QEBOB. /K JV IFPQ LC QEFKDP ) KBBABA QL AL, FQ T>P >IT>VP
>Q QEB ?LQQLJ. 4EBK >ILKD @>JB +F@HPQ>OQBO, >KA TFQE >K >RAFBK@B PFQQFKD >KA T>FQFKD (>KA
!@HKLTIBADJBKQP
SFF
E>SFKD PMBKQ @>PE JLKBV), ) IFSBA FK CB>O LC KLQ JBBQFKD JV AB>AIFKB. !KA QEB C>@Q QE>Q VLR >OB
OB>AFKD QEFP KLT FP >K FKAF@>QFLK QE>Q FQ TLOHBA.
-LPQ FJMLOQ>KQIV, PBIC-MR?IFPEFKD QEB ?LLH E>P >IILTBA JB > DOB>Q AB>I LC CIBUF?FIFQV FK ELT )
MOF@B >KA AFPQOF?RQB QEB @LKQBKQ. /K %IPBSFBOfP TB? PFQB, VLR @>K MRO@E>PB (95FB=B;
,FC79GG=B; >P >K B-?LLH CLO $53.95. 4E>QfP OFDEQ, CFCQV-QEOBB ALII>OP >KA KFKBQV-CFSB @BKQP.
)K@FABKQ>IIV, CLO B>@E B-?LLH PLIA ) DBQ > OLV>IQV LC 5%, TEF@E FP $2.70. 4E>QfP OFDEQ, QTL ALII>OP
>KA PBSBKQV @BKQP. )C ) PBIC-MR?IFPE, ) @>K J>HB QEB ?LLH J>PPFSBIV @EB>MBO. 3BIIFKD > AFDFQ>I
@LMV CLO $10, )fJ OBAR@FKD QEB @LPQ QL QEB OB>ABO ?V LSBO BFDEQV MBO@BKQ >KA QOFMIFKD QEB JLKBV
M>FA QL JB. )fJ Q>HFKD QEFP BSBK CROQEBO TFQE QEB 0$& >KA >IILTFKD ?RVBOP QL PBQ QEB MOF@B
QEBJPBISBP.
)K >AAFQFLK, ?V LTKFKD >II QEB @LKQBKQ, ) >J >?IB QL OBIB>PB QEB BKQFOB ?LLH LKIFKB CLO COBB >P
TBII >P BUMBOFJBKQ TFQE KBT AFDFQ>I CLOJ>QP. 4EB O>T QBUQ LC QEB ?LLH, >P TBII >P >II QEB @LAB
>KA FIIRPQO>QFLKP, FP IF@BKPBA RKABO > #OB>QFSB #LJJLKP !QQOF?RQFLK-.LK#LJJBO@F>I IF@BKPB
>KA FP >S>FI>?IB LK 'FQER?, TEBOB OB>ABOP @>K PR?JFQ FPPRBP (KLQ QL JBKQFLK MRII OBNRBPQP!)
TFQE @LOOB@QFLKP >KA @LJJBKQP. &FK>IIV, ?V RPFKD JLOB CIBUF?IB MOFKQ-LK-ABJ>KA PBOSF@BP, ) @>K
JLOB B>PFIV J>HB @E>KDBP >KA HBBM QEB ?LLH @ROOBKQ, OBIB>PFKD KBT BAFQFLKP >P LCQBK >P )
IFHB. (! LKB-QFJB MRO@E>PB LC > AFDFQ>I @LMV LC QEB ?LLH FK@IRABP IFCBQFJB RMDO>ABP CLO COBB.)
3L QE>KH VLR QL +F@HPQ>OQBO, ?LQE QEB @LJM>KV (BPMB@F>IIV &OBA "BKBKPLK, TEL @LKSFK@BA JB
QL Q>HB QEB MIRKDB FK QEB CFOPQ MI>@B >KA >ASFPBA JB LK ELT QL IF@BKPB QEB ?LLH) >P TBII >P >II
QEB ?>@HBOP TEL QLLH > @E>K@B LK QEFP ?LLH. 3LJB LC QEBPB ?>@HBOP, QEOLRDE DBKBOLPFQV
?BVLKA QEB @>II LC ARQV, B>OKBA >K BUQO> QE>KH-VLR >P M>OQ LC QEBFO OBT>OA:
i !IBU>KAOB ".
i 2L?BOQ (LADFK
i *LL9LRK 0>BH
i !KDBI> -@.>JBB ("LVE>K)
i "L? )MMLIFQL
!II LC QEB ?>@HBOP AFOB@QIV @LKQOF?RQBA QL QEB CFKFPEFKD LC QEFP ?LLH. *RPQ QEB PEBBO >@Q LC
PFDKFKD RM QL @LKQOF?RQB JLKBV CLO AO>CQ >KA CFK>I SBOPFLKP IFQ > CFOB FK JB QL CFKFPE, KLQ QL
JBKQFLK MOLSFABA JB TFQE QEB OBPLRO@BP QL M>V CLO ABPFDK >KA BAFQFKD TLOH (>KA PLJB
?>?VPFQQFKD AROFKD 3>QROA>V JLOKFKD TOFQFKD PBPPFLKP).
)K >AAFQFLK QL @LKQOF?RQFKD CRKAP, +F@HPQ>OQBO ?>@HBOP OB>A MOB-OBIB>PB SBOPFLKP LC QEB @E>MQBOP
>KA MOLSFABA QLKP LC CBBA?>@H, @>Q@EFKD J>KV BOOLOP >KA MLFKQFKD LRQ @LKCRPFKD PB@QFLKP LC
QEB ?LLH. 4TL PR@E OB>ABOP QE>Q )fA IFHB QL QE>KH >OB &OBABOFH 6>KELRQQB >KA (>KP AB 7LIC,
TELPB BUMBOQ HKLTIBADB LC .BTQLKF>K MEVPF@P T>P BKLOJLRPIV EBIMCRI FK QEB OBSFPFKD LC
#E>MQBOP 2 >KA 3.
4EB .>QROB LC #LAB (S005)
SFFF
Preface Preface
P.1 What is this book? P.1 What is this book?
!Q )40 (EQQM://FQM.KVR.BAR), ) QB>@E > @LROPB BKQFQIBA )KQOLAR@QFLK QL #LJMRQ>QFLK>I -BAF>. )K
QEFP @LROPB, QEB PQRABKQP IB>OK QEB ?>PF@P LC MOLDO>JJFKD (S>OF>?IBP, @LKAFQFLK>IP, ILLMP,
L?GB@QP, >OO>VP) >P TBII >P > PROSBV LC >MMIF@>QFLKP OBI>QBA QL J>HFKD FKQBO>@QFSB MOLGB@QP
(FJ>DBP, MFUBIP, @LJMRQBO SFPFLK, KBQTLOHFKD, A>Q>, 3$). 4EB @LROPB JLPQIV CLIILTP QEB
J>QBOF>I CLRKA FK JV FKQOL ?LLH (95FB=B; ,FC79GG=B;; FK J>KV T>VP, /<9 *5HIF9 C: CC89
PBOSBP >P > CLIILT-RM. /K@B VLRfSB IB>OKBA QEB ?>PF@P >KA PBBK >K >OO>V LC >MMIF@>QFLKP,
VLRO KBUQ PQBM JFDEQ ?B QL ABISB ABBMIV FKQL > M>OQF@RI>O >OB>. &LO BU>JMIB, VLR @LRIA CL@RP
LK @LJMRQBO SFPFLK (>KA OB>A > ?LLH IFHB 'OBD "LOBKPQBFKfP )5?=B; /<=B;G .99). )K QEB JLPQ
?>PF@ PBKPB, QEFP ?LLH FP LKB MLPPF?IB KBUQ PQBM FK > TLOIA LC J>KV. )Q MF@HP RM BU>@QIV
TEBOB (95FB=B; ,FC79GG=B; IB>SBP LCC, ABJLKPQO>QFKD JLOB >AS>K@BA MOLDO>JJFKD
QB@EKFNRBP TFQE 0OL@BPPFKD QE>Q CL@RP LK >IDLOFQEJP >KA PFJRI>QFLK.
4EB DL>I LC QEFP ?LLH FP PFJMIB. 7B T>KQ QL Q>HB > ILLH >Q PLJBQEFKD QE>Q K>QRO>IIV L@@ROP FK
LRO MEVPF@>I TLOIA, QEBK ABQBOJFKB ELT TB @>K TOFQB @LAB QL PFJRI>QB QE>Q L@@ROOBK@B.
3L QEBK TE>Q FP QEFP ?LLH BU>@QIV? )P FQ > P@FBK@B ?LLH? 4EB >KPTBO FP > OBPLRKAFKD KL.
4ORB, TB JFDEQ BU>JFKB QLMF@P QE>Q @LJB COLJ MEVPF@P LO ?FLILDV, ?RQ FQ TLKfQ ?B LRO GL? QL
FKSBPQFD>QB QEBPB QLMF@P TFQE > M>OQF@RI>OIV EFDE IBSBI LC >@>ABJF@ OFDLO. )KPQB>A, TBfOB DLFKD
QL DI>K@B >Q P@FBKQFCF@ @LK@BMQP >KA DO>? QEB M>OQP QE>Q TB KBBA FK QEB PBOSF@B LC ?RFIAFKD >
M>OQF@RI>O PLCQT>OB BU>JMIB.
)P QEFP >K >OQ LO ABPFDK ?LLH? ) TLRIA >IPL P>V KL; >CQBO >II, TB >OB DLFKD QL CL@RP LK
>IDLOFQEJP >KA QEBFO >CCFIF>QBA MOLDO>JJFKD QB@EKFNRBP. 3ROB, QEB OBPRIQP TFII >II ?B SFPR>I FK
K>QROB (J>KFCBPQBA >P >KFJ>QBA 0OL@BPPFKD PHBQ@EBP), ?RQ QEBV TFII BUFPQ JLOB >P
ABJLKPQO>QFLKP LC QEB >IDLOFQEJP >KA MOLDO>JJFKD QB@EKFNRBP QEBJPBISBP, AO>TK LKIV TFQE
PFJMIB PE>MBP >KA DO>VP@>IB. )Q FP JV ELMB, ELTBSBO, QE>Q ABPFDKBOP >KA >OQFPQP @>K
FK@LOMLO>QB >II LC QEB J>QBOF>I EBOB FKQL QEBFO MO>@QF@B QL J>HB KBT, BKD>DFKD TLOH.
0OBC>@B
FU
)K QEB BKA, FC QEFP ?LLH FP >KVQEFKD, FQ FP OB>IIV GRPQ > DLLA LIA-C>PEFLKBA MOLDO>JJFKD ?LLH.
7EFIB > P@FBKQFCF@ QLMF@ J>V PBBA > @E>MQBO (.BTQLKF>K MEVPF@P, @BIIRI>O DOLTQE, BSLIRQFLK) LO
QEB OBPRIQP JFDEQ FKPMFOB >K >OQFPQF@ MOLGB@Q, QEB @LKQBKQ FQPBIC TFII >IT>VP ?LFI ALTK QL QEB @LAB
FJMIBJBKQ>QFLK, TFQE > M>OQF@RI>O CL@RP LK L?GB@Q-LOFBKQBA MOLDO>JJFKD.
P.2 A word about Processing P.2 A word about Processing
) >J RPFKD 0OL@BPPFKD FK QEFP ?LLH CLO > KRJ?BO LC OB>PLKP. &LO LKB, FQfP QEB I>KDR>DB >KA
BKSFOLKJBKQ TFQE TEF@E ) >J JLPQ @LJCLOQ>?IB, >KA FQfP TE>Q ) BKGLV RPFKD CLO JV MBOPLK>I
TLOH. 4TL, FQfP COBB, LMBK-PLRO@B, >KA TBII PRFQBA QL ?BDFKKBOP. 4EBOB FP >K >@QFSB, BKBODBQF@
@LJJRKFQV LC MBLMIB TEL MOLDO>J TFQE 0OL@BPPFKD; CLO J>KV, FQfP QEB CFOPQ MOLDO>JJFKD
I>KDR>DB QEBVfSB IB>OKBA. )K QEFP PBKPB, ) ELMB QE>Q ) @>K OB>@E > TFAB >RAFBK@B >KA
ABJLKPQO>QB QEB @LK@BMQP FK > COFBKAIV J>KKBO ?V RPFKD 0OL@BPPFKD.
!II QE>Q P>FA, QEBOB FP KLQEFKD QE>Q QFBP TE>Q TB >OB ALFKD FK QEFP ?LLH PQOF@QIV QL 0OL@BPPFKD.
4EFP ?LLH @LRIA E>SB ?BBK TOFQQBK RPFKD !@QFLK3@OFMQ, *>S>3@OFMQ, *>S> (TFQELRQ 0OL@BPPFKD),
LO >KV KRJ?BO LC LQEBO LMBK-PLRO@B g@OB>QFSB @LAFKDh BKSFOLKJBKQP IFHB LMBK&O>JBTLOHP,
#FKABO, LO QEB KBTIV OBIB>PBA ML@LAB. )Q FP JV ELMB QE>Q >CQBO )fSB @LJMIBQBA QEFP ?LLH, )fII ?B
>?IB QL OBIB>PB SBOPFLKP LC QEB BU>JMIBP QE>Q ORK FK LQEBO BKSFOLKJBKQP. )C >KVLKB FP
FKQBOBPQBA FK EBIMFKD QL MLOQ QEB BU>JMIBP, MIB>PB CBBI COBB QL @LKQ>@Q JB
(A>KFBIPEFCCJ>K.KBQ).
!II LC QEB BU>JMIBP FK QEFP ?LLH E>SB ?BBK QBPQBA TFQE 0OL@BPPFKD 2.0?6, ?RQ CLO QEB JLPQ
M>OQ, QEBV PELRIA >IPL TLOH TFQE B>OIFBO SBOPFLKP LC 0OL@BPPFKD. )fII ?B HBBMFKD QEBJ RM-QL-A>QB
TFQE TE>QBSBO QEB I>QBPQ SBOPFLK FP. 4EB JLPQ OB@BKQ @LAB @>K >IT>VP ?B CLRKA LK 'FQ(R?
(EQQM://DFQER?.@LJ/PEFCCJ>K/4EB-.>QROB-LC-#LAB-%U>JMIBP).
P.3 What do you need to know? P.3 What do you need to know?
4EB MOBOBNRFPFQB CLO RKABOPQ>KAFKD QEB J>QBOF>I FK QEFP ?LLH @LRIA ?B PQ>QBA >P: gLKB PBJBPQBO
LC MOLDO>JJFKD FKPQOR@QFLK TFQE 0OL@BPPFKD (FK@IRAFKD C>JFIF>OFQV TFQE L?GB@Q-LOFBKQBA
MOLDO>JJFKD).h 4E>Q P>FA, QEBOBfP KL OB>PLK TEV VLR @LRIAKfQ OB>A QEFP ?LLH E>SFKD IB>OKBA
MOLDO>JJFKD RPFKD > AFCCBOBKQ I>KDR>DB LO ABSBILMJBKQ BKSFOLKJBKQ. 4EB HBV EBOB FP QE>Q
VLR E>SB BUMBOFBK@B TFQE MOLDO>JJFKD.
)C VLRfSB KBSBO TOFQQBK >KV @LAB ?BCLOB, VLR >OB DLFKD QL PQORDDIB, ?B@>RPB QEFP ?LLH
>PPRJBP HKLTIBADB LC >II QEB ?>PF@P. ) TLRIA PRDDBPQ MF@HFKD RM >K FKQOLAR@QLOV ?LLH LK
0OL@BPPFKD, > KRJ?BO LC TEF@E >OB IFPQBA LK QEB 0OL@BPPFKD TB?PFQB (EQQM://MOL@BPPFKD.LOD/
IB>OKFKD/?LLHP/).
)C VLR >OB >K BUMBOFBK@BA MOLDO>JJBO, ?RQ E>SBKfQ TLOHBA TFQE 0OL@BPPFKD, VLR @>K MOL?>?IV
MF@H FQ RM ?V ALTKIL>AFKD 0OL@BPPFKD (EQQM://MOL@BPPFKD.LOD/ALTKIL>A/), MLHFKD QEOLRDE QEB
4EB .>QROB LC #LAB (S005)
U
BU>JMIBP, >KA OB>AFKD QEOLRDE QEB 'BQQFKD 3Q>OQBA (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/
DBQQFKDPQ>OQBA/) M>DB.
) PELRIA >IPL MLFKQ LRQ QE>Q BUMBOFBK@B TFQE L?GB@Q-LOFBKQBA MOLDO>JJFKD FP @OR@F>I. 7BfII
OBSFBT PLJB LC QEB ?>PF@P FK QEB ?LLHfP FKQOLAR@QFLK, ?RQ ) TLRIA PRDDBPQ OB>AFKD QEB
0OL@BPPFKD QRQLOF>I LK L?GB@QP (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/L?GB@QP) CFOPQ.
P.4 What are you using to read this book? P.4 What are you using to read this book?
!OB VLR OB>AFKD QEFP ?LLH LK > +FKAIB? 0OFKQBA M>MBO? /K VLRO I>MQLM FK 0$& CLOJ? /K >
Q>?IBQ PELTFKD >K >KFJ>QBA (4-,5 SBOPFLK? !OB VLR PQO>MMBA QL > @E>FO, >?PLO?FKD QEB
@LKQBKQ AFOB@QIV FKQL VLRO ?O>FK SF> > PBOFBP LC BIB@QOLABP, QR?BP, >KA @>OQOFADBP?
4EB ?LLH VLR >OB OB>AFKD OFDEQ KLT T>P DBKBO>QBA TFQE QEB ->DF@ "LLH MOLGB@Q
(EQQM://TTT.J>DF@?LLHMOLGB@Q.@LJ). 4EB ->DF@ "LLH FP >K LMBK-PLRO@B CO>JBTLOH CLO PBIC-
MR?IFPEFKD ABSBILMBA >Q )40 (EQQM://FQM.KVR.BAR). 4EB FAB> EBOB FP QE>Q VLR LKIV KBBA QL TOFQB
QEB ?LLH LK@B >P > PFJMIB QBUQ CFIB. /K@B VLRfSB TOFQQBK VLRO @LKQBKQ, VLR MOBPP > J>DF@
?RQQLK, >KA LRQ @LJBP VLRO ?LLH FK > S>OFBQV LC CLOJ>QPd0$&, (4-,5, MOFKQBA E>OA@LMV,
+FKAIB, BQ@. %SBOVQEFKD FP ABPFDKBA >KA PQVIBA RPFKD #33. !P LC QEB CFOPQ OBIB>PB, QEB LKIV
SBOPFLKP >S>FI>?IB TFII ?B AFDFQ>I 0$&, MOFKQBA E>OA@LMV, >KA (4-,5 (TEF@E TFII FK@IRAB
>KFJ>QBA SBOPFLKP LC QEB BU>JMIBP RPFKD 0OL@BPPFKD.GP). (LMBCRIIV LSBO QEB @LROPB LC QEB
KBUQ VB>O, QEB ?LLH TFII ?B >S>FI>?IB FK >AAFQFLK>I CLOJ>QP. )C VLRfA IFHB QL EBIM TFQE QEFP,
MIB>PB @LKQ>@Q JB (A>KFBIPEFCCJ>K.KBQ).
P.5 The story of this book P.5 The story of this book
)C VLR DI>K@B LSBO QEB ?LLHfP Q>?IB LC @LKQBKQP, VLRfII KLQF@B QEBOB >OB QBK @E>MQBOP, B>@E
LKB @LSBOFKD > AFCCBOBKQ QLMF@. !KA FK LKB PBKPB, QEFP ?LLH FP GRPQ QE>Qd> PROSBV LC QBK
@LK@BMQP >KA >PPL@F>QBA @LAB BU>JMIBP. .BSBOQEBIBPP, FK MRQQFKD QLDBQEBO QEB J>QBOF>I, )
E>A >IT>VP FJ>DFKBA PLJBQEFKD LC > IFKB>O K>OO>QFSB. "BCLOB VLR ?BDFK OB>AFKD QEB
@E>MQBOP, )fA IFHB QL T>IH VLR QEOLRDE QEFP PQLOV. ) QEFKH FQfP FJMLOQ>KQ QL CFOPQ E>SB > PBKPB LC
QEB LSBO>II GLROKBV.
0OBC>@B
UF
Part I: Inanimate Objects. Part I: Inanimate Objects.
! PL@@BO ?>II IFBP FK QEB DO>PP. ! HF@H I>RK@EBP FQ FKQL QEB >FO. 'O>SFQV MRIIP FQ ?>@H ALTK. !
EB>SV DRPQ LC TFKA HBBMP FQ >CIL>Q > JLJBKQ ILKDBO RKQFI FQ C>IIP >KA ?LRK@BP LCC QEB EB>A LC >
GRJMFKD MI>VBO. 4EB PL@@BO ?>II FP KLQ >IFSB; FQ J>HBP KL @ELF@BP >P QL ELT FQ TFII JLSB
QEOLRDELRQ QEB TLOIA. 2>QEBO, FQ FP >K FK>KFJ>QB L?GB@Q T>FQFKD QL ?B MRPEBA >KA MRIIBA ?V QEB
CLO@BP LC FQP BKSFOLKJBKQ.
(LT TLRIA TB JLABI > PL@@BO ?>II JLSFKD FK 0OL@BPPFKD? )C VLRfSB BSBO MOLDO>JJBA > @FO@IB
JLSFKD >@OLPP > TFKALT, QEBK VLRfSB MOL?>?IV TOFQQBK QEB CLIILTFKD IFKB LC @LAB.
9LR AO>T PLJB PE>MB >Q IL@>QFLK R. 7FQE B>@E CO>JB LC >KFJ>QFLK, VLR FK@OBJBKQ QEB S>IRB LC
R, OBAO>T QEB PE>MB >KA SLFI>dQEB FIIRPFLK LC JLQFLK! ->V?B VLR QLLH FQ > PQBM LO QTL CROQEBO,
>KA FK@IRABA > S IL@>QFLK, >P TBII >P S>OF>?IBP CLO PMBBA >ILKD QEB L >KA M >UBP.
0>OQ ) LC QEFP PQLOV TFII Q>HB RP LKB PQBM CROQEBO. 7BfOB DLFKD QL Q>HB QEBPB S>OF>?IBP RMJ??>
>KA SMJ??> >KA IB>OK ELT QLDBQEBO QEBV CLOJ > SB@QLO (Chapter 1 Chapter 1), QEB ?RFIAFKD ?IL@H LC
JLQFLK. 7B TLKfQ DBQ >KV KBT CRK@QFLK>IFQV LRQ LC QEFP, ?RQ FQ TFII ?RFIA > PLIFA CLRKA>QFLK CLO
QEB OBPQ LC QEB ?LLH.
/K@B TB HKLT > IFQQIB PLJBQEFKD >?LRQ SB@QLOP, TBfOB DLFKD QL NRF@HIV OB>IFWB QE>Q > CLO@B
(Chapter 2 Chapter 2) FP > SB@QLO. +F@H > PL@@BO ?>II >KA VLR >OB >MMIVFKD > CLO@B. 7E>Q ALBP > CLO@B
@>RPB >K L?GB@Q QL AL? !@@LOAFKD QL )P>>@ .BTQLK, CLO@B BNR>IP J>PP QFJBP >@@BIBO>QFLK. 4E>Q
CLO@B @>RPBP >K L?GB@Q QL >@@BIBO>QB. -LABIFKD CLO@BP TFII >IILT RP QL @OB>QB PVPQBJP TFQE
AVK>JF@ JLQFLK TEBOB L?GB@QP JLSB >@@LOAFKD QL > S>OFBQV LC ORIBP.
.LT, QE>Q PL@@BO ?>II QL TEF@E VLR >MMIFBA > CLO@B JFDEQ E>SB >IPL ?BBK PMFKKFKD. )C >K L?GB@Q
JLSBP >@@LOAFKD QL FQP >@@BIBO>QFLK, FQ @>K PMFK >@@LOAFKD QL FQP >KDRI>O >@@BIBO>QFLK (Chapter Chapter
33). 5KABOPQ>KAFKD QEB ?>PF@P LC >KDIBP >KA QOFDLKLJBQOV TFII >IILT RP QL JLABI OLQ>QFKD
L?GB@QP >P TBII >P DO>PM QEB MOFK@FMIBP ?BEFKA LP@FII>QFKD JLQFLK, IFHB > MBKARIRJ PTFKDFKD LO
> PMOFKD ?LRK@FKD.
/K@B TBfSB Q>@HIBA QEB ?>PF@P LC JLQFLK >KA CLO@BP CLO >K FKAFSFAR>I FK>KFJ>QB L?GB@Q, TBfII
IB>OK ELT QL J>HB QELRP>KAP RMLK QELRP>KAP LC QELPB L?GB@QP >KA J>K>DB QEBJ FK > PFKDIB
PVPQBJ @>IIBA > M>OQF@IB PVPQBJ (Chapter 4 Chapter 4). 0>OQF@IB PVPQBJP TFII >IILT RP QL ILLH >Q PLJB
>AS>K@BA CB>QROBP LC L?GB@Q-LOFBKQBA MOLDO>JJFKD, K>JBIV FKEBOFQ>K@B >KA MLIVJLOMEFPJ.
)K #E>MQBOP 1 QEOLRDE 4, >II LC QEB BU>JMIBP TFII ?B TOFQQBK COLJ gP@O>Q@EhdJB>KFKD QEB @LAB
CLO QEB >IDLOFQEJP AOFSFKD QEB JLQFLK LC QEB L?GB@QP TFII ?B TOFQQBK AFOB@QIV FK 0OL@BPPFKD. 7BfOB
@BOQ>FKIV KLQ QEB CFOPQ MOLDO>JJBOP BSBO QL @LKPFABO QEB FAB> LC PFJRI>QFKD MEVPF@P FK
>KFJ>QFLK, PL KBUQ TBfII BU>JFKB ELT MEVPF@P IF?O>OFBP (Chapter 5 Chapter 5) @>K ?B RPBA QL JLABI JLOB
R = R + 1;
R = R + RMJ??>;
S = S + SMJ??>;
4EB .>QROB LC #LAB (S005)
UFF
>AS>K@BA >KA PLMEFPQF@>QBA ?BE>SFLOP. 7BfII ILLH >Q "LU2$ (EQQM://TTT.?LU2A.LOD) >KA
QLUF@IF?P' 6BOIBQ 0EVPF@P M>@H>DB (EQQM://QLUF@IF?P.LOD/).
Part II: Its alive! Part II: Its alive!
7E>Q ALBP FQ JB>K QL JLABI IFCB? .LQ >K B>PV NRBPQFLK QL >KPTBO, ?RQ TB @>K ?BDFK ?V
?RFIAFKD L?GB@QP QE>Q E>SB >K >?FIFQV QL MBO@BFSB QEBFO BKSFOLKJBKQ. ,BQfP QEFKH >?LRQ QEFP CLO
> JLJBKQ. ! ?IL@H QE>Q C>IIP LCC > Q>?IB JLSBP >@@LOAFKD QL CLO@BP, >P ALBP > ALIMEFK
PTFJJFKD QEOLRDE QEB T>QBO. "RQ QEBOB FP > HBV AFCCBOBK@B. 4EB ?IL@H @>KKLQ AB@FAB QL IB>M
LCC QE>Q Q>?IB. 4EB ALIMEFK @>K AB@FAB QL IB>M LRQ LC QEB T>QBO. 4EB ALIMEFK @>K E>SB AOB>JP
>KA ABPFOBP. )Q @>K CBBI ERKDBO LO CB>O, >KA QELPB CBBIFKDP @>K FKCLOJ FQP JLSBJBKQP. "V
BU>JFKFKD QB@EKFNRBP ?BEFKA JLABIFKD >RQLKLJLRP >DBKQP (Chapter 6 Chapter 6), TB TFII ?OB>QEB IFCB
FKQL LRO FK>KFJ>QB L?GB@QP, >IILTFKD QEBJ QL J>HB AB@FPFLKP >?LRQ QEBFO JLSBJBKQP
>@@LOAFKD QL QEBFO RKABOPQ>KAFKD LC QEBFO BKSFOLKJBKQ.
4EOLRDE @LJ?FKFKD QEB @LK@BMQ LC >RQLKLJLRP >DBKQP TFQE TE>Q TB IB>OKBA >?LRQ
JLABIFKD PVPQBJP FK #E>MQBO 4, TBfII ILLH >Q JLABIP LC DOLRM ?BE>SFLO QE>Q BUEF?FQ QEB
MOLMBOQFBP LC @LJMIBUFQV. ! @LJMIBU PVPQBJ FP QVMF@>IIV ABCFKBA >P > PVPQBJ QE>Q FP gJLOB
QE>K QEB PRJ LC FQP M>OQP.h 7EFIB QEB FKAFSFAR>I BIBJBKQP LC QEB PVPQBJ J>V ?B FK@OBAF?IV
PFJMIB >KA B>PFIV RKABOPQLLA, QEB ?BE>SFLO LC QEB PVPQBJ >P > TELIB @>K ?B EFDEIV @LJMIBU,
FKQBIIFDBKQ, >KA AFCCF@RIQ QL MOBAF@Q. 4EFP TFII IB>A RP >T>V COLJ QEFKHFKD MROBIV >?LRQ
JLABIFKD JLQFLK >KA FKQL QEB OB>IJ LC ORIB-?>PBA PVPQBJP. 7E>Q @>K TB JLABI TFQE @BIIRI>O
>RQLJ>Q> (Chapter 7 Chapter 7), > PVPQBJ LC @BIIP IFSFKD LK > DOFA? 7E>Q QVMBP LC M>QQBOKP @>K TB
DBKBO>QB TFQE CO>@Q>IP (Chapter 8 Chapter 8), QEB DBLJBQOV LC K>QROB?
Part III: Intelligence Part III: Intelligence
7B J>AB QEFKDP JLSB. 4EBK TB D>SB QELPB QEFKDP ELMBP >KA AOB>JP >KA CB>OP, >ILKD TFQE
ORIBP QL IFSB ?V. 4EB I>PQ PQBM FK QEFP ?LLH TFII ?B QL J>HB LRO @OB>QFLKP BSBK PJ>OQBO. #>K
TB >MMIV QEB ?FLILDF@>I MOL@BPP LC BSLIRQFLK QL @LJMRQ>QFLK>I PVPQBJP (Chapter 9 Chapter 9) FK LOABO
QL BSLISB LRO L?GB@QP? 4>HFKD FKPMFO>QFLK COLJ QEB ERJ>K ?O>FK, @>K TB MOLDO>J >K >OQFCF@F>I
KBRO>I KBQTLOH (Chapter 10 Chapter 10) QE>Q @>K IB>OK COLJ FQP JFPQ>HBP >KA >IILT LRO L?GB@QP QL >A>MQ
QL QEBFO BKSFOLKJBKQ?
7BBH 1 7BBH 1 )KQOLAR@QFLK >KA 6B@QLOP (#E>MQBO 1)
7BBH 2 7BBH 2 &LO@BP (#E>MQBO 2)
P.6 This book as a syllabus P.6 This book as a syllabus
7EFIB QEB @LKQBKQ FK QEFP ?LLH @BOQ>FKIV J>HBP CLO >K FKQBKPB >KA EFDEIV @LJMOBPPBA
PBJBPQBO, ) E>SB ABPFDKBA FQ QL CFQ FKQL > CLROQBBK-TBBH @LROPB. .BSBOQEBIBPP, FQfP TLOQE
JBKQFLKFKD QE>Q ) CFKA QE>Q QEB ?LLH @E>MQBOP PLJBQFJBP TLOH ?BQQBO BUM>KABA >@OLPP
JRIQFMIB TBBHP. &LO BU>JMIB, QEB PVII>?RP CLO JV @LROPB DBKBO>IIV TLOHP LRQ >P CLIILTP:
0OBC>@B
UFFF
7BBH 3 7BBH 3 /P@FII>QFLKP (#E>MQBO 3)
7BBH 4 7BBH 4 0>OQF@IB 3VPQBJP (#E>MQBO 4)
7BBH 5 7BBH 5 0EVPF@P ,F?O>OFBP 0>OQ ) (#E>MQBO 5)
7BBH 6 7BBH 6 0EVPF@P ,F?O>OFBP 0>OQ )) & 3QBBOFKD (#E>MQBOP 5-6)
7BBH 7 7BBH 7 0OBPBKQ JFAQBOJ MOLGB@QP >?LRQ JLQFLK
7BBH 8 7BBH 8 #LJMIBU 3VPQBJP: &IL@HFKD >KA 1$ #BIIRI>O !RQLJ>Q> (#E>MQBOP 6-7)
7BBH 9 7BBH 9 #LJMIBU 3VPQBJP: 2$ #BIIRI>O !RQLJ>Q> >KA &O>@Q>IP (#E>MQBOP 7-8)
7BBH 10 7BBH 10 'BKBQF@ !IDLOFQEJP (#E>MQBO 9)
7BBH 11 7BBH 11 .BRO>I .BQTLOHP (#E>MQBO 10)
7BBHP 12-13 7BBHP 12-13 &FK>I MOLGB@Q TLOHPELM
7BBH 14 7BBH 14 &FK>I MOLGB@Q MOBPBKQ>QFLK
)C VLR >OB @LKPFABOFKD RPFKD QEFP QBUQ CLO > @LROPB LO TLOHPELM, MIB>PB CBBI COBB QL @LKQ>@Q JB.
) ELMB QL BSBKQR>IIV OBIB>PB > @LJM>KFLK PBQ LC SFABLP >KA PIFAB MOBPBKQ>QFLKP >P
PRMMIBJBKQ>OV BAR@>QFLK>I J>QBOF>IP.
P.7 The Ecosystem Project P.7 The Ecosystem Project
!P JR@E >P )fA IFHB QL MOBQBKA VLR @LRIA IB>OK BSBOVQEFKD ?V @ROIFKD RM FK > @LJCV @E>FO >KA
OB>AFKD PLJB MOLPB >?LRQ MOLDO>JJFKD, QL IB>OK MOLDO>JJFKD, VLRfOB OB>IIV DLFKD QL E>SB QL
AL PLJB MOLDO>JJFKD. 9LR JFDEQ CFKA FQ EBIMCRI QL HBBM FK JFKA > MOLGB@Q FAB> (LO QTL) QL
ABSBILM >P > PBQ LC BUBO@FPBP TEFIB DLFKD COLJ @E>MQBO QL @E>MQBO. )K C>@Q, TEBK QB>@EFKD QEB
.>QROB LC #LAB @LROPB >Q )40, ) E>SB LCQBK CLRKA QE>Q PQRABKQP BKGLV ?RFIAFKD > PFKDIB MOLGB@Q,
PQBM ?V PQBM, TBBH ?V TBBH, LSBO QEB @LROPB LC > PBJBPQBO.
!Q QEB BKA LC B>@E @E>MQBO, VLRfII CFKA > PBOFBP LC BUBO@FPBP CLO LKB PR@E MOLGB@QdBUBO@FPBP
QE>Q ?RFIA LK B>@E LQEBO, LKB QLMF@ >Q > QFJB. #LKPFABO QEB CLIILTFKD P@BK>OFL. 9LRfSB ?BBK
>PHBA ?V > P@FBK@B JRPBRJ QL ABSBILM QEB PLCQT>OB CLO > KBT BUEF?FQd4EB $FDFQ>I %@LPVPQBJ,
> TLOIA LC >KFJ>QBA, MOL@BARO>I @OB>QROBP QE>Q IFSB LK > MOLGB@QFLK P@OBBK CLO SFPFQLOP QL BKGLV
>P QEBV BKQBO QEB JRPBRJ. ) ALKfQ JB>K QL PRDDBPQ QE>Q QEFP FP > M>OQF@RI>OIV FKKLS>QFSB LO
@OB>QFSB @LK@BMQ. 2>QEBO, TBfII RPB QEFP BU>JMIB MOLGB@Q FAB> >P > IFQBO>I OBMOBPBKQ>QFLK LC QEB
@LKQBKQ FK QEB ?LLH, ABJLKPQO>QFKD ELT QEB BIBJBKQP CFQ QLDBQEBO FK > PFKDIB PLCQT>OB MOLGB@Q.
) BK@LRO>DB VLR QL ABSBILM VLRO LTK FAB>, LKB QE>Q FP JLOB >?PQO>@Q >KA @OB>QFSB FK FQP
QEFKHFKD.
P.8 Where do I find the code online and submit P.8 Where do I find the code online and submit
feedback? feedback?
&LO >II QEFKDP ?LLH-OBI>QBA, MIB>PB SFPFQ QEB .>QROB LC #LAB TB?PFQB
(EQQM://TTT.K>QROBLC@LAB.@LJ). 4EB O>T PLRO@B QBUQ LC QEB ?LLH >KA >II LC QEB FIIRPQO>QFLKP >OB
4EB .>QROB LC #LAB (S005)
UFS
LK 'FQ(R? (EQQM://DFQER?.@LJ/PEFCCJ>K/4EB-.>QROB-LC-#LAB). 0IB>PB IB>SB CBBA?>@H >KA
PR?JFQ @LOOB@QFLKP RPFKD 'FQ(R? FPPRBP.
4EB PLRO@B @LAB CLO >II LC QEB BU>JMIBP (>KA BUBO@FPBP) FP >IPL >S>FI>?IB LK 'FQ(R?
(EQQM://DFQER?.@LJ/PEFCCJ>K/4EB-.>QROB-LC-#LAB-%U>JMIBP). 4EB @E>MQBOP QEBJPBISBP
FK@IRAB @LAB PKFMMBQP FK-IFKB TFQE QEB QBUQ. (LTBSBO, ) T>KQ QL JBKQFLK QE>Q FK J>KV @>PBP, )
E>SB PELOQBKBA LO PFJMIFCFBA QEB @LAB PKFMMBQP FK LOABO QL FIIRPQO>QB > PMB@FCF@ MLFKQ. )K >II
@>PBP, QEB CRII @LAB TFQE @LJJBKQP @>K ?B CLRKA SF> 'FQ(R?.
)C VLR E>SB NRBPQFLKP >?LRQ QEB @LAB FQPBIC, ) TLRIA PRDDBPQ MLPQFKD QEBJ LK QEB 0OL@BPPFKD
CLORJ (EQQM://CLORJ.MOL@BPPFKD.LOD).
0OBC>@B
US
Table of Contents Table of Contents
Acknowledgments
!.1 ! IFQQIB ?FQ LC EFPQLOV
!.2 +F@HPQ>OQBO
Preface
0.1 7E>Q FP QEFP ?LLH?
0.2 ! TLOA >?LRQ 0OL@BPPFKD
0.3 7E>Q AL VLR KBBA QL HKLT?
0.4 7E>Q >OB VLR RPFKD QL OB>A QEFP ?LLH?
0.5 4EB gPQLOVh LC QEFP ?LLH
0.6 4EFP ?LLH >P > PVII>?RP
0.7 4EB %@LPVPQBJ 0OLGB@Q
0.8 7EBOB AL ) CFKA QEB @LAB LKIFKB >KA PR?JFQ CBBA?>@H?
Introduction
).1 2>KALJ 7>IHP
).2 4EB 2>KALJ 7>IHBO #I>PP
).3 0OL?>?FIFQV >KA .LK-5KFCLOJ $FPQOF?RQFLKP
iv
S
SFF
ix
FU
U
U
UF
UF
UFFF
UFS
UFS
1
1
2
7
4EB .>QROB LC #LAB (S005)
USF
).4 ! .LOJ>I $FPQOF?RQFLK LC 2>KALJ .RJ?BOP
).5 ! #RPQLJ $FPQOF?RQFLK LC 2>KALJ .RJ?BOP
).6 0BOIFK .LFPB (! 3JLLQEBO !MMOL>@E)
).7 /KT>OA
Chapter 1. Vectors
1.1 6B@QLOP, 9LR #LJMIBQB -B
1.2 6B@QLOP CLO 0OL@BPPFKD 0OLDO>JJBOP
1.3 6B@QLO !AAFQFLK
1.4 -LOB 6B@QLO ->QE
1.5 6B@QLO ->DKFQRAB
1.6 .LOJ>IFWFKD 6B@QLOP
1.7 6B@QLO -LQFLK: 6BIL@FQV
1.8 6B@QLO -LQFLK: !@@BIBO>QFLK
1.9 3Q>QF@ SP. .LK-3Q>QF@ &RK@QFLKP
1.10 )KQBO>@QFSFQV TFQE !@@BIBO>QFLK
Chapter 2. Forces
2.1 &LO@BP >KA .BTQLKfP ,>TP LC -LQFLK
2.2 &LO@BP >KA 0OL@BPPFKDd.BTQLKfP 3B@LKA ,>T >P > &RK@QFLK
2.3 &LO@B !@@RJRI>QFLK
2.4 $B>IFKD TFQE ->PP
2.5 #OB>QFKD &LO@BP
2.6 'O>SFQV LK %>OQE >KA -LABIFKD > &LO@B
2.7 &OF@QFLK
2.8 !FO >KA &IRFA 2BPFPQ>K@B
2.9 'O>SFQ>QFLK>I !QQO>@QFLK
2.10 %SBOVQEFKD !QQO>@QP (LO 2BMBIP) %SBOVQEFKD
Chapter 3. Oscillation
11
14
17
26
27
28
30
33
37
42
43
45
49
54
57
63
63
67
68
70
73
77
80
83
88
97
101
4>?IB LC #LKQBKQP
USFF
3.1 !KDIBP
3.2 !KDRI>O -LQFLK
3.3 4OFDLKLJBQOV
3.4 0LFKQFKD FK QEB $FOB@QFLK LC -LSBJBKQ
3.5 0LI>O SP. #>OQBPF>K #LLOAFK>QBP
3.6 /P@FII>QFLK !JMIFQRAB >KA 0BOFLA
3.7 /P@FII>QFLK TFQE !KDRI>O 6BIL@FQV
3.8 7>SBP
3.9 4OFDLKLJBQOV >KA &LO@BP: 4EB 0BKARIRJ
3.10 3MOFKD &LO@BP
Chapter 4. Particle Systems
4.1 7EV 7B .BBA 0>OQF@IB 3VPQBJP
4.2 ! 3FKDIB 0>OQF@IB
4.3 4EB !OO>V,FPQ
4.4 4EB 0>OQF@IB 3VPQBJ #I>PP
4.5 ! 3VPQBJ LC 3VPQBJP
4.6 )KEBOFQ>K@B >KA 0LIVJLOMEFPJ: !K )KQOLAR@QFLK
4.7 )KEBOFQ>K@B ">PF@P
4.8 0>OQF@IBP TFQE )KEBOFQ>K@B
4.9 0LIVJLOMEFPJ ">PF@P
4.10 0>OQF@IB 3VPQBJP TFQE 0LIVJLOMEFPJ
4.11 0>OQF@IB 3VPQBJP TFQE &LO@BP
4.12 0>OQF@IB 3VPQBJP TFQE 2BMBIIBOP
4.13 )J>DB 4BUQROBP >KA !AAFQFSB "IBKAFKD
Chapter 5. Physics Libraries
5.1 7E>Q )P "LU2$ >KA 7EBK )P )Q 5PBCRI?
5.2 'BQQFKD "LU2$ FK 0OL@BPPFKD
5.3 "LU2$ ">PF@P
101
104
108
109
112
116
119
122
127
134
143
144
145
149
155
157
160
162
166
168
170
173
178
183
189
190
192
192
4EB .>QROB LC #LAB (S005)
USFFF
5.4 ,FSFKD FK > "LU2$ 7LOIA
5.5 "RFIAFKD > "LU2$ ?LAV
5.6 4EOBBfP #LJM>KV: "LAFBP >KA 3E>MBP >KA &FUQROBP
5.7 "LU2$ >KA 0OL@BPPFKD: 2BRKFQBA >KA )Q &BBIP 3L 'LLA
5.8 &FUBA "LU2$ /?GB@QP
5.9 ! #ROSV "LRKA>OV
5.10 #LJMIBU &LOJP
5.11 &BBIFKD !QQ>@EBAd"LU2$ *LFKQP
5.12 "OFKDFKD )Q !II ">@H (LJB QL &LO@BP
5.13 #LIIFPFLK %SBKQP
5.14 ! "OFBC )KQBOIRABd)KQBDO>QFLK -BQELAP
5.15 6BOIBQ 0EVPF@P TFQE QLUF@IF?P
5.16 0>OQF@IBP >KA 3MOFKDP FK QLUF@IF?P
5.17 0RQQFKD )Q !II 4LDBQEBO: ! 3FJMIB )KQBO>@QFSB 3MOFKD
5.18 #LKKB@QBA 3VPQBJP 0>OQ ): 3QOFKD
5.19 #LKKB@QBA 3VPQBJP 0>OQ )): &LO@B-$FOB@QBA 'O>ME
5.20 !QQO>@QFLK >KA 2BMRIPFLK "BE>SFLOP
Chapter 6. Autonomous Agents
6.1 &LO@BP COLJ 7FQEFK
6.2 6BEF@IBP >KA 3QBBOFKD
6.3 4EB 3QBBOFKD &LO@B
6.4 !OOFSFKD "BE>SFLO
6.5 9LRO /TK $BPFOBP: $BPFOBA 6BIL@FQV
6.6 &ILT &FBIAP
6.7 4EB $LQ 0OLAR@Q
6.8 0>QE &LIILTFKD
6.9 0>QE &LIILTFKD TFQE -RIQFMIB 3BDJBKQP
6.10 #LJMIBU 3VPQBJP
6.11 'OLRM "BE>SFLOP (LO: ,BQfP KLQ ORK FKQL B>@E LQEBO)
196
198
200
203
209
211
215
222
232
234
238
241
244
247
249
253
256
260
260
262
263
270
274
276
282
286
294
298
300
4>?IB LC #LKQBKQP
UFU
6.12 #LJ?FK>QFLKP
6.13 &IL@HFKD
6.14 !IDLOFQEJF@ %CCF@FBK@V (LO: 7EV ALBP JV $(*%! ORK PL PILTIV?)
6.15 ! &BT ,>PQ .LQBP: /MQFJFW>QFLK 4OF@HP
Chapter 7. Cellular Automata
7.1 7E>Q )P > #BIIRI>O !RQLJ>QLK?
7.2 %IBJBKQ>OV #BIIRI>O !RQLJ>Q>
7.3 (LT QL 0OLDO>J >K %IBJBKQ>OV #!
7.4 $O>TFKD >K %IBJBKQ>OV #!
7.5 7LICO>J #I>PPFCF@>QFLK
7.6 4EB '>JB LC ,FCB
7.7 0OLDO>JJFKD QEB '>JB LC ,FCB
7.8 /?GB@Q-/OFBKQBA #BIIP
7.9 6>OF>QFLKP LC 4O>AFQFLK>I #!
Chapter 8. Fractals
8.1 7E>Q )P > &O>@Q>I?
8.2 2B@ROPFLK
8.3 4EB #>KQLO 3BQ TFQE > 2B@ROPFSB &RK@QFLK
8.4 4EB +L@E #ROSB >KA QEB !OO>V,FPQ 4B@EKFNRB
8.5 4OBBP
8.6 ,-PVPQBJP
Chapter 9. The Evolution of Code
9.1 'BKBQF@ !IDLOFQEJP: )KPMFOBA ?V !@QR>I %SBKQP
9.2 7EV 5PB 'BKBQF@ !IDLOFQEJP?
9.3 $>OTFKF>K .>QRO>I 3BIB@QFLK
9.4 4EB 'BKBQF@ !IDLOFQEJ, 0>OQ ): #OB>QFKD > 0LMRI>QFLK
306
308
315
317
323
324
325
330
336
340
342
345
349
351
355
356
358
363
366
374
382
390
391
392
394
395
4EB .>QROB LC #LAB (S005)
UU
9.5 4EB 'BKBQF@ !IDLOFQEJ, 0>OQ )): 3BIB@QFLK
9.6 4EB 'BKBQF@ !IDLOFQEJ, 0>OQ ))): 2BMOLAR@QFLK
9.7 #LAB CLO #OB>QFKD QEB 0LMRI>QFLK
9.8 'BKBQF@ !IDLOFQEJP: 0RQQFKD )Q !II 4LDBQEBO
9.9 'BKBQF@ !IDLOFQEJP: ->HB 4EBJ 9LRO /TK
9.10 %SLISFKD &LO@BP: 3J>OQ 2L@HBQP
9.11 3J>OQ 2L@HBQP: 0RQQFKD )Q !II 4LDBQEBO
9.12 )KQBO>@QFSB 3BIB@QFLK
9.13 %@LPVPQBJ 3FJRI>QFLK
Chapter 10. Neural Networks
10.1 !OQFCF@F>I .BRO>I .BQTLOHP: )KQOLAR@QFLK >KA !MMIF@>QFLK
10.2 4EB 0BO@BMQOLK
10.3 3FJMIB 0>QQBOK 2B@LDKFQFLK 5PFKD > 0BO@BMQOLK
10.4 #LAFKD QEB 0BO@BMQOLK
10.5 ! 3QBBOFKD 0BO@BMQOLK
10.6 )QfP > g.BQTLOH,h 2BJBJ?BO?
10.7 .BRO>I .BQTLOH $F>DO>J
10.8 !KFJ>QFKD &BBA &LOT>OA
Further Reading
"LLHP
0>MBOP >KA !OQF@IBP
Index
397
399
402
409
413
420
425
431
435
444
445
448
450
452
460
466
468
473
481
481
482
484
4>?IB LC #LKQBKQP
UUF
Introduction Introduction
I! .: AD< D6A5 ;.AB?2.J
Woody Allen
(BOB TB >OB: QEB ?BDFKKFKD. 7BII, >IJLPQ QEB ?BDFKKFKD. )C FQfP ?BBK > TEFIB PFK@B VLRfSB ALKB
>KV MOLDO>JJFKD FK 0OL@BPPFKD (LO >KV J>QE, CLO QE>Q J>QQBO), QEFP FKQOLAR@QFLK TFII DBQ VLRO
JFKA ?>@H FKQL @LJMRQ>QFLK>I QEFKHFKD ?BCLOB TB >MMOL>@E PLJB LC QEB JLOB AFCCF@RIQ >KA
@LJMIBU J>QBOF>I.
)K #E>MQBO 1, TBfOB DLFKD QL Q>IH >?LRQ QEB @LK@BMQ LC > SB@QLO >KA ELT FQ TFII PBOSB >P QEB
?RFIAFKD ?IL@H CLO PFJRI>QFKD JLQFLK QEOLRDELRQ QEFP ?LLH. "RQ ?BCLOB TB Q>HB QE>Q PQBM, IBQfP
QEFKH >?LRQ TE>Q FQ JB>KP CLO PLJBQEFKD QL PFJMIV JLSB >OLRKA QEB P@OBBK. ,BQfP ?BDFK TFQE
LKB LC QEB ?BPQ-HKLTK >KA PFJMIBPQ PFJRI>QFLKP LC JLQFLKdQEB O>KALJ T>IH.
I.1 Random Walks I.1 Random Walks
)J>DFKB VLR >OB PQ>KAFKD FK QEB JFAAIB LC > ?>I>K@B ?B>J. %SBOV QBK PB@LKAP, VLR CIFM > @LFK.
(B>AP, Q>HB > PQBM CLOT>OA. 4>FIP, Q>HB > PQBM ?>@HT>OA. 4EFP FP > O>KALJ T>IHd> M>QE ABCFKBA
>P > PBOFBP LC O>KALJ PQBMP. 3QBMMFKD LCC QE>Q ?>I>K@B ?B>J >KA LKQL QEB CILLO, VLR @LRIA
MBOCLOJ > O>KALJ T>IH FK QTL AFJBKPFLKP ?V CIFMMFKD QE>Q P>JB @LFK QTF@B TFQE QEB CLIILTFKD
OBPRIQP:
4EB .>QROB LC #LAB (S005)
1
Flip 1 Flip 1 Flip 2 Flip 2 Result Result
(B>AP (B>AP 3QBM CLOT>OA.
(B>AP 4>FIP 3QBM OFDEQ.
4>FIP (B>AP 3QBM IBCQ.
4>FIP 4>FIP 3QBM ?>@HT>OA.
9BP, QEFP J>V PBBJ IFHB > M>OQF@RI>OIV RKPLMEFPQF@>QBA >IDLOFQEJ. .BSBOQEBIBPP, O>KALJ
T>IHP @>K ?B RPBA QL JLABI MEBKLJBK> QE>Q L@@RO FK QEB OB>I TLOIA, COLJ QEB JLSBJBKQP LC
JLIB@RIBP FK > D>P QL QEB ?BE>SFLO LC > D>J?IBO PMBKAFKD > A>V >Q QEB @>PFKL. !P CLO RP, TB
?BDFK QEFP ?LLH PQRAVFKD > O>KALJ T>IH TFQE QEOBB DL>IP FK JFKA.
1. 7B KBBA QL OBSFBT > MOLDO>JJFKD @LK@BMQ @BKQO>I QL QEFP ?LLHdL?GB@Q-LOFBKQBA
MOLDO>JJFKD. 4EB O>KALJ T>IHBO TFII PBOSB >P > QBJMI>QB CLO ELT TB TFII RPB
L?GB@Q-LOFBKQBA ABPFDK QL J>HB QEFKDP QE>Q JLSB >OLRKA > 0OL@BPPFKD TFKALT.
2. 4EB O>KALJ T>IH FKPQFD>QBP QEB QTL NRBPQFLKP QE>Q TB TFII >PH LSBO >KA LSBO >D>FK
QEOLRDELRQ QEFP ?LLH: g(LT AL TB ABCFKB QEB ORIBP QE>Q DLSBOK QEB ?BE>SFLO LC LRO
L?GB@QP?h >KA QEBK, g(LT AL TB FJMIBJBKQ QEBPB ORIBP FK 0OL@BPPFKD?h
3. 4EOLRDELRQ QEB ?LLH, TBfII MBOFLAF@>IIV KBBA > ?>PF@ RKABOPQ>KAFKD LC
O>KALJKBPP, MOL?>?FIFQV, >KA 0BOIFK KLFPB. 4EB O>KALJ T>IH TFII >IILT RP QL
ABJLKPQO>QB > CBT HBV MLFKQP QE>Q TFII @LJB FK E>KAV I>QBO.
I.2 The Random Walker Class I.2 The Random Walker Class
,BQfP OBSFBT > ?FQ LC L?GB@Q-LOFBKQBA MOLDO>JJFKD (//0) CFOPQ ?V ?RFIAFKD > 4;FE?L L?GB@Q.
4EFP TFII ?B LKIV > @ROPLOV OBSFBT. )C VLR E>SB KBSBO TLOHBA TFQE //0 ?BCLOB, VLR J>V T>KQ
PLJBQEFKD JLOB @LJMOBEBKPFSB; )fA PRDDBPQ PQLMMFKD EBOB >KA OBSFBTFKD QEB ?>PF@P LK QEB
0OL@BPPFKD TB?PFQB (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/L?GB@QP/) ?BCLOB @LKQFKRFKD.
!K :-50.? :-50.? FK 0OL@BPPFKD FP >K BKQFQV QE>Q E>P ?LQE A>Q> >KA CRK@QFLK>IFQV. 7B >OB ILLHFKD QL
ABPFDK > 4;FE?L L?GB@Q QE>Q ?LQE HBBMP QO>@H LC FQP A>Q> (TEBOB FQ BUFPQP LK QEB P@OBBK) >KA
E>P QEB @>M>?FIFQV QL MBOCLOJ @BOQ>FK >@QFLKP (PR@E >P AO>T FQPBIC LO Q>HB > PQBM).
! .7,>> .7,>> FP QEB QBJMI>QB CLO ?RFIAFKD >@QR>I FKPQ>K@BP LC L?GB@QP. 4EFKH LC > @I>PP >P QEB
@LLHFB @RQQBO; QEB L?GB@QP >OB QEB @LLHFBP QEBJPBISBP.
,BQfP ?BDFK ?V ABCFKFKD QEB 4;FE?L @I>PPdTE>Q FQ JB>KP QL ?B > 4;FE?L L?GB@Q. 4EB 4;FE?L
LKIV KBBAP QTL MFB@BP LC A>Q>d> KRJ?BO CLO FQP U-IL@>QFLK >KA LKB CLO FQP V-IL@>QFLK.
=F;MM 4;FE?L U
)KQOLAR@QFLK
2
%SBOV @I>PP JRPQ E>SB > @LKPQOR@QLO, > PMB@F>I CRK@QFLK QE>Q FP @>IIBA TEBK QEB L?GB@Q FP CFOPQ
@OB>QBA. 9LR @>K QEFKH LC FQ >P QEB L?GB@QfP M?NOJ(). 4EBOB, TBfII FKFQF>IFWB QEB 4;FE?LfP PQ>OQFKD
IL@>QFLK (FK QEFP @>PB, QEB @BKQBO LC QEB TFKALT).
&FK>IIV, FK >AAFQFLK QL A>Q>, @I>PPBP @>K ?B ABCFKBA TFQE CRK@QFLK>IFQV. )K QEFP BU>JMIB, > 4;FE?L
E>P QTL CRK@QFLKP. 7B CFOPQ TOFQB > CRK@QFLK QE>Q >IILTP QEB L?GB@Q QL AFPMI>V FQPBIC (>P > TEFQB
ALQ).
4EB PB@LKA CRK@QFLK AFOB@QP QEB 4;FE?L L?GB@Q QL Q>HB > PQBM. .LT, QEFP FP TEBOB QEFKDP DBQ >
?FQ JLOB FKQBOBPQFKD. 2BJBJ?BO QE>Q CILLO LK TEF@E TB TBOB Q>HFKD O>KALJ PQBMP? 7BII, KLT
TB @>K RPB > 0OL@BPPFKD TFKALT FK QE>Q P>JB @>M>@FQV. 4EBOB >OB CLRO MLPPF?IB PQBMP. ! PQBM
QL QEB OFDEQ @>K ?B PFJRI>QBA ?V FK@OBJBKQFKD R (R++); QL QEB IBCQ ?V AB@OBJBKQFKD R (R--);
CLOT>OA ?V DLFKD ALTK > MFUBI (S++); >KA ?>@HT>OA ?V DLFKD RM > MFUBI (S--). (LT AL TB MF@H
COLJ QEBPB CLRO @ELF@BP? %>OIFBO TB PQ>QBA QE>Q TB @LRIA CIFM QTL @LFKP. )K 0OL@BPPFKD,
ELTBSBO, TEBK TB T>KQ QL O>KALJIV @ELLPB COLJ > IFPQ LC LMQFLKP, TB @>K MF@H > O>KALJ
KRJ?BO RPFKD L;H>IG().
4EB >?LSB IFKB LC @LAB MF@HP > O>KALJ CIL>QFKD MLFKQ KRJ?BO ?BQTBBK 0 >KA 4 >KA @LKSBOQP FQ
QL >K FKQBDBO, TFQE > OBPRIQ LC 0, 1, 2, LO 3. 4B@EKF@>IIV PMB>HFKD, QEB EFDEBPQ KRJ?BO TFII KBSBO
?B 4.0, ?RQ O>QEBO 3.999999999 (TFQE >P J>KV 9P >P QEBOB >OB AB@FJ>I MI>@BP); PFK@B QEB
MOL@BPP LC @LKSBOQFKD QL >K FKQBDBO ILMP LCC QEB AB@FJ>I MI>@B, QEB EFDEBPQ CHN TB @>K DBQ FP 3.
.BUQ, TB Q>HB QEB >MMOLMOF>QB PQBM (IBCQ, OFDEQ, RM, LO ALTK) ABMBKAFKD LK TEF@E O>KALJ
KRJ?BO T>P MF@HBA.
Objects have data. CHN R;
CHN S;
Objects have a constructor where they are
initialized.
4;FE?L() U
R = QC>NB/2;
S = B?CABN/2;
W
Objects have functions. PIC> >CMJF;S() U
MNLIE?(0);
JICHN(R,S);
W
PIC> MN?J() U
0, 1, 2, or 3 CHN =BIC=? = CHN(L;H>IG(4));
4EB .>QROB LC #LAB (S005)
3
.LT QE>Q TBfSB TOFQQBK QEB @I>PP, FQfP QFJB QL J>HB >K >@QR>I 4;FE?L L?GB@Q FK QEB J>FK M>OQ
LC LRO PHBQ@EdM?NOJ() >KA >L;Q(). !PPRJFKD TB >OB ILLHFKD QL JLABI > PFKDIB O>KALJ
T>IH, TB AB@I>OB LKB DIL?>I S>OF>?IB LC QVMB 4;FE?L.
4EBK TB @OB>QB QEB L?GB@Q FK M?NOJ() ?V @>IIFKD QEB @LKPQOR@QLO TFQE QEB H?Q LMBO>QLO.
Example I.1: Traditional random walk
!57< H=A9 MCI G99 H<9 56CJ9 !L5AD@9 <958=B; =B H<=G 6CC?, =H A95BG H<9F9 =G 5
7CFF9GDCB8=B; 7C89 9L5AD@9 5J5=@56@9 CB #=H$I6 (<HHD://;=H<I6.7CA/G<=::A5B//<9-*5HIF9-
C:-CC89-!L5AD@9G).
&FK>IIV, AROFKD B>@E @V@IB QEOLRDE >L;Q(), TB >PH QEB 4;FE?L QL Q>HB > PQBM >KA AO>T > ALQ.
3FK@B TB LKIV AO>T QEB ?>@HDOLRKA LK@B FK M?NOJ(), O>QEBO QE>K @IB>OFKD FQ @LKQFKR>IIV
B>@E QFJB QEOLRDE >L;Q(), TB PBB QEB QO>FI LC QEB O>KALJ T>IH FK LRO 0OL@BPPFKD TFKALT.
The random choice determines our step. C@ (=BIC=? == 0) U
R++;
W ?FM? C@ (=BIC=? == 1) U
R--;
W ?FM? C@ (=BIC=? == 2) U
S++;
W ?FM? U
S--;
W
W
W
A Walker object 4;FE?L Q;
PIC> M?NOJ() U
MCT?(640,360);
Create the Walker. M = D;M 17BA;H();
<;=EALIOH>(255);
W
PIC> >L;Q() U
Call functions on the Walker. M.IJ;F();
M.:?IFB7O();
W
)KQOLAR@QFLK
4
4EBOB >OB > @LRMIB FJMOLSBJBKQP TB @LRIA J>HB QL QEB O>KALJ T>IHBO. &LO LKB, QEFP
4;FE?LfP PQBM @ELF@BP >OB IFJFQBA QL CLRO LMQFLKPdRM, ALTK, IBCQ, >KA OFDEQ. "RQ >KV DFSBK MFUBI
FK QEB TFKALT E>P BFDEQ MLPPF?IB KBFDE?LOP, >KA > KFKQE MLPPF?FIFQV FP QL PQ>V FK QEB P>JB
MI>@B.
4L FJMIBJBKQ > 4;FE?L L?GB@Q QE>Q @>K PQBM QL >KV KBFDE?LOFKD MFUBI (LO PQ>V MRQ), TB @LRIA
MF@H > KRJ?BO ?BQTBBK 0 >KA 8 (KFKB MLPPF?IB @ELF@BP). (LTBSBO, > JLOB BCCF@FBKQ T>V QL
TOFQB QEB @LAB TLRIA ?B QL PFJMIV MF@H COLJ QEOBB MLPPF?IB PQBMP >ILKD QEB U->UFP (-1, 0, LO 1)
>KA QEOBB MLPPF?IB PQBMP >ILKD QEB V->UFP.
4>HFKD QEFP CROQEBO, TB @LRIA RPB CIL>QFKD MLFKQ KRJ?BOP (F.B. AB@FJ>I KRJ?BOP) CLO R >KA S
FKPQB>A >KA JLSB >@@LOAFKD QL >K >O?FQO>OV O>KALJ S>IRB ?BQTBBK -1 >KA 1.
F64B?2 !.1
PIC> MN?J() U
Yields -1, 0, or 1 CHN MN?JR = CHN(L;H>IG(3))-1;
CHN MN?JS = CHN(L;H>IG(3))-1;
R += MN?JR;
S += MN?JS;
W
PIC> MN?J() U
Yields any floating point number between
-1.0 and 1.0
@FI;N MN?JR = L;H>IG(-1, 1);
@FI;N MN?JS = L;H>IG(-1, 1);
4EB .>QROB LC #LAB (S005)
5
!II LC QEBPB S>OF>QFLKP LK QEB gQO>AFQFLK>Ih O>KALJ T>IH E>SB LKB QEFKD FK @LJJLK: >Q >KV
JLJBKQ FK QFJB, QEB MOL?>?FIFQV QE>Q QEB 4;FE?L TFII Q>HB > PQBM FK > DFSBK AFOB@QFLK FP BNR>I
QL QEB MOL?>?FIFQV QE>Q QEB 4;FE?L TFII Q>HB > PQBM FK >KV AFOB@QFLK. )K LQEBO TLOAP, FC QEBOB
>OB CLRO MLPPF?IB PQBMP, QEBOB FP > 1 FK 4 (LO 25%) @E>K@B QEB 4;FE?L TFII Q>HB >KV DFSBK PQBM.
7FQE KFKB MLPPF?IB PQBMP, FQfP > 1 FK 9 (LO 11.1%) @E>K@B.
#LKSBKFBKQIV, QEFP FP ELT QEB L;H>IG() CRK@QFLK TLOHP. 0OL@BPPFKDfP O>KALJ KRJ?BO
DBKBO>QLO (TEF@E LMBO>QBP ?BEFKA QEB P@BKBP) MOLAR@BP TE>Q FP HKLTK >P > gRKFCLOJh
AFPQOF?RQFLK LC KRJ?BOP. 7B @>K QBPQ QEFP AFPQOF?RQFLK TFQE > 0OL@BPPFKD PHBQ@E QE>Q @LRKQP
B>@E QFJB > O>KALJ KRJ?BO FP MF@HBA >KA DO>MEP FQ >P QEB EBFDEQ LC > OB@Q>KDIB.
Example I.2: Random number distribution
R += MN?JR;
S += MN?JS;
W
An array to keep track of how often random
numbers are picked
CHN89 L;H>IG!IOHNM;
PIC> M?NOJ() U
MCT?(640,240);
L;H>IG!IOHNM = H?Q CHN8209;
W
PIC> >L;Q() U
<;=EALIOH>(255);
Pick a random number and increase the
count.
CHN CH>?R = CHN(L;H>IG(L;H>IG!IOHNM.F?HANB));
L;H>IG!IOHNM8CH>?R9++;
MNLIE?(0);
@CFF(175);
CHN Q = QC>NB/L;H>IG!IOHNM.F?HANB;
)KQOLAR@QFLK
6
4EB >?LSB P@OBBKPELQ PELTP QEB OBPRIQ LC QEB PHBQ@E ORKKFKD CLO > CBT JFKRQBP. .LQF@B ELT
B>@E ?>O LC QEB DO>ME AFCCBOP FK EBFDEQ. /RO P>JMIB PFWB (F.B. QEB KRJ?BO LC O>KALJ KRJ?BOP
TBfSB MF@HBA) FP O>QEBO PJ>II >KA QEBOB >OB PLJB L@@>PFLK>I AFP@OBM>K@FBP, TEBOB @BOQ>FK
KRJ?BOP >OB MF@HBA JLOB LCQBK. /SBO QFJB, TFQE > DLLA O>KALJ KRJ?BO DBKBO>QLO, QEFP TLRIA
BSBK LRQ.
Pseudo-Random Numbers Pseudo-Random Numbers
4EB O>KALJ KRJ?BOP TB DBQ COLJ QEB L;H>IG() CRK@QFLK >OB KLQ QORIV O>KALJ;
QEBOBCLOB QEBV >OB HKLTK >P gMPBRAL-O>KALJ.h 4EBV >OB QEB OBPRIQ LC > J>QEBJ>QF@>I
CRK@QFLK QE>Q PFJRI>QBP O>KALJKBPP. 4EFP CRK@QFLK TLRIA VFBIA > M>QQBOK LSBO QFJB, ?RQ
QE>Q QFJB MBOFLA FP PL ILKD QE>Q CLO RP, FQfP GRPQ >P DLLA >P MROB O>KALJKBPP!
Graphing the results @IL (CHN R = 0; R < L;H>IG!IOHNM.F?HANB; R++) U
L?=N(R*Q,B?CABN-L;H>IG!IOHNM8R9,Q-1,L;H>IG!IOHNM8R9);
W
W
#OB>QB > O>KALJ T>IHBO QE>Q E>P > QBKABK@V QL JLSB ALTK >KA QL QEB OFDEQ. (7BfII PBB
QEB PLIRQFLK QL QEFP FK QEB KBUQ PB@QFLK.)
Exercise I.1 Exercise I.1
I.3 Probability and Non-Uniform Distributions I.3 Probability and Non-Uniform Distributions
2BJBJ?BO TEBK VLR CFOPQ PQ>OQBA MOLDO>JJFKD FK 0OL@BPPFKD? 0BOE>MP VLR T>KQBA QL AO>T >
ILQ LC @FO@IBP LK QEB P@OBBK. 3L VLR P>FA QL VLROPBIC: g/E, ) HKLT. )fII AO>T >II QEBPB @FO@IBP >Q
O>KALJ IL@>QFLKP, TFQE O>KALJ PFWBP >KA O>KALJ @LILOP.h )K > @LJMRQBO DO>MEF@P PVPQBJ, FQfP
LCQBK B>PFBPQ QL PBBA > PVPQBJ TFQE O>KALJKBPP. )K QEFP ?LLH, ELTBSBO, TBfOB ILLHFKD QL ?RFIA
PVPQBJP JLABIBA LK TE>Q TB PBB FK K>QROB. $BC>RIQFKD QL O>KALJKBPP FP KLQ > M>OQF@RI>OIV
QELRDEQCRI PLIRQFLK QL > ABPFDK MOL?IBJdFK M>OQF@RI>O, QEB HFKA LC MOL?IBJ QE>Q FKSLISBP
@OB>QFKD >K LOD>KF@ LO K>QRO>I-ILLHFKD PFJRI>QFLK.
7FQE > CBT QOF@HP, TB @>K @E>KDB QEB T>V TB RPB L;H>IG() QL MOLAR@B gKLK-RKFCLOJh
AFPQOF?RQFLKP LC O>KALJ KRJ?BOP. 4EFP TFII @LJB FK E>KAV QEOLRDELRQ QEB ?LLH >P TB ILLH >Q >
KRJ?BO LC AFCCBOBKQ P@BK>OFLP. 7EBK TB BU>JFKB DBKBQF@ >IDLOFQEJP, CLO BU>JMIB, TBfII KBBA >
JBQELALILDV CLO MBOCLOJFKD gPBIB@QFLKhdTEF@E JBJ?BOP LC LRO MLMRI>QFLK PELRIA ?B
PBIB@QBA QL M>PP QEBFO $.! QL QEB KBUQ DBKBO>QFLK? 2BJBJ?BO QEB @LK@BMQ LC PROSFS>I LC QEB
CFQQBPQ? ,BQfP P>V TB E>SB > MLMRI>QFLK LC JLKHBVP BSLISFKD. .LQ BSBOV JLKHBV TFII E>SB >
4EB .>QROB LC #LAB (S005)
7
BNR>I @E>K@B LC OBMOLAR@FKD. 4L PFJRI>QB $>OTFKF>K BSLIRQFLK, TB @>KfQ PFJMIV MF@H QTL
O>KALJ JLKHBVP QL ?B M>OBKQP. 7B KBBA QEB JLOB gCFQh LKBP QL ?B JLOB IFHBIV QL ?B @ELPBK.
7B KBBA QL ABCFKB QEB gMOL?>?FIFQV LC QEB CFQQBPQ.h &LO BU>JMIB, > M>OQF@RI>OIV C>PQ >KA PQOLKD
JLKHBV JFDEQ E>SB > 90% @E>K@B LC MOL@OB>QFKD, TEFIB > TB>HBO LKB E>P LKIV > 10%
@E>K@B.
,BQfP M>RPB EBOB >KA Q>HB > ILLH >Q MOL?>?FIFQVfP ?>PF@ MOFK@FMIBP. &FOPQ TBfII BU>JFKB PFKDIB
BSBKQ MOL?>?FIFQV, F.B. QEB IFHBIFELLA QE>Q > DFSBK BSBKQ TFII L@@RO.
)C VLR E>SB > PVPQBJ TFQE > @BOQ>FK KRJ?BO LC MLPPF?IB LRQ@LJBP, QEB MOL?>?FIFQV LC QEB
L@@ROOBK@B LC > DFSBK BSBKQ BNR>IP QEB KRJ?BO LC LRQ@LJBP QE>Q NR>IFCV >P QE>Q BSBKQ
AFSFABA ?V QEB QLQ>I KRJ?BO LC >II MLPPF?IB LRQ@LJBP. ! @LFK QLPP FP > PFJMIB BU>JMIBdFQ E>P
LKIV QTL MLPPF?IB LRQ@LJBP, EB>AP LO Q>FIP. 4EBOB FP LKIV LKB T>V QL CIFM EB>AP. 4EB
MOL?>?FIFQV QE>Q QEB @LFK TFII QROK RM EB>AP, QEBOBCLOB, FP LKB AFSFABA ?V QTL: 1/2 LO 50%.
4>HB > AB@H LC CFCQV-QTL @>OAP. 4EB MOL?>?FIFQV LC AO>TFKD >K >@B COLJ QE>Q AB@H FP:
DKC8;H E< 79;I / DKC8;H E< 97H:I = 4 / 52 = 0.077 = T 8%
4EB MOL?>?FIFQV LC AO>TFKD > AF>JLKA FP:
DKC8;H E< :?7CED:I / DKC8;H E< 97H:I = 13 / 52 = 0.25 = 25%
7B @>K >IPL @>I@RI>QB QEB MOL?>?FIFQV LC JRIQFMIB BSBKQP L@@ROOFKD FK PBNRBK@B. 4L AL QEFP,
TB PFJMIV JRIQFMIV QEB FKAFSFAR>I MOL?>?FIFQFBP LC B>@E BSBKQ.
4EB MOL?>?FIFQV LC > @LFK QROKFKD RM EB>AP QEOBB QFJBP FK > OLT FP:
(1/2) * (1/2) * (1/2) = 1/8 (EH 0.125)
jJB>KFKD QE>Q > @LFK TFII QROK RM EB>AP QEOBB QFJBP FK > OLT LKB LRQ LC BFDEQ QFJBP (B>@E
gQFJBh ?BFKD QEOBB QLPPBP).
4EBOB >OB > @LRMIB LC T>VP FK TEF@E TB @>K RPB QEB L;H>IG() CRK@QFLK TFQE MOL?>?FIFQV FK
@LAB. /KB QB@EKFNRB FP QL CFII >K >OO>V TFQE > PBIB@QFLK LC KRJ?BOPdPLJB LC TEF@E >OB
OBMB>QBAdQEBK @ELLPB O>KALJ KRJ?BOP COLJ QE>Q >OO>V >KA DBKBO>QB BSBKQP ?>PBA LK
QELPB @ELF@BP.
7E>Q FP QEB MOL?>?FIFQV LC AO>TFKD QTL >@BP FK > OLT COLJ > AB@H LC CFCQV-QTL @>OAP?
Exercise I.2 Exercise I.2
CHN89 MNO@@ = H?Q CHN859
1 is stored in the array twice, making it
more likely to be picked.
MNO@@809 = 1;
MNO@@819 = 1;
)KQOLAR@QFLK
8
2RKKFKD QEFP @LAB TFII MOLAR@B > 40% @E>K@B LC MOFKQFKD QEB S>IRB 1, > 20% @E>K@B LC MOFKQFKD
2, >KA > 40% @E>K@B LC MOFKQFKD 3.
7B @>K >IPL >PH CLO > O>KALJ KRJ?BO (IBQfP J>HB FQ PFJMIB >KA GRPQ @LKPFABO O>KALJ CIL>QFKD
MLFKQ S>IRBP ?BQTBBK 0 >KA 1) >KA >IILT >K BSBKQ QL L@@RO LKIV FC LRO O>KALJ KRJ?BO FP TFQEFK
> @BOQ>FK O>KDB. &LO BU>JMIB:
4EFP JBQELA @>K >IPL ?B >MMIFBA QL JRIQFMIB LRQ@LJBP. ,BQfP P>V QE>Q /RQ@LJB ! E>P > 60%
@E>K@B LC E>MMBKFKD, /RQ@LJB ", > 10% @E>K@B, >KA /RQ@LJB #, > 30% @E>K@B. 7B
FJMIBJBKQ QEFP FK @LAB ?V MF@HFKD > O>KALJ CIL>Q >KA PBBFKD FKQL TE>Q O>KDB FQ C>IIP.
i 69HK99B 0.00 5B8 0.60 (60%) Q> +IH7CA9 A
i 69HK99B 0.60 5B8 0.70 (10%) Q> +IH7CA9 B
i 69HK99B 0.70 5B8 1.00 (30%) Q> +IH7CA9 C
7B @LRIA RPB QEB >?LSB JBQELALILDV QL @OB>QB > O>KALJ T>IHBO QE>Q QBKAP QL JLSB QL QEB
OFDEQ. (BOB FP >K BU>JMIB LC > 4;FE?L TFQE QEB CLIILTFKD MOL?>?FIFQFBP:
i 7<5B79 C: ACJ=B; ID: 20%
MNO@@829 = 2;
MNO@@839 = 3;
MNO@@849 = 3;
Picking a random element from an array CHN CH>?R = CHN(L;H>IG(MNO@@.F?HANB));
A probability of 10% @FI;N JLI< = 0.10;
A random floating point value between 0
and 1
@FI;N L = L;H>IG(1);
If our random number is less than 0.1, try
again!
C@ (L < JLI<) U
// NLS ;A;CH!
W
@FI;N HOG = L;H>IG(1);
If random number is less than 0.6 C@ (HOG < 0.6) U
JLCHNFH("-ON=IG? A");
Between 0.6 and 0.7 W ?FM? C@ (HOG < 0.7) U
JLCHNFH("-ON=IG? ");
Greater than 0.7 W ?FM? U
JLCHNFH("-ON=IG? !");
W
4EB .>QROB LC #LAB (S005)
9
i 7<5B79 C: ACJ=B; 8CKB: 20%
i 7<5B79 C: ACJ=B; @9:H: 20%
i 7<5B79 C: ACJ=B; F=;<H: 40%
Example I.3: Walker that tends to move to the right
PIC> MN?J() U
@FI;N L = L;H>IG(1);
A 40% chance of moving to the right! C@ (L < 0.4) U
R++;
W ?FM? C@ (L < 0.6) U
R--;
W ?FM? C@ (L < 0.8) U
S++;
W ?FM? U
S--;
W
W
#OB>QB > O>KALJ T>IHBO TFQE AVK>JF@ MOL?>?FIFQFBP. &LO BU>JMIB, @>K VLR DFSB FQ >
50% @E>K@B LC JLSFKD FK QEB AFOB@QFLK LC QEB JLRPB?
Exercise I.3 Exercise I.3
)KQOLAR@QFLK
10
I.4 A Normal Distribution of Random Numbers I.4 A Normal Distribution of Random Numbers
,BQfP DL ?>@H QL QE>Q MLMRI>QFLK LC PFJRI>QBA 0OL@BPPFKD JLKHBVP. 9LRO MOLDO>J DBKBO>QBP >
QELRP>KA -LKHBV L?GB@QP, B>@E TFQE > EBFDEQ S>IRB ?BQTBBK 200 >KA 300 (>P QEFP FP > TLOIA
LC JLKHBVP QE>Q E>SB EBFDEQP ?BQTBBK 200 >KA 300 MFUBIP).
$LBP QEFP >@@RO>QBIV ABMF@Q QEB EBFDEQP LC OB>I-TLOIA ?BFKDP? 4EFKH LC > @OLTABA PFABT>IH FK
.BT 9LOH #FQV. 0F@H >KV MBOPLK LCC QEB PQOBBQ >KA FQ J>V >MMB>O QE>Q QEBFO EBFDEQ FP O>KALJ.
.BSBOQEBIBPP, FQfP KLQ QEB HFKA LC O>KALJ QE>Q L;H>IG() MOLAR@BP. 0BLMIBfP EBFDEQP >OB KLQ
RKFCLOJIV AFPQOF?RQBA; QEBOB >OB > DOB>Q AB>I JLOB MBLMIB LC >SBO>DB EBFDEQ QE>K QEBOB >OB
SBOV Q>II LO SBOV PELOQ LKBP. 4L PFJRI>QB K>QROB, TB J>V T>KQ FQ QL ?B JLOB IFHBIV QE>Q LRO
JLKHBVP >OB LC >SBO>DB EBFDEQ (250 MFUBIP), VBQ PQFII >IILT QEBJ QL ?B, LK L@@>PFLK, SBOV PELOQ
LO SBOV Q>II.
! AFPQOF?RQFLK LC S>IRBP QE>Q @IRPQBO >OLRKA >K >SBO>DB (OBCBOOBA QL >P QEB gJB>Kh) FP HKLTK >P
> gKLOJ>Ih AFPQOF?RQFLK. )Q FP >IPL @>IIBA QEB '>RPPF>K AFPQOF?RQFLK (K>JBA CLO J>QEBJ>QF@F>K
#>OI &OFBAOF@E '>RPP) LO, FC VLR >OB &OBK@E, QEB ,>MI>@F>K AFPQOF?RQFLK (K>JBA CLO 0FBOOB-3FJLK
,>MI>@B). "LQE J>QEBJ>QF@F>KP TBOB TLOHFKD @LK@ROOBKQIV FK QEB B>OIV KFKBQBBKQE @BKQROV LK
ABCFKFKD PR@E > AFPQOF?RQFLK.
7EBK VLR DO>ME QEB AFPQOF?RQFLK, VLR DBQ PLJBQEFKD QE>Q ILLHP IFHB QEB CLIILTFKD, FKCLOJ>IIV
HKLTK >P > ?BII @ROSB:
4EB @ROSB FP DBKBO>QBA ?V > J>QEBJ>QF@>I CRK@QFLK QE>Q ABCFKBP QEB MOL?>?FIFQV LC >KV DFSBK
S>IRB L@@ROOFKD >P > CRK@QFLK LC QEB JB>K (LCQBK TOFQQBK >P _, QEB 'OBBH IBQQBO AI) >KA
PQ>KA>OA ABSF>QFLK (b, QEB 'OBBH IBQQBO G=;A5).
4EB JB>K FP MOBQQV B>PV QL RKABOPQ>KA. )K QEB @>PB LC LRO EBFDEQ S>IRBP ?BQTBBK 200 >KA
300, VLR MOL?>?IV E>SB >K FKQRFQFSB PBKPB LC QEB JB>K (F.B. >SBO>DB) >P 250. (LTBSBO, TE>Q FC
) TBOB QL P>V QE>Q QEB PQ>KA>OA ABSF>QFLK FP 3 LO 15? 7E>Q ALBP QEFP JB>K CLO QEB KRJ?BOP? 4EB
@FI;N B = L;H>IG(200,300);
F64B?2 !.2 F64B?2 !.3
4EB .>QROB LC #LAB (S005)
11
DO>MEP >?LSB PELRIA DFSB RP > EFKQ. 4EB DO>ME LK QEB IBCQ PELTP RP QEB AFPQOF?RQFLK TFQE >
SBOV ILT PQ>KA>OA ABSF>QFLK, TEBOB QEB J>GLOFQV LC QEB S>IRBP @IRPQBO @ILPBIV >OLRKA QEB
JB>K. 4EB DO>ME LK QEB OFDEQ PELTP RP > EFDEBO PQ>KA>OA ABSF>QFLK, TEBOB QEB S>IRBP >OB
JLOB BSBKIV PMOB>A LRQ COLJ QEB >SBO>DB.
4EB KRJ?BOP TLOH LRQ >P CLIILTP: 'FSBK > MLMRI>QFLK, 68% LC QEB JBJ?BOP LC QE>Q
MLMRI>QFLK TFII E>SB S>IRBP FK QEB O>KDB LC LKB PQ>KA>OA ABSF>QFLK COLJ QEB JB>K, 98%
TFQEFK QTL PQ>KA>OA ABSF>QFLKP, >KA 99.7% TFQEFK QEOBB PQ>KA>OA ABSF>QFLKP. 'FSBK >
PQ>KA>OA ABSF>QFLK LC 5 MFUBIP, LKIV 0.3% LC QEB JLKHBV EBFDEQP TFII ?B IBPP QE>K 235 MFUBIP
(QEOBB PQ>KA>OA ABSF>QFLKP ?BILT QEB JB>K LC 250) LO DOB>QBO QE>K 265 MFUBIP (QEOBB
PQ>KA>OA ABSF>QFLKP >?LSB QEB JB>K LC 250).
Calculating Mean and Standard Deviation Calculating Mean and Standard Deviation
#LKPFABO > @I>PP LC QBK PQRABKQP TEL OB@BFSB QEB CLIILTFKD P@LOBP (LRQ LC 100) LK >
QBPQ:
85, 82, 88, 86, 85, 93, 98, 40, 73, 83
(30 80,9 4> ?30 ,A0=,20: 81.3 (30 80,9 4> ?30 ,A0=,20: 81.3
4EB PQ>KA>OA ABSF>QFLK FP @>I@RI>QBA >P QEB PNR>OB OLLQ LC QEB >SBO>DB LC QEB PNR>OBP
LC ABSF>QFLKP >OLRKA QEB JB>K. )K LQEBO TLOAP, Q>HB QEB AFCCBOBK@B COLJ QEB JB>K CLO
B>@E MBOPLK >KA PNR>OB FQ (S>OF>K@B). #>I@RI>QB QEB >SBO>DB LC >II QEBPB S>IRBP >KA
Q>HB QEB PNR>OB OLLQ >P QEB PQ>KA>OA ABSF>QFLK.
Score Score Difference from Mean Difference from Mean Variance Variance
85 85-81.3 = 3.7
(3.7)
2
= 13.69
40 40-81.3 = -41.3
(-41.3)
2
= 1705.69
BQ@.
Average Variance: Average Variance: 254.23 254.23
(30 >?,9/,=/ /0A4,?4:9 4> ?30 ><@,=0 =::? :1 ?30 ,A0=,20 A,=4,9.0: 15.13 (30 >?,9/,=/ /0A4,?4:9 4> ?30 ><@,=0 =::? :1 ?30 ,A0=,20 A,=4,9.0: 15.13
,R@HFIV CLO RP, QL RPB > KLOJ>I AFPQOF?RQFLK LC O>KALJ KRJ?BOP FK > 0OL@BPPFKD PHBQ@E, TB
ALKfQ E>SB QL AL >KV LC QEBPB @>I@RI>QFLKP LROPBISBP. )KPQB>A, TB @>K J>HB RPB LC > @I>PP
HKLTK >P /;H>IG, TEF@E TB DBQ CLO COBB >P M>OQ LC QEB ABC>RIQ *>S> IF?O>OFBP FJMLOQBA FKQL
)KQOLAR@QFLK
12
0OL@BPPFKD (PBB QEB *>S>$L@P (EQQM://AL@P.LO>@IB.@LJ/G>S>PB/6/AL@P/>MF/G>S>/RQFI/
2>KALJ.EQJI) CLO JLOB FKCLOJ>QFLK).
4L RPB QEB 2>KALJ @I>PP, TB JRPQ CFOPQ AB@I>OB > S>OF>?IB LC QVMB 2>KALJ >KA @OB>QB QEB
2>KALJ L?GB@Q FK M?NOJ().
)C TB T>KQ QL MOLAR@B > O>KALJ KRJ?BO TFQE > KLOJ>I (LO '>RPPF>K) AFPQOF?RQFLK B>@E QFJB TB
ORK QEOLRDE >L;Q(), FQfP >P B>PV >P @>IIFKD QEB CRK@QFLK H?RN%;OMMC;H().
(BOBfP QEB QEFKD. 7E>Q >OB TB PRMMLPBA QL AL TFQE QEFP S>IRB? 7E>Q FC TB T>KQBA QL RPB FQ, CLO
BU>JMIB, QL >PPFDK QEB U-MLPFQFLK LC > PE>MB TB AO>T LK P@OBBK?
4EB H?RN%;OMMC;H() CRK@QFLK OBQROKP > KLOJ>I AFPQOF?RQFLK LC O>KALJ KRJ?BOP TFQE QEB
CLIILTFKD M>O>JBQBOP: 5 A95B C: N9FC >KA 5 GH5B85F8 89J=5H=CB C: CB9. ,BQfP P>V TB T>KQ >
JB>K LC 320 (QEB @BKQBO ELOFWLKQ>I MFUBI FK > TFKALT LC TFAQE 640) >KA > PQ>KA>OA ABSF>QFLK LC
60 MFUBIP. 7B @>K >AGRPQ QEB S>IRB QL LRO M>O>JBQBOP ?V JRIQFMIVFKD FQ ?V QEB PQ>KA>OA
ABSF>QFLK >KA >AAFKD QEB JB>K.
Example I.4: Gaussian distribution
We use the variable name generator
because what we have here can be thought
of as a random number generator.
/;H>IG A?H?L;NIL;
PIC> M?NOJ() U
MCT?(640,360);
A?H?L;NIL = H?Q /;H>IG();
W
PIC> >L;Q() U
Asking for a Gaussian random number.
(Note nextGaussian() returns a double and
must be converted to float.)
@FI;N HOG = (@FI;N) A?H?L;NIL.H?RN%;OMMC;H();
W
PIC> >L;Q() U
4EB .>QROB LC #LAB (S005)
13
"V AO>TFKD QEB BIIFMPBP LK QLM LC B>@E LQEBO TFQE PLJB QO>KPM>OBK@V, TB @>K >@QR>IIV PBB
QEB AFPQOF?RQFLK. 4EB ?OFDEQBPQ PMLQ FP KB>O QEB @BKQBO, TEBOB JLPQ LC QEB S>IRBP @IRPQBO, ?RQ
BSBOV PL LCQBK @FO@IBP >OB AO>TK C>OQEBO QL QEB OFDEQ LO IBCQ LC QEB @BKQBO.
Note that nextGaussian() returns a double. @FI;N HOG = (@FI;N) A?H?L;NIL.H?RN%;OMMC;H();
@FI;N M> = 60;
@FI;N G?;H = 320;
Multiply by the standard deviation and add
the mean.
@FI;N R = M> * HOG + G?;H;
HI0NLIE?();
@CFF(255,10);
?FFCJM?(R,180,16,16);
W
#LKPFABO > PFJRI>QFLK LC M>FKQ PMI>QQBO AO>TK >P > @LIIB@QFLK LC @LILOBA ALQP. -LPQ LC
QEB M>FKQ @IRPQBOP >OLRKA > @BKQO>I IL@>QFLK, ?RQ PLJB ALQP AL PMI>QQBO LRQ QLT>OAP QEB
BADBP. #>K VLR RPB > KLOJ>I AFPQOF?RQFLK LC O>KALJ KRJ?BOP QL DBKBO>QB QEB
IL@>QFLKP LC QEB ALQP? #>K VLR >IPL RPB > KLOJ>I AFPQOF?RQFLK LC O>KALJ KRJ?BOP QL
DBKBO>QB > @LILO M>IBQQB?
Exercise I.4 Exercise I.4
! '>RPPF>K O>KALJ T>IH FP ABCFKBA >P LKB FK TEF@E QEB PQBM PFWB (ELT C>O QEB L?GB@Q
JLSBP FK > DFSBK AFOB@QFLK) FP DBKBO>QBA TFQE > KLOJ>I AFPQOF?RQFLK. )JMIBJBKQ QEFP
S>OF>QFLK LC LRO O>KALJ T>IH.
Exercise I.5 Exercise I.5
I.5 A Custom Distribution of Random Numbers I.5 A Custom Distribution of Random Numbers
4EBOB TFII @LJB > QFJB FK VLRO IFCB TEBK VLR AL KLQ T>KQ > RKFCLOJ AFPQOF?RQFLK LC O>KALJ
S>IRBP, LO > '>RPPF>K LKB.,BQfP FJ>DFKB CLO > JLJBKQ QE>Q VLR >OB > O>KALJ T>IHBO FK
PB>O@E LC CLLA. -LSFKD O>KALJIV >OLRKA > PM>@B PBBJP IFHB > OB>PLK>?IB PQO>QBDV CLO
CFKAFKD PLJBQEFKD QL B>Q. !CQBO >II, VLR ALKfQ HKLT TEBOB QEB CLLA FP, PL VLR JFDEQ >P TBII
PB>O@E O>KALJIV RKQFI VLR CFKA FQ. 4EB MOL?IBJ, >P VLR J>V E>SB KLQF@BA, FP QE>Q O>KALJ
T>IHBOP OBQROK QL MOBSFLRPIV SFPFQBA IL@>QFLKP J>KV QFJBP (QEFP FP HKLTK >P gLSBOP>JMIFKD.h)
/KB PQO>QBDV QL >SLFA PR@E > MOL?IBJ FP QL, BSBOV PL LCQBK, Q>HB > SBOV I>ODB PQBM. 4EFP
>IILTP QEB T>IHBO QL CLO>DB O>KALJIV >OLRKA > PMB@FCF@ IL@>QFLK TEFIB MBOFLAF@>IIV GRJMFKD
SBOV C>O >T>V QL OBAR@B QEB >JLRKQ LC LSBOP>JMIFKD. 4EFP S>OF>QFLK LK QEB O>KALJ T>IH
)KQOLAR@QFLK
14
(HKLTK >P > ,ZSV CIFDEQ) OBNRFOBP > @RPQLJ PBQ LC MOL?>?FIFQFBP. 4ELRDE KLQ >K BU>@Q
FJMIBJBKQ>QFLK LC > ,ZSV CIFDEQ, TB @LRIA PQ>QB QEB MOL?>?FIFQV AFPQOF?RQFLK >P CLIILTP: QEB
ILKDBO QEB PQBM, QEB IBPP IFHBIV FQ FP QL ?B MF@HBA; QEB PELOQBO QEB PQBM, QEB JLOB IFHBIV.
%>OIFBO FK QEFP MOLILDRB, TB P>T QE>Q TB @LRIA DBKBO>QB @RPQLJ MOL?>?FIFQV AFPQOF?RQFLKP ?V
CFIIFKD >K >OO>V TFQE S>IRBP (PLJB ARMIF@>QBA PL QE>Q QEBV TLRIA ?B MF@HBA JLOB COBNRBKQIV) LO
?V QBPQFKD QEB OBPRIQ LC L;H>IG(). 7B @LRIA FJMIBJBKQ > ,ZSV CIFDEQ ?V P>VFKD QE>Q QEBOB FP >
1% @E>K@B LC QEB T>IHBO Q>HFKD > I>ODB PQBM.
(LTBSBO, QEFP OBAR@BP QEB MOL?>?FIFQFBP QL > CFUBA KRJ?BO LC LMQFLKP. 7E>Q FC TB T>KQBA QL
J>HB > JLOB DBKBO>I ORIBdQEB EFDEBO > KRJ?BO, QEB JLOB IFHBIV FQ FP QL ?B MF@HBA? 3.145
TLRIA ?B JLOB IFHBIV QL ?B MF@HBA QE>K 3.144, BSBK FC QE>Q IFHBIFELLA FP GRPQ > QFKV ?FQ DOB>QBO. )K
LQEBO TLOAP, FC R FP QEB O>KALJ KRJ?BO, TB @LRIA J>M QEB IFHBIFELLA LK QEB V->UFP TFQE S = R.
)C TB @>K CFDROB LRQ ELT QL DBKBO>QB > AFPQOF?RQFLK LC O>KALJ KRJ?BOP >@@LOAFKD QL QEB >?LSB
DO>ME, QEBK TB TFII ?B >?IB QL >MMIV QEB P>JB JBQELALILDV QL >KV @ROSB CLO TEF@E TB E>SB >
CLOJRI>.
/KB PLIRQFLK FP QL MF@H QTL O>KALJ KRJ?BOP FKPQB>A LC LKB. 4EB CFOPQ O>KALJ KRJ?BO FP GRPQ
QE>Q, > O>KALJ KRJ?BO. 4EB PB@LKA LKB, ELTBSBO, FP TE>Q TBfII @>II > gNR>IFCVFKD O>KALJ
S>IRB.h )Q TFII QBII RP TEBQEBO QL RPB QEB CFOPQ LKB LO QEOLT FQ >T>V >KA MF@H >KLQEBO LKB.
.RJ?BOP QE>Q E>SB >K B>PFBO QFJB NR>IFCVFKD TFII ?B MF@HBA JLOB LCQBK, >KA KRJ?BOP QE>Q
O>OBIV NR>IFCV TFII ?B MF@HBA FKCOBNRBKQIV. (BOB >OB QEB PQBMP (CLO KLT, IBQfP @LKPFABO LKIV
O>KALJ S>IRBP ?BQTBBK 0 >KA 1):
1. 0F@H > O>KALJ KRJ?BO: 21
@FI;N L = L;H>IG(1);
A 1% chance of taking a large step C@ (L < 0.01) U
RMN?J = L;H>IG(-100,100);
SMN?J = L;H>IG(-100,100);
W ?FM? U
RMN?J = L;H>IG(-1,1);
SMN?J = L;H>IG(-1,1);
W
F64B?2 !.4
4EB .>QROB LC #LAB (S005)
15
2. #LJMRQB > MOL?>?FIFQV 0 QE>Q 21 PELRIA NR>IFCV. ,BQfP QOV: 0 = 21.
3. 0F@H >KLQEBO O>KALJ KRJ?BO: 22
4. )C 22 FP IBPP QE>K 0, QEBK TB E>SB CLRKA LRO KRJ?BOd21!
5. )C 22 FP KLQ IBPP QE>K 0, DL ?>@H QL PQBM 1 >KA PQ>OQ LSBO.
(BOB TB >OB P>VFKD QE>Q QEB IFHBIFELLA QE>Q > O>KALJ S>IRB TFII NR>IFCV FP BNR>I QL QEB
O>KALJ KRJ?BO FQPBIC. ,BQfP P>V TB MF@H 0.1 CLO 21. 4EFP JB>KP QE>Q 21 TFII E>SB > 10% @E>K@B
LC NR>IFCVFKD. )C TB MF@H 0.83 CLO 21 QEBK FQ TFII E>SB > 83% @E>K@B LC NR>IFCVFKD. 4EB EFDEBO
QEB KRJ?BO, QEB DOB>QBO QEB IFHBIFELLA QE>Q TB TFII >@QR>IIV RPB FQ.
(BOB FP > CRK@QFLK (K>JBA CLO QEB -LKQB #>OIL JBQELA, TEF@E T>P K>JBA CLO QEB -LKQB
#>OIL @>PFKL) QE>Q FJMIBJBKQP QEB >?LSB >IDLOFQEJ, OBQROKFKD > O>KALJ S>IRB ?BQTBBK 0 >KA
1.
@FI;N GIHN?=;LFI() U
We do this forever until we find a
qualifying random value.
QBCF? (NLO?) U
Pick a random value. @FI;N L1 = L;H>IG(1);
Assign a probability. @FI;N JLI<;<CFCNS = L1;
Pick a second random value. @FI;N L2 = L;H>IG(1);
Does it qualify? If so, were done! C@ (L2 < JLI<;<CFCNS) U
L?NOLH L1;
W
W
W
)KQOLAR@QFLK
16
5PB > @RPQLJ MOL?>?FIFQV AFPQOF?RQFLK QL S>OV QEB PFWB LC > PQBM Q>HBK ?V QEB O>KALJ
T>IHBO. 4EB PQBM PFWB @>K ?B ABQBOJFKBA ?V FKCIRBK@FKD QEB O>KDB LC S>IRBP MF@HBA. #>K
VLR J>M QEB MOL?>?FIFQV BUMLKBKQF>IIVdF.B. J>HFKD QEB IFHBIFELLA QE>Q > S>IRB FP MF@HBA
BNR>I QL QEB S>IRB PNR>OBA?
(,>QBO TBfII PBB ELT QL AL QEFP JLOB BCCF@FBKQIV RPFKD SB@QLOP.)
A uniform distribution of step sizes.
Change this!
@FI;N MN?JMCT? = L;H>IG(0,10);
@FI;N MN?JR = L;H>IG(-MN?JMCT?,MN?JMCT?);
@FI;N MN?JS = L;H>IG(-MN?JMCT?,MN?JMCT?);
R += MN?JR;
S += MN?JS;
Exercise I.6 Exercise I.6
I.6 Perlin Noise (A Smoother Approach) I.6 Perlin Noise (A Smoother Approach)
! DLLA O>KALJ KRJ?BO DBKBO>QLO MOLAR@BP KRJ?BOP QE>Q E>SB KL OBI>QFLKPEFM >KA PELT KL
AFP@BOKF?IB M>QQBOK. !P TB >OB ?BDFKKFKD QL PBB, > IFQQIB ?FQ LC O>KALJKBPP @>K ?B > DLLA QEFKD
TEBK MOLDO>JJFKD LOD>KF@, IFCBIFHB ?BE>SFLOP. (LTBSBO, O>KALJKBPP >P QEB PFKDIB DRFAFKD
MOFK@FMIB FP KLQ KB@BPP>OFIV K>QRO>I. !K >IDLOFQEJ HKLTK >P g0BOIFK KLFPB,h K>JBA CLO FQP
FKSBKQLO +BK 0BOIFK, Q>HBP QEFP @LK@BMQ FKQL >@@LRKQ. 0BOIFK ABSBILMBA QEB KLFPB CRK@QFLK TEFIB
TLOHFKD LK QEB LOFDFK>I /FCB JLSFB FK QEB B>OIV 1980P; FQ T>P ABPFDKBA QL @OB>QB MOL@BARO>I
QBUQROBP CLO @LJMRQBO-DBKBO>QBA BCCB@QP. )K 1997 0BOIFK TLK >K !@>ABJV !T>OA FK QB@EKF@>I
>@EFBSBJBKQ CLO QEFP TLOH. 0BOIFK KLFPB @>K ?B RPBA QL DBKBO>QB S>OFLRP BCCB@QP TFQE K>QRO>I
NR>IFQFBP, PR@E >P @ILRAP, I>KAP@>MBP, >KA M>QQBOKBA QBUQROBP IFHB J>O?IB.
0BOIFK KLFPB E>P > JLOB LOD>KF@ >MMB>O>K@B ?B@>RPB FQ MOLAR@BP > K>QRO>IIV LOABOBA
(gPJLLQEh) PBNRBK@B LC MPBRAL-O>KALJ KRJ?BOP. 4EB DO>ME LK QEB IBCQ ?BILT PELTP 0BOIFK
KLFPB LSBO QFJB, TFQE QEB U->UFP OBMOBPBKQFKD QFJB; KLQB QEB PJLLQEKBPP LC QEB @ROSB. 4EB
DO>ME LK QEB OFDEQ PELTP MROB O>KALJ KRJ?BOP LSBO QFJB. (4EB @LAB CLO DBKBO>QFKD QEBPB
DO>MEP FP >S>FI>?IB FK QEB >@@LJM>KVFKD ?LLH ALTKIL>AP.)
4EB .>QROB LC #LAB (S005)
17
0OL@BPPFKD E>P > ?RFIQ-FK FJMIBJBKQ>QFLK LC QEB 0BOIFK KLFPB >IDLOFQEJ: QEB CRK@QFLK HICM?().
4EB HICM?() CRK@QFLK Q>HBP LKB, QTL, LO QEOBB >ODRJBKQP, >P KLFPB FP @LJMRQBA FK LKB, QTL,
LO QEOBB AFJBKPFLKP. ,BQfP PQ>OQ ?V ILLHFKD >Q LKB-AFJBKPFLK>I KLFPB.
Noise Detail Noise Detail
4EB 0OL@BPPFKD KLFPB OBCBOBK@B (EQQM://MOL@BPPFKD.LOD/OBCBOBK@B/KLFPB=.EQJI) QBIIP RP
QE>Q KLFPB FP @>I@RI>QBA LSBO PBSBO>I gL@Q>SBP.h #>IIFKD QEB HICM?"?N;CF()
(EQQM://MOL@BPPFKD.LOD/OBCBOBK@B/KLFPB$BQ>FI=.EQJI) CRK@QFLK TFII @E>KDB ?LQE QEB
KRJ?BO LC L@Q>SBP >KA QEBFO FJMLOQ>K@B OBI>QFSB QL LKB >KLQEBO. 4EFP FK QROK @E>KDBP
ELT QEB KLFPB CRK@QFLK ?BE>SBP.
!K LKIFKB IB@QROB ?V +BK 0BOIFK IBQP VLR IB>OK JLOB >?LRQ ELT KLFPB TLOHP COLJ
0BOIFK EFJPBIC (EQQM://TTT.KLFPBJ>@EFKB.@LJ/Q>IH1/).
#LKPFABO AO>TFKD > @FO@IB FK LRO 0OL@BPPFKD TFKALT >Q > O>KALJ U-IL@>QFLK.
.LT, FKPQB>A LC > O>KALJ U-IL@>QFLK, TB T>KQ > 0BOIFK KLFPB U-IL@>QFLK QE>Q FP gPJLLQEBO.h
9LR JFDEQ QEFKH QE>Q >II VLR KBBA QL AL FP OBMI>@B L;H>IG() TFQE HICM?(), F.B.
7EFIB @LK@BMQR>IIV QEFP FP BU>@QIV TE>Q TB T>KQ QL ALd@>I@RI>QB >K U-S>IRB QE>Q O>KDBP
?BQTBBK 0 >KA QEB TFAQE >@@LOAFKD QL 0BOIFK KLFPBdQEFP FP KLQ QEB @LOOB@Q FJMIBJBKQ>QFLK.
7EFIB QEB >ODRJBKQP QL QEB L;H>IG() CRK@QFLK PMB@FCV > O>KDB LC S>IRBP ?BQTBBK >
JFKFJRJ >KA > J>UFJRJ, HICM?() ALBP KLQ TLOH QEFP T>V. )KPQB>A, QEB LRQMRQ O>KDB FP
F64B?2 !.5: %<6@2 F64B?2 !.6: (.;1<:
A random x-location @FI;N R = L;H>IG(0,QC>NB);
?FFCJM?(R,180,16,16);
A noise x-location? @FI;N R = HICM?(0,QC>NB);
)KQOLAR@QFLK
18
CFUBAdFQ >IT>VP OBQROKP > S>IRB ?BQTBBK 0 >KA 1. 7BfII PBB FK > JLJBKQ QE>Q TB @>K DBQ
>OLRKA QEFP B>PFIV TFQE 0OL@BPPFKDfP G;J() CRK@QFLK, ?RQ CFOPQ TB JRPQ BU>JFKB TE>Q BU>@QIV
HICM?() BUMB@QP RP QL M>PP FK >P >K >ODRJBKQ.
7B @>K QEFKH LC LKB-AFJBKPFLK>I 0BOIFK KLFPB >P > IFKB>O PBNRBK@B LC S>IRBP LSBO QFJB. &LO
BU>JMIB:
Time Time Noise Value Noise Value
0 0.365
1 0.363
2 0.363
3 0.364
4 0.366
.LT, FK LOABO QL >@@BPP > M>OQF@RI>O KLFPB S>IRB FK 0OL@BPPFKD, TB E>SB QL M>PP > PMB@FCF@
"JLJBKQ FK QFJB" QL QEB HICM?() CRK@QFLK. &LO BU>JMIB:
!@@LOAFKD QL QEB >?LSB Q>?IB, HICM?(3) TFII OBQROK 0.364 >Q QFJB BNR>IP 3. 7B @LRIA FJMOLSB
QEFP ?V RPFKD > S>OF>?IB CLO QFJB >KA >PHFKD CLO > KLFPB S>IRB @LKQFKRLRPIV FK >L;Q().
4EB >?LSB @LAB OBPRIQP FK QEB P>JB S>IRB MOFKQBA LSBO >KA LSBO. 4EFP E>MMBKP ?B@>RPB TB
>OB >PHFKD CLO QEB OBPRIQ LC QEB HICM?() CRK@QFLK >Q QEB P>JB MLFKQ FK QFJBd3dLSBO >KA LSBO.
)C TB FK@OBJBKQ QEB QFJB S>OF>?IB N, ELTBSBO, TBfII DBQ > AFCCBOBKQ OBPRIQ.
@FI;N H = HICM?(3);
@FI;N N = 3;
PIC> >L;Q() U
We need the noise value for a specific
moment in time.
@FI;N H = HICM?(N);
JLCHNFH(H);
W
Typically we would start at time = 0, though
this is arbitrary.
@FI;N N = 0;
PIC> >L;Q() U
@FI;N H = HICM?(N);
JLCHNFH(H);
4EB .>QROB LC #LAB (S005)
19
(LT NRF@HIV TB FK@OBJBKQ N >IPL >CCB@QP QEB PJLLQEKBPP LC QEB KLFPB. )C TB J>HB I>ODB
GRJMP FK QFJB, QEBK TB >OB PHFMMFKD >EB>A >KA QEB S>IRBP TFII ?B JLOB O>KALJ.
4OV ORKKFKD QEB @LAB PBSBO>I QFJBP, FK@OBJBKQFKD N ?V 0.01, 0.02, 0.05, 0.1, 0.0001, >KA VLR
TFII PBB AFCCBOBKQ OBPRIQP.
Now, we move forward in time! N += 0.01;
W
F64B?2 !.7
Mapping Noise Mapping Noise
.LT TBfOB OB>AV QL >KPTBO QEB NRBPQFLK LC TE>Q QL AL TFQE QEB KLFPB S>IRB. /K@B TB E>SB
QEB S>IRB TFQE > O>KDB ?BQTBBK 0 >KA 1, FQfP RM QL RP QL J>M QE>Q O>KDB QL TE>Q TB T>KQ. 4EB
B>PFBPQ T>V QL AL QEFP FP TFQE 0OL@BPPFKDfP G;J() CRK@QFLK. 4EB G;J() CRK@QFLK Q>HBP CFSB
>ODRJBKQP. &FOPQ RM FP QEB S>IRB TB T>KQ QL J>M, FK QEFP @>PB H. 4EBK TB E>SB QL DFSB FQ QEB
S>IRBfP @ROOBKQ O>KDB (JFKFJRJ >KA J>UFJRJ), CLIILTBA ?V LRO ABPFOBA O>KDB.
)K QEFP @>PB, TB HKLT QE>Q KLFPB E>P > O>KDB ?BQTBBK 0 >KA 1, ?RQ TBfA IFHB QL AO>T LRO
@FO@IB TFQE > O>KDB ?BQTBBK 0 >KA QEB TFKALTfP TFAQE.
F64B?2 !.8
)KQOLAR@QFLK
20
7B @>K >MMIV QEB BU>@Q P>JB ILDF@ QL LRO O>KALJ T>IHBO, >KA >PPFDK ?LQE FQP U- >KA V-S>IRBP
>@@LOAFKD QL 0BOIFK KLFPB.
Example I.5: Perlin noise walker
@FI;N N = 0;
PIC> >L;Q() U
@FI;N H = HICM?(N);
Using map() to customize the range of
Perlin noise
@FI;N R = G;J(H,0,1,0,QC>NB);
?FFCJM?(R,180,16,16);
N += 0.01;
W
=F;MM 4;FE?L U
@FI;N R,S;
@FI;N NR,NS;
4;FE?L() U
NR = 0;
NS = 10000;
W
PIC> MN?J() U
x- and y-location mapped from noise R = G;J(HICM?(NR), 0, 1, 0, QC>NB);
S = G;J(HICM?(NS), 0, 1, 0, B?CABN);
Move forward through time. NR += 0.01;
NS += 0.01;
W
W
4EB .>QROB LC #LAB (S005)
21
.LQF@B ELT QEB >?LSB BU>JMIB OBNRFOBP >K >AAFQFLK>I M>FO LC S>OF>?IBP: NR >KA NS. 4EFP FP
?B@>RPB TB KBBA QL HBBM QO>@H LC QTL QFJB S>OF>?IBP, LKB CLO QEB U-IL@>QFLK LC QEB 4;FE?L
L?GB@Q >KA LKB CLO QEB V-IL@>QFLK. "RQ QEBOB FP PLJBQEFKD > ?FQ LAA >?LRQ QEBPB S>OF>?IBP.
7EV ALBP NR PQ>OQ >Q 0 >KA NS >Q 10,000? 7EFIB QEBPB KRJ?BOP >OB >O?FQO>OV @ELF@BP, TB
E>SB SBOV PMB@FCF@>IIV FKFQF>IFWBA LRO QTL QFJB S>OF>?IBP TFQE AFCCBOBKQ S>IRBP. 4EFP FP ?B@>RPB
QEB KLFPB CRK@QFLK FP ABQBOJFKFPQF@: FQ DFSBP VLR QEB P>JB OBPRIQ CLO > PMB@FCF@ QFJB N B>@E
>KA BSBOV QFJB. )C TB >PHBA CLO QEB KLFPB S>IRB >Q QEB P>JB QFJB N CLO ?LQE R >KA S, QEBK R
>KA S TLRIA >IT>VP ?B BNR>I, JB>KFKD QE>Q QEB 4;FE?L L?GB@Q TLRIA LKIV JLSB >ILKD >
AF>DLK>I. )KPQB>A, TB PFJMIV RPB QTL AFCCBOBKQ M>OQP LC QEB KLFPB PM>@B, PQ>OQFKD >Q 0 CLO R
>KA 10,000 CLO S PL QE>Q R >KA S @>K >MMB>O QL >@Q FKABMBKABKQIV LC B>@E LQEBO.
)K QORQE, QEBOB FP KL >@QR>I @LK@BMQ LC QFJB >Q MI>V EBOB. )QfP > RPBCRI JBQ>MELO QL EBIM RP
RKABOPQ>KA ELT QEB KLFPB CRK@QFLK TLOHP, ?RQ OB>IIV TE>Q TB E>SB FP PM>@B, O>QEBO QE>K
QFJB. 4EB DO>ME >?LSB ABMF@QP > IFKB>O PBNRBK@B LC KLFPB S>IRBP FK > LKB-AFJBKPFLK>I
PM>@B, >KA TB @>K >PH CLO > S>IRB >Q > PMB@FCF@ U-IL@>QFLK TEBKBSBO TB T>KQ. )K BU>JMIBP,
VLR TFII LCQBK PBB > S>OF>?IB K>JBA RI@@ QL FKAF@>QB QEB U-LCCPBQ >ILKD QEB KLFPB DO>ME,
O>QEBO QE>K N CLO QFJB (>P KLQBA FK QEB AF>DO>J).
F64B?2 !.9
)K QEB >?LSB O>KALJ T>IHBO, QEB OBPRIQ LC QEB KLFPB CRK@QFLK FP J>MMBA AFOB@QIV QL QEB
4;FE?LfP IL@>QFLK. #OB>QB > O>KALJ T>IHBO TEBOB VLR FKPQB>A J>M QEB OBPRIQ LC QEB
HICM?() CRK@QFLK QL > 4;FE?LfP PQBM PFWB.
Exercise I.7 Exercise I.7
Two-Dimensional Noise Two-Dimensional Noise
4EFP FAB> LC KLFPB S>IRBP IFSFKD FK > LKB-AFJBKPFLK>I PM>@B FP FJMLOQ>KQ ?B@>RPB FQ IB>AP RP
OFDEQ FKQL > AFP@RPPFLK LC QTL-AFJBKPFLK>I PM>@B. ,BQfP QEFKH >?LRQ QEFP CLO > JLJBKQ. 7FQE
LKB-AFJBKPFLK>I KLFPB, TB E>SB > PBNRBK@B LC S>IRBP FK TEF@E >KV DFSBK S>IRB FP PFJFI>O QL
FQP KBFDE?LO. "B@>RPB QEB S>IRB FP FK LKB AFJBKPFLK, FQ LKIV E>P QTL KBFDE?LOP: > S>IRB QE>Q
@LJBP ?BCLOB FQ (QL QEB IBCQ LK QEB DO>ME) >KA LKB QE>Q @LJBP >CQBO FQ (QL QEB OFDEQ).
)KQOLAR@QFLK
22
4TL-AFJBKPFLK>I KLFPB TLOHP BU>@QIV QEB P>JB T>V @LK@BMQR>IIV. 4EB AFCCBOBK@B LC @LROPB FP
QE>Q TB >OBKfQ ILLHFKD >Q S>IRBP >ILKD > IFKB>O M>QE, ?RQ S>IRBP QE>Q >OB PFQQFKD LK > DOFA. 4EFKH
LC > MFB@B LC DO>ME M>MBO TFQE KRJ?BOP TOFQQBK FKQL B>@E @BII. ! DFSBK S>IRB TFII ?B PFJFI>O QL
>II LC FQP KBFDE?LOP: >?LSB, ?BILT, QL QEB OFDEQ, QL QEB IBCQ, >KA >ILKD >KV AF>DLK>I.
)C VLR TBOB QL SFPR>IFWB QEFP DO>ME M>MBO TFQE B>@E S>IRB J>MMBA QL QEB ?OFDEQKBPP LC > @LILO,
VLR TLRIA DBQ PLJBQEFKD QE>Q ILLHP IFHB @ILRAP. 7EFQB PFQP KBUQ QL IFDEQ DO>V, TEF@E PFQP KBUQ QL
DO>V, TEF@E PFQP KBUQ QL A>OH DO>V, TEF@E PFQP KBUQ QL ?I>@H, TEF@E PFQP KBUQ QL A>OH DO>V, BQ@.
4EFP FP TEV KLFPB T>P LOFDFK>IIV FKSBKQBA. 9LR QTB>H QEB M>O>JBQBOP > ?FQ LO MI>V TFQE @LILO QL
J>HB QEB OBPRIQFKD FJ>DB ILLH JLOB IFHB J>O?IB LO TLLA LO >KV LQEBO LOD>KF@ QBUQROB.
,BQfP Q>HB > NRF@H ILLH >Q ELT QL FJMIBJBKQ QTL-AFJBKPFLK>I KLFPB FK 0OL@BPPFKD. )C VLR
T>KQBA QL @LILO BSBOV MFUBI LC > TFKALT O>KALJIV, VLR TLRIA KBBA > KBPQBA ILLM, LKB QE>Q
>@@BPPBA B>@E MFUBI >KA MF@HBA > O>KALJ ?OFDEQKBPP.
F64B?2 !.10: 1D %<6@2 F64B?2 !.11: 2D %<6@2
FI;>.CR?FM();
@IL (CHN R = 0; R < QC>NB; R++) U
@IL (CHN S = 0; S < B?CABN; S++) U
A random brightness! @FI;N <LCABN = L;H>IG(255);
JCR?FM8R+S*QC>NB9 = =IFIL(<LCABN);
W
W
OJ>;N?.CR?FM();
4EB .>QROB LC #LAB (S005)
23
4L @LILO B>@E MFUBI >@@LOAFKD QL QEB HICM?() CRK@QFLK, TBfII AL BU>@QIV QEB P>JB QEFKD, LKIV
FKPQB>A LC @>IIFKD L;H>IG() TBfII @>II HICM?().
4EFP FP > KF@B PQ>OQ @LK@BMQR>IIVdFQ DFSBP VLR > KLFPB S>IRB CLO BSBOV (R,S) IL@>QFLK FK LRO
QTL-AFJBKPFLK>I PM>@B. 4EB MOL?IBJ FP QE>Q QEFP TLKfQ E>SB QEB @ILRAV NR>IFQV TB T>KQ.
*RJMFKD COLJ MFUBI 200 QL MFUBI 201 FP QLL I>ODB LC > GRJM QEOLRDE KLFPB. 2BJBJ?BO, TEBK
TB TLOHBA TFQE LKB-AFJBKPFLK>I KLFPB, TB FK@OBJBKQBA LRO QFJB S>OF>?IB ?V 0.01 B>@E
CO>JB, KLQ ?V 1! ! MOBQQV DLLA PLIRQFLK QL QEFP MOL?IBJ FP QL GRPQ RPB AFCCBOBKQ S>OF>?IBP CLO
QEB KLFPB >ODRJBKQP. &LO BU>JMIB, TB @LRIA FK@OBJBKQ > S>OF>?IB @>IIBA RI@@ B>@E QFJB TB
JLSB ELOFWLKQ>IIV, >KA > SI@@ S>OF>?IB B>@E QFJB TB JLSB SBOQF@>IIV QEOLRDE QEB KBPQBA
ILLMP.
Example I.6: 2D Perlin noise
A Perlin noise brightness! <BE7J 8H?=>J = C7F(DE?I;(N,O),0,1,0,255);
Start xoff at 0. <BE7J NE<< = 0.0;
@IL (CHN R = 0; R < QC>NB; R++) U
For every xoff, start yoff at 0. <BE7J OE<< = 0.0;
@IL (CHN S = 0; S < B?CABN; S++) U
Use xoff and yoff for noise(). <BE7J 8H?=>J =
C7F(DE?I;(NE<<,OE<<),0,1,0,255);
Use x and y for pixel location. JCR?FM8R+S*QC>NB9 = =IFIL(<LCABN);
Increment yoff. OE<< += 0.01;
W
Increment xoff. NE<< += 0.01;
W
0I>V TFQE @LILO, HICM?"?N;CF(), >KA QEB O>QB >Q TEF@E RI@@ >KA SI@@ >OB
FK@OBJBKQBA QL >@EFBSB AFCCBOBKQ SFPR>I BCCB@QP.
Exercise I.8 Exercise I.8
)KQOLAR@QFLK
24
7BfSB BU>JFKBA PBSBO>I QO>AFQFLK>I RPBP LC 0BOIFK KLFPB FK QEFP PB@QFLK. 7FQE LKB-AFJBKPFLK>I
KLFPB, TB RPBA PJLLQE S>IRBP QL >PPFDK QEB IL@>QFLK LC >K L?GB@Q QL DFSB QEB >MMB>O>K@B LC
T>KABOFKD. 7FQE QTL-AFJBKPFLK>I KLFPB, TB @OB>QBA > @ILRAV M>QQBOK TFQE PJLLQEBA S>IRBP LK
> MI>KB LC MFUBIP. )QfP FJMLOQ>KQ QL OBJBJ?BO, ELTBSBO, QE>Q 0BOIFK KLFPB S>IRBP >OB GRPQ
QE>QdS>IRBP. 4EBV >OBKfQ FKEBOBKQIV QFBA QL MFUBI IL@>QFLKP LO @LILO. !KV BU>JMIB FK QEFP ?LLH
QE>Q E>P > S>OF>?IB @LRIA ?B @LKQOLIIBA SF> 0BOIFK KLFPB. 7EBK TB JLABI > TFKA CLO@B, FQP
PQOBKDQE @LRIA ?B @LKQOLIIBA ?V 0BOIFK KLFPB. 3>JB DLBP CLO QEB >KDIBP ?BQTBBK QEB ?O>K@EBP
FK > CO>@Q>I QOBB M>QQBOK, LO QEB PMBBA >KA AFOB@QFLK LC L?GB@QP JLSFKD >ILKD > DOFA FK > CILT
CFBIA PFJRI>QFLK.
!AA > QEFOA >ODRJBKQ QL KLFPB QE>Q FK@OBJBKQP LK@B MBO @V@IB QEOLRDE >L;Q() QL
>KFJ>QB QEB QTL-AFJBKPFLK>I KLFPB.
Exercise I.9 Exercise I.9
5PB QEB KLFPB S>IRBP >P QEB BIBS>QFLKP LC > I>KAP@>MB. 3BB QEB P@OBBKPELQ ?BILT >P >
OBCBOBK@B.
Exercise I.10 Exercise I.10
4EB .>QROB LC #LAB (S005)
25
F64B?2 !.12: *?22 D6A5 '2?96; ;<6@2 F64B?2 !.13: F9<D 36291 D6A5 '2?96; ;<6@2
I.7 Onward I.7 Onward
7B ?BD>K QEFP @E>MQBO ?V Q>IHFKD >?LRQ ELT O>KALJKBPP @>K ?B > @ORQ@E. )K J>KV T>VP, FQfP
QEB JLPQ L?SFLRP >KPTBO QL QEB HFKAP LC NRBPQFLKP TB >PH @LKQFKRLRPIVdELT PELRIA QEFP
L?GB@Q JLSB? 7E>Q @LILO PELRIA FQ ?B? 4EFP L?SFLRP >KPTBO, ELTBSBO, @>K >IPL ?B > I>WV
LKB.
!P TB CFKFPE LCC QEB FKQOLAR@QFLK, FQfP >IPL TLOQE KLQFKD QE>Q TB @LRIA GRPQ >P B>PFIV C>II FKQL
QEB QO>M LC RPFKD 0BOIFK KLFPB >P > @ORQ@E. (LT PELRIA QEFP L?GB@Q JLSB? 0BOIFK KLFPB! 7E>Q
@LILO PELRIA FQ ?B? 0BOIFK KLFPB! (LT C>PQ PELRIA FQ DOLT? 0BOIFK KLFPB!
4EB MLFKQ LC >II LC QEFP FP KLQ QL P>V QE>Q VLR PELRIA LO PELRIAKfQ RPB O>KALJKBPP. /O QE>Q
VLR PELRIA LO PELRIAKfQ RPB 0BOIFK KLFPB. 4EB MLFKQ FP QE>Q QEB ORIBP LC VLRO PVPQBJ >OB
ABCFKBA ?V VLR, >KA QEB I>ODBO VLRO QLLI?LU, QEB JLOB @ELF@BP VLRfII E>SB >P VLR FJMIBJBKQ
QELPB ORIBP. 4EB DL>I LC QEFP ?LLH FP QL CFII VLRO QLLI?LU. )C >II VLR HKLT FP O>KALJ, QEBK VLRO
ABPFDK QEFKHFKD FP IFJFQBA. 3ROB, 0BOIFK KLFPB EBIMP, ?RQ VLRfII KBBA JLOB. ! ILQ JLOB.
) QEFKH TBfOB OB>AV QL ?BDFK.
)KQOLAR@QFLK
26
Chapter 1. Vectors Chapter 1. Vectors
I(<42?, (<42?. ,5.AK@ <B? C20A<?, +60A<??J
Captain Oveur (Airplane)
4EFP ?LLH FP >II >?LRQ ILLHFKD >Q QEB TLOIA >OLRKA RP >KA @LJFKD RM TFQE @IBSBO T>VP QL
PFJRI>QB QE>Q TLOIA TFQE @LAB. $FSFABA FKQL QEOBB M>OQP, QEB ?LLH TFII PQ>OQ ?V ILLHFKD >Q ?>PF@
MEVPF@PdELT >K >MMIB C>IIP COLJ > QOBB, > MBKARIRJ PTFKDP FK QEB >FO, QEB B>OQE OBSLISBP
>OLRKA QEB PRK, BQ@. !?PLIRQBIV BSBOVQEFKD @LKQ>FKBA TFQEFK QEB CFOPQ CFSB @E>MQBOP LC QEFP ?LLH
OBNRFOBP QEB RPB LC QEB JLPQ ?>PF@ ?RFIAFKD ?IL@H CLO MOLDO>JJFKD JLQFLKdQEB A0.?:= A0.?:=. !KA PL
QEFP FP TEBOB TB ?BDFK LRO PQLOV.
.LT, QEB TLOA SB@QLO @>K JB>K > ILQ LC AFCCBOBKQ QEFKDP. 6B@QLO FP QEB K>JB LC > .BT 7>SB
OL@H ?>KA CLOJBA FK 3>@O>JBKQL, #! FK QEB B>OIV 1980P. )QfP QEB K>JB LC > ?OB>HC>PQ @BOB>I
J>KRC>@QROBA ?V +BIILDDfP #>K>A>. )K QEB CFBIA LC BMFABJFLILDV, > SB@QLO FP RPBA QL ABP@OF?B
>K LOD>KFPJ QE>Q QO>KPJFQP FKCB@QFLK COLJ LKB ELPQ QL >KLQEBO. )K QEB #++ MOLDO>JJFKD
I>KDR>DB, > SB@QLO (PQA::SB@QLO) FP >K FJMIBJBKQ>QFLK LC > AVK>JF@>IIV OBPFW>?IB >OO>V A>Q>
PQOR@QROB. 7EFIB >II QEBPB ABCFKFQFLKP >OB FKQBOBPQFKD, QEBVfOB KLQ TE>Q TBfOB ILLHFKD CLO. 7E>Q
TB T>KQ FP @>IIBA > E@.74/0,9 A0.?:= E@.74/0,9 A0.?:= (K>JBA CLO QEB 'OBBH J>QEBJ>QF@F>K %R@IFA >KA >IPL
HKLTK >P > DBLJBQOF@ SB@QLO). 7EBK VLR PBB QEB QBOJ gSB@QLOh FK QEFP ?LLH, VLR @>K >PPRJB FQ
OBCBOP QL > %R@IFAB>K SB@QLO, ABCFKBA >P 5B 9BH=HM H<5H <5G 6CH< A5;B=HI89 5B8 8=F97H=CB.
! SB@QLO FP QVMF@>IIV AO>TK >P > >OOLT; QEB AFOB@QFLK FP FKAF@>QBA ?V TEBOB QEB >OOLT FP
MLFKQFKD, >KA QEB J>DKFQRAB ?V QEB IBKDQE LC QEB >OOLT FQPBIC.
4EB .>QROB LC #LAB (S005)
27
)K QEB >?LSB FIIRPQO>QFLK, QEB SB@QLO FP AO>TK >P >K >OOLT COLJ MLFKQ ! QL MLFKQ " >KA PBOSBP
>P >K FKPQOR@QFLK CLO ELT QL QO>SBI COLJ ! QL ".
F64B?2 1.1: A C20A<? (1?.D; .@ .; .??<D) 5.@ :.4;6AB12 (92;4A5 <3 .??<D) .;1 16?20A6<; (D5605 D.F
6A 6@ =<6;A6;4).
1.1 Vectors, You Complete Me 1.1 Vectors, You Complete Me
"BCLOB TB AFSB FKQL JLOB LC QEB ABQ>FIP >?LRQ SB@QLOP, IBQfP ILLH >Q > ?>PF@ 0OL@BPPFKD
BU>JMIB QE>Q ABJLKPQO>QBP TEV TB PELRIA @>OB >?LRQ SB@QLOP FK QEB CFOPQ MI>@B. )C VLRfSB
OB>A >KV LC QEB FKQOLAR@QLOV 0OL@BPPFKD QBUQ?LLHP LO Q>HBK > @I>PP LK MOLDO>JJFKD TFQE
0OL@BPPFKD (>KA ELMBCRIIV VLRfSB ALKB LKB LC QEBPB QEFKDP QL EBIM MOBM>OB VLR CLO QEFP
?LLH), VLR MOL?>?IV, >Q LKB MLFKQ LO >KLQEBO, IB>OKBA ELT QL TOFQB > PFJMIB ?LRK@FKD ?>II
PHBQ@E.
!3 F<B .?2 ?2.16;4 A56@ /<<8 .@ . 'DF <? 6; =?6;A, A52; F<B D699 <;9F @22 @0?22;@5<A@ <3 A52 0<12.
$<A6<;, <3 0<B?@2, 6@ . 82F 292:2;A <3 <B? 16@0B@@6<;, @< A< A52 2EA2;A =<@@6/92, A52 @A.A60 @0?22;@5<A@
D699 6;09B12 A?.69@ A< 46C2 . @2;@2 <3 A52 /25.C6<?. F<? :<?2 ./<BA 5<D A< 1?.D A?.69@, @22 A52 0<12
2E.:=92@ .C.69./92 3<? 1<D;9<.1.
#E>MQBO 1. 6B@QLOP
28
,L@>QFLK ,L@>QFLK L 5B8 M
3MBBA 3MBBA LGD998 5B8 MGD998
Example 1.1: Bouncing ball with no vectors
)K QEB >?LSB BU>JMIB, TB E>SB > SBOV PFJMIB TLOIAd> ?I>KH @>KS>P TFQE > @FO@RI>O PE>MB (>
g?>IIh) QO>SBIFKD >OLRKA. 4EFP ?>II E>P PLJB MOLMBOQFBP, TEF@E >OB OBMOBPBKQBA FK QEB @LAB >P
S>OF>?IBP.
)K > JLOB >AS>K@BA PHBQ@E, TB @LRIA FJ>DFKB E>SFKD J>KV JLOB S>OF>?IBP:
Variables for location and speed of ball. @FI;N R = 100;
@FI;N S = 100;
@FI;N RMJ??> = 1;
@FI;N SMJ??> = 3.3;
Remember how Processing works? setup()
is executed once when the sketch starts
and draw() loops forever and ever (until you
quit).
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
<;=EALIOH>(255);
W
PIC> >L;Q() U
<;=EALIOH>(255);
Move the ball according to its speed. R = R + RMJ??>;
S = S + SMJ??>;
Check for bouncing. C@ ((R > QC>NB) VV (R < 0)) U
RMJ??> = RMJ??> * -1;
W
C@ ((S > B?CABN) VV (S < 0)) U
SMJ??> = SMJ??> * -1;
W
MNLIE?(0);
@CFF(175);
Display the ball at the location (x,y). ?FFCJM?(R,S,16,16);
W
4EB .>QROB LC #LAB (S005)
29
!@@BIBO>QFLK !@@BIBO>QFLK L5779@9F5H=CB 5B8 M5779@9F5H=CB
4>ODBQ IL@>QFLK 4>ODBQ IL@>QFLK LH5F;9H 5B8 MH5F;9H
7FKA 7FKA LK=B8 5B8 MK=B8
&OF@QFLK &OF@QFLK L:F=7H=CB 5B8 M:F=7H=CB
)QfP ?B@LJFKD @IB>OBO QE>Q CLO BSBOV @LK@BMQ FK QEFP TLOIA (TFKA, IL@>QFLK, >@@BIBO>QFLK, BQ@.),
TBfII KBBA QTL S>OF>?IBP. !KA QEFP FP LKIV > QTL-AFJBKPFLK>I TLOIA. )K > 3$ TLOIA, TBfII KBBA
R, S, T, RMJ??>, SMJ??>, TMJ??>, >KA PL LK.
7LRIAKfQ FQ ?B KF@B FC TB @LRIA PFJMIFCV LRO @LAB >KA RPB CBTBO S>OF>?IBP?
)KPQB>A LC:
7B @LRIA PFJMIV E>SBj
4>HFKD QEFP CFOPQ PQBM FK RPFKD SB@QLOP TLKfQ >IILT RP QL AL >KVQEFKD KBT. *RPQ >AAFKD SB@QLOP
TLKfQ J>DF@>IIV J>HB VLRO 0OL@BPPFKD PHBQ@EBP PFJRI>QB MEVPF@P. (LTBSBO, QEBV TFII
PFJMIFCV VLRO @LAB >KA MOLSFAB > PBQ LC CRK@QFLKP CLO @LJJLK J>QEBJ>QF@>I LMBO>QFLKP QE>Q
E>MMBK LSBO >KA LSBO >KA LSBO >D>FK TEFIB MOLDO>JJFKD JLQFLK.
!P >K FKQOLAR@QFLK QL SB@QLOP, TBfOB DLFKD QL IFSB FK QTL AFJBKPFLKP CLO NRFQB PLJB QFJB (>Q
IB>PQ RKQFI TB DBQ QEOLRDE QEB CFOPQ PBSBO>I @E>MQBOP). !II LC QEBPB BU>JMIBP @>K ?B C>FOIV
B>PFIV BUQBKABA QL QEOBB AFJBKPFLKP (>KA QEB @I>PP TB TFII RPBd.3?=NILd>IILTP CLO QEOBB
AFJBKPFLKP.) (LTBSBO, FQfP B>PFBO QL PQ>OQ TFQE GRPQ QTL.
@FI;N R;
@FI;N S;
@FI;N RMJ??>;
@FI;N SMJ??>;
3?=NIL FI=;NCIH;
3?=NIL MJ??>;
1.2 Vectors for Processing Programmers 1.2 Vectors for Processing Programmers
/KB T>V QL QEFKH LC > SB@QLO FP QEB AFCCBOBK@B ?BQTBBK QTL MLFKQP. #LKPFABO ELT VLR JFDEQ
DL >?LRQ MOLSFAFKD FKPQOR@QFLKP QL T>IH COLJ LKB MLFKQ QL >KLQEBO.
(BOB >OB PLJB SB@QLOP >KA MLPPF?IB QO>KPI>QFLKP:
#E>MQBO 1. 6B@QLOP
30
(-15, 3) (-15, 3) 25@? :=:H99B GH9DG K9GH; HIFB 5B8 K5@? H<F99 GH9DG BCFH<.
(3, 4) (3, 4) 25@? H<F99 GH9DG 95GH; HIFB 5B8 K5@? :=J9 GH9DG BCFH<.
(2, -1) (2, -1) 25@? HKC GH9DG 95GH; HIFB 5B8 K5@? CB9 GH9D GCIH<.
9LRfSB MOL?>?IV ALKB QEFP ?BCLOB TEBK MOLDO>JJFKD JLQFLK. &LO BSBOV CO>JB LC >KFJ>QFLK
(F.B. > PFKDIB @V@IB QEOLRDE 0OL@BPPFKDfP >L;Q() ILLM), VLR FKPQOR@Q B>@E L?GB@Q LK QEB P@OBBK
QL JLSB > @BOQ>FK KRJ?BO LC MFUBIP ELOFWLKQ>IIV >KA > @BOQ>FK KRJ?BO LC MFUBIP SBOQF@>IIV.
&LO BSBOV CO>JB:
90B 7:.,?4:9 = A07:.4?D ,;;740/ ?: .@==09? 7:.,?4:9 90B 7:.,?4:9 = A07:.4?D ,;;740/ ?: .@==09? 7:.,?4:9
)C SBIL@FQV FP > SB@QLO (QEB AFCCBOBK@B ?BQTBBK QTL MLFKQP), TE>Q FP IL@>QFLK? )P FQ > SB@QLO QLL?
4B@EKF@>IIV, LKB JFDEQ >ODRB QE>Q IL@>QFLK FP KLQ > SB@QLO, PFK@B FQfP KLQ ABP@OF?FKD ELT QL
JLSB COLJ LKB MLFKQ QL >KLQEBOdFQfP PFJMIV ABP@OF?FKD > PFKDRI>O MLFKQ FK PM>@B.
F64B?2 1.2
F64B?2 1.3
4EB .>QROB LC #LAB (S005)
31
IL@>QFLK IL@>QFLK L,M
SBIL@FQV SBIL@FQV LGD998,MGD998
.BSBOQEBIBPP, >KLQEBO T>V QL ABP@OF?B > IL@>QFLK FP QEB M>QE Q>HBK COLJ QEB LOFDFK QL OB>@E
QE>Q IL@>QFLK. (BK@B, > IL@>QFLK @>K ?B QEB SB@QLO OBMOBPBKQFKD QEB AFCCBOBK@B ?BQTBBK
IL@>QFLK >KA LOFDFK.
,BQfP BU>JFKB QEB RKABOIVFKD A>Q> CLO ?LQE IL@>QFLK >KA SBIL@FQV. )K QEB ?LRK@FKD ?>II
BU>JMIB, TB E>A QEB CLIILTFKD:
.LQF@B ELT TB >OB PQLOFKD QEB P>JB A>Q> CLO ?LQEdQTL CIL>QFKD MLFKQ KRJ?BOP, >K R >KA > S.
)C TB TBOB QL TOFQB > SB@QLO @I>PP LROPBISBP, TBfA PQ>OQ TFQE PLJBQEFKD O>QEBO ?>PF@:
!Q FQP @LOB, > .3?=NIL FP GRPQ > @LKSBKFBKQ T>V QL PQLOB QTL S>IRBP (LO QEOBB, >P TBfII PBB FK
3$ BU>JMIBP).
!KA PL QEFP j
F64B?2 1.4
=F;MM .3?=NIL U
@FI;N R;
@FI;N S;
.3?=NIL(@FI;N R:, @FI;N S:) U
R = R:;
S = S:;
W
W
#E>MQBO 1. 6B@QLOP
32
?B@LJBP j
.LT QE>Q TB E>SB QTL SB@QLO L?GB@QP (IL@>QFLK >KA SBIL@FQV), TBfOB OB>AV QL FJMIBJBKQ QEB
>IDLOFQEJ CLO JLQFLKd7:.,?4:9 = 7:.,?4:9 + A07:.4?D 7:.,?4:9 = 7:.,?4:9 + A07:.4?D. )K %U>JMIB 1.1, TFQELRQ SB@QLOP, TB E>A:
)K >K FAB>I TLOIA, TB TLRIA ?B >?IB QL OBTOFQB QEB >?LSB >P:
(LTBSBO, FK 0OL@BPPFKD, QEB >AAFQFLK LMBO>QLO + FP OBPBOSBA CLO MOFJFQFSB S>IRBP (FKQBDBOP,
CIL>QP, BQ@.) LKIV. 0OL@BPPFKD ALBPKfQ HKLT ELT QL >AA QTL .3?=NIL L?GB@QP QLDBQEBO >KV JLOB
QE>K FQ HKLTP ELT QL >AA QTL .$IHN L?GB@QP LO .'G;A? L?GB@QP. &LOQRK>QBIV CLO RP, QEB
.3?=NIL @I>PP FK@IRABP CRK@QFLKP CLO @LJJLK J>QEBJ>QF@>I LMBO>QFLKP.
@FI;N R = 100;
@FI;N S = 100;
@FI;N RMJ??> = 1;
@FI;N SMJ??> = 3.3;
.3?=NIL FI=;NCIH = H?Q .3?=NIL(100,100);
.3?=NIL P?FI=CNS = H?Q .3?=NIL(1,3.3);
Add each speed to each location. R = R + RMJ??>;
S = S + SMJ??>;
Add the velocity vector to the location
vector.
FI=;NCIH = FI=;NCIH + P?FI=CNS;
1.3 Vector Addition 1.3 Vector Addition
"BCLOB TB @LKQFKRB ILLHFKD >Q QEB .3?=NIL @I>PP >KA FQP ;>>() JBQELA (MROBIV CLO QEB P>HB LC
IB>OKFKD PFK@B FQfP >IOB>AV FJMIBJBKQBA CLO RP FK 0OL@BPPFKD FQPBIC), IBQfP BU>JFKB SB@QLO
>AAFQFLK RPFKD QEB KLQ>QFLK CLRKA FK J>QE >KA MEVPF@P QBUQ?LLHP.
6B@QLOP >OB QVMF@>IIV TOFQQBK BFQEBO FK ?LIAC>@B QVMB LO TFQE >K >OOLT LK QLM. &LO QEB MROMLPBP
LC QEFP ?LLH, QL AFPQFKDRFPE > A0.?:= A0.?:= COLJ > >.,7,= >.,7,= (G75@5F OBCBOP QL > PFKDIB S>IRB, PR@E >P >K
FKQBDBO LO > CIL>QFKD MLFKQ KRJ?BO), TBfII RPB QEB >OOLT KLQ>QFLK:
i 6B@QLO: u
i 3@>I>O: x
,BQfP P>V ) E>SB QEB CLIILTFKD QTL SB@QLOP:
4EB .>QROB LC #LAB (S005)
33
%>@E SB@QLO E>P QTL @LJMLKBKQP, >K R >KA > S. 4L >AA QTL SB@QLOP QLDBQEBO, TB PFJMIV >AA
?LQE RfP >KA ?LQE SfP.
)K LQEBO TLOAP:
w
= u
+ v
= (8, 6)
F64B?2 1.5
F64B?2 1.6
#E>MQBO 1. 6B@QLOP
34
.LT QE>Q TB RKABOPQ>KA ELT QL >AA QTL SB@QLOP QLDBQEBO, TB @>K ILLH >Q ELT >AAFQFLK FP
FJMIBJBKQBA FK QEB .3?=NIL @I>PP FQPBIC. ,BQfP TOFQB > CRK@QFLK @>IIBA ;>>() QE>Q Q>HBP >KLQEBO
.3?=NIL L?GB@Q >P FQP >ODRJBKQ.
.LT QE>Q TB PBB ELT ;>>() FP TOFQQBK FKPFAB LC .3?=NIL, TB @>K OBQROK QL LRO ?LRK@FKD ?>II
BU>JMIB TFQE FQP 7:.,?4:9 + A07:.4?D 7:.,?4:9 + A07:.4?D >IDLOFQEJ >KA FJMIBJBKQ SB@QLO >AAFQFLK:
!KA EBOB TB >OB, OB>AV QL OBTOFQB QEB ?LRK@FKD ?>II BU>JMIB RPFKD .3?=NIL.
Example 1.2: Bouncing ball with PVectors!
=F;MM .3?=NIL U
@FI;N R;
@FI;N S;
.3?=NIL(@FI;N R:, @FI;N S:) U
R = R:;
S = S:;
W
New! A function to add another PVector to
this PVector. Simply add the L components
and the M components together.
LE?: 7::(+0;9JEH L) Q
O = O + L.O;
N = N + L.N;
S
W
Add the current velocity to the location. FI=;NCIH = FI=;NCIH + P?FI=CNS;
FI=;NCIH.;>>(P?FI=CNS);
Instead of a bunch of floats, we now just
have two PVector variables.
+0;9JEH BE97J?ED;
+0;9JEH L;BE9?JO;
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
BE97J?ED = D;M +0;9JEH(100,100);
L;BE9?JO = D;M +0;9JEH(2.5,5);
W
PIC> >L;Q() U
<;=EALIOH>(255);
BE97J?ED.7::(L;BE9?JO);
4EB .>QROB LC #LAB (S005)
35
.LT, VLR JFDEQ CBBI PLJBTE>Q AFP>MMLFKQBA. !CQBO >II, QEFP J>V FKFQF>IIV >MMB>O QL E>SB
J>AB QEB @LAB JLOB @LJMIF@>QBA QE>K QEB LOFDFK>I SBOPFLK. 7EFIB QEFP FP > MBOCB@QIV
OB>PLK>?IB >KA S>IFA @OFQFNRB, FQfP FJMLOQ>KQ QL RKABOPQ>KA QE>Q TB E>SBKfQ CRIIV OB>IFWBA QEB
MLTBO LC MOLDO>JJFKD TFQE SB@QLOP GRPQ VBQ. ,LLHFKD >Q > PFJMIB ?LRK@FKD ?>II >KA LKIV
FJMIBJBKQFKD SB@QLO >AAFQFLK FP GRPQ QEB CFOPQ PQBM. !P TB JLSB CLOT>OA FKQL > JLOB @LJMIBU
TLOIA LC JRIQFMIB L?GB@QP >KA JRIQFMIB 1:=.0> 1:=.0> (TEF@E TBfII FKQOLAR@B FK #E>MQBO 2), QEB
?BKBCFQP LC .3?=NIL TFII ?B@LJB JLOB >MM>OBKQ.
7B PELRIA, ELTBSBO, KLQB >K FJMLOQ>KQ >PMB@Q LC QEB >?LSB QO>KPFQFLK QL MOLDO>JJFKD TFQE
SB@QLOP. %SBK QELRDE TB >OB RPFKD .3?=NIL L?GB@QP QL ABP@OF?B QTL S>IRBPdQEB R >KA S LC
IL@>QFLK >KA QEB R >KA S LC SBIL@FQVdTB PQFII LCQBK KBBA QL OBCBO QL QEB L >KA M @LJMLKBKQP
LC B>@E .3?=NIL FKAFSFAR>IIV. 7EBK TB DL QL AO>T >K L?GB@Q FK 0OL@BPPFKD, QEBOBfP KL
JB>KP CLO RP QL P>V:
4EB ?FFCJM?() CRK@QFLK ALBP KLQ >IILT CLO > .3?=NIL >P >K >ODRJBKQ. !K BIIFMPB @>K LKIV
?B AO>TK TFQE QTL P@>I>O S>IRBP, >K R-@LLOAFK>QB >KA > S-@LLOAFK>QB. !KA PL TB JRPQ AFD
FKQL QEB .3?=NIL L?GB@Q >KA MRII LRQ QEB L >KA M @LJMLKBKQP RPFKD L?GB@Q-LOFBKQBA ALQ
PVKQ>U.
4EB P>JB FPPRB >OFPBP TEBK QBPQFKD FC QEB @FO@IB E>P OB>@EBA QEB BADB LC QEB TFKALT, >KA
TB KBBA QL >@@BPP QEB FKAFSFAR>I @LJMLKBKQP LC ?LQE SB@QLOP: FI=;NCIH >KA P?FI=CNS.
We still sometimes need to refer to the
individual components of a PVector and
can do so using the dot syntax: location.x,
velocity.y, etc.
?< ((BE97J?ED.N > M?:J>) RR (BE97J?ED.N < 0)) Q
L;BE9?JO.N = L;BE9?JO.N * -1;
S
?< ((BE97J?ED.O > >;?=>J) RR (BE97J?ED.O < 0)) Q
L;BE9?JO.O = L;BE9?JO.O * -1;
S
MNLIE?(0);
@CFF(175);
;BB?FI;(BE97J?ED.N,BE97J?ED.O,16,16);
W
?FFCJM?(FI=;NCIH,16,16);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);
C@ ((FI=;NCIH.R > QC>NB) VV (FI=;NCIH.R < 0)) U
P?FI=CNS.R = P?FI=CNS.R * -1;
W
#E>MQBO 1. 6B@QLOP
36
&FKA PLJBQEFKD VLRfSB MOBSFLRPIV J>AB FK 0OL@BPPFKD RPFKD PBM>O>QB R >KA S S>OF>?IBP
>KA RPB .3?=NILP FKPQB>A.
Exercise 1.1 Exercise 1.1
4>HB LKB LC QEB T>IHBO BU>JMIBP COLJ QEB FKQOLAR@QFLK >KA @LKSBOQ FQ QL RPB .3?=NILP.
Exercise 1.2 Exercise 1.2
%UQBKA QEB ?LRK@FKD ?>II TFQE SB@QLOP BU>JMIB FKQL 3$. #>K VLR DBQ > PMEBOB QL ?LRK@B
>OLRKA > ?LU?
Exercise 1.3 Exercise 1.3
1.4 More Vector Math 1.4 More Vector Math
!AAFQFLK T>P OB>IIV GRPQ QEB CFOPQ PQBM. 4EBOB >OB J>KV J>QEBJ>QF@>I LMBO>QFLKP QE>Q >OB
@LJJLKIV RPBA TFQE SB@QLOP. "BILT FP > @LJMOBEBKPFSB IFPQ LC QEB LMBO>QFLKP >S>FI>?IB >P
CRK@QFLKP FK QEB .3?=NIL @I>PP. 7BfII DL QEOLRDE > CBT LC QEB HBV LKBP KLT. !P LRO BU>JMIBP
DBQ JLOB PLMEFPQF@>QBA FK I>QBO @E>MQBOP, TBfII @LKQFKRB QL OBSB>I QEB ABQ>FIP LC JLOB CRK@QFLKP.
i ;>>() d >AA SB@QLOP
i MO<() d PR?QO>@Q SB@QLOP
i GOFN() d P@>IB QEB SB@QLO TFQE JRIQFMIF@>QFLK
i >CP() d P@>IB QEB SB@QLO TFQE AFSFPFLK
i G;A() d @>I@RI>QB QEB J>DKFQRAB LC > SB@QLO
i M?N+;A() - PBQ QEB J>DKFQRAB LC > SB@QLO
i HILG;FCT?() d KLOJ>IFWB QEB SB@QLO QL > RKFQ IBKDQE LC 1
i FCGCN() d IFJFQ QEB J>DKFQRAB LC > SB@QLO
i B?;>CHA2"() d QEB EB>AFKD LC > SB@QLO BUMOBPPBA >P >K >KDIB
i LIN;N?() d OLQ>QB > 2$ SB@QLO ?V >K >KDIB
4EB .>QROB LC #LAB (S005)
37
i F?LJ() d IFKB>O FKQBOMLI>QB QL >KLQEBO SB@QLO
i >CMN() d QEB %R@IFAB>K AFPQ>K@B ?BQTBBK QTL SB@QLOP (@LKPFABOBA >P MLFKQP)
i ;HAF??NQ??H() d CFKA QEB >KDIB ?BQTBBK QTL SB@QLOP
i >IN() d QEB ALQ MOLAR@Q LC QTL SB@QLOP
i =LIMM() d QEB @OLPP MOLAR@Q LC QTL SB@QLOP (LKIV OBIBS>KQ FK QEOBB AFJBKPFLKP)
i L;H>IG2"() - J>HB > O>KALJ 2$ SB@QLO
i L;H>IG3"() - J>HB > O>KALJ 3$ SB@QLO
(>SFKD >IOB>AV @LSBOBA >AAFQFLK, IBQfP PQ>OQ TFQE PR?QO>@QFLK. 4EFP LKBfP KLQ PL ?>A; GRPQ Q>HB
QEB MIRP PFDK >KA OBMI>@B FQ TFQE > JFKRP!
Vector subtraction Vector subtraction
w
= u
+ v
= v
+ u
+ ( v
+ w
) = ( u
+ v
) + w
&>K@V QBOJFKLILDV >KA PVJ?LIP >PFAB, QEFP FP OB>IIV NRFQB > PFJMIB @LK@BMQ. 7BfOB GRPQ
P>VFKD QE>Q @LJJLK PBKPB MOLMBOQFBP LC >AAFQFLK >MMIV QL SB@QLOP >P TBII.
3 + 2 = 2 + 3
(3 + 2) + 1 = 3 + (2 + 1)
PIC> M?NOJ() U
MCT?(200,200);
W
PIC> >L;Q() U
<;=EALIOH>(255);
Two PVectors, one for the mouse location
and one for the center of the window
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
.3?=NIL =?HN?L = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
PVector subtraction! GIOM?.MO<(=?HN?L);
Draw a line to represent the vector. NL;HMF;N?(QC>NB/2,B?CABN/2);
FCH?(0,0,GIOM?.R,GIOM?.S);
W
4EB .>QROB LC #LAB (S005)
39
Vector multiplication Vector multiplication
-LSFKD LK QL JRIQFMIF@>QFLK, TB E>SB QL QEFKH > IFQQIB ?FQ AFCCBOBKQIV. 7EBK TB Q>IH >?LRQ
JRIQFMIVFKD > SB@QLO, TE>Q TB QVMF@>IIV JB>K FP >.,7492 >.,7492 > SB@QLO. )C TB T>KQBA QL P@>IB >
SB@QLO QL QTF@B FQP PFWB LO LKB-QEFOA LC FQP PFWB (IB>SFKD FQP AFOB@QFLK QEB P>JB), TB TLRIA P>V:
g-RIQFMIV QEB SB@QLO ?V 2h LO g-RIQFMIV QEB SB@QLO ?V 1/3.h .LQB QE>Q TB >OB JRIQFMIVFKD >
SB@QLO ?V > P@>I>O, > PFKDIB KRJ?BO, KLQ >KLQEBO SB@QLO.
4L P@>IB > SB@QLO, TB JRIQFMIV B>@E @LJMLKBKQ (R >KA S) ?V > P@>I>O.
w
= u
* n
@>K ?B TOFQQBK >P:
w
x
= u
x
* n
w
y
= u
y
* n
,BQfP ILLH >Q >K BU>JMIB TFQE SB@QLO
KLQ>QFLK.
u
= (3, 7)
n = 3
w
= u
* n
w
x
= 3 * 3
w
y
= 7 * 3
w
= (9, 21)
4EBOBCLOB, QEB CRK@QFLK FKPFAB QEB .3?=NIL
@I>PP FP TOFQQBK >P:
!KA FJMIBJBKQFKD JRIQFMIF@>QFLK FK @LAB FP >P PFJMIB >P:
F64B?2 1.8: )0.96;4 . C20A<?
PIC> GOFN(@FI;N H) U
With multiplication, the components of the
vector are multiplied by a number.
R = R * H;
S = S * H;
W
.3?=NIL O = H?Q .3?=NIL(-3,7);
This PVector is now three times the size
and is equal to (-9,21).
O.GOFN(3);
#E>MQBO 1. 6B@QLOP
40
Example 1.4: Multiplying a vector
$FSFPFLK TLOHP GRPQ IFHB JRIQFMIF@>QFLKdTB
PFJMIV OBMI>@B QEB JRIQFMIF@>QFLK PFDK
(>PQBOFPH) TFQE QEB AFSFPFLK PFDK (CLOT>OA
PI>PE).
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
W
PIC> >L;Q() U
<;=EALIOH>(255);
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
.3?=NIL =?HN?L = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
GIOM?.MO<(=?HN?L);
Multiplying a vector! The vector is now half
its original size (multiplied by 0.5).
GIOM?.GOFN(0.5);
NL;HMF;N?(QC>NB/2,B?CABN/2);
FCH?(0,0,GIOM?.R,GIOM?.S);
W
F64B?2 1.9
PIC> >CP(@FI;N H) U
R = R / H;
S = S / H;
W
.3?=NIL O = H?Q .3?=NIL(8,-4);
4EB .>QROB LC #LAB (S005)
41
More Number Properties with Vectors More Number Properties with Vectors
!P TFQE >AAFQFLK, ?>PF@ >IDB?O>F@ ORIBP LC JRIQFMIF@>QFLK >KA AFSFPFLK >MMIV QL SB@QLOP.
4EB >PPL@F>QFSB ORIB: (n * m) * v
= n * (m* v
)
4EB AFPQOF?RQFSB ORIB TFQE 2 P@>I>OP, 1 SB@QLO: (n * m) * v
= n * v
+ m* v
+ v
) * n = u
* n + v
* n
Dividing a vector! The vector is now half its
original size (divided by 2).
O.>CP(2);
1.5 Vector Magnitude 1.5 Vector Magnitude
-RIQFMIF@>QFLK >KA AFSFPFLK, >P TB GRPQ P>T, >OB JB>KP ?V TEF@E QEB IBKDQE LC QEB SB@QLO @>K
?B @E>KDBA TFQELRQ >CCB@QFKD AFOB@QFLK. 0BOE>MP VLRfOB TLKABOFKD: g/+, PL ELT AL ) HKLT
TE>Q QEB IBKDQE LC > SB@QLO FP? ) HKLT QEB @LJMLKBKQP (R >KA S), ?RQ ELT ILKD (FK MFUBIP) FP
QEB >@QR>I >OOLT?h 5KABOPQ>KAFKD ELT QL @>I@RI>QB QEB IBKDQE (>IPL HKLTK >P 8,294?@/0 8,294?@/0) LC
> SB@QLO FP FK@OBAF?IV RPBCRI >KA FJMLOQ>KQ.
.LQF@B FK QEB >?LSB AF>DO>J ELT QEB
SB@QLO, AO>TK >P >K >OOLT >KA QTL
@LJMLKBKQP (R >KA S), @OB>QBP > OFDEQ
QOF>KDIB. 4EB PFABP >OB QEB @LJMLKBKQP >KA
QEB EVMLQBKRPB FP QEB >OOLT FQPBIC. 7BfOB
SBOV IR@HV QL E>SB QEFP OFDEQ QOF>KDIB,
?B@>RPB LK@B RMLK > QFJB, > 'OBBH
J>QEBJ>QF@F>K K>JBA 0VQE>DLO>P
ABSBILMBA > ILSBIV CLOJRI> QL ABP@OF?B QEB
OBI>QFLKPEFM ?BQTBBK QEB PFABP >KA
EVMLQBKRPB LC > OFDEQ QOF>KDIB.
4EB 0VQE>DLOB>K QEBLOBJ FP 5 PNR>OBA
MIRP 6 PNR>OBA BNR>IP 7 PNR>OBA.
!OJBA TFQE QEFP CLOJRI>, TB @>K KLT
@LJMRQB QEB J>DKFQRAB LC v
>P CLIILTP:
v
= v
x
* v
x
+ v
y
* v
y
LO FK .3?=NIL:
F64B?2 1.10: *52 92;4A5 <? I:.4;6AB12J <3 .
C20A<? C 6@ <3A2; D?6AA2; .@: C
F64B?2 1.11: *52 'FA5.4<?2.; *52<?2:
#E>MQBO 1. 6B@QLOP
42
Example 1.5: Vector magnitude
@FI;N G;A() U
L?NOLH MKLN(R*R + S*S);
W
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
W
PIC> >L;Q() U
<;=EALIOH>(255);
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
.3?=NIL =?HN?L = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
GIOM?.MO<(=?HN?L);
The magnitude (i.e. length) of a vector can
be accessed via the mag() function. Here it
is used as the width of a rectangle drawn at
the top of the window.
@FI;N G = GIOM?.G;A();
@CFF(0);
L?=N(0,0,G,10);
NL;HMF;N?(QC>NB/2,B?CABN/2);
FCH?(0,0,GIOM?.R,GIOM?.S);
W
1.6 Normalizing Vectors 1.6 Normalizing Vectors
#>I@RI>QFKD QEB J>DKFQRAB LC > SB@QLO FP LKIV QEB ?BDFKKFKD. 4EB J>DKFQRAB CRK@QFLK LMBKP
QEB ALLO QL J>KV MLPPF?FIFQFBP, QEB CFOPQ LC TEF@E FP 9:=8,74E,?4:9 9:=8,74E,?4:9. .LOJ>IFWFKD OBCBOP QL QEB
MOL@BPP LC J>HFKD PLJBQEFKD gPQ>KA>OAh LO, TBII, gKLOJ>I.h )K QEB @>PB LC SB@QLOP, IBQfP
>PPRJB CLO QEB JLJBKQ QE>Q > PQ>KA>OA SB@QLO E>P > IBKDQE LC 1. 4L KLOJ>IFWB > SB@QLO,
4EB .>QROB LC #LAB (S005)
43
QEBOBCLOB, FP QL Q>HB > SB@QLO LC >KV IBKDQE >KA, HBBMFKD FQ MLFKQFKD FK QEB P>JB AFOB@QFLK,
@E>KDB FQP IBKDQE QL 1, QROKFKD FQ FKQL TE>Q FP @>IIBA > @94? A0.?:= @94? A0.?:=.
3FK@B FQ ABP@OF?BP > SB@QLOfP AFOB@QFLK
TFQELRQ OBD>OA QL FQP IBKDQE, FQfP RPBCRI QL
E>SB QEB RKFQ SB@QLO OB>AFIV >@@BPPF?IB.
7BfII PBB QEFP @LJB FK E>KAV LK@B TB PQ>OQ
QL TLOH TFQE CLO@BP FK #E>MQBO 2.
&LO >KV DFSBK SB@QLO u
=
u
)K LQEBO TLOAP, QL KLOJ>IFWB > SB@QLO, PFJMIV AFSFAB B>@E @LJMLKBKQ ?V FQP J>DKFQRAB. 4EFP
FP MOBQQV FKQRFQFSB. 3>V > SB@QLO FP LC IBKDQE 5. 7BII, 5 AFSFABA ?V 5 FP 1. 3L, ILLHFKD >Q LRO
OFDEQ QOF>KDIB, TB QEBK KBBA QL P@>IB QEB EVMLQBKRPB ALTK ?V AFSFAFKD ?V 5. )K QE>Q MOL@BPP
QEB PFABP PEOFKH, AFSFABA ?V 5 >P TBII.
)K QEB .3?=NIL @I>PP, TB QEBOBCLOB TOFQB
LRO KLOJ>IFW>QFLK CRK@QFLK >P CLIILTP:
/C @LROPB, QEBOBfP LKB PJ>II FPPRB. 7E>Q FC QEB J>DKFQRAB LC QEB SB@QLO FP 0? 7B @>KfQ AFSFAB
?V 0! 3LJB NRF@H BOOLO @EB@HFKD TFII CFU QE>Q OFDEQ RM:
F64B?2 1.12
F64B?2 1.13
PIC> HILG;FCT?() U
@FI;N G = G;A();
>CP(G);
W
PIC> HILG;FCT?() U
@FI;N G = G;A();
C@ (G != 0) U
>CP(G);
W
W
#E>MQBO 1. 6B@QLOP
44
Example 1.6: Normalizing a vector
PIC> >L;Q() U
<;=EALIOH>(255);
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
.3?=NIL =?HN?L = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
GIOM?.MO<(=?HN?L);
In this example, after the vector is
normalized, it is multiplied by 50 so that it is
viewable onscreen. Note that no matter
where the mouse is, the vector will have the
same length (50) due to the normalization
process.
GIOM?.HILG;FCT?();
GIOM?.GOFN(50);
NL;HMF;N?(QC>NB/2,B?CABN/2);
FCH?(0,0,GIOM?.R,GIOM?.S);
W
1.7 Vector Motion: Velocity 1.7 Vector Motion: Velocity
!II QEFP SB@QLO J>QE PQRCC PLRKAP IFHB PLJBQEFKD TB PELRIA HKLT >?LRQ, ?RQ TEV? (LT TFII FQ
>@QR>IIV EBIM RP TOFQB @LAB? 4EB QORQE LC QEB J>QQBO FP QE>Q TB KBBA QL E>SB PLJB M>QFBK@B. )Q
TFII Q>HB PLJB QFJB ?BCLOB QEB >TBPLJBKBPP LC RPFKD QEB .3?=NIL @I>PP CRIIV @LJBP QL IFDEQ.
4EFP FP >@QR>IIV > @LJJLK L@@ROOBK@B TEBK CFOPQ IB>OKFKD > KBT A>Q> PQOR@QROB. &LO BU>JMIB,
TEBK VLR CFOPQ IB>OK >?LRQ >K >OO>V, FQ JFDEQ PBBJ IFHB JR@E JLOB TLOH QL RPB >K >OO>V QE>K QL
GRPQ E>SB PBSBO>I S>OF>?IBP PQ>KA CLO JRIQFMIB QEFKDP. "RQ QE>Q MI>K NRF@HIV ?OB>HP ALTK TEBK
VLR KBBA > ERKAOBA, LO > QELRP>KA, LO QBK QELRP>KA QEFKDP. 4EB P>JB @>K ?B QORB CLO
.3?=NIL. 7E>Q JFDEQ PBBJ IFHB JLOB TLOH KLT TFII M>V LCC I>QBO, >KA M>V LCC NRFQB KF@BIV. !KA
VLR ALKfQ E>SB QL T>FQ QLL ILKD, >P VLRO OBT>OA TFII @LJB FK QEB KBUQ @E>MQBO.
&LO KLT, ELTBSBO, TB T>KQ QL CL@RP LK PFJMIF@FQV. 7E>Q ALBP FQ JB>K QL MOLDO>J JLQFLK RPFKD
SB@QLOP? 7BfSB PBBK QEB ?BDFKKFKD LC QEFP FK %U>JMIB 1.2 (PBB M>DB 35): QEB ?LRK@FKD ?>II. !K
L?GB@Q LK P@OBBK E>P > IL@>QFLK (TEBOB FQ FP >Q >KV DFSBK JLJBKQ) >P TBII >P > SBIL@FQV
(FKPQOR@QFLKP CLO ELT FQ PELRIA JLSB COLJ LKB JLJBKQ QL QEB KBUQ). 6BIL@FQV FP >AABA QL
IL@>QFLK:
4EB .>QROB LC #LAB (S005)
45
!KA QEBK TB AO>T QEB L?GB@Q >Q QE>Q IL@>QFLK:
4EFP FP -LQFLK 101.
1. A// A07:.4?D ?: 7:.,?4:9 A// A07:.4?D ?: 7:.,?4:9
2. D=,B :-50.? ,? 7:.,?4:9 D=,B :-50.? ,? 7:.,?4:9
)K QEB ?LRK@FKD ?>II BU>JMIB, >II LC QEFP @LAB E>MMBKBA FK 0OL@BPPFKDfP J>FK Q>?, TFQEFK
M?NOJ() >KA >L;Q(). 7E>Q TB T>KQ QL AL KLT FP JLSB QLT>OAP BK@>MPRI>QFKD >II LC QEB
ILDF@ CLO JLQFLK FKPFAB LC > .7,>> .7,>>. 4EFP T>V, TB @>K @OB>QB > CLRKA>QFLK CLO MOLDO>JJFKD
JLSFKD L?GB@QP FK 0OL@BPPFKD. )K PB@QFLK ).2 LC QEB FKQOLAR@QFLK (PBB M>DB 2), g4EB 2>KALJ
7>IHBO #I>PP,h TB ?OFBCIV OBSFBTBA QEB ?>PF@P LC L?GB@Q-LOFBKQBA-MOLDO>JJFKD (g//0h).
"BVLKA QE>Q PELOQ FKQOLAR@QFLK, QEFP ?LLH >PPRJBP BUMBOFBK@B TFQE L?GB@QP >KA @I>PPBP FK
0OL@BPPFKD. )C VLR KBBA > OBCOBPEBO, ) BK@LRO>DB VLR QL @EB@H LRQ QEB 0OL@BPPFKD L?GB@QP
QRQLOF>I (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/L?GB@QP/).
)K QEFP @>PB, TBfOB DLFKD QL @OB>QB > DBKBOF@ +IP?L @I>PP QE>Q TFII ABP@OF?B > QEFKD JLSFKD
>OLRKA QEB P@OBBK. !KA PL TB JRPQ @LKPFABO QEB CLIILTFKD QTL NRBPQFLKP:
1. +3,? /,?, /:0> , 8:A0= 3,A0? +3,? /,?, /:0> , 8:A0= 3,A0?
2. +3,? 1@9.?4:9,74?D /:0> , 8:A0= 3,A0? +3,? 1@9.?4:9,74?D /:0> , 8:A0= 3,A0?
/RO -LQFLK 101 >IDLOFQEJ QBIIP RP QEB >KPTBOP QL QEBPB NRBPQFLKP. ! +IP?L L?GB@Q E>P QTL
MFB@BP LC A>Q>: FI=;NCIH >KA P?FI=CNS, TEF@E >OB ?LQE .3?=NIL L?GB@QP.
)QP CRK@QFLK>IFQV FP GRPQ >?LRQ >P PFJMIB. 4EB +IP?L KBBAP QL JLSB >KA FQ KBBAP QL ?B PBBK.
7BfII FJMIBJBKQ QEBPB KBBAP >P CRK@QFLKP K>JBA OJ>;N?() >KA >CMJF;S(). 7BfII MRQ >II LC
LRO JLQFLK ILDF@ @LAB FK OJ>;N?() >KA AO>T QEB L?GB@Q FK >CMJF;S().
FI=;NCIH.;>>(P?FI=CNS);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
PIC> OJ>;N?() U
The Mover moves. FI=;NCIH.;>>(P?FI=CNS);
W
PIC> >CMJF;S() U
MNLIE?(0);
@CFF(175);
#E>MQBO 1. 6B@QLOP
46
7BfSB CLODLQQBK LKB @OR@F>I FQBJ, ELTBSBO: QEB L?GB@QfP .:9>?=@.?:= .:9>?=@.?:=. 4EB @LKPQOR@QLO FP >
PMB@F>I CRK@QFLK FKPFAB LC > @I>PP QE>Q @OB>QBP QEB FKPQ>K@B LC QEB L?GB@Q FQPBIC. )Q FP TEBOB VLR
DFSB FKPQOR@QFLKP LK ELT QL PBQ RM QEB L?GB@Q. )Q >IT>VP E>P QEB P>JB K>JB >P QEB @I>PP >KA FP
@>IIBA ?V FKSLHFKD QEB 90B 90B LMBO>QLO:
)K LRO @>PB, IBQfP >O?FQO>OFIV AB@FAB QL FKFQF>IFWB LRO +IP?L L?GB@Q ?V DFSFKD FQ > O>KALJ IL@>QFLK
>KA > O>KALJ SBIL@FQV.
)C L?GB@Q-LOFBKQBA MOLDO>JJFKD FP >Q >II KBT QL VLR, LKB >PMB@Q EBOB J>V PBBJ > ?FQ
@LKCRPFKD. !CQBO >II, TB PMBKQ QEB ?BDFKKFKD LC QEFP @E>MQBO AFP@RPPFKD QEB .3?=NIL @I>PP. 4EB
.3?=NIL @I>PP FP QEB QBJMI>QB CLO J>HFKD QEB FI=;NCIH L?GB@Q >KA QEB P?FI=CNS L?GB@Q. 3L
TE>Q >OB QEBV ALFKD FKPFAB LC VBQ >KLQEBO L?GB@Q, QEB +IP?L L?GB@Q? )K C>@Q, QEFP FP GRPQ >?LRQ
QEB JLPQ KLOJ>I QEFKD BSBO. !K L?GB@Q FP PFJMIV PLJBQEFKD QE>Q ELIAP A>Q> (>KA CRK@QFLK>IFQV).
4E>Q A>Q> @>K ?B KRJ?BOP (FKQBDBOP, CIL>QP, BQ@.) LO LQEBO L?GB@QP! 7BfII PBB QEFP LSBO >KA LSBO
>D>FK FK QEFP ?LLH. &LO BU>JMIB, FK #E>MQBO 4 (PBB M>DB 144) TBfII TOFQB > @I>PP QL ABP@OF?B >
PVPQBJ LC M>OQF@IBP. 4E>Q .;LNC=F?0SMN?G L?GB@Q TFII E>SB >P FQP A>Q> > IFPQ LC .;LNC=F?
L?GB@QPj>KA B>@E .;LNC=F? L?GB@Q TFII E>SB >P FQP A>Q> PBSBO>I .3?=NIL L?GB@QP!
,BQfP CFKFPE LCC QEB +IP?L @I>PP ?V FK@LOMLO>QFKD > CRK@QFLK QL ABQBOJFKB TE>Q QEB L?GB@Q PELRIA
AL TEBK FQ OB>@EBP QEB BADB LC QEB TFKALT. &LO KLT IBQfP AL PLJBQEFKD PFJMIB, >KA GRPQ E>SB
FQ TO>M >OLRKA QEB BADBP.
The Mover is displayed. ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);
W
W
+IP?L G = H?Q +IP?L();
+IP?L() U
FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN));
P?FI=CNS = H?Q .3?=NIL(L;H>IG(-2,2),L;H>IG(-2,2));
W
PIC> =B?=E#>A?M() U
When it reaches one edge, set location to
the other.
C@ (FI=;NCIH.R > QC>NB) U
FI=;NCIH.R = 0;
W ?FM? C@ (FI=;NCIH.R < 0) U
FI=;NCIH.R = QC>NB;
W
C@ (FI=;NCIH.S > B?CABN) U
FI=;NCIH.S = 0;
W ?FM? C@ (FI=;NCIH.S < 0) U
FI=;NCIH.S = B?CABN;
W
4EB .>QROB LC #LAB (S005)
47
.LT QE>Q QEB +IP?L @I>PP FP CFKFPEBA, TB @>K ILLH >Q TE>Q TB KBBA QL AL FK LRO J>FK
MOLDO>J. 7B CFOPQ AB@I>OB > +IP?L L?GB@Q:
4EBK FKFQF>IFWB QEB JLSBO FK M?NOJ():
>KA @>II QEB >MMOLMOF>QB CRK@QFLKP FK >L;Q():
(BOB FP QEB BKQFOB BU>JMIB CLO OBCBOBK@B:
Example 1.7: Motion 101 (velocity)
W
+IP?L GIP?L;
GIP?L = H?Q +IP?L();
GIP?L.OJ>;N?();
GIP?L.=B?=E#>A?M();
GIP?L.>CMJF;S();
Declare Mover object. +IP?L GIP?L;
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
Create Mover object. GIP?L = H?Q +IP?L();
W
PIC> >L;Q() U
<;=EALIOH>(255);
#E>MQBO 1. 6B@QLOP
48
Call functions on Mover object. GIP?L.OJ>;N?();
GIP?L.=B?=E#>A?M();
GIP?L.>CMJF;S();
W
=F;MM +IP?L U
Our object has two PVectors: location and
velocity.
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
+IP?L() U
FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN));
P?FI=CNS = H?Q .3?=NIL(L;H>IG(-2,2),L;H>IG(-2,2));
W
PIC> OJ>;N?() U
Motion 101: Location changes by velocity. FI=;NCIH.;>>(P?FI=CNS);
W
PIC> >CMJF;S() U
MNLIE?(0);
@CFF(175);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);
W
PIC> =B?=E#>A?M() U
C@ (FI=;NCIH.R > QC>NB) U
FI=;NCIH.R = 0;
W ?FM? C@ (FI=;NCIH.R < 0) U
FI=;NCIH.R = QC>NB;
W
C@ (FI=;NCIH.S > B?CABN) U
FI=;NCIH.S = 0;
W ?FM? C@ (FI=;NCIH.S < 0) U
FI=;NCIH.S = B?CABN;
W
W
W
1.8 Vector Motion: Acceleration 1.8 Vector Motion: Acceleration
/+. !Q QEFP MLFKQ, TB PELRIA CBBI @LJCLOQ>?IB TFQE QTL QEFKDP: (1) TE>Q > .3?=NIL FP >KA (2) ELT
TB RPB .3?=NILP FKPFAB LC >K L?GB@Q QL HBBM QO>@H LC FQP IL@>QFLK >KA JLSBJBKQ. 4EFP FP >K
BU@BIIBKQ CFOPQ PQBM >KA ABPBOSBP > JFIA OLRKA LC >MMI>RPB. "BCLOB PQ>KAFKD LS>QFLKP >KA
P@OB>JFKD C>KP, ELTBSBO, TB KBBA QL J>HB LKB JLOB, PLJBTE>Q ?FDDBO PQBM CLOT>OA. !CQBO
>II, T>Q@EFKD QEB -LQFLK 101 BU>JMIB FP C>FOIV ?LOFKDdQEB @FO@IB KBSBO PMBBAP RM, KBSBO PILTP
4EB .>QROB LC #LAB (S005)
49
ALTK, >KA KBSBO QROKP. &LO JLOB FKQBOBPQFKD JLQFLK, CLO JLQFLK QE>Q >MMB>OP FK QEB OB>I
TLOIA >OLRKA RP, TB KBBA QL >AA LKB JLOB .3?=NIL QL LRO @I>PPd;==?F?L;NCIH.
4EB PQOF@Q ABCFKFQFLK LC ,..070=,?4:9 ,..070=,?4:9 TBfOB RPFKD EBOB FP: H<9 F5H9 C: 7<5B;9 C: J9@C7=HM. ,BQfP
QEFKH >?LRQ QE>Q ABCFKFQFLK CLO > JLJBKQ. )P QEFP > KBT @LK@BMQ? .LQ OB>IIV. 6BIL@FQV FP
ABCFKBA >P H<9 F5H9 C: 7<5B;9 C: @C75H=CB. )K BPPBK@B, TB >OB ABSBILMFKD > gQOF@HIB-ALTKh
BCCB@Q. !@@BIBO>QFLK >CCB@QP SBIL@FQV, TEF@E FK QROK >CCB@QP IL@>QFLK (CLO PLJB ?OFBC
CLOBPE>ALTFKD, QEFP MLFKQ TFII ?B@LJB BSBK JLOB @OR@F>I FK QEB KBUQ @E>MQBO, TEBK TB PBB
ELT CLO@BP >CCB@Q >@@BIBO>QFLK, TEF@E >CCB@QP SBIL@FQV, TEF@E >CCB@QP IL@>QFLK). )K @LAB, QEFP
OB>AP:
!P >K BUBO@FPB, COLJ QEFP MLFKQ CLOT>OA, IBQfP J>HB > ORIB CLO LROPBISBP. ,BQfP TOFQB BSBOV
BU>JMIB FK QEB OBPQ LC QEFP ?LLH TFQELRQ BSBO QLR@EFKD QEB S>IRB LC SBIL@FQV >KA IL@>QFLK
(BU@BMQ QL FKFQF>IFWB QEBJ). )K LQEBO TLOAP, LRO DL>I KLT CLO MOLDO>JJFKD JLQFLK FP: #LJB RM
TFQE >K >IDLOFQEJ CLO ELT TB @>I@RI>QB >@@BIBO>QFLK >KA IBQ QEB QOF@HIB-ALTK BCCB@Q TLOH FQP
J>DF@. ()K QORQE, VLRfII CFKA OB>PLKP QL ?OB>H QEFP ORIB, ?RQ FQfP FJMLOQ>KQ QL FIIRPQO>QB QEB
MOFK@FMIBP ?BEFKA LRO JLQFLK >IDLOFQEJ.) !KA PL TB KBBA QL @LJB RM TFQE PLJB T>VP QL
@>I@RI>QB >@@BIBO>QFLK:
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
Acceleration Algorithms! Acceleration Algorithms!
1. A 7CBGH5BH 5779@9F5H=CB
2. A HCH5@@M F5B8CA 5779@9F5H=CB
3. A779@9F5H=CB HCK5F8G H<9 ACIG9
!IDLOFQEJ #1, 5 7CBGH5BH 5779@9F5H=CB, FP KLQ M>OQF@RI>OIV FKQBOBPQFKD, ?RQ FQ FP QEB PFJMIBPQ
>KA TFII EBIM RP ?BDFK FK@LOMLO>QFKD >@@BIBO>QFLK FKQL LRO @LAB. 4EB CFOPQ QEFKD TB KBBA QL
AL FP >AA >KLQEBO .3?=NIL QL QEB +IP?L @I>PP:
!KA FK@LOMLO>QB >@@BIBO>QFLK FKQL QEB OJ>;N?() CRK@QFLK:
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
A new PVector for acceleration +0;9JEH 799;B;H7J?ED;
PIC> OJ>;N?() U
Our motion algorithm is now two lines of
code!
L;BE9?JO.7::(799;B;H7J?ED);
FI=;NCIH.;>>(P?FI=CNS);
W
#E>MQBO 1. 6B@QLOP
50
7BfOB >IJLPQ ALKB. 4EB LKIV JFPPFKD MFB@B FP FKFQF>IFW>QFLK FK QEB @LKPQOR@QLO.
,BQfP PQ>OQ QEB +IP?L L?GB@Q FK QEB JFAAIB LC QEB TFKALTj
jTFQE >K FKFQF>I SBIL@FQV LC WBOL.
4EFP JB>KP QE>Q TEBK QEB PHBQ@E PQ>OQP, QEB L?GB@Q FP >Q OBPQ. 7B ALKfQ E>SB QL TLOOV >?LRQ
SBIL@FQV >KVJLOB, >P TB >OB @LKQOLIIFKD QEB L?GB@QfP JLQFLK BKQFOBIV TFQE >@@BIBO>QFLK.
3MB>HFKD LC TEF@E, >@@LOAFKD QL !IDLOFQEJ #1, LRO CFOPQ PHBQ@E FKSLISBP @LKPQ>KQ >@@BIBO>QFLK.
3L IBQfP MF@H > S>IRB.
->V?B VLRfOB QEFKHFKD, g'LPE, QELPB S>IRBP PBBJ >TCRIIV PJ>II!h 4E>QfP OFDEQ, QEBV >OB NRFQB
QFKV. )QfP FJMLOQ>KQ QL OB>IFWB QE>Q LRO >@@BIBO>QFLK S>IRBP (JB>PROBA FK MFUBIP) >@@RJRI>QB LSBO
QFJB FK QEB SBIL@FQV, >?LRQ QEFOQV QFJBP MBO PB@LKA ABMBKAFKD LK LRO PHBQ@EfP CO>JB O>QB. !KA
PL QL HBBM QEB J>DKFQRAB LC QEB SBIL@FQV SB@QLO TFQEFK > OB>PLK>?IB O>KDB, LRO >@@BIBO>QFLK
S>IRBP PELRIA OBJ>FK NRFQB PJ>II. 7B @>K >IPL EBIM QEFP @>RPB ?V FK@LOMLO>QFKD QEB .3?=NIL
CRK@QFLK FCGCN().
4EFP QO>KPI>QBP QL QEB CLIILTFKD:
2<5H =G H<9 A5;B=HI89 C: J9@C7=HM? %: =HSG @9GG H<5B 10, BC KCFF=9G; >IGH @95J9 =H 5G =G. %: =HSG ACF9
H<5B 10, <CK9J9F, F98I79 =H HC 10!
+IP?L() U
FI=;NCIH = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
P?FI=CNS = H?Q .3?=NIL(0,0);
;==?F?L;NCIH = H?Q .3?=NIL(-0.001,0.01);
W
The limit() function constrains the magnitude
of a vector.
P?FI=CNS.FCGCN(10);
4EB .>QROB LC #LAB (S005)
51
,BQfP Q>HB > ILLH >Q QEB @E>KDBP QL QEB +IP?L @I>PP, @LJMIBQB TFQE ;==?F?L;NCIH >KA
FCGCN().
Example 1.8: Motion 101 (velocity and constant acceleration)
7OFQB QEB FCGCN() CRK@QFLK CLO QEB .3?=NIL @I>PP.
PIC> FCGCN(@FI;N G;R) U
C@ (::::::: > :::::::) U
:::::::::();
::::(G;R);
W
W
Exercise 1.4 Exercise 1.4
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
Acceleration is the key! .3?=NIL ;==?F?L;NCIH;
The variable topspeed will limit the
magnitude of velocity.
@FI;N NIJMJ??>;
+IP?L() U
FI=;NCIH = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
P?FI=CNS = H?Q .3?=NIL(0,0);
;==?F?L;NCIH = H?Q .3?=NIL(-0.001,0.01);
NIJMJ??> = 10;
W
PIC> OJ>;N?() U
Velocity changes by acceleration and is
limited by topspeed.
P?FI=CNS.;>>(;==?F?L;NCIH);
P?FI=CNS.FCGCN(NIJMJ??>);
#E>MQBO 1. 6B@QLOP
52
.LT LK QL !IDLOFQEJ #2, 5 HCH5@@M F5B8CA 5779@9F5H=CB. )K QEFP @>PB, FKPQB>A LC FKFQF>IFWFKD
>@@BIBO>QFLK FK QEB L?GB@QfP @LKPQOR@QLO, TB T>KQ QL MF@H > KBT >@@BIBO>QFLK B>@E @V@IB, F.B.
B>@E QFJB OJ>;N?() FP @>IIBA.
Example 1.9: Motion 101 (velocity and random acceleration)
"B@>RPB QEB O>KALJ SB@QLO FP > KLOJ>IFWBA LKB, TB @>K QOV P@>IFKD FQ:
(>) P@>IFKD QEB >@@BIBO>QFLK QL > @LKPQ>KQ S>IRB
FI=;NCIH.;>>(P?FI=CNS);
W
display() is the same. PIC> >CMJF;S() UW
checkEdges() is the same. PIC> =B?=E#>A?M() UW
W
#OB>QB > PFJRI>QFLK LC > @>O (LO ORKKBO) QE>Q >@@BIBO>QBP TEBK VLR MOBPP QEB RM HBV >KA
?O>HBP TEBK VLR MOBPP QEB ALTK HBV.
Exercise 1.5 Exercise 1.5
PIC> OJ>;N?() U
The random2D() function will give us a
PVector of length 1 pointing in a random
direction.
;==?F?L;NCIH = .3?=NIL.L;H>IG2"();
P?FI=CNS.;>>(;==?F?L;NCIH);
P?FI=CNS.FCGCN(NIJMJ??>);
FI=;NCIH.;>>(P?FI=CNS);
W
;==?F?L;NCIH = .3?=NIL.L;H>IG2"();
4EB .>QROB LC #LAB (S005)
53
(?) P@>IFKD QEB >@@BIBO>QFLK QL > O>KALJ S>IRB
7EFIB QEFP J>V PBBJ IFHB >K L?SFLRP MLFKQ, FQfP @OR@F>I QL RKABOPQ>KA QE>Q >@@BIBO>QFLK ALBP
KLQ JBOBIV OBCBO QL QEB GD998=B; ID LO G@CK=B; 8CKB LC > JLSFKD L?GB@Q, ?RQ O>QEBO 5BM
7<5B;9 FK SBIL@FQV FK BFQEBO J>DKFQRAB LO AFOB@QFLK. !@@BIBO>QFLK FP RPBA QL PQBBO >K L?GB@Q,
>KA TBfII PBB QEFP >D>FK >KA >D>FK FK CRQROB @E>MQBOP >P TB ?BDFK QL MOLDO>J L?GB@QP QE>Q
J>HB AB@FPFLKP >?LRQ ELT QL JLSB >?LRQ QEB P@OBBK.
Constant 799;B;H7J?ED.CKBJ(0.5);
;==?F?L;NCIH = .3?=NIL.L;H>IG2"();
Random 799;B;H7J?ED.CKBJ(H7D:EC(2));
2BCBOOFKD ?>@H QL QEB )KQOLAR@QFLK (PBB M>DB 17), FJMIBJBKQ >@@BIBO>QFLK >@@LOAFKD QL
0BOIFK KLFPB.
Exercise 1.6 Exercise 1.6
1.9 Static vs. Non-Static Functions 1.9 Static vs. Non-Static Functions
"BCLOB TB DBQ QL !IDLOFQEJ #3 (5779@9F5H9 HCK5F8G H<9 ACIG9), TB KBBA QL @LSBO LKB JLOB
O>QEBO FJMLOQ>KQ >PMB@Q LC TLOHFKD TFQE SB@QLOP >KA QEB .3?=NIL @I>PP: QEB AFCCBOBK@B
?BQTBBK RPFKD >?,?4. >?,?4. JBQELAP >KA 9:9->?,?4. 9:9->?,?4. JBQELAP.
&LODBQQFKD >?LRQ SB@QLOP CLO > JLJBKQ, Q>HB > ILLH >Q QEB CLIILTFKD @LAB:
0OBQQV PFJMIB, OFDEQ? R E>P QEB S>IRB LC 0, TB >AA S QL FQ, >KA KLT R FP BNR>I QL 5. 7B @LRIA
TOFQB QEB @LOOBPMLKAFKD @LAB MOBQQV B>PFIV ?>PBA LK TE>Q TBfSB IB>OKBA >?LRQ .3?=NIL.
4EB SB@QLO P E>P QEB S>IRB LC (0,0), TB >AA O QL FQ, >KA KLT P FP BNR>I QL (4,5). %>PV, OFDEQ?
,BQfP Q>HB > ILLH >Q >KLQEBO BU>JMIB LC PLJB PFJMIB CIL>QFKD MLFKQ J>QE:
@FI;N R = 0;
@FI;N S = 5;
R = R + S;
.3?=NIL P = H?Q .3?=NIL(0,0);
.3?=NIL O = H?Q .3?=NIL(4,5);
P.;>>(O);
#E>MQBO 1. 6B@QLOP
54
R E>P QEB S>IRB LC 0, TB >AA S QL FQ, >KA PQLOB QEB OBPRIQ FK > KBT S>OF>?IB T. 4EB S>IRB LC R
ALBP KLQ @E>KDB FK QEFP BU>JMIB (KBFQEBO ALBP S)! 4EFP J>V PBBJ IFHB > QOFSF>I MLFKQ, >KA LKB
QE>Q FP NRFQB FKQRFQFSB TEBK FQ @LJBP QL J>QEBJ>QF@>I LMBO>QFLKP TFQE CIL>QP. (LTBSBO, FQfP KLQ
PL L?SFLRP TFQE J>QEBJ>QF@>I LMBO>QFLKP FK .3?=NIL. ,BQfP QOV QL TOFQB QEB @LAB ?>PBA LK TE>Q
TB HKLT PL C>O.
4EB >?LSB JFDEQ PBBJ IFHB > DLLA DRBPP, ?RQ FQfP GRPQ KLQ QEB T>V QEB .3?=NIL @I>PP TLOHP. )C
TB ILLH >Q QEB ABCFKFQFLK LC ;>>() . . .
TB PBB QE>Q QEFP @LAB ALBP KLQ >@@LJMIFPE LRO DL>I. &FOPQ, FQ ALBP KLQ OBQROK > KBT .3?=NIL
(QEB OBQROK QVMB FP gSLFAh) >KA PB@LKA, FQ @E>KDBP QEB S>IRB LC QEB .3?=NIL RMLK TEF@E FQ FP
@>IIBA. )K LOABO QL >AA QTL .3?=NIL L?GB@QP QLDBQEBO >KA OBQROK QEB OBPRIQ >P > KBT .3?=NIL,
TB JRPQ RPB QEB PQ>QF@ ;>>() CRK@QFLK.
&RK@QFLKP QE>Q TB @>II COLJ QEB @I>PP K>JB FQPBIC (O>QEBO QE>K COLJ > PMB@Fk@ L?GB@Q FKPQ>K@B)
>OB HKLTK >P >?,?4. 1@9.?4:9> >?,?4. 1@9.?4:9>. (BOB >OB QTL BU>JMIBP LC CRK@QFLK @>IIP QE>Q >PPRJB QTL
.3?=NIL L?GB@QP, P >KA O:
3FK@B VLR @>KfQ TOFQB PQ>QF@ CRK@QFLKP VLROPBIC FK 0OL@BPPFKD, VLR JFDEQ KLQ E>SB BK@LRKQBOBA
QEBJ ?BCLOB. .3?=NIL'P PQ>QF@ CRK@QFLKP >IILT RP QL MBOCLOJ DBKBOF@ J>QEBJ>QF@>I LMBO>QFLKP
LK .3?=NIL L?GB@QP TFQELRQ E>SFKD QL >AGRPQ QEB S>IRB LC LKB LC QEB FKMRQ .3?=NILP. ,BQfP ILLH
>Q ELT TB JFDEQ TOFQB QEB PQ>QF@ SBOPFLK LC ;>>():
@FI;N R = 0;
@FI;N S = 5;
@FI;N T = R + S;
.3?=NIL P = H?Q .3?=NIL(0,0);
.3?=NIL O = H?Q .3?=NIL(4,5);
Dont be fooled; this is incorrect!!! .3?=NIL Q = P.;>>(O);
PIC> ;>>(.3?=NIL P) U
R = R + P.R;
S = S + P.S;
W
Static: called from the class name. .3?=NIL.;>>(P,O);
Not static: called from an object instance. P.;>>(O);
4EB .>QROB LC #LAB (S005)
55
4EBOB >OB PBSBO>I AFCCBOBK@BP EBOB:
i 4EB CRK@QFLK FP I>?BIBA >P >?,?4. >?,?4..
i 4EB CRK@QFLK ALBP KLQ E>SB > A:4/ A:4/ OBQROK QVMB, ?RQ O>QEBO OBQROKP > .3?=NIL.
i 4EB CRK@QFLK @OB>QBP > KBT .3?=NIL (P3) >KA OBQROKP QEB PRJ LC QEB @LJMLKBKQP
LC P1 >KA P2 FK QE>Q KBT .3?=NIL.
7EBK VLR @>II > PQ>QF@ CRK@QFLK, FKPQB>A LC OBCBOBK@FKD >K >@QR>I L?GB@Q FKPQ>K@B, VLR PFJMIV
OBCBOBK@B QEB K>JB LC QEB @I>PP FQPBIC.
4EB .3?=NIL @I>PP E>P PQ>QF@ SBOPFLKP LC ;>>(), MO<(), GOFN(), >KA >CP().
The static version of add allows us to add
two PVectors together and assign the
result to a new PVector while leaving the
original PVectors (v and u above) intact.
MN;NC= .3?=NIL ;>>(.3?=NIL P1, .3?=NIL P2) U
.3?=NIL P3 = H?Q .3?=NIL(P1.R + P2.R, P1.S + P2.S);
L?NOLH P3;
W
.3?=NIL P = H?Q .3?=NIL(0,0);
.3?=NIL O = H?Q .3?=NIL(4,5);
.3?=NIL Q = P.;>>(O);
+0;9JEH M = +0;9JEH.7::(L,K);
4O>KPI>QB QEB CLIILTFKD MPBRAL@LAB QL @LAB RPFKD PQ>QF@ LO KLK-PQ>QF@ CRK@QFLKP TEBOB
>MMOLMOF>QB.
i 4EB .3?=NIL P BNR>IP (1,5).
i 4EB .3?=NIL O BNR>IP P JRIQFMIFBA ?V 2.
i 4EB .3?=NIL Q BNR>IP P JFKRP O.
i $FSFAB QEB .3?=NIL T ?V 3.
.3?=NIL P = H?Q .3?=NIL(1,5);
.3?=NIL O = ::::::::.:::::(::,::);
.3?=NIL Q = ::::::::.:::::(::,::);
:::::::::::;
Exercise 1.7 Exercise 1.7
#E>MQBO 1. 6B@QLOP
56
1.10 Interactivity with Acceleration 1.10 Interactivity with Acceleration
4L CFKFPE LRQ QEFP @E>MQBO, IBQfP QOV PLJBQEFKD
> ?FQ JLOB @LJMIBU >KA > DOB>Q AB>I JLOB
RPBCRI. 7BfII AVK>JF@>IIV @>I@RI>QB >K
L?GB@QfP >@@BIBO>QFLK >@@LOAFKD QL > ORIB
PQ>QBA FK !IDLOFQEJ #3 d H<9 C6>97H
5779@9F5H9G HCK5F8G H<9 ACIG9.
!KVQFJB TB T>KQ QL @>I@RI>QB > SB@QLO ?>PBA
LK > ORIB LO > CLOJRI>, TB KBBA QL @LJMRQB
QTL QEFKDP: 8,294?@/0 8,294?@/0 >KA /4=0.?4:9 /4=0.?4:9. ,BQfP PQ>OQ TFQE AFOB@QFLK. 7B HKLT QEB >@@BIBO>QFLK
SB@QLO PELRIA MLFKQ COLJ QEB L?GB@QfP IL@>QFLK QLT>OAP QEB JLRPB IL@>QFLK. ,BQfP P>V QEB L?GB@Q
FP IL@>QBA >Q QEB MLFKQ (R,S) >KA QEB JLRPB >Q (GIOM?5,GIOM?6).
)K &FDROB 1.15, TB PBB QE>Q TB @>K DBQ >
SB@QLO (>R,>S) ?V PR?QO>@QFKD QEB L?GB@QfP
IL@>QFLK COLJ QEB JLRPBfP IL@>QFLK.
i :N = CEKI;2 - N
i :O = CEKI;3 - O
,BQfP OBTOFQB QEB >?LSB RPFKD .3?=NIL
PVKQ>U. !PPRJFKD TB >OB FK QEB +IP?L @I>PP
>KA QERP E>SB >@@BPP QL QEB L?GB@QfP .3?=NIL FI=;NCIH, TB QEBK E>SB:
7B KLT E>SB > .3?=NIL QE>Q MLFKQP COLJ QEB JLSBOfP IL@>QFLK >II QEB T>V QL QEB JLRPB. )C QEB
L?GB@Q TBOB QL >@QR>IIV >@@BIBO>QB RPFKD QE>Q SB@QLO, FQ TLRIA >MMB>O FKPQ>KQ>KBLRPIV >Q QEB
JLRPB IL@>QFLK. 4EFP ALBP KLQ J>HB CLO DLLA >KFJ>QFLK, LC @LROPB, >KA TE>Q TB T>KQ QL AL
KLT FP AB@FAB ELT NRF@HIV QE>Q L?GB@Q PELRIA >@@BIBO>QB QLT>OA QEB JLRPB.
)K LOABO QL PBQ QEB J>DKFQRAB (TE>QBSBO FQ J>V ?B) LC LRO >@@BIBO>QFLK .3?=NIL, TB JRPQ CFOPQ
444 QE>Q AFOB@QFLK SB@QLO. 4E>QfP OFDEQ, VLR P>FA FQ. *CFA5@=N9. )C TB @>K PEOFKH QEB SB@QLO ALTK
QL FQP RKFQ SB@QLO (LC IBKDQE LKB) QEBK TB E>SB > SB@QLO QE>Q QBIIP RP QEB AFOB@QFLK >KA @>K B>PFIV
?B P@>IBA QL >KV S>IRB. /KB JRIQFMIFBA ?V >KVQEFKD BNR>IP >KVQEFKD.
F64B?2 1.14
F64B?2 1.15
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
Look! Were using the static reference to
sub() because we want a new PVector
pointing from one point to another.
.3?=NIL >CL = .3?=NIL.MO<(GIOM?,FI=;NCIH);
@FI;N ;HSNBCHA = ?????
>CL.HILG;FCT?();
>CL.GOFN(;HSNBCHA);
4EB .>QROB LC #LAB (S005)
57
4L PRJJ>OFWB, TB Q>HB QEB CLIILTFKD PQBMP:
1. #>I@RI>QB > SB@QLO QE>Q MLFKQP COLJ QEB L?GB@Q QL QEB Q>ODBQ IL@>QFLK (JLRPB)
2. .LOJ>IFWB QE>Q SB@QLO (OBAR@FKD FQP IBKDQE QL 1)
3. 3@>IB QE>Q SB@QLO QL >K >MMOLMOF>QB S>IRB (?V JRIQFMIVFKD FQ ?V PLJB S>IRB)
4. !PPFDK QE>Q SB@QLO QL >@@BIBO>QFLK
!KA EBOB >OB QELPB PQBMP FK QEB OJ>;N?() CRK@QFLK FQPBIC:
Example 1.10: Accelerating towards the mouse
9LR J>V ?B TLKABOFKD TEV QEB @FO@IB ALBPKfQ PQLM TEBK FQ OB>@EBP QEB Q>ODBQ. )QfP FJMLOQ>KQ
QL KLQB QE>Q QEB L?GB@Q JLSFKD E>P KL HKLTIBADB >?LRQ QOVFKD QL PQLM >Q > ABPQFK>QFLK; FQ
LKIV HKLTP TEBOB QEB ABPQFK>QFLK FP >KA QOFBP QL DL QEBOB >P NRF@HIV >P MLPPF?IB. 'LFKD >P
PIC> OJ>;N?() U
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
Step 1: Compute direction .3?=NIL >CL = .3?=NIL.MO<(GIOM?,FI=;NCIH);
Step 2: Normalize >CL.HILG;FCT?();
Step 3: Scale >CL.GOFN(0.5);
Step 4: Accelerate ;==?F?L;NCIH = >CL;
P?FI=CNS.;>>(;==?F?L;NCIH);
P?FI=CNS.FCGCN(NIJMJ??>);
FI=;NCIH.;>>(P?FI=CNS);
W
#E>MQBO 1. 6B@QLOP
58
NRF@HIV >P MLPPF?IB JB>KP FQ TFII FKBSFQ>?IV LSBOPELLQ QEB IL@>QFLK >KA E>SB QL QROK >OLRKA,
>D>FK DLFKD >P NRF@HIV >P MLPPF?IB QLT>OAP QEB ABPQFK>QFLK, LSBOPELLQFKD FQ >D>FK, >KA PL LK
>KA PL CLOQE. 3Q>V QRKBA; FK I>QBO @E>MQBOP TBfII IB>OK ELT QL MOLDO>J >K L?GB@Q QL ,==4A0 ,==4A0 >Q >
IL@>QFLK (PILT ALTK LK >MMOL>@E).
4EFP BU>JMIB FP OBJ>OH>?IV @ILPB QL QEB @LK@BMQ LC DO>SFQ>QFLK>I >QQO>@QFLK (FK TEF@E QEB L?GB@Q
FP >QQO>@QBA QL QEB JLRPB IL@>QFLK). 'O>SFQ>QFLK>I >QQO>@QFLK TFII ?B @LSBOBA FK JLOB ABQ>FI FK QEB
KBUQ @E>MQBO. (LTBSBO, LKB QEFKD JFPPFKD EBOB FP QE>Q QEB PQOBKDQE LC DO>SFQV (J>DKFQRAB LC
>@@BIBO>QFLK) FP FKSBOPBIV MOLMLOQFLK>I QL AFPQ>K@B. 4EFP JB>KP QE>Q QEB @ILPBO QEB L?GB@Q FP QL
QEB JLRPB, QEB C>PQBO FQ >@@BIBO>QBP.
,BQfP PBB TE>Q QEFP BU>JMIB TLRIA ILLH IFHB TFQE >K >OO>V LC JLSBOP (O>QEBO QE>K GRPQ LKB).
Example 1.11: Array of movers accelerating towards the mouse
4OV FJMIBJBKQFKD QEB >?LSB BU>JMIB TFQE > S>OF>?IB J>DKFQRAB LC >@@BIBO>QFLK,
PQOLKDBO TEBK FQ FP BFQEBO @ILPBO LO C>OQEBO >T>V.
Exercise 1.8 Exercise 1.8
An array of objects +IP?L89 GIP?LM = H?Q +IP?L8209;
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
<;=EALIOH>(255);
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
4EB .>QROB LC #LAB (S005)
59
Initialize each object in the array. GIP?LM8C9 = H?Q +IP?L();
W
W
PIC> >L;Q() U
<;=EALIOH>(255);
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
Calling functions on all the objects in the
array
GIP?LM8C9.OJ>;N?();
GIP?LM8C9.=B?=E#>A?M();
GIP?LM8C9.>CMJF;S();
W
W
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
@FI;N NIJMJ??>;
+IP?L() U
FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN));
P?FI=CNS = H?Q .3?=NIL(0,0);
NIJMJ??> = 4;
W
PIC> OJ>;N?() U
Our algorithm for calculating
acceleration:
Find the vector pointing towards the
mouse.
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
.3?=NIL >CL = .3?=NIL.MO<(GIOM?,FI=;NCIH);
Normalize. >CL.HILG;FCT?();
Scale. >CL.GOFN(0.5);
Set to acceleration. ;==?F?L;NCIH = >CL;
Motion 101! Velocity changes by
acceleration. Location changes by velocity.
P?FI=CNS.;>>(;==?F?L;NCIH);
P?FI=CNS.FCGCN(NIJMJ??>);
FI=;NCIH.;>>(P?FI=CNS);
W
#E>MQBO 1. 6B@QLOP
60
Display the Mover PIC> >CMJF;S() U
MNLIE?(0);
@CFF(175);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);
W
What to do at the edges PIC> =B?=E#>A?M() U
C@ (FI=;NCIH.R > QC>NB) U
FI=;NCIH.R = 0;
W ?FM? C@ (FI=;NCIH.R < 0) U
FI=;NCIH.R = QC>NB;
W
C@ (FI=;NCIH.S > B?CABN) U
FI=;NCIH.S = 0;
W ?FM? C@ (FI=;NCIH.S < 0) U
FI=;NCIH.S = B?CABN;
W
W
W
4EB .>QROB LC #LAB (S005)
61
The Ecosystem Project The Ecosystem Project
AG A9BH=CB98 =B H<9 DF9:579, CB9 K5M HC IG9 H<=G 6CC? =G HC 6I=@8 5 G=B;@9 DFC>97H
CJ9F H<9 7CIFG9 C: F958=B; =H, =B7CFDCF5H=B; 9@9A9BHG :FCA 957< 7<5DH9F CB9 GH9D
5H 5 H=A9. 29S@@ :C@@CK H<9 89J9@CDA9BH C: 5B 9L5AD@9 DFC>97H H<FCI;<CIH H<=G
6CC?R5 G=AI@5H=CB C: 5B 97CGMGH9A. %A5;=B9 5 DCDI@5H=CB C: 7CADIH5H=CB5@
7F95HIF9G GK=AA=B; 5FCIB8 5 8=;=H5@ DCB8, =BH9F57H=B; K=H< 957< CH<9F 577CF8=B;
HC J5F=CIG FI@9G.
Step 1 Exercise:
Develop a set of rules for simulating the real-world behavior of a creature, such as
a nervous fly, swimming fish, hopping bunny, slithering snake, etc. Can you
control the objects motion by only manipulating the acceleration? Try to give the
creature a personality through its behavior (rather than through its visual design).
F64B?2 1.16: *52 E0<@F@A2: '?<720A
#E>MQBO 1. 6B@QLOP
62
Chapter 2. Forces Chapter 2. Forces
ID<;KA B;12?2@A6:.A2 A52 F<?02.J
Darth Vader
)K QEB CFK>I BU>JMIB LC #E>MQBO 1, TB P>T ELT TB @LRIA @>I@RI>QB > AVK>JF@ >@@BIBO>QFLK
?>PBA LK > SB@QLO MLFKQFKD COLJ > @FO@IB LK QEB P@OBBK QL QEB JLRPB IL@>QFLK. 4EB OBPRIQFKD
JLQFLK OBPBJ?IBA > J>DKBQF@ >QQO>@QFLK ?BQTBBK @FO@IB >KA JLRPB, >P FC PLJB :CF79 TBOB
MRIIFKD QEB @FO@IB FK QLT>OAP QEB JLRPB. )K QEFP @E>MQBO TB TFII CLOJ>IFWB LRO RKABOPQ>KAFKD LC
QEB @LK@BMQ LC > CLO@B >KA FQP OBI>QFLKPEFM QL >@@BIBO>QFLK. /RO DL>I, ?V QEB BKA LC QEFP @E>MQBO,
FP QL RKABOPQ>KA ELT QL J>HB JRIQFMIB L?GB@QP JLSB >OLRKA QEB P@OBBK >KA OBPMLKA QL >
S>OFBQV LC BKSFOLKJBKQ>I CLO@BP.
2.1 Forces and Newtons Laws of Motion 2.1 Forces and Newtons Laws of Motion
"BCLOB TB ?BDFK BU>JFKFKD QEB MO>@QF@>I OB>IFQFBP LC PFJRI>QFKD CLO@BP FK @LAB, IBQfP Q>HB >
@LK@BMQR>I ILLH >Q TE>Q FQ JB>KP QL ?B > CLO@B FK QEB OB>I TLOIA. *RPQ IFHB QEB TLOA gSB@QLO,h
gCLO@Bh FP LCQBK RPBA QL JB>K > S>OFBQV LC QEFKDP. )Q @>K FKAF@>QB > MLTBOCRI FKQBKPFQV, >P FK g3EB
MRPEBA QEB ?LRIABO TFQE DOB>Q CLO@Bh LO g(B PMLHB CLO@BCRIIV.h 4EB ABCFKFQFLK LC 1:=.0 1:=.0 QE>Q TB
@>OB >?LRQ FP JR@E JLOB CLOJ>I >KA @LJBP COLJ )P>>@ .BTQLKfP I>TP LC JLQFLK:
! CLO@B FP > SB@QLO QE>Q @>RPBP >K L?GB@Q TFQE J>PP QL >@@BIBO>QB.
4EB .>QROB LC #LAB (S005)
63
4EB DLLA KBTP EBOB FP QE>Q TB OB@LDKFWB QEB CFOPQ M>OQ LC QEB ABCFKFQFLK: 5 :CF79 =G 5 J97HCF.
4E>KH DLLAKBPP TB GRPQ PMBKQ > TELIB @E>MQBO IB>OKFKD TE>Q > SB@QLO FP >KA ELT QL
MOLDO>J TFQE .3?=NILP!
,BQfP ILLH >Q .BTQLKfP QEOBB I>TP LC JLQFLK FK OBI>QFLK QL QEB @LK@BMQ LC > CLO@B.
Newtons First Law Newtons First Law
.BTQLKfP CFOPQ I>T FP @LJJLKIV PQ>QBA >P:
!K L?GB@Q >Q OBPQ PQ>VP >Q OBPQ >KA >K L?GB@Q FK JLQFLK PQ>VP FK JLQFLK.
(LTBSBO, QEFP FP JFPPFKD >K FJMLOQ>KQ BIBJBKQ OBI>QBA QL CLO@BP. 7B @LRIA BUM>KA FQ ?V
PQ>QFKD:
!K L?GB@Q >Q OBPQ PQ>VP >Q OBPQ >KA >K L?GB@Q FK JLQFLK PQ>VP FK JLQFLK >Q >
@LKPQ>KQ PMBBA >KA AFOB@QFLK RKIBPP >@QBA RMLK ?V >K RK?>I>K@BA CLO@B.
"V QEB QFJB .BTQLK @>JB >ILKD, QEB MOBS>FIFKD QEBLOV LC JLQFLKdCLOJRI>QBA ?V
!OFPQLQIBdT>P KB>OIV QTL QELRP>KA VB>OP LIA. )Q PQ>QBA QE>Q FC >K L?GB@Q FP JLSFKD, PLJB PLOQ
LC CLO@B FP OBNRFOBA QL HBBM FQ JLSFKD. 5KIBPP QE>Q JLSFKD QEFKD FP ?BFKD MRPEBA LO MRIIBA, FQ
TFII PFJMIV PILT ALTK LO PQLM. 2FDEQ?
4EFP, LC @LROPB, FP KLQ QORB. )K QEB >?PBK@B LC >KV CLO@BP, KL CLO@B FP OBNRFOBA QL HBBM >K
L?GB@Q JLSFKD. !K L?GB@Q (PR@E >P > ?>II) QLPPBA FK QEB B>OQEfP >QJLPMEBOB PILTP ALTK
?B@>RPB LC >FO OBPFPQ>K@B (> CLO@B). !K L?GB@QfP SBIL@FQV TFII LKIV OBJ>FK @LKPQ>KQ FK QEB
>?PBK@B LC >KV CLO@BP LO FC QEB CLO@BP QE>Q >@Q LK FQ @>K@BI B>@E LQEBO LRQ, F.B. QEB KBQ CLO@B
>AAP RM QL WBOL. 4EFP FP LCQBK OBCBOOBA QL >P 0<@474-=4@8 0<@474-=4@8. 4EB C>IIFKD ?>II TFII OB>@E > QBOJFK>I
SBIL@FQV (QE>Q PQ>VP @LKPQ>KQ) LK@B QEB CLO@B LC >FO OBPFPQ>K@B BNR>IP QEB CLO@B LC DO>SFQV.
F64B?2 2.1: *52 =2;1B9B: 1<2@;'A :<C2 /20.B@2 .99 A52 3<?02@ 0.;029 2.05 <A52? <BA (.11 B= A< . ;2A
3<?02 <3 G2?<).
#E>MQBO 2. &LO@BP
64
)K LRO 0OL@BPPFKD TLOIA, TB @LRIA OBPQ>QB .BTQLKfP CFOPQ I>T >P CLIILTP:
!K L?GB@QfP 06B@QLO SBIL@FQV TFII OBJ>FK @LKPQ>KQ FC FQ FP FK > PQ>QB LC
BNRFIF?OFRJ.
3HFMMFKD .BTQLKfP PB@LKA I>T (>ODR>?IV QEB JLPQ FJMLOQ>KQ I>T CLO LRO MROMLPBP) CLO >
JLJBKQ, IBQfP JLSB LK QL QEB QEFOA I>T.
Newtons Third Law Newtons Third Law
4EFP I>T FP LCQBK PQ>QBA >P:
&LO BSBOV >@QFLK QEBOB FP >K BNR>I >KA LMMLPFQB OB>@QFLK.
4EFP I>T COBNRBKQIV @>RPBP PLJB @LKCRPFLK FK QEB T>V QE>Q FQ FP PQ>QBA. &LO LKB, FQ PLRKAP IFHB
LKB CLO@B @>RPBP >KLQEBO. 9BP, FC VLR MRPE PLJBLKB, QE>Q PLJBLKB J>V 57H=J9@M AB@FAB QL
MRPE VLR ?>@H. "RQ QEFP FP KLQ QEB >@QFLK >KA OB>@QFLK TB >OB Q>IHFKD >?LRQ TFQE .BTQLKfP QEFOA
I>T.
,BQfP P>V VLR MRPE >D>FKPQ > T>II. 4EB T>II ALBPKfQ >@QFSBIV AB@FAB QL MRPE ?>@H LK VLR. 4EBOB
FP KL gLOFDFKh CLO@B. 9LRO MRPE PFJMIV FK@IRABP ?LQE CLO@BP, OBCBOOBA QL >P >K g>@QFLK/OB>@QFLK
M>FO.h
! ?BQQBO T>V LC PQ>QFKD QEB I>T JFDEQ ?B:
&LO@BP >IT>VP L@@RO FK M>FOP. 4EB QTL CLO@BP >OB LC BNR>I PQOBKDQE, ?RQ FK
LMMLPFQB AFOB@QFLKP.
.LT, QEFP PQFII @>RPBP @LKCRPFLK ?B@>RPB FQ PLRKAP IFHB QEBPB CLO@BP TLRIA >IT>VP @>K@BI B>@E
LQEBO LRQ. 4EFP FP KLQ QEB @>PB. 2BJBJ?BO, QEB CLO@BP >@Q LK AFCCBOBKQ L?GB@QP. !KA GRPQ
?B@>RPB QEB QTL CLO@BP >OB BNR>I, FQ ALBPKfQ JB>K QE>Q QEB JLSBJBKQP >OB BNR>I (LO QE>Q QEB
L?GB@QP TFII PQLM JLSFKD).
4OV MRPEFKD LK > PQ>QFLK>OV QOR@H. !IQELRDE QEB QOR@H FP C>O JLOB MLTBOCRI QE>K VLR, RKIFHB >
JLSFKD LKB, > PQ>QFLK>OV QOR@H TFII KBSBO LSBOMLTBO VLR >KA PBKA VLR CIVFKD ?>@HT>OAP. 4EB
CLO@B VLR BUBOQ LK FQ FP BNR>I >KA LMMLPFQB QL QEB CLO@B BUBOQBA LK VLRO E>KAP. 4EB LRQ@LJB
ABMBKAP LK > S>OFBQV LC LQEBO C>@QLOP. )C QEB QOR@H FP > PJ>II QOR@H LK >K F@V ALTKEFII, VLRfII
4EB .>QROB LC #LAB (S005)
65
MOL?>?IV ?B >?IB QL DBQ FQ QL JLSB. /K QEB LQEBO E>KA, FC FQfP > SBOV I>ODB QOR@H LK > AFOQ OL>A
>KA VLR MRPE E>OA BKLRDE (J>V?B BSBK Q>HB > ORKKFKD PQ>OQ), VLR @LRIA FKGROB VLRO E>KA.
!KA FC VLR >OB TB>OFKD OLIIBO PH>QBP TEBK VLR MRPE LK QE>Q QOR@H?
9LRfII >@@BIBO>QB >T>V COLJ QEB QOR@H, PIFAFKD >ILKD QEB OL>A TEFIB QEB QOR@H PQ>VP MRQ. 7EV
AL VLR PIFAB ?RQ KLQ QEB QOR@H? &LO LKB, QEB QOR@H E>P > JR@E I>ODBO J>PP (TEF@E TBfII DBQ
FKQL TFQE .BTQLKfP PB@LKA I>T). 4EBOB >OB LQEBO CLO@BP >Q TLOH QLL, K>JBIV QEB COF@QFLK LC
QEB QOR@HfP QFOBP >KA VLRO OLIIBO PH>QBP >D>FKPQ QEB OL>A.
F64B?2 2.2
Newtons Third Law (as seen through the eyes of Processing) Newtons Third Law (as seen through the eyes of Processing)
)C TB @>I@RI>QB > 06B@QLO C QE>Q FP > CLO@B LC L?GB@Q ! LK L?GB@Q ", TB JRPQ
>IPL >MMIV QEB CLO@Bd06B@QLO.JRIQ(C,-1);dQE>Q " BUBOQP LK L?GB@Q !.
7BfII PBB QE>Q FK QEB TLOIA LC 0OL@BPPFKD MOLDO>JJFKD, TB ALKfQ >IT>VP E>SB QL PQ>V QORB QL
QEB >?LSB. 3LJBQFJBP, PR@E >P FK QEB @>PB LC PBB DO>SFQ>QFLK>I >QQO>@QFLK ?BQTBBK ?LAFBP
(PBB M>DB 94), TBfII T>KQ QL JLABI BNR>I >KA LMMLPFQB CLO@BP. /QEBO QFJBP, PR@E >P TEBK
TBfOB PFJMIV P>VFKD, g(BV, QEBOBfP PLJB TFKA FK QEB BKSFOLKJBKQ,h TBfOB KLQ DLFKD QL ?LQEBO
QL JLABI QEB CLO@B QE>Q > ?LAV BUBOQP ?>@H LK QEB >FO. )K C>@Q, TBfOB KLQ JLABIFKD QEB >FO >Q
>II! 2BJBJ?BO, TB >OB PFJMIV Q>HFKD FKPMFO>QFLK COLJ QEB MEVPF@P LC QEB K>QRO>I TLOIA, KLQ
PFJRI>QFKD BSBOVQEFKD TFQE MBOCB@Q MOB@FPFLK.
#E>MQBO 2. &LO@BP
66
2.2 Forces and ProcessingNewtons Second Law 2.2 Forces and ProcessingNewtons Second Law
as a Function as a Function
!KA EBOB TB >OB >Q QEB JLPQ FJMLOQ>KQ I>T CLO QEB 0OL@BPPFKD MOLDO>JJBO.
Newtons Second Law Newtons Second Law
4EFP I>T FP PQ>QBA >P:
&LO@B BNR>IP J>PP QFJBP >@@BIBO>QFLK.
/O:
F
= M A
7EV FP QEFP QEB JLPQ FJMLOQ>KQ I>T CLO RP? 7BII, IBQfP TOFQB FQ > AFCCBOBKQ T>V.
A
= F
/ M
!@@BIBO>QFLK FP AFOB@QIV MOLMLOQFLK>I QL CLO@B >KA FKSBOPBIV MOLMLOQFLK>I QL J>PP. 4EFP JB>KP
QE>Q FC VLR DBQ MRPEBA, QEB E>OABO VLR >OB MRPEBA, QEB C>PQBO VLRfII JLSB (>@@BIBO>QB). 4EB
?FDDBO VLR >OB, QEB PILTBO VLRfII JLSB.
Weight vs. Mass Weight vs. Mass
i 4EB 8,>> 8,>> LC >K L?GB@Q FP > JB>PROB LC QEB >JLRKQ LC J>QQBO FK QEB L?GB@Q
(JB>PROBA FK HFILDO>JP).
i +0423? +0423?, QELRDE LCQBK JFPQ>HBK CLO J>PP, FP QB@EKF@>IIV QEB CLO@B LC DO>SFQV LK
>K L?GB@Q. &OLJ .BTQLKfP PB@LKA I>T, TB @>K @>I@RI>QB FQ >P J>PP QFJBP QEB
>@@BIBO>QFLK LC DO>SFQV (Q = G * A). 7BFDEQ FP JB>PROBA FK KBTQLKP.
i D09>4?D D09>4?D FP ABCFKBA >P QEB >JLRKQ LC J>PP MBO RKFQ LC SLIRJB (DO>JP MBO @R?F@
@BKQFJBQBO, CLO BU>JMIB).
.LQB QE>Q >K L?GB@Q QE>Q E>P > J>PP LC LKB HFILDO>J LK B>OQE TLRIA E>SB > J>PP LC LKB
HFILDO>J LK QEB JLLK. (LTBSBO, FQ TLRIA TBFDE LKIV LKB-PFUQE >P JR@E.
.LT, FK QEB TLOIA LC 0OL@BPPFKD, TE>Q FP J>PP >KVT>V? !OBKfQ TB AB>IFKD TFQE MFUBIP? 4L PQ>OQ
FK > PFJMIBO MI>@B, IBQfP P>V QE>Q FK LRO MOBQBKA MFUBI TLOIA, >II LC LRO L?GB@QP E>SB > J>PP
BNR>I QL 1. $/ 1 = $. !KA PL:
4EB .>QROB LC #LAB (S005)
67
A
= F
4EB >@@BIBO>QFLK LC >K L?GB@Q FP BNR>I QL CLO@B. 4EFP FP DOB>Q KBTP. !CQBO >II, TB P>T FK
#E>MQBO 1 QE>Q >@@BIBO>QFLK T>P QEB HBV QL @LKQOLIIFKD QEB JLSBJBKQ LC LRO L?GB@QP LK
P@OBBK. ,L@>QFLK FP >AGRPQBA ?V SBIL@FQV, >KA SBIL@FQV ?V >@@BIBO>QFLK. !@@BIBO>QFLK T>P
TEBOB FQ >II ?BD>K. .LT TB IB>OK QE>Q :CF79 FP QORIV TEBOB FQ >II ?BDFKP.
,BQfP Q>HB LRO +IP?L @I>PP, TFQE IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK.
.LT LRO DL>I FP QL ?B >?IB QL >AA CLO@BP QL QEFP L?GB@Q, MBOE>MP P>VFKD:
LO:
TEBOB TFKA >KA DO>SFQV >OB .3?=NILP. !@@LOAFKD QL .BTQLKfP PB@LKA I>T, TB @LRIA
FJMIBJBKQ QEFP CRK@QFLK >P CLIILTP.
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
W
GIP?L.;JJFS$IL=?(QCH>);
GIP?L.;JJFS$IL=?(AL;PCNS);
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
Newtons second law at its simplest. ;==?F?L;NCIH = @IL=?;
W
2.3 Force Accumulation 2.3 Force Accumulation
4EFP ILLHP MOBQQV DLLA. !CQBO >II, 5779@9F5H=CB = :CF79 FP > IFQBO>I QO>KPI>QFLK LC .BTQLKfP
PB@LKA I>T (TFQELRQ J>PP). .BSBOQEBIBPP, QEBOBfP > MOBQQV ?FD MOL?IBJ EBOB. ,BQfP OBQROK QL
TE>Q TB >OB QOVFKD QL >@@LJMIFPE: @OB>QFKD > JLSFKD L?GB@Q LK QEB P@OBBK QE>Q OBPMLKAP QL
TFKA >KA DO>SFQV.
/H, IBQfP 69 QEB @LJMRQBO CLO > JLJBKQ. &FOPQ, TB @>II ;JJFS$IL=?() TFQE TFKA. !KA PL QEB
+IP?L L?GB@QfP >@@BIBO>QFLK FP KLT >PPFDKBA QEB .3?=NIL QCH>. 3B@LKA, TB @>II
GIP?L.;JJFS$IL=?(QCH>);
GIP?L.;JJFS$IL=?(AL;PCNS);
GIP?L.OJ>;N?();
GIP?L.>CMJF;S();
#E>MQBO 2. &LO@BP
68
;JJFS$IL=?() TFQE DO>SFQV. .LT QEB +IP?L L?GB@QfP >@@BIBO>QFLK FP PBQ QL QEB DO>SFQV .3?=NIL.
4EFOA, TB @>II OJ>;N?(). 7E>Q E>MMBKP FK OJ>;N?()? !@@BIBO>QFLK FP >AABA QL SBIL@FQV.
7BfOB KLQ DLFKD QL PBB >KV BOOLO FK 0OL@BPPFKD, ?RQ WLFKHP! 7BfSB DLQ > J>GLO MOL?IBJ. 7E>Q
FP QEB S>IRB LC >@@BIBO>QFLK TEBK FQ FP >AABA QL SBIL@FQV? )Q FP BNR>I QL QEB DO>SFQV CLO@B. 7FKA
E>P ?BBK IBCQ LRQ! )C TB @>II ;JJFS$IL=?() JLOB QE>K LK@B, FQ LSBOOFABP B>@E MOBSFLRP @>II.
(LT >OB TB DLFKD QL E>KAIB JLOB QE>K LKB CLO@B?
4EB QORQE LC QEB J>QQBO EBOB FP QE>Q TB PQ>OQBA TFQE > PFJMIFCFBA PQ>QBJBKQ LC .BTQLKfP PB@LKA
I>T. (BOBfP > JLOB >@@RO>QB T>V QL MRQ FQ:
.BQ &LO@B BNR>IP J>PP QFJBP >@@BIBO>QFLK.
/O, >@@BIBO>QFLK FP BNR>I QL QEB GIA C: 5@@ :CF79G AFSFABA ?V J>PP. 4EFP J>HBP MBOCB@Q PBKPB.
!CQBO >II, >P TB P>T FK .BTQLKfP CFOPQ I>T, FC >II QEB CLO@BP >AA RM QL WBOL, >K L?GB@Q
BUMBOFBK@BP >K BNRFIF?OFRJ PQ>QB (F.B. KL >@@BIBO>QFLK). /RO FJMIBJBKQ>QFLK LC QEFP FP QEOLRDE >
MOL@BPP HKLTK >P 1:=.0 ,..@8@7,?4:9 1:=.0 ,..@8@7,?4:9. )QfP >@QR>IIV SBOV PFJMIB; >II TB KBBA QL AL FP >AA >II LC
QEB CLO@BP QLDBQEBO. !Q >KV DFSBK JLJBKQ, QEBOB JFDEQ ?B 1, 2, 6, 12, LO 303 CLO@BP. !P ILKD >P
LRO L?GB@Q HKLTP ELT QL >@@RJRI>QB QEBJ, FQ ALBPKfQ J>QQBO ELT J>KV CLO@BP >@Q LK FQ.
.LT, TBfOB KLQ CFKFPEBA GRPQ VBQ. &LO@B >@@RJRI>QFLK E>P LKB JLOB MFB@B. 3FK@B TBfOB >AAFKD
>II QEB CLO@BP QLDBQEBO >Q >KV DFSBK JLJBKQ, TB E>SB QL J>HB PROB QE>Q TB @IB>O >@@BIBO>QFLK
(F.B. PBQ FQ QL WBOL) ?BCLOB B>@E QFJB OJ>;N?() FP @>IIBA. ,BQfP QEFKH >?LRQ TFKA CLO > JLJBKQ.
3LJBQFJBP QEB TFKA FP SBOV PQOLKD, PLJBQFJBP FQfP TB>H, >KA PLJBQFJBP QEBOBfP KL TFKA >Q >II.
!Q >KV DFSBK JLJBKQ, QEBOB JFDEQ ?B > ERDB DRPQ LC TFKA, P>V, TEBK QEB RPBO ELIAP ALTK QEB
JLRPB.
7EBK QEB RPBO OBIB>PBP QEB JLRPB, QEB TFKA TFII PQLM, >KA >@@LOAFKD QL .BTQLKfP CFOPQ I>T, QEB
L?GB@Q TFII @LKQFKRB QL JLSB >Q > @LKPQ>KQ SBIL@FQV. (LTBSBO, FC TB E>A CLODLQQBK QL OBPBQ
>@@BIBO>QFLK QL WBOL, QEB DRPQ LC TFKA TLRIA PQFII ?B FK BCCB@Q. %SBK TLOPB, FQ TLRIA >AA LKQL
FQPBIC COLJ QEB MOBSFLRP CO>JB, PFK@B TB >OB >@@RJRI>QFKD CLO@BP! !@@BIBO>QFLK, FK LRO
PFJRI>QFLK, E>P KL JBJLOV; FQ FP PFJMIV @>I@RI>QBA ?>PBA LK QEB BKSFOLKJBKQ>I CLO@BP MOBPBKQ
P?FI=CNS.;>>(;==?F?L;NCIH);
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
Newtons second law, but with force
accumulation. We now add each force to
acceleration, one at a time.
;==?F?L;NCIH.;>>(@IL=?);
W
C@ (GIOM?.L?MM?>) U
.3?=NIL QCH> = H?Q .3?=NIL(0.5,0);
GIP?L.;JJFS$IL=?(QCH>);
W
4EB .>QROB LC #LAB (S005)
69
>Q > JLJBKQ FK QFJB. 4EFP FP AFCCBOBKQ QE>K, P>V, IL@>QFLK, TEF@E JRPQ OBJBJ?BO TEBOB QEB
L?GB@Q T>P FK QEB MOBSFLRP CO>JB FK LOABO QL JLSB MOLMBOIV QL QEB KBUQ.
4EB B>PFBPQ T>V QL FJMIBJBKQ @IB>OFKD QEB >@@BIBO>QFLK CLO B>@E CO>JB FP QL JRIQFMIV QEB
.3?=NIL ?V 0 >Q QEB BKA LC OJ>;N?().
PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
;==?F?L;NCIH.GOFN(0);
W
5PFKD CLO@BP, PFJRI>QB > EBIFRJ-CFIIBA ?>IILLK CIL>QFKD RMT>OA >KA ?LRK@FKD LCC QEB
QLM LC > TFKALT. #>K VLR >AA > TFKA CLO@B QE>Q @E>KDBP LSBO QFJB, MBOE>MP
>@@LOAFKD QL 0BOIFK KLFPB?
Exercise 2.1 Exercise 2.1
2.4 Dealing with Mass 2.4 Dealing with Mass
/+. 7BfSB DLQ LKB QFKV IFQQIB >AAFQFLK QL J>HB ?BCLOB TB >OB ALKB TFQE FKQBDO>QFKD CLO@BP
FKQL LRO +IP?L @I>PP >KA >OB OB>AV QL ILLH >Q BU>JMIBP. !CQBO >II, .BTQLKfP PB@LKA I>T FP
OB>IIV F
= M A
, KLQ A
= F
= M A
= Nv
)C FQfP ?BBK > TEFIB PFK@B VLRfSB ILLHBA >Q > CLOJRI> COLJ > J>QE LO MEVPF@P QBUQ?LLH,
QEBOB >OB QEOBB HBV MLFKQP QE>Q >OB FJMLOQ>KQ QL @LSBO ?BCLOB TB JLSB LK.
i EA,7@,?0 ?30 =423? >4/0, ,>>429 ?: ?30 701? >4/0. EA,7@,?0 ?30 =423? >4/0, ,>>429 ?: ?30 701? >4/0. 4EFP FP GRPQ IFHB FK @LAB! 7E>Q
TBfOB ALFKD EBOB FP BS>IR>QFKD QEB OFDEQ PFAB LC QEB BNR>QFLK >KA >PPFDKFKD FQ
QL QEB IBCQ. )K QEB @>PB >?LSB, TB T>KQ QL @>I@RI>QB QEB CLO@B LC COF@QFLKdQEB
IBCQ PFAB QBIIP RP TE>Q TB T>KQ QL @>I@RI>QB >KA QEB OFDEQ PFAB QBIIP RP ELT QL
AL FQ.
i A=0 B0 ?,76492 ,-:@? , A0.?:= := , >.,7,=? A=0 B0 ?,76492 ,-:@? , A0.?:= := , >.,7,=? )QfP FJMLOQ>KQ CLO RP QL OB>IFWB QE>Q
FK PLJB @>PBP, TBfII ?B ILLHFKD >Q > SB@QLO; FK LQEBOP, > P@>I>O. &LO BU>JMIB, FK
QEFP @>PB QEB CLO@B LC COF@QFLK FP > SB@QLO. 7B @>K PBB QE>Q ?V QEB >OOLT >?LSB
QEB TLOA gCOF@QFLK.h )Q E>P > J>DKFQRAB >KA AFOB@QFLK. 4EB OFDEQ PFAB LC QEB
BNR>QFLK >IPL E>P > SB@QLO, >P FKAF@>QBA ?V QEB PVJ?LI v
. 7B
T>KQ QL JRIQFMIV QEBJ QLDBQEBO >KA OB>A QEB CLOJRI> >P: Friction
= 1 * * N * v
, LO -1 QFJBP QEB SBIL@FQV RKFQ SB@QLO. )K 0OL@BPPFKD, QEFP TLRIA JB>K Q>HFKD QEB SBIL@FQV
SB@QLO, KLOJ>IFWFKD FQ, >KA JRIQFMIVFKD ?V -1.
.LQF@B QTL >AAFQFLK>I PQBMP EBOB. &FOPQ, FQfP FJMLOQ>KQ QL J>HB > @LMV LC QEB SBIL@FQV SB@QLO,
>P TB ALKfQ T>KQ QL OBSBOPB QEB L?GB@QfP AFOB@QFLK ?V >@@FABKQ. 3B@LKA, TB KLOJ>IFWB QEB
SB@QLO. 4EFP FP ?B@>RPB QEB J>DKFQRAB LC COF@QFLK FP KLQ >PPL@F>QBA TFQE ELT C>PQ FQ FP JLSFKD,
>KA TB T>KQ QL PQ>OQ TFQE > COF@QFLK SB@QLO LC J>DKFQRAB 1 PL QE>Q FQ @>K B>PFIV ?B P@>IBA.
F64B?2 2.3
.3?=NIL @LC=NCIH = P?FI=CNS.A?N();
@LC=NCIH.HILG;FCT?();
Lets figure out the direction of the friction
force (a unit vector in the opposite direction
of velocity).
@LC=NCIH.GOFN(-1);
#E>MQBO 2. &LO@BP
80
!@@LOAFKD QL QEB CLOJRI>, QEB J>DKFQRAB FP X * ,. X, QEB 'OBBH IBQQBO AI (MOLKLRK@BA gJBTh),
FP RPBA EBOB QL ABP@OF?B QEB .:0114.409? :1 1=4.?4:9 .:0114.409? :1 1=4.?4:9. 4EB @LBCCF@FBKQ LC COF@QFLK BPQ>?IFPEBP QEB
PQOBKDQE LC > COF@QFLK CLO@B CLO > M>OQF@RI>O PROC>@B. 4EB EFDEBO FQ FP, QEB PQOLKDBO QEB COF@QFLK; QEB
ILTBO, QEB TB>HBO. ! ?IL@H LC F@B, CLO BU>JMIB, TFII E>SB > JR@E ILTBO @LBCCF@FBKQ LC COF@QFLK
QE>K, P>V, P>KAM>MBO. 3FK@B TBfOB FK > MOBQBKA 0OL@BPPFKD TLOIA, TB @>K >O?FQO>OFIV PBQ QEB
@LBCCF@FBKQ ?>PBA LK ELT JR@E COF@QFLK TB T>KQ QL PFJRI>QB.
.LT CLO QEB PB@LKA M>OQ: ,. , OBCBOP QL QEB 9:=8,7 1:=.0 9:=8,7 1:=.0, QEB CLO@B MBOMBKAF@RI>O QL QEB
L?GB@QfP JLQFLK >ILKD > PROC>@B. 4EFKH LC > SBEF@IB AOFSFKD >ILKD > OL>A. 4EB SBEF@IB MRPEBP
ALTK >D>FKPQ QEB OL>A TFQE DO>SFQV, >KA .BTQLKfP QEFOA I>T QBIIP RP QE>Q QEB OL>A FK QROK
MRPEBP ?>@H >D>FKPQ QEB SBEF@IB. 4E>QfP QEB KLOJ>I CLO@B. 4EB DOB>QBO QEB DO>SFQ>QFLK>I CLO@B,
QEB DOB>QBO QEB KLOJ>I CLO@B. !P TBfII PBB FK QEB KBUQ PB@QFLK, DO>SFQV FP >PPL@F>QBA TFQE J>PP,
>KA PL > IFDEQTBFDEQ PMLOQP @>O TLRIA BUMBOFBK@B IBPP COF@QFLK QE>K > J>PPFSB QO>@QLO QO>FIBO
QOR@H. 7FQE QEB AF>DO>J >?LSB, ELTBSBO, TEBOB QEB L?GB@Q FP JLSFKD >ILKD > PROC>@B >Q >K
>KDIB, @LJMRQFKD QEB KLOJ>I CLO@B FP > ?FQ JLOB @LJMIF@>QBA ?B@>RPB FQ ALBPKfQ MLFKQ FK QEB
P>JB AFOB@QFLK >P DO>SFQV. 7BfII KBBA QL HKLT PLJBQEFKD >?LRQ >KDIBP >KA QOFDLKLJBQOV.
!II LC QEBPB PMB@FCF@P >OB FJMLOQ>KQ; ELTBSBO, FK 0OL@BPPFKD, > gDLLA BKLRDEh PFJRI>QFLK @>K
?B >@EFBSBA TFQELRQ QEBJ. 7B @>K, CLO BU>JMIB, J>HB COF@QFLK TLOH TFQE QEB >PPRJMQFLK QE>Q
QEB KLOJ>I CLO@B TFII >IT>VP E>SB > J>DKFQRAB LC 1. 7EBK TB DBQ FKQL QOFDLKLJBQOV FK QEB KBUQ
@E>MQBO, TBfII OBJBJ?BO QL OBQROK QL QEFP NRBPQFLK >KA J>HB LRO COF@QFLK BU>JMIB > ?FQ JLOB
PLMEFPQF@>QBA. 4EBOBCLOB:
.LT QE>Q TB E>SB ?LQE QEB J>DKFQRAB >KA AFOB@QFLK CLO COF@QFLK, TB @>K MRQ FQ >II QLDBQEBOj
j>KA >AA FQ QL LRO gCLO@BPh BU>JMIB, TEBOB J>KV L?GB@QP BUMBOFBK@B TFKA, DO>SFQV, >KA KLT
COF@QFLK:
@FI;N = = 0.01;
@FI;N HILG;F = 1;
@FI;N = = 0.01;
@FI;N HILG;F = 1;
Lets figure out the magnitude of friction
(really just an arbitrary constant).
@FI;N @LC=NCIH+;A = =*HILG;F;
.3?=NIL @LC=NCIH = P?FI=CNS.A?N();
@LC=NCIH.GOFN(-1);
@LC=NCIH.HILG;FCT?();
Take the unit vector and multiply it by
magnitude and we have our force vector!
@LC=NCIH.GOFN(@LC=NCIH+;A);
4EB .>QROB LC #LAB (S005)
81
Example 2.4: Including friction
2RKKFKD QEFP BU>JMIB, VLRfII KLQF@B QE>Q QEB @FO@IBP ALKfQ BSBK J>HB FQ QL QEB OFDEQ PFAB LC QEB
TFKALT. 3FK@B COF@QFLK @LKQFKRLRPIV MRPEBP >D>FKPQ QEB L?GB@Q FK QEB LMMLPFQB AFOB@QFLK LC FQP
JLSBJBKQ, QEB L?GB@Q @LKQFKRLRPIV PILTP ALTK. 4EFP @>K ?B > RPBCRI QB@EKFNRB LO >
MOL?IBJ ABMBKAFKD LK QEB DL>IP LC VLRO SFPR>IFW>QFLK.
%< 3?60A6<; ,6A5 3?60A6<;
PIC> >L;Q() U
<;=EALIOH>(255);
.3?=NIL QCH> = H?Q .3?=NIL(0.001,0);
We could scale by mass to be more
accurate.
.3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1);
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
<BE7J 9 = 0.01;
+0;9JEH <H?9J?ED = CEL;HI4?5.L;BE9?JO.=;J();
<H?9J?ED.CKBJ(-1);
<H?9J?ED.DEHC7B?P;();
<H?9J?ED.CKBJ(9);
Apply the friction force vector to the object. CEL;HI4?5.7FFBO"EH9;(<H?9J?ED);
GIP?LM8C9.;JJFS$IL=?(QCH>);
GIP?LM8C9.;JJFS$IL=?(AL;PCNS);
GIP?LM8C9.OJ>;N?();
GIP?LM8C9.>CMJF;S();
GIP?LM8C9.=B?=E#>A?M();
W
W
#E>MQBO 2. &LO@BP
82
#OB>QB ML@HBQP LC COF@QFLK FK > 0OL@BPPFKD PHBQ@E PL QE>Q L?GB@QP LKIV BUMBOFBK@B COF@QFLK
TEBK @OLPPFKD LSBO QELPB ML@HBQP. 7E>Q FC VLR S>OV QEB PQOBKDQE (COF@QFLK @LBCCF@FBKQ) LC
B>@E >OB>? 7E>Q FC VLR J>HB PLJB ML@HBQP CB>QROB QEB LMMLPFQB LC COF@QFLKdF.B., TEBK
VLR BKQBO > DFSBK ML@HBQ VLR >@QR>IIV PMBBA RM FKPQB>A LC PILTFKD ALTK?
Exercise 2.4 Exercise 2.4
2.8 Air and Fluid Resistance 2.8 Air and Fluid Resistance
&OF@QFLK >IPL L@@ROP TEBK > ?LAV M>PPBP QEOLRDE > IFNRFA LO D>P. 4EFP CLO@B E>P J>KV AFCCBOBKQ
K>JBP, >II OB>IIV JB>KFKD QEB P>JB QEFKD: J=G7CIG :CF79, 8F5; :CF79, :@I=8 F9G=GH5B79. 7EFIB QEB
OBPRIQ FP RIQFJ>QBIV QEB P>JB >P LRO MOBSFLRP COF@QFLK BU>JMIBP (QEB L?GB@Q PILTP ALTK), QEB
T>V FK TEF@E TB @>I@RI>QB > AO>D CLO@B TFII ?B PIFDEQIV AFCCBOBKQ. ,BQfP ILLH >Q QEB CLOJRI>:
F
d
=
1
2
v
2
AC
d
v
.LT IBQfP ?OB>H QEFP ALTK >KA PBB TE>Q TB OB>IIV KBBA CLO >K BCCB@QFSB PFJRI>QFLK FK
0OL@BPPFKD, J>HFKD LROPBISBP > JR@E PFJMIBO CLOJRI> FK QEB MOL@BPP.
i
F
d OBCBOP QL 8F5; :CF79, QEB SB@QLO TB RIQFJ>QBIV T>KQ QL @LJMRQB >KA M>PP FKQL LRO
;JJFS$IL=?() CRK@QFLK.
i - 1/2 FP > @LKPQ>KQ: -0.5. 4EFP FP C>FOIV FOOBIBS>KQ FK QBOJP LC LRO 0OL@BPPFKD TLOIA, >P
TB TFII ?B J>HFKD RM S>IRBP CLO LQEBO @LKPQ>KQP >KVT>V. (LTBSBO, QEB C>@Q QE>Q FQ FP
KBD>QFSB FP FJMLOQ>KQ, >P FQ QBIIP RP QE>Q QEB CLO@B FP FK QEB LMMLPFQB AFOB@QFLK LC
SBIL@FQV (GRPQ >P TFQE COF@QFLK).
F64B?2 2.4
4EB .>QROB LC #LAB (S005)
83
i FP QEB 'OBBH IBQQBO F<C, >KA OBCBOP QL QEB ABKPFQV LC QEB IFNRFA, PLJBQEFKD TB
ALKfQ KBBA QL TLOOV >?LRQ. 7B @>K PFJMIFCV QEB MOL?IBJ >KA @LKPFABO QEFP QL E>SB
> @LKPQ>KQ S>IRB LC 1.
i v OBCBOP QL QEB PMBBA LC QEB L?GB@Q JLSFKD. /+, TBfSB DLQ QEFP LKB! 4EB L?GB@QfP
PMBBA FP QEB J>DKFQRAB LC QEB SBIL@FQV SB@QLO: P?FI=CNS.G;AHCNO>?(). !KA v
2
GRPQ
JB>KP v PNR>OBA LO v * v.
i A OBCBOP QL QEB COLKQ>I >OB> LC QEB L?GB@Q QE>Q FP MRPEFKD QEOLRDE QEB IFNRFA (LO D>P).
!K >BOLAVK>JF@ ,>J?LODEFKF, CLO BU>JMIB, TFII BUMBOFBK@B IBPP >FO OBPFPQ>K@B QE>K
> ?LUV 6LISL. .BSBOQEBIBPP, CLO > ?>PF@ PFJRI>QFLK, TB @>K @LKPFABO LRO L?GB@Q QL
?B PMEBOF@>I >KA FDKLOB QEFP BIBJBKQ.
i
C
d FP QEB @LBCCF@FBKQ LC AO>D, BU>@QIV QEB P>JB >P QEB @LBCCF@FBKQ LC COF@QFLK ( ). 4EFP
FP > @LKPQ>KQ TBfII ABQBOJFKB ?>PBA LK TEBQEBO TB T>KQ QEB AO>D CLO@B QL ?B
PQOLKD LO TB>H.
i v
,LLH C>JFIF>O? )Q PELRIA. 4EFP OBCBOP QL QEB SBIL@FQV RKFQ SB@QLO, F.B.
P?FI=CNS.HILG;FCT?(). *RPQ IFHB TFQE COF@QFLK, AO>D FP > CLO@B QE>Q MLFKQP FK QEB
LMMLPFQB AFOB@QFLK LC SBIL@FQV.
.LT QE>Q TBfSB >K>IVWBA B>@E LC QEBPB @LJMLKBKQP >KA ABQBOJFKBA TE>Q TB KBBA CLO >
PFJMIB PFJRI>QFLK, TB @>K OBAR@B LRO CLOJRI> QL:
LO:
,BQfP FJMIBJBKQ QEFP CLO@B FK LRO +IP?L @I>PP BU>JMIB TFQE LKB >AAFQFLK. 7EBK TB TOLQB LRO
COF@QFLK BU>JMIB, QEB CLO@B LC COF@QFLK T>P >IT>VP MOBPBKQ. 7EBKBSBO >K L?GB@Q T>P JLSFKD,
COF@QFLK TLRIA PILT FQ ALTK. (BOB, IBQfP FKQOLAR@B >K BIBJBKQ QL QEB BKSFOLKJBKQd> gIFNRFAh
QE>Q QEB +IP?L L?GB@QP M>PP QEOLRDE. 4EB *CKOC> L?GB@Q TFII ?B > OB@Q>KDIB >KA TFII HKLT
F64B?2 2.5: &B? @6:=963621 1?.4 3<?02 3<?:B9.
@FI;N = = 0.1;
@FI;N MJ??> = P.G;A();
Part 1 of our formula (magnitude): Cd * v2 @FI;N >L;A+;AHCNO>? = = * MJ??> * MJ??>;
.3?=NIL >L;A = P?FI=CNS.A?N();
Part 2 of our formula (direction): -1 *
velocity
>L;A.GOFN(-1);
>L;A.HILG;FCT?();
Magnitude and direction together! >L;A.GOFN(>L;A+;AHCNO>?);
#E>MQBO 2. &LO@BP
84
>?LRQ FQP IL@>QFLK, TFAQE, EBFDEQ, >KA g@LBCCF@FBKQ LC AO>DhdF.B., FP FQ B>PV CLO L?GB@QP QL JLSB
QEOLRDE FQ (IFHB >FO) LO AFCCF@RIQ (IFHB JLI>PPBP)? )K >AAFQFLK, FQ PELRIA FK@IRAB > CRK@QFLK QL AO>T
FQPBIC LK QEB P@OBBK (>KA QTL JLOB CRK@QFLKP, TEF@E TBfII PBB FK > JLJBKQ).
4EB J>FK MOLDO>J TFII KLT FK@IRAB > *CKOC> L?GB@Q OBCBOBK@B >P TBII >P > IFKB LC @LAB QE>Q
FKFQF>IFWBP QE>Q L?GB@Q.
.LT @LJBP >K FKQBOBPQFKD NRBPQFLK: ELT AL TB DBQ QEB +IP?L L?GB@Q QL Q>IH QL QEB *CKOC>
L?GB@Q? )K LQEBO TLOAP, TB T>KQ QL BUB@RQB QEB CLIILTFKD:
2<9B 5 ACJ9F D5GG9G H<FCI;< 5 @=EI=8 =H 9LD9F=9B79G 5 8F5; :CF79.
jLO FK L?GB@Q-LOFBKQBA PMB>H (>PPRJFKD TB >OB ILLMFKD QEOLRDE >K >OO>V LC +IP?L L?GB@QP TFQE
FKABU F):
=F;MM *CKOC> U
The liquid object includes a variable defining
its coefficient of drag.
@FI;N R,S,Q,B;
@FI;N =;
*CKOC>(@FI;N R:, @FI;N S:, @FI;N Q:, @FI;N B:, @FI;N =:) U
R = R:;
S = S:;
Q = Q:;
B = B:;
= = =:;
W
PIC> >CMJF;S() U
HI0NLIE?();
@CFF(175);
L?=N(R,S,Q,B);
W
W
*CKOC> FCKOC>;
PIC> M?NOJ() U
FCKOC> = H?Q *CKOC>(0, B?CABN/2, QC>NB, B?CABN/2, 0.1);
W
Initialize a Liquid object. Note the coefficient
is low (0.1), otherwise the object would
come to a halt fairly quickly (which may
someday be the effect you want).
C@ (GIP?LM8C9.CM'HMC>?(FCKOC>)) U
If a Mover is inside a Liquid, apply the drag
force.
GIP?LM8C9.>L;A(FCKOC>);
W
4EB .>QROB LC #LAB (S005)
85
4EB >?LSB @LAB QBIIP RP QE>Q TB KBBA QL >AA QTL CRK@QFLKP QL QEB +IP?L @I>PP: (1) > CRK@QFLK
QE>Q ABQBOJFKBP FC > +IP?L L?GB@Q FP FKPFAB QEB *CKOC> L?GB@Q, >KA (2) > CRK@QFLK QE>Q
@LJMRQBP >KA >MMIFBP > AO>D CLO@B LK QEB +IP?L L?GB@Q.
4EB CFOPQ FP B>PV; TB @>K PFJMIV RPB > @LKAFQFLK>I PQ>QBJBKQ QL ABQBOJFKB FC QEB IL@>QFLK
SB@QLO OBPQP FKPFAB QEB OB@Q>KDIB ABCFKBA ?V QEB IFNRFA.
4EB >L;A() CRK@QFLK FP > ?FQ JLOB @LJMIF@>QBA; ELTBSBO, TBfSB TOFQQBK QEB @LAB CLO FQ
>IOB>AV. 4EFP FP PFJMIV >K FJMIBJBKQ>QFLK LC LRO CLOJRI>. 4EB AO>D CLO@B FP BNR>I QL H<9
7C9::=7=9BH C: 8F5; AI@H=D@=98 6M H<9 GD998 C: H<9 +IP?L GEI5F98 =B H<9 CDDCG=H9 8=F97H=CB C:
J9@C7=HM!
!KA TFQE QEBPB QTL CRK@QFLKP >AABA QL QEB +IP?L @I>PP, TBfOB OB>AV QL MRQ FQ >II QLDBQEBO FK
QEB J>FK Q>?:
<IIF?;H CM'HMC>?(*CKOC> F) U
C@ (FI=;NCIH.R>F.R && FI=;NCIH.R<F.R+F.Q && FI=;NCIH.S>F.S && FI=;NCIH.S<F.S+F.B)
U
L?NOLH NLO?;
W ?FM? U
L?NOLH @;FM?;
W
W
This conditional statement determines if
the PVector location is inside the rectangle
defined by the Liquid class.
PIC> >L;A(*CKOC> F) U
@FI;N MJ??> = P?FI=CNS.G;A();
The forces magnitude: Cd * v~2~ @FI;N >L;A+;AHCNO>? = F.= * MJ??> * MJ??>;
.3?=NIL >L;A = P?FI=CNS.A?N();
>L;A.GOFN(-1);
The force's direction: -1 * velocity >L;A.HILG;FCT?();
Finalize the force: magnitude and direction
together.
>L;A.GOFN(>L;A+;AHCNO>?);
Apply the force. ;JJFS$IL=?(>L;A);
W
#E>MQBO 2. &LO@BP
86
Example 2.5: Fluid Resistance
2RKKFKD QEB BU>JMIB, VLR PELRIA KLQF@B QE>Q TB >OB PFJRI>QFKD ?>IIP C>IIFKD FKQL T>QBO. 4EB
L?GB@QP LKIV PILT ALTK TEBK @OLPPFKD QEOLRDE QEB DO>V >OB> >Q QEB ?LQQLJ LC QEB TFKALT
(OBMOBPBKQFKD QEB IFNRFA). 9LRfII >IPL KLQF@B QE>Q QEB PJ>IIBO L?GB@QP PILT ALTK > DOB>Q AB>I
+IP?L89 GIP?LM = H?Q +IP?L81009;
*CKOC> FCKOC>;
PIC> M?NOJ() U
MCT?(360, 640);
MGIINB();
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
GIP?LM8C9 = H?Q +IP?L(L;H>IG(0.1,5),0,0);
W
FCKOC> = H?Q *CKOC>(0, B?CABN/2, QC>NB, B?CABN/2, 0.1);
W
PIC> >L;Q() U
<;=EALIOH>(255);
FCKOC>.>CMJF;S();
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
C@ (GIP?LM8C9.CM'HMC>?(FCKOC>)) U
GIP?LM8C9.>L;A(FCKOC>);
W
@FI;N G = 0.1*GIP?LM8C9.G;MM;
Note that we are scaling gravity according to
mass.
.3?=NIL AL;PCNS = H?Q .3?=NIL(0, G);
GIP?LM8C9.;JJFS$IL=?(AL;PCNS);
GIP?LM8C9.OJ>;N?();
GIP?LM8C9.>CMJF;S();
GIP?LM8C9.=B?=E#>A?M();
W
W
4EB .>QROB LC #LAB (S005)
87
JLOB QE>K QEB I>ODBO L?GB@QP. 2BJBJ?BO .BTQLKfP PB@LKA I>T? A = $ / +. !@@BIBO>QFLK
BNR>IP CLO@B 8=J=898 ?V J>PP. ! J>PPFSB L?GB@Q TFII >@@BIBO>QB IBPP. ! PJ>IIBO L?GB@Q TFII
>@@BIBO>QB JLOB. )K QEFP @>PB, QEB >@@BIBO>QFLK TBfOB Q>IHFKD >?LRQ FP QEB gPILTFKD ALTKh
ARB QL AO>D. 4EB PJ>IIBO L?GB@QP TFII PILT ALTK >Q > DOB>QBO O>QB QE>K QEB I>ODBO LKBP.
4>HB > ILLH >Q LRO CLOJRI> CLO AO>D >D>FK: /=,2 1:=.0 = .:0114.409? * >;00/ * >;00/ /=,2 1:=.0 = .:0114.409? * >;00/ * >;00/.
4EB C>PQBO >K L?GB@Q JLSBP, QEB DOB>QBO QEB AO>D CLO@B >D>FKPQ FQ. )K C>@Q, >K L?GB@Q KLQ
JLSFKD FK T>QBO BUMBOFBK@BP KL AO>D >Q >II. %UM>KA QEB BU>JMIB QL AOLM QEB ?>IIP
COLJ AFCCBOBKQ EBFDEQP. (LT ALBP QEFP >CCB@Q QEB AO>D >P QEBV EFQ QEB T>QBO?
Exercise 2.5 Exercise 2.5
4EB CLOJRI> CLO AO>D >IPL FK@IRABA PROC>@B >OB>. #>K VLR @OB>QB > PFJRI>QFLK LC ?LUBP
C>IIFKD FKQL T>QBO TFQE > AO>D CLO@B ABMBKABKQ LK QEB IBKDQE LC QEB PFAB EFQQFKD QEB
T>QBO?
Exercise 2.6 Exercise 2.6
&IRFA OBPFPQ>K@B ALBP KLQ LKIV TLOH LMMLPFQB QL QEB SBIL@FQV SB@QLO, ?RQ >IPL
MBOMBKAF@RI>O QL FQ. 4EFP FP HKLTK >P gIFCQ-FKAR@BA AO>Dh >KA TFII @>RPB >K >FOMI>KB
TFQE >K >KDIBA TFKD QL OFPB FK >IQFQRAB. 4OV @OB>QFKD > PFJRI>QFLK LC IFCQ.
Exercise 2.7 Exercise 2.7
2.9 Gravitational Attraction 2.9 Gravitational Attraction
0OL?>?IV QEB JLPQ C>JLRP CLO@B LC >II FP
DO>SFQV. 7B ERJ>KP LK B>OQE QEFKH LC
DO>SFQV >P >K >MMIB EFQQFKD )P>>@ .BTQLK LK
QEB EB>A. 'O>SFQV JB>KP QE>Q PQRCC C>IIP
ALTK. "RQ QEFP FP LKIV CIF BUMBOFBK@B LC
DO>SFQV. )K QORQE, GRPQ >P QEB B>OQE MRIIP QEB
>MMIB QLT>OAP FQ ARB QL > DO>SFQ>QFLK>I
CLO@B, QEB >MMIB MRIIP QEB B>OQE >P TBII. 4EB
QEFKD FP, QEB B>OQE FP GRPQ PL COB>HFKD ?FD
QE>Q FQ LSBOTEBIJP >II QEB LQEBO DO>SFQV
FKQBO>@QFLKP. %SBOV L?GB@Q TFQE J>PP BUBOQP
> DO>SFQ>QFLK>I CLO@B LK BSBOV LQEBO L?GB@Q.
F64B?2 2.6
#E>MQBO 2. &LO@BP
88
!KA QEBOB FP > CLOJRI> CLO @>I@RI>QFKD QEB PQOBKDQEP LC QEBPB CLO@BP, >P ABMF@QBA FK &FDROB 2.6.
,BQfP BU>JFKB QEFP CLOJRI> > ?FQ JLOB @ILPBIV.
i $ OBCBOP QL QEB DO>SFQ>QFLK>I CLO@B, QEB SB@QLO TB RIQFJ>QBIV T>KQ QL @LJMRQB >KA M>PP
FKQL LRO ;JJFS$IL=?() CRK@QFLK.
i % FP QEB IB=J9FG5@ ;F5J=H5H=CB5@ 7CBGH5BH, TEF@E FK LRO TLOIA BNR>IP 6.67428 U 10
-11
JBQBOP @R?BA MBO HFILDO>J MBO PB@LKA PNR>OBA. 4EFP FP > MOBQQV FJMLOQ>KQ KRJ?BO FC
VLRO K>JB FP )P>>@ .BTQLK LO !I?BOQ %FKPQBFK. )QfP KLQ >K FJMLOQ>KQ KRJ?BO FC VLR >OB
> 0OL@BPPFKD MOLDO>JJBO. !D>FK, FQfP > @LKPQ>KQ QE>Q TB @>K RPB QL J>HB QEB CLO@BP
FK LRO TLOIA TB>HBO LO PQOLKDBO. *RPQ J>HFKD FQ BNR>I QL LKB >KA FDKLOFKD FQ FPKfQ PR@E
> QBOOF?IB @ELF@B BFQEBO.
i G
1
>KA G
2
>OB QEB J>PPBP LC L?GB@QP 1 >KA 2. !P TB P>T TFQE .BTQLKfP PB@LKA I>T (
F
= M A
), J>PP FP >IPL PLJBQEFKD TB @LRIA @ELLPB QL FDKLOB. !CQBO >II, PE>MBP AO>TK
LK QEB P@OBBK ALKfQ >@QR>IIV E>SB > MEVPF@>I J>PP. (LTBSBO, FC TB HBBM QEBPB
S>IRBP, TB @>K @OB>QB JLOB FKQBOBPQFKD PFJRI>QFLKP FK TEF@E g?FDDBOh L?GB@QP BUBOQ >
PQOLKDBO DO>SFQ>QFLK>I CLO@B QE>K PJ>IIBO LKBP.
i r
OBCBOP QL QEB RKFQ SB@QLO MLFKQFKD COLJ L?GB@Q 1 QL L?GB@Q 2. !P TBfII PBB FK >
JLJBKQ, TB @>K @LJMRQB QEFP AFOB@QFLK SB@QLO ?V PR?QO>@QFKD QEB IL@>QFLK LC LKB
L?GB@Q COLJ QEB LQEBO.
i L
2
OBCBOP QL QEB AFPQ>K@B ?BQTBBK QEB QTL L?GB@QP PNR>OBA. ,BQfP Q>HB > JLJBKQ QL
QEFKH >?LRQ QEFP > ?FQ JLOB. 7FQE BSBOVQEFKD LK QEB QLM LC QEB CLOJRI>d%, G
1
, G
2
dQEB
?FDDBO FQP S>IRB, QEB PQOLKDBO QEB CLO@B. "FD J>PP, ?FD CLO@B. "FD %, ?FD CLO@B. .LT,
TEBK TB AFSFAB ?V PLJBQEFKD, TB E>SB QEB LMMLPFQB. 4EB PQOBKDQE LC QEB CLO@B FP
FKSBOPBIV MOLMLOQFLK>I QL QEB AFPQ>K@B PNR>OBA. 4EB :5FH<9F 5K5M >K L?GB@Q FP, QEB
K95?9F QEB CLO@B; QEB 7@CG9F, QEB GHFCB;9F.
(LMBCRIIV ?V KLT QEB CLOJRI> J>HBP PLJB PBKPB QL RP. 7BfSB ILLHBA >Q > AF>DO>J >KA
AFPPB@QBA QEB FKAFSFAR>I @LJMLKBKQP LC QEB CLOJRI>. .LT FQfP QFJB QL CFDROB LRQ ELT TB
QO>KPI>QB QEB J>QE FKQL 0OL@BPPFKD @LAB. ,BQfP J>HB QEB CLIILTFKD >PPRJMQFLKP.
7B E>SB QTL L?GB@QP, >KA:
1. %>@E L?GB@Q E>P > IL@>QFLK: .3?=NIL FI=;NCIH1 >KA .3?=NIL FI=;NCIH2.
2. %>@E L?GB@Q E>P > J>PP: @FI;N G;MM1 >KA @FI;N G;MM2.
3. 4EBOB FP > S>OF>?IB @FI;N % CLO QEB RKFSBOP>I DO>SFQ>QFLK>I @LKPQ>KQ.
'FSBK QEBPB >PPRJMQFLKP, TB T>KQ QL @LJMRQB .3?=NIL @IL=?, QEB CLO@B LC DO>SFQV. 7BfII AL FQ
FK QTL M>OQP. &FOPQ, TBfII @LJMRQB QEB AFOB@QFLK LC QEB CLO@B r
>KA B
:
A
= (
a
x
, a
y
)
B
= (
b
x
, b
y
)
4(% $/4 02/$5#4: A
=
a
x
b
x
+ a
y
b
y
&LO BU>JMIB, FC TB E>SB QEB CLIILTFKD QTL SB@QLOP:
A
= (3, 5)
B
= (10, 1)
A
= 3 * 10 + 5 * 1 = 30 + 5 = 35
.LQF@B QE>Q QEB OBPRIQ LC QEB ALQ MOLAR@Q FP > P@>I>O S>IRB (> PFKDIB KRJ?BO) >KA KLQ > SB@QLO.
)K 0OL@BPPFKD, QEFP TLRIA QO>KPI>QB QL:
F64B?2 6.18
4EB .>QROB LC #LAB (S005)
283
!KA FC TB TBOB QL ILLH FK QEB DRQP LC QEB .3?=NIL PLRO@B, TBfA CFKA > MOBQQV PFJMIB
FJMIBJBKQ>QFLK LC QEFP CRK@QFLK:
4EFP FP PFJMIB BKLRDE, ?RQ TEV AL TB KBBA QEB ALQ MOLAR@Q, >KA TEBK FP FQ DLFKD QL ?B
RPBCRI CLO RP FK @LAB?
/KB LC QEB JLOB @LJJLK RPBP LC QEB ALQ MOLAR@Q FP QL CFKA QEB >KDIB ?BQTBBK QTL SB@QLOP.
!KLQEBO T>V FK TEF@E QEB ALQ MOLAR@Q @>K ?B BUMOBPPBA FP:
A
= A
cos()
)K LQEBO TLOAP, ! ALQ " FP BNR>I QL QEB J>DKFQRAB LC ! QFJBP J>DKFQRAB LC " QFJBP @LPFKB LC
QEBQ> (TFQE QEBQ> ABCFKBA >P H<9 5B;@9 69HK99B H<9 HKC J97HCFG A 5B8 B).
4EB QTL CLOJRI>P CLO ALQ MOLAR@Q @>K ?B ABOFSBA COLJ LKB >KLQEBO TFQE QOFDLKLJBQOV
(EQQM://J>QETLOIA.TLICO>J.@LJ/$LQ0OLAR@Q.EQJI), ?RQ CLO LRO MROMLPBP TB @>K ?B E>MMV TFQE
LMBO>QFKD LK QEB >PPRJMQFLK QE>Q:
A
= A
cos()
A
=
a
x
b
x
+ a
y
b
y
?LQE ELIA QORB >KA QEBOBCLOB:
a
x
b
x
+ a
y
b
y
= A
cos()
.LT, IBQfP PQ>OQ TFQE QEB CLIILTFKD MOL?IBJ.
7B E>SB QEB SB@QLOP ! >KA ":
A
= (10, 2)
B
= (4, 3)
7B KLT E>SB > PFQR>QFLK FK TEF@E TB HKLT
BSBOVQEFKD BU@BMQ CLO QEBQ>. 7B HKLT QEB
@LJMLKBKQP LC QEB SB@QLO >KA @>K
@>I@RI>QB QEB J>DKFQRAB LC B>@E SB@QLO. 7B
@>K QEBOBCLOB PLISB CLO @LPFKB LC QEBQ>:
cos() = ( A
) / ( A
)
.3?=NIL ; = H?Q .3?=NIL(-3,5);
.3?=NIL < = H?Q .3?=NIL(10,1);
The PVector class includes a function to
calculate the dot product.
@FI;N H = ;.>IN(<);
JO<FC= @FI;N >IN(.3?=NIL P) U
L?NOLH R*P.R + S*P.S + T*P.T;
W
F64B?2 6.19
#E>MQBO 6. !RQLKLJLRP !DBKQP
284
4L PLISB CLO QEBQ>, TB @>K Q>HB QEB FKSBOPB @LPFKB (LCQBK BUMOBPPBA >P 7CG=B9
-1
LO 5F77CG=B9).
= cos
1
( ( A
) / ( A
) )
,BQfP KLT AL QEB J>QE TFQE >@QR>I KRJ?BOP:
A
= 10.2
B
= 5
4EBOBCLOB:
= cos
1
( ( 10 4 + 2 -3 ) / ( 10.2 5 ) )
= cos
1
( 34 / 51 )
= 48
>KA B
)
FP BNR>I QL 0.
2. )C QTL SB@QLOP >OB RKFQ SB@QLOP, QEBK QEB ALQ MOLAR@Q FP PFJMIV BNR>I QL @LPFKB LC
QEB >KDIB ?BQTBBK QEBJ, F.B. A
= cos() FC A
>KA B
>OB LC IBKDQE 1.
6.8 Path Following 6.8 Path Following
.LT QE>Q TBfSB DLQ > ?>PF@ RKABOPQ>KAFKD LC QEB ALQ MOLAR@Q RKABO LRO ?BIQ, TB @>K OBQROK
QL > AFP@RPPFLK LC #O>FD 2BVKLIAPfP M>QE-CLIILTFKD >IDLOFQEJ. ,BQfP NRF@HIV @I>OFCV PLJBQEFKD.
7B >OB Q>IHFKD >?LRQ M>QE :C@@CK=B;, KLQ M>QE :=B8=B;. 0>QECFKAFKD OBCBOP QL > OBPB>O@E QLMF@
(@LJJLKIV PQRAFBA FK >OQFCF@F>I FKQBIIFDBK@B) QE>Q FKSLISBP PLISFKD CLO QEB PELOQBPQ AFPQ>K@B
?BQTBBK QTL MLFKQP, LCQBK FK > J>WB. 7FQE ;,?3 1:77:B492 ;,?3 1:77:B492, QEB M>QE >IOB>AV BUFPQP >KA TBfOB
>PHFKD > SBEF@IB QL CLIILT QE>Q M>QE.
"BCLOB TB TLOH LRQ QEB FKAFSFAR>I MFB@BP, IBQfP Q>HB > ILLH >Q QEB LSBO>II >IDLOFQEJ CLO M>QE
CLIILTFKD, >P ABCFKBA ?V 2BVKLIAP.
7BfII CFOPQ ABCFKB TE>Q TB JB>K ?V > M>QE. 4EBOB >OB J>KV T>VP TB @LRIA FJMIBJBKQ > M>QE,
?RQ CLO RP, > PFJMIB T>V TFII ?B QL ABCFKB > M>QE >P > PBOFBP LC @LKKB@QBA MLFKQP:
F64B?2 6.20
#E>MQBO 6. !RQLKLJLRP !DBKQP
286
!K BSBK PFJMIBO M>QE TLRIA ?B > IFKB ?BQTBBK QTL MLFKQP.
7BfOB >IPL DLFKD QL @LKPFABO > M>QE QL E>SB > O>AFRP. )C TB QEFKH LC QEB M>QE >P > OL>A, QEB
O>AFRP ABQBOJFKBP QEB OL>AfP TFAQE. 7FQE > PJ>IIBO O>AFRP, LRO SBEF@IBP TFII E>SB QL CLIILT QEB
M>QE JLOB @ILPBIV; > TFABO O>AFRP TFII >IILT QEBJ QL PQO>V > ?FQ JLOB.
0RQQFKD QEFP FKQL > @I>PP, TB E>SB:
F64B?2 6.21: '.A5
F64B?2 6.22: )6:=92 =.A5
=F;MM .;NB U
A Path is only two points, start and end. .3?=NIL MN;LN;
.3?=NIL ?H>;
A path has a radius, i.e. how wide it is. @FI;N L;>COM;
.;NB() U
4EB .>QROB LC #LAB (S005)
287
.LT, IBQfP >PPRJB TB E>SB > SBEF@IB (>P ABMF@QBA ?BILT) LRQPFAB LC QEB M>QEfP O>AFRP,
JLSFKD TFQE > SBIL@FQV.
4EB CFOPQ QEFKD TB T>KQ QL AL FP MOBAF@Q, >PPRJFKD > @LKPQ>KQ SBIL@FQV, TEBOB QE>Q SBEF@IB TFII
?B FK QEB CRQROB.
/K@B TB E>SB QE>Q IL@>QFLK, FQfP KLT LRO GL? QL CFKA LRQ QEB SBEF@IBfP @ROOBKQ AFPQ>K@B COLJ
QEB M>QE LC QE>Q MOBAF@QBA IL@>QFLK. )C FQfP SBOV C>O >T>V, TBII, QEBK, TBfSB PQO>VBA COLJ QEB
M>QE >KA KBBA QL PQBBO ?>@H QLT>OAP FQ. )C FQfP @ILPB, QEBK TBfOB ALFKD /+ >KA >OB CLIILTFKD
QEB M>QE KF@BIV.
Picking some arbitrary values to initialize
the path
L;>COM = 20;
MN;LN = H?Q .3?=NIL(0,B?CABN/3);
?H> = H?Q .3?=NIL(QC>NB,2*B?CABN/3);
W
PIC> >CMJF;S() U // "CMJF;S NB? J;NB.
MNLIE?4?CABN(L;>COM*2);
MNLIE?(0,100);
FCH?(MN;LN.R,MN;LN.S,?H>.R,?H>.S);
MNLIE?4?CABN(1);
MNLIE?(0);
FCH?(MN;LN.R,MN;LN.S,?H>.R,?H>.S);
W
W
F64B?2 6.23
Start by making a copy of the velocity. .3?=NIL JL?>C=N = P?F.A?N();
Normalize it and look 25 pixels ahead by
scaling the vector up.
JL?>C=N.HILG;FCT?();
JL?>C=N.GOFN(25);
Add vector to location to find the predicted
location.
.3?=NIL JL?>C=N*I= = .3?=NIL.;>>(FI=, JL?>C=N);
#E>MQBO 6. !RQLKLJLRP !DBKQP
288
3L, ELT AL TB CFKA QEB AFPQ>K@B ?BQTBBK > MLFKQ >KA > IFKB? 4EFP @LK@BMQ FP HBV. 4EB AFPQ>K@B
?BQTBBK > MLFKQ >KA > IFKB FP ABCFKBA >P QEB IBKDQE LC QEB KLOJ>I ?BQTBBK QE>Q MLFKQ >KA IFKB.
4EB KLOJ>I FP > SB@QLO QE>Q BUQBKAP COLJ QE>Q MLFKQ >KA FP MBOMBKAF@RI>O QL QEB IFKB.
,BQfP CFDROB LRQ TE>Q TB AL HKLT. 7B HKLT TB E>SB > SB@QLO (@>II FQ A
FP:
7.C7=()*9EI(J>;J7)
TEF@E FP QEB @LAB QO>KPI>QFLK LC:
A
cos()
!KA FC VLR OB@>II:
A
= A
cos()
.LT, TE>Q FC SB@QLO B
= A
1 cos()
LO
A
= A
cos()
!KA TE>Q >OB TB ALFKD FK LRO @LAB? .LOJ>IFWFKD ?!
"B@>RPB LC QEFP C>@Q, TB @>K PFJMIFCV LRO @LAB >P:
The distance from START to NORMAL @FI;N > = ;.G;A()*=IM(NB?N;);
<.HILG;FCT?();
Scale PVector b to that distance. <.GOFN(>);
The normal point can be found by adding
the scaled version of b to the paths starting
point.
.3?=NIL HILG;F.ICHN = .3?=NIL.;>>(J;NB.MN;LN,<);
What is theta? The angle between A and B @FI;N NB?N; = .3?=NIL.;HAF??NQ??H(;,<);
<.HILG;FCT?();
<.GOFN(;.G;A()*=IM(NB?N;));
.3?=NIL HILG;F.ICHN = .3?=NIL.;>>(J;NB.MN;LN,<);
<.HILG;FCT?();
@FI;N NB?N; = .3?=NIL.;HAF??NQ??H(;,<);
<.HILG;FCT?();
#E>MQBO 6. !RQLKLJLRP !DBKQP
290
4EFP MOL@BPP FP @LJJLKIV HKLTK >P gP@>I>O MOLGB@QFLK.h FAF .:>(G) 4> ?30 >.,7,= ;=:50.?4:9 :1 FAF .:>(G) 4> ?30 >.,7,= ;=:50.?4:9 :1
A :9?: B. A :9?: B.
/K@B TB E>SB QEB KLOJ>I MLFKQ >ILKD QEB M>QE, TB E>SB QL AB@FAB TEBQEBO QEB SBEF@IB PELRIA
PQBBO QLT>OAP QEB M>QE >KA ELT. 2BVKLIAPfP >IDLOFQEJ PQ>QBP QE>Q QEB SBEF@IB PELRIA LKIV PQBBO
QLT>OAP QEB M>QE FC FQ PQO>VP ?BVLKA QEB M>QE (F.B., FC QEB AFPQ>K@B ?BQTBBK QEB KLOJ>I MLFKQ >KA
QEB MOBAF@QBA CRQROB IL@>QFLK FP DOB>QBO QE>K QEB M>QE O>AFRP).
We can use the dot product to scale bs
length.
<.GOFN(;.>IN(<));
.3?=NIL HILG;F.ICHN = .3?=NIL.;>>(J;NB.MN;LN,<);
F64B?2 6.26
F64B?2 6.27
@FI;N >CMN;H=? = .3?=NIL.>CMN(JL?>C=N*I=, HILG;F.ICHN);
If the vehicle is outside the path, seek the
target.
C@ (>CMN;H=? > J;NB.L;>COM) U
We dont have to work out the desired
velocity and steering force; all that is taken
care of by seek(), which we already wrote in
Example 6.1.
M??E(N;LA?N);
W
4EB .>QROB LC #LAB (S005)
291
"RQ TE>Q FP QEB Q>ODBQ?
2BVKLIAPfP >IDLOFQEJ FKSLISBP MF@HFKD > MLFKQ >EB>A LC QEB KLOJ>I LK QEB M>QE (PBB PQBM #3
>?LSB). "RQ CLO PFJMIF@FQV, TB @LRIA GRPQ P>V QE>Q QEB Q>ODBQ FP QEB KLOJ>I FQPBIC. 4EFP TFII TLOH
C>FOIV TBII:
3FK@B TB HKLT QEB SB@QLO QE>Q ABCFKBP QEB M>QE (TBfOB @>IIFKD FQ g"h), TB @>K FJMIBJBKQ
2BVKLIAPfP gMLFKQ >EB>A LK QEB M>QEh TFQELRQ QLL JR@E QOLR?IB.
0RQQFKD FQ >II QLDBQEBO, TB E>SB QEB CLIILTFKD PQBBOFKD CRK@QFLK FK LRO 3?BC=F? @I>PP.
@FI;N >CMN;H=? = .3?=NIL.>CMN(JL?>C=N*I=, HILG;F.ICHN);
C@ (>CMN;H=? > J;NB.L;>COM) U
Seek the normal point on the path. M??E(HILG;F.ICHN);
W
F64B?2 6.28
@FI;N >CMN;H=? = .3?=NIL.>CMN(JL?>C=N*I=, HILG;F.ICHN);
C@ (>CMN;H=? > J;NB.L;>COM) U
Normalize and scale b (pick 25 pixels
arbitrarily).
<.HILG;FCT?();
<.GOFN(25);
By adding b to normalPoint, we now move
25 pixels ahead on the path.
.3?=NIL N;LA?N = .3?=NIL.;>>(HILG;F.ICHN,<);
M??E(N;LA?N);
W
#E>MQBO 6. !RQLKLJLRP !DBKQP
292
Example 6.5: Simple path following
.LT, VLR J>V KLQF@B >?LSB QE>Q FKPQB>A LC RPFKD >II QE>Q ALQ MOLAR@Q/P@>I>O MOLGB@QFLK @LAB QL
CFKA QEB KLOJ>I MLFKQ, TB FKPQB>A @>II > CRK@QFLK: A?N,ILG;F.ICHN(). )K @>PBP IFHB QEFP, FQfP
RPBCRI QL ?OB>H LRQ QEB @LAB QE>Q MBOCLOJP > PMB@FCF@ Q>PH (CFKAFKD > KLOJ>I MLFKQ) FKQL >
CRK@QFLK QE>Q FQ @>K ?B RPBA DBKBOF@>IIV FK >KV @>PB TEBOB FQ FP OBNRFOBA. 4EB CRK@QFLK Q>HBP
QEOBB .3?=NILP: QEB CFOPQ ABCFKBP > MLFKQ FK #>OQBPF>K PM>@B >KA QEB PB@LKA >KA QEFOA
>ODRJBKQP ABCFKB > IFKB PBDJBKQ.
PIC> @IFFIQ(.;NB J) U
Step 1: Predict the vehicles future location. .3?=NIL JL?>C=N = P?F.A?N();
JL?>C=N.HILG;FCT?();
JL?>C=N.GOFN(25);
.3?=NIL JL?>C=N*I= = .3?=NIL.;>>(FI=, JL?>C=N);
Step 2: Find the normal point along the
path.
.3?=NIL ; = J.MN;LN;
.3?=NIL < = J.?H>;
.3?=NIL HILG;F.ICHN = A?N,ILG;F.ICHN(JL?>C=N*I=, ;, <);
Step 3: Move a little further along the path
and set a target.
.3?=NIL >CL = .3?=NIL.MO<(<, ;);
>CL.HILG;FCT?();
>CL.GOFN(10);
.3?=NIL N;LA?N = .3?=NIL.;>>(HILG;F.ICHN, >CL);
Step 4: If we are off the path, seek that
target in order to stay on the path.
@FI;N >CMN;H=? =
.3?=NIL.>CMN(HILG;F.ICHN, JL?>C=N*I=);
C@ (>CMN;H=? > J.L;>COM) U
M??E(N;LA?N);
W
W
4EB .>QROB LC #LAB (S005)
293
7E>Q AL TB E>SB PL C>O? 7B E>SB > .;NB @I>PP QE>Q ABCFKBP > M>QE >P > IFKB ?BQTBBK QTL
MLFKQP. 7B E>SB > 3?BC=F? @I>PP QE>Q ABCFKBP > SBEF@IB QE>Q @>K CLIILT QEB M>QE (RPFKD >
PQBBOFKD ?BE>SFLO QL PBBH > Q>ODBQ >ILKD QEB M>QE). 7E>Q FP JFPPFKD?
4>HB > ABBM ?OB>QE. 7BfOB >IJLPQ QEBOB.
F64B?2 6.29
.3?=NIL A?N,ILG;F.ICHN(.3?=NIL J, .3?=NIL ;, .3?=NIL <) U
PVector that points from a to p .3?=NIL ;J = .3?=NIL.MO<(J, ;);
PVector that points from a to b .3?=NIL ;< = .3?=NIL.MO<(<, ;);
Using the dot product for scalar projection ;<.HILG;FCT?();
;<.GOFN(;J.>IN(;<));
Finding the normal point along the line
segment
.3?=NIL HILG;F.ICHN = .3?=NIL.;>>(;, ;<);
L?NOLH HILG;F.ICHN;
W
6.9 Path Following with Multiple Segments 6.9 Path Following with Multiple Segments
7BfSB ?RFIQ > DOB>Q BU>JMIB PL C>O, VBP, ?RQ FQfP MOBQQV A>OK IFJFQFKD. !CQBO >II, TE>Q FC TB T>KQ
LRO M>QE QL ?B PLJBQEFKD QE>Q ILLHP JLOB IFHB:
F64B?2 6.30
#E>MQBO 6. !RQLKLJLRP !DBKQP
294
7EFIB FQfP QORB QE>Q TB @LRIA J>HB QEFP BU>JMIB TLOH CLO > @ROSBA M>QE, TBfOB JR@E IBPP IFHBIV
QL BKA RM KBBAFKD > @LLI @LJMOBPP LK LRO CLOBEB>A FC TB PQF@H TFQE IFKB PBDJBKQP. )K QEB BKA,
TB @>K >IT>VP BJMILV QEB P>JB QB@EKFNRB TB AFP@LSBOBA TFQE "LU2$dTB @>K AO>T TE>QBSBO
C>K@V @ROSBA M>QE TB T>KQ >KA >MMOLUFJ>QB FQ ?BEFKA QEB P@BKBP TFQE PFJMIB DBLJBQOF@
CLOJP.
3L, TE>QfP QEB MOL?IBJ? )C TB J>AB M>QE CLIILTFKD TLOH TFQE LKB IFKB PBDJBKQ, ELT AL TB
J>HB FQ TLOH TFQE > PBOFBP LC @LKKB@QBA IFKB PBDJBKQP? ,BQfP Q>HB > ILLH >D>FK >Q LRO SBEF@IB
AOFSFKD >ILKD QEB P@OBBK. 3>V TB >OOFSB >Q 3QBM 3.
'?0; 3: F49/ , ?,=20? ;:49? :9 ?30 ;,?3. '?0; 3: F49/ , ?,=20? ;:49? :9 ?30 ;,?3.
4L CFKA QEB Q>ODBQ, TB KBBA QL CFKA QEB KLOJ>I QL QEB IFKB PBDJBKQ. "RQ KLT QE>Q TB E>SB >
PBOFBP LC IFKB PBDJBKQP, TB E>SB > PBOFBP LC KLOJ>I MLFKQP (PBB >?LSB)! 7EF@E LKB AL TB
@ELLPB? 4EB PLIRQFLK TBfII BJMILV FP QL MF@H QEB KLOJ>I MLFKQ QE>Q FP (>) @ILPBPQ >KA (?) LK QEB
M>QE FQPBIC.
)C TB E>SB > MLFKQ >KA >K FKCFKFQBIV ILKD IFKB, TBfII >IT>VP E>SB > KLOJ>I. "RQ, >P FK QEB M>QE-
CLIILTFKD BU>JMIB, FC TB E>SB > MLFKQ >KA > IFKB PBDJBKQ, TB TLKfQ KB@BPP>OFIV CFKA > KLOJ>I
QE>Q FP LK QEB IFKB PBDJBKQ FQPBIC. 3L FC QEFP E>MMBKP CLO >KV LC QEB PBDJBKQP, TB @>K AFPNR>IFCV
QELPB KLOJ>IP. /K@B TB >OB IBCQ TFQE KLOJ>IP QE>Q >OB LK QEB M>QE FQPBIC (LKIV QTL FK QEB >?LSB
AF>DO>J), TB PFJMIV MF@H QEB LKB QE>Q FP @ILPBPQ QL LRO SBEF@IBfP IL@>QFLK.
F64B?2 6.31
F64B?2 6.32
4EB .>QROB LC #LAB (S005)
295
)K LOABO QL TOFQB QEB @LAB CLO QEFP, TBfII E>SB QL BUM>KA LRO .;NB @I>PP QL E>SB >K
ALL;S*CMN LC MLFKQP (O>QEBO QE>K GRPQ QTL, > PQ>OQ >KA >K BKA).
.LT QE>Q TB E>SB QEB .;NB @I>PP ABCFKBA, FQfP QEB SBEF@IBfP QROK QL AB>I TFQE JRIQFMIB IFKB
PBDJBKQP. !II TB AFA ?BCLOB T>P CFKA QEB KLOJ>I CLO LKB IFKB PBDJBKQ. 7B @>K KLT CFKA QEB
KLOJ>IP CLO >II QEB IFKB PBDJBKQP FK > ILLM.
=F;MM .;NB U
A Path is now an ArrayList of points
(PVector objects).
ALL;S*CMN<.3?=NIL> JICHNM;
@FI;N L;>COM;
.;NB() U
L;>COM = 20;
JICHNM = H?Q ALL;S*CMN<.3?=NIL>();
W
This function allows us to add points to the
path.
PIC> ;>>.ICHN(@FI;N R, @FI;N S) U .
.3?=NIL JICHN = H?Q .3?=NIL(R,S);
JICHNM.;>>(JICHN);
W
Display the path as a series of points. PIC> >CMJF;S() U
MNLIE?(0);
HI$CFF();
<?ACH0B;J?();
@IL (.3?=NIL P : JICHNM) U
P?LN?R(P.R,P.S);
W
?H>0B;J?();
W
W
@IL (CHN C = 0; C < J.JICHNM.MCT?()-1; C++) U
.3?=NIL ; = J.JICHNM.A?N(C);
.3?=NIL < = J.JICHNM.A?N(C+1);
#E>MQBO 6. !RQLKLJLRP !DBKQP
296
4EBK TB PELRIA J>HB PROB QEB KLOJ>I MLFKQ FP >@QR>IIV ?BQTBBK MLFKQP ; >KA <. 3FK@B TB
HKLT LRO M>QE DLBP COLJ IBCQ QL OFDEQ FK QEFP BU>JMIB, TB @>K QBPQ FC QEB L @LJMLKBKQ LC
HILG;F.ICHN FP LRQPFAB QEB L @LJMLKBKQP LC ; >KA <.
!P > IFQQIB QOF@H, TBfII P>V QE>Q FC FQfP KLQ TFQEFK QEB IFKB PBDJBKQ, IBQfP GRPQ MOBQBKA QEB BKA MLFKQ
LC QE>Q IFKB PBDJBKQ FP QEB KLOJ>I. 4EFP TFII BKPROB QE>Q LRO SBEF@IB >IT>VP PQ>VP LK QEB M>QE,
BSBK FC FQ PQO>VP LRQ LC QEB ?LRKAP LC LRO IFKB PBDJBKQP.
&FK>IIV, TBfII KBBA QL J>HB PROB TB CFKA QEB KLOJ>I MLFKQ QE>Q FP @ILPBPQ QL LRO SBEF@IB. 4L
>@@LJMIFPE QEFP, TB PQ>OQ TFQE > SBOV EFDE gTLOIA OB@LOAh AFPQ>K@B >KA FQBO>QB QEOLRDE B>@E
KLOJ>I MLFKQ QL PBB FC FQ ?B>QP QEB OB@LOA (F.B. FP IBPP QE>K). %>@E QFJB > KLOJ>I MLFKQ ?B>QP QEB
OB@LOA, QEB TLOIA OB@LOA FP RMA>QBA >KA QEB TFKKFKD MLFKQ FP PQLOBA FK > S>OF>?IB K>JBA
N;LA?N. !Q QEB BKA LC QEB ILLM, TBfII E>SB QEB @ILPBPQ KLOJ>I MLFKQ FK QE>Q S>OF>?IB.
Example 6.6: Path following
.3?=NIL HILG;F.ICHN = A?N,ILG;F.ICHN(JL?>C=N*I=, ;, <);
Finding the normals for each line segment
C@ (HILG;F.ICHN.R < ;.R VV HILG;F.ICHN.R > <.R) U
Use the end point of the segment as our
normal point if we cant find one.
HILG;F.ICHN = <.A?N();
W
.3?=NIL N;LA?N = HOFF;
4EB .>QROB LC #LAB (S005)
297
Start with a very high record that can easily
be beaten.
@FI;N QILF>/?=IL> = 1000000;
@IL (CHN C = 0; C < J.JICHNM.MCT?()-1; C++) U
.3?=NIL ; = J.JICHNM.A?N(C);
.3?=NIL < = J.JICHNM.A?N(C+1);
.3?=NIL HILG;F.ICHN = A?N,ILG;F.ICHN(JL?>C=N*I=, ;, <);
C@ (HILG;F.ICHN.R < ;.R VV HILG;F.ICHN.R > <.R) U
HILG;F.ICHN = <.A?N();
W
@FI;N >CMN;H=? = .3?=NIL.>CMN(JL?>C=N*I=, HILG;F.ICHN);
If we beat the record, then this should be
our target!
C@ (>CMN;H=? < QILF>/?=IL>) U
QILF>/?=IL> = >CMN;H=?;
N;LA?N = HILG;F.ICHN.A?N();
W
W
5MA>QB QEB M>QE-CLIILTFKD BU>JMIB PL QE>Q QEB M>QE @>K DL FK >KV AFOB@QFLK. ((FKQ:
VLRfII KBBA QL RPB QEB GCH() >KA G;R() CRK@QFLK TEBK ABQBOJFKFKD FC QEB KLOJ>I MLFKQ
FP FKPFAB QEB IFKB PBDJBKQ.)
C@ (HILG;F.ICHN.R < ::::(::::,::::) VV HILG;F.ICHN.R > ::::(::::,::::)) U
HILG;F.ICHN = <.A?N();
W
Exercise 6.10 Exercise 6.10
#OB>QB > M>QE QE>Q @E>KDBP LSBO QFJB. #>K QEB MLFKQP QE>Q ABCFKB QEB M>QE FQPBIC E>SB
QEBFO LTK PQBBOFKD ?BE>SFLOP?
Exercise 6.11 Exercise 6.11
6.10 Complex Systems 6.10 Complex Systems
2BJBJ?BO LRO MROMLPB? 4L ?OB>QEB IFCB FKQL QEB QEFKDP QE>Q JLSB >OLRKA LRO 0OL@BPPFKD
TFKALTP? "V IB>OKFKD QL TOFQB QEB @LAB CLO >K >RQLKLJLRP >DBKQ >KA ?RFIAFKD > PBOFBP LC
BU>JMIBP LC FKAFSFAR>I ?BE>SFLOP, ELMBCRIIV LRO PLRIP CBBI > IFQQIB JLOB CRII. "RQ QEFP FP KL
MI>@B QL PQLM >KA OBPQ LK LRO I>ROBIP. 7BfOB GRPQ DBQQFKD PQ>OQBA. !CQBO >II, QEBOB FP > ABBMBO
MROMLPB >Q TLOH EBOB. 9BP, > SBEF@IB FP > PFJRI>QBA ?BFKD QE>Q J>HBP AB@FPFLKP >?LRQ ELT
QL PBBH >KA CILT >KA CLIILT. "RQ TE>Q FP > IFCB IBA >ILKB, TFQELRQ QEB ILSB >KA PRMMLOQ LC
#E>MQBO 6. !RQLKLJLRP !DBKQP
298
LQEBOP? /RO MROMLPB EBOB FP KLQ LKIV QL ?RFIA FKAFSFAR>I ?BE>SFLOP CLO LRO SBEF@IBP, ?RQ QL MRQ
LRO SBEF@IBP FKQL PVPQBJP LC J>KV SBEF@IBP >KA >IILT QELPB SBEF@IBP QL FKQBO>@Q TFQE B>@E
LQEBO.
,BQfP QEFKH >?LRQ > QFKV, @O>TIFKD >KQdLKB PFKDIB >KQ. !K >KQ FP >K >RQLKLJLRP >DBKQ; FQ @>K
MBO@BFSB FQP BKSFOLKJBKQ (RPFKD >KQBKK>B QL D>QEBO FKCLOJ>QFLK >?LRQ QEB AFOB@QFLK >KA
PQOBKDQE LC @EBJF@>I PFDK>IP) >KA J>HB AB@FPFLKP >?LRQ ELT QL JLSB ?>PBA LK QELPB PFDK>IP.
"RQ @>K > PFKDIB >KQ >@QFKD >ILKB ?RFIA > KBPQ, D>QEBO CLLA, ABCBKA FQP NRBBK? !K >KQ FP >
PFJMIB RKFQ >KA @>K LKIV MBO@BFSB FQP FJJBAF>QB BKSFOLKJBKQ. ! @LILKV LC >KQP, ELTBSBO, FP >
PLMEFPQF@>QBA @LJMIBU PVPQBJ, > gPRMBOLOD>KFPJh FK TEF@E QEB @LJMLKBKQP TLOH QLDBQEBO QL
>@@LJMIFPE AFCCF@RIQ >KA @LJMIF@>QBA DL>IP.
7B T>KQ QL Q>HB TE>Q TBfSB IB>OKBA AROFKD QEB MOL@BPP LC ?RFIAFKD >RQLKLJLRP >DBKQP FK
0OL@BPPFKD FKQL PFJRI>QFLKP QE>Q FKSLISB J>KV >DBKQP LMBO>QFKD FK M>O>IIBId>DBKQP QE>Q E>SB
>K >?FIFQV QL MBO@BFSB KLQ LKIV QEBFO MEVPF@>I BKSFOLKJBKQ ?RQ >IPL QEB >@QFLKP LC QEBFO CBIILT
>DBKQP, >KA QEBK >@Q >@@LOAFKDIV. 7B T>KQ QL @OB>QB @LJMIBU PVPQBJP FK 0OL@BPPFKD.
7E>Q FP > @LJMIBU PVPQBJ? ! @LJMIBU PVPQBJ FP QVMF@>IIV ABCFKBA >P > PVPQBJ QE>Q FP gJLOB
QE>K QEB PRJ LC FQP M>OQP.h 7EFIB QEB FKAFSFAR>I BIBJBKQP LC QEB PVPQBJ J>V ?B FK@OBAF?IV
PFJMIB >KA B>PFIV RKABOPQLLA, QEB ?BE>SFLO LC QEB PVPQBJ >P > TELIB @>K ?B EFDEIV @LJMIBU,
FKQBIIFDBKQ, >KA AFCCF@RIQ QL MOBAF@Q. (BOB >OB QEOBB HBV MOFK@FMIBP LC @LJMIBU PVPQBJP.
i '48;70 @94?> B4?3 >3:=?-=,920 =07,?4:9>34;>. '48;70 @94?> B4?3 >3:=?-=,920 =07,?4:9>34;>. 4EFP FP TE>Q TBfSB ?BBK ?RFIAFKD >II
>ILKD: SBEF@IBP QE>Q E>SB > IFJFQBA MBO@BMQFLK LC QEBFO BKSFOLKJBKQ.
i '48;70 @94?> :;0=,?0 49 ;,=,7707. '48;70 @94?> :;0=,?0 49 ;,=,7707. 4EFP FP TE>Q TB KBBA QL PFJRI>QB FK @LAB. &LO
BSBOV @V@IB QEOLRDE 0OL@BPPFKDfP >L;Q() ILLM, B>@E RKFQ TFII AB@FAB ELT QL JLSB (QL
@OB>QB QEB >MMB>O>K@B LC QEBJ >II TLOHFKD FK M>O>IIBI).
i 'D>?08 ,> , B3:70 0C34-4?> 080=209? ;309:809,. 'D>?08 ,> , B3:70 0C34-4?> 080=209? ;309:809,. /RQ LC QEB FKQBO>@QFLKP ?BQTBBK
QEBPB PFJMIB RKFQP BJBODBP @LJMIBU ?BE>SFLO, M>QQBOKP, >KA FKQBIIFDBK@B. (BOB TBfOB
Q>IHFKD >?LRQ QEB OBPRIQ TB >OB ELMFKD CLO FK LRO PHBQ@EBP. 9BP, TB HKLT QEFP
E>MMBKP FK K>QROB (>KQ @LILKFBP, QBOJFQBP, JFDO>QFLK M>QQBOKP, B>OQENR>HBP,
PKLTCI>HBP, BQ@.), ?RQ @>K TB >@EFBSB QEB P>JB OBPRIQ FK LRO 0OL@BPPFKD PHBQ@EBP?
&LIILTFKD >OB QEOBB >AAFQFLK>I CB>QROBP LC @LJMIBU PVPQBJP QE>Q TFII EBIM CO>JB QEB AFP@RPPFLK,
>P TBII >P MOLSFAB DRFABIFKBP CLO CB>QROBP TB TFII T>KQ QL FK@IRAB FK LRO PLCQT>OB PFJRI>QFLKP.
)QfP FJMLOQ>KQ QL >@HKLTIBADB QE>Q QEFP FP > CRWWV PBQ LC @E>O>@QBOFPQF@P >KA KLQ >II @LJMIBU
PVPQBJP E>SB >II LC QEBJ.
i #:9-7490,=4?D. #:9-7490,=4?D. 4EFP >PMB@Q LC @LJMIBU PVPQBJP FP LCQBK @>PR>IIV OBCBOOBA QL >P gQEB
?RQQBOCIV BCCB@Q,h @LFKBA ?V J>QEBJ>QF@F>K >KA JBQBLOLILDFPQ %AT>OA .LOQLK ,LOBKW,
> MFLKBBO FK QEB PQRAV LC @E>LP QEBLOV. )K 1961, ,LOBKW T>P ORKKFKD > @LJMRQBO
TB>QEBO PFJRI>QFLK CLO QEB PB@LKA QFJB >KA, MBOE>MP QL P>SB > IFQQIB QFJB, QVMBA FK >
PQ>OQFKD S>IRB LC 0.506 FKPQB>A LC 0.506127. 4EB BKA OBPRIQ T>P @LJMIBQBIV AFCCBOBKQ
COLJ QEB CFOPQ OBPRIQ LC QEB PFJRI>QFLK. )K LQEBO TLOAP, QEB QEBLOV FP QE>Q > PFKDIB
?RQQBOCIV CI>MMFKD FQP TFKDP LK QEB LQEBO PFAB LC QEB TLOIA @LRIA @>RPB > J>PPFSB
4EB .>QROB LC #LAB (S005)
299
TB>QEBO PEFCQ >KA ORFK LRO TBBHBKA >Q QEB ?B>@E. 7B @>II FQ gKLK-IFKB>Oh ?B@>RPB
QEBOB FPKfQ > IFKB>O OBI>QFLKPEFM ?BQTBBK > @E>KDB FK FKFQF>I @LKAFQFLKP >KA >
@E>KDB FK LRQ@LJB. ! PJ>II @E>KDB FK FKFQF>I @LKAFQFLKP @>K E>SB > J>PPFSB BCCB@Q
LK QEB LRQ@LJB. .LK-IFKB>O PVPQBJP >OB > PRMBOPBQ LC @E>LQF@ PVPQBJP. )K QEB KBUQ
@E>MQBO, TBfII PBB ELT BSBK FK > PVPQBJ LC J>KV WBOLP >KA LKBP, FC TB @E>KDB GRPQ
LKB ?FQ, QEB OBPRIQ TFII ?B @LJMIBQBIV AFCCBOBKQ.
i C:8;0?4?4:9 ,9/ .::;0=,?4:9. C:8;0?4?4:9 ,9/ .::;0=,?4:9. /KB LC QEB QEFKDP QE>Q LCQBK J>HBP > @LJMIBU
PVPQBJ QF@H FP QEB MOBPBK@B LC ?LQE @LJMBQFQFLK >KA @LLMBO>QFLK ?BQTBBK QEB
BIBJBKQP. )K LRO RM@LJFKD CIL@HFKD PVPQBJ, TB TFII E>SB QEOBB ORIBPd>IFDKJBKQ,
@LEBPFLK, >KA PBM>O>QFLK. !IFDKJBKQ >KA @LEBPFLK TFII >PH QEB BIBJBKQP QL
g@LLMBO>QBhdF.B. TLOH QLDBQEBO QL PQ>V QLDBQEBO >KA JLSB QLDBQEBO. 3BM>O>QFLK,
ELTBSBO, TFII >PH QEB BIBJBKQP QL g@LJMBQBh CLO PM>@B. !P TB DBQ QL QEB CIL@HFKD
PVPQBJ, QOV Q>HFKD LRQ QEB @LLMBO>QFLK LO QEB @LJMBQFQFLK >KA VLRfII PBB ELT VLR
>OB IBCQ TFQELRQ @LJMIBUFQV. #LJMBQFQFLK >KA @LLMBO>QFLK >OB CLRKA FK IFSFKD
@LJMIBU PVPQBJP, ?RQ KLQ FK KLK-IFSFKD @LJMIBU PVPQBJP IFHB QEB TB>QEBO.
i F00/-,.6. F00/-,.6. #LJMIBU PVPQBJP LCQBK FK@IRAB > CBBA?>@H ILLM TEBOB QEB QEB LRQMRQ
LC QEB PVPQBJ FP CBA ?>@H FKQL QEB PVPQBJ QL FKCIRBK@B FQP ?BE>SFLO FK > MLPFQFSB LO
KBD>QFSB AFOB@QFLK. ,BQfP P>V VLR AOFSB QL TLOH B>@E A>V ?B@>RPB QEB MOF@B LC D>P
FP ILT. )K C>@Q, BSBOVLKB AOFSBP QL TLOH. 4EB MOF@B LC D>P DLBP RM >P ABJ>KA
?BDFKP QL BU@BBA PRMMIV. 9LR, >KA BSBOVLKB BIPB, AB@FAB QL Q>HB QEB QO>FK QL TLOH
?B@>RPB AOFSFKD FP QLL BUMBKPFSB. !KA QEB MOF@B LC D>P AB@IFKBP >P QEB ABJ>KA
AB@IFKBP. 4EB MOF@B LC D>P FP ?LQE QEB FKMRQ LC QEB PVPQBJ (ABQBOJFKFKD TEBQEBO
VLR @ELLPB QL AOFSB LO OFAB QEB QO>FK) >KA QEB LRQMRQ (QEB ABJ>KA QE>Q OBPRIQP COLJ
VLRO @ELF@B). ) PELRIA KLQB QE>Q B@LKLJF@ JLABIP (IFHB PRMMIV/ABJ>KA, QEB PQL@H
J>OHBQ) >OB LKB BU>JMIB LC > ERJ>K @LJMIBU PVPQBJ. /QEBOP FK@IRAB C>AP >KA
QOBKAP, BIB@QFLKP, @OLTAP, >KA QO>CCF@ CILT.
#LJMIBUFQV TFII PBOSB >P > QEBJB CLO QEB OBJ>FKFKD @LKQBKQ FK QEFP ?LLH. )K QEFP @E>MQBO, TBfII
?BDFK ?V >AAFKD LKB JLOB CB>QROB QL LRO 3?BC=F? @I>PP: >K >?FIFQV QL ILLH >Q KBFDE?LOFKD
SBEF@IBP.
6.11 Group Behaviors (or: Lets not run into each 6.11 Group Behaviors (or: Lets not run into each
other) other)
! DOLRM FP @BOQ>FKIV KLQ > KBT @LK@BMQ. 7BfSB ALKB QEFP ?BCLOBdFK #E>MQBO 4, TEBOB TB
ABSBILMBA > CO>JBTLOH CLO J>K>DFKD @LIIB@QFLKP LC M>OQF@IBP FK > .;LNC=F?0SMN?G @I>PP.
4EBOB, TB PQLOBA > IFPQ LC M>OQF@IBP FK >K ALL;S*CMN. 7BfII AL QEB P>JB QEFKD EBOB: PQLOB >
?RK@E LC 3?BC=F? L?GB@QP FK >K ALL;S*CMN.
#E>MQBO 6. !RQLKLJLRP !DBKQP
300
.LT TEBK FQ @LJBP QFJB QL AB>I TFQE >II QEB SBEF@IBP FK >L;Q(), TB PFJMIV ILLM QEOLRDE >II LC
QEBJ >KA @>II QEB KB@BPP>OV CRK@QFLKP.
/+, PL J>V?B TB T>KQ QL >AA > ?BE>SFLO, > CLO@B QL ?B >MMIFBA QL >II QEB SBEF@IBP. 4EFP @LRIA
?B PBBHFKD QEB JLRPB.
"RQ QE>QfP >K FKAFSFAR>I ?BE>SFLO. 7BfSB >IOB>AV PMBKQ QEFOQV-LAA M>DBP TLOOVFKD >?LRQ
FKAFSFAR>I ?BE>SFLOP. 7BfOB EBOB ?B@>RPB TB T>KQ QL >MMIV > DOLRM ?BE>SFLO. ,BQfP ?BDFK TFQE
PBM>O>QFLK, > ?BE>SFLO QE>Q @LJJ>KAP, g!SLFA @LIIFAFKD TFQE VLRO KBFDE?LOP!h
)P QE>Q OFDEQ? )Q PLRKAP DLLA, ?RQ FQfP KLQ. 7E>QfP JFPPFKD? )K QEB @>PB LC PBBH, TB P>FA, g3BBH
GIOM?5 >KA GIOM?6.h )K QEB @>PB LC PBM>O>QB, TBfOB P>VFKD gPBM>O>QB COLJ 9J9FMCB9 9@G9.h
7EL FP BSBOVLKB BIPB? )QfP QEB IFPQ LC >II QEB LQEBO SBEF@IBP.
4EFP FP QEB ?FD IB>M ?BVLKA TE>Q TB AFA ?BCLOB TFQE M>OQF@IB PVPQBJP. )KPQB>A LC E>SFKD B>@E
BIBJBKQ (M>OQF@IB LO SBEF@IB) LMBO>QB LK FQP LTK, TBfOB KLT P>VFKD, g(BV VLR, QEB SBEF@IB!
7EBK FQ @LJBP QFJB CLO VLR QL LMBO>QB, VLR KBBA QL LMBO>QB TFQE >K >T>OBKBPP LC BSBOVLKB
BIPB. 3L )fJ DLFKD QL DL >EB>A >KA M>PP VLR QEB ALL;S*CMN LC BSBOVLKB BIPB.h
4EFP FP ELT TBfSB J>MMBA LRQ M?NOJ() >KA >L;Q() QL AB>I TFQE > DOLRM ?BE>SFLO.
Declare an ArrayList of Vehicle objects. ALL;S*CMN<3?BC=F?> P?BC=F?M;
PIC> M?NOJ() U
Initialize and fill the ArrayList with a bunch of
Vehicles.
P?BC=F?M = H?Q ALL;S*CMN<3?BC=F?>;
@IL (CHN C = 0; C < 100; C++) U
P?BC=F?M.;>>(H?Q 3?BC=F?(L;H>IG(QC>NB),L;H>IG(B?CABN)));
W
W
PIC> >L;Q()U
@IL (3?BC=F? P : P?BC=F?M) U
P.OJ>;N?();
P.>CMJF;S();
W
W
P.M??E(GIOM?5,GIOM?6);
P.M?J;L;N?();
P.M?J;L;N?(P?BC=F?M);
4EB .>QROB LC #LAB (S005)
301
/C @LROPB, QEFP FP GRPQ QEB ?BDFKKFKD. 4EB
OB>I TLOH E>MMBKP FKPFAB QEB M?J;L;N?()
CRK@QFLK FQPBIC. ,BQfP CFDROB LRQ ELT TB T>KQ
QL ABCFKB PBM>O>QFLK. 2BVKLIAP PQ>QBP:
g3QBBO QL >SLFA @OLTAFKD.h )K LQEBO TLOAP, FC
> DFSBK SBEF@IB FP QLL @ILPB QL VLR, PQBBO
>T>V COLJ QE>Q SBEF@IB. 3LRKA C>JFIF>O? 2BJBJ?BO QEB PBBH ?BE>SFLO TEBOB > SBEF@IB PQBBOP
QLT>OAP > Q>ODBQ? 2BSBOPB QE>Q CLO@B >KA TB E>SB QEB CIBB ?BE>SFLO.
"RQ TE>Q FC JLOB QE>K LKB SBEF@IB FP QLL
@ILPB? )K QEFP @>PB, TBfII ABCFKB PBM>O>QFLK
>P QEB >SBO>DB LC >II QEB SB@QLOP MLFKQFKD
>T>V COLJ >KV @ILPB SBEF@IBP.
,BQfP ?BDFK QL TOFQB QEB @LAB. !P TB GRPQ
TLOHBA LRQ, TBfOB TOFQFKD > CRK@QFLK @>IIBA
M?J;L;N?() QE>Q OB@BFSBP >K ALL;S*CMN LC
6BEF@IB L?GB@QP >P >K >ODRJBKQ.
)KPFAB QEFP CRK@QFLK, TBfOB DLFKD QL ILLM QEOLRDE >II LC QEB SBEF@IBP >KA PBB FC >KV >OB QLL
@ILPB.
ALL;S*CMN<3?BC=F?> P?BC=F?M;
PIC> M?NOJ() U
MCT?(320,240);
P?BC=F?M = H?Q ALL;S*CMN<3?BC=F?>();
@IL (CHN C = 0; C < 100; C++) U
P?BC=F?M.;>>(H?Q 3?BC=F?(L;H>IG(QC>NB),L;H>IG(B?CABN)));
W
W
PIC> >L;Q() U
<;=EALIOH>(255);
@IL (3?BC=F? P : P?BC=F?M) U
This is really the only new thing were
doing in this section. Were asking a
Vehicle object to examine all the other
vehicles in the process of calculating a
separation force.
L.I;F7H7J;(L;>?9B;I);
P.OJ>;N?();
P.>CMJF;S();
W
W
F64B?2 6.33
F64B?2 6.34
PIC> M?J;L;N? (ALL;S*CMN<3?BC=F?> P?BC=F?M) U
W
#E>MQBO 6. !RQLKLJLRP !DBKQP
302
.LQF@B ELT FK QEB >?LSB @LAB, TB >OB KLQ LKIV @EB@HFKD FC QEB AFPQ>K@B FP IBPP QE>K > ABPFOBA
PBM>O>QFLK (F.B. QLL @ILPB!), ?RQ >IPL FC QEB AFPQ>K@B FP DOB>QBO QE>K WBOL. 4EFP FP > IFQQIB QOF@H QE>Q
J>HBP PROB TB ALKfQ >PH > SBEF@IB QL PBM>O>QB COLJ FQPBIC. 2BJBJ?BO, >II QEB SBEF@IBP >OB FK
QEB ALL;S*CMN, PL FC VLR >OBKfQ @>OBCRI VLRfII ?B @LJM>OFKD B>@E SBEF@IB QL FQPBIC!
/K@B TB HKLT QE>Q QTL SBEF@IBP >OB QLL @ILPB, TB KBBA QL J>HB > SB@QLO QE>Q MLFKQP >T>V
COLJ QEB LCCBKAFKD SBEF@IB.
4EFP FP KLQ BKLRDE. 7B E>SB QE>Q SB@QLO KLT, ?RQ TB KBBA QL J>HB PROB TB @>I@RI>QB QEB
>SBO>DB LC >II SB@QLOP MLFKQFKD >T>V COLJ @ILPB SBEF@IBP. (LT AL TB @LJMRQB >SBO>DB? 7B
>AA RM >II QEB SB@QLOP >KA AFSFAB ?V QEB QLQ>I.
This variable specifies how close is too
close.
@FI;N >?MCL?>M?J;L;NCIH = 20;
@IL (3?BC=F? INB?L : P?BC=F?M) U
@FI;N > = .3?=NIL.>CMN(FI=;NCIH, INB?L.FI=;NCIH);
C@ ((> > 0) && (> < >?MCL?>M?J;L;NCIH)) U
What is the distance between me and
another Vehicle?
Any code here will be executed if the
Vehicle is within 20 pixels.
W
W
C@ ((> > 0) && (> < >?MCL?>M?J;L;NCIH)) U
.3?=NIL >C@@ = .3?=NIL.MO<(FI=;NCIH, INB?L.FI=;NCIH);
>C@@.HILG;FCT?();
W
A PVector pointing away from the others
location
Start with an empty PVector. +0;9JEH IKC = D;M +0;9JEH();
?DJ 9EKDJ = 0;
We have to keep track of how many
Vehicles are too close.
@IL (3?BC=F? INB?L : P?BC=F?M) U
@FI;N > = .3?=NIL.>CMN(FI=;NCIH, INB?L.FI=;NCIH);
C@ ((> > 0) && (> < >?MCL?>M?J;L;NCIH)) U
+0;9JEH :?<< = +0;9JEH.IK8(BE97J?ED, EJ>;H.BE97J?ED);
>C@@.HILG;FCT?();
Add all the vectors together and increment
the count.
IKC.7::(:?<<);
=IOHN++;
W
W
4EB .>QROB LC #LAB (S005)
303
/K@B TB E>SB QEB >SBO>DB SB@QLO (PQLOBA FK QEB .3?=NIL L?GB@Q gPRJh), QE>Q .3?=NIL @>K ?B
P@>IBA QL J>UFJRJ PMBBA >KA ?B@LJB LRO ABPFOBA SBIL@FQVdTB 89G=F9 QL JLSB FK QE>Q
AFOB@QFLK >Q J>UFJRJ PMBBA! !KA LK@B TB E>SB QEB ABPFOBA SBIL@FQV, FQfP QEB P>JB LIA
2BVKLIAP PQLOV: PQBBOFKD BNR>IP ABPFOBA JFKRP SBIL@FQV.
,BQfP PBB QEB CRK@QFLK FK FQP BKQFOBQV. 4EBOB >OB QTL >AAFQFLK>I FJMOLSBJBKQP, KLQBA FK QEB
@LAB @LJJBKQP.
Example 6.7: Group behavior: Separation
We have to make sure we found at least
one close vehicle. We dont want to bother
doing anything if nothing is too close (not to
mention we cant divide by zero!)
?< (9EKDJ > 0) Q
IKC.:?L(9EKDJ);
S
C@ (=IOHN > 0) U
MOG.>CP(=IOHN);
Scale average to maxspeed (this becomes
desired).
MOG.M?N+;A(G;RMJ??>);
Reynoldss steering formula .3?=NIL MN??L = .3?=NIL.MO<(MOG,P?F);
MN??L.FCGCN(G;R@IL=?);
Apply the force to the Vehicles
acceleration.
;JJFS$IL=?(MN??L);
W
PIC> M?J;L;N? (ALL;S*CMN<3?BC=F?> P?BC=F?M) U
#E>MQBO 6. !RQLKLJLRP !DBKQP
304
Note how the desired separation is based
on the Vehicles size.
<BE7J :;I?H;:I;F7H7J?ED = H*2;
.3?=NIL MOG = H?Q .3?=NIL();
CHN =IOHN = 0;
@IL (3?BC=F? INB?L : P?BC=F?M) U
@FI;N > = .3?=NIL.>CMN(FI=;NCIH, INB?L.FI=;NCIH);
C@ ((> > 0) && (> < >?MCL?>M?J;L;NCIH)) U
.3?=NIL >C@@ = .3?=NIL.MO<(FI=;NCIH, INB?L.FI=;NCIH);
>C@@.HILG;FCT?();
What is the magnitude of the PVector
pointing away from the other vehicle? The
closer it is, the more we should flee. The
farther, the less. So we divide by the
distance to weight it appropriately.
:?<<.:?L(:);
MOG.;>>(>C@@);
=IOHN++;
W
W
C@ (=IOHN > 0) U
MOG.>CP(=IOHN);
MOG.HILG;FCT?();
MOG.GOFN(G;RMJ??>);
.3?=NIL MN??L = .3?=NIL.MO<(MOG, P?F);
MN??L.FCGCN(G;R@IL=?);
;JJFS$IL=?(MN??L);
W
W
2BTOFQB M?J;L;N?() QL TLOH FK QEB LMMLPFQB C>PEFLK (g@LEBPFLKh). )C > SBEF@IB FP ?BVLKA
> @BOQ>FK AFPQ>K@B, PQBBO QLT>OAP QE>Q SBEF@IB. 4EFP TFII HBBM QEB DOLRM QLDBQEBO. (.LQB
QE>Q FK > JLJBKQ, TBfOB DLFKD QL ILLH >Q TE>Q E>MMBKP TEBK TB E>SB ?LQE @LEBPFLK
>KA PBM>O>QFLK FK QEB P>JB PFJRI>QFLK.)
Exercise 6.12 Exercise 6.12
4EB .>QROB LC #LAB (S005)
305
!AA QEB PBM>O>QFLK CLO@B QL M>QE CLIILTFKD QL @OB>QB > PFJRI>QFLK LC 2BVKLIAPfP
g#OLTA 0>QE &LIILTFKD.h
Exercise 6.13 Exercise 6.13
6.12 Combinations 6.12 Combinations
4EB MOBSFLRP QTL BUBO@FPBP EFKQ >Q TE>Q FP MBOE>MP QEB JLPQ FJMLOQ>KQ >PMB@Q LC QEFP
@E>MQBO. !CQBO >II, TE>Q FP > 0OL@BPPFKD PHBQ@E TFQE LKB PQBBOFKD CLO@B @LJM>OBA QL LKB TFQE
J>KV? (LT @LRIA TB BSBK ?BDFK QL PFJRI>QB BJBODBK@B FK LRO PHBQ@EBP TFQE LKIV LKB ORIB?
4EB JLPQ BU@FQFKD >KA FKQOFDRFKD ?BE>SFLOP TFII @LJB COLJ JFUFKD >KA J>Q@EFKD JRIQFMIB
PQBBOFKD CLO@BP, >KA TBfII KBBA > JB@E>KFPJ CLO ALFKD PL.
9LR J>V ?B QEFKHFKD, g$RE, QEFP FP KLQEFKD KBT. 7B AL QEFP >II QEB QFJB.h 9LR TLRIA ?B OFDEQ.
)K C>@Q, TB AFA QEFP >P B>OIV >P #E>MQBO 2.
(BOB TB E>SB > JLSBO QE>Q OBPMLKAP QL QTL CLO@BP. 4EFP >II TLOHP KF@BIV ?B@>RPB LC QEB T>V
TB ABPFDKBA QEB +IP?L @I>PP QL >@@RJRI>QB QEB CLO@B SB@QLOP FKQL FQP >@@BIBO>QFLK SB@QLO. )K
QEFP @E>MQBO, ELTBSBO, LRO CLO@BP PQBJ COLJ FKQBOK>I ABPFOBP LC QEB JLSBOP (KLT @>IIBA
SBEF@IBP). !KA QELPB ABPFOBP @>K ?B TBFDEQBA. ,BQfP @LKPFABO > PHBQ@E TEBOB >II SBEF@IBP
E>SB QTL ABPFOBP:
i '006 ?30 8:@>0 7:.,?4:9. '006 ?30 8:@>0 7:.,?4:9.
i '0;,=,?0 1=:8 ,9D A034.70> ?3,? ,=0 ?:: .7:>0. '0;,=,?0 1=:8 ,9D A034.70> ?3,? ,=0 ?:: .7:>0.
.3?=NIL QCH> = H?Q .3?=NIL(0.001,0);
.3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1);
GIP?L.;JJFS$IL=?(QCH>);
GIP?L.;JJFS$IL=?(AL;PCNS);
#E>MQBO 6. !RQLKLJLRP !DBKQP
306
7B JFDEQ ?BDFK ?V >AAFKD > CRK@QFLK QL QEB 3?BC=F? @I>PP QE>Q J>K>DBP >II LC QEB ?BE>SFLOP.
,BQfP @>II FQ ;JJFS?B;PCILM().
(BOB TB PBB ELT > PFKDIB CRK@QFLK Q>HBP @>OB LC @>IIFKD QEB LQEBO CRK@QFLKP QE>Q >MMIV QEB
CLO@BPdM?J;L;N?() >KA M??E(). 7B @LRIA PQ>OQ JR@HFKD >OLRKA TFQE QELPB CRK@QFLKP >KA PBB
FC TB @>K >AGRPQ QEB PQOBKDQE LC QEB CLO@BP QEBV >OB @>I@RI>QFKD. "RQ FQ TLRIA ?B B>PFBO CLO RP QL
>PH QELPB CRK@QFLKP QL OBQROK QEB CLO@BP PL QE>Q TB @>K >AGRPQ QEBFO PQOBKDQE ?BCLOB >MMIVFKD
QEBJ QL QEB SBEF@IBfP >@@BIBO>QFLK.
,BQfP ILLH >Q ELT QEB PBBH CRK@QFLK @E>KDBA.
4EFP FP > PR?QIB @E>KDB, ?RQ FK@OBAF?IV FJMLOQ>KQ CLO RP: FQ >IILTP RP QL >IQBO QEB PQOBKDQE LC
QEBPB CLO@BP FK LKB MI>@B.
PIC> ;JJFS?B;PCILM(ALL;S*CMN<3?BC=F?> P?BC=F?M) U
M?J;L;N?(P?BC=F?M);
M??E(H?Q .3?=NIL(GIOM?5,GIOM?6));
W
PIC> ;JJFS?B;PCILM(ALL;S*CMN<3?BC=F?> P?BC=F?M) U
.3?=NIL M?J;L;N? = M?J;L;N?(P?BC=F?M);
.3?=NIL M??E = M??E(H?Q .3?=NIL(GIOM?5,GIOM?6));
We have to apply the force here since
seek() and separate() no longer do so.
;JJFS$IL=?(M?J;L;N?);
;JJFS$IL=?(M??E);
W
.3?=NIL M??E(.3?=NIL N;LA?N) U
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=);
>?MCL?>.HILG;FCT?();
>?MCL?>.GOFN(G;RMJ??>);
.3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?F);
MN??L.FCGCN(G;R@IL=?);
Instead of applying the force we return the
PVector.
;JJFS$IL=?(MN??L);
L?NOLH MN??L;
W
4EB .>QROB LC #LAB (S005)
307
Example 6.8: Combining steering behaviors: Seek and separate
PIC> ;JJFS?B;PCILM(ALL;S*CMN<3?BC=F?> P?BC=F?M) U
.3?=NIL M?J;L;N? = M?J;L;N?(P?BC=F?M);
.3?=NIL M??E = M??E(H?Q .3?=NIL(GIOM?5,GIOM?6));
These values can be whatever you want
them to be! They can be variables that are
customized for each vehicle, or they can
change over time.
I;F7H7J;.CKBJ(1.5);
I;;A.CKBJ(0.5);
;JJFS$IL=?(M?J;L;N?);
;JJFS$IL=?(M??E);
W
2BAL %U>JMIB 6.8 PL QE>Q QEB ?BE>SFLO TBFDEQP >OB KLQ @LKPQ>KQP. 7E>Q E>MMBKP FC
QEBV @E>KDB LSBO QFJB (>@@LOAFKD QL > PFKB T>SB LO 0BOIFK KLFPB)? /O FC PLJB SBEF@IBP
>OB JLOB @LK@BOKBA TFQE PBBHFKD >KA LQEBOP JLOB @LK@BOKBA TFQE PBM>O>QFKD? #>K
VLR FKQOLAR@B LQEBO PQBBOFKD ?BE>SFLOP >P TBII?
Exercise 6.14 Exercise 6.14
6.13 Flocking 6.13 Flocking
&IL@HFKD FP >K DOLRM >KFJ>I ?BE>SFLO QE>Q FP @E>O>@QBOFPQF@ LC J>KV IFSFKD @OB>QROBP, PR@E >P
?FOAP, CFPE, >KA FKPB@QP. )K 1986, #O>FD 2BVKLIAP @OB>QBA > @LJMRQBO PFJRI>QFLK LC CIL@HFKD
?BE>SFLO >KA AL@RJBKQBA QEB >IDLOFQEJ FK EFP M>MBO, g&IL@HP, (BOAP, >KA 3@ELLIP: !
$FPQOF?RQBA "BE>SFLO>I -LABI.h 2B@OB>QFKD QEFP PFJRI>QFLK FK 0OL@BPPFKD TFII ?OFKD QLDBQEBO
>II QEB @LK@BMQP FK QEFP @E>MQBO.
1. 29 K=@@ IG9 H<9 GH99F=B; :CF79 :CFAI@5 (GH99F = 89G=F98 - J9@C7=HM) HC =AD@9A9BH H<9
FI@9G C: :@C7?=B;.
2. /<9G9 GH99F=B; :CF79G K=@@ 69 ;FCID 69<5J=CFG 5B8 F9EI=F9 957< J9<=7@9 HC @CC? 5H
5@@ H<9 CH<9F J9<=7@9G.
3. 29 K=@@ 7CA6=B9 5B8 K9=;<H AI@H=D@9 :CF79G.
4. /<9 F9GI@H K=@@ 69 5 7CAD@9L GMGH9AR=BH9@@=;9BH ;FCID 69<5J=CF K=@@ 9A9F;9 :FCA
H<9 G=AD@9 FI@9G C: :@C7?=B; K=H<CIH H<9 DF9G9B79 C: 5 79BHF5@=N98 GMGH9A CF
@9589F.
4EB DLLA KBTP FP, TBfSB >IOB>AV ALKB FQBJP 1 QEOLRDE 3 FK QEFP @E>MQBO, PL QEFP PB@QFLK TFII
?B >?LRQ GRPQ MRQQFKD FQ >II QLDBQEBO >KA PBBFKD QEB OBPRIQ.
#E>MQBO 6. !RQLKLJLRP !DBKQP
308
"BCLOB TB ?BDFK, ) PELRIA JBKQFLK QE>Q TBfOB DLFKD QL @E>KDB QEB K>JB LC LRO 3?BC=F? @I>PP
(VBQ >D>FK). 2BVKLIAP RPBP QEB QBOJ g?LFAh (> J>AB-RM TLOA QE>Q OBCBOP QL > ?FOA-IFHB L?GB@Q) QL
ABP@OF?B QEB BIBJBKQP LC > CIL@HFKD PVPQBJ >KA TB TFII AL QEB P>JB.
,BQfP Q>HB >K LSBOSFBT LC QEB QEOBB ORIBP LC CIL@HFKD.
1. '0;,=,?4:9 '0;,=,?4:9 (>IPL HKLTK >P g>SLFA>K@Bh): 3QBBO QL >SLFA @LIIFAFKD TFQE VLRO
KBFDE?LOP.
2. A7429809? A7429809? (>IPL HKLTK >P g@LMVh): 3QBBO FK QEB P>JB AFOB@QFLK >P VLRO KBFDE?LOP.
3. C:30>4:9 C:30>4:9 (>IPL HKLTK >P g@BKQBOh): 3QBBO QLT>OAP QEB @BKQBO LC VLRO KBFDE?LOP (PQ>V
TFQE QEB DOLRM).
*RPQ >P TB AFA TFQE LRO PBM>O>QB >KA PBBH BU>JMIB, TBfII T>KQ LRO IC> L?GB@QP QL E>SB >
PFKDIB CRK@QFLK QE>Q J>K>DBP >II QEB >?LSB ?BE>SFLOP. 7BfII @>II QEFP CRK@QFLK @FI=E().
.LT, FQfP GRPQ > J>QQBO LC FJMIBJBKQFKD QEB QEOBB ORIBP. 7B AFA PBM>O>QFLK ?BCLOB; FQfP FABKQF@>I
QL LRO MOBSFLRP BU>JMIB. ,BQfP Q>HB > ILLH >Q >IFDKJBKQ, LO PQBBOFKD FK QEB P>JB AFOB@QFLK >P
F64B?2 6.35
PIC> @FI=E(ALL;S*CMN<IC>> <IC>M) U
The three flocking rules .3?=NIL M?J = M?J;L;N?(<IC>M);
.3?=NIL ;FC = ;FCAH(<IC>M);
.3?=NIL =IB = =IB?MCIH(<IC>M);
Arbitrary weights for these forces (Try
different ones!)
M?J.GOFN(1.5);
;FC.GOFN(1.0);
=IB.GOFN(1.0);
Applying all the forces ;JJFS$IL=?(M?J);
;JJFS$IL=?(;FC);
;JJFS$IL=?(=IB);
W
4EB .>QROB LC #LAB (S005)
309
VLRO KBFDE?LOP. !P TFQE >II LC LRO PQBBOFKD ?BE>SFLOP, TBfSB DLQ QL ?LFI ALTK QEFP @LK@BMQ
FKQL > ABPFOB: QEB ?LFAfP ABPFOBA SBIL@FQV FP QEB >SBO>DB SBIL@FQV LC FQP KBFDE?LOP.
3L LRO >IDLOFQEJ FP QL @>I@RI>QB QEB >SBO>DB SBIL@FQV LC >II QEB LQEBO ?LFAP >KA PBQ QE>Q QL
ABPFOBA.
4EB >?LSB FP MOBQQV DLLA, ?RQ FQfP JFPPFKD LKB O>QEBO @OR@F>I ABQ>FI. /KB LC QEB HBV MOFK@FMIBP
?BEFKA @LJMIBU PVPQBJP IFHB CIL@HFKD FP QE>Q QEB BIBJBKQP (FK QEFP @>PB, ?LFAP) E>SB PELOQ-
O>KDB OBI>QFLKPEFMP. 4EFKHFKD >?LRQ >KQP >D>FK, FQfP MOBQQV B>PV QL FJ>DFKB >K >KQ ?BFKD >?IB
QL PBKPB FQP FJJBAF>QB BKSFOLKJBKQ, ?RQ IBPP PL >K >KQ E>SFKD >K >T>OBKBPP LC TE>Q
>KLQEBO >KQ FP ALFKD ERKAOBAP LC CBBQ >T>V. 4EB C>@Q QE>Q QEB >KQP @>K MBOCLOJ PR@E
@LJMIBU @LIIB@QFSB ?BE>SFLO COLJ LKIV QEBPB KBFDE?LOFKD OBI>QFLKPEFMP FP TE>Q J>HBP QEBJ
PL BU@FQFKD FK QEB CFOPQ MI>@B.
)K LRO >IFDKJBKQ CRK@QFLK, TBfOB Q>HFKD QEB >SBO>DB SBIL@FQV LC >II QEB ?LFAP, TEBOB>P TB
PELRIA OB>IIV LKIV ?B ILLHFKD >Q QEB ?LFAP TFQEFK > @BOQ>FK AFPQ>K@B. 4E>Q AFPQ>K@B QEOBPELIA
FP RM QL VLR, LC @LROPB. 9LR @LRIA ABPFDK ?LFAP QE>Q @>K PBB LKIV QTBKQV MFUBIP >T>V LO
?LFAP QE>Q @>K PBB > ERKAOBA MFUBIP >T>V.
.3?=NIL ;FCAH (ALL;S*CMN<IC>> <IC>M) U
Add up all the velocities and divide by the
total to calculate the average velocity.
.3?=NIL MOG = H?Q .3?=NIL(0,0);
@IL (IC> INB?L : <IC>M) U
MOG.;>>(INB?L.P?FI=CNS);
W
MOG.>CP(<IC>M.MCT?());
We desire to go in that direction at
maximum speed.
MOG.M?N+;A(G;RMJ??>);
Reynoldss steering force formula .3?=NIL MN??L = .3?=NIL.MO<(MOG,P?FI=CNS);
MN??L.FCGCN(G;R@IL=?);
L?NOLH MN??L;
W
#E>MQBO 6. !RQLKLJLRP !DBKQP
310
-R@E IFHB TB AFA TFQE PBM>O>QFLK (LKIV @>I@RI>QFKD > CLO@B CLO LQEBOP TFQEFK > @BOQ>FK AFPQ>K@B),
TBfII T>KQ QL AL QEB P>JB TFQE >IFDKJBKQ (>KA @LEBPFLK).
F64B?2 6.36
.3?=NIL ;FCAH (ALL;S*CMN<IC>> <IC>M) U
This is an arbitrary value and could vary
from boid to boid.
@FI;N H?CAB<IL>CMN = 50;
.3?=NIL MOG = H?Q .3?=NIL(0,0);
CHN =IOHN = 0;
@IL (IC> INB?L : <IC>M) U
@FI;N > = .3?=NIL.>CMN(FI=;NCIH,INB?L.FI=;NCIH);
C@ ((> > 0) && (> < H?CAB<IL>CMN)) U
MOG.;>>(INB?L.P?FI=CNS);
For an average, we need to keep track of
how many boids are within the distance.
=IOHN++;
W
W
C@ (=IOHN > 0) U
MOG.>CP(=IOHN);
MOG.HILG;FCT?();
MOG.GOFN(G;RMJ??>);
.3?=NIL MN??L = .3?=NIL.MO<(MOG,P?FI=CNS);
MN??L.FCGCN(G;R@IL=?);
L?NOLH MN??L;
If we dont find any close boids, the steering
force is zero.
W ?FM? U
L?NOLH H?Q .3?=NIL(0,0);
W
W
4EB .>QROB LC #LAB (S005)
311
&FK>IIV, TB >OB OB>AV CLO @LEBPFLK. (BOB LRO @LAB FP SFOQR>IIV FABKQF@>I QL QE>Q CLO
>IFDKJBKQdLKIV FKPQB>A LC @>I@RI>QFKD QEB >SBO>DB SBIL@FQV LC QEB ?LFAfP KBFDE?LOP, TB T>KQ
QL @>I@RI>QB QEB >SBO>DB IL@>QFLK LC QEB ?LFAfP KBFDE?LOP (>KA RPB QE>Q >P > Q>ODBQ QL PBBH).
)QfP >IPL TLOQE Q>HFKD QEB QFJB QL TOFQB > @I>PP @>IIBA $FI=E, TEF@E TFII ?B SFOQR>IIV FABKQF@>I
QL QEB .;LNC=F?0SMN?G @I>PP TB TOLQB FK #E>MQBO 4 TFQE LKIV LKB QFKV @E>KDB: 7EBK TB
@>II LOH() LK B>@E IC> L?GB@Q (>P TB AFA QL B>@E .;LNC=F? L?GB@Q), TBfII M>PP FK >
OBCBOBK@B QL QEB BKQFOB ALL;S*CMN LC ?LFAP.
#>K VLR TOFQB QEB >?LSB @LAB PL QE>Q
?LFAP @>K LKIV PBB LQEBO ?LFAP QE>Q >OB
>@QR>IIV TFQEFK QEBFO gMBOFMEBO>Ih SFPFLK
(>P FC QEBV E>A BVBP)?
Exercise 6.15 Exercise 6.15
.3?=NIL =IB?MCIH (ALL;S*CMN<IC>> <IC>M) U
@FI;N H?CAB<IL>CMN = 50;
.3?=NIL MOG = H?Q .3?=NIL(0,0);
CHN =IOHN = 0;
@IL (IC> INB?L : <IC>M) U
@FI;N > = .3?=NIL.>CMN(FI=;NCIH,INB?L.FI=;NCIH);
C@ ((> > 0) && (> < H?CAB<IL>CMN)) U
Adding up all the others locations MOG.;>>(INB?L.FI=;NCIH);
=IOHN++;
W
W
C@ (=IOHN > 0) U
MOG.>CP(=IOHN);
Here we make use of the seek() function
we wrote in Example 6.8. The target we
seek is the average location of our
neighbors.
H;JKHD I;;A(IKC);
W ?FM? U
L?NOLH H?Q .3?=NIL(0,0);
W
W
#E>MQBO 6. !RQLKLJLRP !DBKQP
312
!KA LRO J>FK MOLDO>J TFII ILLH IFHB:
Example 6.9: Flocking
=F;MM $FI=E U
ALL;S*CMN<IC>> <IC>M;
$FI=E() U
<IC>M = H?Q ALL;S*CMN<IC>>();
W
PIC> LOH() U
@IL (IC> < : <IC>M) U
Each Boid object must know about all the
other Boids.
8.HKD(8E?:I);
W
W
PIC> ;>>IC>(IC> <) U
<IC>M.;>>(<);
W
W
A Flock object manages the entire group. $FI=E @FI=E;
PIC> M?NOJ() U
MCT?(300,200);
@FI=E = H?Q $FI=E();
@IL (CHN C = 0; C < 100; C++) U
IC> < = H?Q IC>(QC>NB/2,B?CABN/2);
The Flock starts out with 100 Boids. @FI=E.;>>IC>(<);
W
W
PIC> >L;Q() U
<;=EALIOH>(255);
@FI=E.LOH();
W
4EB .>QROB LC #LAB (S005)
313
#LJ?FKB CIL@HFKD TFQE PLJB LQEBO PQBBOFKD ?BE>SFLOP.
Exercise 6.16 Exercise 6.16
)K EFP ?LLH 4EB CCADIH5H=CB5@ B95IHM
C: *5HIF9 (-)4 0OBPP, 2000), '>OV &I>HB
ABP@OF?BP > CLROQE ORIB CLO CIL@HFKD:
g6FBT: JLSB I>QBO>IIV >T>V COLJ >KV
?LFA QE>Q ?IL@HP QEB SFBT.h (>SB VLRO
?LFAP CLIILT QEFP ORIB.
Exercise 6.17 Exercise 6.17
#OB>QB > CIL@HFKD PFJRI>QFLK TEBOB >II LC QEB M>O>JBQBOP (G9D5F5H=CB K9=;<H, 7C<9G=CB
K9=;<H, 5@=;BA9BH K9=;<H, A5L=AIA :CF79, A5L=AIA GD998) @E>KDB LSBO QFJB. 4EBV
@LRIA ?B @LKQOLIIBA ?V 0BOIFK KLFPB LO ?V RPBO FKQBO>@QFLK. (&LO BU>JMIB, VLR @LRIA
RPB > IF?O>OV PR@E >P @LKQOLIM5 (EQQM://TTT.PLG>JL.AB/IF?O>OFBP/@LKQOLI05/) QL QFB QEB
S>IRBP QL PIFABO MLPFQFLKP.)
Exercise 6.18 Exercise 6.18
6FPR>IFWB QEB CIL@H FK >K BKQFOBIV AFCCBOBKQ T>V.
Exercise 6.19 Exercise 6.19
#E>MQBO 6. !RQLKLJLRP !DBKQP
314
6.14 Algorithmic Efficiency (or: Why does my $@(*%! 6.14 Algorithmic Efficiency (or: Why does my $@(*%!
run so slowly?) run so slowly?)
) TLRIA IFHB QL EFAB QEB A>OH QORQE ?BEFKA TBfSB GRPQ ALKB, ?B@>RPB ) TLRIA IFHB VLR QL ?B
E>MMV >KA IFSB > CRICFIIFKD >KA JB>KFKDCRI IFCB. "RQ ) >IPL TLRIA IFHB QL ?B >?IB QL PIBBM >Q KFDEQ
TFQELRQ TLOOVFKD >?LRQ VLR PL JR@E. 3L FQ FP TFQE > EB>SV EB>OQ QE>Q ) JRPQ ?OFKD RM QEFP QLMF@.
'OLRM ?BE>SFLOP >OB TLKABOCRI. "RQ QEBV @>K ?B PILT, >KA QEB JLOB BIBJBKQP FK QEB DOLRM,
QEB PILTBO QEBV @>K ?B. 5PR>IIV, TEBK TB Q>IH >?LRQ 0OL@BPPFKD PHBQ@EBP ORKKFKD PILTIV, FQfP
?B@>RPB AO>TFKD QL QEB P@OBBK @>K ?B PILTdQEB JLOB VLR AO>T, QEB PILTBO VLRO PHBQ@E ORKP.
4EFP FP >@QR>IIV > @>PB, ELTBSBO, TEBOB QEB PILTKBPP ABOFSBP COLJ QEB >IDLOFQEJ FQPBIC. ,BQfP
AFP@RPP.
#LJMRQBO P@FBKQFPQP @I>PPFCV >IDLOFQEJP TFQE PLJBQEFKD @>IIBA g"FD / KLQ>QFLK,h TEF@E
ABP@OF?BP QEB BCCF@FBK@V LC >K >IDLOFQEJ: ELT J>KV @LJMRQ>QFLK>I @V@IBP ALBP FQ OBNRFOB QL
@LJMIBQB? ,BQfP @LKPFABO > PFJMIB >K>ILD PB>O@E MOL?IBJ. 9LR E>SB > ?>PHBQ @LKQ>FKFKD LKB
ERKAOBA @EL@LI>QB QOB>QP, LKIV LKB LC TEF@E FP MROB A>OH @EL@LI>QB. 4E>QfP QEB LKB VLR T>KQ
QL B>Q. 4L CFKA FQ, VLR MF@H QEB @EL@LI>QBP LRQ LC QEB ?>PHBQ LKB ?V LKB. 3ROB, VLR JFDEQ ?B
IR@HV >KA CFKA FQ LK QEB CFOPQ QOV, ?RQ FK QEB TLOPQ-@>PB P@BK>OFL VLR E>SB QL @EB@H >II LKB
ERKAOBA ?BCLOB VLR CFKA QEB A>OH @EL@LI>QB. 4L CFKA LKB QEFKD FK LKB ERKAOBA, VLR E>SB QL
@EB@H LKB ERKAOBA QEFKDP (LO QL CFKA LKB QEFKD FK . QEFKDP, VLR E>SB QL @EB@H . QFJBP.) 9LRO
"FD / .LQ>QFLK FP .. 4EFP, FK@FABKQ>IIV, FP QEB "FD / .LQ>QFLK QE>Q ABP@OF?BP LRO PFJMIB M>OQF@IB
PVPQBJ. )C TB E>SB . M>OQF@IBP, TB E>SB QL ORK >KA AFPMI>V QELPB M>OQF@IBP . QFJBP.
.LT, IBQfP QEFKH >?LRQ > DOLRM ?BE>SFLO (PR@E >P CIL@HFKD). &LO BSBOV IC> L?GB@Q, TB E>SB QL
@EB@H BSBOV LQEBO IC> L?GB@Q (CLO FQP SBIL@FQV >KA IL@>QFLK). ,BQfP P>V TB E>SB LKB ERKAOBA
?LFAP. &LO ?LFA #1, TB KBBA QL @EB@H LKB ERKAOBA ?LFAP; CLO ?LFA #2, TB KBBA QL @EB@H LKB
ERKAOBA ?LFAP, >KA PL LK >KA PL CLOQE. &LO LKB ERKAOBA ?LFAP, TB KBBA QL MBOCLOJ LKB
ERKAOBA QFJBP LKB ERKAOBA @EB@HP, LO QBK QELRP>KA. .L MOL?IBJ: @LJMRQBOP >OB C>PQ >KA @>K
AL QEFKDP QBK QELRP>KA QFJBP MOBQQV B>PFIV. ,BQfP QOV LKB QELRP>KA.
1,000 U 1,000 = 1,000,000 @V@IBP.
/+, QEFP FP O>QEBO PILT, ?RQ PQFII PLJBTE>Q J>K>DB>?IB. ,BQfP QOV 10,000 BIBJBKQP:
10,000 U 10,000 BIBJBKQP = 100,000,000 @V@IBP.
.LT, TBfOB OB>IIV DBQQFKD PILT. 2B>IIV, OB>IIV, OB>IIV PILT.
.LQF@B PLJBQEFKD LAA? !P QEB KRJ?BO LC BIBJBKQP FK@OB>PBP ?V > C>@QLO LC 10, QEB KRJ?BO LC
OBNRFOBA @V@IBP FK@OB>PBP ?V > C>@QLO LC 100. /O >P QEB KRJ?BO LC BIBJBKQP FK@OB>PBP ?V >
C>@QLO LC ., QEB @V@IBP FK@OB>PB ?V > C>@QLO LC . QFJBP .. 4EFP FP HKLTK >P "FD / .LQ>QFLK .-
3NR>OBA.
) HKLT TE>Q VLR >OB QEFKHFKD. 9LR >OB QEFKHFKD: g.L MOL?IBJ; TFQE CIL@HFKD, TB LKIV KBBA QL
@LKPFABO QEB ?LFAP QE>Q >OB @ILPB QL LQEBO ?LFAP. 3L BSBK FC TB E>SB 1,000 ?LFAP, TB @>K GRPQ
ILLH >Q, P>V, QEB 5 @ILPBPQ ?LFAP >KA QEBK TB LKIV E>SB 5,000 @V@IBP.h 9LR M>RPB CLO >
4EB .>QROB LC #LAB (S005)
315
JLJBKQ, >KA QEBK PQ>OQ QEFKHFKD: g3L CLO B>@E ?LFA ) GRPQ KBBA QL @EB@H >II QEB ?LFAP >KA CFKA
QEB CFSB @ILPBPQ LKBP >KA )fJ DLLA!h 3BB QEB @>Q@E-22? %SBK FC TB LKIV T>KQ QL ILLH >Q QEB
@ILPB LKBP, QEB LKIV T>V QL HKLT TE>Q QEB @ILPB LKBP >OB TLRIA ?B QL @EB@H >II LC QEBJ.
/O FP QEBOB >KLQEBO T>V?
,BQfP Q>HB > KRJ?BO QE>Q TB JFDEQ >@QR>IIV T>KQ QL RPB, ?RQ TLRIA PQFII ORK QLL PILTIV: 2,000
(4,000,000 @V@IBP OBNRFOBA).
7E>Q FC TB @LRIA AFSFAB QEB P@OBBK FKQL > DOFA? 7B TLRIA Q>HB >II 2,000 ?LFAP >KA >PPFDK
B>@E ?LFA QL > @BII TFQEFK QE>Q DOFA. 7B TLRIA QEBK ?B >?IB QL ILLH >Q B>@E ?LFA >KA
@LJM>OB FQ QL FQP KBFDE?LOP TFQEFK QE>Q @BII >Q >KV DFSBK JLJBKQ. )J>DFKB > 10 U 10 DOFA. )K >
PVPQBJ LC 2,000 BIBJBKQP, LK >SBO>DB, >MMOLUFJ>QBIV 20 BIBJBKQP TLRIA ?B CLRKA FK B>@E
@BII (20 U 10 U 10 = 2,000). %>@E @BII TLRIA QEBK OBNRFOB 20 U 20 = 400 @V@IBP. 7FQE 100 @BIIP,
TBfA E>SB 100 U 400 = 40,000 @V@IBP, > J>PPFSB P>SFKDP LSBO 4,000,000.
4EFP QB@EKFNRB FP HKLTK >P g?FK-I>QQF@B PM>QF>I PR?AFSFPFLKh >KA FP LRQIFKBA FK JLOB ABQ>FI FK
(PROMOFPB, PROMOFPB) 2BVKLIAPfP 2000 M>MBO, g)KQBO>@QFLK TFQE 'OLRMP LC !RQLKLJLRP
#E>O>@QBOPh (EQQM://TTT.OBA3A.@LJ/@TO/M>MBOP/2000/MFM.MAC). (LT AL TB FJMIBJBKQ PR@E
>K >IDLOFQEJ FK 0OL@BPPFKD? /KB T>V FP QL HBBM JRIQFMIB ALL;S*CMNP. /KB ALL;S*CMN TLRIA
HBBM QO>@H LC >II QEB ?LFAP, GRPQ IFHB FK LRO CIL@HFKD BU>JMIB.
)K >AAFQFLK QL QE>Q ALL;S*CMN, TB PQLOB >K >AAFQFLK>I OBCBOBK@B QL B>@E IC> L?GB@Q FK >
QTL-AFJBKPFLK>I ALL;S*CMN. &LO B>@E @BII FK QEB DOFA, QEBOB FP >K ALL;S*CMN QE>Q QO>@HP QEB
L?GB@QP FK QE>Q @BII.
F64B?2 6.37
ALL;S*CMN<IC>> <IC>M;
#E>MQBO 6. !RQLKLJLRP !DBKQP
316
)K QEB J>FK >L;Q() ILLM, B>@E IC> L?GB@Q QEBK OBDFPQBOP FQPBIC FK QEB >MMOLMOF>QB @BII
>@@LOAFKD QL FQP IL@>QFLK.
4EBK TEBK FQ @LJBP QFJB QL E>SB QEB ?LFAP @EB@H CLO KBFDE?LOP, QEBV @>K ILLH >Q LKIV QELPB FK
QEBFO M>OQF@RI>O @BII (FK QORQE, TB >IPL KBBA QL @EB@H KBFDE?LOFKD @BIIP QL AB>I TFQE ?LOABO
@>PBP).
Example 6.10: Bin-lattice spatial subdivision
7BfOB LKIV @LSBOFKD QEB ?>PF@P EBOB; CLO QEB CRII @LAB, @EB@H QEB ?LLHfP TB?PFQB.
.LT, QEBOB >OB @BOQ>FKIV CI>TP TFQE QEFP PVPQBJ. 7E>Q FC >II QEB ?LFAP @LKDOBD>QB FK QEB @LOKBO
>KA IFSB FK QEB P>JB @BII? 4EBK ALKfQ TB E>SB QL @EB@H >II 2,000 >D>FKPQ >II 2,000?
4EB DLLA KBTP FP QE>Q QEFP KBBA CLO LMQFJFW>QFLK FP > @LJJLK LKB >KA QEBOB >OB > TFAB
S>OFBQV LC PFJFI>O QB@EKFNRBP LRQ QEBOB. &LO RP, FQfP IFHBIV QE>Q > ?>PF@ >MMOL>@E TFII ?B DLLA
BKLRDE (FK JLPQ @>PBP, VLR TLKfQ KBBA LKB >Q >II.) &LO >KLQEBO, JLOB PLMEFPQF@>QBA >MMOL>@E,
@EB@H LRQ QLUF@IF?P' /@QOBB BU>JMIBP (EQQM://QLUF@IF?P.LOD/2010/02/KBT-M>@H>DB-PFJRQFIP/).
ALL;S*CMN<IC>>8989 ALC>;
CHN =IFOGH = CHN(<IC>.R) / L?MIFONCIH;
CHN LIQ = CHN(<IC>.S) /L?MIFONCIH;
ALC>8=IFOGH98LIQ9.;>>(<IC>);
CHN =IFOGH = CHN(<IC>.R) / L?MIFONCIH;
CHN LIQ = CHN(<IC>.S) /L?MIFONCIH;
<IC>.@FI=E(<IC>M);
Instead of looking at all the boids, just this
cell
<IC>.@FI=E(ALC>8=IFOGH98LIQ9);
6.15 A Few Last Notes: Optimization Tricks 6.15 A Few Last Notes: Optimization Tricks
4EFP FP PLJBQEFKD LC > JLJBKQLRP L@@>PFLK. 4EB BKA LC #E>MQBO 6 J>OHP QEB BKA LC LRO PQLOV
LC JLQFLK (FK QEB @LKQBUQ LC QEFP ?LLH, QE>Q FP). 7B PQ>OQBA TFQE QEB @LK@BMQ LC > SB@QLO, JLSBA
LK QL CLO@BP, ABPFDKBA PVPQBJP LC J>KV BIBJBKQP, BU>JFKBA MEVPF@P IF?O>OFBP, ?RFIQ BKQFQFBP
TFQE ELMBP >KA AOB>JP >KA CB>OP, >KA PFJRI>QBA BJBODBK@B. 4EB PQLOV ALBPKfQ BKA EBOB, ?RQ
FQ ALBP Q>HB > ?FQ LC > QROK. 4EB KBUQ QTL @E>MQBOP TLKfQ CL@RP LK JLSFKD ?LAFBP, ?RQ O>QEBO LK
PVPQBJP LC ORIBP. "BCLOB TB DBQ QEBOB, ) E>SB > CBT NRF@H FQBJP )fA IFHB QL JBKQFLK QE>Q >OB
FJMLOQ>KQ TEBK TLOHFKD TFQE QEB BU>JMIBP FK #E>MQBOP 1 QEOLRDE 6. 4EBV >IPL OBI>QB QL
LMQFJFWFKD VLRO @LAB, TEF@E CFQP FK TFQE QEB MOBSFLRP PB@QFLK.
4EB .>QROB LC #LAB (S005)
317
1) Magnitude squared (or sometimes distance squared) 1) Magnitude squared (or sometimes distance squared)
7E>Q FP J>DKFQRAB PNR>OBA >KA TEBK PELRIA VLR RPB FQ? ,BQfP OBSFPFQ ELT QEB J>DKFQRAB LC
> SB@QLO FP @>I@RI>QBA.
->DKFQRAB OBNRFOBP QEB PNR>OB OLLQ LMBO>QFLK. !KA FQ PELRIA. !CQBO >II, FC VLR T>KQ QEB
J>DKFQRAB LC > SB@QLO, QEBK VLRfSB DLQ QL ILLH RM QEB 0VQE>DLOB>K QEBLOBJ >KA @LJMRQB FQ
(TB AFA QEFP FK #E>MQBO 1). (LTBSBO, FC VLR @LRIA PLJBELT PHFM RPFKD QEB PNR>OB OLLQ, VLRO
@LAB TLRIA ORK C>PQBO. ,BQfP @LKPFABO > PFQR>QFLK TEBOB VLR GRPQ T>KQ QL HKLT QEB OBI>QFSB
J>DKFQRAB LC > SB@QLO. &LO BU>JMIB, FP QEB J>DKFQRAB DOB>QBO QE>K QBK? (!PPRJB > .3?=NIL
P.)
7BII, QEFP FP BNRFS>IBKQ QL P>VFKD:
!KA ELT FP J>DKFQRAB PNR>OBA @>I@RI>QBA?
3>JB >P J>DKFQRAB, ?RQ TFQELRQ QEB PNR>OB OLLQ. )K QEB @>PB LC > PFKDIB .3?=NIL L?GB@Q, QEFP
TFII KBSBO J>HB > PFDKFCF@>KQ AFCCBOBK@B LK > 0OL@BPPFKD PHBQ@E. (LTBSBO, FC VLR >OB
@LJMRQFKD QEB J>DKFQRAB LC QELRP>KAP LC .3?=NIL L?GB@QP B>@E QFJB QEOLRDE >L;Q(), RPFKD
G;A0K() FKPQB>A LC G;A() @LRIA EBIM VLRO @LAB ORK > TBB ?FQ C>PQBO. (.LQB: G;A0K() FP LKIV
>S>FI>?IB FK 0OL@BPPFKD 2.0>1 LO I>QBO.)
@FI;N G;A() U
L?NOLH MKLN(R*R + S*S);
W
C@ (P.G;A() > 10) U
// "I 0IG?NBCHA!
W
C@ (P.G;A0K() > 100) U
// "I 0IG?NBCHA!
W
@FI;N G;A0K() U
L?NOLH R*R + S*S;
W
2) Sine and cosine lookup tables 2) Sine and cosine lookup tables
4EBOBfP > M>QQBOK EBOB. 7E>Q HFKAP LC CRK@QFLKP >OB PILT QL @LJMRQB? 3NR>OB OLLQ. 3FKB.
#LPFKB. 4>KDBKQ. !D>FK, FC VLR GRPQ KBBA > PFKB LO @LPFKB S>IRB EBOB LO QEBOB FK VLRO @LAB,
VLR >OB KBSBO DLFKD QL ORK FKQL > MOL?IBJ. "RQ TE>Q FC VLR E>A PLJBQEFKD IFHB QEFP?
#E>MQBO 6. !RQLKLJLRP !DBKQP
318
3ROB, QEFP FP > QLQ>IIV OFAF@RILRP @LAB PKFMMBQ QE>Q VLR TLRIA KBSBO TOFQB. "RQ FQ FIIRPQO>QBP >
@BOQ>FK MLFKQ. )C VLR >OB @>I@RI>QFKD QEB PFKB LC MF QBK QELRP>KA QFJBP, TEV KLQ GRPQ @>I@RI>QB FQ
LK@B, P>SB QE>Q S>IRB, >KA OBCBO QL FQ TEBKBSBO KB@BPP>OV? 4EFP FP QEB MOFK@FMIB ?BEFKA PFKB
>KA @LPFKB ILLHRM Q>?IBP. )KPQB>A LC @>IIFKD QEB PFKB >KA @LPFKB CRK@QFLKP FK VLRO @LAB
TEBKBSBO VLR KBBA QEBJ, VLR @>K ?RFIA >K >OO>V QE>Q PQLOBP QEB OBPRIQP LC PFKB >KA @LPFKB >Q
>KDIBP ?BQTBBK 0 >KA 14-:.' >KA GRPQ ILLH RM QEB S>IRBP TEBK VLR KBBA QEBJ. &LO BU>JMIB,
EBOB >OB QTL >OO>VP QE>Q PQLOB QEB PFKB >KA @LPFKB S>IRBP CLO BSBOV >KDIB, 0 QL 359 ABDOBBP.
.LT, TE>Q FC VLR KBBA QEB S>IRB LC PFKB LC MF?
! JLOB PLMEFPQF@>QBA BU>JMIB LC QEFP QB@EKFNRB FP >S>FI>?IB LK QEB 0OL@BPPFKD TFHF
(EQQM://TFHF.MOL@BPPFKD.LOD/T/3FK/#LP=ILLH-RM=Q>?IB).
PIC> >L;Q() U
@IL (CHN C = 0; C < 10000; C++) U
JLCHNFH(MCH(.'));
W
W
@FI;N MCHP;FO?M89 = H?Q @FI;N83609;
@FI;N =IMP;FO?M89 = H?Q @FI;N83609;
@IL (CHN C = 0; C < 360; C++) U
MCHP;FO?M8C9 = MCH(L;>C;HM(C));
=IMP;FO?M8C9 = =IM(L;>C;HM(C));
W
CHN ;HAF? = CHN(>?AL??M(.'));
@FI;N ;HMQ?L = MCHP;FO?M8;HAF?9;
3) Making gajillions of unnecessary PVector objects 3) Making gajillions of unnecessary PVector objects
) E>SB QL >AJFQ, ) >J MBOE>MP QEB ?FDDBPQ @RIMOFQ LC QEFP I>PQ KLQB. )K C>@Q, FK QEB FKQBOBPQ LC
TOFQFKD @IB>O >KA RKABOPQ>KA>?IB BU>JMIBP, ) LCQBK @ELLPB QL J>HB BUQO> .3?=NIL L?GB@QP
TEBK ) >?PLIRQBIV AL KLQ KBBA QL. &LO QEB JLPQ M>OQ, QEFP FP KLQ > MOL?IBJ >Q >II. "RQ
PLJBQFJBP, FQ @>K ?B. ,BQfP Q>HB > ILLH >Q >K BU>JMIB.
,BQfP P>V LRO ALL;S*CMN LC SBEF@IBP E>P LKB QELRP>KA SBEF@IBP FK FQ. 7B GRPQ J>AB LKB
QELRP>KA KBT .3?=NIL L?GB@QP BSBOV PFKDIB QFJB QEOLRDE >L;Q(). .LT, LK >KV LIf I>MQLM LO
ABPHQLM @LJMRQBO VLRfSB MRO@E>PBA FK OB@BKQ QFJBP, VLRO PHBQ@E TFII IFHBIV KLQ OBDFPQBO >
@LJMI>FKQ, ORK PILTIV, LO E>SB >KV MOL?IBJP. !CQBO >II, VLRfSB DLQ QLKP LC 2!-, >KA *>S> TFII
PIC> >L;Q() U
@IL (3?BC=F? P : P?BC=F?M) U
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
P.M??E(GIOM?);
W
W
4EB .>QROB LC #LAB (S005)
319
?B >?IB QL E>KAIB J>HFKD > QELRP>KA LO PL QBJMLO>OV L?GB@QP >KA AFPMLPB LC QEBJ TFQELRQ
JR@E LC > MOL?IBJ.
)C VLRO KRJ?BOP DOLT I>ODBO (>KA QEBV B>PFIV @LRIA) LO MBOE>MP JLOB IFHBIV, FC VLR >OB
TLOHFKD TFQE 0OL@BPPFKD LK !KAOLFA, VLR TFII >IJLPQ @BOQ>FKIV ORK FKQL > MOL?IBJ. )K @>PBP
IFHB QEFP VLR T>KQ QL ILLH CLO T>VP QL OBAR@B QEB KRJ?BO LC .3?=NIL L?GB@QP VLR J>HB. !K
L?SFLRP CFU CLO QEB >?LSB @LAB FP:
.LT VLRfSB J>AB GRPQ LKB .3?=NIL FKPQB>A LC LKB QELRP>KA. %SBK ?BQQBO, VLR @LRIA QROK
QEB .3?=NIL FKQL > DIL?>I S>OF>?IB >KA GRPQ >PPFDK QEB R >KA S S>IRB:
.LT VLR KBSBO J>HB > KBT .3?=NIL; VLR RPB GRPQ LKB LSBO QEB IBKDQE LC VLRO PHBQ@E!
4EOLRDELRQ QEB ?LLHfP BU>JMIBP, VLR @>K CFKA ILQP LC LMMLOQRKFQFBP QL OBAR@B QEB KRJ?BO LC
QBJMLO>OV L?GB@QP. ,BQfP ILLH >Q LKB JLOB. (BOB FP > PKFMMBQ COLJ LRO M??E() CRK@QFLK.
3BB ELT TBfSB J>AB QTL .3?=NIL L?GB@QP? &FOPQ, TB CFDROB LRQ QEB ABPFOBA SB@QLO, QEBK TB
@>I@RI>QB QEB PQBBOFKD CLO@B. .LQF@B ELT TB @LRIA OBTOFQB QEFP QL @OB>QB LKIV LKB .3?=NIL.
PIC> >L;Q() U
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
@IL (3?BC=F? P : P?BC=F?M) U
P.M??E(GIOM?);
W
W
.3?=NIL GIOM? = H?Q .3?=NIL();
PIC> >L;Q() U
GIOM?.R = GIOM?5;
GIOM?.S = GIOM?6;
@IL (3?BC=F? P : P?BC=F?M) U
P.M??E(GIOM?);
W
W
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
>?MCL?>.HILG;FCT?();
>?MCL?>.GOFN(G;RMJ??>);
Create a new PVector to store the steering
force.
+0;9JEH IJ;;H = +0;9JEH.IK8(:;I?H;:,L;BE9?JO);
MN??L.FCGCN(G;R@IL=?);
L?NOLH MN??L;
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N, FI=;NCIH);
>?MCL?>.HILG;FCT?();
>?MCL?>.GOFN(G;RMJ??>);
#E>MQBO 6. !RQLKLJLRP !DBKQP
320
7B ALKfQ >@QR>IIV KBBA > PB@LKA .3?=NIL @>IIBA MN??L. 7B @LRIA GRPQ RPB QEB ABPFOBA
.3?=NIL L?GB@Q >KA QROK FQ FKQL QEB PQBBOFKD CLO@B ?V PR?QO>@QFKD SBIL@FQV. ) AFAKfQ AL QEFP FK JV
BU>JMIB ?B@>RPB FQ FP JLOB @LKCRPFKD QL OB>A. "RQ FK PLJB @>PBP, FQ J>V ?B DOB>QIV JLOB
BCCF@FBKQ.
Calculate the steering force in the desired
PVector.
:;I?H;:.IK8(L;BE9?JO);
>?MCL?>.FCGCN(G;R@IL=?);
L?NOLH >?MCL?>;
%IFJFK>QB >P J>KV QBJMLO>OV .3?=NIL L?GB@QP COLJ QEB CIL@HFKD BU>JMIB >P MLPPF?IB.
!IPL RPB G;A0K() TEBOB MLPPF?IB.
Exercise 6.20 Exercise 6.20
5PB PQBBOFKD ?BE>SFLOP TFQE "LU2$ LO QLUF@IF?P.
Exercise 6.21 Exercise 6.21
4EB .>QROB LC #LAB (S005)
321
The Ecosystem Project The Ecosystem Project
Step 6 Exercise:
Use the concept of steering forces to drive the behavior of the creatures in your
ecosystem. Some possibilities:
Create schools or flocks of creatures.
Use a seeking behavior for creatures to search for food (for chasing
moving prey, consider pursuit).
Use a flow field for the ecosystem environment. For example, how does
your system behave if the creatures live in a flowing river?
Build a creature with countless steering behaviors (as many as you can
reasonably add). Think about ways to vary the weights of these
behaviors so that you can dial those behaviors up and down, mixing and
matching on the fly. How are creatures initial weights set? What rules
drive how the weights change over time?
Complex systems can be nested. Can you design a single creature out of
a flock of boids? And can you then make a flock of those creatures?
Complex systems can have memory (and be adaptive). Can the history of
your ecosystem affect the behavior in its current state? (This could be
the driving force behind how the creatures adjust their steering force
weights.)
#E>MQBO 6. !RQLKLJLRP !DBKQP
322
Chapter 7. Cellular Chapter 7. Cellular
Automata Automata
I*< =9.F 9632 F<B :B@A 5.C2 . 3.6?9F 9.?42 052082?/<.?1 .;1 . =92;A63B9
@B==9F <3 39.A 0<B;A2?@ <3 AD< 0<9<?@. !A 6@ =<@@6/92 A< D<?8 D6A5 =2;069 .;1
4?.=5 =.=2? /BA 6A 6@ :B05 2.@62?, =.?A60B9.?9F 3<? /246;;2?@, A< B@2
0<B;A2?@ .;1 . /<.?1.J
Martin Gardner, .7=9BH=:=7 AA9F=75B (October 1970)
)K QEFP @E>MQBO, TBfOB DLFKD QL Q>HB > ?OB>H COLJ Q>IHFKD >?LRQ SB@QLOP >KA JLQFLK. )K C>@Q, QEB
OBPQ LC QEB ?LLH TFII JLPQIV CL@RP LK PVPQBJP >KA >IDLOFQEJP (>I?BFQ LKBP QE>Q TB @>K, PELRIA,
>KA TFII >MMIV QL JLSFKD ?LAFBP). )K QEB MOBSFLRP @E>MQBO, TB BK@LRKQBOBA LRO CFOPQ 0OL@BPPFKD
BU>JMIB LC > @LJMIBU PVPQBJ: CIL@HFKD. 7B ?OFBCIV PQ>QBA QEB @LOB MOFK@FMIBP ?BEFKA @LJMIBU
PVPQBJP: JLOB QE>K QEB PRJ LC FQP M>OQP, > @LJMIBU PVPQBJ FP > PVPQBJ LC BIBJBKQP, LMBO>QFKD
FK M>O>IIBI, TFQE PELOQ-O>KDB OBI>QFLKPEFMP QE>Q >P > TELIB BUEF?FQ BJBODBKQ ?BE>SFLO. 4EFP
BKQFOB @E>MQBO FP DLFKD QL ?B ABAF@>QBA QL ?RFIAFKD >KLQEBO @LJMIBU PVPQBJ PFJRI>QFLK FK
0OL@BPPFKD. /AAIV, TB >OB DLFKD QL Q>HB PLJB PQBMP ?>@HT>OA >KA PFJMIFCV QEB BIBJBKQP LC
LRO PVPQBJ. .L ILKDBO >OB QEB FKAFSFAR>I BIBJBKQP DLFKD QL ?B JBJ?BOP LC > MEVPF@P TLOIA;
FKPQB>A TB TFII ?RFIA > PVPQBJ LRQ LC QEB PFJMIBPQ AFDFQ>I BIBJBKQ MLPPF?IB, > PFKDIB ?FQ. 4EFP ?FQ
FP DLFKD QL ?B @>IIBA > @BII >KA FQP S>IRB (0 LO 1) TFII ?B @>IIBA FQP PQ>QB. 7LOHFKD TFQE PR@E
PFJMIB BIBJBKQP TFII EBIM RP RKABOPQ>KA JLOB LC QEB ABQ>FIP ?BEFKA ELT @LJMIBU PVPQBJP
TLOH, >KA TBfII >IPL ?B >?IB QL BI>?LO>QB LK PLJB MOLDO>JJFKD QB@EKFNRBP QE>Q TB @>K >MMIV
QL @LAB-?>PBA MOLGB@QP.
4EB .>QROB LC #LAB (S005)
323
7.1 What Is a Cellular Automaton? 7.1 What Is a Cellular Automaton?
&FOPQ, IBQfP DBQ LKB QEFKD PQO>FDEQ. 4EB QBOJ .077@7,= ,@?:8,?, .077@7,= ,@?:8,?, FP MIRO>I. /RO @LAB BU>JMIBP
TFII PFJRI>QB GRPQ LKBd> .077@7,= ,@?:8,?:9 .077@7,= ,@?:8,?:9, PFKDRI>O. 4L PFJMIFCV LRO IFSBP, TBfII >IPL OBCBO
QL @BIIRI>O >RQLJ>Q> >P g#!.h
)K #E>MQBOP 1 QEOLRDE 6, LRO L?GB@QP (JLSBO, M>OQF@IB, SBEF@IB, ?LFA) DBKBO>IIV BUFPQBA FK LKIV
LKB gPQ>QB.h 4EBV JFDEQ E>SB JLSBA >OLRKA TFQE >AS>K@BA ?BE>SFLOP >KA MEVPF@P, ?RQ
RIQFJ>QBIV QEBV OBJ>FKBA QEB P>JB QVMB LC L?GB@Q LSBO QEB @LROPB LC QEBFO AFDFQ>I IFCBQFJB.
7BfSB >IIRABA QL QEB MLPPF?FIFQV QE>Q QEBPB BKQFQFBP @>K @E>KDB LSBO QFJB (CLO BU>JMIB, QEB
TBFDEQP LC PQBBOFKD gABPFOBPh @>K S>OV), ?RQ TB E>SBKfQ CRIIV MRQ QEFP FKQL MO>@QF@B. )K QEFP
@LKQBUQ, @BIIRI>O >RQLJ>Q> J>HB > DOB>Q CFOPQ PQBM FK ?RFIAFKD > PVPQBJ LC J>KV L?GB@QP QE>Q
E>SB S>OVFKD PQ>QBP LSBO QFJB.
! @BIIRI>O >RQLJ>QLK FP > JLABI LC > PVPQBJ LC g@BIIh L?GB@QP TFQE QEB CLIILTFKD
@E>O>@QBOFPQF@P.
i 4EB @BIIP IFSB LK > 2=4/ 2=4/. (7BfII PBB BU>JMIBP FK ?LQE LKB >KA QTL AFJBKPFLKP FK
QEFP @E>MQBO, QELRDE > @BIIRI>O >RQLJ>QLK @>K BUFPQ FK >KV CFKFQB KRJ?BO LC
AFJBKPFLKP.)
i %>@E @BII E>P > >?,?0 >?,?0. 4EB KRJ?BO LC PQ>QB MLPPF?FIFQFBP FP QVMF@>IIV CFKFQB. 4EB
PFJMIBPQ BU>JMIB E>P QEB QTL MLPPF?FIFQFBP LC 1 >KA 0 (LQEBOTFPB OBCBOOBA QL >P gLKh
>KA gLCCh LO g>IFSBh >KA gAB>Ah).
i %>@E @BII E>P > 90423-:=3::/ 90423-:=3::/. 4EFP @>K ?B ABCFKBA FK >KV KRJ?BO LC T>VP, ?RQ FQ FP
QVMF@>IIV > IFPQ LC >AG>@BKQ @BIIP.
4EB ABSBILMJBKQ LC @BIIRI>O >RQLJ>Q> PVPQBJP FP QVMF@>IIV >QQOF?RQBA QL 3Q>KFP[>T 5I>J >KA
*LEK SLK .BRJ>KK, TEL TBOB ?LQE OBPB>O@EBOP >Q QEB ,LP !I>JLP .>QFLK>I ,>?LO>QLOV FK
F64B?2 7.1
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
324
.BT -BUF@L FK QEB 1940P. 5I>J T>P PQRAVFKD QEB DOLTQE LC @OVPQ>IP >KA SLK .BRJ>KK T>P
FJ>DFKFKD > TLOIA LC PBIC-OBMIF@>QFKD OL?LQP. 4E>QfP OFDEQ, OL?LQP QE>Q ?RFIA @LMFBP LC
QEBJPBISBP. /K@B TB PBB PLJB BU>JMIBP LC #! SFPR>IFWBA, FQfII ?B @IB>O ELT LKB JFDEQ
FJ>DFKB JLABIFKD @OVPQ>I DOLTQE; QEB OL?LQP FAB> FP MBOE>MP IBPP L?SFLRP. #LKPFABO QEB ABPFDK
LC > OL?LQ >P > M>QQBOK LK > DOFA LC @BIIP (QEFKH LC CFIIFKD FK PLJB PNR>OBP LK > MFB@B LC DO>ME
M>MBO). .LT @LKPFABO > PBQ LC PFJMIB ORIBP QE>Q TLRIA >IILT QE>Q M>QQBOK QL @OB>QB @LMFBP LC
FQPBIC LK QE>Q DOFA. 4EFP FP BPPBKQF>IIV QEB MOL@BPP LC > #! QE>Q BUEF?FQP ?BE>SFLO PFJFI>O QL
?FLILDF@>I OBMOLAR@QFLK >KA BSLIRQFLK. ()K@FABKQ>IIV, SLK .BRJ>KKfP @BIIP E>A QTBKQV-KFKB
MLPPF?IB PQ>QBP.) 6LK .BRJ>KKfP TLOH FK PBIC-OBMIF@>QFLK >KA #! FP @LK@BMQR>IIV PFJFI>O QL
TE>Q FP MOL?>?IV QEB JLPQ C>JLRP @BIIRI>O >RQLJ>QLK: QEB g'>JB LC ,FCB,h TEF@E TB TFII
AFP@RPP FK ABQ>FI FK PB@QFLK 7.3.
0BOE>MP QEB JLPQ PFDKFCF@>KQ P@FBKQFCF@ (>KA IBKDQEV) TLOH PQRAVFKD @BIIRI>O >RQLJ>Q> >OOFSBA FK
2002: 3QBMEBK 7LICO>JfP 1,280-M>DB A *9K '=B8 C: .7=9B79 (EQQM://TTT.TLICO>JP@FBK@B.@LJ/
KHPLKIFKB/QL@.EQJI). !S>FI>?IB FK FQP BKQFOBQV CLO COBB LKIFKB, 7LICO>JfP ?LLH AFP@RPPBP ELT #!
>OB KLQ PFJMIV KB>Q QOF@HP, ?RQ >OB OBIBS>KQ QL QEB PQRAV LC ?FLILDV, @EBJFPQOV, MEVPF@P, >KA >II
?O>K@EBP LC P@FBK@B. 4EFP @E>MQBO TFII ?>OBIV P@O>Q@E QEB PROC>@B LC QEB QEBLOFBP 7LICO>J
LRQIFKBP (TB TFII CL@RP LK QEB @LAB FJMIBJBKQ>QFLK) PL FC QEB BU>JMIBP MOLSFABA PM>OH VLRO
@ROFLPFQV, VLRfII CFKA MIBKQV JLOB QL OB>A >?LRQ FK EFP ?LLH.
7.2 Elementary Cellular Automata 7.2 Elementary Cellular Automata
4EB BU>JMIBP FK QEFP @E>MQBO TFII ?BDFK TFQE > PFJRI>QFLK LC 7LICO>JfP TLOH. 4L RKABOPQ>KA
7LICO>JfP BIBJBKQ>OV #!, TB PELRIA >PH LROPBISBP QEB NRBPQFLK: g7E>Q FP QEB PFJMIBPQ @BIIRI>O
>RQLJ>QLK TB @>K FJ>DFKB?h 7E>QfP BU@FQFKD >?LRQ QEFP NRBPQFLK >KA FQP >KPTBO FP QE>Q BSBK
TFQE QEB PFJMIBPQ #! FJ>DFK>?IB, TB TFII PBB QEB MOLMBOQFBP LC @LJMIBU PVPQBJP >Q TLOH.
,BQfP ?RFIA 7LICO>JfP BIBJBKQ>OV #! COLJ P@O>Q@E. #LK@BMQP CFOPQ, QEBK @LAB. 7E>Q >OB QEB
QEOBB HBV BIBJBKQP LC > #!?
1) G=4/ G=4/. 4EB PFJMIBPQ DOFA TLRIA ?B LKB-AFJBKPFLK>I: > IFKB LC @BIIP.
2) '?,?0> '?,?0>. 4EB PFJMIBPQ PBQ LC PQ>QBP (?BVLKA E>SFKD LKIV LKB PQ>QB) TLRIA ?B QTL PQ>QBP: 0 LO
1.
F64B?2 7.2
F64B?2 7.3
4EB .>QROB LC #LAB (S005)
325
3) #0423-:=3::/ #0423-:=3::/. 4EB PFJMIBPQ KBFDE?LOELLA FK LKB AFJBKPFLK CLO >KV DFSBK @BII TLRIA ?B
QEB @BII FQPBIC >KA FQP QTL >AG>@BKQ KBFDE?LOP: LKB QL QEB IBCQ >KA LKB QL QEB OFDEQ.
3L TB ?BDFK TFQE > IFKB LC @BIIP, B>@E TFQE >K FKFQF>I PQ>QB (IBQfP P>V FQ FP O>KALJ), >KA B>@E
TFQE QTL KBFDE?LOP. 7BfII E>SB QL CFDROB LRQ TE>Q TB T>KQ QL AL TFQE QEB @BIIP LK QEB BADBP
(PFK@B QELPB E>SB LKIV LKB KBFDE?LO B>@E), ?RQ QEFP FP PLJBQEFKD TB @>K PLOQ LRQ I>QBO.
7B E>SBKfQ VBQ AFP@RPPBA, ELTBSBO, TE>Q FP MBOE>MP QEB JLPQ FJMLOQ>KQ ABQ>FI LC ELT
@BIIRI>O >RQLJ>Q> TLOHdH=A9. 7BfOB KLQ OB>IIV Q>IHFKD >?LRQ OB>I-TLOIA QFJB EBOB, ?RQ >?LRQ
QEB #! IFSFKD LSBO > MBOFLA LC H=A9, TEF@E @LRIA >IPL ?B @>IIBA > 2090=,?4:9 2090=,?4:9 >KA, FK LRO
@>PB, TFII IFHBIV OBCBO QL QEB 1=,80 .:@9? 1=,80 .:@9? LC >K >KFJ>QFLK. 4EB CFDROBP >?LSB PELT RP QEB #!
>Q QFJB BNR>IP 0 LO DBKBO>QFLK 0. 4EB NRBPQFLKP TB E>SB QL >PH LROPBISBP >OB: $CK 8C K9
7CADIH9 H<9 GH5H9G :CF 5@@ 79@@G 5H ;9B9F5H=CB 1? AB8 ;9B9F5H=CB 2? !KA PL LK >KA PL CLOQE.
,BQfP P>V TB E>SB >K FKAFSFAR>I @BII FK QEB #!, >KA IBQfP @>II FQ #%,,. 4EB CLOJRI> CLO
@>I@RI>QFKD #%,,fP PQ>QB >Q >KV DFSBK QFJB N FP >P CLIILTP:
C!'' IJ7J; 7J J?C; J = <(C!'' D;?=>8EH>EE: 7J J?C; J - 1)
)K LQEBO TLOAP, > @BIIfP KBT PQ>QB FP > CRK@QFLK LC >II QEB PQ>QBP FK QEB @BIIfP KBFDE?LOELLA >Q
QEB MOBSFLRP JLJBKQ FK QFJB (LO AROFKD QEB MOBSFLRP DBKBO>QFLK). 7B @>I@RI>QB > KBT PQ>QB
S>IRB ?V ILLHFKD >Q >II QEB MOBSFLRP KBFDE?LO PQ>QBP.
F64B?2 7.4: A ;2645/<?5<<1 6@ A5?22 0299@.
F64B?2 7.5: *52 2142 0299 <;9F 5.@ . ;2645/<?5<<1 <3 AD<.
F64B?2 7.6
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
326
.LT, FK QEB TLOIA LC @BIIRI>O >RQLJ>Q>, QEBOB >OB J>KV T>VP TB @LRIA @LJMRQB > @BIIfP PQ>QB
COLJ > DOLRM LC @BIIP. #LKPFABO ?IROOFKD >K FJ>DB. ('RBPP TE>Q? )J>DB MOL@BPPFKD TLOHP TFQE
#!-IFHB ORIBP.) ! MFUBIfP KBT PQ>QB (F.B. FQP @LILO) FP QEB >SBO>DB LC >II LC FQP KBFDE?LOPf @LILOP.
7B @LRIA >IPL P>V QE>Q > @BIIfP KBT PQ>QB FP QEB PRJ LC >II LC FQP KBFDE?LOPf PQ>QBP. 7FQE
7LICO>JfP BIBJBKQ>OV #!, ELTBSBO, TB @>K >@QR>IIV AL PLJBQEFKD > ?FQ PFJMIBO >KA PBBJFKDIV
>?PROA: 7B @>K ILLH >Q >II QEB MLPPF?IB @LKCFDRO>QFLKP LC > @BII >KA FQP KBFDE?LO >KA ABCFKB QEB
PQ>QB LRQ@LJB CLO BSBOV MLPPF?IB @LKCFDRO>QFLK. )Q PBBJP OFAF@RILRPdTLRIAKfQ QEBOB ?B T>V QLL
J>KV MLPPF?FIFQFBP CLO QEFP QL ?B MO>@QF@>I? ,BQfP DFSB FQ > QOV.
7B E>SB QEOBB @BIIP, B>@E TFQE > PQ>QB LC 0 LO 1. (LT J>KV MLPPF?IB T>VP @>K TB @LKCFDROB
QEB PQ>QBP? )C VLR ILSB ?FK>OV, VLRfII KLQF@B QE>Q QEOBB @BIIP ABCFKB > 3-?FQ KRJ?BO, >KA ELT EFDE
@>K VLR @LRKQ TFQE 3 ?FQP? 5M QL 8. ,BQfP E>SB > ILLH.
/K@B TB E>SB ABCFKBA >II QEB MLPPF?IB KBFDE?LOELLAP, TB KBBA QL ABCFKB >K LRQ@LJB (KBT
PQ>QB S>IRB: 0 LO 1) CLO B>@E KBFDE?LOELLA @LKCFDRO>QFLK.
4EB PQ>KA>OA 7LICO>J JLABI FP QL PQ>OQ DBKBO>QFLK 0 TFQE >II @BIIP E>SFKD > PQ>QB LC 0 BU@BMQ
CLO QEB JFAAIB @BII, TEF@E PELRIA E>SB > PQ>QB LC 1.
F64B?2 7.7
F64B?2 7.8
F64B?2 7.9
F64B?2 7.10
4EB .>QROB LC #LAB (S005)
327
2BCBOOFKD QL QEB ORIBPBQ >?LSB, IBQfP PBB ELT > DFSBK @BII (TBfII MF@H QEB @BKQBO LKB) TLRIA
@E>KDB COLJ DBKBO>QFLK 0 QL DBKBO>QFLK 1.
4OV >MMIVFKD QEB P>JB ILDF@ QL >II LC QEB @BIIP >?LSB >KA CFII FK QEB BJMQV @BIIP.
.LT, IBQfP DL M>PQ GRPQ LKB DBKBO>QFLK >KA @LILO QEB @BIIP d0 JB>KP TEFQB, 1 JB>KP
?I>@Hd>KA PQ>@H QEB DBKBO>QFLKP, TFQE B>@E KBT DBKBO>QFLK >MMB>OFKD ?BILT QEB MOBSFLRP
LKB.
4EB ILT-OBPLIRQFLK PE>MB TBfOB PBBFKD >?LSB FP QEB g3FBOMF\PHF QOF>KDIB.h .>JBA >CQBO QEB
0LIFPE J>QEBJ>QF@F>K 7>@[>T 3FBOMF\PHF, FQfP > CO>@Q>I M>QQBOK QE>Q TBfII BU>JFKB FK QEB KBUQ
@E>MQBO. 4E>QfP OFDEQ: QEFP FK@OBAF?IV PFJMIB PVPQBJ LC 0P >KA 1P, TFQE IFQQIB KBFDE?LOELLAP LC
QEOBB @BIIP, @>K DBKBO>QB > PE>MB >P PLMEFPQF@>QBA >KA ABQ>FIBA >P QEB 3FBOMF\PHF QOF>KDIB.
,BQfP ILLH >Q FQ >D>FK, LKIV TFQE B>@E @BII > PFKDIB MFUBI TFAB PL QE>Q QEB OBPLIRQFLK FP JR@E
EFDEBO.
F64B?2 7.11
F64B?2 7.12: (B92 90
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
328
4EFP M>OQF@RI>O OBPRIQ AFAKfQ E>MMBK ?V >@@FABKQ. ) MF@HBA QEFP PBQ LC ORIBP ?B@>RPB LC QEB
M>QQBOK FQ DBKBO>QBP. 4>HB > ILLH >Q &FDROB 7.8 LKB JLOB QFJB. .LQF@B ELT QEBOB >OB BFDEQ
MLPPF?IB KBFDE?LOELLA @LKCFDRO>QFLKP; TB QEBOBCLOB ABCFKB > gORIBPBQh >P > IFPQ LC 8 ?FQP.
3L QEFP M>OQF@RI>O ORIB @>K ?B FIIRPQO>QBA >P CLIILTP:
%FDEQ 0P >KA 1P JB>KP >K 8-?FQ KRJ?BO. (LT J>KV @LJ?FK>QFLKP LC BFDEQ 0P >KA 1P >OB QEBOB?
256. 4EFP FP GRPQ IFHB ELT TB ABCFKB QEB @LJMLKBKQP LC >K 2'" @LILO. 7B DBQ 8 ?FQP CLO OBA,
DOBBK, >KA ?IRB, JB>KFKD TB J>HB @LILOP TFQE S>IRBP COLJ 0 QL 255 (256 MLPPF?FIFQFBP).
)K QBOJP LC > 7LICO>J BIBJBKQ>OV #!, TB E>SB KLT AFP@LSBOBA QE>Q QEBOB >OB 256 MLPPF?IB
ORIBPBQP. 4EB >?LSB ORIBPBQ FP @LJJLKIV OBCBOOBA QL >P g2RIB 90h ?B@>RPB FC VLR @LKSBOQ QEB
?FK>OV PBNRBK@Bd01011010dQL > AB@FJ>I KRJ?BO, VLRfII DBQ QEB FKQBDBO 90. ,BQfP QOV ILLHFKD >Q
QEB OBPRIQP LC >KLQEBO ORIBPBQ.
F64B?2 7.13: (B92 90
F64B?2 7.14: (B92 90
4EB .>QROB LC #LAB (S005)
329
!P TB @>K KLT PBB, QEB PFJMIB >@Q LC
@OB>QFKD > #! >KA ABCFKFKD > ORIBPBQ ALBP
KLQ DR>O>KQBB SFPR>IIV FKQBOBPQFKD OBPRIQP.
/RQ LC >II 256 ORIBPBQP, LKIV > E>KACRI
MOLAR@B @LJMBIIFKD LRQ@LJBP. (LTBSBO,
FQfP NRFQB FK@OBAF?IB QE>Q BSBK LKB LC QEBPB
ORIBPBQP CLO > LKB-AFJBKPFLK>I #! TFQE LKIV
QTL MLPPF?IB PQ>QBP @>K MOLAR@B QEB
M>QQBOKP TB PBB BSBOV A>V FK K>QROB (PBB
&FDROB 7.16), >KA FQ ABJLKPQO>QBP ELT
S>IR>?IB QEBPB PVPQBJP @>K ?B FK
PFJRI>QFLK >KA M>QQBOK DBKBO>QFLK.
"BCLOB TB DL QLL C>O ALTK QEB OL>A LC ELT
7LICO>J @I>PPFCFBP QEB OBPRIQP LC S>OVFKD
ORIBPBQP, IBQfP ILLH >Q ELT TB >@QR>IIV ?RFIA
> 0OL@BPPFKD PHBQ@E QE>Q DBKBO>QBP QEB
7LICO>J #! >KA SFPR>IFWBP FQ LKP@OBBK.
F64B?2 7.15: (B92 222
F64B?2 7.16: A *2EA692 C<;2 );.69 (C<;B@ A2EA692),
C<1 <92, G?2.A B.??62? (223, AB@A?.96., 7
AB4B@A 2005. '5<A<4?.=52?: (605.?1 #6;4
?605.?1@?2@2.?05.0.;<;.0<:..B
7.3 How to Program an Elementary CA 7.3 How to Program an Elementary CA
9LR J>V ?B QEFKHFKD: g/+, )fSB DLQ QEFP @BII QEFKD. !KA QEB @BII QEFKD E>P PLJB MOLMBOQFBP,
IFHB > PQ>QB, TE>Q DBKBO>QFLK FQfP LK, TEL FQP KBFDE?LOP >OB, TEBOB FQ IFSBP MFUBI-TFPB LK QEB
P@OBBK. !KA J>V?B FQ E>P PLJB CRK@QFLKP: FQ @>K AFPMI>V FQPBIC, FQ @>K DBKBO>QB FQP KBT PQ>QB,
BQ@.h 4EFP IFKB LC QEFKHFKD FP >K BU@BIIBKQ LKB >KA TLRIA IFHBIV IB>A VLR QL TOFQB PLJB @LAB
IFHB QEFP:
=F;MM !?FF U
W
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
330
4EFP IFKB LC QEFKHFKD, ELTBSBO, FP KLQ QEB OL>A TB TFII CFOPQ QO>SBI. ,>QBO FK QEFP @E>MQBO, TB TFII
AFP@RPP TEV >K L?GB@Q-LOFBKQBA >MMOL>@E @LRIA MOLSB S>IR>?IB FK ABSBILMFKD > #! PFJRI>QFLK,
?RQ QL ?BDFK, TB @>K TLOH TFQE > JLOB BIBJBKQ>OV A>Q> PQOR@QROB. !CQBO >II, TE>Q FP >K
BIBJBKQ>OV #! ?RQ > IFPQ LC 0P >KA 1P? #BOQ>FKIV, TB @LRIA ABP@OF?B QEB CLIILTFKD #!
DBKBO>QFLK RPFKD >K >OO>V:
4L AO>T QE>Q >OO>V, TB PFJMIV @EB@H FC TBfSB DLQ > 0 LO > 1 >KA @OB>QB > CFII >@@LOAFKDIV.
.LT QE>Q TB E>SB QEB >OO>V QL ABP@OF?B QEB @BII PQ>QBP LC > DFSBK DBKBO>QFLK (TEF@E TBfII
RIQFJ>QBIV @LKPFABO QEB g@ROOBKQh DBKBO>QFLK), TB KBBA > JB@E>KFPJ ?V TEF@E QL @LJMRQB QEB
KBUQ DBKBO>QFLK. ,BQfP QEFKH >?LRQ QEB MPBRAL@LAB LC TE>Q TB >OB ALFKD >Q QEB JLJBKQ.
For every cell in the array: For every cell in the array:
i (,60 , 7::6 ,? ?30 90423-:=3::/ >?,?0>: 701?, 84//70, =423?. (,60 , 7::6 ,? ?30 90423-:=3::/ >?,?0>: 701?, 84//70, =423?.
i !::6 @; ?30 90B A,7@0 1:= ?30 .077 >?,?0 ,..:=/492 ?: >:80 =@70>0?. !::6 @; ?30 90B A,7@0 1:= ?30 .077 >?,?0 ,..:=/492 ?: >:80 =@70>0?.
i '0? ?30 .077H> >?,?0 ?: ?3,? 90B A,7@0. '0? ?30 .077H> >?,?0 ?: ?3,? 90B A,7@0.
4EFP J>V IB>A VLR QL TOFQB PLJB @LAB IFHB QEFP:
F64B?2 7.17
CHN89 =?FFM = U1,0,1,0,0,0,0,1,0,1,1,1,0,0,0,1,1,1,0,0W;
Loop through every cell. @IL (CHN C = 0; C < =?FFM.F?HANB; C++) U
C@ (=?FFM8C9 == 0) @CFF(255);
Create a fill based on its state (0 or 1). ?FM? @CFF(0);
MNLIE?(0);
L?=N(C*50,0,50,50);
W
For every cell in the array... @IL (CHN C = 0; C < =?FFM.F?HANB; C++) U
...take a look at the neighborhood. CHN F?@N = =?FF8C-19;
CHN GC>>F? = =?FF8C9;
CHN LCABN = =?FF8C+19;
Look up the new value according to the
rules.
CHN H?QMN;N? = LOF?M(F?@N,GC>>F?,LCABN);
4EB .>QROB LC #LAB (S005)
331
7BfOB C>FOIV @ILPB QL DBQQFKD QEFP OFDEQ, ?RQ TBfSB J>AB LKB JFKLO ?IRKABO >KA LKB J>GLO
?IRKABO FK QEB >?LSB @LAB. ,BQfP Q>IH >?LRQ TE>Q TBfSB ALKB TBII PL C>O.
.LQF@B ELT B>PV FQ FP QL ILLH >Q > @BIIfP KBFDE?LOP. "B@>RPB >K >OO>V FP >K LOABOBA IFPQ LC
A>Q>, TB @>K RPB QEB C>@Q QE>Q QEB FKAF@BP >OB KRJ?BOBA QL HKLT TEF@E @BIIP >OB KBUQ QL
TEF@E @BIIP. 7B HKLT QE>Q @BII KRJ?BO 15, CLO BU>JMIB, E>P @BII 14 QL FQP IBCQ >KA 16 QL FQP
OFDEQ. -LOB DBKBO>IIV, TB @>K P>V QE>Q CLO >KV @BII C, FQP KBFDE?LOP >OB C-1 >KA C+1.
7BfOB >IPL C>OJFKD LRQ QEB @>I@RI>QFLK LC > KBT PQ>QB S>IRB QL PLJB CRK@QFLK @>IIBA LOF?M().
/?SFLRPIV, TBfOB DLFKD QL E>SB QL TOFQB QEFP CRK@QFLK LROPBISBP, ?RQ QEB MLFKQ TBfOB J>HFKD
EBOB FP JLARI>OFQV. 7B E>SB > ?>PF@ CO>JBTLOH CLO QEB #! FK QEFP CRK@QFLK, >KA FC TB I>QBO
T>KQ QL @E>KDB ELT QEB ORIBP LMBO>QB, TB ALKfQ E>SB QL QLR@E QE>Q CO>JBTLOH; TB @>K
PFJMIV OBTOFQB QEB LOF?M() CRK@QFLK QL @LJMRQB QEB KBT PQ>QBP AFCCBOBKQIV.
3L TE>Q E>SB TB ALKB TOLKD? ,BQfP Q>IH QEOLRDE ELT QEB @LAB TFII BUB@RQB. &FOPQ, TB ILLH
>Q @BII FKABU C BNR>IP 0. .LT IBQfP ILLH >Q 0fP KBFDE?LOP. ,BCQ FP FKABU -1. -FAAIB FP FKABU 0.
!KA OFDEQ FP FKABU 1. (LTBSBO, LRO >OO>V ?V ABCFKFQFLK ALBP KLQ E>SB >K BIBJBKQ TFQE QEB
FKABU -1. )Q PQ>OQP TFQE 0. 4EFP FP > MOL?IBJ TBfSB >IIRABA QL ?BCLOB: QEB BADB @>PBP.
(LT AL TB AB>I TFQE QEB @BIIP LK QEB BADB TEL ALKfQ E>SB > KBFDE?LO QL ?LQE QEBFO IBCQ >KA
QEBFO OFDEQ? (BOB >OB QEOBB MLPPF?IB PLIRQFLKP QL QEFP MOL?IBJ:
1. E/20> =08,49 .:9>?,9?. E/20> =08,49 .:9>?,9?. 4EFP FP MBOE>MP QEB PFJMIBPQ PLIRQFLK. 7B KBSBO ?LQEBO QL
BS>IR>QB QEB BADBP >KA >IT>VP IB>SB QEBFO PQ>QB S>IRB @LKPQ>KQ (0 LO 1).
2. E/20> B=,; ,=:@9/. E/20> B=,; ,=:@9/. 4EFKH LC QEB #! >P > PQOFM LC M>MBO >KA QROK QE>Q PQOFM LC
M>MBO FKQL > OFKD. 4EB @BII LK QEB IBCQ BADB FP > KBFDE?LO LC QEB @BII LK QEB OFDEQ
>KA SF@B SBOP>. 4EFP @>K @OB>QB QEB >MMB>O>K@B LC >K FKCFKFQB DOFA >KA FP MOL?>?IV
QEB JLPQ RPBA PLIRQFLK.
3. E/20> 3,A0 /4110=09? 90423-:=3::/> ,9/ =@70>. E/20> 3,A0 /4110=09? 90423-:=3::/> ,9/ =@70>. )C TB T>KQBA QL, TB @LRIA QOB>Q
QEB BADB @BIIP AFCCBOBKQIV >KA @OB>QB ORIBP CLO @BIIP QE>Q E>SB > KBFDE?LOELLA LC
QTL FKPQB>A LC QEOBB. 9LR J>V T>KQ QL AL QEFP FK PLJB @FO@RJPQ>K@BP, ?RQ FK LRO
@>PB, FQfP DLFKD QL ?B > ILQ LC BUQO> IFKBP LC @LAB CLO IFQQIB ?BKBCFQ.
4L J>HB QEB @LAB B>PFBPQ QL OB>A >KA RKABOPQ>KA OFDEQ KLT, TBfII DL TFQE LMQFLK #1 >KA GRPQ
PHFM QEB BADB @>PBP, IB>SFKD QEBFO S>IRBP @LKPQ>KQ. 4EFP @>K ?B >@@LJMIFPEBA ?V PQ>OQFKD QEB
ILLM LKB @BII I>QBO >KA BKAFKD LKB @BII B>OIFBO:
Set the cells state to the new value. =?FF8C9 = H?QMN;N?;
W
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
332
4EBOBfP LKB JLOB MOL?IBJ TB E>SB QL CFU ?BCLOB TBfOB ALKB. )QfP PR?QIB >KA VLR TLKfQ DBQ >
@LJMFI>QFLK BOOLO; QEB #! GRPQ TLKfQ MBOCLOJ @LOOB@QIV. (LTBSBO, FABKQFCVFKD QEFP MOL?IBJ FP
>?PLIRQBIV CRKA>JBKQ>I QL QEB QB@EKFNRBP ?BEFKA MOLDO>JJFKD #! PFJRI>QFLKP. )Q >II IFBP FK QEFP
IFKB LC @LAB:
4EFP PBBJP IFHB > MBOCB@QIV FKKL@BKQ IFKB. !CQBO >II, TBfSB @LJMRQBA QEB KBT PQ>QB S>IRB >KA
TBfOB PFJMIV DFSFKD QEB @BII FQP KBT PQ>QB. "RQ FK QEB KBUQ FQBO>QFLK, VLRfII AFP@LSBO > J>PPFSB
?RD. ,BQfP P>V TBfSB GRPQ @LJMRQBA QEB KBT PQ>QB CLO @BII #5. 7E>Q AL TB AL KBUQ? 7B
@>I@RI>QB QEB KBT PQ>QB S>IRB CLO @BII #6.
C9@@ #6, ;9B9F5H=CB 0 = GCA9 GH5H9, 0 CF 1
C9@@ #6, ;9B9F5H=CB 1 = 5 :IB7H=CB C: GH5H9G :CF .077 #5 .077 #5, 79@@ #6, 5B8 79@@ #7 5H *;9B9F5H=CB 0*
.LQF@B ELT TB KBBA QEB S>IRB LC @BII #5 >Q DBKBO>QFLK 0 FK LOABO QL @>I@RI>QB @BII #6fP KBT
PQ>QB >Q DBKBO>QFLK 1? ! @BIIfP KBT PQ>QB FP > CRK@QFLK LC QEB MOBSFLRP KBFDE?LO PQ>QBP. $L TB
HKLT @BII #5fP S>IRB >Q DBKBO>QFLK 0? 2BJBJ?BO, 0OL@BPPFKD GRPQ BUB@RQBP QEFP IFKB LC @LAB
CLO = = 5.
/K@B QEFP E>MMBKP, TB KL ILKDBO E>SB >@@BPP QL @BII #5fP PQ>QB >Q DBKBO>QFLK 0, >KA @BII FKABU
5 FP PQLOFKD QEB S>IRB CLO DBKBO>QFLK 1. 7B @>KKLQ LSBOTOFQB QEB S>IRBP FK QEB >OO>V TEFIB TB
>OB MOL@BPPFKD QEB >OO>V, ?B@>RPB TB KBBA QELPB S>IRBP QL @>I@RI>QB QEB KBT S>IRBP. !
PLIRQFLK QL QEFP MOL?IBJ FP QL E>SB QTL >OO>VP, LKB QL PQLOB QEB @ROOBKQ DBKBO>QFLK PQ>QBP >KA
LKB CLO QEB KBUQ DBKBO>QFLK PQ>QBP.
A loop that ignores the first and last cell <EH (?DJ ? = 1; ? < 9;BBI.B;D=J>-1; ?++) Q
CHN F?@N = =?FF8C-19;
CHN GC>>F? = =?FF8C9;
CHN LCABN = =?FF8C+19;
CHN H?QMN;N? = LOF?M(F?@N,GC>>F?,LCABN);
=?FF8C9 = H?QMN;N?;
W
=?FF8C9 = H?QMN;N?;
=?FF8C9 = H?QMN;N?;
Another array to store the states for the next
generation.
?DJ45 D;M9;BBI = D;M ?DJ49;BBI.B;D=J>5;
@IL (CHN C = 1; C < =?FFM.F?HANB-1; C++) U
Look at the states from the current array. CHN F?@N = =?FF8C-19;
CHN GC>>F? = =?FF8C9;
CHN LCABN = =?FF8C+19;
CHN H?QMN;N? = LOF?M(F?@N,GC>>F?,LCABN);
Saving the new state in the new array D;M9;BBI4?5 = D;MIJ7J;;
W
4EB .>QROB LC #LAB (S005)
333
/K@B QEB BKQFOB >OO>V LC S>IRBP FP MOL@BPPBA, TB @>K QEBK AFP@>OA QEB LIA >OO>V >KA PBQ FQ
BNR>I QL QEB KBT >OO>V LC PQ>QBP.
7BfOB >IJLPQ ALKB. 4EB >?LSB @LAB FP @LJMIBQB BU@BMQ CLO QEB C>@Q QE>Q TB E>SBKfQ VBQ
TOFQQBK QEB LOF?M() CRK@QFLK QE>Q @LJMRQBP QEB KBT PQ>QB S>IRB ?>PBA LK QEB KBFDE?LOELLA
(IBCQ, JFAAIB, >KA OFDEQ @BIIP). 7B HKLT QE>Q CRK@QFLK KBBAP QL OBQROK >K FKQBDBO (0 LO 1) >P
TBII >P OB@BFSB QEOBB >ODRJBKQP (CLO QEB QEOBB KBFDE?LOP).
.LT, QEBOB >OB J>KV T>VP TB @LRIA TOFQB QEFP CRK@QFLK, ?RQ )fA IFHB QL PQ>OQ TFQE > ILKD-
TFKABA LKB QE>Q TFII ELMBCRIIV MOLSFAB > @IB>O FIIRPQO>QFLK LC TE>Q TB >OB ALFKD.
,BQfP CFOPQ BPQ>?IFPE ELT TB >OB PQLOFKD QEB ORIBPBQ. 4EB ORIBPBQ, FC VLR OBJBJ?BO COLJ QEB
MOBSFLRP PB@QFLK, FP > PBOFBP LC 8 ?FQP (0 LO 1) QE>Q ABCFKBP QE>Q LRQ@LJB CLO BSBOV MLPPF?IB
KBFDE?LOELLA @LKCFDRO>QFLK.
7B @>K PQLOB QEFP ORIBPBQ FK 0OL@BPPFKD >P >K >OO>V.
!KA QEBK P>V:
)C IBCQ, JFAAIB, >KA OFDEQ >II E>SB QEB PQ>QB 1, QEBK QE>Q J>Q@EBP QEB @LKCFDRO>QFLK 111 >KA QEB
KBT PQ>QB PELRIA ?B BNR>I QL QEB CFOPQ S>IRB FK QEB ORIBPBQ >OO>V. 7B @>K KLT ARMIF@>QB QEFP
PQO>QBDV CLO >II BFDEQ MLPPF?FIFQFBP.
The new generation becomes the current
generation.
9;BBI = D;M9;BBI;
Function receives 3 ints and returns 1. CHN LOF?M (CHN ;, CHN <, CHN =) U
F64B?2 7.14 (?2=2.A21)
CHN89 LOF?M?N = U0,1,0,1,1,0,1,0W;
C@ (; == 1 && < == 1 && = == 1) L?NOLH LOF?M?N809;
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
334
) IFHB E>SFKD QEB BU>JMIB TOFQQBK >P >?LSB ?B@>RPB FQ ABP@OF?BP IFKB ?V IFKB BU>@QIV TE>Q FP
E>MMBKFKD CLO B>@E KBFDE?LOELLA @LKCFDRO>QFLK. (LTBSBO, FQfP KLQ > DOB>Q PLIRQFLK. !CQBO >II,
TE>Q FC TB ABPFDK > #! QE>Q E>P 4 MLPPF?IB PQ>QBP (0-3) >KA PRAABKIV TB E>SB 64 MLPPF?IB
KBFDE?LOELLA @LKCFDRO>QFLKP? 7FQE 10 MLPPF?IB PQ>QBP, TB E>SB 1,000 @LKCFDRO>QFLKP. #BOQ>FKIV
TB ALKfQ T>KQ QL QVMB FK 1,000 IFKBP LC @LAB!
!KLQEBO PLIRQFLK, QELRDE MBOE>MP > ?FQ JLOB AFCCF@RIQ QL CLIILT, FP QL @LKSBOQ QEB KBFDE?LOELLA
@LKCFDRO>QFLK (> 3-?FQ KRJ?BO) FKQL > OBDRI>O FKQBDBO >KA RPB QE>Q S>IRB >P QEB FKABU FKQL QEB
ORIBPBQ >OO>V. 4EFP @>K ?B ALKB FK *>S> IFHB PL.
4EBOBfP LKB QFKV MOL?IBJ TFQE QEFP PLIRQFLK, ELTBSBO. ,BQfP P>V TB >OB FJMIBJBKQFKD ORIB 222:
!KA TB E>SB QEB KBFDE?LOELLA g111h. 4EB OBPRIQFKD PQ>QB FP BNR>I QL ORIBPBQ FKABU 0, >P TB PBB
FK QEB CFOPQ T>V TB TOLQB QEB CRK@QFLK.
)C TB @LKSBOQ g111h QL > AB@FJ>I KRJ?BO, TB DBQ 7. "RQ TB ALKfQ T>KQ ORIBPBQ;7<; TB T>KQ
ORIBPBQ;0<. &LO QEFP QL TLOH, TB KBBA QL TOFQB QEB ORIBPBQ TFQE QEB ?FQP FK OBSBOPB LOABO, F.B.
CHN LOF?M (CHN ;, CHN <, CHN =) U
C@ (; == 1 && < == 1 && = == 1) L?NOLH LOF?M?N809;
?FM? C@ (; == 1 && < == 1 && = == 0) L?NOLH LOF?M?N819;
?FM? C@ (; == 1 && < == 0 && = == 1) L?NOLH LOF?M?N829;
?FM? C@ (; == 1 && < == 0 && = == 0) L?NOLH LOF?M?N839;
?FM? C@ (; == 0 && < == 1 && = == 1) L?NOLH LOF?M?N849;
?FM? C@ (; == 0 && < == 1 && = == 0) L?NOLH LOF?M?N859;
?FM? C@ (; == 0 && < == 0 && = == 1) L?NOLH LOF?M?N869;
?FM? C@ (; == 0 && < == 0 && = == 0) L?NOLH LOF?M?N879;
For this function to be valid, we have to
make sure something is returned in cases
where the states do not match one of the
eight possibilities. We know this is
impossible given the rest of our code, but
Processing does not.
L?NOLH 0;
W
CHN LOF?M (CHN ;, CHN <, CHN =) U
A quick way to join three bits into a String 0NLCHA M = "" + ; + < + =;
The second argument 2 indicates that we
intend to parse a binary number (base 2).
CHN CH>?R = 'HN?A?L.J;LM?'HN(M,2);
L?NOLH LOF?M?N8CH>?R9;
W
Rule 222 CHN89 LOF?M?N = U1,1,0,1,1,1,1,0W;
C@ (; == 1 && < == 1 && = == 1) L?NOLH LOF?M?N809;
4EB .>QROB LC #LAB (S005)
335
3L C>O FK QEFP PB@QFLK, TBfSB TOFQQBK BSBOVQEFKD TB KBBA QL @LJMRQB QEB DBKBO>QFLKP CLO >
7LICO>J BIBJBKQ>OV #!. ,BQfP Q>HB > JLJBKQ QL LOD>KFWB QEB >?LSB @LAB FKQL > @I>PP, TEF@E
TFII RIQFJ>QBIV EBIM FK QEB ABPFDK LC LRO LSBO>II PHBQ@E.
Rule 222 in reverse order CHN89 LOF?M?N = U0,1,1,1,1,0,1,1W;
=F;MM !A U
We need an array for the cells and one for
the rules.
CHN89 =?FFM;
CHN89 LOF?M?N;
!A() U
=?FFM = H?Q CHN8QC>NB9;
Arbitrarily starting with rule 90 LOF?M?N = U0,1,0,1,1,0,1,0W;
@IL (CHN C = 0; C < =?FFM.F?HANB; C++) U
=?FFM8C9 = 0;
W
All cells start with state 0, except the center
cell has state 1.
=?FFM8=?FFM.F?HANB/29 = 1;
W
PIC> A?H?L;N?() U
Compute the next generation. CHN89 H?RNA?H = H?Q CHN8=?FFM.F?HANB9;
@IL (CHN C = 1; C < =?FFM.F?HANB-1; C++) U
CHN F?@N = =?FFM8C-19;
CHN G? = =?FFM8C9;
CHN LCABN = =?FFM8C+19;
H?RNA?H8C9 = LOF?M(F?@N, G?, LCABN);
W
=?FFM = H?RNA?H;
W
Look up a new state from the ruleset. CHN LOF?M (CHN ;, CHN <, CHN =) U
0NLCHA M = "" + ; + < + =;
CHN CH>?R = 'HN?A?L.J;LM?'HN(M,2);
L?NOLH LOF?M?N8CH>?R9;
W
W
7.4 Drawing an Elementary CA 7.4 Drawing an Elementary CA
7E>QfP JFPPFKD? 0OBPRJ>?IV, FQfP LRO FKQBKQFLK QL AFPMI>V @BIIP >KA QEBFO PQ>QBP FK SFPR>I CLOJ.
!P TB P>T B>OIFBO, QEB PQ>KA>OA QB@EKFNRB CLO ALFKD QEFP FP QL PQ>@H QEB DBKBO>QFLKP LKB LK
QLM LC B>@E LQEBO >KA AO>T > OB@Q>KDIB QE>Q FP ?I>@H (CLO PQ>QB 1) LO TEFQB (CLO PQ>QB 0).
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
336
"BCLOB TB FJMIBJBKQ QEFP M>OQF@RI>O SFPR>IFW>QFLK, )fA IFHB QL MLFKQ LRQ QTL QEFKDP.
/KB, QEFP SFPR>I FKQBOMOBQ>QFLK LC QEB A>Q> FP @LJMIBQBIV IFQBO>I. )QfP RPBCRI CLO ABJLKPQO>QFKD QEB
>IDLOFQEJP >KA OBPRIQP LC 7LICO>JfP BIBJBKQ>OV #!, ?RQ FQ PELRIAKfQ KB@BPP>OFIV AOFSB VLRO LTK
MBOPLK>I TLOH. )QfP O>QEBO RKIFHBIV QE>Q VLR >OB ?RFIAFKD > MOLGB@Q QE>Q KBBAP MOB@FPBIV QEFP
>IDLOFQEJ TFQE QEFP SFPR>I PQVIB. 3L TEFIB IB>OKFKD QL AO>T QEB #! FK QEFP T>V TFII EBIM VLR
RKABOPQ>KA >KA FJMIBJBKQ #! PVPQBJP, QEFP PHFII PELRIA BUFPQ LKIV >P > CLRKA>QFLK.
3B@LKA, QEB C>@Q QE>Q TB >OB SFPR>IFWFKD > LKB-AFJBKPFLK>I #! TFQE > QTL-AFJBKPFLK>I FJ>DB
@>K ?B @LKCRPFKD. )QfP SBOV FJMLOQ>KQ QL OBJBJ?BO QE>Q QEFP FP KLQ > 2$ #!. 7B >OB PFJMIV
@ELLPFKD QL PELT > EFPQLOV LC >II QEB DBKBO>QFLKP PQ>@HBA SBOQF@>IIV. 4EFP QB@EKFNRB @OB>QBP >
QTL-AFJBKPFLK>I FJ>DB LRQ LC J>KV FKPQ>K@BP LC LKB-AFJBKPFLK>I A>Q>. "RQ QEB PVPQBJ FQPBIC FP
LKB-AFJBKPFLK>I. ,>QBO, TB >OB DLFKD QL ILLH >Q >K >@QR>I 2$ #! (QEB '>JB LC ,FCB) >KA
AFP@RPP ELT TB JFDEQ @ELLPB QL AFPMI>V PR@E > PVPQBJ.
4EB DLLA KBTP FP QE>Q AO>TFKD QEB #! FP KLQ M>OQF@RI>OIV AFCCF@RIQ. ,BQfP ?BDFK ?V ILLHFKD >Q
ELT TB TLRIA OBKABO > PFKDIB DBKBO>QFLK. !PPRJB TB E>SB > 0OL@BPPFKD TFKALT 600 MFUBIP
TFAB >KA TB T>KQ B>@E @BII QL ?B > 10U10 PNR>OB. 7B QEBOBCLOB E>SB > #! TFQE 60 @BIIP. /C
@LROPB, TB @>K @>I@RI>QB QEFP S>IRB AVK>JF@>IIV.
!PPRJFKD TBfSB DLKB QEOLRDE QEB MOL@BPP LC DBKBO>QFKD QEB @BII PQ>QBP (TEF@E TB AFA FK QEB
MOBSFLRP PB@QFLK), TB @>K KLT ILLM QEOLRDE QEB BKQFOB >OO>V LC @BIIP, AO>TFKD > ?I>@H @BII TEBK
QEB PQ>QB FP 1 >KA > TEFQB LKB TEBK QEB PQ>QB FP 0.
F64B?2 7.12 (?2=2.A21)
CHN Q = 10;
How many cells fit across given a certain
width
CHN89 =?FFM = H?Q CHN8QC>NB/Q9;
@IL (CHN C = 0; C < =?FFM.F?HANB; C++) U
Black or white fill? C@ (=?FFM8C9 == 1) @CFF(0);
?FM? @CFF(255);
4EB .>QROB LC #LAB (S005)
337
)K QORQE, TB @LRIA LMQFJFWB QEB >?LSB ?V E>SFKD > TEFQB ?>@HDOLRKA >KA LKIV AO>TFKD TEBK
QEBOB FP > ?I>@H @BII (P>SFKD RP QEB TLOH LC AO>TFKD J>KV TEFQB PNR>OBP), ?RQ FK JLPQ @>PBP
QEFP PLIRQFLK FP DLLA BKLRDE (>KA KB@BPP>OV CLO LQEBO JLOB PLMEFPQF@>QBA ABPFDKP TFQE
S>OVFKD @LILOP, BQ@.) !IPL, FC TB T>KQBA B>@E @BII QL ?B OBMOBPBKQBA >P > PFKDIB MFUBI, TB
TLRIA KLQ T>KQ QL RPB 0OL@BPPFKDfP L?=N() CRK@QFLK, ?RQ O>QEBO >@@BPP QEB MFUBI >OO>V
AFOB@QIV.
)K QEB >?LSB @LAB, VLRfII KLQF@B QEB V-IL@>QFLK CLO B>@E OB@Q>KDIB FP 0. )C TB T>KQ QEB
DBKBO>QFLKP QL ?B AO>TK KBUQ QL B>@E LQEBO, TFQE B>@E OLT LC @BIIP J>OHFKD > KBT
DBKBO>QFLK, TBfII >IPL KBBA QL @LJMRQB > V-IL@>QFLK ?>PBA LK ELT J>KV FQBO>QFLKP LC QEB #!
TBfSB BUB@RQBA. 7B @LRIA >@@LJMIFPE QEFP ?V >AAFKD > gDBKBO>QFLKh S>OF>?IB (>K FKQBDBO) QL
LRO #! @I>PP >KA FK@OBJBKQFKD FQ B>@E QFJB QEOLRDE A?H?L;N?(). 7FQE QEBPB >AAFQFLKP, TB
@>K KLT ILLH >Q QEB #! @I>PP TFQE >II QEB CB>QROBP CLO ?LQE @LJMRQFKD >KA AO>TFKD QEB #!.
Example 7.1: Wolfram elementary cellular automata
Notice how the x-location is the cell index
times the cell width. In the above scenario,
this would give us cells located at x equals
0, 10, 20, 30, all the way up to 600.
L?=N(C*Q, 0, Q, Q);
W
=F;MM !A U
CHN89 =?FFM;
CHN89 LOF?M?N;
CHN Q = 10;
The CA should keep track of how many
generations.
CHN A?H?L;NCIH = 0;
!A() U
=?FFM = H?Q CHN8QC>NB/Q9;
LOF?M?N = U0,1,0,1,1,0,1,0W;
=?FFM8=?FFM.F?HANB/29 = 1;
W
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
338
Function to compute the next generation PIC> A?H?L;N?() U
CHN89 H?RNA?H = H?Q CHN8=?FFM.F?HANB9;
@IL (CHN C = 1; C < =?FFM.F?HANB-1; C++) U
CHN F?@N = =?FFM8C-19;
CHN G? = =?FFM8C9;
CHN LCABN = =?FFM8C+19;
H?RNA?H8C9 = LOF?M(F?@N, G?, LCABN);
W
=?FFM = H?RNA?H;
Increment the generation counter. A?H?L;NCIH++;
W
CHN LOF?M(CHN ;, CHN <, CHN =) U
0NLCHA M = "" + ; + < + =;
CHN CH>?R = 'HN?A?L.J;LM?'HN(M,2);
L?NOLH LOF?M?N8CH>?R9;
W
@IL (CHN C = 0; C < =?FFM.F?HANB; C++) U
C@ (=?FFM8C9 == 1) @CFF(0);
?FM? @CFF(255);
Set the y-location according to the
generation.
L?=N(C*Q, A?H?L;NCIH*Q, Q, Q);
W
W
%UM>KA %U>JMIB 7.1 QL E>SB QEB CLIILTFKD CB>QROB: TEBK QEB #! OB>@EBP QEB ?LQQLJ LC
QEB 0OL@BPPFKD TFKALT, QEB #! PQ>OQP LSBO TFQE > KBT, O>KALJ ORIBPBQ.
Exercise 7.1 Exercise 7.1
%U>JFKB TE>Q M>QQBOKP L@@RO FC VLR FKFQF>IFWB QEB CFOPQ DBKBO>QFLK TFQE B>@E @BII E>SFKD >
O>KALJ PQ>QB.
Exercise 7.2 Exercise 7.2
6FPR>IFWB QEB #! FK > KLK-QO>AFQFLK>I T>V. "OB>H >II QEB ORIBP VLR @>K; ALKfQ CBBI QFBA QL
RPFKD PNR>OBP LK > MBOCB@Q DOFA TFQE ?I>@H >KA TEFQB.
Exercise 7.3 Exercise 7.3
4EB .>QROB LC #LAB (S005)
339
#OB>QB > SFPR>IFW>QFLK LC QEB #! QE>Q P@OLIIP RMT>OAP >P QEB DBKBO>QFLKP FK@OB>PB PL
QE>Q VLR @>K SFBT QEB DBKBO>QFLKP QL gFKCFKFQV.h (FKQ: FKPQB>A LC HBBMFKD QO>@H LC LKIV
LKB DBKBO>QFLK >Q > QFJB, VLRfII KBBA QL PQLOB > EFPQLOV LC DBKBO>QFLKP, >IT>VP >AAFKD
> KBT LKB >KA ABIBQFKD QEB LIABPQ LKB FK B>@E CO>JB.
Exercise 7.4 Exercise 7.4
7.5 Wolfram Classification 7.5 Wolfram Classification
"BCLOB TB JLSB LK QL ILLHFKD >Q #! FK QTL AFJBKPFLKP, FQfP TLOQE Q>HFKD > ?OFBC ILLH >Q
7LICO>JfP @I>PPFCF@>QFLK CLO @BIIRI>O >RQLJ>Q>. !P TB KLQBA B>OIFBO, QEB S>PQ J>GLOFQV LC
BIBJBKQ>OV #! ORIBPBQP MOLAR@B RKFKPMFOFKD OBPRIQP, TEFIB PLJB OBPRIQ FK TLKAOLRPIV
@LJMIBU M>QQBOKP IFHB QELPB CLRKA FK K>QROB. 7LICO>J E>P AFSFABA RM QEB O>KDB LC LRQ@LJBP
FKQL CLRO @I>PPBP:
C7,>> 1: )941:=84?D. C7,>> 1: )941:=84?D. #I>PP 1 #!P BKA RM, >CQBO PLJB KRJ?BO LC DBKBO>QFLKP, TFQE BSBOV @BII
@LKPQ>KQ. 4EFP FP KLQ QBOOF?IV BU@FQFKD QL T>Q@E. 2RIB 222 (>?LSB) FP > @I>PP 1 #!; FC VLR ORK FQ
CLO BKLRDE DBKBO>QFLKP, BSBOV @BII TFII BSBKQR>IIV ?B@LJB >KA OBJ>FK ?I>@H.
F64B?2 7.18: (B92 222
F64B?2 7.19: (B92 190
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
340
C7,>> 2: &0;0?4?4:9. C7,>> 2: &0;0?4?4:9. ,FHB @I>PP 1 #!P, @I>PP 2 #!P OBJ>FK PQ>?IB, ?RQ QEB @BII PQ>QBP >OB KLQ
@LKPQ>KQ. 2>QEBO, QEBV LP@FII>QB FK PLJB OBDRI>O M>QQBOK ?>@H >KA CLOQE COLJ 0 QL 1 QL 0 QL 1 >KA
PL LK. )K ORIB 190 (>?LSB), B>@E @BII CLIILTP QEB PBNRBK@B 11101110111011101110.
C7,>> 3: &,9/:8. C7,>> 3: &,9/:8. #I>PP 3 #!P >MMB>O O>KALJ >KA E>SB KL B>PFIV AFP@BOKF?IB M>QQBOK. )K C>@Q,
ORIB 30 (>?LSB) FP RPBA >P > O>KALJ KRJ?BO DBKBO>QLO FK 7LICO>JfP ->QEBJ>QF@> PLCQT>OB.
!D>FK, QEFP FP > JLJBKQ TEBOB TB @>K CBBI >J>WBA QE>Q PR@E > PFJMIB PVPQBJ TFQE PFJMIB ORIBP
@>K ABP@BKA FKQL > @E>LQF@ >KA O>KALJ M>QQBOK.
C7,>> 4: C:8;70C4?D. C7,>> 4: C:8;70C4?D. #I>PP 4 #!P @>K ?B QELRDEQ LC >P > JFU ?BQTBBK @I>PP 2 >KA @I>PP 3.
/KB @>K CFKA OBMBQFQFSB, LP@FII>QFKD M>QQBOKP FKPFAB QEB #!, ?RQ TEBOB >KA TEBK QEBPB M>QQBOKP
>MMB>O FP RKMOBAF@Q>?IB >KA PBBJFKDIV O>KALJ. #I>PP 4 #! BUEF?FQ QEB MOLMBOQFBP LC @LJMIBU
PVPQBJP QE>Q TB ABP@OF?BA B>OIFBO FK QEFP @E>MQBO >KA FK #E>MQBO 6. )C > @I>PP 3 #! TLTBA VLR,
QEBK > @I>PP 4 IFHB 2RIB 110 >?LSB PELRIA OB>IIV ?ILT VLRO JFKA.
F64B?2 7.20: (B92 30
F64B?2 7.21: (B92 110
%UBO@FPB: #OB>QB > 0OL@BPPFKD PHBQ@E QE>Q P>SBP >K FJ>DB CLO BSBOV MLPPF?IB ORIBPBQ.
#>K VLR @I>PPFCV QEBJ?
Exercise 7.5 Exercise 7.5
4EB .>QROB LC #LAB (S005)
341
7.6 The Game of Life 7.6 The Game of Life
4EB KBUQ PQBM TB >OB DLFKD QL Q>HB FP QL JLSB COLJ > LKB-AFJBKPFLK>I #! QL > QTL-
AFJBKPFLK>I LKB. 4EFP TFII FKQOLAR@B PLJB >AAFQFLK>I @LJMIBUFQV; B>@E @BII TFII E>SB > ?FDDBO
KBFDE?LOELLA, ?RQ QE>Q TFII LMBK RM QEB ALLO QL > O>KDB LC MLPPF?IB >MMIF@>QFLKP. !CQBO >II,
JLPQ LC TE>Q TB AL FK @LJMRQBO DO>MEF@P IFSBP FK QTL AFJBKPFLKP, >KA QEFP @E>MQBO TFII
ABJLKPQO>QB ELT QL >MMIV #! QEFKHFKD QL TE>Q TB AO>T FK LRO 0OL@BPPFKD PHBQ@EBP.
)K 1970, ->OQFK '>OAKBO TOLQB >K >OQF@IB FK .7=9BH=:=7 AA9F=75B QE>Q AL@RJBKQBA
J>QEBJ>QF@F>K *LEK #LKT>VfP KBT g'>JB LC ,FCB,h ABP@OF?FKD FQ >P gOB@OB>QFLK>Ih
J>QEBJ>QF@P >KA PRDDBPQFKD QE>Q QEB OB>ABO DBQ LRQ > @EBPP?L>OA >KA PLJB @EB@HBOP >KA
gMI>V.h 7EFIB QEB '>JB LC ,FCB E>P ?B@LJB PLJBQEFKD LC > @LJMRQ>QFLK>I @IF@EZ (J>HB KLQB
LC QEB JVOF>A MOLGB@QP QE>Q AFPMI>V QEB '>JB LC ,FCB LK ,%$P, P@OBBKP, MOLGB@QFLK PROC>@BP,
BQ@.), FQ FP PQFII FJMLOQ>KQ CLO RP QL ?RFIA FQ COLJ P@O>Q@E. &LO LKB, FQ MOLSFABP > DLLA
LMMLOQRKFQV QL MO>@QF@B LRO PHFIIP TFQE QTL-AFJBKPFLK>I >OO>VP, L?GB@Q LOFBKQ>QFLK, BQ@. "RQ
MBOE>MP JLOB FJMLOQ>KQIV, FQP @LOB MOFK@FMIBP >OB QFBA AFOB@QIV QL LRO @LOB DL>IPdPFJRI>QFKD
QEB K>QRO>I TLOIA TFQE @LAB. 4ELRDE TB J>V T>KQ QL >SLFA PFJMIV ARMIF@>QFKD FQ TFQELRQ >
DOB>Q AB>I LC QELRDEQ LO @>OB, QEB >IDLOFQEJ >KA FQP QB@EKF@>I FJMIBJBKQ>QFLK TFII MOLSFAB RP
TFQE QEB FKPMFO>QFLK >KA CLRKA>QFLK QL ?RFIA PFJRI>QFLKP QE>Q BUEF?FQ QEB @E>O>@QBOFPQF@P >KA
?BE>SFLOP LC ?FLILDF@>I PVPQBJP LC OBMOLAR@QFLK.
5KIFHB SLK .BRJ>KK, TEL @OB>QBA >K BUQO>LOAFK>OFIV @LJMIBU PVPQBJ LC PQ>QBP >KA ORIBP,
#LKT>V T>KQBA QL >@EFBSB > PFJFI>O gIFCBIFHBh OBPRIQ TFQE QEB PFJMIBPQ PBQ LC ORIBP MLPPF?IB.
->OQFK '>OAKBO LRQIFKBA #LKT>VfP DL>IP >P CLIILTP:
I1. *52?2 @5<B91 /2 ;< 6;6A6.9 =.AA2?; 3<? D5605 A52?2 6@ . @6:=92 =?<<3 A5.A A52 =<=B9.A6<;
0.; 4?<D D6A5<BA 96:6A. 2. *52?2 @5<B91 /2 6;6A6.9 =.AA2?;@ A5.A .==.?2;A9F 1< 4?<D
D6A5<BA 96:6A. 3. *52?2 @5<B91 /2 @6:=92 6;6A6.9 =.AA2?;@ A5.A 4?<D .;1 05.;42 3<? .
0<;@612?./92 =2?6<1 <3 A6:2 /23<?2 0<:6;4 A< .; 2;1 6; A5?22 =<@@6/92 D.F@: 3.16;4 .D.F
0<:=92A29F (3?<: <C2?0?<D16;4 <? /20<:6;4 A<< @=.?@2), @2AA96;4 6;A< . @A./92
0<;364B?.A6<; A5.A ?2:.6;@ B;05.;421 A52?2.3A2?, <? 2;A2?6;4 .; <@0699.A6;4 =5.@2 6;
D5605 A52F ?2=2.A .; 2;192@@ 0F092 <3 AD< <? :<?2 =2?6<1@.J
H$.?A6; G.?1;2?, Scientific American (http://www.ibiblio.org/lifepatterns/
october1970.html) 223 (&0A</2? 1970): 120-123.
4EB >?LSB JFDEQ PLRKA > ?FQ @OVMQF@, ?RQ FQ BPPBKQF>IIV ABP@OF?BP > 7LICO>J @I>PP 4 #!. 4EB
#! PELRIA ?B M>QQBOKBA ?RQ RKMOBAF@Q>?IB LSBO QFJB, BSBKQR>IIV PBQQIFKD FKQL > RKFCLOJ LO
LP@FII>QFKD PQ>QB. )K LQEBO TLOAP, QELRDE #LKT>V AFAKfQ RPB QEFP QBOJFKLILDV, FQ PELRIA E>SB
>II QELPB MOLMBOQFBP LC > 7CAD@9L GMGH9A QE>Q TB HBBM JBKQFLKFKD.
,BQfP ILLH >Q ELT QEB '>JB LC ,FCB TLOHP. )Q TLKfQ Q>HB RM QLL JR@E QFJB LO PM>@B, PFK@B
TBfSB @LSBOBA QEB ?>PF@P LC #! >IOB>AV.
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
342
&FOPQ, FKPQB>A LC > IFKB LC @BIIP, TB KLT E>SB
> QTL-AFJBKPFLK>I J>QOFU LC @BIIP. !P TFQE
QEB BIBJBKQ>OV #!, QEB MLPPF?IB PQ>QBP >OB 0
LO 1. /KIV FK QEFP @>PB, PFK@B TBfOB Q>IHFKD
>?LRQ gIFCB," 0 JB>KP AB>A >KA 1 JB>KP
>IFSB.
4EB @BIIfP KBFDE?LOELLA E>P >IPL BUM>KABA.
)C > KBFDE?LO FP >K >AG>@BKQ @BII, >
KBFDE?LOELLA FP KLT KFKB @BIIP FKPQB>A LC
QEOBB.
7FQE QEOBB @BIIP, TB E>A > 3-?FQ KRJ?BO LO
BFDEQ MLPPF?IB @LKCFDRO>QFLKP. 7FQE KFKB
@BIIP, TB E>SB 9 ?FQP, LO 512 MLPPF?IB
KBFDE?LOELLAP. )K JLPQ @>PBP, FQ TLRIA ?B
FJMO>@QF@>I QL ABCFKB >K LRQ@LJB CLO BSBOV
PFKDIB MLPPF?FIFQV. 4EB '>JB LC ,FCB DBQP
>OLRKA QEFP MOL?IBJ ?V ABCFKFKD > PBQ LC ORIBP >@@LOAFKD QL DBKBO>I @E>O>@QBOFPQF@P LC QEB
KBFDE?LOELLA. )K LQEBO TLOAP, FP QEB KBFDE?LOELLA LSBOMLMRI>QBA TFQE IFCB? 3ROOLRKABA ?V
AB>QE? /O GRPQ OFDEQ? (BOB >OB QEB ORIBP LC IFCB.
1. D0,?3. D0,?3. )C > @BII FP >IFSB (PQ>QB = 1) FQ TFII AFB (PQ>QB ?B@LJBP 0) RKABO QEB CLIILTFKD
@FO@RJPQ>K@BP.
$A0=;:;@7,?4:9: $A0=;:;@7,?4:9: )C QEB @BII E>P CLRO LO JLOB >IFSB KBFDE?LOP, FQ AFBP.
!:907490>>: !:907490>>: )C QEB @BII E>P LKB LO CBTBO >IFSB KBFDE?LOP, FQ AFBP.
2. B4=?3. B4=?3. )C > @BII FP AB>A (PQ>QB = 0) FQ TFII @LJB QL IFCB (PQ>QB ?B@LJBP 1) FC FQ E>P BU>@QIV
QEOBB >IFSB KBFDE?LOP (KL JLOB, KL IBPP).
3. '?,>4>. '?,>4>. )K >II LQEBO @>PBP, QEB @BII PQ>QB ALBP KLQ @E>KDB. 4L ?B QELOLRDE, IBQfP
ABP@OF?B QELPB P@BK>OFLP.
'?,D492 A74A0: '?,D492 A74A0: )C > @BII FP >IFSB >KA E>P BU>@QIV QTL LO QEOBB IFSB KBFDE?LOP,
FQ PQ>VP >IFSB.
'?,D492 D0,/: '?,D492 D0,/: )C > @BII FP AB>A >KA E>P >KVQEFKD LQEBO QE>K QEOBB IFSB
KBFDE?LOP, FQ PQ>VP AB>A.
,BQfP ILLH >Q > CBT BU>JMIBP.
F64B?2 7.22
4EB .>QROB LC #LAB (S005)
343
7FQE QEB BIBJBKQ>OV #!, TB TBOB >?IB QL ILLH >Q >II QEB DBKBO>QFLKP KBUQ QL B>@E LQEBO,
PQ>@HBA >P OLTP FK > 2$ DOFA. 7FQE QEB '>JB LC ,FCB, ELTBSBO, QEB #! FQPBIC FP FK QTL
AFJBKPFLKP. 7B @LRIA QOV @OB>QFKD >K BI>?LO>QB 3$ SFPR>IFW>QFLK LC QEB OBPRIQP >KA PQ>@H >II
QEB DBKBO>QFLKP FK > @R?B PQOR@QROB (>KA FK C>@Q, VLR JFDEQ T>KQ QL QOV QEFP >P >K BUBO@FPB).
.BSBOQEBIBPP, QEB QVMF@>I T>V QEB '>JB LC ,FCB FP AFPMI>VBA FP QL QOB>Q B>@E DBKBO>QFLK >P >
PFKDIB CO>JB FK >K >KFJ>QFLK. 3L FKPQB>A LC SFBTFKD >II QEB DBKBO>QFLKP >Q LK@B, TB PBB QEBJ
LKB >Q > QFJB, >KA QEB OBPRIQ OBPBJ?IBP O>MFAIV DOLTFKD ?>@QBOF> FK > MBQOF AFPE.
/KB LC QEB BU@FQFKD >PMB@QP LC QEB '>JB LC ,FCB FP QE>Q QEBOB >OB FKFQF>I M>QQBOKP QE>Q VFBIA
FKQOFDRFKD OBPRIQP. &LO BU>JMIB, PLJB OBJ>FK PQ>QF@ >KA KBSBO @E>KDB.
4EBOB >OB M>QQBOKP QE>Q LP@FII>QB ?>@H >KA CLOQE ?BQTBBK QTL PQ>QBP.
F64B?2 7.23
F64B?2 7.24
F64B?2 7.25
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
344
!KA QEBOB >OB >IPL M>QQBOKP QE>Q COLJ DBKBO>QFLK QL DBKBO>QFLK JLSB >?LRQ QEB DOFA. ()QfP
FJMLOQ>KQ QL KLQB QE>Q QEB @BIIP QEBJPBISBP >OBKfQ >@QR>IIV JLSFKD, >IQELRDE TB PBB QEB
>MMB>O>K@B LC JLQFLK FK QEB OBPRIQ >P QEB @BIIP QROK LK >KA LCC.)
)C VLR >OB FKQBOBPQBA FK QEBPB M>QQBOKP, QEBOB >OB PBSBO>I DLLA gLRQ LC QEB ?LUh '>JB LC ,FCB
ABJLKPQO>QFLKP LKIFKB QE>Q >IILT VLR QL @LKCFDROB QEB #!fP FKFQF>I PQ>QB >KA T>Q@E FQ ORK >Q
S>OVFKD PMBBAP. 4TL BU>JMIBP VLR JFDEQ T>KQ QL BU>JFKB >OB:
i %UMILOFKD %JBODBK@B (EQQM://IIH.JBAF>.JFQ.BAR/MOLGB@QP/BJBODBK@B/) ?V -FQ@EBI
2BPKF@H >KA "OF>K 3FISBOJ>K, ,FCBILKD +FKABOD>OQBK 'OLRM, -)4 -BAF> ,>?LO>QLOV
i #LKT>VfP '>JB LC ,FCB (EQQM://PQBSBKHIFPB.DFQER?.@LJ/#LKT>VP'>JB/C,FCB) ?V
3QBSBK +IFPB (RPBP 0OL@BPPFKD.GP!)
&LO QEB BU>JMIB TBfII ?RFIA COLJ P@O>Q@E FK QEB KBUQ PB@QFLK, FQ TFII ?B B>PFBO QL PFJMIV
O>KALJIV PBQ QEB PQ>QBP CLO B>@E @BII.
F64B?2 7.26
7.7 Programming the Game of Life 7.7 Programming the Game of Life
.LT TB GRPQ KBBA QL BUQBKA LRO @LAB COLJ QEB 7LICO>J #! QL QTL AFJBKPFLKP. 7B RPBA >
LKB-AFJBKPFLK>I >OO>V QL PQLOB QEB IFPQ LC @BII PQ>QBP ?BCLOB, >KA CLO QEB '>JB LC ,FCB, TB @>K
RPB > QTL-AFJBKPFLK>I >OO>V (EQQM://TTT.MOL@BPPFKD.LOD/IB>OKFKD/2A>OO>V/).
7BfII ?BDFK ?V FKFQF>IFWFKD B>@E @BII LC QEB ?L>OA TFQE > O>KALJ PQ>QB: 0 LO 1.
CHN8989 <I;L> = H?Q CHN8=IFOGHM98LIQM9;
@IL (CHN R = 0; R < =IFOGHM; R++) U
@IL (CHN S = 0; S < LIQM; S++) U
Initialize each cell with a 0 or 1. =OLL?HN8R98S9 = CHN(L;H>IG(2));
W
W
4EB .>QROB LC #LAB (S005)
345
!KA QL @LJMRQB QEB KBUQ DBKBO>QFLK, GRPQ >P ?BCLOB, TB KBBA > COBPE 2$ >OO>V QL TOFQB QL >P
TB >K>IVWB B>@E @BIIfP KBFDE?LOELLA >KA @>I@RI>QB > KBT PQ>QB.
/+. "BCLOB TB @>K PLOQ LRQ ELT QL >@QR>IIV
@>I@RI>QB QEB KBT PQ>QB, TB KBBA QL HKLT
ELT TB @>K OBCBOBK@B B>@E @BIIfP KBFDE?LO.
)K QEB @>PB LC QEB 1$ #!, QEFP T>P PFJMIB: FC
> @BII FKABU T>P C, FQP KBFDE?LOP TBOB F-1
>KA F+1. (BOB B>@E @BII ALBPKfQ E>SB >
PFKDIB FKABU, ?RQ O>QEBO > @LIRJK >KA OLT
FKABU: U,V. !P PELTK FK &FDROB 7.27, TB @>K
PBB QE>Q FQP KBFDE?LOP >OB: (L-1,M-1) (L,M-1),
(L+1,M-2), (L-1,M), (L+1,M), (L-1,M+1), (L,M+1), >KA
(L+1,M+1).
!II LC QEB '>JB LC ,FCB ORIBP LMBO>QB ?V
HKLTFKD ELT J>KV KBFDE?LOP >OB >IFSB. 3L
FC TB @OB>QB > KBFDE?LO @LRKQBO S>OF>?IB
>KA FK@OBJBKQ FQ B>@E QFJB TB CFKA >
KBFDE?LO TFQE > PQ>QB LC 1, TBfII E>SB QEB
QLQ>I LC IFSB KBFDE?LOP.
CHN8989 H?RN = H?Q CHN8=IFOGHM98LIQM9;
@IL (CHN R = 0; R < =IFOGHM; R++) U
@IL (CHN S = 0; S < LIQM; S++) U
We need a new state for each cell. H?RN8R98S9 = :::::::::::::::?;
W
W
F64B?2 7.27
CHN H?CAB<ILM = 0;
Top row of neighbors C@ (<I;L>8R-198S-19 == 1) H?CAB<ILM++;
C@ (<I;L>8R 98S-19 == 1) H?CAB<ILM++;
C@ (<I;L>8R+198S-19 == 1) H?CAB<ILM++;
Middle row of neighbors (note we dont
count self)
C@ (<I;L>8R-198S9 == 1) H?CAB<ILM++;
C@ (<I;L>8R+198S9 == 1) H?CAB<ILM++;
Bottom row of neighbors C@ (<I;L>8R-198S+19 == 1) H?CAB<ILM++;
C@ (<I;L>8R 98S+19 == 1) H?CAB<ILM++;
C@ (<I;L>8R+198S+19 == 1) H?CAB<ILM++;
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
346
!KA >D>FK, GRPQ >P TFQE QEB 7LICO>J #!, TB CFKA LROPBISBP FK > PFQR>QFLK TEBOB QEB >?LSB FP >
RPBCRI >KA @IB>O T>V QL TOFQB QEB @LAB CLO QB>@EFKD MROMLPBP, >IILTFKD RP QL PBB BSBOV PQBM
(B>@E QFJB TB CFKA > KBFDE?LO TFQE > PQ>QB LC LKB, TB FK@OB>PB > @LRKQBO). .BSBOQEBIBPP, FQfP >
?FQ PFIIV QL P>V, g)C QEB @BII PQ>QB BNR>IP LKB, >AA LKB QL > @LRKQBOh TEBK TB @LRIA GRPQ P>V,
g!AA QEB @BII PQ>QB QL > @LRKQBO.h !CQBO >II, FC QEB PQ>QB FP LKIV > 0 LO 1, QEB PRJ LC >II QEB
KBFDE?LOPf PQ>QBP TFII VFBIA QEB QLQ>I KRJ?BO LC IFSB @BIIP. 3FK@B QEB KBFDE?LOP >OB >OO>KDBA FK >
JFKF 3U3 DOFA, TB @>K >AA QEBJ >II RM TFQE >KLQEBO ILLM.
/C @LROPB, TBfSB J>AB > JFPQ>HB FK QEB @LAB >?LSB. )K QEB '>JB LC ,FCB, QEB @BII FQPBIC ALBP
KLQ @LRKQ >P LKB LC QEB KBFDE?LOP. 7B @LRIA RPB > @LKAFQFLK>I QL PHFM >AAFKD QEB PQ>QB TEBK
?LQE C >KA D BNR>I 0, ?RQ >KLQEBO LMQFLK TLRIA ?B QL GRPQ PR?QO>@Q QEB @BII PQ>QB LK@B TBfSB
CFKFPEBA QEB ILLM.
&FK>IIV, LK@B TB HKLT QEB QLQ>I KRJ?BO LC IFSB KBFDE?LOP, TB @>K AB@FAB TE>Q QEB @BIIfP KBT
PQ>QB PELRIA ?B >@@LOAFKD QL QEB ORIBP: ?FOQE, AB>QE, LO PQ>PFP.
0RQQFKD QEFP >II QLDBQEBO, TB E>SB:
@IL (CHN C = -1; C <= 1; C++) U
@IL (CHN D = -1; D <= 1; D++) U
Add up all the neighbors states. H?CAB<ILM += <I;L>8R+C98S+D9;
W
W
Whoops! Subtract the cells state, which we
dont want in the total.
H?CAB<ILM -= <I;L>8R98S9;
If it is alive and has less than 2 live
neighbors, it dies from loneliness.
C@ ((<I;L>8R98S9 == 1) && (H?CAB<ILM < 2)) U
H?RN8R98S9 = 0;
W
If it is alive and has more than 3 live
neighbors, it dies from overpopulation.
?FM? C@ ((<I;L>8R98S9 == 1) && (H?CAB<ILM > 3)) U
H?RN8R98S9 = 0;
W
If it is dead and has exactly 3 live
neighbors, it is born!
?FM? C@ ((<I;L>8R98S9 == 0) && (H?CAB<ILM == 3)) U
H?RN8R98S9 = 1;
W
In all other cases, its state remains the
same.
?FM? U
H?RN8R98S9 = <I;L>8R98S9;
W
The next board CHN8989 H?RN = H?Q CHN8=IFOGHM98LIQM9;
4EB .>QROB LC #LAB (S005)
347
&FK>IIV, LK@B QEB KBUQ DBKBO>QFLK FP @>I@RI>QBA, TB @>K BJMILV QEB P>JB JBQELA TB RPBA QL
AO>T QEB 7LICO>J #!d> PNR>OB CLO B>@E PMLQ, TEFQB CLO LCC, ?I>@H CLO LK.
Example 7.2: Game of Life
Looping but skipping the edge cells @IL (CHN R = 1; R < =IFOGHM-1; R++) U
@IL (CHN S = 1; S < LIQM-1; S++) U
Add up all the neighbor states to calculate
the number of live neighbors.
CHN H?CAB<ILM = 0;
@IL (CHN C = -1; C <= 1; C++) U
@IL (CHN D = -1; D <= 1; D++) U
H?CAB<ILM += <I;L>8R+C98S+D9;
W
W
Correct by subtracting the cell state itself. H?CAB<ILM -= <I;L>8R98S9;
The rules of life!
C@ ((<I;L>8R98S9 == 1) && (H?CAB<ILM < 2)) H?RN8R98S9 = 0;
?FM? C@ ((<I;L>8R98S9 == 1) && (H?CAB<ILM > 3)) H?RN8R98S9 = 0;
?FM? C@ ((<I;L>8R98S9 == 0) && (H?CAB<ILM == 3)) H?RN8R98S9 = 1;
?FM? H?RN8R98S9 = <I;L>8R98S9;
W
W
The 2D array next is now the current
board.
<I;L> = H?RN;
@IL ( CHN C = 0; C < =IFOGHM;C++) U
@IL ( CHN D = 0; D < LIQM;D++) U
Black when state = 1 C@ ((<I;L>8C98D9 == 1)) @CFF(0);
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
348
White when state = 0 ?FM? @CFF(255);
MNLIE?(0);
L?=N(C*Q, D*Q, Q, Q);
W
W
#OB>QB > '>JB LC ,FCB PFJRI>QFLK QE>Q >IILTP VLR QL J>KR>IIV @LKCFDROB QEB DOFA ?V
AO>TFKD LO TFQE PMB@FCF@ HKLTK M>QQBOKP.
Exercise 7.6 Exercise 7.6
)JMIBJBKQ gTO>M->OLRKAh CLO QEB '>JB LC ,FCB PL QE>Q @BIIP LK QEB BADBP E>SB
KBFDE?LOP LK QEB LMMLPFQB PFAB LC QEB DOFA.
Exercise 7.7 Exercise 7.7
7EFIB QEB >?LSB PLIRQFLK (%U>JMIB 7.2) FP @LKSBKFBKQ, FQ FP KLQ M>OQF@RI>OIV JBJLOV-
BCCF@FBKQ. )Q @OB>QBP > KBT 2$ >OO>V CLO BSBOV CO>JB LC >KFJ>QFLK! 4EFP J>QQBOP SBOV IFQQIB
CLO > 0OL@BPPFKD ABPHQLM >MMIF@>QFLK, ?RQ FC VLR TBOB FJMIBJBKQFKD QEB '>JB LC ,FCB LK
> JF@OL@LKQOLIIBO LO JL?FIB ABSF@B, VLRfA T>KQ QL ?B JLOB @>OBCRI. /KB PLIRQFLK FP QL
E>SB LKIV QTL >OO>VP >KA @LKPQ>KQIV PT>M QEBJ, TOFQFKD QEB KBUQ PBQ LC PQ>QBP FKQL
TEF@EBSBO LKB FPKfQ QEB @ROOBKQ >OO>V. )JMIBJBKQ QEFP M>OQF@RI>O PLIRQFLK.
Exercise 7.8 Exercise 7.8
7.8 Object-Oriented Cells 7.8 Object-Oriented Cells
/SBO QEB @LROPB LC QEB MOBSFLRP PFU @E>MQBOP, TBfSB PILTIV ?RFIQ BU>JMIBP LC PVPQBJP LC
C6>97HG TFQE MOLMBOQFBP QE>Q JLSB >?LRQ QEB P@OBBK. !KA FK QEFP @E>MQBO, >IQELRDE TBfSB ?BBK
Q>IHFKD >?LRQ > g@BIIh >P FC FQ TBOB >K L?GB@Q, TB >@QR>IIV E>SBKfQ ?BBK RPFKD >KV L?GB@Q
LOFBKQ>QFLK FK LRO @LAB (LQEBO QE>K > @I>PP QL ABP@OF?B QEB #! PVPQBJ >P > TELIB). 4EFP E>P
TLOHBA ?B@>RPB > @BII FP PR@E >K BKLOJLRPIV PFJMIB L?GB@Q (> PFKDIB ?FQ). (LTBSBO, FK >
JLJBKQ, TB >OB DLFKD QL AFP@RPP PLJB FAB>P CLO CROQEBO ABSBILMFKD #! PVPQBJP, J>KV LC
TEF@E FKSLISB HBBMFKD QO>@H LC JRIQFMIB MOLMBOQFBP CLO B>@E @BII. &LO BU>JMIB, TE>Q FC > @BII
KBBABA QL OBJBJ?BO FQP I>PQ QBK PQ>QBP? /O TE>Q FC TB T>KQBA QL >MMIV PLJB LC LRO JLQFLK
>KA MEVPF@P QEFKHFKD QL > #! >KA E>SB QEB @BIIP JLSB >?LRQ QEB TFKALT, AVK>JF@>IIV
@E>KDFKD QEBFO KBFDE?LOP COLJ CO>JB QL CO>JB?
4EB .>QROB LC #LAB (S005)
349
4L >@@LJMIFPE >KV LC QEBPB FAB>P (>KA JLOB), FQ TLRIA ?B EBIMCRI QL PBB ELT TB JFDEQ QOB>Q
> @BII >P >K L?GB@Q TFQE JRIQFMIB MOLMBOQFBP, O>QEBO QE>K >P > PFKDIB 0 LO 1. 4L PELT QEFP, IBQfP
GRPQ OB@OB>QB QEB '>JB LC ,FCB PFJRI>QFLK. /KIV FKPQB>A LC:
,BQfP E>SB:
TEBOB !?FF FP > @I>PP TB TFII TOFQB. 7E>Q >OB QEB MOLMBOQFBP LC > !?FF L?GB@Q? )K LRO '>JB
LC ,FCB BU>JMIB, B>@E @BII E>P > IL@>QFLK >KA PFWB, >P TBII >P > PQ>QB.
)K QEB KLK-//0 SBOPFLK, TB RPBA > PBM>O>QB 2$ >OO>V QL HBBM QO>@H LC QEB PQ>QBP CLO QEB
@ROOBKQ >KA KBUQ DBKBO>QFLK. "V J>HFKD > @BII >K L?GB@Q, ELTBSBO, B>@E @BII @LRIA HBBM
QO>@H LC ?LQE PQ>QBP. )K QEFP @>PB, TBfII QEFKH LC QEB @BII >P OBJBJ?BOFKD FQP MOBSFLRP PQ>QB (CLO
TEBK KBT PQ>QBP KBBA QL ?B @LJMRQBA).
4EFP >IILTP RP QL SFPR>IFWB JLOB FKCLOJ>QFLK >?LRQ TE>Q QEB PQ>QB FP ALFKD. &LO BU>JMIB, TB
@LRIA @ELLPB QL @LILO > @BII AFCCBOBKQIV FC FQP PQ>QB E>P @E>KDBA. &LO BU>JMIB:
Example 7.3: Game of Life OOP
CHN8989 <I;L>;
!?FF8989 <I;L>;
=F;MM !?FF U
Location and size @FI;N R, S;
@FI;N Q;
What is the cells state? CHN MN;N?;
What was its previous state? CHN JL?PCIOM;
PIC> >CMJF;S() U
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
350
.LQ JR@E BIPB >?LRQ QEB @LAB (>Q IB>PQ CLO LRO MROMLPBP EBOB) E>P QL @E>KDB. 4EB KBFDE?LOP
@>K PQFII ?B @LRKQBA QEB P>JB T>V; QEB AFCCBOBK@B FP QE>Q TB KLT KBBA QL OBCBO QL QEB L?GB@QfP
PQ>QB S>OF>?IBP >P TB ILLM QEOLRDE QEB 2$ >OO>V.
If the cell is born, color it blue! C@ (JL?PCIOM == 0 && MN;N? == 1) @CFF(0,0,255);
?FM? C@ (MN;N? == 1) @CFF(0);
?FM? C@ (JL?PCIOM == 1 && MN;N? == 0) @CFF(255,0,0);
?FM? @CFF(255);
L?=N(R, S, Q, Q);
W
If the cell dies, color it red!
@IL (CHN R = 1; R < =IFOGHM-1; R++) U
@IL (CHN S = 1; S < LIQM-1; S++) U
CHN H?CAB<ILM = 0;
@IL (CHN C = -1; C <= 1; C++) U
@IL (CHN D = -1; D <= 1; D++) U
Use the previous state when tracking
neighbors.
D;?=>8EHI += 8E7H:4N+?54O+@5.FH;L?EKI;
W
W
H?CAB<ILM -= <I;L>8R98S9.JL?PCIOM;
We are calling a function newState() to
assign a new state to each cell.
C@ ((<I;L>8R98S9.MN;N? == 1) && (H?CAB<ILM < 2)) <I;L>8R98S9.H?Q0N;N?(0);
?FM? C@ ((<I;L>8R98S9.MN;N? == 1) && (H?CAB<ILM > 3)) <I;L>8R98S9.H?Q0N;N?(0);
?FM? C@ ((<I;L>8R98S9.MN;N? == 0) && (H?CAB<ILM == 3)) <I;L>8R98S9.H?Q0N;N?(1);
else do nothing! W
W
7.9 Variations of Traditional CA 7.9 Variations of Traditional CA
.LT QE>Q TB E>SB @LSBOBA QEB ?>PF@ @LK@BMQP, >IDLOFQEJP, >KA MOLDO>JJFKD PQO>QBDFBP
?BEFKA QEB JLPQ C>JLRP 1$ >KA 2$ @BIIRI>O >RQLJ>Q>, FQfP QFJB QL QEFKH >?LRQ ELT VLR JFDEQ
Q>HB QEFP CLRKA>QFLK LC @LAB >KA ?RFIA LK FQ, ABSBILMFKD @OB>QFSB >MMIF@>QFLKP LC #!P FK VLRO
LTK TLOH. )K QEFP PB@QFLK, TBfII Q>IH QEOLRDE PLJB FAB>P CLO BUM>KAFKD QEB CB>QROBP LC QEB #!
BU>JMIBP. %U>JMIB >KPTBOP QL B>@E LC QEBPB BUBO@FPBP @>K ?B CLRKA LK QEB ?LLH TB?PFQB.
1) #:9-=0.?,92@7,= G=4/> 1) #:9-=0.?,92@7,= G=4/>. 4EBOBfP KL M>OQF@RI>O OB>PLK TEV VLR PELRIA IFJFQ VLROPBIC QL E>SFKD
VLRO @BIIP LK > OB@Q>KDRI>O DOFA. 7E>Q E>MMBKP FC VLR ABPFDK > #! TFQE >KLQEBO QVMB LC PE>MB?
4EB .>QROB LC #LAB (S005)
351
2) %=:-,-474>?4. 2) %=:-,-474>?4.. 4EB ORIBP LC > #! ALKfQ KB@BPP>OFIV E>SB QL ABCFKB >K BU>@Q LRQ@LJB.
3) C:9?49@:@> 3) C:9?49@:@>. 7BfSB ILLHBA >Q BU>JMIBP TEBOB QEB @BIIfP PQ>QB @>K LKIV ?B > 1 LO > 0. "RQ
TE>Q FC QEB @BIIfP PQ>QB T>P > CIL>QFKD MLFKQ KRJ?BO ?BQTBBK 0 >KA 1?
4) I8,20 %=:.0>>492 4) I8,20 %=:.0>>492. 7B ?OFBCIV QLR@EBA LK QEFP B>OIFBO, ?RQ J>KV FJ>DB-MOL@BPPFKD
>IDLOFQEJP LMBO>QB LK #!-IFHB ORIBP. "IROOFKD >K FJ>DB FP @OB>QFKD > KBT MFUBI LRQ LC QEB
>SBO>DB LC > KBFDE?LOELLA LC MFUBIP. 3FJRI>QFLKP LC FKH AFPMBOPFKD LK M>MBO LO T>QBO
OFMMIFKD LSBO >K FJ>DB @>K ?B >@EFBSBA TFQE #! ORIBP.
#OB>QB > #! RPFKD > DOFA LC EBU>DLKP (>P ?BILT), B>@E TFQE PFU KBFDE?LOP.
Exercise 7.9 Exercise 7.9
2BTOFQB QEB '>JB LC ,FCB ORIBP >P CLIILTP:
/SBOMLMRI>QFLK: )C QEB @BII E>P CLRO LO JLOB >IFSB KBFDE?LOP, FQ E>P > 80% @E>K@B LC
AVFKD.
,LKBIFKBPP: )C QEB @BII E>P LKB LO CBTBO >IFSB KBFDE?LOP, FQ E>P > 60% @E>K@B LC AVFKD.
%Q@.
Exercise 7.10 Exercise 7.10
!A>MQ 7LICO>J BIBJBKQ>OV #! QL E>SB QEB PQ>QB ?B > CIL>Q. 9LR @LRIA ABCFKB ORIBP
PR@E >P, g)C QEB PQ>QB FP DOB>QBO QE>K 0.5h LO gjIBPP QE>K 0.2.h
Exercise 7.11 Exercise 7.11
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
352
5) H4>?:=4.,7 5) H4>?:=4.,7. )K QEB '>JB LC ,FCB L?GB@Q-LOFBKQBA BU>JMIB, TB RPBA QTL S>OF>?IBP QL HBBM
QO>@H LC FQP PQ>QB: @ROOBKQ >KA MOBSFLRP. 7E>Q FC VLR RPB >K >OO>V QL HBBM QO>@H LC > @BIIfP PQ>QB
EFPQLOV? 4EFP OBI>QBP QL QEB FAB> LC > g@LJMIBU >A>MQFSB PVPQBJ,h LKB QE>Q E>P QEB >?FIFQV QL
>A>MQ >KA @E>KDB FQP ORIBP LSBO QFJB ?V IB>OKFKD COLJ FQP EFPQLOV. 7BfII PBB >K BU>JMIB LC QEFP
FK #E>MQBO 10: .BRO>I .BQTLOHP.
6) ":A492 .077> 6) ":A492 .077>. )K QEBPB ?>PF@ BU>JMIBP, @BIIP E>SB > CFUBA MLPFQFLK LK > DOFA, ?RQ VLR @LRIA
?RFIA > #! TFQE @BIIP QE>Q E>SB KL CFUBA MLPFQFLK >KA FKPQB>A JLSB >?LRQ QEB P@OBBK.
7) #0>?492 7) #0>?492. !KLQEBO CB>QROB LC @LJMIBU PVPQBJP FP QE>Q QEBV @>K ?B KBPQBA. /RO TLOIA QBKAP QL
TLOH QEFP T>V: > @FQV FP > @LJMIBU PVPQBJ LC MBLMIB, > MBOPLK FP > @LJMIBU PVPQBJ LC LOD>KP,
>K LOD>K FP > @LJMIBU PVPQBJ LC @BIIP, >KA PL LK >KA PL CLOQE.
#OB>QB > #! FK TEF@E > MFUBI FP > @BII >KA > @LILO FP FQP PQ>QB.
Exercise 7.12 Exercise 7.12
6FPR>IFWB QEB '>JB LC ,FCB ?V @LILOFKD B>@E @BII >@@LOAFKD QL ELT ILKD FQfP ?BBK >IFSB LO
AB>A. #>K VLR >IPL RPB QEB @BIIfP EFPQLOV QL FKCLOJ QEB ORIBP?
Exercise 7.13 Exercise 7.13
5PB #! ORIBP FK > CIL@HFKD PVPQBJ. 7E>Q FC B>@E ?LFA E>A > PQ>QB (QE>Q MBOE>MP FKCLOJP
FQP PQBBOFKD ?BE>SFLOP) >KA FQP KBFDE?LOELLA @E>KDBA COLJ CO>JB QL CO>JB >P FQ JLSBA
@ILPBO QL LO CROQEBO COLJ LQEBO ?LFAP?
Exercise 7.14 Exercise 7.14
$BPFDK > #! FK TEF@E B>@E @BII FQPBIC FP > PJ>IIBO #! LO > PVPQBJ LC ?LFAP.
Exercise 7.15 Exercise 7.15
4EB .>QROB LC #LAB (S005)
353
The Ecosystem Project The Ecosystem Project
Step 7 Exercise:
Incorporate cellular automata into your ecosystem. Some possibilities:
Give each creature a state. How can that state drive their behavior?
Taking inspiration from CA, how can that state change over time
according to its neighbors states?
Consider the ecosystems world to be a CA. The creatures move from
tile to tile. Each tile has a stateis it land? water? food?
Use a CA to generate a pattern for the design of a creature in your
ecosystem.
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
354
Chapter 8. Fractals Chapter 8. Fractals
I'.A5<9<460.9 :<;@A2?@! 0?621 A52 A2??63621 :.A52:.A606.;
EC2?F <;2 <3 A52: . @=96;A2? 6; :F 2F2
! 5.A2 A52 '2.;< )=.02 .;1 A52 "<05 CB?C2
! 32.? A52 C.;A<? *2?;.?F )2A
*52 )62?=6;@86 G.@82A :.82@ :2 D.;;. 0?F
A;1 . :6996<; :692@ .D.F . /BAA2?39F 39.==21 6A@ D6;4@
&; . 0<91 %<C2:/2? 1.F . :.; ;.:21 B2;<6A $.;129/?<A D.@ /<?;J
Jonathan Coulton, lyrics from Mandelbrot Set
/K@B RMLK > QFJB, ) QLLH > @LROPB FK EFDE P@ELLI @>IIBA g'BLJBQOV.h 0BOE>MP VLR AFA QLL. 9LR
IB>OKBA >?LRQ PE>MBP FK LKB AFJBKPFLK, QTL AFJBKPFLKP, >KA J>V?B BSBK QEOBB. 7E>Q FP QEB
@FO@RJCBOBK@B LC > @FO@IB? 4EB >OB> LC > OB@Q>KDIB? 4EB AFPQ>K@B ?BQTBBK > MLFKQ >KA > IFKB?
#LJB QL QEFKH LC FQ, TBfSB ?BBK PQRAVFKD DBLJBQOV >II >ILKD FK QEFP ?LLH, RPFKD SB@QLOP QL
ABP@OF?B QEB JLQFLK LC ?LAFBP FK #>OQBPF>K PM>@B. 4EFP PLOQ LC DBLJBQOV FP DBKBO>IIV OBCBOOBA
QL >P %R@IFAB>K DBLJBQOV, >CQBO QEB 'OBBH J>QEBJ>QF@F>K %R@IFA.
F64B?2 8.1
4EB .>QROB LC #LAB (S005)
355
&LO RP K>QROB @LABOP, TB E>SB QL >PH QEB NRBPQFLK: #>K TB ABP@OF?B LRO TLOIA TFQE
%R@IFAB>K DBLJBQOV? 4EB ,#$ P@OBBK )fJ PQ>OFKD >Q OFDEQ KLT PROB ILLHP IFHB > OB@Q>KDIB.
!KA QEB MIRJ ) >QB QEFP JLOKFKD FP @FO@RI>O. "RQ TE>Q FC ) TBOB QL ILLH CROQEBO, >KA @LKPFABO
QEB QOBBP QE>Q IFKB QEB PQOBBQ, QEB IB>SBP QE>Q E>KD LCC QELPB QOBBP, QEB IFDEQKFKD COLJ I>PQ
KFDEQfP QERKABOPQLOJ, QEB @>RIFCILTBO ) >QB CLO AFKKBO, QEB ?ILLA SBPPBIP FK JV ?LAV, >KA QEB
JLRKQ>FKP >KA @L>PQIFKBP QE>Q @LSBO I>KA ?BVLKA .BT 9LOH #FQV? -LPQ LC QEB PQRCC VLR CFKA
FK K>QROB @>KKLQ ?B ABP@OF?BA ?V QEB FAB>IFWBA DBLJBQOF@>I CLOJP LC %R@IFAB>K DBLJBQOV. 3L
FC TB T>KQ QL PQ>OQ ?RFIAFKD @LJMRQ>QFLK>I ABPFDKP TFQE M>QQBOKP ?BVLKA QEB PFJMIB PE>MBP
?FFCJM?(), L?=N(), >KA FCH?(), FQfP QFJB CLO RP QL IB>OK >?LRQ QEB @LK@BMQP ?BEFKA >KA
QB@EKFNRBP CLO PFJRI>QFKD QEB DBLJBQOV LC K>QROB: CO>@Q>IP.
8.1 What Is a Fractal? 8.1 What Is a Fractal?
4EB QBOJ 1=,.?,7 1=,.?,7 (COLJ QEB ,>QFK :F57HIG, JB>KFKD g?OLHBKh) T>P @LFKBA ?V QEB
J>QEBJ>QF@F>K "BKLFQ ->KABI?OLQ FK 1975. )K EFP PBJFK>I TLOH g4EB &O>@Q>I 'BLJBQOV LC
.>QROB,h EB ABCFKBP > CO>@Q>I >P g> OLRDE LO CO>DJBKQBA DBLJBQOF@ PE>MB QE>Q @>K ?B PMIFQ
FKQL M>OQP, B>@E LC TEF@E FP (>Q IB>PQ >MMOLUFJ>QBIV) > OBAR@BA-PFWB @LMV LC QEB TELIB.h
,BQfP FIIRPQO>QB QEFP ABCFKFQFLK TFQE QTL PFJMIB BU>JMIBP. &FOPQ, IBQfP QEFKH >?LRQ > QOBB
?O>K@EFKD PQOR@QROB (CLO TEF@E TBfII TOFQB QEB @LAB I>QBO):
F64B?2 8.2: &;2 <3 A52 :<@A D299-8;<D; .;1 ?20<4;6G./92 3?.0A.9 =.AA2?;@ 6@ ;.:21 3<? B2;<6A
$.;129/?<A 56:@293. G2;2?.A6;4 A52 $.;129/?<A @2A 6;C<9C2@ A2@A6;4 A52 =?<=2?A62@ <3 0<:=92E
;B:/2?@ .3A2? A52F .?2 =.@@21 A5?<B45 .; 6A2?.A6C2 3B;0A6<;. D< A52F A2;1 A< 6;36;6AF? D< A52F @A.F
/<B;121? ,5692 . 3.@06;.A6;4 :.A52:.A60.9 16@0B@@6<;, A56@ I2@0.=2-A6:2J .94<?6A5: 6@ . 92@@
=?.0A60.9 :2A5<1 3<? 42;2?.A6;4 3?.0A.9@ A5.; A52 ?20B?@6C2 A205;6>B2@ D2K99 2E.:6;2 6; A56@ 05.=A2?.
<D2C2?, .; 2E.:=92 3<? 42;2?.A6;4 A52 $.;129/?<A @2A 6@ 6;09B121 6; A52 0<12 2E.:=92@.
#E>MQBO 8. &O>@Q>IP
356
.LQF@B ELT QEB QOBB FK &FDROB 8.3 E>P > PFKDIB OLLQ TFQE QTL ?O>K@EBP @LKKB@QBA >Q FQP BKA.
%>@E LKB LC QELPB ?O>K@EBP E>P QTL ?O>K@EBP >Q FQP BKA >KA QELPB ?O>K@EBP E>SB QTL
?O>K@EBP >KA PL LK >KA PL CLOQE. 7E>Q FC TB TBOB QL MIR@H LKB ?O>K@E COLJ QEB QOBB >KA
BU>JFKB FQ LK FQP LTK?
,LLHFKD @ILPBIV >Q > DFSBK PB@QFLK LC QEB QOBB, TB CFKA QE>Q QEB PE>MB LC QEFP ?O>K@E OBPBJ?IBP
QEB QOBB FQPBIC. 4EFP FP HKLTK >P >071->4847,=4?D >071->4847,=4?D; >P ->KABI?OLQ PQ>QBA, B>@E M>OQ FP > gOBAR@BA-
PFWB @LMV LC QEB TELIB.h
4EB >?LSB QOBB FP MBOCB@QIV PVJJBQOF@>I >KA QEB M>OQP >OB, FK C>@Q, BU>@Q OBMIF@>P LC QEB TELIB.
(LTBSBO, CO>@Q>IP AL KLQ E>SB QL ?B MBOCB@QIV PBIC-PFJFI>O. ,BQfP Q>HB > ILLH >Q > DO>ME LC QEB
PQL@H J>OHBQ (>A>MQBA COLJ >@QR>I !MMIB PQL@H A>Q>).
!KA LKB JLOB.
F64B?2 8.3
F64B?2 8.4
F64B?2 8.5: G?.=5 A
4EB .>QROB LC #LAB (S005)
357
)K QEBPB DO>MEP, QEB U->UFP FP QFJB >KA QEB V->UFP FP QEB PQL@HfP S>IRB. )QfP KLQ >K >@@FABKQ QE>Q
) LJFQQBA QEB I>?BIP, ELTBSBO. 'O>MEP LC PQL@H J>OHBQ A>Q> >OB BU>JMIBP LC CO>@Q>IP ?B@>RPB
QEBV ILLH QEB P>JB >Q >KV P@>IB. !OB QEBPB DO>MEP LC QEB PQL@H LSBO LKB VB>O? /KB A>V?
/KB ELRO? 4EBOBfP KL T>V CLO VLR QL HKLT TFQELRQ > I>?BI. ()K@FABKQ>IIV, DO>ME ! PELTP PFU
JLKQEPf TLOQE LC A>Q> >KA DO>ME " WLLJP FKQL > QFKV M>OQ LC DO>ME !, PELTFKD PFU ELROP.)
4EFP FP >K BU>JMIB LC > >?:.3,>?4. >?:.3,>?4. CO>@Q>I, JB>KFKD QE>Q FQ FP ?RFIQ LRQ LC MOL?>?FIFQFBP >KA
O>KALJKBPP. 5KIFHB QEB ABQBOJFKFPQF@ QOBB-?O>K@EFKD PQOR@QROB, FQ FP PQ>QFPQF@>IIV PBIC-PFJFI>O.
!P TB DL QEOLRDE QEB BU>JMIBP FK QEFP @E>MQBO, TB TFII ILLH >Q ?LQE ABQBOJFKFPQF@ >KA
PQL@E>PQF@ QB@EKFNRBP CLO DBKBO>QFKD CO>@Q>I M>QQBOKP.
7EFIB PBIC-PFJFI>OFQV FP > HBV QO>FQ LC CO>@Q>IP, FQfP FJMLOQ>KQ QL OB>IFWB QE>Q PBIC-PFJFI>OFQV >ILKB
ALBP KLQ J>HB > CO>@Q>I. !CQBO >II, > IFKB FP PBIC-PFJFI>O. ! IFKB ILLHP QEB P>JB >Q >KV P@>IB,
>KA @>K ?B QELRDEQ LC >P @LJMOFPFKD ILQP LC IFQQIB IFKBP. "RQ FQfP KLQ > CO>@Q>I. &O>@Q>IP >OB
@E>O>@QBOFWBA ?V E>SFKD > CFKB PQOR@QROB >Q PJ>II P@>IBP (HBBM WLLJFKD FKQL QEB PQL@H J>OHBQ
DO>ME >KA VLRfII @LKQFKRB QL CFKA CIR@QR>QFLKP) >KA @>KKLQ ?B ABP@OF?BA TFQE %R@IFAB>K
DBLJBQOV. )C VLR @>K P>V g)QfP > IFKB!h QEBK FQfP KLQ > CO>@Q>I.
!KLQEBO CRKA>JBKQ>I @LJMLKBKQ LC CO>@Q>I DBLJBQOV FP OB@ROPFLK. &O>@Q>IP >II E>SB >
OB@ROPFSB ABCFKFQFLK. 7BfII PQ>OQ TFQE OB@ROPFLK ?BCLOB ABSBILMFKD QB@EKFNRBP >KA @LAB
BU>JMIBP CLO ?RFIAFKD CO>@Q>I M>QQBOKP FK 0OL@BPPFKD.
F64B?2 8.6: G?.=5 B
F64B?2 8.7
8.2 Recursion 8.2 Recursion
,BQfP ?BDFK LRO AFP@RPPFLK LC OB@ROPFLK ?V BU>JFKFKD QEB CFOPQ >MMB>O>K@B LC CO>@Q>IP FK
JLABOK J>QEBJ>QF@P. )K 1883, 'BOJ>K J>QEBJ>QF@F>K 'BLODB #>KQLO ABSBILMBA PFJMIB
ORIBP QL DBKBO>QB >K FKCFKFQB PBQ:
#E>MQBO 8. &O>@Q>IP
358
4EBOB FP > CBBA?>@H ILLM >Q TLOH EBOB. 4>HB > PFKDIB IFKB >KA ?OB>H FQ FKQL QTL. 4EBK OBQROK QL
QELPB QTL IFKBP >KA >MMIV QEB P>JB ORIB, ?OB>HFKD B>@E IFKB FKQL QTL, >KA KLT TBfOB IBCQ TFQE
CLRO. 4EBK OBQROK QL QELPB CLRO IFKBP >KA >MMIV QEB ORIB. .LT VLRfSB DLQ BFDEQ. 4EFP MOL@BPP FP
HKLTK >P =0.@=>4:9 =0.@=>4:9: QEB OBMB>QBA >MMIF@>QFLK LC > ORIB QL PR@@BPPFSB OBPRIQP. #>KQLO T>P
FKQBOBPQBA FK TE>Q E>MMBKP TEBK VLR >MMIV QEBPB ORIBP >K FKCFKFQB KRJ?BO LC QFJBP. 7B,
ELTBSBO, >OB TLOHFKD FK > CFKFQB MFUBI PM>@B >KA @>K JLPQIV FDKLOB QEB NRBPQFLKP >KA
M>O>ALUBP QE>Q >OFPB COLJ FKCFKFQB OB@ROPFLK. 7B TFII FKPQB>A @LKPQOR@Q LRO @LAB FK PR@E > T>V
QE>Q TB AL KLQ >MMIV QEB ORIBP CLOBSBO (TEF@E TLRIA @>RPB LRO MOLDO>J QL COBBWB).
"BCLOB TB FJMIBJBKQ QEB #>KQLO PBQ, IBQfP Q>HB > ILLH >Q TE>Q FQ JB>KP QL E>SB OB@ROPFLK FK
@LAB. (BOBfP PLJBQEFKD TBfOB RPBA QL ALFKD >II QEB QFJBd@>IIFKD > CRK@QFLK FKPFAB >KLQEBO
CRK@QFLK.
7E>Q TLRIA E>MMBK FC TB @>IIBA QEB CRK@QFLK TB >OB ABCFKFKD TFQEFK QEB CRK@QFLK FQPBIC? #>K
MIG?$OH=NCIH() @>II MIG?$OH=NCIH()?
)K C>@Q, QEFP FP KLQ LKIV >IILTBA, ?RQ FQfP NRFQB @LJJLK (>KA BPPBKQF>I QL ELT TB TFII FJMIBJBKQ
QEB #>KQLO PBQ). &RK@QFLKP QE>Q @>II QEBJPBISBP >OB F97IFG=J9 >KA DLLA CLO PLISFKD @BOQ>FK
MOL?IBJP. &LO BU>JMIB, @BOQ>FK J>QEBJ>QF@>I @>I@RI>QFLKP >OB FJMIBJBKQBA OB@ROPFSBIV; QEB
JLPQ @LJJLK BU>JMIB FP :57HCF=5@.
4EB C>@QLOF>I LC >KV KRJ?BO K, RPR>IIV TOFQQBK >P K!, FP ABCFKBA >P:
D! = D * D W 1 * . . . . * 3 * 2 * 1
0! = 1
(BOB TBfII TOFQB > CRK@QFLK FK 0OL@BPPFKD QE>Q RPBP > @IL ILLM QL @>I@RI>QB C>@QLOF>I:
F64B?2 8.8: *52 C.;A<? @2A
PIC> MIG?$OH=NCIH() U
Calling the function background() in the
definition of someFunction()
<;=EALIOH>(0);
W
PIC> MIG?$OH=NCIH() U
MIG?$OH=NCIH();
W
CHN @;=NILC;F(CHN H) U
CHN @ = 1;
4EB .>QROB LC #LAB (S005)
359
5MLK @ILPB BU>JFK>QFLK, VLRfII KLQF@B PLJBQEFKD FKQBOBPQFKD >?LRQ ELT C>@QLOF>I TLOHP. ,BQfP
ILLH >Q 4! >KA 3!
4! = 4 * 3 * 2 * 1
3! = 3 * 2 * 1
?30=01:=0. . . ?30=01:=0. . .
4! = 4 * 3!
)K JLOB DBKBO>I QBOJP, CLO >KV MLPFQFSB FKQBDBO K:
D! = D * (D-1)!
1! = 1
7OFQQBK LRQ:
4EB :57HCF=5@ LC H FP ABCFKBA >P H QFJBP QEB :57HCF=5@ LC H-1.
4EB ABCFKFQFLK LC 1,.?:=4,7 1,.?:=4,7 FK@IRABP 1,.?:=4,7 1,.?:=4,7?! )QfP HFKA LC IFHB ABCFKFKD gQFOBA" >P gQEB CBBIFKD
VLR DBQ TEBK VLR >OB QFOBA.h 4EFP @LK@BMQ LC PBIC-OBCBOBK@B FK CRK@QFLKP FP >K BU>JMIB LC
OB@ROPFLK. !KA TB @>K RPB FQ QL TOFQB > C>@QLOF>I CRK@QFLK QE>Q @>IIP FQPBIC.
)Q J>V ILLH @O>WV, ?RQ FQ TLOHP. (BOB >OB QEB PQBMP QE>Q E>MMBK TEBK @;=NILC;F(4) FP
@>IIBA.
Using a regular loop to compute factorial @IL (CHN C = 0; C < H; C++) U
@ = @ * (C+1);
W
L?NOLH @;
W
CHN @;=NILC;F(CHN H) U
C@ (H == 1) U
L?NOLH 1;
W ?FM? U
L?NOLH H * @;=NILC;F(H-1);
W
W
#E>MQBO 8. &O>@Q>IP
360
7B @>K >MMIV QEB P>JB MOFK@FMIB QL DO>MEF@P TFQE FKQBOBPQFKD OBPRIQP, >P TB TFII PBB FK J>KV
BU>JMIBP QEOLRDELRQ QEFP @E>MQBO. 4>HB > ILLH >Q QEFP OB@ROPFSB CRK@QFLK.
Example 8.1: Recursive Circles I
>L;Q!CL=F?() AO>TP >K BIIFMPB ?>PBA LK > PBQ LC M>O>JBQBOP QE>Q FQ OB@BFSBP >P >ODRJBKQP. )Q
QEBK @>IIP FQPBIC TFQE QELPB P>JB M>O>JBQBOP, >AGRPQFKD QEBJ PIFDEQIV. 4EB OBPRIQ FP > PBOFBP LC
@FO@IBP, B>@E LC TEF@E FP AO>TK FKPFAB QEB MOBSFLRP @FO@IB.
F64B?2 8.9
PIC> >L;Q!CL=F?(CHN R, CHN S, @FI;N L;>COM) U
?FFCJM?(R, S, L;>COM, L;>COM);
C@(L;>COM > 2) U
L;>COM *= 0.75@;
The drawCircle() function is calling itself
recursively.
>L;Q!CL=F?(R, S, L;>COM);
W
W
4EB .>QROB LC #LAB (S005)
361
.LQF@B QE>Q QEB >?LSB CRK@QFLK LKIV OB@ROPFSBIV @>IIP FQPBIC FC QEB O>AFRP FP DOB>QBO QE>K 2. 4EFP
FP > @OR@F>I MLFKQ. !P TFQE FQBO>QFLK, 5@@ F97IFG=J9 :IB7H=CBG AIGH <5J9 5B 9L=H 7CB8=H=CB! 9LR
IFHBIV >OB >IOB>AV >T>OB QE>Q >II @IL >KA QBCF? ILLMP JRPQ FK@IRAB > ?LLIB>K BUMOBPPFLK
QE>Q BSBKQR>IIV BS>IR>QBP QL C>IPB, QERP BUFQFKD QEB ILLM. 7FQELRQ LKB, QEB MOLDO>J TLRIA
@O>PE, @>RDEQ FKPFAB LC >K FKCFKFQB ILLM. 4EB P>JB @>K ?B P>FA >?LRQ OB@ROPFLK. )C > OB@ROPFSB
CRK@QFLK @>IIP FQPBIC CLOBSBO >KA BSBO, VLRfII ?B JLPQ IFHBIV ?B QOB>QBA QL > KF@B COLWBK P@OBBK.
4EFP @FO@IBP BU>JMIB FP O>QEBO QOFSF>I; FQ @LRIA B>PFIV ?B >@EFBSBA QEOLRDE PFJMIB FQBO>QFLK.
(LTBSBO, CLO P@BK>OFLP FK TEF@E > CRK@QFLK @>IIP FQPBIC JLOB QE>K LK@B, OB@ROPFLK ?B@LJBP
TLKABOCRIIV BIBD>KQ.
,BQfP J>HB >L;Q!CL=F?() > ?FQ JLOB @LJMIBU. &LO BSBOV @FO@IB AFPMI>VBA, AO>T > @FO@IB E>IC
FQP PFWB QL QEB IBCQ >KA OFDEQ LC QE>Q @FO@IB.
Example 8.2: Recursion twice
PIC> M?NOJ() U
MCT?(400,400);
MGIINB();
W
PIC> >L;Q() U
<;=EALIOH>(255);
>L;Q!CL=F?(QC>NB/2,B?CABN/2,200);
W
PIC> >L;Q!CL=F?(@FI;N R, @FI;N S, @FI;N L;>COM) U
MNLIE?(0);
HI$CFF();
?FFCJM?(R, S, L;>COM, L;>COM);
C@(L;>COM > 2) U
drawCircle() calls itself twice, creating a
branching effect. For every circle, a
smaller circle is drawn to the left and the
right.
>L;Q!CL=F?(R + L;>COM/2, S, L;>COM/2);
>L;Q!CL=F?(R - L;>COM/2, S, L;>COM/2);
W
W
#E>MQBO 8. &O>@Q>IP
362
7FQE GRPQ > IFQQIB JLOB @LAB, TB @LRIA >IPL >AA > @FO@IB >?LSB >KA ?BILT B>@E @FO@IB.
Example 8.3: Recursion four times
4OV OBMOLAR@FKD QEFP PHBQ@E TFQE FQBO>QFLK FKPQB>A LC OB@ROPFLKd) A>OB VLR!
PIC> >L;Q!CL=F?(@FI;N R, @FI;N S, @FI;N L;>COM) U
?FFCJM?(R, S, L;>COM, L;>COM);
C@(L;>COM > 8) U
>L;Q!CL=F?(R + L;>COM/2, S, L;>COM/2);
>L;Q!CL=F?(R - L;>COM/2, S, L;>COM/2);
>L;Q!CL=F?(R, S + L;>COM/2, L;>COM/2);
>L;Q!CL=F?(R, S - L;>COM/2, L;>COM/2);
W
W
8.3 The Cantor Set with a Recursive Function 8.3 The Cantor Set with a Recursive Function
.LT TBfOB OB>AV QL SFPR>IFWB QEB #>KQLO PBQ FK 0OL@BPPFKD RPFKD > OB@ROPFSB CRK@QFLK. 7EBOB
AL TB ?BDFK? 7BII, TB HKLT QE>Q QEB #>KQLO PBQ ?BDFKP TFQE > IFKB. 3L IBQfP PQ>OQ QEBOB >KA
TOFQB > CRK@QFLK QE>Q AO>TP > IFKB.
4EB >?LSB =;HNIL() CRK@QFLK AO>TP > IFKB QE>Q PQ>OQP >Q MFUBI @LLOAFK>QB (L,M) TFQE > IBKDQE LC
F?H. (4EB IFKB FP AO>TK ELOFWLKQ>IIV EBOB, ?RQ QEFP FP >K >O?FQO>OV AB@FPFLK.) 3L FC TB @>IIBA QE>Q
CRK@QFLK, P>VFKD:
TBfA DBQ QEB CLIILTFKD:
PIC> =;HNIL(@FI;N R, @FI;N S, @FI;N F?H) U
FCH?(R,S,R+F?H,S);
W
=;HNIL(10, 20, QC>NB-20);
4EB .>QROB LC #LAB (S005)
363
.LT, QEB #>KQLO ORIB QBIIP RP QL BO>PB QEB
JFAAIB QEFOA LC QE>Q IFKB, TEF@E IB>SBP RP
TFQE QTL IFKBP, LKB COLJ QEB ?BDFKKFKD LC
QEB IFKB QL QEB LKB-QEFOA J>OH, >KA LKB COLJ
QEB QTL-QEFOAP J>OH QL QEB BKA LC QEB IFKB.
7B @>K KLT >AA QTL JLOB IFKBP LC @LAB QL
AO>T QEB PB@LKA M>FO LC IFKBP, JLSFKD QEB
V-IL@>QFLK ALTK > ?RK@E LC MFUBIP PL QE>Q
TB @>K PBB QEB OBPRIQ ?BILT QEB LOFDFK>I
IFKB.
7EFIB QEFP FP > CFKB PQ>OQ, PR@E > J>KR>I >MMOL>@E LC @>IIFKD FCH?() CLO B>@E IFKB FP KLQ TE>Q
TB T>KQ. )Q TFII DBQ RKTFBIAV SBOV NRF@HIV, >P TBfA KBBA CLRO, QEBK BFDEQ, QEBK PFUQBBK @>IIP QL
FCH?(). 9BP, > @IL ILLM FP LRO RPR>I T>V >OLRKA PR@E > MOL?IBJ, ?RQ DFSB QE>Q > QOV >KA
VLRfII PBB QE>Q TLOHFKD LRQ QEB J>QE CLO B>@E FQBO>QFLK NRF@HIV MOLSBP FKLOAFK>QBIV
@LJMIF@>QBA. (BOB FP TEBOB OB@ROPFLK @LJBP >KA OBP@RBP RP.
4>HB > ILLH >Q TEBOB TB AO>T QE>Q CFOPQ IFKB COLJ QEB PQ>OQ QL QEB LKB-QEFOA J>OH.
)KPQB>A LC @>IIFKD QEB FCH?() CRK@QFLK AFOB@QIV, TB @>K PFJMIV @>II QEB =;HNIL() CRK@QFLK
FQPBIC. !CQBO >II, TE>Q ALBP QEB =;HNIL() CRK@QFLK AL? )Q AO>TP > IFKB >Q >K (L,M) IL@>QFLK TFQE >
DFSBK IBKDQE! !KA PL:
F64B?2 8.10
F64B?2 8.11
PIC> =;HNIL(@FI;N R, @FI;N S, @FI;N F?H) U
FCH?(R,S,R+F?H,S);
S += 20;
From start to 1/3rd B?D;(N,O,N+B;D/3,O);
From 2/3rd to end B?D;(N+B;D*2/3,O,N+B;D,O);
W
F64B?2 8.12
FCH?(R,S,R+F?H/3,S);
FCH?(R,S,R+F?H/3,S); <?=IG?M -------> =;HNIL(R,S,F?H/3);
#E>MQBO 8. &O>@Q>IP
364
!KA CLO QEB PB@LKA IFKB:
,B>SFKD RP TFQE:
!KA PFK@B QEB =;HNIL() CRK@QFLK FP @>IIBA OB@ROPFSBIV, QEB P>JB ORIB TFII ?B >MMIFBA QL QEB KBUQ
IFKBP >KA QL QEB KBUQ >KA QL QEB KBUQ >P =;HNIL() @>IIP FQPBIC >D>FK >KA >D>FK! .LT, ALKfQ DL
>KA ORK QEFP @LAB VBQ. 7BfOB JFPPFKD QE>Q @OR@F>I BIBJBKQ: >K BUFQ @LKAFQFLK. 7BfII T>KQ QL
J>HB PROB TB PQLM >Q PLJB MLFKQdCLO BU>JMIB, FC QEB IBKDQE LC QEB IFKB BSBO FP IBPP QE>K 1
MFUBI.
Example 8.4: Cantor set
FCH?(R+F?H*2/3,S,R+F?H,S); <?=IG?M -------> =;HNIL(R+F?H*2/3,S,F?H/3);
PIC> =;HNIL(@FI;N R, @FI;N S, @FI;N F?H) U
FCH?(R,S,R+F?H,S);
S += 20;
=;HNIL(R,S,F?H/3);
=;HNIL(R+F?H*2/3,S,F?H/3);
W
PIC> =;HNIL(@FI;N R, @FI;N S, @FI;N F?H) U
Stop at 1 pixel! C@ (F?H >= 1) U
FCH?(R,S,R+F?H,S);
S += 20;
=;HNIL(R,S,F?H/3);
=;HNIL(R+F?H*2/3,S,F?H/3);
W
W
4EB .>QROB LC #LAB (S005)
365
%UBO@FPB: 5PFKD >L;Q!CL=F?() >KA QEB #>KQLO PBQ >P JLABIP, DBKBO>QB VLRO LTK
M>QQBOK TFQE OB@ROPFLK. (BOB FP > P@OBBKPELQ LC LKB QE>Q RPBP IFKBP.
Exercise 8.1 Exercise 8.1
8.4 The Koch Curve and the ArrayList Technique 8.4 The Koch Curve and the ArrayList Technique
7OFQFKD > CRK@QFLK QE>Q OB@ROPFSBIV @>IIP FQPBIC FP LKB QB@EKFNRB CLO DBKBO>QFKD > CO>@Q>I
M>QQBOK LK P@OBBK. (LTBSBO, TE>Q FC VLR T>KQBA QEB IFKBP FK QEB >?LSB #>KQLO PBQ QL BUFPQ >P
FKAFSFAR>I L?GB@QP QE>Q @LRIA ?B JLSBA FKABMBKABKQIV? 4EB OB@ROPFSB CRK@QFLK FP PFJMIB >KA
BIBD>KQ, ?RQ FQ ALBP KLQ >IILT VLR QL AL JR@E ?BPFABP PFJMIV DBKBO>QFKD QEB M>QQBOK FQPBIC.
(LTBSBO, QEBOB FP >KLQEBO T>V TB @>K >MMIV OB@ROPFLK FK @LJ?FK>QFLK TFQE >K ALL;S*CMN
QE>Q TFII >IILT RP QL KLQ LKIV DBKBO>QB > CO>@Q>I M>QQBOK, ?RQ HBBM QO>@H LC >II FQP FKAFSFAR>I
M>OQP >P L?GB@QP.
4L ABJLKPQO>QB QEFP QB@EKFNRB, IBQfP ILLH >Q >KLQEBO C>JLRP CO>@Q>I M>QQBOK, AFP@LSBOBA FK
1904 ?V 3TBAFPE J>QEBJ>QF@F>K (BIDB SLK +L@E. (BOB >OB QEB ORIBP. (.LQB QE>Q FQ PQ>OQP QEB
P>JB T>V >P QEB #>KQLO PBQ, TFQE > PFKDIB IFKB.)
#E>MQBO 8. &O>@Q>IP
366
4EB OBPRIQ ILLHP IFHB:
The Monster Curve The Monster Curve
4EB +L@E @ROSB >KA LQEBO CO>@Q>I M>QQBOKP >OB LCQBK @>IIBA gJ>QEBJ>QF@>I JLKPQBOP.h
4EFP FP ARB QL >K LAA M>O>ALU QE>Q BJBODBP TEBK VLR >MMIV QEB OB@ROPFSB ABCFKFQFLK >K
FKCFKFQB KRJ?BO LC QFJBP. )C QEB IBKDQE LC QEB LOFDFK>I PQ>OQFKD IFKB FP LKB, QEB CFOPQ
FQBO>QFLK LC QEB +L@E @ROSB TFII VFBIA > IFKB LC IBKDQE CLRO-QEFOAP (B>@E PBDJBKQ FP LKB-
QEFOA QEB IBKDQE LC QEB PQ>OQFKD IFKB). $L FQ >D>FK >KA VLR DBQ > IBKDQE LC PFUQBBK-KFKQEP.
!P VLR FQBO>QB QLT>OAP FKCFKFQV, QEB IBKDQE LC QEB +L@E @ROSB >MMOL>@EBP FKCFKFQV. 9BQ FQ
CFQP FK QEB QFKV CFKFQB PM>@B MOLSFABA OFDEQ EBOB LK QEFP M>MBO (LO P@OBBK)!
3FK@B TB >OB TLOHFKD FK QEB 0OL@BPPFKD I>KA LC CFKFQB MFUBIP, QEFP QEBLOBQF@>I M>O>ALU
TLKfQ ?B > C>@QLO CLO RP. 7BfII E>SB QL IFJFQ QEB KRJ?BO LC QFJBP TB OB@ROPFSBIV >MMIV
QEB +L@E ORIBP PL QE>Q LRO MOLDO>J TLKfQ ORK LRQ LC JBJLOV LO @O>PE.
7B @LRIA MOL@BBA FK QEB P>JB J>KKBO >P TB AFA TFQE QEB #>KQLO PBQ, >KA TOFQB > OB@ROPFSB
CRK@QFLK QE>Q FQBO>QFSBIV >MMIFBP QEB +L@E ORIBP LSBO >KA LSBO. .BSBOQEBIBPP, TB >OB DLFKD QL
Q>@HIB QEFP MOL?IBJ FK > AFCCBOBKQ J>KKBO ?V QOB>QFKD B>@E PBDJBKQ LC QEB +L@E @ROSB >P >K
FKAFSFAR>I L?GB@Q. 4EFP TFII LMBK RM PLJB ABPFDK MLPPF?FIFQFBP. &LO BU>JMIB, FC B>@E PBDJBKQ FP
F64B?2 8.13
F64B?2 8.14
4EB .>QROB LC #LAB (S005)
367
>K L?GB@Q, TB @LRIA >IILT B>@E PBDJBKQ QL JLSB FKABMBKABKQIV COLJ FQP LOFDFK>I IL@>QFLK
>KA M>OQF@FM>QB FK > MEVPF@P PFJRI>QFLK. )K >AAFQFLK, TB @LRIA RPB > O>KALJ @LILO, IFKB
QEF@HKBPP, BQ@. QL AFPMI>V B>@E PBDJBKQ AFCCBOBKQIV.
)K LOABO QL >@@LJMIFPE LRO DL>I LC QOB>QFKD B>@E PBDJBKQ >P >K FKAFSFAR>I L?GB@Q, TB JRPQ
CFOPQ AB@FAB TE>Q QEFP L?GB@Q PELRIA ?B FK QEB CFOPQ MI>@B. 7E>Q A>Q> PELRIA FQ PQLOB? 7E>Q
CRK@QFLKP PELRIA FQ E>SB?
4EB +L@E @ROSB FP > PBOFBP LC @LKKB@QBA IFKBP, >KA PL TB TFII QEFKH LC B>@E PBDJBKQ >P >
g+L@E,FKB.h %>@E +L@E IFKB E>P > PQ>OQ MLFKQ (g>h) >KA >K BKA MLFKQ (g?h). 4EBPB MLFKQP >OB
.3?=NIL L?GB@QP, >KA QEB IFKB FP AO>TK TFQE 0OL@BPPFKDfP FCH?() CRK@QFLK.
.LT QE>Q TB E>SB LRO )I=B*CH? @I>PP, TB @>K DBQ PQ>OQBA LK QEB J>FK MOLDO>J. 7BfII KBBA
> A>Q> PQOR@QROB QL HBBM QO>@H LC TE>Q TFII BSBKQR>IIV ?B@LJB J>KV )I=B*CH? L?GB@QP, >KA
>K ALL;S*CMN (PBB #E>MQBO 4 CLO > OBSFBT LC ALL;S*CMNP) TFII AL GRPQ CFKB.
)K M?NOJ(), TBfII T>KQ QL @OB>QB QEB ALL;S*CMN >KA >AA QEB CFOPQ IFKB PBDJBKQ QL FQ, > IFKB
QE>Q PQOBQ@EBP COLJ 0 QL QEB TFAQE LC QEB PHBQ@E.
=F;MM )I=B*CH? U
A line between two points: start and end .3?=NIL MN;LN;
.3?=NIL ?H>;
)I=B*CH?(.3?=NIL ;, .3?=NIL <) U
MN;LN = ;.A?N();
?H> = <.A?N();
W
PIC> >CMJF;S() U
MNLIE?(0);
Draw the line from PVector start to end. FCH?(MN;LN.R, MN;LN.S, ?H>.R, ?H>.S);
W
W
ALL;S*CMN<)I=B*CH?> FCH?M;
PIC> M?NOJ() U
MCT?(600, 300);
Create the ArrayList. FCH?M = H?Q ALL;S*CMN<)I=B*CH?>();
Left side of window .3?=NIL MN;LN = H?Q .3?=NIL(0, 200);
Right side of window .3?=NIL ?H> = H?Q .3?=NIL(QC>NB, 200);
#E>MQBO 8. &O>@Q>IP
368
4EBK FK >L;Q(), >II )I=B*CH? L?GB@QP (GRPQ LKB OFDEQ KLT) @>K ?B AFPMI>VBA FK > ILLM.
4EFP FP LRO CLRKA>QFLK. ,BQfP OBSFBT TE>Q TB E>SB PL C>O:
i :.3!490 .7,>>: :.3!490 .7,>>: ! @I>PP QL HBBM QO>@H LC > IFKB COLJ MLFKQ ! QL ".
i A==,D!4>?: A==,D!4>?: ! IFPQ LC >II )I=B*CH? L?GB@QP.
7FQE QEB >?LSB BIBJBKQP, ELT >KA TEBOB AL TB >MMIV +L@E ORIBP >KA MOFK@FMIBP LC OB@ROPFLK?
2BJBJ?BO QEB '>JB LC ,FCB @BIIRI>O >RQLJ>Q>? )K QE>Q PFJRI>QFLK, TB >IT>VP HBMQ QO>@H LC QTL
DBKBO>QFLKP: @ROOBKQ >KA KBUQ. 7EBK TB TBOB CFKFPEBA @LJMRQFKD QEB KBUQ DBKBO>QFLK, KBUQ
?B@>JB @ROOBKQ >KA TB JLSBA LK QL @LJMRQFKD QEB KBT KBUQ DBKBO>QFLK. 7B >OB DLFKD QL
>MMIV > PFJFI>O QB@EKFNRB EBOB. 7B E>SB >K ALL;S*CMN QE>Q HBBMP QO>@H LC QEB @ROOBKQ PBQ LC
)I=B*CH? L?GB@QP (>Q QEB PQ>OQ LC QEB MOLDO>J, QEBOB FP LKIV LKB). 7B TFII KBBA > PB@LKA
ALL;S*CMN (IBQfP @>II FQ gKBUQh) TEBOB TB TFII MI>@B >II QEB KBT )I=B*CH? L?GB@QP QE>Q >OB
DBKBO>QBA COLJ >MMIVFKD QEB +L@E ORIBP. &LO BSBOV )I=B*CH? L?GB@Q FK QEB @ROOBKQ ALL;S*CMN,
CLRO KBT )I=B*CH? L?GB@QP >OB >AABA QL QEB KBUQ ALL;S*CMN. 7EBK TBfOB ALKB, QEB KBUQ
ALL;S*CMN ?B@LJBP QEB @ROOBKQ LKB.
The first KochLine object FCH?M.;>>(H?Q )I=B*CH?(MN;LN, ?H>));
W
PIC> >L;Q() U
<;=EALIOH>(255);
@IL ()I=B*CH? F : FCH?M) U
F.>CMJF;S();
W
W
F64B?2 8.15
4EB .>QROB LC #LAB (S005)
369
(BOBfP ELT QEB @LAB TFII ILLH:
"V @>IIFKD A?H?L;N?() LSBO >KA LSBO >D>FK (CLO BU>JMIB, B>@E QFJB QEB JLRPB FP MOBPPBA),
TB OB@ROPFSBIV >MMIV QEB +L@E @ROSB ORIBP QL QEB BUFPQFKD PBQ LC )I=B*CH? L?GB@QP. /C
@LROPB, QEB >?LSB LJFQP QEB OB>I gTLOHh EBOB, TEF@E FP CFDROFKD LRQ QELPB ORIBP. (LT AL TB
?OB>H LKB IFKB PBDJBKQ FKQL CLRO >P ABP@OF?BA ?V QEB ORIBP? 7EFIB QEFP @>K ?B >@@LJMIFPEBA
TFQE PLJB PFJMIB >OFQEJBQF@ >KA QOFDLKLJBQOV, PFK@B LRO )I=B*CH? L?GB@Q RPBP .3?=NIL, QEFP
FP > KF@B LMMLOQRKFQV CLO RP QL MO>@QF@B LRO SB@QLO J>QE. ,BQfP BPQ>?IFPE ELT J>KV MLFKQP TB
KBBA QL @LJMRQB CLO B>@E )I=B*CH? L?GB@Q.
!P VLR @>K PBB COLJ QEB >?LSB CFDROB, TB KBBA CFSB MLFKQP (>, ?, @, A, >KA B) QL DBKBO>QB QEB
KBT )I=B*CH? L?GB@QP >KA J>HB QEB KBT IFKB PBDJBKQP (>?, @?, @A, >KA AB).
7EBOB AL TB DBQ QEBPB MLFKQP? 3FK@B TB E>SB > )I=B*CH? L?GB@Q, TEV KLQ >PH QEB
)I=B*CH? L?GB@Q QL @LJMRQB >II QEBPB MLFKQP CLO RP?
PIC> A?H?L;N?() U
Create the next ArrayList... ALL;S*CMN H?RN = H?Q ALL;S*CMN<)I=B*CH?>();
...for every current line. @IL ()I=B*CH? F : FCH?M) U
Add four new lines. (We need to figure out
how to compute the locations of these
lines!)
H?RN.;>>(H?Q )I=B*CH?(???,???));
H?RN.;>>(H?Q )I=B*CH?(???,???));
H?RN.;>>(H?Q )I=B*CH?(???,???));
H?RN.;>>(H?Q )I=B*CH?(???,???));
W
The new ArrayList is now the one we care
about!
FCH?M = H?RN;
W
F64B?2 8.16
H?RN.;>>(H?Q )I=B*CH?(;,<));
H?RN.;>>(H?Q )I=B*CH?(<,=));
H?RN.;>>(H?Q )I=B*CH?(=,>));
H?RN.;>>(H?Q )I=B*CH?(>,?));
PIC> A?H?L;N?() U
ALL;S*CMN H?RN = H?Q ALL;S*CMN<)I=B*CH?>();
@IL ()I=B*CH? F : FCH?M) U
#E>MQBO 8. &O>@Q>IP
370
.LT TB GRPQ KBBA QL TOFQB CFSB KBT CRK@QFLKP FK QEB )I=B*CH? @I>PP, B>@E LKB OBQROKFKD >
.3?=NIL >@@LOAFKD QL &FDROB 8.16 (PBB M>DB 370) >?LSB. ,BQfP HKL@H LCC EI=BA() >KA EI=B#()
CFOPQ, TEF@E >OB PFJMIV QEB PQ>OQ >KA BKA MLFKQP LC QEB LOFDFK>I IFKB.
.LT IBQfP JLSB LK QL MLFKQP " >KA $. " FP LKB-QEFOA LC QEB T>V >ILKD QEB IFKB PBDJBKQ >KA $ FP
QTL-QEFOAP. (BOB TB @>K J>HB > .3?=NIL QE>Q MLFKQP COLJ PQ>OQ QL BKA >KA PEOFKH FQ QL LKB-QEFOA
QEB IBKDQE CLO " >KA QTL-QEFOAP QEB IBKDQE CLO $ QL CFKA QEBPB MLFKQP.
The KochLine object has five functions,
each of which return a PVector according to
the Koch rules.
.3?=NIL ; = F.EI=BA();
.3?=NIL < = F.EI=B();
.3?=NIL = = F.EI=B!();
.3?=NIL > = F.EI=B"();
.3?=NIL ? = F.EI=B#();
H?RN.;>>(H?Q )I=B*CH?(;, <));
H?RN.;>>(H?Q )I=B*CH?(<, =));
H?RN.;>>(H?Q )I=B*CH?(=, >));
H?RN.;>>(H?Q )I=B*CH?(>, ?));
W
FCH?M = H?RN;
W
.3?=NIL EI=BA() U
Note the use of get(), which returns a copy
of the PVector. As was noted in Chapter 6,
section 14, we want to avoid making copies
whenever possible, but here we will need a
new PVector in case we want the segments
to move independently of each other.
L?NOLH MN;LN.A?N();
W
.3?=NIL EI=B#() U
L?NOLH ?H>.A?N();
W
F64B?2 8.17
.3?=NIL EI=B() U
PVector from start to end .3?=NIL P = .3?=NIL.MO<(?H>, MN;LN);
One-third the length P.>CP(3);
4EB .>QROB LC #LAB (S005)
371
4EB I>PQ MLFKQ, #, FP QEB JLPQ AFCCF@RIQ LKB QL CFKA. (LTBSBO, FC VLR OB@>II QE>Q QEB >KDIBP LC >K
BNRFI>QBO>I QOF>KDIB >OB >II PFUQV ABDOBBP, QEFP J>HBP FQ > IFQQIB ?FQ B>PFBO. )C TB HKLT ELT QL
CFKA MLFKQ " TFQE > .3?=NIL LKB-QEFOA QEB IBKDQE LC QEB IFKB, TE>Q FC TB TBOB QL OLQ>QB QE>Q
P>JB .3?=NIL PFUQV ABDOBBP >KA JLSB >ILKD QE>Q SB@QLO COLJ MLFKQ "? 7BfA ?B >Q MLFKQ #!
0RQQFKD FQ >II QLDBQEBO, FC TB @>II A?H?L;N?() CFSB QFJBP FK M?NOJ(), TBfII PBB QEB CLIILTFKD
OBPRIQ.
Add that PVector to the beginning of the
line to find the new point.
P.;>>(MN;LN);
L?NOLH P;
W
.3?=NIL EI=B"() U
.3?=NIL P = .3?=NIL.MO<(?H>, MN;LN);
Same thing here, only we need to move
two-thirds along the line instead of one-
third.
P.GOFN(2/3.0);
P.;>>(MN;LN);
L?NOLH P;
W
F64B?2 8.18
.3?=NIL EI=B!() U
Start at the beginning. .3?=NIL ; = MN;LN.A?N();
.3?=NIL P = .3?=NIL.MO<(?H>, MN;LN);
Move 1/3rd of the way to point B. P.>CP(3);
;.;>>(P);
Rotate above the line 60 degrees. P.LIN;N?(-L;>C;HM(60));
Move along that vector to point C. ;.;>>(P);
L?NOLH ;;
W
#E>MQBO 8. &O>@Q>IP
372
Example 8.5: Koch curve
ALL;S*CMN<)I=B*CH?> FCH?M;
PIC> M?NOJ() U
MCT?(600, 300);
<;=EALIOH>(255);
FCH?M = H?Q ALL;S*CMN<)I=B*CH?>();
.3?=NIL MN;LN = H?Q .3?=NIL(0, 200);
.3?=NIL ?H> = H?Q .3?=NIL(QC>NB, 200);
FCH?M.;>>(H?Q )I=B*CH?(MN;LN, ?H>));
Arbitrarily apply the Koch rules five times. @IL (CHN C = 0; C < 5; C++) U
A?H?L;N?();
W
W
$O>T QEB +L@E PKLTCI>HB (LO PLJB LQEBO
S>OF>QFLK LC QEB +L@E @ROSB).
Exercise 8.2 Exercise 8.2
4EB .>QROB LC #LAB (S005)
373
4OV >KFJ>QFKD QEB +L@E @ROSB. &LO BU>JMIB, @>K VLR AO>T FQ COLJ IBCQ QL OFDEQ? #>K VLR
S>OV QEB SFPR>I ABPFDK LC QEB IFKB PBDJBKQP? #>K VLR JLSB QEB IFKB PBDJBKQP RPFKD
QB@EKFNRBP COLJ B>OIFBO @E>MQBOP? 7E>Q FC B>@E IFKB PBDJBKQ TBOB J>AB FKQL > PMOFKD
(QLUF@IF?P) LO GLFKQ ("LU2$)?
Exercise 8.3 Exercise 8.3
2BTOFQB QEB #>KQLO PBQ BU>JMIB RPFKD L?GB@QP >KA >K ALL;S*CMN.
Exercise 8.4 Exercise 8.4
$O>T QEB 3FBOMF\PHF QOF>KDIB (>P PBBK FK 7LICO>J BIBJBKQ>OV #!) RPFKD OB@ROPFLK.
Exercise 8.5 Exercise 8.5
8.5 Trees 8.5 Trees
4EB CO>@Q>IP TB E>SB BU>JFKBA FK QEFP @E>MQBO PL C>O >OB ABQBOJFKFPQF@, JB>KFKD QEBV E>SB
KL O>KALJKBPP >KA TFII >IT>VP MOLAR@B QEB FABKQF@>I LRQ@LJB B>@E QFJB QEBV >OB ORK. 4EBV
>OB BU@BIIBKQ ABJLKPQO>QFLKP LC @I>PPF@ CO>@Q>IP >KA QEB MOLDO>JJFKD QB@EKFNRBP ?BEFKA
AO>TFKD QEBJ, ?RQ >OB QLL MOB@FPB QL CBBI B5HIF5@. )K QEFP KBUQ M>OQ LC QEB @E>MQBO, ) T>KQ QL
BU>JFKB PLJB QB@EKFNRBP ?BEFKA DBKBO>QFKD > PQL@E>PQF@ (LO KLK-ABQBOJFKFPQF@) CO>@Q>I. 4EB
BU>JMIB TBfII RPB FP > ?O>K@EFKD QOBB. ,BQfP CFOPQ T>IH QEOLRDE QEB PQBMP QL @OB>QB >
ABQBOJFKFPQF@ SBOPFLK. (BOB >OB LRO MOLAR@QFLK ORIBP:
#E>MQBO 8. &O>@Q>IP
374
!D>FK, TB E>SB > KF@B CO>@Q>I TFQE > OB@ROPFSB ABCFKFQFLK: ! ?O>K@E FP > IFKB TFQE QTL ?O>K@EBP
@LKKB@QBA QL FQ.
4EB M>OQ QE>Q FP > ?FQ JLOB AFCCF@RIQ QE>K LRO MOBSFLRP CO>@Q>IP IFBP FK QEB RPB LC QEB TLOA FCH5H9
FK QEB CO>@Q>IfP ORIBP. %>@E KBT ?O>K@E JRPQ OLQ>QB OBI>QFSB QL QEB MOBSFLRP ?O>K@E, TEF@E FP
OLQ>QBA OBI>QFSB QL >II FQP MOBSFLRP ?O>K@EBP. ,R@HFIV CLO RP, 0OL@BPPFKD E>P > JB@E>KFPJ QL
HBBM QO>@H LC OLQ>QFLKP CLO RPdQEB ?=,9>1:=8,?4:9 8,?=4C ?=,9>1:=8,?4:9 8,?=4C. )C VLR >OBKfQ C>JFIF>O TFQE QEB
CRK@QFLKP JOMB+;NLCR() >KA JIJ+;NLCR(), ) PRDDBPQ VLR OB>A QEB LKIFKB 0OL@BPPFKD QRQLOF>I
2$ 4O>KPCLOJ>QFLKP (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/QO>KPCLOJ2A/), TEF@E TFII @LSBO QEB
@LK@BMQP VLRfII KBBA CLO QEFP M>OQF@RI>O BU>JMIB.
,BQfP ?BDFK ?V AO>TFKD > PFKDIB ?O>K@E, QEB QORKH LC QEB QOBB. 3FK@B TB >OB DLFKD QL FKSLISB QEB
LIN;N?() CRK@QFLK, TBfII KBBA QL J>HB PROB TB >OB @LKQFKRLRPIV QO>KPI>QFKD >ILKD QEB
?O>K@EBP TEFIB TB AO>T QEB QOBB. !KA PFK@B QEB OLLQ PQ>OQP >Q QEB ?LQQLJ LC QEB TFKALT (PBB
>?LSB), QEB CFOPQ PQBM OBNRFOBP QO>KPI>QFKD QL QE>Q PMLQ:
F64B?2 8.19
NL;HMF;N?(QC>NB/2,B?CABN);
4EB .>QROB LC #LAB (S005)
375
jCLIILTBA ?V AO>TFKD > IFKB RMT>OAP
(&FDROB 8.20):
/K@B TBfSB CFKFPEBA QEB OLLQ, TB GRPQ KBBA QL QO>KPI>QB QL QEB BKA >KA OLQ>QB FK LOABO QL
AO>T QEB KBUQ ?O>K@E. (%SBKQR>IIV, TBfOB DLFKD QL KBBA QL M>@H>DB RM TE>Q TBfOB ALFKD
OFDEQ KLT FKQL > OB@ROPFSB CRK@QFLK, ?RQ IBQfP PLOQ LRQ QEB PQBMP CFOPQ.)
2BJBJ?BO, TEBK TB OLQ>QB FK 0OL@BPPFKD, TB >OB >IT>VP OLQ>QFKD >OLRKA QEB MLFKQ LC LOFDFK,
PL EBOB QEB MLFKQ LC LOFDFK JRPQ >IT>VP ?B QO>KPI>QBA QL QEB BKA LC LRO @ROOBKQ ?O>K@E.
.LT QE>Q TB E>SB > ?O>K@E DLFKD QL QEB OFDEQ, TB KBBA LKB DLFKD QL QEB IBCQ. 7B @>K RPB
JOMB+;NLCR() QL P>SB QEB QO>KPCLOJ>QFLK PQ>QB ?BCLOB TB OLQ>QB, IBQQFKD RP @>II
JIJ+;NLCR() QL OBPQLOB QE>Q PQ>QB >KA AO>T QEB ?O>K@E QL QEB IBCQ. ,BQfP ILLH >Q >II QEB @LAB
QLDBQEBO.
F64B?2 8.20
FCH?(0,0,0,-100);
F64B?2 8.21
NL;HMF;N?(0,-100);
LIN;N?(.'/6);
FCH?(0,0,0,-100);
#E>MQBO 8. &O>@Q>IP
376
)C VLR QEFKH LC B>@E @>II QL QEB CRK@QFLK FCH?() >P > g?O>K@E,h VLR @>K PBB COLJ QEB @LAB
>?LSB QE>Q TB E>SB FJMIBJBKQBA LRO ABCFKFQFLK LC ?O>K@EFKD >P > IFKB QE>Q E>P QTL IFKBP
@LKKB@QBA QL FQP BKA. 7B @LRIA HBBM >AAFKD JLOB >KA JLOB @>IIP QL FCH?() CLO JLOB >KA
JLOB ?O>K@EBP, ?RQ GRPQ >P TFQE QEB #>KQLO PBQ >KA +L@E @ROSB, LRO @LAB TLRIA ?B@LJB
FK@OBAF?IV @LJMIF@>QBA >KA RKTFBIAV. )KPQB>A, TB @>K RPB QEB >?LSB ILDF@ >P LRO CLRKA>QFLK
CLO TOFQFKD > OB@ROPFSB CRK@QFLK, OBMI>@FKD QEB AFOB@Q @>IIP QL FCH?() TFQE LRO LTK CRK@QFLK
@>IIBA <L;H=B(). ,BQfP Q>HB > ILLH.
Example 8.6: Recursive tree
F64B?2 8.22 F64B?2 8.23
NL;HMF;N?(QC>NB/2,B?CABN);
The root FCH?(0,0,0,-100);
NL;HMF;N?(0,-100);
JOMB+;NLCR();
LIN;N?(.'/6);
Branch to the right FCH?(0,0,0,-100);
JIJ+;NLCR();
LIN;N?(-.'/6);
Branch to the left FCH?(0,0,0,-100);
PIC> <L;H=B() U
Draw the branch itself. FCH?(0, 0, 0, -100);
Translate to the end. NL;HMF;N?(0, -100);
JOMB+;NLCR();
4EB .>QROB LC #LAB (S005)
377
.LQF@B ELT FK QEB >?LSB @LAB TB RPB JOMB+;NLCR() >KA JIJ+;NLCR() >OLRKA B>@E
PR?PBNRBKQ @>II QL <L;H=B(). 4EFP FP LKB LC QELPB BIBD>KQ @LAB PLIRQFLKP QE>Q CBBIP >IJLPQ
IFHB J>DF@. %>@E @>II QL <L;H=B() Q>HBP > JLJBKQ QL OBJBJ?BO QEB IL@>QFLK LC QE>Q
M>OQF@RI>O ?O>K@E. )C VLR QROK VLROPBIC FKQL 0OL@BPPFKD CLO > JLJBKQ >KA QOV QL CLIILT QEB
OB@ROPFSB CRK@QFLK TFQE MBK@FI >KA M>MBO, VLRfII KLQF@B QE>Q FQ AO>TP >II LC QEB ?O>K@EBP QL QEB
OFDEQ CFOPQ. 7EBK FQ DBQP QL QEB BKA, JIJ+;NLCR() TFII MLM RP ?>@H >ILKD >II LC QEB ?O>K@EBP
TBfSB AO>TK >KA PQ>OQ PBKAFKD ?O>K@EBP LRQ QL QEB IBCQ.
9LR J>V E>SB KLQF@BA QE>Q QEB OB@ROPFSB CRK@QFLK TB GRPQ TOLQB TLRIA KLQ >@QR>IIV AO>T QEB
>?LSB QOBB. !CQBO >II, FQ E>P KL BUFQ @LKAFQFLK >KA TLRIA DBQ PQR@H FK FKCFKFQB OB@ROPFSB @>IIP QL
FQPBIC. 9LRfII >IPL MOL?>?IV KLQF@B QE>Q QEB ?O>K@EBP LC QEB QOBB DBQ PELOQBO >Q B>@E IBSBI.
,BQfP ILLH >Q ELT TB @>K PEOFKH QEB IBKDQE LC QEB IFKBP >P QEB QOBB FP AO>TK, >KA PQLM
?O>K@EFKD LK@B QEB IFKBP E>SB ?B@LJB QLL PELOQ.
Rotate to the right and branch again. LIN;N?(.'/6);
<L;H=B();
JIJ+;NLCR();
JOMB+;NLCR();
Rotate to the left and branch again. LIN;N?(-.'/6);
<L;H=B();
JIJ+;NLCR();
W
%JRI>QB QEB 0OL@BPPFKD @LAB FK %U>JMIB 8.6 (PBB M>DB 377) >KA KRJ?BO QEB
?O>K@EBP FK QEB >?LSB AF>DO>J FK QEB LOABO QE>Q 0OL@BPPFKD TLRIA >@QR>IIV AO>T B>@E
LKB.
Exercise 8.6 Exercise 8.6
#E>MQBO 8. &O>@Q>IP
378
7BfSB >IPL FK@IRABA > S>OF>?IB CLO QEBQ> QE>Q >IILTP RP, TEBK TOFQFKD QEB OBPQ LC QEB @LAB FK
M?NOJ() >KA >L;Q(), QL S>OV QEB ?O>K@EFKD >KDIB >@@LOAFKD QL, P>V, QEB GIOM?5 IL@>QFLK.
Example 8.7: Recursive tree
Each branch now receives its length as an
argument.
PIC> <L;H=B(@FI;N F?H) U
FCH?(0, 0, 0, -F?H);
NL;HMF;N?(0, -F?H);
Each branchs length shrinks by two-thirds. F?H *= 0.66;
C@ (F?H > 2) U
JOMB+;NLCR();
LIN;N?(NB?N;);
Subsequent calls to branch() include the
length argument.
<L;H=B(F?H);
JIJ+;NLCR();
JOMB+;NLCR();
LIN;N?(-NB?N;);
<L;H=B(F?H);
JIJ+;NLCR();
W
W
@FI;N NB?N;;
PIC> M?NOJ() U
MCT?(300, 200);
W
PIC> >L;Q() U
<;=EALIOH>(255);
4EB .>QROB LC #LAB (S005)
379
4EB OB@ROPFSB QOBB CO>@Q>I FP > KF@B BU>JMIB LC > P@BK>OFL FK TEF@E >AAFKD > IFQQIB ?FQ LC
O>KALJKBPP @>K J>HB QEB QOBB ILLH JLOB K>QRO>I. 4>HB > ILLH LRQPFAB >KA VLRfII KLQF@B QE>Q
?O>K@E IBKDQEP >KA >KDIBP S>OV COLJ ?O>K@E QL ?O>K@E, KLQ QL JBKQFLK QEB C>@Q QE>Q ?O>K@EBP
ALKfQ >II E>SB BU>@QIV QEB P>JB KRJ?BO LC PJ>IIBO ?O>K@EBP. &FOPQ, IBQfP PBB TE>Q E>MMBKP
Pick an angle according to the mouse
location.
NB?N; = G;J(GIOM?5,0,QC>NB,0,.'/2);
The first branch starts at the bottom of the
window.
NL;HMF;N?(QC>NB/2, B?CABN);
MNLIE?(0);
<L;H=B(60);
W
6>OV QEB MNLIE?4?CABN() CLO B>@E ?O>K@E. ->HB QEB OLLQ QEF@H >KA B>@E PR?PBNRBKQ
?O>K@E QEFKKBO.
Exercise 8.7 Exercise 8.7
4EB QOBB PQOR@QROB @>K >IPL ?B DBKBO>QBA RPFKD QEB ALL;S*CMN QB@EKFNRB
ABJLKPQO>QBA TFQE QEB +L@E @ROSB. 2B@OB>QB QEB QOBB RPFKD > L;H=B L?GB@Q >KA >K
ALL;S*CMN QL HBBM QO>@H LC QEB ?O>K@EBP. (FKQ: VLRfII T>KQ QL HBBM QO>@H LC QEB ?O>K@E
AFOB@QFLKP >KA IBKDQEP RPFKD SB@QLO J>QE FKPQB>A LC 0OL@BPPFKD QO>KPCLOJ>QFLKP.
Exercise 8.8 Exercise 8.8
/K@B VLR E>SB QEB QOBB ?RFIQ TFQE >K ALL;S*CMN LC L;H=B L?GB@QP, >KFJ>QB QEB QOBBfP
DOLTQE. #>K VLR AO>T IB>SBP >Q QEB BKA LC QEB ?O>K@EBP?
Exercise 8.9 Exercise 8.9
#E>MQBO 8. &O>@Q>IP
380
TEBK TB PFJMIV S>OV QEB >KDIB >KA IBKDQE. 4EFP FP > MOBQQV B>PV LKB, DFSBK QE>Q TB @>K GRPQ
>PH 0OL@BPPFKD CLO > O>KALJ KRJ?BO B>@E QFJB TB AO>T QEB QOBB.
)K QEB >?LSB CRK@QFLK, TB >IT>VP @>II <L;H=B() QTF@B. "RQ TEV KLQ MF@H > O>KALJ KRJ?BO LC
?O>K@EBP >KA @>II <L;H=B() QE>Q KRJ?BO LC QFJBP?
PIC> <L;H=B(@FI;N F?H) U
Start by picking a random angle for each
branch.
@FI;N NB?N; = L;H>IG(0,.'/3);
FCH?(0, 0, 0, -F?H);
NL;HMF;N?(0, -F?H);
F?H *= 0.66;
C@ (F?H > 2) U
JOMB+;NLCR();
LIN;N?(NB?N;);
<L;H=B(F?H);
JIJ+;NLCR();
JOMB+;NLCR();
LIN;N?(-NB?N;);
<L;H=B(F?H);
JIJ+;NLCR();
W
W
4EB .>QROB LC #LAB (S005)
381
Example 8.8: Stochastic tree
PIC> <L;H=B(@FI;N F?H) U
FCH?(0, 0, 0, -F?H);
NL;HMF;N?(0, -F?H);
C@ (F?H > 2) U
Call branch() a random number of times. CHN H = CHN(L;H>IG(1,4));
@IL (CHN C = 0; C < H; C++) U
Each branch gets its own random angle. @FI;N NB?N; = L;H>IG(-.'/2, .'/2);
JOMB+;NLCR();
LIN;N?(NB?N;);
<L;H=B(B);
JIJ+;NLCR();
W
W
3BQ QEB >KDIBP LC QEB ?O>K@EBP LC QEB QOBB >@@LOAFKD QL 0BOIFK KLFPB S>IRBP. !AGRPQ QEB
KLFPB S>IRBP LSBO QFJB QL >KFJ>QB QEB QOBB. 3BB FC VLR @>K DBQ FQ QL >MMB>O >P FC FQ FP
?ILTFKD FK QEB TFKA.
Exercise 8.10 Exercise 8.10
5PB QLUF@IF?P QL PFJRI>QB QOBB MEVPF@P. %>@E ?O>K@E LC QEB QOBB PELRIA ?B QTL M>OQF@IBP
@LKKB@QBA TFQE > PMOFKD. (LT @>K VLR DBQ QEB QOBB QL PQ>KA RM >KA KLQ C>II ALTK?
Exercise 8.11 Exercise 8.11
8.6 L-systems 8.6 L-systems
)K 1968, (RKD>OF>K ?LQ>KFPQ !OFPQFA ,FKABKJ>VBO ABSBILMBA > DO>JJ>O-?>PBA PVPQBJ QL
JLABI QEB DOLTQE M>QQBOKP LC MI>KQP. ,-PVPQBJP (PELOQ CLO ,FKABKJ>VBO PVPQBJP) @>K ?B RPBA
QL DBKBO>QB >II LC QEB OB@ROPFSB CO>@Q>I M>QQBOKP TBfSB PBBK PL C>O FK QEFP @E>MQBO. 7B ALKfQ
KBBA ,-PVPQBJP QL AL QEB HFKA LC TLOH TBfOB ALFKD EBOB; ELTBSBO, QEBV >OB FK@OBAF?IV RPBCRI
?B@>RPB QEBV MOLSFAB > JB@E>KFPJ CLO HBBMFKD QO>@H LC CO>@Q>I PQOR@QROBP QE>Q OBNRFOB
@LJMIBU >KA JRIQF-C>@BQBA MOLAR@QFLK ORIBP.
#E>MQBO 8. &O>@Q>IP
382
)K LOABO QL @OB>QB >K BU>JMIB QE>Q FJMIBJBKQP ,-PVPQBJP FK 0OL@BPPFKD, TB >OB DLFKD QL E>SB
QL ?B @LJCLOQ>?IB TFQE TLOHFKD TFQE (>) OB@ROPFLK, (?) QO>KPCLOJ>QFLK J>QOF@BP, >KA (@) PQOFKDP.
3L C>O TBfSB TLOHBA TFQE OB@ROPFLK >KA QO>KPCLOJ>QFLKP, ?RQ PQOFKDP >OB KBT EBOB. 7B TFII
>PPRJB QEB ?>PF@P, ?RQ FC QE>Q FP KLQ @LJCLOQ>?IB CLO VLR, ) TLRIA PRDDBPQ Q>HFKD > ILLH >Q QEB
0OL@BPPFKD QRQLOF>I 3QOFKDP >KA $O>TFKD 4BUQ (EQQM://TTT.MOL@BPPFKD.LOD/IB>OKFKD/QBUQ/).
!K ,-PVPQBJ FKSLISBP QEOBB J>FK @LJMLKBKQP:
i A7;3,-0?. A7;3,-0?. !K ,-PVPQBJfP >IME>?BQ FP @LJMOFPBA LC QEB S>IFA @E>O>@QBOP QE>Q @>K ?B
FK@IRABA. &LO BU>JMIB, TB @LRIA P>V QEB >IME>?BQ FP g!"#,h JB>KFKD QE>Q >KV S>IFA
gPBKQBK@Bh (> PQOFKD LC @E>O>@QBOP) FK >K ,-PVPQBJ @>K LKIV FK@IRAB QEBPB QEOBB
@E>O>@QBOP.
i AC4:8. AC4:8. 4EB >UFLJ FP > PBKQBK@B (J>AB RM TFQE @E>O>@QBOP COLJ QEB >IME>?BQ) QE>Q
ABP@OF?BP QEB FKFQF>I PQ>QB LC QEB PVPQBJ. &LO BU>JMIB, TFQE QEB >IME>?BQ g!"#,h PLJB
BU>JMIB >UFLJP >OB g!!!h LO g"h LO g!#"!".h
i &@70>. &@70>. 4EB ORIBP LC >K ,-PVPQBJ >OB >MMIFBA QL QEB >UFLJ >KA QEBK >MMIFBA
OB@ROPFSBIV, DBKBO>QFKD KBT PBKQBK@BP LSBO >KA LSBO >D>FK. !K ,-PVPQBJ ORIB
FK@IRABP QTL PBKQBK@BP, > gMOBAB@BPPLOh >KA > gPR@@BPPLO.h &LO BU>JMIB, TFQE QEB
2RIB g! d> !"h, TEBKBSBO >K g!h FP CLRKA FK > PQOFKD, FQ FP OBMI>@BA TFQE g!".h
,BQfP ?BDFK TFQE > SBOV PFJMIB ,-PVPQBJ. (4EFP FP, FK C>@Q, ,FKABKJ>VBOfP LOFDFK>I ,-PVPQBJ CLO
JLABIFKD QEB DOLTQE LC >ID>B.)
ABF>78;J: A B
AN?EC: A
,KB;I: (A X AB) (B X A)
!P TFQE LRO OB@ROPFSB CO>@Q>I PE>MBP, TB @>K
@LKPFABO B>@E PR@@BPPFSB >MMIF@>QFLK LC QEB
,-PVPQBJ ORIBP QL ?B > DBKBO>QFLK.
'BKBO>QFLK 0 FP, ?V ABCFKFQFLK, QEB >UFLJ.
,BQfP ILLH >Q ELT TB JFDEQ @OB>QB QEBPB
DBKBO>QFLKP TFQE @LAB. 7BfII PQ>OQ ?V RPFKD >
0NLCHA L?GB@Q QL PQLOB QEB >UFLJ.
!KA LK@B >D>FK, GRPQ >P TB AFA TFQE QEB '>JB LC ,FCB >KA QEB +L@E @ROSB ALL;S*CMN
BU>JMIBP, TB TFII KBBA >K BKQFOBIV PBM>O>QB PQOFKD QL HBBM QO>@H LC QEB gKBUQh DBKBO>QFLK.
F64B?2 8.24: A;1 @< <; .;1 @< 3<?A5...
0NLCHA =OLL?HN = "A";
4EB .>QROB LC #LAB (S005)
383
.LT FQfP QFJB QL >MMIV QEB ORIBP QL QEB @ROOBKQ DBKBO>QFLK >KA MI>@B QEB OBPRIQP FK QEB KBUQ.
!KA TEBK TBfOB ALKB, @ROOBKQ @>K ?B@LJB KBUQ.
4L ?B PROB QEFP FP TLOHFKD, IBQfP M>@H>DB FQ FKQL > CRK@QFLK >KA >KA @>II FQ BSBOV QFJB QEB
JLRPB FP MOBPPBA.
Example 8.9: Simple L-system sentence generation
0NLCHA H?RN = "";
@IL (CHN C = 0; C < =OLL?HN.F?HANB(); C++) U
=B;L = = =OLL?HN.=B;LAN(C);
Production rule A --> AB C@ (= == 'A') U
H?RN += "A";
Production rule B --> A W ?FM? C@ (= == '') U
H?RN += "A";
W
W
=OLL?HN = H?RN;
Start with an axiom. 0NLCHA =OLL?HN = "A";
Lets keep track of how many generations. CHN =IOHN = 0;
PIC> M?NOJ() U
JLCHNFH("%?H?L;NCIH " + =IOHN + ": " + =OLL?HN);
W
PIC> >L;Q() U
W
PIC> GIOM?.L?MM?>() U
0NLCHA H?RN = "";
#E>MQBO 8. &O>@Q>IP
384
3FK@B QEB ORIBP >OB >MMIFBA OB@ROPFSBIV QL B>@E DBKBO>QFLK, QEB IBKDQE LC QEB PQOFKD DOLTP
BUMLKBKQF>IIV. "V DBKBO>QFLK #11, QEB PBKQBK@B FP 233 @E>O>@QBOP ILKD; ?V DBKBO>QFLK #22, FQ FP
LSBO 46,000 @E>O>@QBOP ILKD. 4EB *>S> 0NLCHA @I>PP, TEFIB @LKSBKFBKQ QL RPB, FP > DOLPPIV
FKBCCF@FBKQ A>Q> PQOR@QROB CLO @LK@>QBK>QFKD I>ODB PQOFKDP. ! 0NLCHA L?GB@Q FP gFJJRQ>?IB,h
TEF@E JB>KP LK@B QEB L?GB@Q FP @OB>QBA FQ @>K KBSBO ?B @E>KDBA. 7EBKBSBO VLR >AA LK QL QEB
BKA LC > 0NLCHA L?GB@Q, *>S> E>P QL J>HB > ?O>KA KBT 0NLCHA L?GB@Q (BSBK FC VLR >OB RPFKD
QEB P>JB S>OF>?IB K>JB).
)K JLPQ @>PBP, QEFP FP CFKB, ?RQ TEV ARMIF@>QB > 46,000-@E>O>@QBO PQOFKD FC VLR ALKfQ E>SB QL?
&LO ?BQQBO BCCF@FBK@V FK LRO ,-PVPQBJ BU>JMIBP, TBfII RPB QEB 0NLCHAO@@?L @I>PP, TEF@E FP
LMQFJFWBA CLO QEFP QVMB LC Q>PH >KA @>K B>PFIV ?B @LKSBOQBA FKQL > PQOFKD >CQBO @LK@>QBK>QFLK FP
@LJMIBQB.
9LR J>V CFKA VLROPBIC TLKABOFKD OFDEQ >?LRQ KLT: TE>Q BU>@QIV FP QEB MLFKQ LC >II QEFP? !CQBO >II,
FPKfQ QEFP > @E>MQBO >?LRQ 8F5K=B; CO>@Q>I M>QQBOKP? 9BP, QEB OB@ROPFSB K>QROB LC QEB ,-PVPQBJ
PBKQBK@B PQOR@QROB PBBJP OBIBS>KQ QL QEB AFP@RPPFLK, ?RQ ELT BU>@QIV ALBP QEFP JLABI MI>KQ
DOLTQE FK > SFPR>I T>V?
Traverse the current String and make the
new one.
@IL (CHN C = 0; C < =OLL?HN.F?HANB(); C++) U
=B;L = = =OLL?HN.=B;LAN(C);
C@ (= == 'A') U
H?RN += "A";
W ?FM? C@ (= == '') U
H?RN += "A";
W
W
=OLL?HN = H?RN;
=IOHN++;
JLCHNFH("%?H?L;NCIH " + =IOHN + ": " + =OLL?HN);
W
0NLCHA M = "<F;B";
M += ";>> MIG? GIL? MNO@@";
A StringBuffer for the next sentence 0NLCHAO@@?L H?RN = H?Q 0NLCHAO@@?L();
@IL (CHN C = 0; C < =OLL?HN.F?HANB(); C++) U
=B;L = = =OLL?HN.=B;LAN(C);
C@ (= == 'A') U
append() instead of += H?RN.;JJ?H>("A");
W ?FM? C@ (= == '') U
H?RN.;JJ?H>("A");
W
W
StringBuffer can easily be converted back to
a String.
=OLL?HN = H?RN.NI0NLCHA();
4EB .>QROB LC #LAB (S005)
385
7E>Q TBfSB IBCQ RKP>FA RKQFI KLT FP QE>Q BJ?BAABA FKQL QEBPB ,-PVPQBJ PBKQBK@BP >OB
FKPQOR@QFLKP CLO AO>TFKD. ,BQfP PBB ELT QEFP TLOHP TFQE >KLQEBO BU>JMIB.
ABF>78;J: A B
AN?EC: A
,KB;I: (A X ABA) (B X BBB)
4L OB>A > PBKQBK@B, TBfII QO>KPI>QB FQ FK QEB CLIILTFKD T>V:
A: H7M 7 B?D; <EHM7H:.
B: (EL; <EHM7H: M?J>EKJ :H7M?D=.
,BQfP ILLH >Q QEB PBKQBK@B LC B>@E DBKBO>QFLK >KA FQP SFPR>I LRQMRQ.
#;D;H7J?ED 0: A
#;D;H7J?ED 1: ABA
#;D;H7J?ED 2: ABABBBABA
#;D;H7J?ED 3: ABABBBABABBBBBBBBBABABBBABA
,LLH C>JFIF>O? 4EFP FP QEB #>KQLO PBQ DBKBO>QBA TFQE >K ,-PVPQBJ.
4EB CLIILTFKD >IME>?BQ FP LCQBK RPBA TFQE ,-PVPQBJP: g&'+-;<h, JB>KFKD:
": H7M 7 B?D; 7D: CEL; <EHM7H:
#: (EL; <EHM7H: (M?J>EKJ :H7M?D= 7 B?D;)
+: .KHD H?=>J
-: .KHD B;<J
4: -7L; 9KHH;DJ BE97J?ED
5: ,;IJEH; FH;L?EKI BE97J?ED
4EFP QVMB LC AO>TFKD CO>JBTLOH FP LCQBK OBCBOOBA QL >P g4ROQIB DO>MEF@Ph (COLJ QEB LIA A>VP
LC ,/'/ MOLDO>JJFKD). )J>DFKB > QROQIB PFQQFKD LK VLRO @LJMRQBO P@OBBK QL TEF@E VLR @LRIA
FPPRB > PJ>II PBQ LC @LJJ>KAP: QROK IBCQ, QROK OFDEQ, AO>T > IFKB, BQ@. 0OL@BPPFKD FPKfQ PBQ RM
QL LMBO>QB QEFP T>V ?V ABC>RIQ, ?RQ ?V RPFKD NL;HMF;N?(), LIN;N?(), >KA FCH?(), TB @>K
BJRI>QB > 4ROQIB DO>MEF@P BKDFKB C>FOIV B>PFIV.
F64B?2 8.25
#E>MQBO 8. &O>@Q>IP
386
(BOBfP ELT TB TLRIA QO>KPI>QB QEB >?LSB ,-PVPQBJ >IME>?BQ FKQL 0OL@BPPFKD @LAB.
": B?D;(0,0,0,B;D); JH7DIB7J;(0,B;D);
#: JH7DIB7J;(0,B;D);
+: HEJ7J;(7D=B;);
-: HEJ7J;(-7D=B;);
4: FKI>(7JH?N();
5: FEF(7JH?N();
!PPRJFKD TB E>SB > PBKQBK@B DBKBO>QBA COLJ QEB ,-PVPQBJ, TB @>K T>IH QEOLRDE QEB
PBKQBK@B @E>O>@QBO ?V @E>O>@QBO >KA @>II QEB >MMOLMOF>QB CRK@QFLK >P LRQIFKBA >?LSB.
4EB KBUQ BU>JMIB TFII AO>T > JLOB BI>?LO>QB PQOR@QROB TFQE QEB CLIILTFKD ,-PVPQBJ.
ABF>78;J: "#+-45
AN?EC: "
,KB;I: " -X ""+4+"-"-"5-4-"+"+"5
4EB BU>JMIB >S>FI>?IB CLO ALTKIL>A LK QEB ?LLHfP TB?PFQB Q>HBP >II LC QEB ,-PVPQBJ @LAB
MOLSFABA FK QEFP PB@QFLK >KA LOD>KFWBP FQ FKQL QEOBB @I>PPBP:
i ,KB;: ! @I>PP QE>Q PQLOBP QEB MOBAB@BPPLO >KA PR@@BPPLO PQOFKDP CLO >K ,-PVPQBJ ORIB.
i '-OIJ;C: ! @I>PP QL FQBO>QB > KBT ,-PVPQBJ DBKBO>QFLK (>P ABJLKPQO>QBA TFQE QEB
0NLCHAO@@?L QB@EKFNRB).
i .KHJB;: ! @I>PP QL J>K>DB OB>AFKD QEB ,-PVPQBJ PBKQBK@B >KA CLIILTFKD FQP
FKPQOR@QFLKP QL AO>T LK QEB P@OBBK.
@IL (CHN C = 0; C < M?HN?H=?.F?HANB(); C++) U
Looking at each character one at a time =B;L = = M?HN?H=?.=B;LAN(C);
Performing the correct task for each
character. This could also be written with a
case statement, which might be nicer to
look at, but leaving it as an if/else if
structure helps readers not familiar with
case statements.
C@ (= == '$') U
FCH?(0,0,F?H,0);
NL;HMF;N?(F?H,0);
W ?FM? C@ (= == '$') U
NL;HMF;N?(F?H,0);
W ?FM? C@ (= == '+') U
LIN;N?(NB?N;);
W ?FM? C@ (= == '-') U
LIN;N?(-NB?N;);
W ?FM? C@ (= == '8') U
JOMB+;NLCR();
W ?FM? C@ (= == '9') U
JIJ+;NLCR();
W
W
4EB .>QROB LC #LAB (S005)
387
7B TLKfQ TOFQB LRQ QEBPB @I>PPBP EBOB PFK@B QEBV PFJMIV ARMIF@>QB QEB @LAB TBfSB >IOB>AV
TLOHBA LRQ FK QEFP @E>MQBO. (LTBSBO, IBQfP PBB ELT QEBV >OB MRQ QLDBQEBO FK QEB J>FK Q>?.
Example 8.10: LSystem
*0SMN?G FMSM;
1OLNF? NOLNF?;
PIC> M?NOJ() U
MCT?(600,600);
A ruleset is an array of Rule objects. /OF?89 LOF?M?N = H?Q /OF?819;
LOF?M?N809 = H?Q /OF?('$',"$$+8+$-$-$9-8-$+$+$9");
The L-system is created with an axiom and
a ruleset.
FMSM = H?Q *0SMN?G("$",LOF?M?N);
NOLNF? = H?Q 1OLNF?(FMSM.A?N0?HN?H=?(),QC>NB/4,L;>C;HM(25));
W
PIC> >L;Q() U
<;=EALIOH>(255);
The Turtle graphics renderer is given a
sentence, a starting length, and an angle
for rotations.
Start at the bottom of the window and draw. NL;HMF;N?(QC>NB/2,B?CABN);
NOLNF?.L?H>?L();
W
PIC> GIOM?.L?MM?>() U
Generate a new sentence when the mouse
is pressed.
FMSM.A?H?L;N?();
NOLNF?.M?N1I"I(FMSM.A?N0?HN?H=?());
The length shrinks each generation. NOLNF?.=B;HA?*?H(0.5);
W
#E>MQBO 8. &O>@Q>IP
388
The Ecosystem Project The Ecosystem Project
Step 8 Exercise:
Incorporate fractals into your ecosystem. Some possibilities:
Add plant-like creatures to the ecosystem environment.
Lets say one of your plants is similar to a tree. Can you add leaves or
flowers to the end of the branches? What if the leaves can fall off the tree
(depending on a wind force)? What if you add fruit that can be picked and
eaten by the creatures?
Design a creature with a fractal pattern.
Use an L-system to generate instructions for how a creature should move
or behave.
5PB >K ,-PVPQBJ >P > PBQ LC FKPQOR@QFLKP CLO @OB>QFKD L?GB@QP PQLOBA FK >K ALL;S*CMN.
5PB QOFDLKLJBQOV >KA SB@QLO J>QE QL MBOCLOJ QEB OLQ>QFLKP FKPQB>A LC J>QOFU
QO>KPCLOJ>QFLKP (JR@E IFHB TB AFA FK QEB +L@E @ROSB BU>JMIB).
Exercise 8.12 Exercise 8.12
4EB PBJFK>I TLOH FK ,-PVPQBJP >KA MI>KQ PQOR@QROBP, /<9 A@;CF=H<A=7 B95IHM C: ,@5BHG
?V 0OWBJVP[>T 0ORPFKHFBTF@W >KA !OFPQFA ,FKABKJ>VBO, T>P MR?IFPEBA FK 1990. )Q FP
>S>FI>?IB CLO COBB FK FQP BKQFOBQV LKIFKB (EQQM://>IDLOFQEJF@?LQ>KV.LOD/M>MBOP/#>?LM).
#E>MQBO 1 ABP@OF?BP J>KV PLMEFPQF@>QBA ,-PVPQBJP TFQE >AAFQFLK>I AO>TFKD ORIBP >KA
>S>FI>?IB >IME>?BQ @E>O>@QBOP. )K >AAFQFLK, FQ ABP@OF?BP PBSBO>I JBQELAP CLO DBKBO>QFKD
PQL@E>PQF@ ,-PVPQBJP. %UM>KA QEB ,-PVPQBJ BU>JMIB QL FK@IRAB LKB LO JLOB >AAFQFLK>I
CB>QROBP ABP@OF?BA ?V 0ORPFKHFBTF@W >KA ,FKABKJ>VBO.
Exercise 8.13 Exercise 8.13
)K QEFP @E>MQBO, TB BJME>PFWBA RPFKD CO>@Q>I >IDLOFQEJP CLO DBKBO>QFKD SFPR>I M>QQBOKP.
(LTBSBO, CO>@Q>IP @>K ?B CLRKA FK LQEBO @OB>QFSB JBAFRJP. &LO BU>JMIB, CO>@Q>I M>QQBOKP
>OB BSFABKQ FK *LE>KK 3B?>PQF>K ">@EfP #BIIL 3RFQB KL. 3. 4EB PQOR@QROB LC $>SFA &LPQBO
7>II>@BfP KLSBI %B:=B=H9 &9GH T>P FKPMFOBA ?V CO>@Q>IP. #LKPFABO RPFKD QEB BU>JMIBP FK
QEFP @E>MQBO QL DBKBO>QB >RAFL LO QBUQ.
Exercise 8.14 Exercise 8.14
4EB .>QROB LC #LAB (S005)
389
Chapter 9. The Chapter 9. The
Evolution of Code Evolution of Code
I*52 3.0A A5.A 9632 2C<9C21 <BA <3 ;2.?9F ;<A56;4, @<:2 10 /6996<; F2.?@
.3A2? A52 B;6C2?@2 2C<9C21 <BA <3 96A2?.99F ;<A56;4, 6@ . 3.0A @< @A.442?6;4
A5.A ! D<B91 /2 :.1 A< .AA2:=A D<?1@ A< 1< 6A 7B@A602.J
Richard Dawkins
,BQfP Q>HB > JLJBKQ QL QEFKH ?>@H QL > PFJMIBO QFJB, TEBK VLR TOLQB VLRO CFOPQ 0OL@BPPFKD
PHBQ@EBP >KA IFCB T>P COBB >KA B>PV. 7E>Q FP LKB LC MOLDO>JJFKDfP CRKA>JBKQ>I @LK@BMQP
QE>Q VLR IFHBIV RPBA FK QELPB CFOPQ PHBQ@EBP >KA @LKQFKRB QL RPB LSBO >KA LSBO >D>FK?
15F=56@9G. 6>OF>?IBP >IILT VLR QL P>SB A>Q> >KA OBRPB QE>Q A>Q> TEFIB > MOLDO>J ORKP. 4EFP,
LC @LROPB, FP KLQEFKD KBT QL RP. )K C>@Q, TB E>SB JLSBA C>O ?BVLKA > PHBQ@E TFQE GRPQ LKB LO
QTL S>OF>?IBP >KA LK QL JLOB @LJMIBU A>Q> PQOR@QROBPdS>OF>?IBP J>AB COLJ @RPQLJ QVMBP
(L?GB@QP) QE>Q FK@IRAB ?LQE A>Q> >KA CRK@QFLK>IFQV. 7BfSB J>AB LRO LTK IFQQIB TLOIAP LC
JLSBOP >KA M>OQF@IBP >KA SBEF@IBP >KA @BIIP >KA QOBBP.
)K B>@E >KA BSBOV BU>JMIB FK QEFP ?LLH, QEB S>OF>?IBP LC QEBPB L?GB@QP E>SB QL ?B FKFQF>IFWBA.
0BOE>MP VLR J>AB > TELIB ?RK@E LC M>OQF@IBP TFQE O>KALJ @LILOP >KA PFWBP LO > IFPQ LC
SBEF@IBP >II PQ>OQFKD >Q QEB P>JB R,S IL@>QFLK LK P@OBBK. "RQ FKPQB>A LC >@QFKD >P gFKQBIIFDBKQ
ABPFDKBOPh >KA >PPFDKFKD QEB MOLMBOQFBP LC LRO L?GB@QP QEOLRDE O>KALJKBPP LO QELRDEQCRI
@LKPFABO>QFLK, TB @>K IBQ > MOL@BPP CLRKA FK K>QROBd9JC@IH=CBdAB@FAB CLO RP.
#>K TB QEFKH LC QEB S>OF>?IBP LC >K L?GB@Q >P FQP $.!? #>K L?GB@QP J>HB LQEBO L?GB@QP >KA
M>PP ALTK QEBFO $.! QL > KBT DBKBO>QFLK? #>K LRO PFJRI>QFLK BSLISB?
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
390
4EB >KPTBO QL >II QEBPB NRBPQFLKP FP VBP. !CQBO >II, TB TLRIAKfQ ?B >?IB QL C>@B LROPBISBP FK QEB
JFOOLO >P K>QROB-LC-@LABOP TFQELRQ Q>@HIFKD > PFJRI>QFLK LC LKB LC QEB JLPQ MLTBOCRI
>IDLOFQEJF@ MOL@BPPBP CLRKA FK K>QROB FQPBIC. 4EFP @E>MQBO FP ABAF@>QBA QL BU>JFKFKD QEB
MOFK@FMIBP ?BEFKA ?FLILDF@>I BSLIRQFLK >KA CFKAFKD T>VP QL >MMIV QELPB MOFK@FMIBP FK @LAB.
9.1 Genetic Algorithms: Inspired by Actual Events 9.1 Genetic Algorithms: Inspired by Actual Events
)QfP FJMLOQ>KQ CLO RP QL @I>OFCV QEB DL>IP LC QEFP @E>MQBO. 7B TFII KLQ DL FKQL ABMQE >?LRQ QEB
P@FBK@B LC DBKBQF@P >KA BSLIRQFLK >P FQ E>MMBKP FK QEB OB>I TLOIA. 7B TLKfQ ?B J>HFKD 0RKKBQQ
PNR>OBP (PLOOV QL AFP>MMLFKQ) >KA QEBOB TFII ?B KL AFP@RPPFLK LC KR@IBLQFABP, MOLQBFK PVKQEBPFP,
2.!, >KA LQEBO QLMF@P OBI>QBA QL QEB >@QR>I ?FLILDF@>I MOL@BPPBP LC BSLIRQFLK. )KPQB>A, TB >OB
DLFKD QL ILLH >Q QEB @LOB MOFK@FMIBP ?BEFKA $>OTFKF>K BSLIRQFLK>OV QEBLOV >KA ABSBILM > PBQ LC
>IDLOFQEJP =BGD=F98 ?V QEBPB MOFK@FMIBP. 7B ALKfQ @>OB PL JR@E >?LRQ >K >@@RO>QB PFJRI>QFLK
LC BSLIRQFLK; O>QEBO, TB @>OB >?LRQ JBQELAP CLO >MMIVFKD BSLIRQFLK>OV PQO>QBDFBP FK PLCQT>OB.
4EFP FP KLQ QL P>V QE>Q > MOLGB@Q TFQE JLOB P@FBKQFCF@ ABMQE TLRIAKfQ E>SB S>IRB, >KA )
BK@LRO>DB OB>ABOP TFQE > M>OQF@RI>O FKQBOBPQ FK QEFP QLMF@ QL BUMILOB MLPPF?FIFQFBP CLO BUM>KAFKD
QEB BU>JMIBP MOLSFABA TFQE >AAFQFLK>I BSLIRQFLK>OV CB>QROBP. .BSBOQEBIBPP, CLO QEB P>HB LC
HBBMFKD QEFKDP J>K>DB>?IB, TBfOB DLFKD QL PQF@H QL QEB ?>PF@P, TEF@E TFII ?B MIBKQV @LJMIBU
>KA BU@FQFKD.
4EB QBOJ gDBKBQF@ >IDLOFQEJh OBCBOP QL > PMB@FCF@ >IDLOFQEJ FJMIBJBKQBA FK > PMB@FCF@ T>V QL
PLISB PMB@FCF@ PLOQP LC MOL?IBJP. 7EFIB QEB CLOJ>I DBKBQF@ >IDLOFQEJ FQPBIC TFII PBOSB >P QEB
CLRKA>QFLK CLO QEB BU>JMIBP TB @OB>QB FK QEFP @E>MQBO, TB KBBAKfQ TLOOV >?LRQ FJMIBJBKQFKD
QEB >IDLOFQEJ TFQE MBOCB@Q >@@RO>@V, DFSBK QE>Q TB >OB ILLHFKD CLO @OB>QFSB RPBP LC
BSLIRQFLK>OV QEBLOFBP FK LRO @LAB. 4EFP @E>MQBO TFII ?B ?OLHBK ALTK FKQL QEB CLIILTFKD QEOBB
M>OQP (TFQE QEB J>GLOFQV LC QEB QFJB PMBKQ LK QEB CFOPQ).
1. (=,/4?4:9,7 G090?4. A72:=4?38. (=,/4?4:9,7 G090?4. A72:=4?38. 7BfII ?BDFK TFQE QEB QO>AFQFLK>I @LJMRQBO P@FBK@B
DBKBQF@ >IDLOFQEJ. 4EFP >IDLOFQEJ T>P ABSBILMBA QL PLISB MOL?IBJP FK TEF@E QEB
PLIRQFLK PM>@B FP PL S>PQ QE>Q > g?ORQB CLO@Bh >IDLOFQEJ TLRIA PFJMIV Q>HB QLL ILKD.
(BOBfP >K BU>JMIB: )fJ QEFKHFKD LC > KRJ?BO. ! KRJ?BO ?BQTBBK LKB >KA LKB ?FIIFLK.
(LT ILKD TFII FQ Q>HB CLO VLR QL DRBPP FQ? 3LISFKD > MOL?IBJ TFQE g?ORQB CLO@Bh OBCBOP
QL QEB MOL@BPP LC @EB@HFKD BSBOV MLPPF?IB PLIRQFLK. )P FQ LKB? )P FQ QTL? )P FQ QEOBB? )P
FQ CLRO? !KA PL >KA >KA PL CLOQE. 4ELRDE IR@H ALBP MI>V > C>@QLO EBOB, TFQE ?ORQB
CLO@B TB TLRIA LCQBK CFKA LROPBISBP M>QFBKQIV T>FQFKD CLO VB>OP TEFIB VLR @LRKQ QL
LKB ?FIIFLK. (LTBSBO, TE>Q FC ) @LRIA QBII VLR FC >K >KPTBO VLR D>SB T>P DLLA LO ?>A?
7>OJ LO @LIA? 6BOV T>OJ? (LQ? 3RMBO, PRMBO @LIA? )C VLR @LRIA BS>IR>QB ELT gCFQh >
DRBPP FP, VLR @LRIA MF@H LQEBO KRJ?BOP @ILPBO QL QE>Q DRBPP >KA >OOFSB >Q QEB >KPTBO
JLOB NRF@HIV. 9LRO >KPTBO @LRIA BSLISB.
2. I9?0=,.?4A0 '070.?4:9. I9?0=,.?4A0 '070.?4:9. /K@B TB BPQ>?IFPE QEB QO>AFQFLK>I @LJMRQBO P@FBK@B >IDLOFQEJ,
TBfII ILLH >Q LQEBO >MMIF@>QFLKP LC DBKBQF@ >IDLOFQEJP FK QEB SFPR>I >OQP. )KQBO>@QFSB
PBIB@QFLK OBCBOP QL QEB MOL@BPP LC BSLISFKD PLJBQEFKD (LCQBK >K @LJMRQBO-DBKBO>QBA
FJ>DB) QEOLRDE RPBO FKQBO>@QFLK. ,BQfP P>V VLR T>IH FKQL > JRPBRJ D>IIBOV >KA PBB
4EB .>QROB LC #LAB (S005)
391
QBK M>FKQFKDP. 7FQE FKQBO>@QFSB PBIB@QFLK, VLR TLRIA MF@H VLRO C>SLOFQBP >KA >IILT
>K >IDLOFQEJF@ MOL@BPP QL DBKBO>QB (LO gBSLISBh) KBT M>FKQFKDP ?>PBA LK VLRO
MOBCBOBK@BP.
3. E.:>D>?08 '48@7,?4:9. E.:>D>?08 '48@7,?4:9. 4EB QO>AFQFLK>I @LJMRQBO P@FBK@B DBKBQF@ >IDLOFQEJ >KA
FKQBO>@QFSB PBIB@QFLK QB@EKFNRB >OB TE>Q VLR TFII IFHBIV CFKA FC VLR PB>O@E LKIFKB LO
OB>A > QBUQ?LLH >?LRQ >OQFCF@F>I FKQBIIFDBK@B. "RQ >P TBfII PLLK PBB, QEBV ALKfQ OB>IIV
PFJRI>QB QEB MOL@BPP LC BSLIRQFLK >P FQ E>MMBKP FK QEB OB>I TLOIA. )K QEFP @E>MQBO, )
T>KQ QL >IPL BUMILOB QB@EKFNRBP CLO PFJRI>QFKD QEB MOL@BPP LC BSLIRQFLK FK >K
B@LPVPQBJ LC MPBRAL-IFSFKD ?BFKDP. (LT @>K LRO L?GB@QP QE>Q JLSB >?LRQ QEB
P@OBBK JBBQ B>@E LQEBO, J>QB, >KA M>PP QEBFO DBKBP LK QL > KBT DBKBO>QFLK? 4EFP
TLRIA >MMIV AFOB@QIV QL QEB %@LPVPQBJ 0OLGB@Q LRQIFKBA >Q QEB BKA LC B>@E @E>MQBO.
9.2 Why Use Genetic Algorithms? 9.2 Why Use Genetic Algorithms?
7EFIB @LJMRQBO PFJRI>QFLKP LC BSLIRQFLK>OV MOL@BPPBP A>QB ?>@H QL QEB 1950P, JR@E LC TE>Q
TB QEFKH LC >P DBKBQF@ >IDLOFQEJP (>IPL HKLTK >P g'!Ph) QLA>V T>P ABSBILMBA ?V *LEK
(LII>KA, > MOLCBPPLO >Q QEB 5KFSBOPFQV LC -F@EFD>K, TELPB ?LLH A85DH5H=CB =B *5HIF5@ 5B8
AFH=:=7=5@ .MGH9AG MFLKBBOBA '! OBPB>O@E. 4LA>V, JLOB DBKBQF@ >IDLOFQEJP >OB M>OQ LC >
TFABO CFBIA LC OBPB>O@E, LCQBK OBCBOOBA QL >P "%SLIRQFLK>OV #LJMRQFKD."
4L EBIM FIIRPQO>QB QEB QO>AFQFLK>I DBKBQF@ >IDLOFQEJ, TB >OB DLFKD QL PQ>OQ TFQE JLKHBVP. .L,
KLQ LRO BSLIRQFLK>OV >K@BPQLOP. 7BfOB DLFKD QL PQ>OQ TFQE PLJB CF@QFLK>I JLKHBVP QE>Q ?>KD
>T>V LK HBV?L>OAP TFQE QEB DL>I LC QVMFKD LRQ QEB @LJMIBQB TLOHP LC 3E>HBPMB>OB.
F64B?2 9.1
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
392
4EB gFKCFKFQB JLKHBV QEBLOBJh FP PQ>QBA >P CLIILTP: ! JLKHBV EFQQFKD HBVP O>KALJIV LK >
QVMBTOFQBO TFII BSBKQR>IIV QVMB QEB @LJMIBQB TLOHP LC 3E>HBPMB>OB (DFSBK >K FKCFKFQB >JLRKQ LC
QFJB). 4EB MOL?IBJ TFQE QEFP QEBLOV FP QE>Q QEB MOL?>?FIFQV LC P>FA JLKHBV >@QR>IIV QVMFKD
3E>HBPMB>OB FP PL ILT QE>Q BSBK FC QE>Q JLKHBV PQ>OQBA >Q QEB "FD ">KD, FQfP RK?BIFBS>?IV
RKIFHBIV TBfA BSBK E>SB $5A@9H >Q QEFP MLFKQ.
,BQfP @LKPFABO > JLKHBV K>JBA 'BLODB. 'BLODB QVMBP LK > OBAR@BA QVMBTOFQBO @LKQ>FKFKD
LKIV QTBKQV-PBSBK @E>O>@QBOP: QTBKQV-PFU IBQQBOP >KA LKB PM>@B ?>O. 3L QEB MOL?>?FIFQV LC
'BLODB EFQQFKD >KV DFSBK HBV FP LKB FK QTBKQV-PBSBK.
,BQfP @LKPFABO QEB MEO>PB gQL ?B LO KLQ QL ?B QE>Q FP QEB NRBPQFLKh (TBfOB PFJMIFCVFKD FQ COLJ QEB
LOFDFK>I g4L ?B, LO KLQ QL ?B: QE>Q FP QEB NRBPQFLKh). 4EB MEO>PB FP 39 @E>O>@QBOP ILKD. )C 'BLODB
PQ>OQP QVMFKD, QEB @E>K@B EBfII DBQ QEB CFOPQ @E>O>@QBO OFDEQ FP 1 FK 27. 3FK@B QEB MOL?>?FIFQV EBfII
DBQ QEB PB@LKA @E>O>@QBO OFDEQ FP >IPL 1 FK 27, EB E>P > 1 FK 27*27 @E>K@B LC I>KAFKD QEB CFOPQ
QTL @E>O>@QBOP FK @LOOB@Q LOABOdTEF@E CLIILTP AFOB@QIV COLJ LRO AFP@RPPFLK LC "BSBKQ
MOL?>?FIFQV" FK QEB )KQOLAR@QFLK (PBB M>DB 0). 4EBOBCLOB, QEB MOL?>?FIFQV QE>Q 'BLODB TFII QVMB
QEB CRII MEO>PB FP:
(1/27) JRIQFMIFBA ?V FQPBIC 39 QFJBP, F.B. (1/27)
39
TEF@E BNR>IP > 1 FK
66,555,937,033,867,822,607,895,549,241,096,482,953,017,615,834,735,226,163 @E>K@B LC
DBQQFKD FQ OFDEQ!
.BBAIBPP QL P>V, BSBK EFQQFKD GRPQ QEFP LKB MEO>PB, KLQ QL JBKQFLK >K BKQFOB MI>V, FP EFDEIV
RKIFHBIV. %SBK FC 'BLODB FP > @LJMRQBO PFJRI>QFLK >KA @>K QVMB LKB JFIIFLK O>KALJ MEO>PBP
MBO PB@LKA, CLO 'BLODB QL E>SB > 99% MOL?>?FIFQV LC BSBKQR>IIV DBQQFKD FQ OFDEQ, EB TLRIA E>SB
QL QVMB CLO 9,719,096,182,010,563,073,125,591,133,903,305,625,605,017 VB>OP. (.LQB QE>Q QEB
>DB LC QEB RKFSBOPB FP BPQFJ>QBA QL ?B > JBOB 13,750,000,000 VB>OP.)
4EB MLFKQ LC >II QEBPB RKC>QELJ>?IV I>ODB KRJ?BOP FP KLQ QL DFSB VLR > EB>A>@EB, ?RQ QL
ABJLKPQO>QB QE>Q > ?ORQB CLO@B >IDLOFQEJ (QVMFKD BSBOV MLPPF?IB O>KALJ MEO>PB) FP KLQ >
OB>PLK>?IB PQO>QBDV CLO >OOFSFKD O>KALJIV >Q gQL ?B LO KLQ QL ?B QE>Q FP QEB NRBPQFLKh. %KQBO
DBKBQF@ >IDLOFQEJP, TEF@E TFII PELT QE>Q TB @>K PQFII PQ>OQ TFQE O>KALJ MEO>PBP >KA CFKA QEB
PLIRQFLK QEOLRDE PFJRI>QBA BSLIRQFLK.
.LT, FQfP TLOQE KLQFKD QE>Q QEFP MOL?IBJ (5FF=J9 5H H<9 D<F5G9 THC 69 CF BCH HC 69 H<5H =G H<9
EI9GH=CBU) FP > OFAF@RILRP LKB. 3FK@B TB HKLT QEB >KPTBO, >II TB KBBA QL AL FP QVMB FQ. (BOBfP >
0OL@BPPFKD PHBQ@E QE>Q PLISBP QEB MOL?IBJ.
.BSBOQEBIBPP, QEB MLFKQ EBOB FP QE>Q PLISFKD > MOL?IBJ TFQE > HKLTK >KPTBO >IILTP RP QL B>PFIV
QBPQ LRO @LAB. /K@B TBfSB PR@@BPPCRIIV PLISBA QEB MOL?IBJ, TB @>K CBBI JLOB @LKCFABKQ FK
RPFKD DBKBQF@ >IDLOFQEJP QL AL PLJB >@QR>I RPBCRI TLOH: PLISFKD MOL?IBJP TFQE RKHKLTK
>KPTBOP. 3L QEFP CFOPQ BU>JMIB PBOSBP KL OB>I MROMLPB LQEBO QE>K QL ABJLKPQO>QB ELT DBKBQF@
MNLCHA M = "1I <? IL HIN NI <? NB;N CM NB? KO?MNCIH";
JLCHNFH(M);
4EB .>QROB LC #LAB (S005)
393
>IDLOFQEJP TLOH. )C TB QBPQ QEB '! OBPRIQP >D>FKPQ QEB HKLTK >KPTBO >KA DBQ gQL ?B LO KLQ QL
?Bh, QEBK TBfSB PR@@BBABA FK TOFQFKD LRO DBKBQF@ >IDLOFQEJ.
#OB>QB > PHBQ@E QE>Q DBKBO>QBP O>KALJ PQOFKDP. 7BfII KBBA QL HKLT ELT QL AL QEFP FK
LOABO QL FJMIBJBKQ QEB DBKBQF@ >IDLOFQEJ BU>JMIB QE>Q TFII PELOQIV CLIILT. (LT ILKD
ALBP FQ Q>HB CLO 0OL@BPPFKD QL O>KALJIV DBKBO>QB QEB PQOFKD g@>Qh? (LT @LRIA VLR
>A>MQ QEFP QL DBKBO>QB > O>KALJ ABPFDK RPFKD 0OL@BPPFKDfP PE>MB-AO>TFKD CRK@QFLKP?
Exercise 9.1 Exercise 9.1
9.3 Darwinian Natural Selection 9.3 Darwinian Natural Selection
"BCLOB TB ?BDFK T>IHFKD QEOLRDE QEB DBKBQF@ >IDLOFQEJ, IBQfP Q>HB > JLJBKQ QL ABP@OF?B
QEOBB @LOB MOFK@FMIBP LC $>OTFKF>K BSLIRQFLK QE>Q TFII ?B OBNRFOBA >P TB FJMIBJBKQ LRO
PFJRI>QFLK. )K LOABO CLO K>QRO>I PBIB@QFLK QL L@@RO >P FQ ALBP FK K>QROB, >II QEOBB LC QEBPB
BIBJBKQP JRPQ ?B MOBPBKQ.
1. H0=0/4?D. H0=0/4?D. 4EBOB JRPQ ?B > MOL@BPP FK MI>@B ?V TEF@E @EFIAOBK OB@BFSB QEB
MOLMBOQFBP LC QEBFO M>OBKQP. )C @OB>QROBP IFSB ILKD BKLRDE QL OBMOLAR@B, QEBK QEBFO
QO>FQP >OB M>PPBA ALTK QL QEBFO @EFIAOBK FK QEB KBUQ DBKBO>QFLK LC @OB>QROBP.
2. *,=4,?4:9. *,=4,?4:9. 4EBOB JRPQ ?B > S>OFBQV LC QO>FQP MOBPBKQ FK QEB MLMRI>QFLK LO > JB>KP
TFQE TEF@E QL FKQOLAR@B S>OF>QFLK. &LO BU>JMIB, IBQfP P>V QEBOB FP > MLMRI>QFLK LC
?BBQIBP FK TEF@E >II QEB ?BBQIBP >OB BU>@QIV QEB P>JB: P>JB @LILO, P>JB PFWB, P>JB
TFKDPM>K, P>JB BSBOVQEFKD. 7FQELRQ >KV S>OFBQV FK QEB MLMRI>QFLK, QEB @EFIAOBK TFII
>IT>VP ?B FABKQF@>I QL QEB M>OBKQP >KA QL B>@E LQEBO. .BT @LJ?FK>QFLKP LC QO>FQP
@>K KBSBO L@@RO >KA KLQEFKD @>K BSLISB.
3. '070.?4:9. '070.?4:9. 4EBOB JRPQ ?B > JB@E>KFPJ ?V TEF@E PLJB JBJ?BOP LC > MLMRI>QFLK
E>SB QEB LMMLOQRKFQV QL ?B M>OBKQP >KA M>PP ALTK QEBFO DBKBQF@ FKCLOJ>QFLK >KA
PLJB AL KLQ. 4EFP FP QVMF@>IIV OBCBOOBA QL >P gPROSFS>I LC QEB CFQQBPQ.h &LO BU>JMIB,
IBQfP P>V > MLMRI>QFLK LC D>WBIIBP FP @E>PBA ?V IFLKP BSBOV A>V. 4EB C>PQBO D>WBIIBP
>OB JLOB IFHBIV QL BP@>MB QEB IFLKP >KA >OB QEBOBCLOB JLOB IFHBIV QL IFSB ILKDBO >KA
E>SB > @E>K@B QL OBMOLAR@B >KA M>PP QEBFO DBKBP ALTK QL QEBFO @EFIAOBK. 4EB QBOJ
:=HH9GH, ELTBSBO, @>K ?B > ?FQ JFPIB>AFKD. 'BKBO>IIV, TB QEFKH LC FQ >P JB>KFKD
?FDDBO, C>PQBO, LO PQOLKDBO. 7EFIB QEFP J>V ?B QEB @>PB FK PLJB FKPQ>K@BP, K>QRO>I
PBIB@QFLK LMBO>QBP LK QEB MOFK@FMIB QE>Q PLJB QO>FQP >OB ?BQQBO >A>MQBA CLO QEB
@OB>QROBfP BKSFOLKJBKQ >KA QEBOBCLOB MOLAR@B > DOB>QBO IFHBIFELLA LC PROSFSFKD
>KA OBMOLAR@FKD. )Q E>P KLQEFKD QL AL TFQE > DFSBK @OB>QROB ?BFKD g?BQQBOh (>CQBO >II,
QEFP FP > PR?GB@QFSB QBOJ) LO JLOB gMEVPF@>IIV CFQ.h )K QEB @>PB LC LRO QVMFKD
JLKHBVP, CLO BU>JMIB, > JLOB gCFQh JLKHBV FP LKB QE>Q E>P QVMBA > MEO>PB @ILPBO
QL gQL ?B LO KLQ QL ?Bh.
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
394
.BUQ )fA IFHB QL T>IH QEOLRDE QEB K>OO>QFSB LC QEB DBKBQF@ >IDLOFQEJ. 7BfII AL QEFP FK QEB @LKQBUQ
LC QEB QVMFKD JLKHBV. 4EB >IDLOFQEJ FQPBIC TFII ?B AFSFABA FKQL QTL M>OQP: > PBQ LC @LKAFQFLKP CLO
FKFQF>IFW>QFLK (F.B. 0OL@BPPFKDfP M?NOJ()) >KA QEB PQBMP QE>Q >OB OBMB>QBA LSBO >KA LSBO >D>FK
(F.B. 0OL@BPPFKDfP >L;Q()) RKQFI TB >OOFSB >Q QEB @LOOB@Q >KPTBO.
9.4 The Genetic Algorithm, Part I: Creating a 9.4 The Genetic Algorithm, Part I: Creating a
Population Population
)K QEB @LKQBUQ LC QEB QVMFKD JLKHBV BU>JMIB, TB TFII @OB>QB > MLMRI>QFLK LC MEO>PBP. (.LQB
QE>Q TB >OB RPFKD QEB QBOJ gMEO>PBh O>QEBO ILLPBIV, JB>KFKD > PQOFKD LC @E>O>@QBOP.) 4EFP ?BDP
QEB NRBPQFLK: (LT AL TB @OB>QB QEFP MLMRI>QFLK? (BOB FP TEBOB QEB $>OTFKF>K MOFK@FMIB LC
A,=4,?4:9 A,=4,?4:9 >MMIFBP. ,BQfP P>V, CLO PFJMIF@FQV, QE>Q TB >OB QOVFKD QL BSLISB QEB MEO>PB g@>Qh >KA
QE>Q TB E>SB > MLMRI>QFLK LC QEOBB MEO>PBP.
>K=
H?:
MED
3ROB, QEBOB FP S>OFBQV FK QEB QEOBB MEO>PBP >?LSB, ?RQ QOV QL JFU >KA J>Q@E QEB @E>O>@QBOP
BSBOV TEF@E T>V >KA VLR TFII KBSBO DBQ 75H. 4EBOB FP KLQ 9BCI;< S>OFBQV EBOB QL BSLISB QEB
LMQFJ>I PLIRQFLK. (LTBSBO, FC TB E>A > MLMRI>QFLK LC QELRP>KAP LC MEO>PBP, >II DBKBO>QBA
O>KALJIV, @E>K@BP >OB QE>Q >Q IB>PQ LKB JBJ?BO LC QEB MLMRI>QFLK TFII E>SB > 7 >P QEB CFOPQ
@E>O>@QBO, LKB TFII E>SB >K 5 >P QEB PB@LKA, >KA LKB > H >P QEB QEFOA. ! I>ODB MLMRI>QFLK TFII
JLPQ IFHBIV DFSB RP BKLRDE S>OFBQV QL DBKBO>QB QEB ABPFOBA MEO>PB (>KA FK 0>OQ 2 LC QEB
>IDLOFQEJ, TBfII E>SB >KLQEBO LMMLOQRKFQV QL FKQOLAR@B BSBK JLOB S>OF>QFLK FK @>PB QEBOB FPKfQ
BKLRDE FK QEB CFOPQ MI>@B). 3L TB @>K ?B JLOB PMB@FCF@ FK ABP@OF?FKD 3QBM 1 >KA P>V:
#OB>QB > MLMRI>QFLK LC O>KALJIV DBKBO>QBA BIBJBKQP.
4EFP ?OFKDP RM >KLQEBO FJMLOQ>KQ NRBPQFLK. 7E>Q FP QEB BIBJBKQ FQPBIC? !P TB JLSB QEOLRDE
QEB BU>JMIBP FK QEFP @E>MQBO, TBfII PBB PBSBO>I AFCCBOBKQ P@BK>OFLP; TB JFDEQ E>SB > MLMRI>QFLK
LC FJ>DBP LO > MLMRI>QFLK LC SBEF@IBP Y I> #E>MQBO 6 (PBB M>DB 308). 4EB HBV, >KA QEB M>OQ QE>Q
FP KBT CLO RP FK QEFP @E>MQBO, FP QE>Q B>@E JBJ?BO LC QEB MLMRI>QFLK E>P > SFOQR>I g$.!,h > PBQ
LC MOLMBOQFBP (TB @>K @>II QEBJ gDBKBPh) QE>Q ABP@OF?B ELT > DFSBK BIBJBKQ ILLHP LO ?BE>SBP.
)K QEB @>PB LC QEB QVMFKD JLKHBV, CLO BU>JMIB, QEB $.! FP PFJMIV > PQOFKD LC @E>O>@QBOP.
)K QEB CFBIA LC DBKBQF@P, QEBOB FP >K FJMLOQ>KQ AFPQFK@QFLK ?BQTBBK QEB @LK@BMQP ;9BCHMD9 >KA
D<9BCHMD9. 4EB >@QR>I DBKBQF@ @LABdFK LRO @>PB, QEB AFDFQ>I FKCLOJ>QFLK FQPBICdFP >K BIBJBKQfP
209:?D;0 209:?D;0. 4EFP FP TE>Q DBQP M>PPBA ALTK COLJ DBKBO>QFLK QL DBKBO>QFLK. 4EB ;309:?D;0 ;309:?D;0,
ELTBSBO, FP QEB BUMOBPPFLK LC QE>Q A>Q>. 4EFP AFPQFK@QFLK FP HBV QL ELT VLR TFII RPB DBKBQF@
>IDLOFQEJP FK VLRO LTK TLOH. 7E>Q >OB QEB L?GB@QP FK VLRO TLOIA? (LT TFII VLR ABPFDK QEB
4EB .>QROB LC #LAB (S005)
395
DBKLQVMB CLO VLRO L?GB@QP (QEB A>Q> PQOR@QROB QL PQLOB B>@E L?GB@QfP MOLMBOQFBP) >P TBII >P
QEB MEBKLQVMB (TE>Q >OB MCI RPFKD QEBPB S>OF>?IBP QL BUMOBPP?) 7B AL QEFP >II QEB QFJB FK
DO>MEF@P MOLDO>JJFKD. 4EB PFJMIBPQ BU>JMIB FP MOL?>?IV @LILO.
Genotype Genotype Phenotype Phenotype
FKQ @ = 255;
FKQ @ = 127;
FKQ @ = 0;
!P TB @>K PBB, QEB DBKLQVMB FP QEB AFDFQ>I FKCLOJ>QFLK. %>@E @LILO FP > S>OF>?IB QE>Q PQLOBP >K
FKQBDBO >KA TB @ELLPB QL BUMOBPP QE>Q FKQBDBO >P > @LILO. "RQ ELT TB @ELLPB QL BUMOBPP QEB
A>Q> FP >O?FQO>OV. )K > AFCCBOBKQ >MMOL>@E, TB @LRIA E>SB RPBA QEB FKQBDBO QL ABP@OF?B QEB
IBKDQE LC > IFKB, QEB TBFDEQ LC > CLO@B, BQ@.
Same Genotype Same Genotype Different Phenotype (line length) Different Phenotype (line length)
FKQ @ = 255;
FKQ @ = 127;
FKQ @ = 0;
4EB KF@B QEFKD >?LRQ LRO JLKHBV-QVMFKD BU>JMIB FP QE>Q QEBOB FP KL AFCCBOBK@B ?BQTBBK
DBKLQVMB >KA MEBKLQVMB. 4EB $.! A>Q> FQPBIC FP > PQOFKD LC @E>O>@QBOP >KA QEB BUMOBPPFLK LC
QE>Q A>Q> FP QE>Q SBOV PQOFKD.
3L, TB @>K CFK>IIV BKA QEB AFP@RPPFLK LC QEFP CFOPQ PQBM >KA ?B JLOB PMB@FCF@ TFQE FQP
ABP@OFMQFLK, P>VFKD:
#OB>QB > MLMRI>QFLK LC . BIBJBKQP, B>@E TFQE O>KALJIV DBKBO>QBA $.!.
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
396
9.5 The Genetic Algorithm, Part II: Selection 9.5 The Genetic Algorithm, Part II: Selection
(BOB FP TEBOB TB >MMIV QEB $>OTFKF>K MOFK@FMIB LC G9@97H=CB. 7B KBBA QL BS>IR>QB QEB
MLMRI>QFLK >KA ABQBOJFKB TEF@E JBJ?BOP >OB CFQ QL ?B PBIB@QBA >P M>OBKQP CLO QEB KBUQ
DBKBO>QFLK. 4EB MOL@BPP LC PBIB@QFLK @>K ?B AFSFABA FKQL QTL PQBMP.
1) EA,7@,?0 14?90>>. 1) EA,7@,?0 14?90>>.
&LO LRO DBKBQF@ >IDLOFQEJ QL CRK@QFLK MOLMBOIV, TB TFII KBBA QL ABPFDK TE>Q FP OBCBOOBA QL >P >
14?90>> 1@9.?4:9 14?90>> 1@9.?4:9. 4EB CRK@QFLK TFII MOLAR@B > KRJBOF@ P@LOB QL ABP@OF?B QEB CFQKBPP LC > DFSBK
JBJ?BO LC QEB MLMRI>QFLK. 4EFP, LC @LROPB, FP KLQ ELT QEB OB>I TLOIA TLOHP >Q >II. #OB>QROBP >OB
KLQ DFSBK > P@LOB; QEBV PFJMIV PROSFSB LO KLQ. "RQ FK QEB @>PB LC QEB QO>AFQFLK>I DBKBQF@
>IDLOFQEJ, TEBOB TB >OB QOVFKD QL BSLISB >K LMQFJ>I PLIRQFLK QL > MOL?IBJ, TB KBBA QL ?B >?IB
QL KRJBOF@>IIV BS>IR>QB >KV DFSBK MLPPF?IB PLIRQFLK.
,BQfP BU>JFKB LRO @ROOBKQ BU>JMIB, QEB QVMFKD JLKHBV. !D>FK, IBQfP PFJMIFCV QEB P@BK>OFL >KA
P>V TB >OB >QQBJMQFKD QL BSLISB QEB TLOA g@>Qh. 7B E>SB QEOBB JBJ?BOP LC QEB MLMRI>QFLK:
<IH, 75F, >KA 6CL. C5F FP L?SFLRPIV QEB JLPQ CFQ, DFSBK QE>Q FQ E>P QTL @LOOB@Q @E>O>@QBOP, <IH E>P
LKIV LKB, >KA 6CL E>P WBOL. !KA QEBOB FQ FP, LRO CFQKBPP CRK@QFLK:
<?JD;II = J>; DKC8;H E< 9EHH;9J 9>7H79J;HI
DNA DNA Fitness Fitness
ERQ 1
@>O 2
?LU 0
7B TFII BSBKQR>IIV T>KQ QL ILLH >Q BU>JMIBP TFQE JLOB PLMEFPQF@>QBA CFQKBPP CRK@QFLKP, ?RQ QEFP
FP > DLLA MI>@B QL PQ>OQ.
2) C=0,?0 , 8,?492 ;::7. 2) C=0,?0 , 8,?492 ;::7.
/K@B QEB CFQKBPP E>P ?BBK @>I@RI>QBA CLO >II JBJ?BOP LC QEB MLMRI>QFLK, TB @>K QEBK PBIB@Q
TEF@E JBJ?BOP >OB CFQ QL ?B@LJB M>OBKQP >KA MI>@B QEBJ FK > J>QFKD MLLI. 4EBOB >OB PBSBO>I
AFCCBOBKQ >MMOL>@EBP TB @LRIA Q>HB EBOB. &LO BU>JMIB, TB @LRIA BJMILV TE>Q FP HKLTK >P QEB
074?4>? 074?4>? JBQELA >KA P>V, g7EF@E QTL JBJ?BOP LC QEB MLMRI>QFLK P@LOBA QEB EFDEBPQ? 9LR QTL
TFII J>HB >II QEB @EFIAOBK CLO QEB KBUQ DBKBO>QFLK.h 4EFP FP MOL?>?IV LKB LC QEB B>PFBO JBQELAP
QL MOLDO>J; ELTBSBO, FQ CIFBP FK QEB C>@B LC QEB MOFK@FMIB LC S>OF>QFLK. )C QTL JBJ?BOP LC QEB
MLMRI>QFLK (LRQ LC MBOE>MP QELRP>KAP) >OB QEB LKIV LKBP >S>FI>?IB QL OBMOLAR@B, QEB KBUQ
DBKBO>QFLK TFII E>SB IFQQIB S>OFBQV >KA QEFP J>V PQRKQ QEB BSLIRQFLK>OV MOL@BPP. 7B @LRIA
FKPQB>A J>HB > J>QFKD MLLI LRQ LC > I>ODBO KRJ?BOdCLO BU>JMIB, QEB QLM 50% LC QEB
4EB .>QROB LC #LAB (S005)
397
MLMRI>QFLK, 500 LRQ LC 1,000. 4EFP FP >IPL GRPQ >P B>PV QL MOLDO>J, ?RQ FQ TFII KLQ MOLAR@B
LMQFJ>I OBPRIQP. )K QEFP @>PB, QEB EFDE-P@LOFKD QLM BIBJBKQP TLRIA E>SB QEB P>JB @E>K@B LC
?BFKD PBIB@QBA >P > M>OBKQ >P QEB LKBP QLT>OA QEB JFAAIB. !KA TEV PELRIA BIBJBKQ KRJ?BO
500 E>SB > PLIFA PELQ LC OBMOLAR@FKD, TEFIB BIBJBKQ KRJ?BO 501 E>P KL PELQ?
! ?BQQBO PLIRQFLK CLO QEB J>QFKD MLLI FP QL RPB > ;=:-,-474>?4. ;=:-,-474>?4. JBQELA, TEF@E TBfII @>II QEB
gTEBBI LC CLOQRKBh (>IPL HKLTK >P QEB gOLRIBQQB TEBBIh). 4L FIIRPQO>QB QEFP JBQELA, IBQfP
@LKPFABO > PFJMIB BU>JMIB TEBOB TB E>SB > MLMRI>QFLK LC CFSB BIBJBKQP, B>@E TFQE > CFQKBPP
P@LOB.
Element Element Fitness Fitness
! 3
" 4
# 0.5
$ 1.5
% 1
4EB CFOPQ QEFKD TBfII T>KQ QL AL FP 9:=8,74E0 9:=8,74E0 >II QEB P@LOBP. 2BJBJ?BO KLOJ>IFWFKD > SB@QLO?
4E>Q FKSLISBA Q>HFKD >K SB@QLO >KA PQ>KA>OAFWFKD FQP IBKDQE, PBQQFKD FQ QL 1. 7EBK TB
KLOJ>IFWB > PBQ LC CFQKBPP P@LOBP, TB >OB PQ>KA>OAFWFKD QEBFO O>KDB QL ?BQTBBK 0 >KA 1, >P >
MBO@BKQ>DB LC QLQ>I CFQKBPP. ,BQfP >AA RM >II QEB CFQKBPP P@LOBP.
QLQ>I CFQKBPP = 3 + 4 + 0.5 + 1.5 + 1 = 10
4EBK IBQfP AFSFAB B>@E P@LOB ?V QEB QLQ>I CFQKBPP, DFSFKD RP QEB KLOJ>IFWBA CFQKBPP.
Element Element Fitness Fitness Normalized Fitness Normalized Fitness
Expressed as a Expressed as a
Percentage Percentage
! 3 0.3 30%
" 4 0.4 40%
# 0.5 0.05 5%
$ 1.5 0.15 15%
% 1 0.1 10%
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
398
.LT FQfP QFJB CLO QEB TEBBI LC CLOQRKB.
3MFK QEB TEBBI >KA VLRfII KLQF@B QE>Q %IBJBKQ " E>P QEB EFDEBPQ @E>K@B LC ?BFKD PBIB@QBA,
CLIILTBA ?V !, QEBK $, QEBK %, >KA CFK>IIV #. 4EFP MOL?>?FIFQV-?>PBA PBIB@QFLK >@@LOAFKD QL
CFQKBPP FP >K BU@BIIBKQ >MMOL>@E. /KB, FQ DR>O>KQBBP QE>Q QEB EFDEBPQ-P@LOFKD BIBJBKQP TFII ?B
JLPQ IFHBIV QL OBMOLAR@B. 4TL, FQ ALBP KLQ BKQFOBIV BIFJFK>QB >KV S>OF>QFLK COLJ QEB MLMRI>QFLK.
5KIFHB TFQE QEB BIFQFPQ JBQELA, BSBK QEB ILTBPQ-P@LOFKD BIBJBKQ (FK QEFP @>PB #) E>P > @E>K@B
QL M>PP FQP FKCLOJ>QFLK ALTK QL QEB KBUQ DBKBO>QFLK. )QfP NRFQB MLPPF?IB (>KA LCQBK QEB @>PB)
QE>Q BSBK ILT-P@LOFKD BIBJBKQP E>SB > QFKV KRDDBQ LC DBKBQF@ @LAB QE>Q FP QORIV RPBCRI >KA
PELRIA KLQ BKQFOBIV ?B BIFJFK>QBA COLJ QEB MLMRI>QFLK. &LO BU>JMIB, FK QEB @>PB LC BSLISFKD gQL
?B LO KLQ QL ?Bh, TB JFDEQ E>SB QEB CLIILTFKD BIBJBKQP.
A: JE 8; EH DEJ JE =E
B: JE 8; EH DEJ JE F?
C: NNNNNNNNNNNNNNNN8;
!P VLR @>K PBB, BIBJBKQP ! >KA " >OB @IB>OIV QEB JLPQ CFQ >KA TLRIA E>SB QEB EFDEBPQ P@LOB.
"RQ KBFQEBO @LKQ>FKP QEB @LOOB@Q @E>O>@QBOP CLO QEB BKA LC QEB MEO>PB. %IBJBKQ #, BSBK QELRDE
FQ TLRIA OB@BFSB > SBOV ILT P@LOB, E>MMBKP QL E>SB QEB DBKBQF@ A>Q> CLO QEB BKA LC QEB MEO>PB.
!KA PL TEFIB TB TLRIA T>KQ ! >KA " QL ?B MF@HBA QL DBKBO>QB QEB J>GLOFQV LC QEB KBUQ
DBKBO>QFLK, TB TLRIA PQFII T>KQ # QL E>SB > PJ>II @E>K@B QL M>OQF@FM>QB FK QEB OBMOLAR@QFSB
MOL@BPP.
F64B?2 9.2
9.6 The Genetic Algorithm, Part III: Reproduction 9.6 The Genetic Algorithm, Part III: Reproduction
.LT QE>Q TB E>SB > PQO>QBDV CLO MF@HFKD M>OBKQP, TB KBBA QL CFDROB LRQ ELT QL RPB
F9DFC8I7H=CB QL J>HB QEB MLMRI>QFLKfP KBUQ DBKBO>QFLK, HBBMFKD FK JFKA QEB $>OTFKF>K
MOFK@FMIB LC EBOBAFQVdQE>Q @EFIAOBK FKEBOFQ MOLMBOQFBP COLJ QEBFO M>OBKQP. !D>FK, QEBOB >OB >
KRJ?BO LC AFCCBOBKQ QB@EKFNRBP TB @LRIA BJMILV EBOB. &LO BU>JMIB, LKB OB>PLK>?IB (>KA B>PV
QL MOLDO>J) PQO>QBDV FP >PBUR>I OBMOLAR@QFLK, JB>KFKD TB MF@H GRPQ LKB M>OBKQ >KA @OB>QB >
4EB .>QROB LC #LAB (S005)
399
@EFIA QE>Q FP >K BU>@Q @LMV LC QE>Q M>OBKQ. 4EB PQ>KA>OA >MMOL>@E TFQE DBKBQF@ >IDLOFQEJP,
ELTBSBO, FP QL MF@H QTL M>OBKQP >KA @OB>QB > @EFIA >@@LOAFKD QL QEB CLIILTFKD PQBMP.
1) C=:>>:A0=. 1) C=:>>:A0=.
#OLPPLSBO FKSLISBP @OB>QFKD > @EFIA LRQ LC QEB DBKBQF@ @LAB LC QTL M>OBKQP. )K QEB @>PB LC
QEB JLKHBV-QVMFKD BU>JMIB, IBQfP >PPRJB TBfSB MF@HBA QTL MEO>PBP COLJ QEB J>QFKD MLLI
(>P LRQIFKBA FK LRO PBIB@QFLK PQBM).
+7H;DJ A: "*,&
+7H;DJ B: +'A3
)QfP KLT RM QL RP QL J>HB > @EFIA MEO>PB COLJ QEBPB QTL. 0BOE>MP QEB JLPQ L?SFLRP T>V
(IBQfP @>II QEFP QEB 50/50 JBQELA) TLRIA ?B QL Q>HB QEB CFOPQ QTL @E>O>@QBOP COLJ ! >KA QEB
PB@LKA QTL COLJ ", IB>SFKD RP TFQE:
! S>OF>QFLK LC QEFP QB@EKFNRB FP QL MF@H > O>KALJ JFAMLFKQ. )K LQEBO TLOAP, TB ALKfQ E>SB QL
MF@H BU>@QIV E>IC LC QEB @LAB COLJ B>@E M>OBKQ. 7B @LRIA PLJBQFJBP BKA RM TFQE &,!9, >KA
PLJBQFJBP TFQE &/29. 4EFP FP MOBCBO>?IB QL QEB 50/50 >MMOL>@E, PFK@B TB FK@OB>PB QEB
S>OFBQV LC MLPPF?FIFQFBP CLO QEB KBUQ DBKBO>QFLK.
F64B?2 9.3
F64B?2 9.4: '6086;4 . ?.;1<: :61=<6;A
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
400
!KLQEBO MLPPF?FIFQV FP QL O>KALJIV PBIB@Q > M>OBKQ CLO B>@E @E>O>@QBO FK QEB @EFIA PQOFKD. 9LR
@>K QEFKH LC QEFP >P CIFMMFKD > @LFK CLRO QFJBP: EB>AP Q>HB COLJ M>OBKQ !, Q>FIP COLJ M>OBKQ ".
(BOB TB @LRIA BKA RM TFQE J>KV AFCCBOBKQ OBPRIQP PR@E >P: 0,29, &,2+, &,29, &/29, BQ@.
4EFP PQO>QBDV TFII MOLAR@B BPPBKQF>IIV QEB P>JB OBPRIQP >P QEB O>KALJ JFAMLFKQ JBQELA;
ELTBSBO, FC QEB LOABO LC QEB DBKBQF@ FKCLOJ>QFLK MI>VP PLJB OLIB FK BUMOBPPFKD QEB MEBKLQVMB,
VLR J>V MOBCBO LKB PLIRQFLK LSBO QEB LQEBO.
2) "@?,?4:9. 2) "@?,?4:9.
/K@B QEB @EFIA $.! E>P ?BBK @OB>QBA SF> @OLPPLSBO, TB >MMIV LKB CFK>I MOL@BPP ?BCLOB >AAFKD
QEB @EFIA QL QEB KBUQ DBKBO>QFLKd8@?,?4:9 8@?,?4:9. -RQ>QFLK FP >K LMQFLK>I PQBM, >P QEBOB >OB PLJB
@>PBP FK TEF@E FQ FP RKKB@BPP>OV. (LTBSBO, FQ BUFPQP ?B@>RPB LC QEB $>OTFKF>K MOFK@FMIB LC
S>OF>QFLK. 7B @OB>QBA >K FKFQF>I MLMRI>QFLK O>KALJIV, J>HFKD PROB QE>Q TB PQ>OQ TFQE > S>OFBQV LC
BIBJBKQP. (LTBSBO, QEBOB @>K LKIV ?B PL JR@E S>OFBQV TEBK PBBAFKD QEB CFOPQ DBKBO>QFLK, >KA
JRQ>QFLK >IILTP RP QL FKQOLAR@B >AAFQFLK>I S>OFBQV QEOLRDELRQ QEB BSLIRQFLK>OV MOL@BPP FQPBIC.
-RQ>QFLK FP ABP@OF?BA FK QBOJP LC > F5H9. !
DFSBK DBKBQF@ >IDLOFQEJ JFDEQ E>SB >
JRQ>QFLK O>QB LC 5% LO 1% LO 0.1%, BQ@. ,BQfP
>PPRJB TB GRPQ CFKFPEBA TFQE @OLPPLSBO >KA
BKABA RM TFQE QEB @EFIA &/29. )C TB E>SB >
JRQ>QFLK O>QB LC 1%, QEFP JB>KP QE>Q CLO B>@E
@E>O>@QBO FK QEB MEO>PB DBKBO>QBA COLJ
@OLPPLSBO, QEBOB FP > 1% @E>K@B QE>Q FQ TFII
JRQ>QB. 7E>Q ALBP FQ JB>K CLO > @E>O>@QBO QL
JRQ>QB? )K QEFP @>PB, TB ABCFKB JRQ>QFLK >P
MF@HFKD > KBT O>KALJ @E>O>@QBO. ! 1%
MOL?>?FIFQV FP C>FOIV ILT, >KA JLPQ LC QEB QFJB JRQ>QFLK TFII KLQ L@@RO >Q >II FK > CLRO-@E>O>@QBO
PQOFKD (96% LC QEB QFJB QL ?B JLOB MOB@FPB). (LTBSBO, TEBK FQ ALBP, QEB JRQ>QBA @E>O>@QBO FP
OBMI>@BA TFQE > O>KALJIV DBKBO>QBA LKB (PBB &FDROB 9.6).
F64B?2 9.5: C<6;-396==6;4 .==?<.05
F64B?2 9.6
4EB .>QROB LC #LAB (S005)
401
!P TBfII PBB FK PLJB LC QEB BU>JMIBP, QEB JRQ>QFLK O>QB @>K DOB>QIV >CCB@Q QEB ?BE>SFLO LC
QEB PVPQBJ. #BOQ>FKIV, > SBOV EFDE JRQ>QFLK O>QB (PR@E >P, P>V, 80%) TLRIA KBD>QB QEB
BSLIRQFLK>OV MOL@BPP FQPBIC. )C QEB J>GLOFQV LC > @EFIAfP DBKBP >OB DBKBO>QBA O>KALJIV, QEBK
TB @>KKLQ DR>O>KQBB QE>Q QEB JLOB gCFQh DBKBP L@@RO TFQE DOB>QBO COBNRBK@V TFQE B>@E
PR@@BPPFSB DBKBO>QFLK.
4EB MOL@BPP LC PBIB@QFLK (MF@HFKD QTL M>OBKQP) >KA OBMOLAR@QFLK (@OLPPLSBO >KA JRQ>QFLK) FP
>MMIFBA LSBO >KA LSBO >D>FK , QFJBP RKQFI TB E>SB > KBT MLMRI>QFLK LC , BIBJBKQP. !Q QEFP
MLFKQ, QEB KBT MLMRI>QFLK LC @EFIAOBK ?B@LJBP QEB @ROOBKQ MLMRI>QFLK >KA TB ILLM ?>@H QL
BS>IR>QB CFQKBPP >KA MBOCLOJ PBIB@QFLK >KA OBMOLAR@QFLK >D>FK.
.LT QE>Q TB E>SB ABP@OF?BA >II QEB PQBMP LC QEB DBKBQF@ >IDLOFQEJ FK ABQ>FI, FQfP QFJB QL
QO>KPI>QB QEBPB PQBMP FKQL 0OL@BPPFKD @LAB. "B@>RPB QEB MOBSFLRP ABP@OFMQFLK T>P > ?FQ
ILKDTFKABA, IBQfP ILLH >Q >K LSBOSFBT LC QEB >IDLOFQEJ CFOPQ. 7BfII QEBK @LSBO B>@E LC QEB
QEOBB PQBMP FK FQP LTK PB@QFLK, TLOHFKD LRQ QEB @LAB.
'E()%: 'E()%:
3QBM 1: I94?4,74E0 I94?4,74E0. #OB>QB > MLMRI>QFLK LC . BIBJBKQP, B>@E TFQE O>KALJIV DBKBO>QBA $.!.
!$$%: !$$%:
3QBM 2: '070.?4:9 '070.?4:9. %S>IR>QB QEB CFQKBPP LC B>@E BIBJBKQ LC QEB MLMRI>QFLK >KA ?RFIA > J>QFKD
MLLI.
3QBM 3: &0;=:/@.?4:9 &0;=:/@.?4:9. 2BMB>Q . QFJBP:
>) 0F@H QTL M>OBKQP TFQE MOL?>?FIFQV >@@LOAFKD QL OBI>QFSB CFQKBPP.
?) #OLPPLSBOd@OB>QB > g@EFIAh ?V @LJ?FKFKD QEB $.! LC QEBPB QTL M>OBKQP.
@) -RQ>QFLKdJRQ>QB QEB @EFIAfP $.! ?>PBA LK > DFSBK MOL?>?FIFQV.
A) !AA QEB KBT @EFIA QL > KBT MLMRI>QFLK.
3QBM 4. 2BMI>@B QEB LIA MLMRI>QFLK TFQE QEB KBT MLMRI>QFLK >KA OBQROK QL 3QBM 2.
9.7 Code for Creating the Population 9.7 Code for Creating the Population
Step 1: Initialize Population Step 1: Initialize Population
)C TBfOB DLFKD QL @OB>QB > MLMRI>QFLK, TB KBBA > A>Q> PQOR@QROB QL PQLOB > IFPQ LC JBJ?BOP LC
QEB MLMRI>QFLK. )K JLPQ @>PBP (PR@E >P LRO QVMFKD-JLKHBV BU>JMIB), QEB KRJ?BO LC BIBJBKQP
FK QEB MLMRI>QFLK @>K ?B CFUBA, >KA PL TB RPB >K >OO>V. (,>QBO TBfII PBB BU>JMIBP QE>Q
FKSLISB > DOLTFKD/PEOFKHFKD MLMRI>QFLK >KA TBfII RPB >K ALL;S*CMN.) "RQ >K >OO>V LC TE>Q?
7B KBBA >K L?GB@Q QE>Q PQLOBP QEB DBKBQF@ FKCLOJ>QFLK CLO > JBJ?BO LC QEB MLMRI>QFLK. ,BQfP
@>II FQ D#A D#A.
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
402
4EB MLMRI>QFLK TFII QEBK ?B >K >OO>V LC ",A L?GB@QP.
"RQ TE>Q PQRCC DLBP FK QEB ",A @I>PP? &LO > QVMFKD JLKHBV, FQP $.! FP QEB O>KALJ MEO>PB FQ
QVMBP, > PQOFKD LC @E>O>@QBOP.
7EFIB QEFP FP MBOCB@QIV OB>PLK>?IB CLO QEFP M>OQF@RI>O BU>JMIB, TBfOB KLQ DLFKD QL RPB >K >@QR>I
0NLCHA L?GB@Q >P QEB DBKBQF@ @LAB. )KPQB>A, TBfII RPB >K >OO>V LC @E>O>@QBOP.
"V RPFKD >K >OO>V, TBfII ?B >?IB QL BUQBKA >II QEB @LAB TB TOFQB FKQL LQEBO BU>JMIBP. &LO
BU>JMIB, QEB $.! LC > @OB>QROB FK > MEVPF@P PVPQBJ JFDEQ ?B >K >OO>V LC .3?=NILPdLO CLO >K
FJ>DB, >K >OO>V LC FKQBDBOP (2'" @LILOP). 7B @>K ABP@OF?B >KV PBQ LC MOLMBOQFBP FK >K >OO>V,
>KA BSBK QELRDE > PQOFKD FP @LKSBKFBKQ CLO QEFP M>OQF@RI>O PHBQ@E, >K >OO>V TFII PBOSB >P > ?BQQBO
CLRKA>QFLK CLO CRQROB BSLIRQFLK>OV BU>JMIBP.
/RO DBKBQF@ >IDLOFQEJ AF@Q>QBP QE>Q TB @OB>QB > MLMRI>QFLK LC . BIBJBKQP, B>@E TFQE F5B8CA@M
;9B9F5H98 *A. 4EBOBCLOB, FK QEB L?GB@QfP @LKPQOR@QLO, TB O>KALJIV @OB>QB B>@E @E>O>@QBO LC
QEB >OO>V.
.LT QE>Q TB E>SB QEB @LKPQOR@QLO, TB @>K OBQROK QL M?NOJ() >KA FKFQF>IFWB B>@E ",A L?GB@Q FK
QEB MLMRI>QFLK >OO>V.
=F;MM ",A U
W
A population of 100 DNA objects ",A89 JIJOF;NCIH = H?Q ",A81009;
=F;MM ",A U
0NLCHA JBL;M?;
W
=F;MM ",A U
Each "gene" is one element of the array. We
need 18 genes because to be or not to be
is 18 characters long.
=B;L89 A?H?M = H?Q =B;L8189;
W
=F;MM ",A U
=B;L89 A?H?M = H?Q =B;L8189;
",A() U
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
Picking randomly from a range of characters
with ASCII values between 32 and 128. For
more about ASCII: http://en.wikipedia.org/
wiki/ASCII
A?H?M8C9 = (=B;L) L;H>IG(32,128);
W
W
W
4EB .>QROB LC #LAB (S005)
403
/RO ",A @I>PP FP KLQ >Q >II @LJMIBQB. 7BfII KBBA QL >AA CRK@QFLKP QL FQ QL MBOCLOJ >II QEB LQEBO
Q>PHP FK LRO DBKBQF@ >IDLOFQEJ, TEF@E TBfII AL >P TB T>IH QEOLRDE PQBMP 2 >KA 3.
",A89 JIJOF;NCIH = H?Q ",A81009;
PIC> M?NOJ() U
@IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
Initializing each member of the population JIJOF;NCIH8C9 = H?Q ",A();
W
W
Step 2: Selection Step 2: Selection
3QBM 2 OB>AP, T!J5@I5H9 H<9 :=HB9GG C: 957< 9@9A9BH C: H<9 DCDI@5H=CB 5B8 6I=@8 5 A5H=B;
DCC@.U ,BQfP CFOPQ BS>IR>QB B>@E L?GB@QfP CFQKBPP. %>OIFBO TB PQ>QBA QE>Q LKB MLPPF?IB CFQKBPP
CRK@QFLK CLO LRO QVMBA MEO>PBP FP QEB QLQ>I KRJ?BO LC @LOOB@Q @E>O>@QBOP. ,BQfP OBSFPB QEFP
CFQKBPP CRK@QFLK > IFQQIB ?FQ >KA PQ>QB FQ >P QEB MBO@BKQ>DB LC @LOOB@Q @E>O>@QBOPdF.B., QEB QLQ>I
KRJ?BO LC @LOOB@Q @E>O>@QBOP AFSFABA ?V QEB QLQ>I @E>O>@QBOP.
"?JD;II = .EJ7B # C>7H79J;HI CEHH;9J/.EJ7B # C>7H79J;HI
7EBOB PELRIA TB @>I@RI>QB QEB CFQKBPP? 3FK@B QEB ",A @I>PP @LKQ>FKP QEB DBKBQF@ FKCLOJ>QFLK
(QEB MEO>PB TB TFII QBPQ >D>FKPQ QEB Q>ODBQ MEO>PB), TB @>K TOFQB > CRK@QFLK FKPFAB QEB ",A
@I>PP FQPBIC QL P@LOB FQP LTK CFQKBPP. ,BQfP >PPRJB TB E>SB > Q>ODBQ MEO>PB:
7B @>K KLT @LJM>OB B>@E gDBKBh >D>FKPQ QEB @LOOBPMLKAFKD @E>O>@QBO FK QEB Q>ODBQ MEO>PB,
FK@OBJBKQFKD > @LRKQBO B>@E QFJB TB DBQ > @LOOB@Q @E>O>@QBO.
0NLCHA N;LA?N = "NI <? IL HIN NI <?";
=F;MM ",A U
We are adding another variable to the DNA
class to track fitness.
@FI;N @CNH?MM;
Function to score fitness PIC> @CNH?MM () U
CHN M=IL? = 0;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
Is the character correct? C@ (A?H?M8C9 == N;LA?N.=B;LAN(C)) U
If so, increment the score. M=IL?++;
W
W
Fitness is the percentage correct. @CNH?MM = @FI;N(M=IL?)/N;LA?N.F?HANB();
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
404
)K QEB J>FK Q>?fP >L;Q(), QEB SBOV CFOPQ PQBM TBfII Q>HB FP QL @>II QEB CFQKBPP CRK@QFLK CLO B>@E
JBJ?BO LC QEB MLMRI>QFLK.
!CQBO TB E>SB >II QEB CFQKBPP P@LOBP, TB @>K ?RFIA QEB gJ>QFKD MLLIh QE>Q TBfII KBBA CLO QEB
OBMOLAR@QFLK PQBM. 4EB J>QFKD MLLI FP > A>Q> PQOR@QROB COLJ TEF@E TBfII @LKQFKRLRPIV MF@H QTL
M>OBKQP. 2B@>IIFKD LRO ABP@OFMQFLK LC QEB PBIB@QFLK MOL@BPP, TB T>KQ QL MF@H M>OBKQP TFQE
MOL?>?FIFQFBP @>I@RI>QBA >@@LOAFKD QL CFQKBPP. )K LQEBO TLOAP, QEB JBJ?BOP LC QEB MLMRI>QFLK
QE>Q E>SB QEB EFDEBPQ CFQKBPP P@LOBP PELRIA ?B JLPQ IFHBIV QL ?B MF@HBA; QELPB TFQE QEB ILTBPQ
P@LOBP, QEB IB>PQ IFHBIV.
)K QEB )KQOLAR@QFLK (PBB M>DB 7), TB @LSBOBA QEB ?>PF@P LC MOL?>?FIFQV >KA DBKBO>QFKD > @RPQLJ
AFPQOF?RQFLK LC O>KALJ KRJ?BOP. 7BfOB DLFKD QL RPB QELPB QB@EKFNRBP QL >PPFDK > MOL?>?FIFQV
QL B>@E JBJ?BO LC QEB MLMRI>QFLK, MF@HFKD M>OBKQP ?V PMFKKFKD QEB gTEBBI LC CLOQRKB.h ,BQfP
ILLH >Q &FDROB 9.2 >D>FK.
)Q JFDEQ ?B CRK QL AL PLJBQEFKD OFAF@RILRP >KA >@QR>IIV MOLDO>J > PFJRI>QFLK LC > PMFKKFKD
TEBBI >P ABMF@QBA >?LSB. "RQ QEFP FP NRFQB RKKB@BPP>OV.
PIC> >L;Q() U
@IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
JIJOF;NCIH8C9.@CNH?MM();
W
F64B?2 9.2 (.4.6;)
4EB .>QROB LC #LAB (S005)
405
)KPQB>A TB @>K MF@H COLJ QEB CFSB LMQFLKP
(!"#$%) >@@LOAFKD QL QEBFO MOL?>?FIFQFBP ?V
CFIIFKD >K ALL;S*CMN TFQE JRIQFMIB FKPQ>K@BP
LC B>@E M>OBKQ. )K LQEBO TLOAP, IBQfP P>V VLR
E>A > ?R@HBQ LC TLLABK IBQQBOPd30 !P, 40
"P, 5 #P, 15 $P, >KA 10 %P.
)C VLR MF@H > O>KALJ IBQQBO LRQ LC QE>Q
?R@HBQ, QEBOBfP > 30% @E>K@B VLRfII DBQ >K
!, > 5% @E>K@B VLRfII DBQ > #, >KA PL LK.
&LO RP, QE>Q ?R@HBQ FP >K ALL;S*CMN, >KA
B>@E TLLABK IBQQBO FP > MLQBKQF>I M>OBKQ.
7B >AA B>@E M>OBKQ QL QEB ALL;S*CMN .
KRJ?BO LC QFJBP TEBOB . FP BNR>I QL FQP
MBO@BKQ>DB P@LOB.
F64B?2 9.7
Start with an empty mating pool. ALL;S*CMN<",A> G;NCHA.IIF = H?Q
ALL;S*CMN<",A>();
@IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
n is equal to fitness times 100, which
leaves us with an integer between 0 and
100.
CHN H = CHN(JIJOF;NCIH8C9.@CNH?MM * 100);
@IL (CHN D = 0; D < H; D++) U
Add each member of the population to the
mating pool N times.
G;NCHA.IIF.;>>(JIJOF;NCIH8C9);
W
W
/KB LC QEB LQEBO JBQELAP TB RPBA QL DBKBO>QB > @RPQLJ AFPQOF?RQFLK LC O>KALJ
KRJ?BOP FP @>IIBA QEB -LKQB #>OIL JBQELA. 4EFP QB@EKFNRB FKSLISBA MF@HFKD QTL
O>KALJ KRJ?BOP, TFQE QEB PB@LKA KRJ?BO >@QFKD >P > NR>IFCVFKD KRJ?BO >KA
ABQBOJFKFKD FC QEB CFOPQ O>KALJ KRJ?BO PELRIA ?B HBMQ LO QEOLTK >T>V. 2BTOFQB QEB
>?LSB J>QFKD MLLI >IDLOFQEJ QL RPB QEB -LKQB #>OIL JBQELA FKPQB>A.
Exercise 9.2 Exercise 9.2
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
406
)K PLJB @>PBP, QEB TEBBI LC CLOQRKB >IDLOFQEJ TFII E>SB >K BUQO>LOAFK>OFIV EFDE
MOBCBOBK@B CLO PLJB BIBJBKQP LSBO LQEBOP. 4>HB QEB CLIILTFKD MOL?>?FIFQFBP:
A: 98%
B: 1%
C: 1%
4EFP FP PLJBQFJBP RKABPFO>?IB DFSBK ELT FQ TFII AB@OB>PB QEB >JLRKQ LC S>OFBQV FK QEFP
PVPQBJ. ! PLIRQFLK QL QEFP MOL?IBJ FP QL OBMI>@B QEB @>I@RI>QBA CFQKBPP P@LOBP TFQE QEB
LOAFK>IP LC P@LOFKD (JB>KFKD QEBFO O>KH).
A: 50% (3/6)
B: 33% (2/6)
C: 17% (1/6)
2BTOFQB QEB J>QFKD MLLI >IDLOFQEJ QL RPB QEFP JBQELA FKPQB>A.
Exercise 9.3 Exercise 9.3
Step 3: Reproduction Step 3: Reproduction
7FQE QEB J>QFKD MLLI OB>AV QL DL, FQfP QFJB QL J>HB PLJB ?>?FBP. 4EB CFOPQ PQBM FP QL MF@H QTL
M>OBKQP. !D>FK, FQfP PLJBTE>Q LC >K >O?FQO>OV AB@FPFLK QL MF@H QTL M>OBKQP. )Q @BOQ>FKIV JFOOLOP
ERJ>K OBMOLAR@QFLK >KA FP QEB PQ>KA>OA JB>KP FK QEB QO>AFQFLK>I '!, ?RQ FK QBOJP LC VLRO
TLOH, QEBOB OB>IIV >OBKfQ >KV OBPQOF@QFLKP EBOB. 9LR @LRIA @ELLPB QL MBOCLOJ g>PBUR>Ih
OBMOLAR@QFLK TFQE LKB M>OBKQ, LO @LJB RM TFQE > P@EBJB CLO MF@HFKD QEOBB LO CLRO M>OBKQP COLJ
TEF@E QL DBKBO>QB @EFIA $.!. &LO QEFP @LAB ABJLKPQO>QFLK, TBfII PQF@H QL QTL M>OBKQP >KA @>II
QEBJ J;L?HNA >KA J;L?HN.
&FOPQ QEFKD TB KBBA >OB QTL O>KALJ FKAF@BP FKQL QEB J>QFKD MLLIdO>KALJ KRJ?BOP ?BQTBBK 0
>KA QEB PFWB LC QEB ALL;S*CMN.
7B @>K RPB QEBPB FKAF@BP QL OBQOFBSB >K >@QR>I $.! FKPQ>K@B COLJ QEB J>QFKD MLLI.
"B@>RPB TB E>SB JRIQFMIB FKPQ>K@BP LC QEB P>JB ",A L?GB@QP FK QEB J>QFKD MLLI (KLQ QL
JBKQFLK QE>Q TB @LRIA MF@H QEB P>JB O>KALJ KRJ?BO QTF@B), FQfP MLPPF?IB QE>Q J;L?HNA >KA
J;L?HN @LRIA ?B QEB P>JB ",A L?GB@Q. )C TB T>KQBA QL ?B PQOF@Q, TB @LRIA TOFQB PLJB @LAB QL
CHN ; = CHN(L;H>IG(G;NCHA.IIF.MCT?()));
CHN < = CHN(L;H>IG(G;NCHA.IIF.MCT?()));
",A J;L?HNA = G;NCHA.IIF.A?N(;);
",A J;L?HN = G;NCHA.IIF.A?N(<);
4EB .>QROB LC #LAB (S005)
407
BKPROB QE>Q TB E>SBKfQ MF@HBA QEB P>JB M>OBKQ QTF@B, ?RQ TB TLRIA D>FK SBOV IFQQIB
BCCF@FBK@V CLO >II QE>Q BUQO> @LAB. 3QFII, FQeP TLOQE QOVFKD QEFP >P >K BUBO@FPB.
/K@B TB E>SB QEB QTL M>OBKQP, TB @>K MBOCLOJ .=:>>:A0= .=:>>:A0= QL DBKBO>QB QEB @EFIA $.!,
CLIILTBA ?V 8@?,?4:9 8@?,?4:9.
/C @LROPB, QEB CRK@QFLKP =LIMMIP?L() >KA GON;N?() ALKfQ J>DF@>IIV BUFPQ FK LRO ",A @I>PP;
TB E>SB QL TOFQB QEBJ. 4EB T>V TB @>IIBA =LIMMIP?L() >?LSB FKAF@>QBP QE>Q QEB CRK@QFLK
OB@BFSBP >K FKPQ>K@B LC $.! >P >K >ODRJBKQ >KA OBQROKP > KBT FKPQ>K@B LC $.!, QEB @EFIA.
4EB >?LSB @OLPPLSBO CRK@QFLK RPBP QEB gO>KALJ JFAMLFKQh JBQELA LC @OLPPLSBO, FK TEF@E
QEB CFOPQ PB@QFLK LC DBKBP FP Q>HBK COLJ M>OBKQ ! >KA QEB PB@LKA PB@QFLK COLJ M>OBKQ ".
!AA @LAB QL QEB >?LSB QL DR>O>KQBB QE>Q VLR E>SB MF@HBA QTL RKFNRB gM>OBKQP.h
Exercise 9.4 Exercise 9.4
A function for crossover ",A =BCF> = J;L?HNA.=LIMMIP?L(J;L?HN);
A function for mutation =BCF>.GON;N?();
The function receives one argument (DNA)
and returns DNA.
",A =LIMMIP?L(",A J;LNH?L) U
The child is a new instance of DNA. Note
that the DNA is generated randomly in the
constructor, but we will overwrite it below
with DNA from parents.
",A =BCF> = H?Q ",A();
Picking a random midpoint in the genes
array
CHN GC>JICHN = CHN(L;H>IG(A?H?M.F?HANB));
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
Before midpoint copy genes from one
parent, after midpoint copy genes from the
other parent
C@ (C > GC>JICHN) =BCF>.A?H?M8C9 = A?H?M8C9;
?FM? =BCF>.A?H?M8C9 = J;LNH?L.A?H?M8C9;
W
Return the new child DNA L?NOLH =BCF>;
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
408
4EB JRQ>QB() CRK@QFLK FP BSBK PFJMIBO QL TOFQB QE>K @OLPPLSBO(). !II TB KBBA QL AL FP ILLM
QEOLRDE QEB >OO>V LC DBKBP >KA CLO B>@E O>KALJIV MF@H > KBT @E>O>@QBO >@@LOAFKD QL QEB
JRQ>QFLK O>QB. 7FQE > JRQ>QFLK O>QB LC 1%, CLO BU>JMIB, TB TLRIA MF@H > KBT @E>O>@QBO LKB
QFJB LRQ LC > ERKAOBA.
4EB BKQFOB CRK@QFLK QEBOBCLOB OB>AP:
2BTOFQB QEB @OLPPLSBO CRK@QFLK QL RPB QEB g@LFK CIFMMFKDh JBQELA FKPQB>A, FK TEF@E B>@E
DBKB E>P > 50% @E>K@B LC @LJFKD COLJ M>OBKQ ! >KA > 50% @E>K@B LC @LJFKD COLJ
M>OBKQ ".
Exercise 9.5 Exercise 9.5
@FI;N GON;NCIH/;N? = 0.01;
C@ (L;H>IG(1) < GON;NCIH/;N?) U
Any code here would be executed 1% of the
time.
W
PIC> GON;N?() U
Looking at each gene in the array @IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
C@ (L;H>IG(1) < GON;NCIH/;N?) U
Mutation, a new random character A?H?M8C9 = (=B;L) L;H>IG(32,128);
W
W
W
9.8 Genetic Algorithms: Putting It All Together 9.8 Genetic Algorithms: Putting It All Together
9LR J>V E>SB KLQF@BA QE>Q TBfSB BPPBKQF>IIV T>IHBA QEOLRDE QEB PQBMP LC QEB DBKBQF@
>IDLOFQEJ QTF@B, LK@B ABP@OF?FKD FQ FK K>OO>QFSB CLOJ >KA >KLQEBO QFJB TFQE @LAB PKFMMBQP
FJMIBJBKQFKD B>@E LC QEB PQBMP. 7E>Q )fA IFHB QL AL FK QEFP PB@QFLK FP @LKABKPB QEB MOBSFLRP
QTL PB@QFLKP FKQL LKB M>DB, TFQE QEB >IDLOFQEJ ABP@OF?BA FK GRPQ QEOBB PQBMP >KA QEB
@LOOBPMLKAFKD @LAB >ILKDPFAB.
4EB .>QROB LC #LAB (S005)
409
Example 9.1: Genetic algorithm: Evolving Shakespeare
Variables we need for our GA
Mutation rate @FI;N GON;NCIH/;N?;
Population total CHN NIN;F.IJOF;NCIH = 150;
Population array ",A89 JIJOF;NCIH;
Mating pool ArrayList ALL;S*CMN<",A> G;NCHA.IIF;
Target phrase 0NLCHA N;LA?N;
PIC> M?NOJ() U
MCT?(200, 200);
Initializing target phrase and mutation rate N;LA?N = "NI <? IL HIN NI <?";
GON;NCIH/;N? = 0.01;
Step 1: Initialize Population JIJOF;NCIH = H?Q ",A8NIN;F.IJOF;NCIH9;
@IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
JIJOF;NCIH8C9 = H?Q ",A();
W
W
PIC> >L;Q() U
Step 2: Selection
Step 2a: Calculate fitness. @IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
JIJOF;NCIH8C9.@CNH?MM();
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
410
4EB J>FK Q>? MOB@FPBIV JFOOLOP QEB PQBMP LC QEB DBKBQF@ >IDLOFQEJ. (LTBSBO, JLPQ LC QEB
CRK@QFLK>IFQV @>IIBA RMLK FP >@QR>IIV MOBPBKQ FK QEB ",A @I>PP FQPBIC.
Step 2b: Build mating pool. ALL;S*CMN<",A> G;NCHA.IIF = H?Q ALL;S*CMN<",A>();
@IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
Add each member n times according to its
fitness score.
CHN H = CHN(JIJOF;NCIH8C9.@CNH?MM * 100);
@IL (CHN D = 0; D < H; D++) U
G;NCHA.IIF.;>>(JIJOF;NCIH8C9);
W
W
Step 3: Reproduction @IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
CHN ; = CHN(L;H>IG(G;NCHA.IIF.MCT?()));
CHN < = CHN(L;H>IG(G;NCHA.IIF.MCT?()));
",A J;LNH?LA = G;NCHA.IIF.A?N(;);
",A J;LNH?L = G;NCHA.IIF.A?N(<);
Step 3a: Crossover ",A =BCF> = J;LNH?LA.=LIMMIP?L(J;LNH?L);
Step 3b: Mutation =BCF>.GON;N?(GON;NCIH/;N?);
Note that we are overwriting the population
with the new children. When draw() loops,
we will perform all the same steps with the
new population of children.
JIJOF;NCIH8C9 = =BCF>;
W
W
=F;MM ",A U
=B;L89 A?H?M;
@FI;N @CNH?MM;
Create DNA randomly. ",A() U
A?H?M = H?Q =B;L8N;LA?N.F?HANB()9;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
A?H?M8C9 = (=B;L) L;H>IG(32,128);
W
W
Calculate fitness. PIC> @CNH?MM() U
CHN M=IL? = 0;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
C@ (A?H?M8C9 == N;LA?N.=B;LAN(C)) U
M=IL?++;
W
W
@CNH?MM = @FI;N(M=IL?)/N;LA?N.F?HANB();
W
4EB .>QROB LC #LAB (S005)
411
Crossover ",A =LIMMIP?L(",A J;LNH?L) U
",A =BCF> = H?Q ",A(A?H?M.F?HANB);
CHN GC>JICHN = CHN(L;H>IG(A?H?M.F?HANB));
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
C@ (C > GC>JICHN) =BCF>.A?H?M8C9 = A?H?M8C9;
?FM? =BCF>.A?H?M8C9 = J;LNH?L.A?H?M8C9;
W
L?NOLH =BCF>;
W
Mutation PIC> GON;N?(@FI;N GON;NCIH/;N?) U
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
C@ (L;H>IG(1) < GON;NCIH/;N?) U
A?H?M8C9 = (=B;L) L;H>IG(32,128);
W
W
W
Convert to StringPHENOTYPE. 0NLCHA A?N.BL;M?() U
L?NOLH H?Q 0NLCHA(A?H?M);
W
W
!AA CB>QROBP QL QEB >?LSB BU>JMIB QL OBMLOQ JLOB FKCLOJ>QFLK >?LRQ QEB MOLDOBPP LC
QEB DBKBQF@ >IDLOFQEJ FQPBIC. &LO BU>JMIB, PELT QEB MEO>PB @ILPBPQ QL QEB Q>ODBQ B>@E
DBKBO>QFLK, >P TBII >P OBMLOQ LK QEB KRJ?BO LC DBKBO>QFLKP, >SBO>DB CFQKBPP, BQ@. 3QLM
QEB DBKBQF@ >IDLOFQEJ LK@B FQ E>P PLISBA QEB MEO>PB. #LKPFABO TOFQFKD > .IJOF;NCIH
@I>PP QL J>K>DB QEB '! (FKPQB>A LC FK@IRAFKD >II QEB @LAB FK AO>T()).
Exercise 9.6 Exercise 9.6
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
412
9.9 Genetic Algorithms: Make Them Your Own 9.9 Genetic Algorithms: Make Them Your Own
4EB KF@B QEFKD >?LRQ RPFKD DBKBQF@ >IDLOFQEJP FK > MOLGB@Q FP QE>Q BU>JMIB @LAB @>K B>PFIV ?B
MLOQBA COLJ >MMIF@>QFLK QL >MMIF@>QFLK. 4EB @LOB JB@E>KF@P LC PBIB@QFLK >KA OBMOLAR@QFLK
ALKfQ KBBA QL @E>KDB. 4EBOB >OB, ELTBSBO, QEOBB HBV @LJMLKBKQP QL DBKBQF@ >IDLOFQEJP QE>Q
VLR, QEB ABSBILMBO, TFII E>SB QL @RPQLJFWB CLO B>@E RPB. 4EFP FP @OR@F>I QL JLSFKD ?BVLKA
QOFSF>I ABJLKPQO>QFLKP LC BSLIRQFLK>OV PFJRI>QFLKP (>P FK QEB 3E>HBPMB>OB BU>JMIB) QL @OB>QFSB
RPBP FK MOLGB@QP QE>Q VLR J>HB FK 0OL@BPPFKD >KA LQEBO @OB>QFSB MOLDO>JJFKD BKSFOLKJBKQP.
Key #1: Varying the variables Key #1: Varying the variables
4EBOB >OBKfQ > ILQ LC S>OF>?IBP QL QEB DBKBQF@ >IDLOFQEJ FQPBIC. )K C>@Q, FC VLR ILLH >Q QEB MOBSFLRP
BU>JMIBfP @LAB, VLRfII PBB LKIV QTL DIL?>I S>OF>?IBP (KLQ FK@IRAFKD QEB >OO>VP >KA ALL;S*CMNP
QL PQLOB QEB MLMRI>QFLK >KA J>QFKD MLLI).
4EBPB QTL S>OF>?IBP @>K DOB>QIV >CCB@Q QEB ?BE>SFLO LC QEB PVPQBJ, >KA FQfP KLQ PR@E > DLLA
FAB> QL >O?FQO>OFIV >PPFDK QEBJ S>IRBP (QELRDE QTB>HFKD QEBJ QEOLRDE QOF>I >KA BOOLO FP >
MBOCB@QIV OB>PLK>?IB T>V QL >OOFSB >Q LMQFJ>I S>IRBP).
4EB S>IRBP ) @ELPB CLO QEB 3E>HBPMB>OB ABJLKPQO>QFLK TBOB MF@HBA QL SFOQR>IIV DR>O>KQBB QE>Q
QEB DBKBQF@ >IDLOFQEJ TLRIA PLISB CLO QEB MEO>PB, ?RQ KLQ QLL NRF@HIV (>MMOLUFJ>QBIV 1,000
DBKBO>QFLKP LK >SBO>DB) PL >P QL ABJLKPQO>QB QEB MOL@BPP LSBO > OB>PLK>?IB MBOFLA LC QFJB. !
JR@E I>ODBO MLMRI>QFLK, ELTBSBO, TLRIA VFBIA C>PQBO OBPRIQP (FC QEB DL>I TBOB >IDLOFQEJF@
BCCF@FBK@V O>QEBO QE>K ABJLKPQO>QFLK). (BOB FP > Q>?IB LC PLJB OBPRIQP.
Total Population Total Population Mutation Rate Mutation Rate
Number of Number of
Generations until Generations until
Phrase Solved Phrase Solved
Total Time (in Total Time (in
seconds) until Phrase seconds) until Phrase
Solved Solved
150 1% 1089 18.8
300 1% 448 8.2
1,000 1% 71 1.8
50,000 1% 27 4.3
.LQF@B ELT FK@OB>PFKD QEB MLMRI>QFLK PFWB AO>PQF@>IIV OBAR@BP QEB KRJ?BO LC DBKBO>QFLKP
KBBABA QL PLISB CLO QEB MEO>PB. (LTBSBO, FQ ALBPKfQ KB@BPP>OFIV OBAR@B QEB >JLRKQ LC QFJB.
/K@B LRO MLMRI>QFLK ?>IILLKP QL CFCQV QELRP>KA BIBJBKQP, QEB PHBQ@E ORKP PILTIV, DFSBK QEB
@FI;N GON;NCIH/;N? = 0.01;
CHN NIN;F.IJOF;NCIH = 150;
4EB .>QROB LC #LAB (S005)
413
>JLRKQ LC QFJB OBNRFOBA QL MOL@BPP CFQKBPP >KA ?RFIA > J>QFKD MLLI LRQ LC PL J>KV BIBJBKQP.
(4EBOB >OB, LC @LROPB, LMQFJFW>QFLKP QE>Q @LRIA ?B J>AB PELRIA VLR OBNRFOB PR@E > I>ODB
MLMRI>QFLK.)
)K >AAFQFLK QL QEB MLMRI>QFLK PFWB, QEB JRQ>QFLK O>QB @>K DOB>QIV >CCB@Q MBOCLOJ>K@B.
Total Population Total Population Mutation Rate Mutation Rate
Number of Number of
Generations until Generations until
Phrase Solved Phrase Solved
Total Time (in Total Time (in
seconds) until seconds) until
Phrase Solved Phrase Solved
1,000 0% 37 LO KBSBO? 1.2 LO KBSBO?
1,000 1% 71 1.8
1,000 2% 60 1.6
1,000 10% KBSBO? KBSBO?
7FQELRQ >KV JRQ>QFLK >Q >II (0%), VLR GRPQ E>SB QL DBQ IR@HV. )C >II QEB @LOOB@Q @E>O>@QBOP >OB
MOBPBKQ PLJBTEBOB FK PLJB JBJ?BO LC QEB FKFQF>I MLMRI>QFLK, VLRfII BSLISB QEB MEO>PB SBOV
NRF@HIV. )C KLQ, QEBOB FP KL T>V CLO QEB PHBQ@E QL BSBO OB>@E QEB BU>@Q MEO>PB. 2RK FQ > CBT
QFJBP >KA VLRfII PBB ?LQE FKPQ>K@BP. )K >AAFQFLK, LK@B QEB JRQ>QFLK O>QB DBQP EFDE BKLRDE
(10%, CLO BU>JMIB), QEBOB FP PL JR@E O>KALJKBPP FKSLISBA (1 LRQ LC BSBOV 10 IBQQBOP FP
O>KALJ FK B>@E KBT @EFIA) QE>Q QEB PFJRI>QFLK FP MOBQQV JR@E ?>@H QL > O>KALJ QVMFKD
JLKHBV. )K QEBLOV, FQ TFII BSBKQR>IIV PLISB QEB MEO>PB, ?RQ VLR J>V ?B T>FQFKD JR@E, JR@E
ILKDBO QE>K FP OB>PLK>?IB.
Key #2: The fitness function Key #2: The fitness function
0I>VFKD >OLRKA TFQE QEB JRQ>QFLK O>QB LO MLMRI>QFLK QLQ>I FP MOBQQV B>PV >KA FKSLISBP IFQQIB
JLOB QE>K QVMFKD KRJ?BOP FK VLRO PHBQ@E. 4EB OB>I E>OA TLOH LC > ABSBILMFKD > DBKBQF@
>IDLOFQEJ FP FK TOFQFKD > CFQKBPP CRK@QFLK. )C VLR @>KKLQ ABCFKB VLRO MOL?IBJfP DL>IP >KA
BS>IR>QB KRJBOF@>IIV ELT TBII QELPB DL>IP E>SB ?BBK >@EFBSBA, QEBK VLR TFII KLQ E>SB
PR@@BPPCRI BSLIRQFLK FK VLRO PFJRI>QFLK.
"BCLOB TB QEFKH >?LRQ LQEBO P@BK>OFLP TFQE LQEBO CFQKBPP CRK@QFLKP, IBQfP ILLH >Q CI>TP FK LRO
3E>HBPMB>OB>K CFQKBPP CRK@QFLK. #LKPFABO PLISFKD CLO > MEO>PB QE>Q FP KLQ KFKBQBBK
@E>O>@QBOP ILKD, ?RQ LKB QELRP>KA. .LT, IBQfP P>V QEBOB >OB QTL JBJ?BOP LC QEB MLMRI>QFLK,
LKB TFQE 800 @E>O>@QBOP @LOOB@Q >KA LKB TFQE 801. (BOB >OB QEBFO CFQKBPP P@LOBP:
0EO>PB !: 800 @E>O>@QBOP @LOOB@Q CFQKBPP = 80%
0EO>PB ": 801 @E>O>@QBOP @LOOB@Q CFQKBPP = 80.1%
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
414
4EBOB >OB > @LRMIB LC MOL?IBJP EBOB. &FOPQ, TB >OB >AAFKD BIBJBKQP QL QEB J>QFKD MLLI .
KRJ?BOP LC QFJBP, TEBOB . BNR>IP CFQKBPP JRIQFMIFBA ?V LKB ERKAOBA. /?GB@QP @>K LKIV ?B
>AABA QL >K ALL;S*CMN > TELIB KRJ?BO LC QFJBP, >KA PL ! >KA " TFII ?LQE ?B >AABA 80 QFJBP,
DFSFKD QEBJ >K BNR>I MOL?>?FIFQV LC ?BFKD PBIB@QBA. %SBK TFQE >K FJMOLSBA PLIRQFLK QE>Q Q>HBP
CIL>QFKD MLFKQ MOL?>?FIFQFBP FKQL >@@LRKQ, 80.1% FP LKIV > QBBKV QFKV ?FQ EFDEBO QE>K 80%. "RQ
DBQQFKD 801 @E>O>@QBOP OFDEQ FP > TELIB ILQ ?BQQBO QE>K 800 FK QEB BSLIRQFLK>OV P@BK>OFL. 7B
OB>IIV T>KQ QL J>HB QE>Q >AAFQFLK>I @E>O>@QBO @LRKQ. 7B T>KQ QEB CFQKBPP P@LOB CLO 801
@E>O>@QBOP QL ?B BUMLKBKQF>IIV ?BQQBO QE>K QEB P@LOB CLO 800.
4L MRQ FQ >KLQEBO T>V, IBQfP DO>ME QEB CFQKBPP CRK@QFLK.
4EFP FP > IFKB>O DO>ME; >P QEB KRJ?BO LC @E>O>@QBOP DLBP RM, PL ALBP QEB CFQKBPP P@LOB.
(LTBSBO, TE>Q FC QEB CFQKBPP FK@OB>PBA BUMLKBKQF>IIV >P QEB KRJ?BO LC @LOOB@Q @E>O>@QBOP
FK@OB>PBA? /RO DO>ME @LRIA QEBK ILLH PLJBQEFKD IFHB:
4EB JLOB @LOOB@Q @E>O>@QBOP, QEB BSBK DOB>QBO QEB CFQKBPP. 7B @>K >@EFBSB QEFP QVMB LC OBPRIQ
FK > KRJ?BO LC AFCCBOBKQ T>VP. &LO BU>JMIB, TB @LRIA P>V:
<?JD;II = (DKC8;H E< 9EHH;9J 9>7H79J;HI) * (DKC8;H E< 9EHH;9J 9>7H79J;HI)
,BQfP P>V TB E>SB QTL JBJ?BOP LC QEB MLMRI>QFLK, LKB TFQE CFSB @LOOB@Q @E>O>@QBOP >KA LKB
TFQE PFU. 4EB KRJ?BO 6 FP > 20% FK@OB>PB LSBO QEB KRJ?BO 5. ,BQfP ILLH >Q QEB CFQKBPP P@LOBP
PNR>OBA.
F64B?2 9.8
F64B?2 9.9
4EB .>QROB LC #LAB (S005)
415
Characters correct Characters correct Fitness Fitness
5 25
6 36
4EB CFQKBPP P@LOBP FK@OB>PB BUMLKBKQF>IIV OBI>QFSB QL QEB KRJ?BO LC @LOOB@Q @E>O>@QBOP. 36 FP
> 44% FK@OB>PB LSBO 25.
(BOBfP >KLQEBO CLOJRI>.
<?JD;II = 2
(DKC8;H E< 9EHH;9J 9>7H79J;HI)
Characters correct Characters correct Fitness Fitness
1 2
2 4
3 8
4 16
(BOB, QEB CFQKBPP P@LOBP FK@OB>PB >Q > C>PQBO O>QB, ALR?IFKD TFQE B>@E >AAFQFLK>I @LOOB@Q
@E>O>@QBO.
7EFIB QEFP O>QEBO PMB@FCF@ AFP@RPPFLK LC BUMLKBKQF>I SP. IFKB>O CFQKBPP CRK@QFLKP FP >K
FJMLOQ>KQ ABQ>FI FK QEB ABPFDK LC > DLLA CFQKBPP CRK@QFLK, ) ALKfQ T>KQ RP QL JFPP QEB JLOB
FJMLOQ>KQ MLFKQ EBOB: 9G=;B MCIF CKB :=HB9GG :IB7H=CB! ) PBOFLRPIV ALR?Q QE>Q >KV MOLGB@Q
VLR RKABOQ>HB FK 0OL@BPPFKD TFQE DBKBQF@ >IDLOFQEJP TFII >@QR>IIV FKSLISB @LRKQFKD QEB
@LOOB@Q KRJ?BO LC @E>O>@QBOP FK > PQOFKD. )K QEB @LKQBUQ LC QEFP ?LLH, FQfP JLOB IFHBIV VLR TFII
?B ILLHFKD QL BSLISB > @OB>QROB QE>Q FP M>OQ LC > MEVPF@P PVPQBJ. 0BOE>MP VLR >OB ILLHFKD QL
LMQFJFWB QEB TBFDEQP LC PQBBOFKD ?BE>SFLOP PL > @OB>QROB @>K ?BPQ BP@>MB > MOBA>QLO LO
>SLFA >K L?PQ>@IB LO J>HB FQ QEOLRDE > J>WB. 9LR E>SB QL >PH VLROPBIC TE>Q VLRfOB ELMFKD
QL BS>IR>QB.
2BTOFQB QEB CFQKBPP CRK@QFLK QL FK@OB>PB BUMLKBKQF>IIV >@@LOAFKD QL QEB KRJ?BO LC
@LOOB@Q @E>O>@QBOP. .LQB QE>Q VLR TFII >IPL E>SB QL KLOJ>IFWB QEB CFQKBPP S>IRBP QL >
O>KDB ?BQTBBK 0 >KA 1 PL QEBV @>K ?B >AABA QL QEB J>QFKD MLLI > OB>PLK>?IB
KRJ?BO LC QFJBP.
Exercise 9.7 Exercise 9.7
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
416
,BQfP @LKPFABO > O>@FKD PFJRI>QFLK FK TEF@E > SBEF@IB FP BSLISFKD > ABPFDK LMQFJFWBA CLO PMBBA.
<?JD;II = JEJ7B DKC8;H E< <H7C;I H;GK?H;: <EH L;>?9B; JE H;79> J7H=;J
(LT >?LRQ > @>KKLK QE>Q FP BSLISFKD QEB LMQFJ>I T>V QL PELLQ > Q>ODBQ?
<?JD;II = 97DDED87BB :?IJ7D9; JE J7H=;J
4EB ABPFDK LC @LJMRQBO-@LKQOLIIBA MI>VBOP FK > D>JB FP >IPL > @LJJLK P@BK>OFL. ,BQfP P>V VLR
>OB MOLDO>JJFKD > PL@@BO D>JB FK TEF@E QEB RPBO FP QEB DL>IFB. 4EB OBPQ LC QEB MI>VBOP >OB
@LKQOLIIBA ?V VLRO MOLDO>J >KA E>SB > PBQ LC M>O>JBQBOP QE>Q ABQBOJFKB ELT QEBV HF@H > ?>II
QLT>OAP QEB DL>I. 7E>Q TLRIA QEB CFQKBPP P@LOB CLO >KV DFSBK MI>VBO ?B?
<?JD;II = JEJ7B =E7BI I9EH;:
4EFP, L?SFLRPIV, FP > PFJMIFPQF@ Q>HB LK QEB D>JB LC PL@@BO, ?RQ FQ FIIRPQO>QBP QEB MLFKQ. 4EB JLOB
DL>IP > MI>VBO P@LOBP, QEB EFDEBO FQP CFQKBPP, >KA QEB JLOB IFHBIV FQP DBKBQF@ FKCLOJ>QFLK TFII
>MMB>O FK QEB KBUQ D>JB. %SBK TFQE > CFQKBPP CRK@QFLK >P PFJMIB >P QEB LKB ABP@OF?BA EBOB,
QEFP P@BK>OFL FP ABJLKPQO>QFKD PLJBQEFKD SBOV MLTBOCRIdQEB >A>MQ>?FIFQV LC > PVPQBJ. )C QEB
MI>VBOP @LKQFKRB QL BSLISB COLJ D>JB QL D>JB QL D>JB, TEBK > KBT <IA5B RPBO BKQBOP QEB
D>JB TFQE > @LJMIBQBIV AFCCBOBKQ PQO>QBDV, QEB PVPQBJ TFII NRF@HIV AFP@LSBO QE>Q QEB CFQKBPP
P@LOBP >OB DLFKD ALTK >KA BSLISB > KBT LMQFJ>I PQO>QBDV. )Q TFII >A>MQ. ($LKfQ TLOOV, QEBOB FP
SBOV IFQQIB A>KDBO FK QEFP OBPRIQFKD FK PBKQFBKQ OL?LQP QE>Q TFII BKPI>SB >II ERJ>KP.)
)K QEB BKA, FC VLR AL KLQ E>SB > CFQKBPP CRK@QFLK QE>Q BCCB@QFSBIV BS>IR>QBP QEB MBOCLOJ>K@B LC
QEB FKAFSFAR>I BIBJBKQP LC VLRO MLMRI>QFLK, VLR TFII KLQ E>SB >KV BSLIRQFLK. !KA QEB CFQKBPP
CRK@QFLK COLJ LKB BU>JMIB TFII IFHBIV KLQ >MMIV QL > QLQ>IIV AFCCBOBKQ MOLGB@Q. 3L QEFP FP QEB M>OQ
TEBOB VLR DBQ QL PEFKB. 9LR E>SB QL ABPFDK > CRK@QFLK, PLJBQFJBP COLJ P@O>Q@E, QE>Q TLOHP CLO
VLRO M>OQF@RI>O MOLGB@Q. !KA TEBOB AL VLR AL QEFP? !II VLR E>SB QL BAFQ >OB QELPB CBT IFKBP LC
@LAB FKPFAB QEB CRK@QFLK QE>Q @LJMRQBP QEB CFQKBPP S>OF>?IB.
PIC> @CNH?MM() U
????????????
????????????
@CNH?MM = ??????????
W
Key #3: Genotype and Phenotype Key #3: Genotype and Phenotype
4EB CFK>I HBV QL ABPFDKFKD VLRO LTK DBKBQF@ >IDLOFQEJ OBI>QBP QL ELT VLR @ELLPB QL BK@LAB
QEB MOLMBOQFBP LC VLRO PVPQBJ. 7E>Q >OB VLR QOVFKD QL BUMOBPP, >KA ELT @>K VLR QO>KPI>QB QE>Q
BUMOBPPFLK FKQL > ?RK@E LC KRJ?BOP? 7E>Q FP QEB DBKLQVMB >KA MEBKLQVMB?
7EBK Q>IHFKD >?LRQ QEB CFQKBPP CRK@QFLK, TB E>MMFIV >PPRJBA TB @LRIA @OB>QB @LJMRQBO-
@LKQOLIIBA HF@HBOP QE>Q B>@E E>A > gPBQ LC M>O>JBQBOP QE>Q ABQBOJFKB ELT QEBV HF@H > ?>II
QLT>OAP QEB DL>I.h (LTBSBO, TE>Q QELPB M>O>JBQBOP >OB >KA ELT VLR @ELLPB QL BK@LAB QEBJ
FP RM QL VLR.
4EB .>QROB LC #LAB (S005)
417
7B PQ>OQBA TFQE QEB 3E>HBPMB>OB BU>JMIB ?B@>RPB LC ELT B>PV FQ T>P QL ABPFDK ?LQE QEB
DBKLQVMB (>K >OO>V LC @E>O>@QBOP) >KA FQP BUMOBPPFLK, QEB MEBKLQVMB (QEB PQOFKD AO>TK FK QEB
TFKALT).
4EB DLLA KBTP FPd>KA TB EFKQBA >Q QEFP >Q QEB PQ>OQ LC QEFP @E>MQBOdVLRfSB OB>IIV ?BBK
ALFKD QEFP >II >ILKD. !KVQFJB VLR TOFQB > @I>PP FK 0OL@BPPFKD, VLR J>HB > TELIB ?RK@E LC
S>OF>?IBP.
!II TB KBBA QL AL QL BSLISB QELPB M>O>JBQBOP FP QL QROK QEBJ FKQL >K >OO>V, PL QE>Q QEB >OO>V
@>K ?B RPBA TFQE >II LC QEB CRK@QFLKPd=LIMMIP?L(), GON;N?(), BQ@.dCLRKA FK QEB ",A @I>PP.
/KB @LJJLK PLIRQFLK FP QL RPB >K >OO>V LC CIL>QFKD MLFKQ KRJ?BOP ?BQTBBK 0 >KA 1.
.LQF@B ELT TBfSB KLT MRQ QEB DBKBQF@ A>Q> (DBKLQVMB) >KA FQP BUMOBPPFLK (MEBKLQVMB) FKQL
QTL PBM>O>QB @I>PPBP. 4EB ",A @I>PP FP QEB DBKLQVMB >KA QEB 6BEF@IB @I>PP RPBP > ",A L?GB@Q
QL AOFSB FQP ?BE>SFLOP >KA BUMOBPP QE>Q A>Q> SFPR>IIVdFQ FP QEB MEBKLQVMB. 4EB QTL @>K ?B
IFKHBA ?V @OB>QFKD > ",A FKPQ>K@B FKPFAB QEB SBEF@IB @I>PP FQPBIC.
=F;MM 3?BC=F? U
@FI;N G;RMJ??>;
@FI;N G;R@IL=?;
@FI;N MCT?;
@FI;N M?J;L;NCIH4?CABN;
// ?N=.
=F;MM ",A U
An array of floats @FI;N89 A?H?M;
",A(CHN HOG) U
A?H?M = H?Q @FI;N8HOG9;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
Always pick a number between 0 and 1. A?H?M8C9 = @FI;N(1);
W
W
=F;MM 3?BC=F? U
A DNA object embedded into the Vehicle
class
",A >H;;
@FI;N G;RMJ??>;
@FI;N G;R@IL=?;
@FI;N MCT?;
@FI;N M?J;L;NCIH4?CABN;
Etc.
3?BC=F?() U
",A = H?Q ",A(4);
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
418
/C @LROPB, VLR JLPQ IFHBIV ALKfQ T>KQ >II VLRO S>OF>?IBP QL E>SB > O>KDB ?BQTBBK 0 >KA 1. "RQ
O>QEBO QE>K QOV QL OBJBJ?BO ELT QL >AGRPQ QELPB O>KDBP FK QEB ",A @I>PP FQPBIC, FQfP B>PFBO QL
MRII QEB DBKBQF@ FKCLOJ>QFLK COLJ QEB $.! L?GB@Q >KA RPB 0OL@BPPFKDfP G;J() CRK@QFLK QL
@E>KDB QEB O>KDB. &LO BU>JMIB, FC VLR T>KQ > PFWB S>OF>?IB ?BQTBBK 10 >KA 72, VLR TLRIA P>V:
)K LQEBO @>PBP, VLR TFII T>KQ QL ABPFDK > DBKLQVMB QE>Q FP >K >OO>V LC L?GB@QP. #LKPFABO QEB
ABPFDK LC > OL@HBQ TFQE > PBOFBP LC gQEORPQBOh BKDFKBP. 9LR @LRIA ABP@OF?B B>@E QEORPQBO TFQE >
.3?=NIL QE>Q LRQIFKBP FQP AFOB@QFLK >KA OBI>QFSB PQOBKDQE.
4EB MEBKLQVMB TLRIA ?B > /I=E?N @I>PP QE>Q M>OQF@FM>QBP FK > MEVPF@P PVPQBJ.
7E>QfP DOB>Q >?LRQ QEFP QB@EKFNRB LC AFSFAFKD QEB DBKLQVMB >KA MEBKLQVMB FKQL PBM>O>QB
@I>PPBP (",A >KA /I=E?N CLO BU>JMIB) FP QE>Q TEBK FQ @LJBP QFJB QL ?RFIA >II LC QEB @LAB, VLRfII
KLQF@B QE>Q QEB $.! @I>PP TB ABSBILMBA B>OIFBO OBJ>FKP FKQ>@Q. 4EB LKIV QEFKD QE>Q @E>KDBP FP
QEB >OO>VfP A>Q> QVMB (@FI;N, .3?=NIL, BQ@.) >KA QEB BUMOBPPFLK LC QE>Q A>Q> FK QEB MEBKLQVMB
@I>PP.
)K QEB KBUQ PB@QFLK, TBfII CLIILT QEFP FAB> > ?FQ CROQEBO >KA T>IH QEOLRDE QEB KB@BPP>OV PQBMP CLO
>K BU>JMIB QE>Q FKSLISBP JLSFKD ?LAFBP >KA >K >OO>V LC .3?=NILP >P $.!.
Using the genes to set variables G;RMJ??> = >H;.A?H?M809;
G;R@IL=? = >H;.A?H?M819;
MCT? = >H;.A?H?M829;
M?J;L;NCIH4?CABN = >H;.A?H?M839;
Etc. W
MCT? = G;J(>H;.A?H?M829,0,1,10,72);
=F;MM ",A U
The genotype is an array of PVectors. .3?=NIL89 A?H?M;
",A(CHN HOG) U
A?H?M = H?Q @FI;N8HOG9;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
A PVector pointing in a random direction A?H?M8C9 = .3?=NIL.L;H>IG2"();
And scaled randomly A?H?M8C9.GOFN(L;H>IG(10));
W
W
=F;MM /I=E?N U
",A >H;;
// ?N=.
4EB .>QROB LC #LAB (S005)
419
9.10 Evolving Forces: Smart Rockets 9.10 Evolving Forces: Smart Rockets
7B MF@HBA QEB OL@HBQ FAB> CLO > PMB@FCF@ OB>PLK. )K 2009, *BO 4ELOM (EQQM://?IMOKQ.@LJ)
OBIB>PBA > DBKBQF@ >IDLOFQEJP BU>JMIB LK EFP ?ILD BKQFQIBA g3J>OQ 2L@HBQP.h *BO MLFKQP LRQ
QE>Q .!3! RPBP BSLIRQFLK>OV @LJMRQFKD QB@EKFNRBP QL PLISB >II PLOQP LC MOL?IBJP, COLJ
P>QBIIFQB >KQBKK> ABPFDK QL OL@HBQ CFOFKD M>QQBOKP. 4EFP FKPMFOBA EFJ QL @OB>QB > &I>PE
ABJLKPQO>QFLK LC BSLISFKD OL@HBQP. (BOB FP > ABP@OFMQFLK LC QEB P@BK>OFL:
! MLMRI>QFLK LC OL@HBQP I>RK@EBP COLJ QEB ?LQQLJ LC QEB P@OBBK TFQE QEB DL>I LC EFQQFKD >
Q>ODBQ >Q QEB QLM LC QEB P@OBBK (TFQE L?PQ>@IBP ?IL@HFKD > PQO>FDEQ IFKB M>QE).
%>@E OL@HBQ FP BNRFMMBA TFQE CFSB QEORPQBOP
LC S>OF>?IB PQOBKDQE >KA AFOB@QFLK. 4EB
QEORPQBOP ALKfQ CFOB >II >Q LK@B >KA
@LKQFKRLRPIV; O>QEBO, QEBV CFOB LKB >Q > QFJB
FK > @RPQLJ PBNRBK@B.
)K QEFP PB@QFLK, TBfOB DLFKD QL BSLISB LRO
LTK PFJMIFCFBA 3J>OQ 2L@HBQP, FKPMFOBA ?V
*BO 4ELOMfP. 7EBK TB DBQ QL QEB BKA LC QEB
PB@QFLK, TBfII IB>SB FJMIBJBKQFKD PLJB LC
*BOfP >AAFQFLK>I >AS>K@BA CB>QROBP >P >K
BUBO@FPB.
/RO OL@HBQP TFII E>SB LKIV LKB QEORPQBO, >KA QEFP QEORPQBO TFII ?B >?IB QL CFOB FK >KV AFOB@QFLK
TFQE >KV PQOBKDQE CLO BSBOV CO>JB LC >KFJ>QFLK. 4EFP FPKfQ M>OQF@RI>OIV OB>IFPQF@, ?RQ FQ TFII
J>HB ?RFIAFKD LRQ QEB CO>JBTLOH > IFQQIB B>PFBO. (7B @>K >IT>VP J>HB QEB OL@HBQ >KA FQP
QEORPQBOP JLOB >AS>K@BA >KA OB>IFPQF@ I>QBO.)
,BQfP PQ>OQ ?V Q>HFKD LRO ?>PF@ +IP?L @I>PP COLJ #E>MQBO 2 BU>JMIBP >KA OBK>JFKD FQ /I=E?N.
F64B?2 9.10
F64B?2 9.11
=F;MM /I=E?N U
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
420
5PFKD QEB >?LSB CO>JBTLOH, TB @>K FJMIBJBKQ LRO PJ>OQ OL@HBQ ?V P>VFKD QE>Q CLO BSBOV CO>JB
LC >KFJ>QFLK, TB @>II ;JJFS$IL=?() TFQE > KBT CLO@B. 4EB gQEORPQBOh >MMIFBP > PFKDIB CLO@B QL
QEB OL@HBQ B>@E QFJB QEOLRDE >L;Q().
#LKPFABOFKD QEFP BU>JMIB, IBQfP DL QEOLRDE QEB QEOBB HBVP QL MOLDO>JJFKD LRO LTK @RPQLJ
DBKBQF@ >IDLOFQEJ BU>JMIB >P LRQIFKBA FK QEB MOBSFLRP PB@QFLK.
Key #1: Population size and mutation rate Key #1: Population size and mutation rate
7B @>K >@QR>IIV ELIA LCC LK QEFP CFOPQ HBV CLO QEB JLJBKQ. /RO PQO>QBDV TFII ?B QL MF@H PLJB
OB>PLK>?IB KRJ?BOP (> MLMRI>QFLK LC 100 OL@HBQP, JRQ>QFLK O>QB LC 1%) >KA ?RFIA LRQ QEB
PVPQBJ, MI>VFKD TFQE QEBPB KRJ?BOP LK@B TB E>SB LRO PHBQ@E RM >KA ORKKFKD.
Key #2: The fitness function Key #2: The fitness function
7B PQ>QBA QEB DL>I LC > OL@HBQ OB>@EFKD > Q>ODBQ. )K LQEBO TLOAP, QEB @ILPBO > OL@HBQ DBQP QL
QEB Q>ODBQ, QEB EFDEBO QEB CFQKBPP. &FQKBPP FP FKSBOPBIV MOLMLOQFLK>I QL AFPQ>K@B: QEB PJ>IIBO QEB
AFPQ>K@B, QEB DOB>QBO QEB CFQKBPP; QEB DOB>QBO QEB AFPQ>K@B, QEB PJ>IIBO QEB CFQKBPP.
,BQfP >PPRJB TB E>SB > .3?=NIL Q>ODBQ.
4EFP FP MBOE>MP QEB PFJMIBPQ CFQKBPP CRK@QFLK TB @LRIA TOFQB. "V RPFKD LKB AFSFABA ?V AFPQ>K@B,
I>ODB AFPQ>K@BP ?B@LJB PJ>II KRJ?BOP >KA PJ>II AFPQ>K@BP ?B@LJB I>ODB.
A rocket has three vectors: location,
velocity, acceleration.
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
Accumulating forces into acceleration
(Newtons 2nd law)
PIC> ;JJFS$IL=?(.3?=NIL @) U
;==?F?L;NCIH.;>>(@);
W
Our simple physics model (Euler integration) PIC> OJ>;N?() U
Velocity changes according to acceleration. P?FI=CNS.;>>(;==?F?L;NCIH);
Location changes according to velocity. FI=;NCIH.;>>(P?FI=CNS);
;==?F?L;NCIH.GOFN(0);
W
W
PIC> @CNH?MM() U
How close did we get? @FI;N > = .3?=NIL.>CMN(FI=;NCIH,N;LA?N);
Fitness is inversely proportional to distance. @CNH?MM = 1/>;
W
4EB .>QROB LC #LAB (S005)
421
distance distance 1 / distance 1 / distance
300 1 / 300 = 0.0033
100 1 / 100 = 0.01
5 1 / 5 = 0.2
1 1 / 1 = 1.0
0.1 1 / 0.1 = 10
!KA FC TB T>KQBA QL RPB LRO BUMLKBKQF>I QOF@H COLJ QEB MOBSFLRP PB@QFLK, TB @LRIA RPB LKB
AFSFABA ?V AFPQ>K@B PNR>OBA.
distance distance 1 / distance 1 / distance
(1 / distance) (1 / distance)
22
300 1 / 400 = 0.0025 0.00000625
100 1 / 100 = 0.01 0.0001
5 1 / 5 = 0.2 0.04
1 1 / 1 = 1.0 1.0
0.1 1 / 0.1 = 10 100
4EBOB >OB PBSBO>I >AAFQFLK>I FJMOLSBJBKQP TBfII T>KQ QL J>HB QL QEB CFQKBPP CRK@QFLK, ?RQ
QEFP PFJMIB LKB FP > DLLA PQ>OQ.
+BV #3: 'BKLQVMB >KA 0EBKLQVMB
7B PQ>QBA QE>Q B>@E OL@HBQ E>P > QEORPQBO QE>Q CFOBP FK > S>OF>?IB AFOB@QFLK TFQE > S>OF>?IB
J>DKFQRAB FK B>@E CO>JB. !KA PL TB KBBA > .3?=NIL CLO B>@E CO>JB LC >KFJ>QFLK. /RO
DBKLQVMB, QEB A>Q> OBNRFOBA QL BK@LAB QEB OL@HBQfP ?BE>SFLO, FP QEBOBCLOB >K >OO>V LC
.3?=NILP.
PIC> @CNH?MM() U
@FI;N > = .3?=NIL.>CMN(FI=;NCIH,N;LA?N);
Squaring 1 divided by distance @CNH?MM = JIQ(1/>,2);
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
422
4EB E>MMV KBTP EBOB FP QE>Q TB ALKfQ OB>IIV E>SB QL AL >KVQEFKD BIPB QL QEB ",A @I>PP. !II LC
QEB CRK@QFLK>IFQV TB ABSBILMBA CLO QEB QVMFKD JLKHBV (@OLPPLSBO >KA JRQ>QFLK) >MMIFBP EBOB.
4EB LKB AFCCBOBK@B TB AL E>SB QL @LKPFABO FP ELT TB FKFQF>IFWB QEB >OO>V LC DBKBP. 7FQE QEB
QVMFKD JLKHBV, TB E>A >K >OO>V LC @E>O>@QBOP >KA MF@HBA > O>KALJ @E>O>@QBO CLO B>@E
BIBJBKQ LC QEB >OO>V. (BOB TBfII AL BU>@QIV QEB P>JB QEFKD >KA FKFQF>IFWB > $.! PBNRBK@B >P >K
>OO>V LC O>KALJ .3?=NILP. .LT, VLRO FKPQFK@Q FK @OB>QFKD > O>KALJ .3?=NIL JFDEQ ?B >P
CLIILTP:
4EFP FP MBOCB@QIV CFKB >KA TFII IFHBIV AL QEB
QOF@H. (LTBSBO, FC TB TBOB QL AO>T BSBOV
PFKDIB MLPPF?IB SB@QLO TB JFDEQ MF@H, QEB
OBPRIQ TLRIA CFII > PNR>OB (PBB &FDROB 9.12). )K
QEFP @>PB, FQ MOL?>?IV ALBPKfQ J>QQBO, ?RQ
QEBOB FP > PIFDEQ ?F>P QL AF>DLK>IP EBOB DFSBK
QE>Q > .3?=NIL COLJ QEB @BKQBO LC > PNR>OB
QL > @LOKBO FP ILKDBO QE>K > MROBIV SBOQF@>I LO
ELOFWLKQ>I LKB.
7E>Q TLRIA ?B ?BQQBO EBOB FP QL MF@H >
O>KALJ >KDIB >KA J>HB > .3?=NIL LC IBKDQE
LKB COLJ QE>Q >KDIB, DFSFKD RP > @FO@IB (PBB
&FDROB 9.13). 4EFP @LRIA ?B B>PFIV ALKB TFQE
> NRF@H MLI>O QL #>OQBPF>K @LKSBOPFLK (PBB
M>DB 112), ?RQ > NRF@HBO M>QE QL QEB OBPRIQ FP
GRPQ QL RPB .3?=NIL'P L;H>IG2"().
! .3?=NIL LC IBKDQE LKB FP >@QR>IIV DLFKD QL ?B NRFQB > I>ODB CLO@B. 2BJBJ?BO, CLO@BP >OB
>MMIFBA QL >@@BIBO>QFLK, TEF@E >@@RJRI>QBP FKQL SBIL@FQV QEFOQV QFJBP MBO PB@LKA. 3L, CLO QEFP
BU>JMIB, TB @>K >IPL >AA LKB JLOB S>OF>?IB QL QEB ",A @I>PP: > J>UFJRJ CLO@B QE>Q P@>IBP >II
QEB .3?=NILP. 4EFP TFII @LKQOLI QEB QEORPQBO MLTBO.
=F;MM ",A U
.3?=NIL89 A?H?M;
.3?=NIL P = H?Q .3?=NIL(L;H>IG(-1,1),L;H>IG(-1,1));
F64B?2 9.12
F64B?2 9.13
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
Making a PVector from a random angle A?H?M8C9 = .3?=NIL.L;H>IG2"();
W
4EB .>QROB LC #LAB (S005)
423
.LQF@B >IPL QE>Q TB @OB>QBA >K >OO>V LC .3?=NILP TFQE IBKDQE IFCBQFJB. 7B KBBA > .3?=NIL
CLO B>@E CO>JB LC QEB OL@HBQfP IFCB, >KA QEB >?LSB >PPRJBP QEB BUFPQBK@B LC > DIL?>I S>OF>?IB
IFCBQFJB QE>Q PQLOBP QEB QLQ>I KRJ?BO LC CO>JBP FK B>@E DBKBO>QFLKfP IFCB @V@IB.
4EB BUMOBPPFLK LC QEFP >OO>V LC .3?=NILP, QEB MEBKLQVMB, FP > /I=E?N @I>PP JLABIBA LK LRO
?>PF@ .3?=NIL >KA CLO@BP BU>JMIBP COLJ #E>MQBO 2. !II TB KBBA QL AL FP >AA >K FKPQ>K@B LC
> ",A L?GB@Q QL QEB @I>PP. 4EB CFQKBPP S>OF>?IB TFII >IPL IFSB EBOB. /KIV QEB /I=E?N L?GB@Q
HKLTP ELT QL @LJMRQB FQP AFPQ>K@B QL QEB Q>ODBQ, >KA QEBOBCLOB QEB CFQKBPP CRK@QFLK TFII IFSB
EBOB FK QEB MEBKLQVMB >P TBII.
7E>Q >OB TB RPFKD QEB $.! CLO? 7B >OB J>O@EFKD QEOLRDE QEB >OO>V LC .3?=NILP >KA
>MMIVFKD QEBJ LKB >Q > QFJB >P > CLO@B QL QEB OL@HBQ. 4L AL QEFP, TBfII >IPL E>SB QL >AA >K
FKQBDBO QE>Q >@QP >P > @LRKQBO QL T>IH QEOLRDE QEB >OO>V.
=F;MM ",A U
The genetic sequence is an array of
PVectors.
.3?=NIL89 A?H?M;
How strong can the thrusters be? @FI;N G;R@IL=? = 0.1;
",A() U
We need a PVector for every frame of the
rockets life.
A?H?M = H?Q .3?=NIL8FC@?NCG?9;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
A?H?M8C9 = .3?=NIL.L;H>IG2"();
Scaling the PVectors randomly, but no
stronger than maximum force
A?H?M8C9.GOFN(L;H>IG(0, G;R@IL=?));
W
W
=F;MM /I=E?N U
A Rocket has DNA. ",A >H;;
A Rocket has fitness. @FI;N @CNH?MM;
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
CHN A?H?!IOHN?L = 0;
PIC> LOH() U
Apply a force from the genes array. ;JJFS$IL=?(>H;.A?H?M8A?H?!IOHN?L9);
Go to the next force in the genes array. A?H?!IOHN?L++;
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
424
Update the Rockets physics. OJ>;N?();
W
9.11 Smart Rockets: Putting It All Together 9.11 Smart Rockets: Putting It All Together
7B KLT E>SB LRO ",A @I>PP (DBKLQVMB) >KA LRO /I=E?N @I>PP (MEBKLQVMB). 4EB I>PQ MFB@B LC
QEB MRWWIB FP > .IJOF;NCIH @I>PP, TEF@E J>K>DBP >K >OO>V LC OL@HBQP >KA E>P QEB CRK@QFLK>IFQV
CLO PBIB@QFLK >KA OBMOLAR@QFLK. !D>FK, QEB E>MMV KBTP EBOB FP QE>Q TB ?>OBIV E>SB QL @E>KDB
>KVQEFKD COLJ QEB 3E>HBPMB>OB JLKHBV BU>JMIB. 4EB MOL@BPP CLO ?RFIAFKD > J>QFKD MLLI >KA
DBKBO>QFKD > KBT >OO>V LC @EFIA OL@HBQP FP BU>@QIV QEB P>JB >P TE>Q TB AFA TFQE LRO MLMRI>QFLK
LC PQOFKDP.
4EBOB FP LKB C>FOIV PFDKFCF@>KQ @E>KDB, ELTBSBO. 7FQE QVMFKD JLKHBVP, > O>KALJ MEO>PB T>P
BS>IR>QBA >P PLLK >P FQ T>P @OB>QBA. 4EB PQOFKD LC @E>O>@QBOP E>A KL IFCBPM>K; FQ BUFPQBA MROBIV
CLO QEB MROMLPB LC @>I@RI>QFKD FQP CFQKBPP >KA QEBK TB JLSBA LK. 4EB OL@HBQP, ELTBSBO, KBBA
QL IFSB CLO > MBOFLA LC QFJB ?BCLOB QEBV @>K ?B BS>IR>QBA; QEBV KBBA QL ?B DFSBK > @E>K@B QL
J>HB QEBFO >QQBJMQ >Q OB>@EFKD QEB Q>ODBQ. 4EBOBCLOB, TB KBBA QL >AA LKB JLOB CRK@QFLK QL QEB
.IJOF;NCIH @I>PP QE>Q ORKP QEB MEVPF@P PFJRI>QFLK FQPBIC. 4EFP FP FABKQF@>I QL TE>Q TB AFA FK QEB
LOH() CRK@QFLK LC > M>OQF@IB PVPQBJdRMA>QB >II QEB M>OQF@IB IL@>QFLKP >KA AO>T QEBJ.
&FK>IIV, TBfOB OB>AV CLO M?NOJ() >KA >L;Q(). (BOB FK QEB J>FK Q>?, LRO MOFJ>OV OBPMLKPF?FIFQV FP
QL FJMIBJBKQ QEB PQBMP LC QEB DBKBQF@ >IDLOFQEJ FK QEB >MMOLMOF>QB LOABO ?V @>IIFKD QEB
CRK@QFLKP FK QEB .IJOF;NCIH @I>PP.
=F;MM .IJOF;NCIH U
Population has variables to keep track of
mutation rate, current population array,
mating pool, and number of generations.
@FI;N GON;NCIH/;N?;
/I=E?N89 JIJOF;NCIH;
ALL;S*CMN</I=E?N> G;NCHA.IIF;
CHN A?H?L;NCIHM;
These functions havent changed, so no
need to go through the code again.
PIC> @CNH?MM() UW
PIC> M?F?=NCIH() UW
PIC> L?JLI>O=NCIH() UW
PIC> FCP? () U
@IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
The run function takes care of the forces,
updating the rockets location, and
displaying it.
JIJOF;NCIH8C9.LOH();
W
W
4EB .>QROB LC #LAB (S005)
425
(LTBSBO, RKIFHB QEB 3E>HBPMB>OB BU>JMIB, TB ALKfQ T>KQ QL AL QEFP BSBOV CO>JB. 2>QEBO,
LRO PQBMP TLOH >P CLIILTP:
1. #OB>QB > MLMRI>QFLK LC OL@HBQP
2. ,BQ QEB OL@HBQP IFSB CLO . CO>JBP
3. %SLISB QEB KBUQ DBKBO>QFLK
3BIB@QFLK
2BMOLAR@QFLK
4. 2BQROK QL 3QBM #2
Example 9.2: Simple Smart Rockets
JIJOF;NCIH.@CNH?MM();
JIJOF;NCIH.M?F?=NCIH();
JIJOF;NCIH.L?JLI>O=NCIH();
How many frames does a generation live
for?
CHN FC@?NCG?;
What frame are we on? CHN FC@?!IOHN?L;
The population .IJOF;NCIH JIJOF;NCIH;
PIC> M?NOJ() U
MCT?(640, 480);
FC@?NCG? = 500;
FC@?!IOHN?L = 0;
@FI;N GON;NCIH/;N? = 0.01;
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
426
4EB >?LSB BU>JMIB TLOHP, ?RQ FQ FPKfQ M>OQF@RI>OIV FKQBOBPQFKD. !CQBO >II, QEB OL@HBQP PFJMIV
BSLISB QL E>SFKD $.! TFQE > ?RK@E LC SB@QLOP QE>Q MLFKQ PQO>FDEQ RMT>OAP. )K QEB KBUQ
BU>JMIB, TBfOB DLFKD QL Q>IH QEOLRDE QTL PRDDBPQBA FJMOLSBJBKQP CLO QEB BU>JMIB >KA
MOLSFAB @LAB PKFMMBQP QE>Q FJMIBJBKQ QEBPB FJMOLSBJBKQP.
)JMOLSBJBKQ #1: /?PQ>@IBP
!AAFKD L?PQ>@IBP QE>Q QEB OL@HBQP JRPQ >SLFA TFII J>HB QEB PVPQBJ JLOB @LJMIBU >KA
ABJLKPQO>QB QEB MLTBO LC QEB BSLIRQFLK>OV >IDLOFQEJ JLOB BCCB@QFSBIV. 7B @>K J>HB
OB@Q>KDRI>O, PQ>QFLK>OV L?PQ>@IBP C>FOIV B>PFIV ?V @OB>QFKD > @I>PP QE>Q PQLOBP > IL@>QFLK >KA
AFJBKPFLKP.
Step 1: Create the population. Here is where
we could play with the mutation rate and
population size.
JIJOF;NCIH = H?Q .IJOF;NCIH(GON;NCIH/;N?, 50);
W
PIC> >L;Q() U
<;=EALIOH>(255);
The revised genetic algorithm C@ (FC@?!IOHN?L < FC@?NCG?) U
Step 2: The rockets live their life until
lifeCounter reaches lifetime.
JIJOF;NCIH.FCP?();
FC@?!IOHN?L++;
W ?FM? U
When lifetime is reached, reset lifeCounter
and evolve the next generation (Steps 3 and
4, selection and reproduction).
FC@?!IOHN?L = 0;
JIJOF;NCIH.@CNH?MM();
JIJOF;NCIH.M?F?=NCIH();
JIJOF;NCIH.L?JLI>O=NCIH();
W
W
4EB .>QROB LC #LAB (S005)
427
Example 9.3: Smart Rockets
7B @>K >IPL TOFQB > =IHN;CHM() CRK@QFLK QE>Q TFII L?NOLH NLO? LO L?NOLH @;FM? QL
ABQBOJFKB FC > OL@HBQ E>P EFQ QEB L?PQ>@IB.
!PPRJFKD TB J>HB >K ALL;S*CMN LC L?PQ>@IBP, TB @>K QEBK E>SB B>@E OL@HBQ @EB@H QL PBB
FC FQ E>P @LIIFABA TFQE >K L?PQ>@IB >KA PBQ > <IIF?;H CI>D QL ?B QORB FC FQ ALBP, >AAFKD >
CRK@QFLK QL QEB OL@HBQ @I>PP.
)C QEB OL@HBQ EFQP >K L?PQ>@IB, TB @ELLPB QL PQLM FQ COLJ RMA>QFKD FQP IL@>QFLK.
!KA TB >IPL E>SB >K LMMLOQRKFQV QL >AGRPQ QEB OL@HBQfP CFQKBPP. 7B @LKPFABO FQ QL ?B MOBQQV
QBOOF?IB FC QEB OL@HBQ EFQP >K L?PQ>@IB, >KA PL FQP CFQKBPP PELRIA ?B DOB>QIV OBAR@BA.
=F;MM -<MN;=F? U
An obstacle is a location (top left corner of
rectangle) with a width and height.
.3?=NIL FI=;NCIH;
@FI;N Q,B;
<IIF?;H =IHN;CHM(.3?=NIL P) U
C@ (P.R > FI=;NCIH.R && P.R < FI=;NCIH.R + Q && P.S > FI=;NCIH.S && P.S <
FI=;NCIH.S + B) U
L?NOLH NLO?;
W ?FM? U
L?NOLH @;FM?;
W
W
This new function lives in the rocket class
and checks if a rocket has hit an obstacle.
PIC> I<MN;=F?M() U
@IL (-<MN;=F? I<M : I<MN;=F?M) U
C@ (I<M.=IHN;CHM(FI=;NCIH)) U
MNIJJ?> = NLO?;
W
W
W
PIC> LOH() U
Only run the rocket if it doesnt hit an
obstacle.
?< (!IJEFF;:) Q
;JJFS$IL=?(>H;.A?H?M8A?H?!IOHN?L9);
A?H?!IOHN?L = (A?H?!IOHN?L + 1) % >H;.A?H?M.F?HANB;
OJ>;N?();
E8IJ79B;I();
W
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
428
)JMOLSBJBKQ #2: %SLISB OB>@EFKD QEB Q>ODBQ C>PQBO
)C VLR ILLH @ILPBIV >Q LRO CFOPQ 3J>OQ 2L@HBQP BU>JMIB, VLRfII KLQF@B QE>Q QEB OL@HBQP >OB KLQ
OBT>OABA CLO DBQQFKD QL QEB Q>ODBQ C>PQBO. 4EB LKIV S>OF>?IB FK QEBFO CFQKBPP @>I@RI>QFLK FP QEB
AFPQ>K@B QL QEB Q>ODBQ >Q QEB BKA LC QEB DBKBO>QFLKfP IFCB. )K C>@Q, FK QEB BSBKQ QE>Q QEB OL@HBQP
DBQ SBOV @ILPB QL QEB Q>ODBQ ?RQ LSBOPELLQ FQ >KA CIV M>PQ, QEBV J>V >@QR>IIV ?B MBK>IFWBA CLO
DBQQFKD QL QEB Q>ODBQ C>PQBO. 3ILT >KA PQB>AV TFKP QEB O>@B FK QEFP @>PB.
7B @LRIA FJMOLSB QEB >IDLOFQEJ QL LMQFJFWB CLO PMBBA > KRJ?BO LC T>VP. &FOPQ, FKPQB>A LC
RPFKD QEB AFPQ>K@B QL QEB Q>ODBQ >Q QEB BKA LC QEB DBKBO>QFLK, TB @LRIA RPB QEB AFPQ>K@B QE>Q FP
QEB @ILPBPQ QL QEB Q>ODBQ >Q >KV MLFKQ AROFKD QEB OL@HBQfP IFCB. 7B TLRIA @>II QEFP QEB OL@HBQfP
gOB@LOAh AFPQ>K@B. (!II LC QEB @LAB PKFMMBQP FK QEFP PB@QFLK IFSB FKPFAB QEB /I=E?N @I>PP.)
)K >AAFQFLK, > OL@HBQ PELRIA ?B OBT>OABA >@@LOAFKD QL ELT NRF@HIV FQ OB>@EBP QEB Q>ODBQ. 4EB
C>PQBO FQ OB>@EBP QEB Q>ODBQ, QEB EFDEBO QEB CFQKBPP. 4EB PILTBO, QEB ILTBO. 4L >@@LJMIFPE QEFP,
TB @>K FK@OBJBKQ > @LRKQBO BSBOV @V@IB LC QEB OL@HBQfP IFCB RKQFI FQ OB>@EBP QEB Q>ODBQ. !Q QEB
BKA LC FQP IFCB, QEB @LRKQBO TFII BNR>I QEB >JLRKQ LC QFJB QEB OL@HBQ QLLH QL OB>@E QE>Q Q>ODBQ.
&FQKBPP FP >IPL FKSBOPBIV MOLMLOQFLK>I QL @CHCMB1CG?, >KA PL TB @>K FJMOLSB LRO CFQKBPP
CRK@QFLK >P CLIILTP:
PIC> @CNH?MM() U
@FI;N > = >CMN(FI=;NCIH.R, FI=;NCIH.S, N;LA?N.FI=;NCIH.R, N;LA?N.FI=;NCIH.S);
@CNH?MM = JIQ(1/>, 2);
?< (IJEFF;:) <?JD;II *= 0.1;
W
PIC> =B?=E1;LA?N() U
@FI;N > = >CMN(FI=;NCIH.R, FI=;NCIH.S, N;LA?N.FI=;NCIH.R, N;LA?N.FI=;NCIH.S);
Every frame, we check its distance and see
if its closer than the record distance. If it is,
we have a new record.
C@ (> < L?=IL>"CMN) L?=IL>"CMN = >;
If the object reaches the target, set a
boolean flag to true.
C@ (N;LA?N.=IHN;CHM(FI=;NCIH)) U
BCN1;LA?N = NLO?;
W ?FM? C@ (!BCN1;LA?N) U
As long as we havent yet reached the
target, keep incrementing the counter.
@CHCMB1CG?++;
W
W
PIC> @CNH?MM() U
Finish time and record distance! @CNH?MM = (1/(@CHCMB1CG?*L?=IL>"CMN));
4EB .>QROB LC #LAB (S005)
429
4EBPB FJMOLSBJBKQP >OB ?LQE FK@LOMLO>QBA FKQL QEB @LAB CLO %U>JMIB 9.3: 3J>OQ 2L@HBQP.
/KB LC QEB JLOB C>JLRP FJMIBJBKQ>QFLKP LC DBKBQF@ >IDLOFQEJP FK @LJMRQBO DO>MEF@P FP +>OI
3FJPfP g%SLISBA 6FOQR>I #OB>QROBP.h )K 3FJPfP TLOH, > MLMRI>QFLK LC AFDFQ>I @OB>QROBP (FK >
PFJRI>QBA MEVPF@P BKSFOLKJBKQ) FP BS>IR>QBA CLO QEB @OB>QROBP' >?FIFQV QL MBOCLOJ Q>PHP, PR@E
>P PTFJJFKD, ORKKFKD, GRJMFKD, CLIILTFKD, >KA @LJMBQFKD CLO > DOBBK @R?B.
Make it exponential. @CNH?MM = JIQ(@CNH?MM, 2);
Fitness goes way down if you hit an
obstacle.
C@ (MNIJJ?>) @CNH?MM *= 0.1;
You are rewarded for reaching the target. C@ (BCN1;LA?N) @CNH?MM *= 2;
W
#OB>QB > JLOB @LJMIBU L?PQ>@IB @LROPB. !P VLR J>HB FQ JLOB AFCCF@RIQ CLO QEB OL@HBQP
QL OB>@E QEB Q>ODBQ, AL VLR KBBA QL FJMOLSB LQEBO >PMB@QP LC QEB '!dCLO BU>JMIB,
QEB CFQKBPP CRK@QFLK?
Exercise 9.8 Exercise 9.8
)JMIBJBKQ QEB OL@HBQ CFOFKD M>QQBOK LC *BO 4ELOMfP 3J>OQ 2L@HBQP. %>@E OL@HBQ LKIV
DBQP CFSB QEORPQBOP (LC >KV AFOB@QFLK >KA PQOBKDQE) QE>Q CLIILT > CFOFKD PBNRBK@B (LC
>O?FQO>OV IBKDQE). *BOfP PFJRI>QFLK (EQQM://TTT.?IMOKQ.@LJ/PJ>OQOL@HBQP/) >IPL DFSBP QEB
OL@HBQP > CFKFQB >JLRKQ LC CRBI.
Exercise 9.9 Exercise 9.9
6FPR>IFWB QEB OL@HBQP AFCCBOBKQIV. #>K VLR AO>T > IFKB CLO QEB PELOQBPQ M>QE QL QEB
Q>ODBQ? #>K VLR >AA M>OQF@IB PVPQBJP QE>Q >@Q >P PJLHB FK QEB AFOB@QFLK LC QEB OL@HBQ
QEORPQBOP?
Exercise 9.10 Exercise 9.10
!KLQEBO T>V QL >@EFBSB > PFJFI>O OBPRIQ FP QL BSLISB > CILT CFBIA. #>K VLR J>HB QEB
DBKLQVMB LC > OL@HBQ > CILT CFBIA LC .3?=NILP?
Exercise 9.11 Exercise 9.11
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
430
/KB LC QEB FKKLS>QFLKP FK 3FJPfP TLOH FP > KLAB-?>PBA DBKLQVMB. )K LQEBO TLOAP, QEB
@OB>QROBfP $.! FP KLQ > IFKB>O IFPQ LC .3?=NILP LO KRJ?BOP, ?RQ > J>M LC KLABP. (&LO >K
BU>JMIB LC QEFP, Q>HB > ILLH >Q %UBO@FPB 5.15 (PBB M>DB 256), QLUF@IF?P' &LO@B $FOB@QBA 'O>ME.)
4EB MEBKLQVMB FP QEB @OB>QROBfP ABPFDK FQPBIC, > KBQTLOH LC IFJ?P @LKKB@QBA TFQE JRP@IBP.
5PFKD QLUF@IF?P LO "LU2$ >P QEB MEVPF@P
JLABI, @>K VLR @OB>QB > PFJMIFCFBA 2$
SBOPFLK LC 3FJPfP @OB>QROBP? &LO >
IBKDQEFBO ABP@OFMQFLK LC 3FJPfP
QB@EKFNRBP, ) PRDDBPQ VLR T>Q@E QEB
SFABL >KA OB>A 3FJPfP M>MBO 6FOQR>I
#OB>QROBP (EQQM://TTT.H>OIPFJP.@LJ/
BSLISBA-SFOQR>I-@OB>QROBP.EQJI). )K
>AAFQFLK, VLR @>K CFKA > PFJFI>O BU>JMIB
QE>Q RPBP "LU2$ QL BSLISB > g@>Oh:
"LU#>O2$ (EQQM://?LU@>O2A.@LJ/).
Exercise 9.12 Exercise 9.12
9.12 Interactive Selection 9.12 Interactive Selection
)K >AAFQFLK QL %SLISBA 6FOQR>I #OB>QROBP, 3FJP FP >IPL TBII HKLTK CLO EFP JRPBRJ FKPQ>II>QFLK
#5@5D5;CG. /OFDFK>IIV FKPQ>IIBA FK QEB )KQBO@LJJRKF@>QFLK #BKQBO FK 4LHVL FK 1997, QEB
FKPQ>II>QFLK @LKPFPQP LC QTBISB JLKFQLOP AFPMI>VFKD @LJMRQBO-DBKBO>QBA FJ>DBP. 4EBPB FJ>DBP
BSLISB LSBO QFJB, CLIILTFKD QEB DBKBQF@ >IDLOFQEJ PQBMP LC PBIB@QFLK >KA OBMOLAR@QFLK. 4EB
FKKLS>QFLK EBOB FP KLQ QEB RPB LC QEB DBKBQF@ >IDLOFQEJ FQPBIC, ?RQ O>QEBO QEB PQO>QBDV ?BEFKA
QEB CFQKBPP CRK@QFLK. )K COLKQ LC B>@E JLKFQLO FP > PBKPLO LK QEB CILLO QE>Q @>K ABQB@Q QEB
MOBPBK@B LC > RPBO SFBTFKD QEB P@OBBK. 4EB CFQKBPP LC >K FJ>DB FP QFBA QL QEB IBKDQE LC QFJB
QE>Q SFBTBOP ILLH >Q QEB FJ>DB. 4EFP FP HKLTK >P =BH9F57H=J9 G9@97H=CB, > DBKBQF@ >IDLOFQEJ TFQE
CFQKBPP S>IRBP >PPFDKBA ?V RPBOP.
4EFKH LC >II QEB O>QFKD PVPQBJP VLRfSB BSBO RPBA. #LRIA VLR BSLISB QEB MBOCB@Q JLSFB ?V
P@LOFKD >II CFIJP >@@LOAFKD QL VLRO .BQCIFU O>QFKDP? 4EB MBOCB@Q PFKDBO >@@LOAFKD QL !JBOF@>K
)ALI SLQFKD?
4EB .>QROB LC #LAB (S005)
431
4L FIIRPQO>QB QEFP QB@EKFNRB, TBfOB DLFKD QL
?RFIA > MLMRI>QFLK LC PFJMIB C>@BP. %>@E
C>@B TFII E>SB > PBQ LC MOLMBOQFBP: EB>A
PFWB, EB>A @LILO, BVB IL@>QFLK, BVB PFWB,
JLRQE @LILO, JLRQE IL@>QFLK, JLRQE TFAQE,
>KA JLRQE EBFDEQ.
4EB C>@BfP $.! (DBKLQVMB) FP >K >OO>V LC
CIL>QFKD MLFKQ KRJ?BOP ?BQTBBK 0 >KA 1,
TFQE > PFKDIB S>IRB CLO B>@E MOLMBOQV.
4EB MEBKLQVMB FP > $;=? @I>PP QE>Q FK@IRABP >K FKPQ>K@B LC > ",A L?GB@Q.
7EBK FQ @LJBP QFJB QL AO>T QEB C>@B LK P@OBBK, TB @>K RPB 0OL@BPPFKDfP G;J() CRK@QFLK QL
@LKSBOQ >KV DBKB S>IRB QL QEB >MMOLMOF>QB O>KDB CLO MFUBI AFJBKPFLKP LO @LILO S>IRBP. ()K
QEFP @>PB, TB >OB >IPL RPFKD =IFIL+I>?() QL PBQ QEB 2'" O>KDBP ?BQTBBK 0 >KA 1.)
F64B?2 9.14
=F;MM ",A U
@FI;N89 A?H?M;
We need 20 numbers to draw the face. CHN F?H = 20;
",A() U
A?H?M = H?Q @FI;N8F?H9;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
Each gene is a random float between 0
and 1.
A?H?M8C9 = L;H>IG(0,1);
W
W
=F;MM $;=? U
",A >H;;
@FI;N @CNH?MM;
PIC> >CMJF;S() U
Using map() to convert the genes to a
range for drawing the face.
@FI;N L = G;J(>H;.A?H?M809,0,1,0,70);
=IFIL = = =IFIL(>H;.A?H?M819,>H;.A?H?M829,>H;.A?H?M839);
@FI;N ?S?:S = G;J(>H;.A?H?M849,0,1,0,5);
@FI;N ?S?:R = G;J(>H;.A?H?M859,0,1,0,10);
@FI;N ?S?:MCT? = G;J(>H;.A?H?M859,0,1,0,10);
=IFIL ?S?=IFIL = =IFIL(>H;.A?H?M849,>H;.A?H?M859,>H;.A?H?M869);
=IFIL GIONB!IFIL = =IFIL(>H;.A?H?M879,>H;.A?H?M889,>H;.A?H?M899);
@FI;N GIONB:S = G;J(>H;.A?H?M859,0,1,0,25);
@FI;N GIONB:R = G;J(>H;.A?H?M859,0,1,-25,25);
@FI;N GIONBQ = G;J(>H;.A?H?M859,0,1,0,50);
@FI;N GIONBB = G;J(>H;.A?H?M859,0,1,0,10);
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
432
3L C>O, TBfOB KLQ OB>IIV ALFKD >KVQEFKD KBT. 4EFP FP TE>Q TBfSB ALKB FK BSBOV '! BU>JMIB PL
C>O. 7E>QfP KBT FP QE>Q TB >OB KLQ DLFKD QL TOFQB > @CNH?MM() CRK@QFLK FK TEF@E QEB P@LOB FP
@LJMRQBA ?>PBA LK > J>QE CLOJRI>. )KPQB>A, TB >OB DLFKD QL >PH QEB RPBO QL >PPFDK QEB
CFQKBPP.
.LT, ELT ?BPQ QL >PH > RPBO QL >PPFDK CFQKBPP FP OB>IIV JLOB LC >K FKQBO>@QFLK ABPFDK MOL?IBJ,
>KA FQ FPKfQ OB>IIV TFQEFK QEB P@LMB LC QEFP ?LLH. 3L TBfOB KLQ DLFKD QL I>RK@E FKQL >K BI>?LO>QB
AFP@RPPFLK LC ELT QL MOLDO>J PIFABOP LO ?RFIA VLRO LTK E>OAT>OB AF>IP LO ?RFIA > 7B? >MM CLO
RPBOP QL PR?JFQ LKIFKB P@LOBP. (LT VLR @ELLPB QL >@NRFOB CFQKBPP P@LOBP FP OB>IIV RM QL VLR
>KA QEB M>OQF@RI>O >MMIF@>QFLK VLR >OB ABSBILMFKD.
&LO QEFP PFJMIB ABJLKPQO>QFLK, TBfII FK@OB>PB CFQKBPP TEBKBSBO > RPBO OLIIP QEB JLRPB LSBO >
C>@B. 4EB KBUQ DBKBO>QFLK FP @OB>QBA TEBK QEB RPBO MOBPPBP > ?RQQLK TFQE >K gBSLISB KBUQ
DBKBO>QFLKh I>?BI.
,BQfP ILLH >Q ELT QEB PQBMP LC QEB DBKBQF@ >IDLOFQEJ >OB >MMIFBA FK QEB J>FK Q>?, KLQFKD ELT
CFQKBPP FP >PPFDKBA >@@LOAFKD QL JLRPB FKQBO>@QFLK >KA QEB KBUQ DBKBO>QFLK FP @OB>QBA LK >
?RQQLK MOBPP. 4EB OBPQ LC QEB @LAB CLO @EB@HFKD JLRPB IL@>QFLKP, ?RQQLK FKQBO>@QFLKP, BQ@. @>K
?B CLRKA FK QEB >@@LJM>KVFKD BU>JMIB @LAB.
Example 9.4: Interactive selection
.IJOF;NCIH JIJOF;NCIH;
ONNIH <ONNIH;
PIC> M?NOJ() U
MCT?(780,200);
@FI;N GON;NCIH/;N? = 0.05;
JIJOF;NCIH = H?Q .IJOF;NCIH(GON;NCIH/;N?,10);
<ONNIH = H?Q ONNIH(15,150,160,20, "?PIFP? H?Q A?H?L;NCIH");
W
PIC> >L;Q() U
JIJOF;NCIH.>CMJF;S();
4EB .>QROB LC #LAB (S005)
433
4EFP BU>JMIB, FQ PELRIA ?B KLQBA, FP OB>IIV GRPQ > ABJLKPQO>QFLK LC QEB FAB> LC FKQBO>@QFSB
PBIB@QFLK >KA ALBP KLQ >@EFBSB > M>OQF@RI>OIV JB>KFKDCRI OBPRIQ. &LO LKB, TB AFAKfQ Q>HB
JR@E @>OB FK QEB SFPR>I ABPFDK LC QEB C>@BP; QEBV >OB GRPQ > CBT PFJMIB PE>MBP TFQE PFWBP >KA
@LILOP. 3FJP, CLO BU>JMIB, RPBA JLOB BI>?LO>QB J>QEBJ>QF@>I CRK@QFLKP >P EFP FJ>DBPf
DBKLQVMB. 9LR JFDEQ >IPL @LKPFABO > SB@QLO-?>PBA >MMOL>@E, FK TEF@E > ABPFDKfP DBKLQVMB
FP > PBQ LC MLFKQP >KA/LO M>QEP.
4EB JLOB PFDKFCF@>KQ MOL?IBJ EBOB, ELTBSBO, FP LKB LC QFJB. )K QEB K>QRO>I TLOIA, BSLIRQFLK
L@@ROP LSBO JFIIFLKP LC VB>OP. )K QEB @LJMRQBO PFJRI>QFLK TLOIA LC LRO MOBSFLRP BU>JMIBP,
TB TBOB >?IB QL BSLISB ?BE>SFLOP OBI>QFSBIV NRF@HIV ?B@>RPB TB TBOB MOLAR@FKD KBT
DBKBO>QFLKP >IDLOFQEJF@>IIV. )K QEB 3E>HBPMB>OB JLKHBV BU>JMIB, > KBT DBKBO>QFLK T>P
?LOK FK B>@E CO>JB LC >KFJ>QFLK (>MMOLUFJ>QBIV PFUQV MBO PB@LKA). 3FK@B QEB CFQKBPP S>IRBP
TBOB @LJMRQBA >@@LOAFKD QL > J>QE CLOJRI>, TB @LRIA >IPL E>SB E>A >O?FQO>OFIV I>ODB
MLMRI>QFLKP QE>Q FK@OB>PBA QEB PMBBA LC BSLIRQFLK. )K QEB @>PB LC FKQBO>@QFSB PBIB@QFLK,
ELTBSBO, TB E>SB QL PFQ >KA T>FQ CLO > RPBO QL O>QB B>@E >KA BSBOV JBJ?BO LC QEB
MLMRI>QFLK ?BCLOB TB @>K DBQ QL QEB KBUQ DBKBO>QFLK. ! I>ODB MLMRI>QFLK TLRIA ?B
RKOB>PLK>?IV QBAFLRP QL AB>I TFQEdKLQ QL JBKQFLK, ELT J>KV DBKBO>QFLKP @LRIA VLR PQ>KA
QL PFQ QEOLRDE?
4EBOB >OB @BOQ>FKIV @IBSBO PLIRQFLKP >OLRKA QEFP. 3FJPfP '>I>M>DLP BUEF?FQ @LK@B>IBA QEB
O>QFKD MOL@BPP COLJ QEB RPBOP, >P FQ L@@ROOBA QEOLRDE QEB KLOJ>I ?BE>SFLO LC ILLHFKD >Q
>OQTLOH FK > JRPBRJ PBQQFKD. "RFIAFKD > 7B? >MMIF@>QFLK QE>Q TLRIA >IILT J>KV RPBOP QL O>QB
> MLMRI>QFLK FK > AFPQOF?RQBA C>PEFLK FP >IPL > DLLA PQO>QBDV CLO >@EFBSFKD J>KV O>QFKDP CLO
I>ODB MLMRI>QFLKP NRF@HIV.
)K QEB BKA, QEB HBV QL > PR@@BPPCRI FKQBO>@QFSB PBIB@QFLK PVPQBJ ?LFIP ALTK QL QEB P>JB HBVP
TB MOBSFLRPIV BPQ>?IFPEBA. 7E>Q FP QEB DBKLQVMB >KA MEBKLQVMB? !KA ELT AL VLR @>I@RI>QB
CFQKBPP, TEF@E FK QEFP @>PB TB @>K OBSFPB QL P>V: g7E>Q FP VLRO PQO>QBDV CLO >PPFDKFKD CFQKBPP
>@@LOAFKD QL RPBO FKQBO>@QFLK?h
The mouse location is passed to the
population, which will score each face
according to rollover time.
FEFKB7J?ED.HEBBEL;H(CEKI;2,CEKI;3);
<ONNIH.>CMJF;S();
W
PIC> GIOM?.L?MM?>() U
When a button is pressed, the new
generation is created via selection and
reproduction.
?< (8KJJED.9B?9A;:(CEKI;2,CEKI;3)) Q
JIJOF;NCIH.M?F?=NCIH();
JIJOF;NCIH.L?JLI>O=NCIH();
W
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
434
"RFIA VLRO LTK FKQBO>@QFSB PBIB@QFLK MOLGB@Q. )K >AAFQFLK QL > SFPR>I ABPFDK, @LKPFABO
BSLISFKD PLRKAPdCLO BU>JMIB, > PELOQ PBNRBK@B LC QLKBP. #>K VLR ABSFPB > PQO>QBDV,
PR@E >P > 7B? >MMIF@>QFLK LO MEVPF@>I PBKPLO PVPQBJ, QL >@NRFOB O>QFKDP COLJ J>KV
RPBOP LSBO QFJB?
Exercise 9.14 Exercise 9.14
9.13 Ecosystem Simulation 9.13 Ecosystem Simulation
9LR J>V E>SB KLQF@BA PLJBQEFKD > ?FQ LAA >?LRQ BSBOV PFKDIB BSLIRQFLK>OV PVPQBJ TBfSB ?RFIQ
PL C>O FK QEFP @E>MQBO. !CQBO >II, FK QEB OB>I TLOIA, > MLMRI>QFLK LC ?>?FBP FPKfQ ?LOK >II >Q QEB
P>JB QFJB. 4ELPB ?>?FBP ALKfQ QEBK DOLT RM >KA >II OBMOLAR@B >Q BU>@QIV QEB P>JB QFJB, QEBK
FKPQ>KQIV AFB QL IB>SB QEB MLMRI>QFLK PFWB MBOCB@QIV PQ>?IB. 4E>Q TLRIA ?B OFAF@RILRP. .LQ QL
JBKQFLK QEB C>@Q QE>Q QEBOB FP @BOQ>FKIV KL LKB ORKKFKD >OLRKA QEB CLOBPQ TFQE > @>I@RI>QLO
@ORK@EFKD KRJ?BOP >KA >PPFDKFKD CFQKBPP S>IRBP QL >II QEB @OB>QROBP.
)K QEB OB>I TLOIA, TB ALKfQ OB>IIV E>SB gPROSFS>I LC QEB CFQQBPQh; TB E>SB gPROSFS>I LC QEB
PROSFSLOP.h 4EFKDP QE>Q E>MMBK QL IFSB ILKDBO, CLO TE>QBSBO OB>PLK, E>SB > DOB>QBO @E>K@B LC
OBMOLAR@FKD. ">?FBP >OB ?LOK, QEBV IFSB CLO > TEFIB, J>V?B QEBV QEBJPBISBP E>SB ?>?FBP,
J>V?B QEBV ALKfQ, >KA QEBK QEBV AFB.
9LR TLKfQ KB@BPP>OFIV CFKA PFJRI>QFLKP LC gOB>I-TLOIAh BSLIRQFLK FK >OQFCF@F>I FKQBIIFDBK@B
QBUQ?LLHP. 'BKBQF@ >IDLOFQEJP >OB DBKBO>IIV RPBA FK QEB JLOB CLOJ>I J>KKBO TB LRQIFKBA FK
QEFP @E>MQBO. (LTBSBO, PFK@B TB >OB OB>AFKD QEFP ?LLH QL ABSBILM PFJRI>QFLKP LC K>QRO>I
PVPQBJP, FQfP TLOQE ILLHFKD >Q PLJB T>VP FK TEF@E TB JFDEQ RPB > DBKBQF@ >IDLOFQEJ QL ?RFIA
PLJBQEFKD QE>Q OBPBJ?IBP > IFSFKD gB@LPVPQBJh, JR@E IFHB QEB LKB TBfSB ABP@OF?BA FK QEB
BUBO@FPBP >Q QEB BKA LC B>@E @E>MQBO.
,BQfP ?BDFK ?V ABSBILMFKD > SBOV PFJMIB P@BK>OFL. 7BfII @OB>QB > @OB>QROB @>IIBA > "?ILLM," >
@FO@IB QE>Q JLSBP >?LRQ QEB P@OBBK >@@LOAFKD QL 0BOIFK KLFPB. 4EB @OB>QROB TFII E>SB > O>AFRP
>KA > J>UFJRJ PMBBA. 4EB ?FDDBO FQ FP, QEB PILTBO FQ JLSBP; QEB PJ>IIBO, QEB C>PQBO.
=F;MM FIIJ U
A location .3?=NIL FI=;NCIH;
Variables for size and speed @FI;N L;
@FI;N G;RMJ??>;
4EB .>QROB LC #LAB (S005)
435
4EB >?LSB FP JFPPFKD > CBT ABQ>FIP (PR@E >P FKFQF>IFWFKD QEB S>OF>?IBP FK QEB @LKPQOR@QLO), ?RQ
VLR DBQ QEB FAB>.
&LO QEFP BU>JMIB, TBfII T>KQ QL PQLOB QEB MLMRI>QFLK LC ?ILLMP FK >K ALL;S*CMN, O>QEBO QE>K
>K >OO>V, >P TB BUMB@Q QEB MLMRI>QFLK QL DOLT >KA PEOFKH >@@LOAFKD QL ELT LCQBK ?ILLMP AFB
LO >OB ?LOK. 7B @>K PQLOB QEFP ALL;S*CMN FK > @I>PP @>IIBA 4ILF>, TEF@E TFII J>K>DB >II QEB
BIBJBKQP LC QEB ?ILLMPf TLOIA.
3L C>O, TE>Q TB E>SB FP GRPQ > OBE>PEFKD LC LRO M>OQF@IB PVPQBJ BU>JMIB COLJ #E>MQBO 5. 7B
E>SB >K BKQFQV (FIIJ) QE>Q JLSBP >OLRKA QEB TFKALT >KA > @I>PP (4ILF>) QE>Q J>K>DBP >
S>OF>?IB NR>KQFQV LC QEBPB BKQFQFBP. 4L QROK QEFP FKQL > PVPQBJ QE>Q BSLISBP, TB KBBA QL >AA
QTL >AAFQFLK>I CB>QROBP QL LRO TLOIA:
i B7::;> /40. B7::;> /40.
i B7::;> ,=0 -:=9. B7::;> ,=0 -:=9.
Some variables for Perlin noise
calculations
@FI;N RI@@, SI@@;
PIC> OJ>;N?() U
@FI;N PR = G;J(HICM?(RI@@),0,1,-G;RMJ??>,G;RMJ??>);
@FI;N PS = G;J(HICM?(SI@@),0,1,-G;RMJ??>,G;RMJ??>);
A little Perlin noise algorithm to calculate a
velocity
.3?=NIL P?FI=CNS = H?Q .3?=NIL(PR,PS);
RI@@ += 0.01;
SI@@ += 0.01;
The bloop moves. FI=;NCIH.;>>(P?FI=CNS);
W
A bloop is a circle. PIC> >CMJF;S() U
?FFCJM?(FI=;NCIH.R, FI=;NCIH.S, L, L);
W
W
=F;MM 4ILF> U
A list of bloops ALL;S*CMN<FIIJ> <FIIJM;
4ILF>(CHN HOG) U
<FIIJM = H?Q ALL;S*CMN<FIIJ>();
@IL (CHN C = 0; C < HOG; C++) U
Making an initial population of bloops <FIIJM.;>>(H?Q FIIJ());
W
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
436
"ILLMP AVFKD FP LRO OBMI>@BJBKQ CLO > CFQKBPP CRK@QFLK, QEB MOL@BPP LC gPBIB@QFLK.h )C > ?ILLM
AFBP, FQ @>KKLQ ?B PBIB@QBA QL ?B > M>OBKQ, ?B@>RPB FQ PFJMIV KL ILKDBO BUFPQP! /KB T>V TB @>K
?RFIA > JB@E>KFPJ QL BKPROB ?ILLM AB>QEP FK LRO TLOIA FP ?V >AAFKD > B?;FNB S>OF>?IB QL QEB
FIIJ @I>PP.
)K B>@E CO>JB LC >KFJ>QFLK, > ?ILLM ILPBP PLJB EB>IQE.
)C EB>IQE AOLMP ?BILT 0, QEB ?ILLM AFBP.
4EFP FP > DLLA CFOPQ PQBM, ?RQ TB E>SBKfQ OB>IIV >@EFBSBA >KVQEFKD. !CQBO >II, FC >II ?ILLMP PQ>OQ
TFQE 100 EB>IQE MLFKQP >KA ILPB 1 MLFKQ MBO CO>JB, QEBK >II ?ILLMP TFII IFSB CLO QEB BU>@Q P>JB
>JLRKQ LC QFJB >KA AFB QLDBQEBO. )C BSBOV PFKDIB ?ILLM IFSBP QEB P>JB >JLRKQ LC QFJB, QEBV >II
E>SB BNR>I @E>K@BP LC OBMOLAR@FKD >KA QEBOBCLOB KLQEFKD TFII BSLISB.
4EBOB >OB J>KV T>VP TB @LRIA >@EFBSB S>OF>?IB IFCBPM>KP TFQE > JLOB PLMEFPQF@>QBA TLOIA. &LO
BU>JMIB, TB @LRIA FKQOLAR@B MOBA>QLOP QE>Q B>Q ?ILLMP. 0BOE>MP QEB C>PQBO ?ILLMP TLRIA ?B
>?IB QL BP@>MB ?BFKD B>QBK JLOB B>PFIV, >KA QEBOBCLOB LRO TLOIA TLRIA BSLISB QL E>SB C>PQBO
>KA C>PQBO ?ILLMP. !KLQEBO LMQFLK TLRIA ?B QL FKQOLAR@B CLLA. 7EBK > ?ILLM B>QP CLLA, FQ
FK@OB>PBP FQP EB>IQE MLFKQP, >KA QEBOBCLOB BUQBKAP FQP IFCB.
,BQfP >PPRJB TB E>SB >K ALL;S*CMN LC .3?=NIL IL@>QFLKP CLO CLLA, K>JBA gCLLA.h 7B @LRIA
QBPQ B>@E ?ILLMfP MOLUFJFQV QL B>@E CLLA IL@>QFLK. )C QEB ?ILLM FP @ILPB BKLRDE, FQ B>QP QEB CLLA
(TEF@E FP QEBK OBJLSBA COLJ QEB TLOIA) >KA FK@OB>PBP FQP EB>IQE.
=F;MM FIIJ U
A bloop is born with 100 health points. @FI;N B?;FNB = 100;
PIC> OJ>;N?() U
All that other stuff for movement
Death is always looming! B?;FNB -= 1;
W
We add a function to the Bloop class to test
if the bloop is alive or dead.
<IIF?;H >?;>() U
C@ (B?;FNB < 0.0) U
L?NOLH NLO?;
W ?FM? U
L?NOLH @;FM?;
W
W
PIC> ?;N() U
@IL (CHN C = @II>.MCT?()-1; C >= 0; C--) U
.3?=NIL @II>*I=;NCIH = @II>.A?N(C);
@FI;N > = .3?=NIL.>CMN(FI=;NCIH, @II>*I=;NCIH);
4EB .>QROB LC #LAB (S005)
437
.LT TB E>SB > P@BK>OFL FK TEF@E ?ILLMP QE>Q B>Q JLOB CLLA IFSB ILKDBO >KA E>SB > DOB>QBO
IFHBIFELLA LC OBMOLAR@FKD. 4EBOBCLOB, TB BUMB@Q QE>Q LRO PVPQBJ TLRIA BSLISB ?ILLMP TFQE
>K LMQFJ>I >?FIFQV QL CFKA >KA B>Q CLLA.
.LT QE>Q TB E>SB ?RFIQ LRO TLOIA, FQfP QFJB QL >AA QEB @LJMLKBKQP OBNRFOBA CLO BSLIRQFLK.
&FOPQ TB PELRIA BPQ>?IFPE LRO DBKLQVMB >KA MEBKLQVMB.
Is the Bloop close to the food? C@ (> < L/2) U
If so, it gets 100 more health points. B?;FNB += 100;
The food is no longer available for other
Bloops.
@II>.L?GIP?(C);
W
W
W
Genotype and Phenotype Genotype and Phenotype
4EB >?FIFQV CLO > ?ILLM QL CFKA CLLA FP QFBA QL QTL S>OF>?IBPdPFWB >KA PMBBA. "FDDBO ?ILLMP
TFII CFKA CLLA JLOB B>PFIV PFJMIV ?B@>RPB QEBFO PFWB TFII >IILT QEBJ QL FKQBOPB@Q TFQE CLLA
IL@>QFLKP JLOB LCQBK. !KA C>PQBO ?ILLMP TFII CFKA JLOB CLLA ?B@>RPB QEBV @>K @LSBO JLOB
DOLRKA FK > PELOQBO MBOFLA LC QFJB.
3FK@B PFWB >KA PMBBA >OB FKSBOPBIV OBI>QBA
(I>ODB ?ILLMP >OB PILT, PJ>II ?ILLMP >OB
C>PQ), TB LKIV KBBA > DBKLQVMB TFQE >
PFKDIB KRJ?BO.
F64B?2 9.15
=F;MM ",A U
@FI;N89 A?H?M;
",A() U
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
438
4EB MEBKLQVMB QEBK FP QEB ?ILLM FQPBIC, TELPB PFWB >KA PMBBA FP >PPFDKBA ?V >AAFKD >K
FKPQ>K@B LC > ",A L?GB@Q QL QEB FIIJ @I>PP.
.LQF@B QE>Q TFQE G;RMJ??>, QEB O>KDB FP J>MMBA QL ?BQTBBK 15 >KA 0, JB>KFKD > ?ILLM TFQE >
DBKB S>IRB LC 0 JLSBP >Q > PMBBA LC 15 >KA > ?ILLM TFQE > DBKB S>IRB LC 1 ALBPKfQ JLSB >Q >II
(PMBBA LC 0).
It may seem absurd to use an array when all
we have is a single value, but we stick with
an array in case we want to make more
sophisticated bloops later.
A?H?M = H?Q @FI;N819;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
A?H?M8C9 = L;H>IG(0,1);
W
W
=F;MM FIIJ U
.3?=NIL FI=;NCIH;
@FI;N B?;FNB;
A bloop now has DNA. ",A >H;;
@FI;N L;
@FI;N G;RMJ??>;
FIIJ(",A >H;:) U
FI=;NCIH = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
B?;FNB = 200;
>H; = >H;:;
maxspeed and r (radius) are mapped to
values according to the DNA.
G;RMJ??> = G;J(>H;.A?H?M809, 0, 1, 15, 0);
L = G;J(>H;.A?H?M809, 0, 1, 0, 50);
W
Selection and Reproduction Selection and Reproduction
.LT QE>Q TB E>SB QEB DBKLQVMB >KA MEBKLQVMB, TB KBBA QL JLSB LK QL ABSFPFKD > JB>KP CLO
?ILLMP QL ?B PBIB@QBA >P M>OBKQP. 7B PQ>QBA ?BCLOB QE>Q QEB ILKDBO > ?ILLM IFSBP, QEB JLOB
@E>K@BP FQ E>P QL OBMOLAR@B. 4EB IBKDQE LC IFCB FP QEB ?ILLMfP CFQKBPP.
/KB LMQFLK TLRIA ?B QL P>V QE>Q TEBKBSBO QTL ?ILLMP @LJB FKQL @LKQ>@Q TFQE B>@E LQEBO, QEBV
J>HB > KBT ?ILLM. 4EB ILKDBO > ?ILLM IFSBP, QEB JLOB IFHBIV FQ FP QL @LJB FKQL @LKQ>@Q TFQE
>KLQEBO ?ILLM. (4EFP TLRIA >IPL >CCB@Q QEB BSLIRQFLK>OV LRQ@LJB DFSBK QE>Q, FK >AAFQFLK QL
B>QFKD CLLA, QEBFO >?FIFQV QL CFKA LQEBO ?ILLMP FP > C>@QLO FK QEB IFHBIFELLA LC E>SFKD > ?>?V.)
! PFJMIBO LMQFLK TLRIA ?B QL E>SB g>PBUR>Ih OBMOLAR@QFLK, JB>KFKD > ?ILLM ALBP KLQ OBNRFOB
> M>OQKBO. )Q @>K, >Q >KV JLJBKQ, J>HB > @ILKB LC FQPBIC, >KLQEBO ?ILLM TFQE QEB P>JB DBKBQF@
J>HBRM. )C TB PQ>QB QEFP PBIB@QFLK >IDLOFQEJ >P CLIILTP:
A? ,9D 24A09 8:809?, , -7::; 3,> , 1% .3,9.0 :1 =0;=:/@.492. A? ,9D 24A09 8:809?, , -7::; 3,> , 1% .3,9.0 :1 =0;=:/@.492.
4EB .>QROB LC #LAB (S005)
439
jQEBK QEB ILKDBO > ?ILLM IFSBP, QEB JLOB IFHBIV FQ TFII J>HB >Q IB>PQ LKB @EFIA. 4EFP FP
BNRFS>IBKQ QL P>VFKD QEB JLOB QFJBP VLR MI>V QEB ILQQBOV, QEB DOB>QBO QEB IFHBIFELLA VLRfII TFK
(QELRDE )fJ PLOOV QL P>V VLRO @E>K@BP LC QE>Q >OB PQFII BPPBKQF>IIV WBOL).
4L FJMIBJBKQ QEFP PBIB@QFLK >IDLOFQEJ, TB @>K TOFQB > CRK@QFLK FK QEB FIIJ @I>PP QE>Q MF@HP
> O>KALJ KRJ?BO BSBOV CO>JB. )C QEB KRJ?BO FP IBPP QE>K 0.01 (1%), > KBT ?ILLM FP ?LOK.
(LT ALBP > ?ILLM OBMOLAR@B? )K LRO MOBSFLRP BU>JMIBP, QEB OBMOLAR@QFLK MOL@BPP FKSLISBA
@>IIFKD QEB =LIMMIP?L() CRK@QFLK FK QEB ",A @I>PP >KA J>HFKD > KBT L?GB@Q COLJ QEB KBTIV
J>AB $.!. (BOB, PFK@B TB >OB J>HFKD > @EFIA COLJ > PFKDIB M>OBKQ, TBfII @>II > CRK@QFLK
@>IIBA =IJS() FKPQB>A.
.LQB >IPL QE>Q TBfSB OBAR@BA QEB MOL?>?FIFQV LC OBMOLAR@FKD COLJ 1% QL 0.05%. 4EFP S>IRB
J>HBP NRFQB > AFCCBOBK@B; TFQE > EFDE MOL?>?FIFQV LC OBMOLAR@FKD, QEB PVPQBJ TFII NRF@HIV QBKA
QLT>OAP LSBOMLMRI>QFLK. 4LL ILT > MOL?>?FIFQV, >KA BSBOVQEFKD TFII IFHBIV NRF@HIV AFB LRQ.
7OFQFKD QEB =IJS() CRK@QFLK FKQL QEB ",A @I>PP FP B>PV PFK@B 0OL@BPPFKD FK@IRABP > CRK@QFLK
;LL;S=IJS() QE>Q @LMFBP QEB @LKQBKQP LC LKB >OO>V FKQL >KLQEBO.
This function will return a new bloop, the
child.
FIIJ L?JLI>O=?() U
A 1% chance of executing the code in this
conditional, i.e. a 1% chance of
reproducing
C@ (L;H>IG(1) < 0.01) U
// +;E? NB? FIIJ <;<S
W
W
FIIJ L?JLI>O=?() U
C@ (L;H>IG(1) < 0.0005) U
Make a copy of the DNA. )A 9>?B:)A = :D7.9EFO();
1% mutation rate 9>?B:)A.CKJ7J;(0.01);
Make a new bloop at the same location
with the new DNA.
H;JKHD D;M BBEEF(BE97J?ED, 9>?B:)A);
S ;BI; Q
If the bloop does not reproduce, return null. H;JKHD DKBB;
W
W
=F;MM ",A U
This copy() function replaces crossover() in
this example.
",A =IJS() U
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
440
.LT QE>Q TB E>SB >II QEB MFB@BP FK MI>@B CLO PBIB@QFLK >KA OBMOLAR@QFLK, TB @>K CFK>IFWB QEB
4ILF> @I>PP QE>Q J>K>DBP QEB IFPQ LC >II FIIJ L?GB@QP (>P TBII >P > $II> L?GB@Q, TEF@E FQPBIC FP
> IFPQ LC .3?=NIL IL@>QFLKP CLO CLLA).
"BCLOB VLR ORK QEB BU>JMIB, Q>HB > JLJBKQ QL DRBPP TE>Q PFWB >KA PMBBA LC ?ILLMP QEB
PVPQBJ TFII BSLISB QLT>OAP. 7BfII AFP@RPP CLIILTFKD QEB @LAB.
Example 9.5: Evolution ecosystem
Make a new array the same length and copy
its contents.
@FI;N89 H?QA?H?M = H?Q @FI;N8A?H?M.F?HANB9;
;LL;S=IJS(A?H?M,H?QA?H?M);
L?NOLH H?Q ",A(H?QA?H?M);
W
W
4ILF> QILF>;
setup() and draw() do nothing more than
create and run a World object.
PIC> M?NOJ() U
MCT?(600,400);
QILF> = H?Q 4ILF>(20);
W
PIC> >L;Q() U
<;=EALIOH>(255);
QILF>.LOH();
W
=F;MM 4ILF> U
The World object keeps track of the
population bloops as well as the food.
ALL;S*CMN<FIIJ> <FIIJM;
$II> @II>;
4ILF>(CHN HOG) U
@II> = H?Q $II>(HOG);
<FIIJM = H?Q ALL;S*CMN<FIIJ>();
4EB .>QROB LC #LAB (S005)
441
)C VLR DRBPPBA JBAFRJ-PFWBA ?ILLMP TFQE JBAFRJ PMBBA, VLR TBOB OFDEQ. 7FQE QEB ABPFDK LC
QEFP PVPQBJ, ?ILLMP QE>Q >OB I>ODB >OB PFJMIV QLL PILT QL CFKA CLLA. !KA ?ILLMP QE>Q >OB C>PQ
>OB QLL PJ>II QL CFKA CLLA. 4EB LKBP QE>Q >OB >?IB QL IFSB QEB ILKDBPQ QBKA QL ?B FK QEB
JFAAIB, I>ODB BKLRDE >KA C>PQ BKLRDE QL CFKA CLLA (?RQ KLQ QLL I>ODB LO QLL C>PQ). 4EBOB >OB
>IPL PLJB >KLJ>IFBP. &LO BU>JMIB, FC FQ PL E>MMBKP QE>Q > ?RK@E LC I>ODB ?ILLMP BKA RM FK
QEB P>JB IL@>QFLK (>KA ?>OBIV JLSB ?B@>RPB QEBV >OB PL I>ODB), QEBV J>V >II AFB LRQ
PRAABKIV, IB>SFKD > ILQ LC CLLA CLO LKB I>ODB ?ILLM TEL E>MMBKP QL ?B QEBOB QL B>Q >KA
>IILTFKD > JFKF-MLMRI>QFLK LC I>ODB ?ILLMP QL PRPQ>FK QEBJPBISBP CLO > MBOFLA LC QFJB FK LKB
IL@>QFLK.
4EFP BU>JMIB FP O>QEBO PFJMIFPQF@ DFSBK FQP PFKDIB DBKB >KA >PBUR>I OBMOLAR@QFLK. (BOB >OB
PLJB PRDDBPQFLKP CLO ELT VLR JFDEQ >MMIV QEB ?ILLM BU>JMIB FK > JLOB BI>?LO>QB
B@LPVPQBJ PFJRI>QFLK.
Creating the population @IL (CHN C = 0; C < HOG; C++) U
.3?=NIL FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN));
",A >H; = H?Q ",A();
<FIIJM.;>>(H?Q FIIJ(F,>H;));
W
W
PIC> LOH() U
@II>.LOH();
@IL (CHN C = <FIIJM.MCT?()-1; C >= 0; C--) U
The bloops live their life. FIIJ < = <FIIJM.A?N(C);
<.LOH();
<.?;N(@II>);
If one dies, it is removed from the
population and food is added at its
location.
C@ (<.>?;>()) U
<FIIJM.L?GIP?(C);
@II>.;>>(<.FI=;NCIH);
W
Here is where each living bloop has a
chance to reproduce. As long as a child is
made (i.e. not null) it is added to the
population.
FIIJ =BCF> = <.L?JLI>O=?();
C@ (=BCF> != HOFF) <FIIJM.;>>(=BCF>);
W
W
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
442
The Ecosystem Project The Ecosystem Project
Step 9 Exercise:
Add evolution to your ecosystem, building from the examples in this chapter.
Add a population of predators to your ecosystem. Biological evolution
between predators and prey (or parasites and hosts) is often referred to as
an arms race, in which the creatures continuously adapt and counter-
adapt to each other. Can you achieve this behavior in a system of multiple
creatures?
How would you implement crossover and mutation between two parents in
an ecosystem modeled after the bloops? Try implementing an algorithm so
that two creatures meet and mate when within a certain proximity. Can
you make creatures with gender?
Try using the weights of multiple steering forces as a creatures DNA. Can
you create a scenario in which creatures evolve to cooperate with each
other?
One of the greatest challenges in ecosystem simulations is achieving a
nice balance. You will likely find that most of your attempts result in either
mass overpopulation (followed by mass extinction) or simply mass
extinction straight away. What techniques can you employ to achieve
balance? Consider using the genetic algorithm itself to evolve optimal
parameters for an ecosystem.
4EB .>QROB LC #LAB (S005)
443
Chapter 10. Neural Chapter 10. Neural
Networks Networks
I-<B 0.;KA =?<02@@ :2 D6A5 . ;<?:.9 /?.6;.J
Charlie Sheen
7BfOB >Q QEB BKA LC LRO PQLOV. 4EFP FP QEB I>PQ LCCF@F>I @E>MQBO LC QEFP ?LLH (QELRDE ) BKSFPFLK
>AAFQFLK>I PRMMIBJBKQ>I J>QBOF>I CLO QEB TB?PFQB >KA MBOE>MP KBT @E>MQBOP FK QEB CRQROB).
7B ?BD>K TFQE FK>KFJ>QB L?GB@QP IFSFKD FK > TLOIA LC CLO@BP >KA D>SB QELPB L?GB@QP ABPFOBP,
>RQLKLJV, >KA QEB >?FIFQV QL Q>HB >@QFLK >@@LOAFKD QL > PVPQBJ LC ORIBP. .BUQ, TB >IILTBA
QELPB L?GB@QP QL IFSB FK > MLMRI>QFLK >KA BSLISB LSBO QFJB. .LT TB >PH: 7E>Q FP B>@E
L?GB@QfP AB@FPFLK-J>HFKD MOL@BPP? (LT @>K FQ >AGRPQ FQP @ELF@BP ?V IB>OKFKD LSBO QFJB? #>K
> @LJMRQ>QFLK>I BKQFQV MOL@BPP FQP BKSFOLKJBKQ >KA DBKBO>QB > AB@FPFLK?
4EB ERJ>K ?O>FK @>K ?B ABP@OF?BA >P > ?FLILDF@>I KBRO>I KBQTLOHd>K FKQBO@LKKB@QBA TB?
LC KBROLKP QO>KPJFQQFKD BI>?LO>QB M>QQBOKP LC BIB@QOF@>I PFDK>IP. $BKAOFQBP OB@BFSB FKMRQ
PFDK>IP >KA, ?>PBA LK QELPB FKMRQP, CFOB >K LRQMRQ PFDK>I SF> >K >ULK. /O PLJBQEFKD IFHB QE>Q.
(LT QEB ERJ>K ?O>FK >@QR>IIV TLOHP FP >K BI>?LO>QB >KA @LJMIBU JVPQBOV, LKB QE>Q TB
@BOQ>FKIV >OB KLQ DLFKD QL >QQBJMQ QL Q>@HIB FK OFDLOLRP ABQ>FI FK QEFP @E>MQBO.
#E>MQBO 10. .BRO>I .BQTLOHP
444
4EB DLLA KBTP FP QE>Q ABSBILMFKD BKD>DFKD >KFJ>QBA PVPQBJP TFQE @LAB ALBP KLQ OBNRFOB
P@FBKQFCF@ OFDLO LO >@@RO>@V, >P TBfSB IB>OKBA QEOLRDELRQ QEFP ?LLH. 7B @>K PFJMIV ?B FKPMFOBA
?V QEB FAB> LC ?O>FK CRK@QFLK.
)K QEFP @E>MQBO, TBfII ?BDFK TFQE > @LK@BMQR>I LSBOSFBT LC QEB MOLMBOQFBP >KA CB>QROBP LC KBRO>I
KBQTLOHP >KA ?RFIA QEB PFJMIBPQ MLPPF?IB BU>JMIB LC LKB (> KBQTLOH QE>Q @LKPFPQP LC > PFKDIB
KBROLK). !CQBOT>OAP, TBfII BU>JFKB PQO>QBDFBP CLO @OB>QFKD > g"O>FKh L?GB@Q QE>Q @>K ?B FKPBOQBA
FKQL LRO 3?BC=F? @I>PP >KA RPBA QL ABQBOJFKB PQBBOFKD. &FK>IIV, TBfII >IPL ILLH >Q QB@EKFNRBP
CLO SFPR>IFWFKD >KA >KFJ>QFKD > KBQTLOH LC KBROLKP.
F64B?2 10.1
10.1 Artificial Neural Networks: Introduction and 10.1 Artificial Neural Networks: Introduction and
Application Application
#LJMRQBO P@FBKQFPQP E>SB ILKD ?BBK FKPMFOBA ?V QEB ERJ>K ?O>FK. )K 1943, 7>OOBK 3.
-@#RIIL@E, > KBROLP@FBKQFPQ, >KA 7>IQBO 0FQQP, > ILDF@F>K, ABSBILMBA QEB CFOPQ @LK@BMQR>I JLABI
LC >K >OQFCF@F>I KBRO>I KBQTLOH. )K QEBFO M>MBO, "! ILDF@>I @>I@RIRP LC QEB FAB>P FJJFKBKQ FK
KBOSLRP >@QFSFQV,h QEBV ABP@OF?B QEB @LK@BMQ LC > KBROLK, > PFKDIB @BII IFSFKD FK > KBQTLOH LC
@BIIP QE>Q OB@BFSBP FKMRQP, MOL@BPPBP QELPB FKMRQP, >KA DBKBO>QBP >K LRQMRQ.
4EBFO TLOH, >KA QEB TLOH LC J>KV P@FBKQFPQP >KA OBPB>O@EBOP QE>Q CLIILTBA, T>P KLQ JB>KQ QL
>@@RO>QBIV ABP@OF?B ELT QEB ?FLILDF@>I ?O>FK TLOHP. 2>QEBO, >K >OQFCF@F>I KBRO>I KBQTLOH (TEF@E
TB TFII KLT PFJMIV OBCBO QL >P > gKBRO>I KBQTLOHh) T>P ABPFDKBA >P > @LJMRQ>QFLK>I JLABI
?>PBA LK QEB ?O>FK QL PLISB @BOQ>FK HFKAP LC MOL?IBJP.
)QfP MOL?>?IV MOBQQV L?SFLRP QL VLR QE>Q QEBOB >OB MOL?IBJP QE>Q >OB FK@OBAF?IV PFJMIB CLO >
@LJMRQBO QL PLISB, ?RQ AFCCF@RIQ CLO VLR. 4>HB QEB PNR>OB OLLQ LC 964,324, CLO BU>JMIB. ! NRF@H
IFKB LC @LAB MOLAR@BP QEB S>IRB 982, > KRJ?BO 0OL@BPPFKD @LJMRQBA FK IBPP QE>K >
JFIIFPB@LKA. 4EBOB >OB, LK QEB LQEBO E>KA, MOL?IBJP QE>Q >OB FK@OBAF?IV PFJMIB CLO VLR LO JB QL
PLISB, ?RQ KLQ PL B>PV CLO > @LJMRQBO. 3ELT >KV QLAAIBO > MF@QROB LC > HFQQBK LO MRMMV >KA
QEBVfII ?B >?IB QL QBII VLR SBOV NRF@HIV TEF@E LKB FP TEF@E. 3>V EBIIL >KA PE>HB JV E>KA LKB
JLOKFKD >KA VLR PELRIA ?B >?IB QL MF@H JB LRQ LC > @OLTA LC MBLMIB QEB KBUQ A>V. "RQ KBBA >
J>@EFKB QL MBOCLOJ LKB LC QEBPB Q>PHP? 3@FBKQFPQP E>SB >IOB>AV PMBKQ BKQFOB @>OBBOP
OBPB>O@EFKD >KA FJMIBJBKQFKD @LJMIBU PLIRQFLKP.
4EB .>QROB LC #LAB (S005)
445
4EB JLPQ @LJJLK >MMIF@>QFLK LC KBRO>I KBQTLOHP FK @LJMRQFKD QLA>V FP QL MBOCLOJ LKB LC
QEBPB gB>PV-CLO->-ERJ>K, AFCCF@RIQ-CLO->-J>@EFKBh Q>PHP, LCQBK OBCBOOBA QL >P M>QQBOK
OB@LDKFQFLK. !MMIF@>QFLKP O>KDB COLJ LMQF@>I @E>O>@QBO OB@LDKFQFLK (QROKFKD MOFKQBA LO
E>KATOFQQBK P@>KP FKQL AFDFQ>I QBUQ) QL C>@F>I OB@LDKFQFLK. 7B ALKfQ E>SB QEB QFJB LO KBBA QL
RPB PLJB LC QEBPB JLOB BI>?LO>QB >OQFCF@F>I FKQBIIFDBK@B >IDLOFQEJP EBOB, ?RQ FC VLR >OB
FKQBOBPQBA FK OBPB>O@EFKD KBRO>I KBQTLOHP, )fA OB@LJJBKA QEB ?LLHP AFH=:=7=5@ %BH9@@=;9B79: A
)C89FB ADDFC57< ?V 3QR>OQ *. 2RPPBII >KA 0BQBO .LOSFD >KA A% :CF #5A9 9J9@CD9FG ?V
$>SFA -. "LROD >KA 'IBKK 3BBJ>KK.
! KBRO>I KBQTLOH FP > g@LKKB@QFLKFPQh
@LJMRQ>QFLK>I PVPQBJ. 4EB @LJMRQ>QFLK>I
PVPQBJP TB TOFQB >OB MOL@BARO>I; >
MOLDO>J PQ>OQP >Q QEB CFOPQ IFKB LC @LAB,
BUB@RQBP FQ, >KA DLBP LK QL QEB KBUQ,
CLIILTFKD FKPQOR@QFLKP FK > IFKB>O C>PEFLK. !
QORB KBRO>I KBQTLOH ALBP KLQ CLIILT > IFKB>O
M>QE. 2>QEBO, FKCLOJ>QFLK FP MOL@BPPBA
@LIIB@QFSBIV, FK M>O>IIBI QEOLRDELRQ >
KBQTLOH LC KLABP (QEB KLABP, FK QEFP @>PB,
?BFKD KBROLKP).
(BOB TB E>SB VBQ >KLQEBO BU>JMIB LC >
@LJMIBU PVPQBJ, JR@E IFHB QEB LKBP TB
BU>JFKBA FK #E>MQBOP 6, 7, >KA 8. 4EB
FKAFSFAR>I BIBJBKQP LC QEB KBQTLOH, QEB KBROLKP, >OB PFJMIB. 4EBV OB>A >K FKMRQ, MOL@BPP FQ,
>KA DBKBO>QB >K LRQMRQ. ! KBQTLOH LC J>KV KBROLKP, ELTBSBO, @>K BUEF?FQ FK@OBAF?IV OF@E
>KA FKQBIIFDBKQ ?BE>SFLOP.
/KB LC QEB HBV BIBJBKQP LC > KBRO>I KBQTLOH FP FQP >?FIFQV QL @95FB. ! KBRO>I KBQTLOH FP KLQ
GRPQ > @LJMIBU PVPQBJ, ?RQ > @LJMIBU ,/,;?4A0 ,/,;?4A0 PVPQBJ, JB>KFKD FQ @>K @E>KDB FQP FKQBOK>I
PQOR@QROB ?>PBA LK QEB FKCLOJ>QFLK CILTFKD QEOLRDE FQ. 4VMF@>IIV, QEFP FP >@EFBSBA QEOLRDE QEB
>AGRPQFKD LC K9=;<HG. )K QEB AF>DO>J >?LSB, B>@E IFKB OBMOBPBKQP > @LKKB@QFLK ?BQTBBK QTL
KBROLKP >KA FKAF@>QBP QEB M>QET>V CLO QEB CILT LC FKCLOJ>QFLK. %>@E @LKKB@QFLK E>P >
B0423? B0423?, > KRJ?BO QE>Q @LKQOLIP QEB PFDK>I ?BQTBBK QEB QTL KBROLKP. )C QEB KBQTLOH
DBKBO>QBP > gDLLAh LRQMRQ (TEF@E TBfII ABCFKB I>QBO), QEBOB FP KL KBBA QL >AGRPQ QEB TBFDEQP.
(LTBSBO, FC QEB KBQTLOH DBKBO>QBP > gMLLOh LRQMRQd>K BOOLO, PL QL PMB>HdQEBK QEB PVPQBJ
>A>MQP, >IQBOFKD QEB TBFDEQP FK LOABO QL FJMOLSB PR?PBNRBKQ OBPRIQP.
4EBOB >OB PBSBO>I PQO>QBDFBP CLO IB>OKFKD, >KA TBfII BU>JFKB QTL LC QEBJ FK QEFP @E>MQBO.
i '@;0=A4>0/ !0,=9492 '@;0=A4>0/ !0,=9492 d%PPBKQF>IIV, > PQO>QBDV QE>Q FKSLISBP > QB>@EBO QE>Q FP
PJ>OQBO QE>K QEB KBQTLOH FQPBIC. &LO BU>JMIB, IBQfP Q>HB QEB C>@F>I OB@LDKFQFLK
BU>JMIB. 4EB QB>@EBO PELTP QEB KBQTLOH > ?RK@E LC C>@BP, >KA QEB QB>@EBO
>IOB>AV HKLTP QEB K>JB >PPL@F>QBA TFQE B>@E C>@B. 4EB KBQTLOH J>HBP FQP
DRBPPBP, QEBK QEB QB>@EBO MOLSFABP QEB KBQTLOH TFQE QEB >KPTBOP. 4EB KBQTLOH
@>K QEBK @LJM>OB FQP >KPTBOP QL QEB HKLTK g@LOOB@Qh LKBP >KA J>HB >AGRPQJBKQP
F64B?2 10.2
#E>MQBO 10. .BRO>I .BQTLOHP
446
>@@LOAFKD QL FQP BOOLOP. /RO CFOPQ KBRO>I KBQTLOH FK QEB KBUQ PB@QFLK TFII CLIILT QEFP
JLABI.
i )9>@;0=A4>0/ !0,=9492 )9>@;0=A4>0/ !0,=9492 d2BNRFOBA TEBK QEBOB FPKfQ >K BU>JMIB A>Q> PBQ TFQE HKLTK
>KPTBOP. )J>DFKB PB>O@EFKD CLO > EFAABK M>QQBOK FK > A>Q> PBQ. !K >MMIF@>QFLK LC QEFP
FP @IRPQBOFKD, F.B. AFSFAFKD > PBQ LC BIBJBKQP FKQL DOLRMP >@@LOAFKD QL PLJB RKHKLTK
M>QQBOK. 7B TLKfQ ?B ILLHFKD >Q >KV BU>JMIBP LC RKPRMBOSFPBA IB>OKFKD FK QEFP
@E>MQBO, >P QEFP PQO>QBDV FP IBPP OBIBS>KQ CLO LRO BU>JMIBP.
i &0491:=.0809? !0,=9492 &0491:=.0809? !0,=9492 d! PQO>QBDV ?RFIQ LK L?PBOS>QFLK. 4EFKH LC > IFQQIB JLRPB
ORKKFKD QEOLRDE > J>WB. )C FQ QROKP IBCQ, FQ DBQP > MFB@B LC @EBBPB; FC FQ QROKP OFDEQ, FQ
OB@BFSBP > IFQQIB PEL@H. ($LKfQ TLOOV, QEFP FP GRPQ > MOBQBKA JLRPB.) 0OBPRJ>?IV, QEB
JLRPB TFII IB>OK LSBO QFJB QL QROK IBCQ. )QP KBRO>I KBQTLOH J>HBP > AB@FPFLK TFQE >K
LRQ@LJB (QROK IBCQ LO OFDEQ) >KA L?PBOSBP FQP BKSFOLKJBKQ (VRJ LO LR@E). )C QEB
L?PBOS>QFLK FP KBD>QFSB, QEB KBQTLOH @>K >AGRPQ FQP TBFDEQP FK LOABO QL J>HB >
AFCCBOBKQ AB@FPFLK QEB KBUQ QFJB. 2BFKCLO@BJBKQ IB>OKFKD FP @LJJLK FK OL?LQF@P. !Q
QFJB N, QEB OL?LQ MBOCLOJP > Q>PH >KA L?PBOSBP QEB OBPRIQP. $FA FQ @O>PE FKQL > T>II LO
C>II LCC > Q>?IB? /O FP FQ RKE>OJBA? 7BfII ILLH >Q OBFKCLO@BJBKQ IB>OKFKD FK QEB @LKQBUQ
LC LRO PFJRI>QBA PQBBOFKD SBEF@IBP.
4EFP >?FIFQV LC > KBRO>I KBQTLOH QL IB>OK, QL J>HB >AGRPQJBKQP QL FQP PQOR@QROB LSBO QFJB, FP TE>Q
J>HBP FQ PL RPBCRI FK QEB CFBIA LC >OQFCF@F>I FKQBIIFDBK@B. (BOB >OB PLJB PQ>KA>OA RPBP LC KBRO>I
KBQTLOHP FK PLCQT>OB QLA>V.
i %,??0=9 &0.:294?4:9 %,??0=9 &0.:294?4:9 d7BfSB JBKQFLKBA QEFP PBSBO>I QFJBP >IOB>AV >KA FQfP MOL?>?IV
QEB JLPQ @LJJLK >MMIF@>QFLK. %U>JMIBP >OB C>@F>I OB@LDKFQFLK, LMQF@>I @E>O>@QBO
OB@LDKFQFLK, BQ@.
i (480 '0=40> %=0/4.?4:9 (480 '0=40> %=0/4.?4:9 d.BRO>I KBQTLOHP @>K ?B RPBA QL J>HB MOBAF@QFLKP. 7FII QEB
PQL@H OFPB LO C>II QLJLOOLT? 7FII FQ O>FK LO ?B PRKKV?
i '429,7 %=:.0>>492 '429,7 %=:.0>>492 d#L@EIB>O FJMI>KQP >KA EB>OFKD >FAP KBBA QL CFIQBO LRQ
RKKB@BPP>OV KLFPB >KA >JMIFCV QEB FJMLOQ>KQ PLRKAP. .BRO>I KBQTLOHP @>K ?B
QO>FKBA QL MOL@BPP >K >RAFL PFDK>I >KA CFIQBO FQ >MMOLMOF>QBIV.
i C:9?=:7 C:9?=:7 d9LR J>V E>SB OB>A >?LRQ OB@BKQ OBPB>O@E >AS>K@BP FK PBIC-AOFSFKD @>OP.
.BRO>I KBQTLOHP >OB LCQBK RPBA QL J>K>DB PQBBOFKD AB@FPFLKP LC MEVPF@>I SBEF@IBP
(LO PFJRI>QBA LKBP).
i ':1? '09>:=> ':1? '09>:=> d! PLCQ PBKPLO OBCBOP QL QEB MOL@BPP LC >K>IVWFKD > @LIIB@QFLK LC J>KV
JB>PROBJBKQP. ! QEBOJLJBQBO @>K QBII VLR QEB QBJMBO>QROB LC QEB >FO, ?RQ TE>Q FC
VLR >IPL HKBT QEB ERJFAFQV, ?>OLJBQOF@ MOBPPROB, ABTMLFKQ, >FO NR>IFQV, >FO ABKPFQV,
BQ@.? .BRO>I KBQTLOHP @>K ?B BJMILVBA QL MOL@BPP QEB FKMRQ A>Q> COLJ J>KV
FKAFSFAR>I PBKPLOP >KA BS>IR>QB QEBJ >P > TELIB.
i A9:8,7D D0?0.?4:9 A9:8,7D D0?0.?4:9 d"B@>RPB KBRO>I KBQTLOHP >OB PL DLLA >Q OB@LDKFWFKD M>QQBOKP,
QEBV @>K >IPL ?B QO>FKBA QL DBKBO>QB >K LRQMRQ TEBK PLJBQEFKD L@@ROP QE>Q ALBPKfQ
4EB .>QROB LC #LAB (S005)
447
CFQ QEB M>QQBOK. 4EFKH LC > KBRO>I KBQTLOH JLKFQLOFKD VLRO A>FIV OLRQFKB LSBO > ILKD
MBOFLA LC QFJB. !CQBO IB>OKFKD QEB M>QQBOKP LC VLRO ?BE>SFLO, FQ @LRIA >IBOQ VLR TEBK
PLJBQEFKD FP >JFPP.
4EFP FP ?V KL JB>KP > @LJMOBEBKPFSB IFPQ LC >MMIF@>QFLKP LC KBRO>I KBQTLOHP. "RQ ELMBCRIIV FQ
DFSBP VLR >K LSBO>II PBKPB LC QEB CB>QROBP >KA MLPPF?FIFQFBP. 4EB QEFKD FP, KBRO>I KBQTLOHP
>OB @LJMIF@>QBA >KA AFCCF@RIQ. 4EBV FKSLISB >II PLOQP LC C>K@V J>QEBJ>QF@P. 7EFIB QEFP FP >II
C>P@FK>QFKD (>KA FK@OBAF?IV FJMLOQ>KQ QL P@FBKQFCF@ OBPB>O@E), > ILQ LC QEB QB@EKFNRBP >OB KLQ
SBOV MO>@QF@>I FK QEB TLOIA LC ?RFIAFKD FKQBO>@QFSB, >KFJ>QBA 0OL@BPPFKD PHBQ@EBP. .LQ QL
JBKQFLK QE>Q FK LOABO QL @LSBO >II QEFP J>QBOF>I, TB TLRIA KBBA >KLQEBO ?LLHdLO JLOB IFHBIV,
> PBOFBP LC ?LLHP.
3L FKPQB>A, TBfII ?BDFK LRO I>PQ EROO>E FK QEB K>QROB LC @LAB TFQE QEB PFJMIBPQ LC >II KBRO>I
KBQTLOHP, FK >K BCCLOQ QL RKABOPQ>KA ELT QEB LSBO>II @LK@BMQP >OB >MMIFBA FK @LAB. 4EBK
TBfII ILLH >Q PLJB 0OL@BPPFKD PHBQ@EBP QE>Q DBKBO>QB SFPR>I OBPRIQP FKPMFOBA ?V QEBPB
@LK@BMQP.
10.2 The Perceptron 10.2 The Perceptron
)KSBKQBA FK 1957 ?V &O>KH 2LPBK?I>QQ >Q QEB #LOKBII !BOLK>RQF@>I ,>?LO>QLOV, > MBO@BMQOLK FP
QEB PFJMIBPQ KBRO>I KBQTLOH MLPPF?IB: > @LJMRQ>QFLK>I JLABI LC > PFKDIB KBROLK. !
MBO@BMQOLK @LKPFPQP LC LKB LO JLOB FKMRQP, > MOL@BPPLO, >KA > PFKDIB LRQMRQ.
! MBO@BMQOLK CLIILTP QEB gCBBA-CLOT>OAh JLABI, JB>KFKD FKMRQP >OB PBKQ FKQL QEB KBROLK,
>OB MOL@BPPBA, >KA OBPRIQ FK >K LRQMRQ. )K QEB AF>DO>J >?LSB, QEFP JB>KP QEB KBQTLOH (LKB
KBROLK) OB>AP COLJ IBCQ QL OFDEQ: FKMRQP @LJB FK, LRQMRQ DLBP LRQ.
,BQfP CLIILT B>@E LC QEBPB PQBMP FK JLOB ABQ>FI.
3QBM 1: 2B@BFSB FKMRQP.
3>V TB E>SB > MBO@BMQOLK TFQE QTL FKMRQPdIBQfP @>II QEBJ L1 >KA L2.
F64B?2 10.3: *52 =2?02=A?<;
#E>MQBO 10. .BRO>I .BQTLOHP
448
%DFKJ 0: N1 = 12
%DFKJ 1: N2 = 4
3QBM 2: 7BFDEQ FKMRQP.
%>@E FKMRQ QE>Q FP PBKQ FKQL QEB KBROLK JRPQ CFOPQ ?B TBFDEQBA, F.B. JRIQFMIFBA ?V PLJB S>IRB
(LCQBK > KRJ?BO ?BQTBBK -1 >KA 1). 7EBK @OB>QFKD > MBO@BMQOLK, TBfII QVMF@>IIV ?BDFK ?V
>PPFDKFKD O>KALJ TBFDEQP. (BOB, IBQfP DFSB QEB FKMRQP QEB CLIILTFKD TBFDEQP:
1;?=>J 0: 0.5
1;?=>J 1: -1
7B Q>HB B>@E FKMRQ >KA JRIQFMIV FQ ?V FQP TBFDEQ.
%DFKJ 0 * 1;?=>J 0 Y 12 * 0.5 = 6
%DFKJ 1 * 1;?=>J 1 Y 4 * -1 = -4
3QBM 3: 3RJ FKMRQP.
4EB TBFDEQBA FKMRQP >OB QEBK PRJJBA.
-KC = 6 + -4 = 2
3QBM 4: 'BKBO>QB LRQMRQ.
4EB LRQMRQ LC > MBO@BMQOLK FP DBKBO>QBA ?V M>PPFKD QE>Q PRJ QEOLRDE >K >@QFS>QFLK CRK@QFLK. )K
QEB @>PB LC > PFJMIB ?FK>OV LRQMRQ, QEB >@QFS>QFLK CRK@QFLK FP TE>Q QBIIP QEB MBO@BMQOLK TEBQEBO
QL gCFOBh LO KLQ. 9LR @>K BKSFPFLK >K ,%$ @LKKB@QBA QL QEB LRQMRQ PFDK>I: FC FQ CFOBP, QEB IFDEQ
DLBP LK; FC KLQ, FQ PQ>VP LCC.
!@QFS>QFLK CRK@QFLKP @>K DBQ > IFQQIB ?FQ E>FOV. )C VLR PQ>OQ OB>AFKD LKB LC QELPB >OQFCF@F>I
FKQBIIFDBK@B QBUQ?LLHP ILLHFKD CLO JLOB FKCL >?LRQ >@QFS>QFLK CRK@QFLKP, VLR J>V PLLK CFKA
VLROPBIC OB>@EFKD CLO > @>I@RIRP QBUQ?LLH. (LTBSBO, TFQE LRO COFBKA QEB PFJMIB MBO@BMQOLK,
TBfOB DLFKD QL AL PLJBQEFKD OB>IIV B>PV. ,BQfP J>HB QEB >@QFS>QFLK CRK@QFLK QEB PFDK LC QEB
PRJ. )K LQEBO TLOAP, FC QEB PRJ FP > MLPFQFSB KRJ?BO, QEB LRQMRQ FP 1; FC FQ FP KBD>QFSB, QEB LRQMRQ
FP -1.
*KJFKJ = I?=D(IKC) Y I?=D(2) Y +1
4EB .>QROB LC #LAB (S005)
449
,BQfP OBSFBT >KA @LKABKPB QEBPB PQBMP PL TB @>K FJMIBJBKQ QEBJ TFQE > @LAB PKFMMBQ.
(30 %0=.0;?=:9 A72:=4?38: (30 %0=.0;?=:9 A72:=4?38:
1. &LO BSBOV FKMRQ, JRIQFMIV QE>Q FKMRQ ?V FQP TBFDEQ.
2. 3RJ >II LC QEB TBFDEQBA FKMRQP.
3. #LJMRQB QEB LRQMRQ LC QEB MBO@BMQOLK ?>PBA LK QE>Q PRJ M>PPBA QEOLRDE >K
>@QFS>QFLK CRK@QFLK (QEB PFDK LC QEB PRJ).
,BQfP >PPRJB TB E>SB QTL >OO>VP LC KRJ?BOP, QEB FKMRQP >KA QEB TBFDEQP. &LO BU>JMIB:
g&LO BSBOV FKMRQh FJMIFBP > ILLM QE>Q JRIQFMIFBP B>@E FKMRQ ?V FQP @LOOBPMLKAFKD TBFDEQ.
3FK@B TB KBBA QEB PRJ, TB @>K >AA RM QEB OBPRIQP FK QE>Q SBOV ILLM.
/K@B TB E>SB QEB PRJ TB @>K @LJMRQB QEB LRQMRQ.
@FI;N89 CHJONM = U12 , 4W;
@FI;N89 Q?CABNM = U0.5,-1W;
Steps 1 and 2: Add up all the weighted
inputs.
@FI;N MOG = 0;
@IL (CHN C = 0; C < CHJONM.F?HANB; C++) U
MOG += CHJONM8C9*Q?CABNM8C9;
W
Step 3: Passing the sum through an
activation function
@FI;N IONJON = ;=NCP;N?(MOG);
The activation function CHN ;=NCP;N?(@FI;N MOG) U
Return a 1 if positive, -1 if negative. C@ (MOG > 0) L?NOLH 1;
?FM? L?NOLH -1;
W
10.3 Simple Pattern Recognition Using a 10.3 Simple Pattern Recognition Using a
Perceptron Perceptron
.LT QE>Q TB RKABOPQ>KA QEB @LJMRQ>QFLK>I MOL@BPP LC > MBO@BMQOLK, TB @>K ILLH >Q >K
BU>JMIB LC LKB FK >@QFLK. 7B PQ>QBA QE>Q KBRO>I KBQTLOHP >OB LCQBK RPBA CLO M>QQBOK
OB@LDKFQFLK >MMIF@>QFLKP, PR@E >P C>@F>I OB@LDKFQFLK. %SBK PFJMIB MBO@BMQOLKP @>K
ABJLKPQO>QB QEB ?>PF@P LC @I>PPFCF@>QFLK, >P FK QEB CLIILTFKD BU>JMIB.
#E>MQBO 10. .BRO>I .BQTLOHP
450
#LKPFABO > IFKB FK QTL-AFJBKPFLK>I PM>@B.
0LFKQP FK QE>Q PM>@B @>K ?B @I>PPFCFBA >P
IFSFKD LK BFQEBO LKB PFAB LC QEB IFKB LO QEB
LQEBO. 7EFIB QEFP FP > PLJBTE>Q PFIIV BU>JMIB
(PFK@B QEBOB FP @IB>OIV KL KBBA CLO > KBRO>I
KBQTLOH; TB @>K ABQBOJFKB LK TEF@E PFAB >
MLFKQ IFBP TFQE PLJB PFJMIB >IDB?O>), FQ
PELTP ELT > MBO@BMQOLK @>K ?B QO>FKBA QL
OB@LDKFWB MLFKQP LK LKB PFAB SBOPRP
>KLQEBO.
,BQfP P>V > MBO@BMQOLK E>P 2 FKMRQP (QEB U-
>KA V-@LLOAFK>QBP LC > MLFKQ). 5PFKD > PFDK >@QFS>QFLK CRK@QFLK, QEB LRQMRQ TFII BFQEBO ?B -1 LO
1dF.B., QEB FKMRQ A>Q> FP @I>PPFCFBA >@@LOAFKD QL QEB PFDK LC QEB LRQMRQ. )K QEB >?LSB AF>DO>J,
TB @>K PBB ELT B>@E MLFKQ FP BFQEBO ?BILT QEB IFKB (-1) LO >?LSB (+1).
4EB MBO@BMQOLK FQPBIC @>K ?B AF>DO>JJBA >P CLIILTP:
7B @>K PBB ELT QEBOB >OB QTL FKMRQP (L >KA M), > TBFDEQ CLO B>@E FKMRQ (K9=;<H
L
>KA K9=;<H
M
),
>P TBII >P > MOL@BPPFKD KBROLK QE>Q DBKBO>QBP QEB LRQMRQ.
4EBOB FP > MOBQQV PFDKFCF@>KQ MOL?IBJ EBOB, ELTBSBO. ,BQfP @LKPFABO QEB MLFKQ (0,0). 7E>Q FC TB
PBKA QEFP MLFKQ FKQL QEB MBO@BMQOLK >P FQP FKMRQ: U = 0 >KA V = 0? 7E>Q TFII QEB PRJ LC FQP
TBFDEQBA FKMRQP ?B? .L J>QQBO TE>Q QEB TBFDEQP >OB, QEB PRJ TFII >IT>VP ?B 0! "RQ QEFP @>KfQ
?B OFDEQd>CQBO >II, QEB MLFKQ (0,0) @LRIA @BOQ>FKIV ?B >?LSB LO ?BILT S>OFLRP IFKBP FK LRO QTL-
AFJBKPFLK>I TLOIA.
4L >SLFA QEFP AFIBJJ>, LRO MBO@BMQOLK TFII OBNRFOB > QEFOA FKMRQ, QVMF@>IIV OBCBOOBA QL >P > -4,> -4,>
FKMRQ. ! ?F>P FKMRQ >IT>VP E>P QEB S>IRB LC 1 >KA FP >IPL TBFDEQBA. (BOB FP LRO MBO@BMQOLK TFQE
QEB >AAFQFLK LC QEB ?F>P:
F64B?2 10.4
F64B?2 10.5
4EB .>QROB LC #LAB (S005)
451
,BQfP DL ?>@H QL QEB MLFKQ (0,0). (BOB >OB LRO FKMRQP:
0 * M;?=>J <EH N = 0
0 * M;?=>J <EH O = 0
1 * M;?=>J <EH 8?7I = M;?=>J <EH 8?7I
4EB LRQMRQ FP QEB PRJ LC QEB >?LSB QEOBB S>IRBP, 0 MIRP 0 MIRP QEB ?F>PfP TBFDEQ. 4EBOBCLOB,
QEB ?F>P, LK FQP LTK, >KPTBOP QEB NRBPQFLK >P QL TEBOB (0,0) FP FK OBI>QFLK QL QEB IFKB. )C QEB
?F>PfP TBFDEQ FP MLPFQFSB, (0,0) FP >?LSB QEB IFKB; KBD>QFSB, FQ FP ?BILT. )Q g?F>PBPh QEB
MBO@BMQOLKfP RKABOPQ>KAFKD LC QEB IFKBfP MLPFQFLK OBI>QFSB QL (0,0).
F64B?2 10.6
10.4 Coding the Perceptron 10.4 Coding the Perceptron
7BfOB KLT OB>AV QL >PPBJ?IB QEB @LAB CLO > .?L=?JNLIH @I>PP. 4EB LKIV A>Q> QEB
MBO@BMQOLK KBBAP QL QO>@H >OB QEB FKMRQ TBFDEQP, >KA TB @LRIA RPB >K >OO>V LC CIL>QP QL PQLOB
QEBPB.
4EB @LKPQOR@QLO @LRIA OB@BFSB >K >ODRJBKQ FKAF@>QFKD QEB KRJ?BO LC FKMRQP (FK QEFP @>PB
QEOBB: U, V, >KA > ?F>P) >KA PFWB QEB >OO>V >@@LOAFKDIV.
! MBO@BMQOLK KBBAP QL ?B >?IB QL OB@BFSB FKMRQP >KA DBKBO>QB >K LRQMRQ. 7B @>K M>@H>DB
QEBPB OBNRFOBJBKQP FKQL > CRK@QFLK @>IIBA @??>@ILQ;L>(). )K QEFP BU>JMIB, TBfII E>SB QEB
=F;MM .?L=?JNLIH U
@FI;N89 Q?CABNM;
.?L=?JNLIH(CHN H) U
Q?CABNM = H?Q @FI;N8H9;
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
The weights are picked randomly to start. Q?CABNM8C9 = L;H>IG(-1,1);
W
W
#E>MQBO 10. .BRO>I .BQTLOHP
452
MBO@BMQOLK OB@BFSB FQP FKMRQP >P >K >OO>V (TEF@E PELRIA ?B QEB P>JB IBKDQE >P QEB >OO>V LC
TBFDEQP) >KA OBQROK QEB LRQMRQ >P >K FKQBDBO.
0OBPRJ>?IV, TB @LRIA KLT @OB>QB > .?L=?JNLIH L?GB@Q >KA >PH FQ QL J>HB > DRBPP CLO >KV
DFSBK MLFKQ.
$FA QEB MBO@BMQOLK DBQ FQ OFDEQ? !Q QEFP MLFKQ, QEB MBO@BMQOLK E>P KL ?BQQBO QE>K > 50/50
@E>K@B LC >OOFSFKD >Q QEB OFDEQ >KPTBO. 2BJBJ?BO, TEBK TB @OB>QBA FQ, TB D>SB B>@E TBFDEQ >
O>KALJ S>IRB. ! KBRO>I KBQTLOH FPKfQ J>DF@. )QfP KLQ DLFKD QL ?B >?IB QL DRBPP >KVQEFKD
@LOOB@QIV RKIBPP TB QB>@E FQ ELT QL!
4L QO>FK > KBRO>I KBQTLOH QL >KPTBO @LOOB@QIV, TBfOB DLFKD QL BJMILV QEB JBQELA LC
GID9FJ=G98 @95FB=B; QE>Q TB ABP@OF?BA FK PB@QFLK 10.1 (PBB M>DB 445).
7FQE QEFP JBQELA, QEB KBQTLOH FP MOLSFABA TFQE FKMRQP CLO TEF@E QEBOB FP > HKLTK >KPTBO. 4EFP
T>V QEB KBQTLOH @>K CFKA LRQ FC FQ E>P J>AB > @LOOB@Q DRBPP. )C FQfP FK@LOOB@Q, QEB KBQTLOH @>K
IB>OK COLJ FQP JFPQ>HB >KA >AGRPQ FQP TBFDEQP. 4EB MOL@BPP FP >P CLIILTP:
1. 0OLSFAB QEB MBO@BMQOLK TFQE FKMRQP CLO TEF@E QEBOB FP > HKLTK >KPTBO.
CHN @??>@ILQ;L>(@FI;N89 CHJONM) U
@FI;N MOG = 0;
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
MOG += CHJONM8C9*Q?CABNM8C9;
W
Result is the sign of the sum, -1 or +1. Here
the perceptron is making a guess. Is it on
one side of the line or the other?
L?NOLH ;=NCP;N?(MOG);
W
F64B?2 10.7
Create the Perceptron. .?L=?JNLIH J = H?Q .?L=?JNLIH(3);
The input is 3 values: x,y and bias. @FI;N89 JICHN = U50,-12,1W;
The answer! CHN L?MOFN = J.@??>@ILQ;L>(JICHN);
4EB .>QROB LC #LAB (S005)
453
2. !PH QEB MBO@BMQOLK QL DRBPP >K >KPTBO.
3. #LJMRQB QEB BOOLO. ($FA FQ DBQ QEB >KPTBO OFDEQ LO TOLKD?)
4. !AGRPQ >II QEB TBFDEQP >@@LOAFKD QL QEB BOOLO.
5. 2BQROK QL 3QBM 1 >KA OBMB>Q!
3QBMP 1 QEOLRDE 4 @>K ?B M>@H>DBA FKQL > CRK@QFLK. "BCLOB TB @>K TOFQB QEB BKQFOB CRK@QFLK,
ELTBSBO, TB KBBA QL BU>JFKB 3QBMP 3 >KA 4 FK JLOB ABQ>FI. (LT AL TB ABCFKB QEB
MBO@BMQOLKfP BOOLO? !KA ELT PELRIA TB >AGRPQ QEB TBFDEQP >@@LOAFKD QL QEFP BOOLO?
4EB MBO@BMQOLKfP BOOLO @>K ?B ABCFKBA >P QEB AFCCBOBK@B ?BQTBBK QEB ABPFOBA >KPTBO >KA FQP
DRBPP.
!,,*, = !-%,! */.+/. - #/!-- */.+/.
4EB >?LSB CLOJRI> J>V ILLH C>JFIF>O QL VLR. )K #E>MQBO 6 (PBB M>DB 263), TB @LJMRQBA >
PQBBOFKD CLO@B >P QEB AFCCBOBK@B ?BQTBBK LRO ABPFOBA SBIL@FQV >KA LRO @ROOBKQ SBIL@FQV.
-.!!,%)# = !-%,! 0!'*C%.3 - C/,,!). 0!'*C%.3
4EFP T>P >IPL >K BOOLO @>I@RI>QFLK. 4EB @ROOBKQ SBIL@FQV >@QP >P > DRBPP >KA QEB BOOLO (QEB
PQBBOFKD CLO@B) QBIIP RP ELT QL >AGRPQ QEB SBIL@FQV FK QEB OFDEQ AFOB@QFLK. )K > JLJBKQ, TBfII
PBB ELT >AGRPQFKD QEB SBEF@IBfP SBIL@FQV QL CLIILT > Q>ODBQ FP GRPQ IFHB >AGRPQFKD QEB TBFDEQP
LC > KBRO>I KBQTLOH QL >OOFSB >Q QEB OFDEQ >KPTBO.
)K QEB @>PB LC QEB MBO@BMQOLK, QEB LRQMRQ E>P LKIV QTL MLPPF?IB S>IRBP: +1 +1 LO -1 -1. 4EFP JB>KP
QEBOB >OB LKIV QEOBB MLPPF?IB BOOLOP.
)C QEB MBO@BMQOLK DRBPPBP QEB @LOOB@Q >KPTBO, QEBK QEB DRBPP BNR>IP QEB ABPFOBA LRQMRQ >KA
QEB BOOLO FP 0. )C QEB @LOOB@Q >KPTBO FP -1 >KA TBfSB DRBPPBA +1, QEBK QEB BOOLO FP -2. )C QEB
@LOOB@Q >KPTBO FP +1 >KA TBfSB DRBPPBA -1, QEBK QEB BOOLO FP +2.
Desired Desired Guess Guess Error Error
-1 -1 0
-1 +1 -2
+1 -1 +2
+1 +1 0
#E>MQBO 10. .BRO>I .BQTLOHP
454
4EB BOOLO FP QEB ABQBOJFKFKD C>@QLO FK ELT QEB MBO@BMQOLKfP TBFDEQP PELRIA ?B >AGRPQBA. &LO
>KV DFSBK TBFDEQ, TE>Q TB >OB ILLHFKD QL @>I@RI>QB FP QEB @E>KDB FK TBFDEQ, LCQBK @>IIBA
OK9=;<H (LO gABIQ>h TBFDEQ, ABIQ> ?BFKD QEB 'OBBH IBQQBO ]).
)!1 1!%#$. = 1!%#$. + U1!%#$.
]TBFDEQ FP @>I@RI>QBA >P QEB BOOLO JRIQFMIFBA ?V QEB FKMRQ.
U1!%#$. = !,,*, * %)+/.
4EBOBCLOB:
)!1 1!%#$. = 1!%#$. + !,,*, * %)+/.
4L RKABOPQ>KA TEV QEFP TLOHP, TB @>K >D>FK OBQROK QL PQBBOFKD (PBB M>DB 263). ! PQBBOFKD
CLO@B FP BPPBKQF>IIV >K BOOLO FK SBIL@FQV. )C TB >MMIV QE>Q CLO@B >P LRO >@@BIBO>QFLK (]SBIL@FQV),
QEBK TB >AGRPQ LRO SBIL@FQV QL JLSB FK QEB @LOOB@Q AFOB@QFLK. 4EFP FP TE>Q TB T>KQ QL AL TFQE
LRO KBRO>I KBQTLOHfP TBFDEQP. 7B T>KQ QL >AGRPQ QEBJ FK QEB OFDEQ AFOB@QFLK, >P ABCFKBA ?V QEB
BOOLO.
7FQE PQBBOFKD, ELTBSBO, TB E>A >K >AAFQFLK>I S>OF>?IB QE>Q @LKQOLIIBA QEB SBEF@IBfP >?FIFQV QL
PQBBO: QEB A5L=AIA :CF79. 7FQE > EFDE J>UFJRJ CLO@B, QEB SBEF@IB T>P >?IB QL >@@BIBO>QB >KA
QROK SBOV NRF@HIV; TFQE > ILTBO CLO@B, QEB SBEF@IB TLRIA Q>HB ILKDBO QL >AGRPQ FQP SBIL@FQV. 4EB
KBRO>I KBQTLOH TFII BJMILV > PFJFI>O PQO>QBDV TFQE > S>OF>?IB @>IIBA QEB gIB>OKFKD @LKPQ>KQ.h
7BfII >AA FK QEB IB>OKFKD @LKPQ>KQ >P CLIILTP:
)!1 1!%#$. = 1!%#$. + !,,*, * %)+/. * '!A,)%)# C*)-.A).
.LQF@B QE>Q > EFDE IB>OKFKD @LKPQ>KQ JB>KP QEB TBFDEQ TFII @E>KDB JLOB AO>PQF@>IIV. 4EFP J>V
EBIM RP >OOFSB >Q > PLIRQFLK JLOB NRF@HIV, ?RQ TFQE PR@E I>ODB @E>KDBP FK TBFDEQ FQfP MLPPF?IB
TB TFII LSBOPELLQ QEB LMQFJ>I TBFDEQP. 7FQE > PJ>II IB>OKFKD @LKPQ>KQ, QEB TBFDEQP TFII ?B
>AGRPQBA PILTIV, OBNRFOFKD JLOB QO>FKFKD QFJB ?RQ >IILTFKD QEB KBQTLOH QL J>HB SBOV PJ>II
>AGRPQJBKQP QE>Q @LRIA FJMOLSB QEB KBQTLOHfP LSBO>II >@@RO>@V.
!PPRJFKD QEB >AAFQFLK LC > S>OF>?IB = CLO QEB IB>OKFKD @LKPQ>KQ, TB @>K KLT TOFQB > QO>FKFKD
CRK@QFLK CLO QEB MBO@BMQOLK CLIILTFKD QEB >?LSB PQBMP.
A new variable is introduced to control the
learning rate.
@FI;N = = 0.01;
Step 1: Provide the inputs and known
answer. These are passed in as arguments
to train().
PIC> NL;CH(@FI;N89 CHJONM, CHN >?MCL?>) U
Step 2: Guess according to those inputs. CHN AO?MM = @??>@ILQ;L>(CHJONM);
Step 3: Compute the error (difference
between answer and guess).
@FI;N ?LLIL = >?MCL?> - AO?MM;
4EB .>QROB LC #LAB (S005)
455
7B @>K KLT PBB QEB .?L=?JNLIH @I>PP >P > TELIB.
4L QO>FK QEB MBO@BMQOLK, TB KBBA > PBQ LC FKMRQP TFQE > HKLTK >KPTBO. 7B @LRIA M>@H>DB
QEFP RM FK > @I>PP IFHB PL:
Step 4: Adjust all the weights according to
the error and learning constant.
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
Q?CABNM8C9 += = * ?LLIL * CHJONM8C9;
W
W
=F;MM .?L=?JNLIH U
The Perceptron stores its weights and
learning constants.
@FI;N89 Q?CABNM;
@FI;N = = 0.01;
.?L=?JNLIH(CHN H) U
Q?CABNM = H?Q @FI;N8H9;
Weights start off random. @IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
Q?CABNM8C9 = L;H>IG(-1,1);
W
W
Return an output based on inputs. CHN @??>@ILQ;L>(@FI;N89 CHJONM) U
@FI;N MOG = 0;
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
MOG += CHJONM8C9*Q?CABNM8C9;
W
L?NOLH ;=NCP;N?(MOG);
W
Output is a +1 or -1. CHN ;=NCP;N?(@FI;N MOG) U
C@ (MOG > 0) L?NOLH 1;
?FM? L?NOLH -1;
W
Train the network against known data. PIC> NL;CH(@FI;N89 CHJONM, CHN >?MCL?>) U
CHN AO?MM = @??>@ILQ;L>(CHJONM);
@FI;N ?LLIL = >?MCL?> - AO?MM;
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
Q?CABNM8C9 += = * ?LLIL * CHJONM8C9;
W
W
W
=F;MM 1L;CH?L U
#E>MQBO 10. .BRO>I .BQTLOHP
456
.LT QEB NRBPQFLK ?B@LJBP, ELT AL TB MF@H > MLFKQ >KA HKLT TEBQEBO FQ FP >?LSB LO ?BILT >
IFKB? ,BQfP PQ>OQ TFQE QEB CLOJRI> CLO > IFKB, TEBOB S FP @>I@RI>QBA >P > CRK@QFLK LC R:
O = <(N)
)K DBKBOF@ QBOJP, > IFKB @>K ?B ABP@OF?BA >P:
O = 7N + 8
(BOBfP > PMB@FCF@ BU>JMIB:
O = 2*N + 1
7B @>K QEBK TOFQB > 0OL@BPPFKD CRK@QFLK TFQE QEFP FK JFKA.
3L, FC TB J>HB RM > MLFKQ:
(LT AL TB HKLT FC QEFP MLFKQ FP >?LSB LO ?BILT QEB IFKB? 4EB IFKB CRK@QFLK @(R) DFSBP RP QEB S
S>IRB LK QEB IFKB CLO QE>Q R MLPFQFLK. ,BQfP @>II QE>Q SFCH?.
)C QEB S S>IRB TB >OB BU>JFKFKD FP >?LSB QEB IFKB, FQ TFII ?B IBPP QE>K SFCH?.
A "Trainer" object stores the inputs and the
correct answer.
@FI;N89 CHJONM;
CHN ;HMQ?L;
1L;CH?L(@FI;N R, @FI;N S, CHN ;) U
CHJONM = H?Q @FI;N839;
CHJONM809 = R;
CHJONM819 = S;
Note that the Trainer has the bias input built
into its array.
CHJONM829 = 1;
;HMQ?L = ;;
W
W
A function to calculate y based on x along a
line
@FI;N @(@FI;N R) U
L?NOLH 2*R+1;
W
@FI;N R = L;H>IG(QC>NB);
@FI;N S = L;H>IG(B?CABN);
The y position on the line @FI;N SFCH? = @(R);
4EB .>QROB LC #LAB (S005)
457
7B @>K QEBK J>HB > 1L;CH?L L?GB@Q TFQE QEB FKMRQP >KA QEB @LOOB@Q >KPTBO.
!PPRJFKD TB E>A > .?L=?JNLIH L?GB@Q JNLIH, TB @LRIA QEBK QO>FK FQ ?V PBKAFKD QEB FKMRQP
>ILKD TFQE QEB HKLTK >KPTBO.
.LT, FQfP FJMLOQ>KQ QL OBJBJ?BO QE>Q QEFP FP GRPQ > ABJLKPQO>QFLK. 2BJBJ?BO LRO
3E>HBPMB>OB-QVMFKD JLKHBVP (PBB M>DB 392)? 7B >PHBA LRO DBKBQF@ >IDLOFQEJ QL PLISB CLO
gQL ?B LO KLQ QL ?Bhd>K >KPTBO TB >IOB>AV HKBT. 7B AFA QEFP QL J>HB PROB LRO DBKBQF@
>IDLOFQEJ TLOHBA MOLMBOIV. 4EB P>JB OB>PLKFKD >MMIFBP QL QEFP BU>JMIB. 7B ALKfQ KBBA >
MBO@BMQOLK QL QBII RP TEBQEBO > MLFKQ FP >?LSB LO ?BILT > IFKB; TB @>K AL QE>Q TFQE PFJMIB
J>QE. 7B >OB RPFKD QEFP P@BK>OFL, LKB QE>Q TB @>K B>PFIV PLISB TFQELRQ > MBO@BMQOLK, QL
ABJLKPQO>QB QEB MBO@BMQOLKfP >IDLOFQEJ >P TBII >P B>PFIV @LKCFOJ QE>Q FQ FP TLOHFKD MOLMBOIV.
,BQfP ILLH >Q ELT QEB MBO@BMQOLK TLOHP TFQE >K >OO>V LC J>KV QO>FKFKD MLFKQP.
F64B?2 10.8
C@ (S < SFCH?) U
The answer is -1 if y is above the line. ;HMQ?L = -1;
W ?FM? U
;HMQ?L = 1;
W
1L;CH?L N = H?Q 1L;CH?L(R, S, ;HMQ?L);
JNLIH.NL;CH(N.CHJONM,N.;HMQ?L);
#E>MQBO 10. .BRO>I .BQTLOHP
458
Example 10.1: The Perceptron
The Perceptron .?L=?JNLIH JNLIH;
2,000 training points 1L;CH?L89 NL;CHCHA = H?Q 1L;CH?L820009;
CHN =IOHN = 0;
The formula for a line @FI;N @(@FI;N R) U
L?NOLH 2*R+1;
W
PIC> M?NOJ() U
MCT?(400, 400);
JNLIH = H?Q .?L=?JNLIH(3);
Make 2,000 training points. @IL (CHN C = 0; C < NL;CHCHA.F?HANB; C++) U
@FI;N R = L;H>IG(-QC>NB/2,QC>NB/2);
@FI;N S = L;H>IG(-B?CABN/2,B?CABN/2);
Is the correct answer 1 or -1? CHN ;HMQ?L = 1;
C@ (S < @(R)) ;HMQ?L = -1;
NL;CHCHA8C9 = H?Q 1L;CH?L(R, S, ;HMQ?L);
W
W
PIC> >L;Q() U
<;=EALIOH>(255);
NL;HMF;N?(QC>NB/2,B?CABN/2);
JNLIH.NL;CH(NL;CHCHA8=IOHN9.CHJONM, NL;CHCHA8=IOHN9.;HMQ?L);
For animation, we are training one point at a
time.
=IOHN = (=IOHN + 1) % NL;CHCHA.F?HANB;
@IL (CHN C = 0; C < =IOHN; C++) U
MNLIE?(0);
CHN AO?MM = JNLIH.@??>@ILQ;L>(NL;CHCHA8C9.CHJONM);
4EB .>QROB LC #LAB (S005)
459
Show the classificationno fill for -1,
black for +1.
C@ (AO?MM > 0) HI$CFF();
?FM? @CFF(0);
?FFCJM?(NL;CHCHA8C9.CHJONM809, NL;CHCHA8C9.CHJONM819, 8, 8);
W
W
)KPQB>A LC RPFKD QEB PRMBOSFPBA IB>OKFKD JLABI >?LSB, @>K VLR QO>FK QEB KBRO>I
KBQTLOH QL CFKA QEB OFDEQ TBFDEQP ?V RPFKD > DBKBQF@ >IDLOFQEJ?
Exercise 10.1 Exercise 10.1
6FPR>IFWB QEB MBO@BMQOLK FQPBIC. $O>T QEB FKMRQP, QEB MOL@BPPFKD KLAB, >KA QEB LRQMRQ.
Exercise 10.2 Exercise 10.2
10.5 A Steering Perceptron 10.5 A Steering Perceptron
7EFIB @I>PPFCVFKD MLFKQP >@@LOAFKD QL QEBFO MLPFQFLK >?LSB LO ?BILT > IFKB T>P > RPBCRI
ABJLKPQO>QFLK LC QEB MBO@BMQOLK FK >@QFLK, FQ ALBPKfQ E>SB JR@E MO>@QF@>I OBIBS>K@B QL QEB
LQEBO BU>JMIBP QEOLRDELRQ QEFP ?LLH. )K QEFP PB@QFLK, TBfII Q>HB QEB @LK@BMQP LC > MBO@BMQOLK
(>OO>V LC FKMRQP, PFKDIB LRQMRQ), >MMIV FQ QL PQBBOFKD ?BE>SFLOP, >KA ABJLKPQO>QB
OBFKCLO@BJBKQ IB>OKFKD >ILKD QEB T>V.
7B >OB KLT DLFKD QL Q>HB PFDKFCF@>KQ @OB>QFSB IF@BKPB TFQE QEB @LK@BMQ LC > KBRO>I KBQTLOH.
4EFP TFII >IILT RP QL PQF@H TFQE QEB ?>PF@P >KA >SLFA PLJB LC QEB EFDEIV @LJMIBU >IDLOFQEJP
>PPL@F>QBA TFQE JLOB PLMEFPQF@>QBA KBRO>I KBQTLOHP. (BOB TBfOB KLQ PL @LK@BOKBA TFQE
CLIILTFKD ORIBP LRQIFKBA FK >OQFCF@F>I FKQBIIFDBK@B QBUQ?LLHPdTBfOB GRPQ ELMFKD QL J>HB
PLJBQEFKD FKQBOBPQFKD >KA ?O>FK-IFHB.
2BJBJ?BO LRO DLLA COFBKA QEB 3?BC=F? @I>PP? 9LR HKLT, QE>Q LKB CLO J>HFKD L?GB@QP TFQE
> IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK? 4E>Q @LRIA L?BV .BTQLKfP I>TP TFQE >K
;JJFS$IL=?() CRK@QFLK >KA JLSB >OLRKA QEB TFKALT >@@LOAFKD QL > S>OFBQV LC PQBBOFKD
ORIBP?
7E>Q FC TB >AABA LKB JLOB S>OF>?IB QL LRO 3?BC=F? @I>PP?
=F;MM 3?BC=F? U
#E>MQBO 10. .BRO>I .BQTLOHP
460
(BOBfP LRO P@BK>OFL. ,BQfP P>V TB E>SB > 0OL@BPPFKD PHBQ@E TFQE >K ALL;S*CMN LC Q>ODBQP >KA
> PFKDIB SBEF@IB.
,BQfP P>V QE>Q QEB SBEF@IB PBBHP >II LC QEB Q>ODBQP. !@@LOAFKD QL QEB MOFK@FMIBP LC #E>MQBO 6, TB
TLRIA KBUQ TOFQB > CRK@QFLK QE>Q @>I@RI>QBP > PQBBOFKD CLO@B QLT>OAP B>@E Q>ODBQ, >MMIVFKD B>@E
CLO@B LKB >Q > QFJB QL QEB L?GB@QfP >@@BIBO>QFLK. !PPRJFKD QEB Q>ODBQP >OB >K ALL;S*CMN LC
.3?=NIL L?GB@QP, FQ TLRIA ILLH PLJBQEFKD IFHB:
)K #E>MQBO 6, TB >IPL BU>JFKBA ELT TB @LRIA @OB>QB JLOB AVK>JF@ PFJRI>QFLKP ?V TBFDEQFKD
B>@E PQBBOFKD CLO@B >@@LOAFKD QL PLJB ORIB. &LO BU>JMIB, TB @LRIA P>V QE>Q QEB C>OQEBO VLR
>OB COLJ > Q>ODBQ, QEB PQOLKDBO QEB CLO@B.
Giving the vehicle a brain! .?L=?JNLIH <L;CH;
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
//?N=...
F64B?2 10.9
PIC> M??E(ALL;S*CMN<.3?=NIL> N;LA?NM) U
@IL (.3?=NIL N;LA?N : N;LA?NM) U
For every target, apply a steering force
towards the target.
.3?=NIL @IL=? = M??E(N;LA?NM.A?N(C));
;JJFS$IL=?(@IL=?);
W
W
PIC> M??E(ALL;S*CMN<.3?=NIL> N;LA?NM) U
@IL (.3?=NIL N;LA?N : N;LA?NM) U
.3?=NIL @IL=? = M??E(N;LA?NM.A?N(C));
@FI;N > = .3?=NIL.>CMN(N;LA?N,FI=;NCIH);
@FI;N Q?CABN = G;J(>,0,QC>NB,0,5);
4EB .>QROB LC #LAB (S005)
461
"RQ TE>Q FC FKPQB>A TB @LRIA >PH LRO ?O>FK (F.B. MBO@BMQOLK) QL Q>HB FK >II QEB CLO@BP >P >K
FKMRQ, MOL@BPP QEBJ >@@LOAFKD QL TBFDEQP LC QEB MBO@BMQOLK FKMRQP, >KA DBKBO>QB >K LRQMRQ
PQBBOFKD CLO@B? 7E>Q FC TB @LRIA FKPQB>A P>V:
)K LQEBO TLOAP, FKPQB>A LC TBFDEQFKD >KA >@@RJRI>QFKD QEB CLO@BP FKPFAB LRO SBEF@IB, TB
PFJMIV M>PP >K >OO>V LC CLO@BP QL QEB SBEF@IBfP g?O>FKh L?GB@Q >KA >IILT QEB ?O>FK QL TBFDEQ
>KA PRJ QEB CLO@BP CLO RP. 4EB LRQMRQ FP QEBK >MMIFBA >P > PQBBOFKD CLO@B. 4EFP LMBKP RM >
O>KDB LC MLPPF?FIFQFBP. ! SBEF@IB @LRIA J>HB AB@FPFLKP >P QL ELT QL PQBBO LK FQP LTK, IB>OKFKD
COLJ FQP JFPQ>HBP >KA OBPMLKAFKD QL PQFJRIF FK FQP BKSFOLKJBKQ. ,BQfP PBB ELT QEFP TLOHP.
7B @>K RPB QEB IFKB @I>PPFCF@>QFLK MBO@BMQOLK >P > JLABI, TFQE LKB FJMLOQ>KQ AFCCBOBK@BdQEB
FKMRQP >OB KLQ PFKDIB KRJ?BOP, ?RQ SB@QLOP! ,BQfP ILLH >Q ELT QEB @??>@ILQ;L>() CRK@QFLK
TLOHP FK LRO SBEF@IBfP MBO@BMQOLK, >ILKDPFAB QEB LKB COLJ LRO MOBSFLRP BU>JMIB.
Vehicle PVector inputs Vehicle PVector inputs Line float inputs Line float inputs
.3?=NIL @??>@ILQ;L>(.3?=NIL89 @IL=?M) U
// 0OG CM ; .3?=NIL.
+0;9JEH IKC = D;M +0;9JEH();
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
// 3?=NIL ;>>CNCIH ;H> GOFNCJFC=;NCIH
<EH9;I4?5.CKBJ(M;?=>JI4?5);
IKC.7::(<EH9;I4?5);
W
// ,I ;=NCP;NCIH @OH=NCIH
H;JKHD IKC;
W
CHN @??>@ILQ;L>(@FI;N89 CHJONM) U
// 0OG CM ; @FI;N.
<BE7J IKC = 0;
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
// 0=;F;L ;>>CNCIH ;H> GOFNCJFC=;NCIH
IKC += ?DFKJI4?5*M;?=>JI4?5;
W
// A=NCP;NCIH @OH=NCIH
H;JKHD 79J?L7J;(IKC);
W
Weighting each steering force individually @IL=?.GOFN(Q?CABN);
;JJFS$IL=?(@IL=?);
W
W
PIC> M??E(ALL;S*CMN<.3?=NIL> N;LA?NM) U
Make an array of inputs for our brain. .3?=NIL89 @IL=?M = H?Q
.3?=NIL8N;LA?NM.MCT?()9;
@IL (CHN C = 0; C < @IL=?M.F?HANB; C++) U
Fill the array with a steering force for each
target.
@IL=?M8C9 = M??E(N;LA?NM.A?N(C));
W
Ask our brain for a result and apply that as
the force!
.3?=NIL IONJON = <L;CH.JLI=?MM(@IL=?M);
;JJFS$IL=?(IONJON);
W
#E>MQBO 10. .BRO>I .BQTLOHP
462
.LQB ELT QEBPB QTL CRK@QFLKP FJMIBJBKQ KB>OIV FABKQF@>I >IDLOFQEJP, TFQE QTL AFCCBOBK@BP:
1. '@88492 %*0.?:=>. '@88492 %*0.?:=>. )KPQB>A LC > PBOFBP LC KRJ?BOP >AABA QLDBQEBO, B>@E FKMRQ FP >
.3?=NIL >KA JRPQ ?B JRIQFMIFBA ?V QEB TBFDEQ >KA >AABA QL > PRJ >@@LOAFKD QL QEB
J>QEBJ>QF@>I .3?=NIL CRK@QFLKP.
2. #: ,.?4A,?4:9 1@9.?4:9. #: ,.?4A,?4:9 1@9.?4:9. )K QEFP @>PB, TBfOB Q>HFKD QEB OBPRIQ >KA >MMIVFKD FQ AFOB@QIV
>P > PQBBOFKD CLO@B CLO QEB SBEF@IB, PL TBfOB KLQ >PHFKD CLO > PFJMIB ?LLIB>K S>IRB
QE>Q @I>PPFCFBP FQ FK LKB LC QTL @>QBDLOFBP. 2>QEBO, TBfOB >PHFKD CLO O>T LRQMRQ FQPBIC,
QEB OBPRIQFKD LSBO>II CLO@B.
/K@B QEB OBPRIQFKD PQBBOFKD CLO@B E>P ?BBK >MMIFBA, FQfP QFJB QL DFSB CBBA?>@H QL QEB ?O>FK, F.B.
F9=B:CF79A9BH @95FB=B;. 7>P QEB AB@FPFLK QL PQBBO FK QE>Q M>OQF@RI>O AFOB@QFLK > DLLA LKB LO >
?>A LKB? 0OBPRJ>?IV FC PLJB LC QEB Q>ODBQP TBOB MOBA>QLOP (OBPRIQFKD FK ?BFKD B>QBK) >KA
PLJB LC QEB Q>ODBQP TBOB CLLA (OBPRIQFKD FK DOB>QBO EB>IQE), QEB KBQTLOH TLRIA >AGRPQ FQP
TBFDEQP FK LOABO QL PQBBO >T>V COLJ QEB MOBA>QLOP >KA QLT>OAP QEB CLLA.
,BQfP Q>HB > PFJMIBO BU>JMIB, TEBOB QEB SBEF@IB PFJMIV T>KQP QL PQ>V @ILPB QL QEB @BKQBO LC QEB
TFKALT. 7BfII QO>FK QEB ?O>FK >P CLIILTP:
(BOB TB >OB M>PPFKD QEB ?O>FK > @LMV LC >II
QEB FKMRQP (TEF@E FQ TFII KBBA CLO BOOLO
@LOOB@QFLK) >P TBII >P >K L?PBOS>QFLK >?LRQ
FQP BKSFOLKJBKQ: > .3?=NIL QE>Q MLFKQP COLJ
FQP @ROOBKQ IL@>QFLK QL TEBOB FQ ABPFOBP QL ?B.
4EFP .3?=NIL BPPBKQF>IIV PBOSBP >P QEB
BOOLOdQEB ILKDBO QEB .3?=NIL, QEB TLOPB QEB SBEF@IB FP MBOCLOJFKD; QEB PELOQBO, QEB ?BQQBO.
4EB ?O>FK @>K QEBK >MMIV QEFP gBOOLOh SB@QLO (TEF@E E>P QTL BOOLO S>IRBP, LKB CLO R >KA LKB CLO
S) >P > JB>KP CLO >AGRPQFKD QEB TBFDEQP, GRPQ >P TB AFA FK QEB IFKB @I>PPFCF@>QFLK BU>JMIB.
Training the Vehicle Training the Vehicle Training the Line Classifier Training the Line Classifier
PIC> NL;CH(.3?=NIL89 @IL=?M, .3?=NIL ?LLIL) U
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
Q?CABNM8C9 += =*?LLIL.R*@IL=?M8C9.R;
Q?CABNM8C9 += =*?LLIL.S*@IL=?M8C9.S;
W
W
PIC> NL;CH(@FI;N89 CHJONM, CHN >?MCL?>) U
CHN AO?MM = @??>@ILQ;L>(CHJONM);
@FI;N ?LLIL = >?MCL?> - AO?MM;
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
Q?CABNM8C9 += = * ?LLIL * CHJONM8C9;
W
W
.3?=NIL >?MCL?> = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
.3?=NIL ?LLIL = .3?=NIL.MO<(>?MCL?>, FI=;NCIH);
<L;CH.NL;CH(@IL=?M,?LLIL);
F64B?2 10.10
4EB .>QROB LC #LAB (S005)
463
"B@>RPB QEB SBEF@IB L?PBOSBP FQP LTK BOOLO, QEBOB FP KL KBBA QL @>I@RI>QB LKB; TB @>K
PFJMIV OB@BFSB QEB BOOLO >P >K >ODRJBKQ. .LQF@B ELT QEB @E>KDB FK TBFDEQ FP MOL@BPPBA
QTF@B, LK@B CLO QEB BOOLO >ILKD QEB U->UFP >KA LK@B CLO QEB V->UFP.
7B @>K KLT ILLH >Q QEB 3?BC=F? @I>PP >KA PBB ELT QEB MN??L CRK@QFLK RPBP > MBO@BMQOLK QL
@LKQOLI QEB LSBO>II PQBBOFKD CLO@B. 4EB KBT @LKQBKQ COLJ QEFP @E>MQBO FP EFDEIFDEQBA.
Example 10.2: Perceptron steering
Q?CABNM8C9 += =*?LLIL.R*@IL=?M8C9.R;
Q?CABNM8C9 += =*?LLIL.S*@IL=?M8C9.S;
=F;MM 3?BC=F? U
The Vehicle now has a brain. +;H9;FJHED 8H7?D;
Same old variables for physics .3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
@FI;N G;R@IL=?;
@FI;N G;RMJ??>;
The Vehicle creates a perceptron with n
inputs and a learning constant.
3?BC=F?(CHN H, @FI;N R, @FI;N S) U
8H7?D = D;M +;H9;FJHED(D,0.001);
;==?F?L;NCIH = H?Q .3?=NIL(0,0);
P?FI=CNS = H?Q .3?=NIL(0,0);
FI=;NCIH = H?Q .3?=NIL(R,S);
G;RMJ??> = 4;
G;R@IL=? = 0.1;
W
#E>MQBO 10. .BRO>I .BQTLOHP
464
Same old update() function PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
P?FI=CNS.FCGCN(G;RMJ??>);
FI=;NCIH.;>>(P?FI=CNS);
;==?F?L;NCIH.GOFN(0);
W
Same old applyForce() function PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
;==?F?L;NCIH.;>>(@IL=?);
W
PIC> MN??L(ALL;S*CMN<.3?=NIL> N;LA?NM) U
.3?=NIL89 @IL=?M = H?Q .3?=NIL8N;LA?NM.MCT?()9;
@IL (CHN C = 0; C < @IL=?M.F?HANB; C++) U
@IL=?M8C9 = M??E(N;LA?NM.A?N(C));
W
All the steering forces are inputs. +0;9JEH H;IKBJ = 8H7?D.<;;:<EHM7H:(<EH9;I);
The result is applied. ;JJFS$IL=?(L?MOFN);
+0;9JEH :;I?H;: = D;M +0;9JEH(M?:J>/2,>;?=>J/2);
+0;9JEH ;HHEH = +0;9JEH.IK8(:;I?H;:, BE97J?ED);
8H7?D.JH7?D(<EH9;I,;HHEH);
W
The brain is trained according to the
distance to the center.
Same old seek() function .3?=NIL M??E(.3?=NIL N;LA?N) U
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
>?MCL?>.HILG;FCT?();
>?MCL?>.GOFN(G;RMJ??>);
.3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?FI=CNS);
MN??L.FCGCN(G;R@IL=?);
L?NOLH MN??L;
W
W
4EB .>QROB LC #LAB (S005)
465
6FPR>IFWB QEB TBFDEQP LC QEB KBQTLOH. 4OV J>MMFKD B>@E Q>ODBQfP @LOOBPMLKAFKD
TBFDEQ QL FQP ?OFDEQKBPP.
Exercise 10.3 Exercise 10.3
4OV AFCCBOBKQ ORIBP CLO OBFKCLO@BJBKQ IB>OKFKD. 7E>Q FC PLJB Q>ODBQP >OB ABPFO>?IB >KA
PLJB >OB RKABPFO>?IB?
Exercise 10.4 Exercise 10.4
10.6 Its a Network, Remember? 10.6 Its a Network, Remember?
9BP, > MBO@BMQOLK @>K E>SB JRIQFMIB FKMRQP, ?RQ FQ FP PQFII > ILKBIV KBROLK. 4EB MLTBO LC
KBRO>I KBQTLOHP @LJBP FK QEB KBQTLOHFKD FQPBIC. 0BO@BMQOLKP >OB, P>AIV, FK@OBAF?IV IFJFQBA FK
QEBFO >?FIFQFBP. )C VLR OB>A >K !) QBUQ?LLH, FQ TFII P>V QE>Q > MBO@BMQOLK @>K LKIV PLISB 7490,=7D 7490,=7D
>0;,=,-70 >0;,=,-70 MOL?IBJP. 7E>QfP > IFKB>OIV PBM>O>?IB MOL?IBJ? ,BQfP Q>HB > ILLH >Q LRO CFOPQ
BU>JMIB, TEF@E ABQBOJFKBA TEBQEBO MLFKQP TBOB LK LKB PFAB LC > IFKB LO QEB LQEBO.
/K QEB IBCQ LC &FDROB 10.11, TB E>SB @I>PPF@ IFKB>OIV PBM>O>?IB A>Q>. 'O>ME >II LC QEB
MLPPF?FIFQFBP; FC VLR @>K @I>PPFCV QEB A>Q> TFQE > PQO>FDEQ IFKB, QEBK FQ FP IFKB>OIV PBM>O>?IB. /K
QEB OFDEQ, ELTBSBO, FP KLK-IFKB>OIV PBM>O>?IB A>Q>. 9LR @>KfQ AO>T > PQO>FDEQ IFKB QL PBM>O>QB
QEB ?I>@H ALQP COLJ QEB DO>V LKBP.
/KB LC QEB PFJMIBPQ BU>JMIBP LC > KLK-IFKB>OIV PBM>O>?IB MOL?IBJ FP 3+-, LO gBU@IRPFSB LO.h
7BfOB >II C>JFIF>O TFQE A*. &LO A A* B QL ?B QORB, ?LQE A >KA B JRPQ ?B QORB. 7FQE +-,
BFQEBO A LO B @>K ?B QORB CLO A +- B QL BS>IR>QB >P QORB. 4EBPB >OB ?LQE IFKB>OIV PBM>O>?IB
MOL?IBJP. ,BQfP ILLH >Q QEB PLIRQFLK PM>@B, > gQORQE Q>?IB.h
F64B?2 10.11
#E>MQBO 10. .BRO>I .BQTLOHP
466
3BB ELT VLR @>K AO>T > IFKB QL PBM>O>QB QEB QORB LRQMRQP COLJ QEB C>IPB LKBP?
3+- FP QEB BNRFS>IBKQ LC +- >KA *+/ A*. )K LQEBO TLOAP, A 3+- B LKIV BS>IR>QBP QL QORB FC
LKB LC QEBJ FP QORB. )C ?LQE >OB C>IPB LO ?LQE >OB QORB, QEBK TB DBQ C>IPB. 4>HB > ILLH >Q QEB
CLIILTFKD QORQE Q>?IB.
4EFP FP KLQ IFKB>OIV PBM>O>?IB. 4OV QL AO>T > PQO>FDEQ IFKB QL PBM>O>QB QEB QORB LRQMRQP COLJ QEB
C>IPB LKBPdVLR @>KfQ!
3L MBO@BMQOLKP @>KfQ BSBK PLISB PLJBQEFKD >P PFJMIB >P 3+-. "RQ TE>Q FC TB J>AB > KBQTLOH
LRQ LC QTL MBO@BMQOLKP? )C LKB MBO@BMQOLK @>K PLISB +- >KA LKB MBO@BMQOLK @>K PLISB *+/
A*, QEBK QTL MBO@BMQOLKP @LJ?FKBA @>K PLISB 3+-.
F64B?2 10.12
F64B?2 10.13
4EB .>QROB LC #LAB (S005)
467
4EB >?LSB AF>DO>J FP HKLTK >P > AI@H=-@5M9F98 D9F79DHFCB, > KBQTLOH LC J>KV KBROLKP.
3LJB >OB FKMRQ KBROLKP >KA OB@BFSB QEB FKMRQP, PLJB >OB M>OQ LC TE>QfP @>IIBA > gEFAABKh
I>VBO (>P QEBV >OB @LKKB@QBA QL KBFQEBO QEB FKMRQP KLO QEB LRQMRQP LC QEB KBQTLOH AFOB@QIV),
>KA QEBK QEBOB >OB QEB LRQMRQ KBROLKP, COLJ TEF@E TB OB>A QEB OBPRIQP.
4O>FKFKD QEBPB KBQTLOHP FP JR@E JLOB @LJMIF@>QBA. 7FQE QEB PFJMIB MBO@BMQOLK, TB @LRIA
B>PFIV BS>IR>QB ELT QL @E>KDB QEB TBFDEQP >@@LOAFKD QL QEB BOOLO. "RQ EBOB QEBOB >OB PL
J>KV AFCCBOBKQ @LKKB@QFLKP, B>@E FK > AFCCBOBKQ I>VBO LC QEB KBQTLOH. (LT ALBP LKB HKLT
ELT JR@E B>@E KBROLK LO @LKKB@QFLK @LKQOF?RQBA QL QEB LSBO>II BOOLO LC QEB KBQTLOH?
4EB PLIRQFLK QL LMQFJFWFKD TBFDEQP LC > JRIQF-I>VBOBA KBQTLOH FP HKLTK >P -,.6;=:;,2,?4:9 -,.6;=:;,2,?4:9.
4EB LRQMRQ LC QEB KBQTLOH FP DBKBO>QBA FK QEB P>JB J>KKBO >P > MBO@BMQOLK. 4EB FKMRQP
JRIQFMIFBA ?V QEB TBFDEQP >OB PRJJBA >KA CBA CLOT>OA QEOLRDE QEB KBQTLOH. 4EB AFCCBOBK@B
EBOB FP QE>Q QEBV M>PP QEOLRDE >AAFQFLK>I I>VBOP LC KBROLKP ?BCLOB OB>@EFKD QEB LRQMRQ.
4O>FKFKD QEB KBQTLOH (F.B. >AGRPQFKD QEB TBFDEQP) >IPL FKSLISBP Q>HFKD QEB BOOLO (ABPFOBA OBPRIQ
- DRBPP). 4EB BOOLO, ELTBSBO, JRPQ ?B CBA ?>@HT>OAP QEOLRDE QEB KBQTLOH. 4EB CFK>I BOOLO
RIQFJ>QBIV >AGRPQP QEB TBFDEQP LC >II QEB @LKKB@QFLKP.
">@HMOLM>D>QFLK FP > ?FQ ?BVLKA QEB P@LMB LC QEFP ?LLH >KA FKSLISBP > C>K@FBO >@QFS>QFLK
CRK@QFLK (@>IIBA QEB PFDJLFA CRK@QFLK) >P TBII >P PLJB ?>PF@ @>I@RIRP. )C VLR >OB FKQBOBPQBA FK
ELT ?>@HMOLM>D>QFLK TLOHP, @EB@H QEB ?LLH TB?PFQB (>KA 'FQ(R? OBMLPFQLOV) CLO >K
BU>JMIB QE>Q PLISBP 3+- RPFKD > JRIQF-I>VBOBA CBBA CLOT>OA KBQTLOH TFQE ?>@HMOLM>D>QFLK.
)KPQB>A, EBOB TBfII CL@RP LK > @LAB CO>JBTLOH CLO ?RFIAFKD QEB SFPR>I >O@EFQB@QROB LC >
KBQTLOH. 7BfII J>HB ,?OLIH L?GB@QP >KA !IHH?=NCIH L?GB@QP COLJ TEF@E > ,?NQILE L?GB@Q
@>K ?B @OB>QBA >KA >KFJ>QBA QL PELT QEB CBBA CLOT>OA MOL@BPP. 4EFP TFII @ILPBIV OBPBJ?IB
PLJB LC QEB CLO@B-AFOB@QBA DO>ME BU>JMIBP TB BU>JFKBA FK #E>MQBO 5 (QLUF@IF?P).
F64B?2 10.14
10.7 Neural Network Diagram 10.7 Neural Network Diagram
/RO DL>I TFII ?B QL @OB>QB QEB CLIILTFKD PFJMIB KBQTLOH AF>DO>J:
#E>MQBO 10. .BRO>I .BQTLOHP
468
4EB MOFJ>OV ?RFIAFKD ?IL@H CLO QEFP AF>DO>J FP > KBROLK. &LO QEB MROMLPB LC QEFP BU>JMIB, QEB
,?OLIH @I>PP ABP@OF?BP >K BKQFQV TFQE >K (L,M) IL@>QFLK.
4EB ,?NQILE @I>PP @>K QEBK J>K>DB >K ALL;S*CMN LC KBROLKP, >P TBII >P E>SB FQP LTK
IL@>QFLK (PL QE>Q B>@E KBROLK FP AO>TK OBI>QFSB QL QEB KBQTLOHfP @BKQBO). 4EFP FP M>OQF@IB
PVPQBJP 101. 7B E>SB > PFKDIB BIBJBKQ (> KBROLK) >KA > KBQTLOH (> gPVPQBJh LC J>KV
KBROLKP).
F64B?2 10.15
An incredibly simple Neuron class stores
and displays the location of a single neuron.
=F;MM ,?OLIH U
.3?=NIL FI=;NCIH;
,?OLIH(@FI;N R, @FI;N S) U
FI=;NCIH = H?Q .3?=NIL(R, S);
W
PIC> >CMJF;S() U
MNLIE?(0);
@CFF(0);
?FFCJM?(FI=;NCIH.R, FI=;NCIH.S, 16, 16);
W
W
A Network is a list of neurons. =F;MM ,?NQILE U
ALL;S*CMN<,?OLIH> H?OLIHM;
.3?=NIL FI=;NCIH;
,?NQILE(@FI;N R, @FI;N S) U
FI=;NCIH = H?Q .3?=NIL(R,S);
H?OLIHM = H?Q ALL;S*CMN<,?OLIH>();
W
We can add an neuron to the network. PIC> ;>>,?OLIH(,?OLIH H) U
H?OLIHM.;>>(H);
W
4EB .>QROB LC #LAB (S005)
469
.LT TB @>K MOBQQV B>PFIV J>HB QEB AF>DO>J >?LSB.
4EB >?LSB VFBIAP:
We can draw the entire network. PIC> >CMJF;S() U
JOMB+;NLCR();
NL;HMF;N?(FI=;NCIH.R, FI=;NCIH.S);
@IL (,?OLIH H : H?OLIHM) U
H.>CMJF;S();
W
JIJ+;NLCR();
W
W
,?NQILE H?NQILE;
PIC> M?NOJ() U
MCT?(640, 360);
Make a Network. H?NQILE = H?Q ,?NQILE(QC>NB/2,B?CABN/2);
Make the Neurons. ,?OLIH ; = H?Q ,?OLIH(-200,0);
,?OLIH < = H?Q ,?OLIH(0,100);
,?OLIH = = H?Q ,?OLIH(0,-100);
,?OLIH > = H?Q ,?OLIH(200,0);
Add the Neurons to the network. H?NQILE.;>>,?OLIH(;);
H?NQILE.;>>,?OLIH(<);
H?NQILE.;>>,?OLIH(=);
H?NQILE.;>>,?OLIH(>);
W
PIC> >L;Q() U
<;=EALIOH>(255);
Show the network. H?NQILE.>CMJF;S();
W
#E>MQBO 10. .BRO>I .BQTLOHP
470
7E>QfP JFPPFKD, LC @LROPB, FP QEB @LKKB@QFLK. 7B @>K @LKPFABO > !IHH?=NCIH L?GB@Q QL ?B
J>AB RM LC QEOBB BIBJBKQP, QTL KBROLKP (COLJ ,?OLIH ; QL ,?OLIH <) >KA > Q?CABN.
/K@B TB E>SB QEB FAB> LC > !IHH?=NCIH L?GB@Q, TB @>K TOFQB > CRK@QFLK (IBQfP MRQ FQ FKPFAB QEB
,?NQILE @I>PP) QE>Q @LKKB@QP QTL KBROLKP QLDBQEBOdQEB DL>I ?BFKD QE>Q FK >AAFQFLK QL J>HFKD
QEB KBROLKP FK M?NOJ(), TB @>K >IPL @LKKB@Q QEBJ.
4EB ,?NQILE @I>PP QEBOBCLOB KBBAP > KBT CRK@QFLK @>IIBA =IHH?=N(), TEF@E J>HBP >
!IHH?=NCIH L?GB@Q ?BQTBBK QEB QTL PMB@FCFBA KBROLKP.
=F;MM !IHH?=NCIH U
A connection is between two neurons. ,?OLIH ;;
,?OLIH <;
A connection has a weight. @FI;N Q?CABN;
!IHH?=NCIH(,?OLIH @LIG, ,?OLIH NI,@FI;N Q) U
Q?CABN = Q;
; = @LIG;
< = NI;
W
A connection is drawn as a line. PIC> >CMJF;S() U
MNLIE?(0);
MNLIE?4?CABN(Q?CABN*4);
FCH?(;.FI=;NCIH.R, ;.FI=;NCIH.S, <.FI=;NCIH.R, <.FI=;NCIH.S);
W
W
PIC> M?NOJ() U
MCT?(640, 360);
H?NQILE = H?Q ,?NQILE(QC>NB/2,B?CABN/2);
,?OLIH ; = H?Q ,?OLIH(-200,0);
,?OLIH < = H?Q ,?OLIH(0,100);
,?OLIH = = H?Q ,?OLIH(0,-100);
,?OLIH > = H?Q ,?OLIH(200,0);
Making connections between the neurons H?NQILE.=IHH?=N(;,<);
H?NQILE.=IHH?=N(;,=);
H?NQILE.=IHH?=N(<,>);
H?NQILE.=IHH?=N(=,>);
H?NQILE.;>>,?OLIH(;);
H?NQILE.;>>,?OLIH(<);
H?NQILE.;>>,?OLIH(=);
H?NQILE.;>>,?OLIH(>);
W
4EB .>QROB LC #LAB (S005)
471
0OBPRJ>?IV, TB JFDEQ QEFKH QE>Q QEB ,?NQILE PELRIA PQLOB >K ALL;S*CMN LC @LKKB@QFLKP,
GRPQ IFHB FQ PQLOBP >K ALL;S*CMN LC KBROLKP. 7EFIB RPBCRI, FK QEFP @>PB PR@E >K ALL;S*CMN FP
KLQ KB@BPP>OV >KA FP JFPPFKD >K FJMLOQ>KQ CB>QROB QE>Q TB KBBA. 5IQFJ>QBIV TB MI>K QL gCBBA
CLOT>OA" QEB KBROLKP QEOLRDE QEB KBQTLOH, PL QEB ,?OLIH L?GB@QP QEBJPBISBP JRPQ HKLT QL
TEF@E KBROLKP QEBV >OB @LKKB@QBA FK QEB gCLOT>OAh AFOB@QFLK. )K LQEBO TLOAP, B>@E KBROLK
PELRIA E>SB FQP LTK IFPQ LC !IHH?=NCIH L?GB@QP. 7EBK ; @LKKB@QP QL <, TB T>KQ ; QL PQLOB >
OBCBOBK@B LC QE>Q @LKKB@QFLK PL QE>Q FQ @>K M>PP FQP LRQMRQ QL < TEBK QEB QFJB @LJBP.
)K PLJB @>PBP, TB >IPL JFDEQ T>KQ ,?OLIH < QL HKLT >?LRQ QEFP @LKKB@QFLK, ?RQ FK QEFP
M>OQF@RI>O BU>JMIB TB >OB LKIV DLFKD QL M>PP FKCLOJ>QFLK FK LKB AFOB@QFLK.
&LO QEFP QL TLOH, TB E>SB QL >AA >K ALL;S*CMN LC @LKKB@QFLKP QL QEB ,?OLIH @I>PP. 4EBK TB
FJMIBJBKQ QEB ;>>!IHH?=NCIH() CRK@QFLK QE>Q PQLOBP QEB @LKKB@QFLK FK QE>Q ALL;S*CMN.
4EB KBROLKfP >CMJF;S() CRK@QFLK @>K AO>T QEB @LKKB@QFLKP >P TBII. !KA CFK>IIV, TB E>SB
LRO KBQTLOH AF>DO>J.
PIC> =IHH?=N(,?OLIH ;, ,?OLIH <) U
Connection has a random weight. !IHH?=NCIH = = H?Q !IHH?=NCIH(;, <,
L;H>IG(1));
// ON QB;N >I Q? >I QCNB NB? !IHH?=NCIH I<D?=N?
W
PIC> =IHH?=N(,?OLIH ;, ,?OLIH <) U
!IHH?=NCIH = = H?Q !IHH?=NCIH(;, <, L;H>IG(1));
;.;>>!IHH?=NCIH(=);
W
=F;MM ,?OLIH U
.3?=NIL FI=;NCIH;
The neuron stores its connections. ALL;S*CMN<!IHH?=NCIH> =IHH?=NCIHM;
,?OLIH(@FI;N R, @FI;N S) U
FI=;NCIH = H?Q .3?=NIL(R, S);
=IHH?=NCIHM = H?Q ALL;S*CMN<!IHH?=NCIH>();
W
Adding a connection to this neuron PIC> ;>>!IHH?=NCIH(!IHH?=NCIH =) U
=IHH?=NCIHM.;>>(=);
W
#E>MQBO 10. .BRO>I .BQTLOHP
472
Example 10.3: Neural network diagram
PIC> >CMJF;S() U
MNLIE?(0);
MNLIE?4?CABN(1);
@CFF(0);
?FFCJM?(FI=;NCIH.R, FI=;NCIH.S, 16, 16);
Drawing all the connections @IL (!IHH?=NCIH = : =IHH?=NCIHM) U
=.>CMJF;S();
W
W
W
10.8 Animating Feed Forward 10.8 Animating Feed Forward
!K FKQBOBPQFKD MOL?IBJ QL @LKPFABO FP ELT QL SFPR>IFWB QEB CILT LC FKCLOJ>QFLK >P FQ QO>SBIP
QEOLRDELRQ > KBRO>I KBQTLOH. /RO KBQTLOH FP ?RFIQ LK QEB CBBA CLOT>OA JLABI, JB>KFKD QE>Q >K
FKMRQ >OOFSBP >Q QEB CFOPQ KBROLK (AO>TK LK QEB IBCQE>KA PFAB LC QEB TFKALT) >KA QEB LRQMRQ LC
QE>Q KBROLK CILTP >@OLPP QEB @LKKB@QFLKP QL QEB OFDEQ RKQFI FQ BUFQP >P LRQMRQ COLJ QEB KBQTLOH
FQPBIC.
/RO CFOPQ PQBM FP QL >AA > CRK@QFLK QL QEB KBQTLOH QL OB@BFSB QEFP FKMRQ, TEF@E TBfII J>HB >
O>KALJ KRJ?BO ?BQTBBK 0 >KA 1.
PIC> M?NOJ() U
All our old network set up code
A new function to send in an input H?NQILE.@??>@ILQ;L>(L;H>IG(1));
W
4EB .>QROB LC #LAB (S005)
473
4EB KBQTLOH, TEF@E J>K>DBP >II QEB KBROLKP, @>K @ELLPB QL TEF@E KBROLKP FQ PELRIA >MMIV
QE>Q FKMRQ. )K QEFP @>PB, TBfII AL PLJBQEFKD PFJMIB >KA GRPQ CBBA > PFKDIB FKMRQ FKQL QEB CFOPQ
KBROLK FK QEB ALL;S*CMN, TEF@E E>MMBKP QL ?B QEB IBCQ-JLPQ LKB.
7E>Q AFA TB AL? 7BII, TB J>AB FQ KB@BPP>OV QL >AA > CRK@QFLK @>IIBA @??>@ILQ;L>() FK QEB
,?OLIH @I>PP QE>Q TFII OB@BFSB QEB FKMRQ >KA MOL@BPP FQ.
)C VLR OB@>II COLJ TLOHFKD TFQE LRO MBO@BMQOLK, QEB PQ>KA>OA Q>PH QE>Q QEB MOL@BPPFKD RKFQ
MBOCLOJP FP QL PRJ RM >II LC FQP FKMRQP. 3L FC LRO ,?OLIH @I>PP >AAP > S>OF>?IB @>IIBA MOG, FQ
@>K PFJMIV >@@RJRI>QB QEB FKMRQP >P QEBV >OB OB@BFSBA.
4EB KBROLK @>K QEBK AB@FAB TEBQEBO FQ PELRIA gCFOB,h LO M>PP >K LRQMRQ QEOLRDE >KV LC FQP
@LKKB@QFLKP QL QEB KBUQ I>VBO FK QEB KBQTLOH. (BOB TB @>K @OB>QB > OB>IIV PFJMIB >@QFS>QFLK
CRK@QFLK: FC QEB PRJ FP DOB>QBO QE>K 1, CFOB!
=F;MM ,?NQILE U
A new function to feed an input into the
neuron
PIC> @??>@ILQ;L>(@FI;N CHJON) U
,?OLIH MN;LN = H?OLIHM.A?N(0);
MN;LN.@??>@ILQ;L>(CHJON);
W
=F;MM ,?OLIH
PIC> @??>@ILQ;L>(@FI;N CHJON) U
What do we do with the input?
W
=F;MM ,?OLIH
?DJ IKC = 0;
PIC> @??>@ILQ;L>(@FI;N CHJON) U
Accumulate the sums. IKC += ?DFKJ;
W
PIC> @??>@ILQ;L>(@FI;N CHJON) U
MOG += CHJON;
Activate the neuron and fire the outputs? C@ (MOG > 1) U
@CL?();
If weve fired off our output, we can reset
our sum to 0.
MOG = 0;
W
W
#E>MQBO 10. .BRO>I .BQTLOHP
474
.LT, TE>Q AL TB AL FK QEB @CL?() CRK@QFLK? )C VLR OB@>II, B>@E KBROLK HBBMP QO>@H LC FQP
@LKKB@QFLKP QL LQEBO KBROLKP. 3L >II TB KBBA QL AL FP ILLM QEOLRDE QELPB @LKKB@QFLKP >KA
@??>@ILQ;L>() QEB KBROLKfP LRQMRQ. &LO QEFP PFJMIB BU>JMIB, TBfII GRPQ Q>HB QEB KBROLKfP MOG
S>OF>?IB >KA J>HB FQ QEB LRQMRQ.
(BOBfP TEBOB QEFKDP DBQ > IFQQIB QOF@HV. !CQBO >II, LRO GL? EBOB FP KLQ QL >@QR>IIV J>HB >
CRK@QFLKFKD KBRO>I KBQTLOH, ?RQ QL >KFJ>QB > PFJRI>QFLK LC LKB. )C QEB KBRO>I KBQTLOH TBOB GRPQ
@LKQFKRFKD FQP TLOH, FQ TLRIA FKPQ>KQIV M>PP QELPB FKMRQP (JRIQFMIFBA ?V QEB @LKKB@QFLKfP TBFDEQ)
>ILKD QL QEB @LKKB@QBA KBROLKP. 7BfA P>V PLJBQEFKD IFHB:
"RQ QEFP FP KLQ TE>Q TB T>KQ. 7E>Q TB T>KQ QL AL FP AO>T PLJBQEFKD QE>Q TB @>K PBB QO>SBIFKD
>ILKD QEB @LKKB@QFLK COLJ ,?OLIH ; QL ,?OLIH <.
,BQfP CFOPQ QEFKH >?LRQ ELT TB JFDEQ AL QE>Q. 7B HKLT QEB IL@>QFLK LC ,?OLIH ;; FQfP QEB
.3?=NIL ;.FI=;NCIH. ,?OLIH < FP IL@>QBA >Q <.FI=;NCIH. 7B KBBA QL PQ>OQ PLJBQEFKD JLSFKD
COLJ ,?OLIH ; ?V @OB>QFKD >KLQEBO .3?=NIL QE>Q TFII PQLOB QEB M>QE LC LRO QO>SBIFKD A>Q>.
/K@B TB E>SB > @LMV LC QE>Q IL@>QFLK, TB @>K RPB >KV LC QEB JLQFLK >IDLOFQEJP QE>Q TBfSB
PQRAFBA QEOLRDELRQ QEFP ?LLH QL JLSB >ILKD QEFP M>QE. (BOBdIBQfP MF@H PLJBQEFKD SBOV PFJMIB
>KA GRPQ FKQBOMLI>QB COLJ ; QL <.
!ILKD TFQE QEB @LKKB@QFLKfP IFKB, TB @>K QEBK AO>T > @FO@IB >Q QE>Q IL@>QFLK:
4EFP OBPBJ?IBP QEB CLIILTFKD:
PIC> @CL?() U
@IL (!IHH?=NCIH = : =IHH?=NCIHM) U
The Neuron sends the sum out through all
of its connections
=.@??>@ILQ;L>(MOG);
W
W
=F;MM !IHH?=NCIH U
PIC> @??>@ILQ;L>(@FI;N P;F) U
<.@??>@ILQ;L>(P;F*Q?CABN);
W
.3?=NIL M?H>?L = ;.FI=;NCIH.A?N();
M?H>?L.R = F?LJ(M?H>?L.R, <.FI=;NCIH.R, 0.1);
M?H>?L.S = F?LJ(M?H>?L.S, <.FI=;NCIH.S, 0.1);
MNLIE?(0);
FCH?(;.FI=;NCIH.R, ;.FI=;NCIH.S, <.FI=;NCIH.R, <.FI=;NCIH.S);
@CFF(0);
;BB?FI;(I;D:;H.N, I;D:;H.O, 8, 8);
4EB .>QROB LC #LAB (S005)
475
/+, PL QE>QfP ELT TB JFDEQ JLSB PLJBQEFKD >ILKD QEB @LKKB@QFLK. "RQ ELT AL TB HKLT
TEBK QL AL PL? 7B PQ>OQ QEFP MOL@BPP QEB JLJBKQ QEB !IHH?=NCIH L?GB@Q OB@BFSBP QEB
gCBBACLOT>OAh PFDK>I. 7B @>K HBBM QO>@H LC QEFP MOL@BPP ?V BJMILVFKD > PFJMIB <IIF?;H QL
HKLT TEBQEBO QEB @LKKB@QFLK FP PBKAFKD LO KLQ. "BCLOB, TB E>A:
.LT, FKPQB>A LC PBKAFKD QEB S>IRB LK PQO>FDEQ >T>V, TBfII QOFDDBO >K >KFJ>QFLK:
.LQF@B ELT LRO !IHH?=NCIH @I>PP KLT KBBAP QEOBB KBT S>OF>?IBP. 7B KBBA > <IIF?;H
gPBKAFKDh QE>Q PQ>OQP >P C>IPB >KA QE>Q TFII QO>@H TEBQEBO LO KLQ QEB @LKKB@QFLK FP >@QFSBIV
PBKAFKD (F.B. >KFJ>QFKD). 7B KBBA > .3?=NIL gPBKABOh CLO QEB IL@>QFLK TEBOB TBfII AO>T QEB
QO>SBIFKD ALQ. !KA PFK@B TB >OBKfQ M>PPFKD QEB LRQMRQ >ILKD QEFP FKPQ>KQ, TBfII KBBA QL PQLOB FQ
FK > S>OF>?IB QE>Q TFII AL QEB GL? I>QBO.
4EB @??>@ILQ;L>() CRK@QFLK FP @>IIBA QEB JLJBKQ QEB @LKKB@QFLK ?B@LJBP >@QFSB. /K@B FQfP
>@QFSB, TBfII KBBA QL @>II >KLQEBO CRK@QFLK @LKQFKRLRPIV (B>@E QFJB QEOLRDE >L;Q()), LKB QE>Q
TFII RMA>QB QEB IL@>QFLK LC QEB QO>SBIFKD A>Q>.
F64B?2 10.16
PIC> @??>@ILQ;L>(@FI;N P;F) U
<.@??>@ILQ;L>(P;F*Q?CABN);
W
=F;MM !IHH?=NCIH U
<IIF?;H M?H>CHA = @;FM?;
.3?=NIL M?H>?L;
@FI;N IONJON;
PIC> @??>@ILQ;L>(@FI;N P;F) U
Sending is now true. M?H>CHA = NLO?;
Start the animation at the location of
Neuron A.
M?H>?L = ;.FI=;NCIH.A?N();
Store the output for when it is actually time
to feed it forward.
IONJON = P;F*Q?CABN;
W
PIC> OJ>;N?() U
C@ (M?H>CHA) U
As long as were sending, interpolate our
points.
M?H>?L.R = F?LJ(M?H>?L.R, <.FI=;NCIH.R,
0.1);
M?H>?L.S = F?LJ(M?H>?L.S, <.FI=;NCIH.S, 0.1);
W
W
#E>MQBO 10. .BRO>I .BQTLOHP
476
7BfOB JFPPFKD > HBV BIBJBKQ, ELTBSBO. 7B KBBA QL @EB@H FC QEB PBKABO E>P >OOFSBA >Q IL@>QFLK
?, >KA FC FQ E>P, CBBA CLOT>OA QE>Q LRQMRQ QL QEB KBUQ KBROLK.
,BQfP ILLH >Q QEB !IHH?=NCIH @I>PP >II QLDBQEBO, >P TBII >P LRO KBT >L;Q() CRK@QFLK.
Example 10.4: Animating neural network diagram
PIC> OJ>;N?() U
C@ (M?H>CHA) U
M?H>?L.R = F?LJ(M?H>?L.R, <.FI=;NCIH.R, 0.1);
M?H>?L.S = F?LJ(M?H>?L.S, <.FI=;NCIH.S, 0.1);
How far are we from neuron b? <BE7J : = +0;9JEH.:?IJ(I;D:;H, 8.BE97J?ED);
If were close enough (within one pixel) pass
on the output. Turn off sending.
?< (: < 1) Q
8.<;;:<EHM7H:(EKJFKJ);
I;D:?D= = <7BI;;
W
W
W
PIC> >L;Q() U
<;=EALIOH>(255);
The Network now has a new update()
method that updates all of the Connection
objects.
D;JMEHA.KF:7J;();
H?NQILE.>CMJF;S();
?< (<H7C;CEKDJ % 30 == 0) Q
We are choosing to send in an input every
30 frames.
D;JMEHA.<;;:<EHM7H:(H7D:EC(1));
S
W
=F;MM !IHH?=NCIH U
4EB .>QROB LC #LAB (S005)
477
The Connections data @FI;N Q?CABN;
,?OLIH ;;
,?OLIH <;
Variables to track the animation <IIF?;H M?H>CHA = @;FM?;
.3?=NIL M?H>?L;
@FI;N IONJON = 0;
!IHH?=NCIH(,?OLIH @LIG, ,?OLIH NI, @FI;N Q) U
Q?CABN = Q;
; = @LIG;
< = NI;
W
The Connection is active with data
traveling from a to b.
PIC> @??>@ILQ;L>(@FI;N P;F) U
IONJON = P;F*Q?CABN;
M?H>?L = ;.FI=;NCIH.A?N();
M?H>CHA = NLO?;
W
Update the animation if it is sending. PIC> OJ>;N?() U
C@ (M?H>CHA) U
M?H>?L.R = F?LJ(M?H>?L.R, <.FI=;NCIH.R, 0.1);
M?H>?L.S = F?LJ(M?H>?L.S, <.FI=;NCIH.S, 0.1);
@FI;N > = .3?=NIL.>CMN(M?H>?L, <.FI=;NCIH);
C@ (> < 1) U
<.@??>@ILQ;L>(IONJON);
M?H>CHA = @;FM?;
W
W
W
Draw the connection as a line and traveling
circle.
PIC> >CMJF;S() U
MNLIE?(0);
MNLIE?4?CABN(1+Q?CABN*4);
FCH?(;.FI=;NCIH.R, ;.FI=;NCIH.S, <.FI=;NCIH.R, <.FI=;NCIH.S);
C@ (M?H>CHA) U
@CFF(0);
MNLIE?4?CABN(1);
?FFCJM?(M?H>?L.R, M?H>?L.S, 16, 16);
W
W
W
#E>MQBO 10. .BRO>I .BQTLOHP
478
4EB KBQTLOH FK QEB >?LSB BU>JMIB T>P J>KR>IIV @LKCFDROBA ?V PBQQFKD QEB IL@>QFLK LC
B>@E KBROLK >KA FQP @LKKB@QFLKP TFQE E>OA-@LABA S>IRBP. 2BTOFQB QEFP BU>JMIB QL
DBKBO>QB QEB KBQTLOHfP I>VLRQ SF> >K >IDLOFQEJ. #>K VLR J>HB > @FO@RI>O KBQTLOH
AF>DO>J? ! O>KALJ LKB? !K BU>JMIB LC > JRIQF-I>VBOBA KBQTLOH FP ?BILT.
Exercise 10.5 Exercise 10.5
2BTOFQB QEB BU>JMIB PL QE>Q B>@E KBROLK HBBMP QO>@H LC FQP CLOT>OA >KA ?>@HT>OA
@LKKB@QFLKP. #>K VLR CBBA FKMRQP QEOLRDE QEB KBQTLOH FK >KV AFOB@QFLK?
Exercise 10.6 Exercise 10.6
)KPQB>A LC F?LJ(), RPB JLSFKD ?LAFBP TFQE PQBBOFKD CLO@BP QL SFPR>IFWB QEB CILT LC
FKCLOJ>QFLK FK QEB KBQTLOH.
Exercise 10.7 Exercise 10.7
4EB .>QROB LC #LAB (S005)
479
The Ecosystem Project The Ecosystem Project
Step 10 Exercise:
Try incorporating the concept of a brain into your creatures.
Use reinforcement learning in the creatures decision-making process.
Create a creature that features a visualization of its brain as part of its
design (even if the brain itself is not functional).
Can the ecosystem as a whole emulate the brain? Can elements of the
environment be neurons and the creatures act as inputs and outputs?
The end The end
)C VLRfOB PQFII OB>AFKD, QE>KH VLR! 9LRfSB OB>@EBA QEB BKA LC QEB ?LLH. "RQ CLO >P JR@E
J>QBOF>I >P QEFP ?LLH @LKQ>FKP, TBfSB ?>OBIV P@O>Q@EBA QEB PROC>@B LC QEB TLOIA TB FKE>?FQ
>KA LC QB@EKFNRBP CLO PFJRI>QFKD FQ. )QfP JV FKQBKQFLK CLO QEFP ?LLH QL IFSB >P >K LKDLFKD
MOLGB@Q, >KA ) ELMB QL @LKQFKRB >AAFKD KBT QRQLOF>IP >KA BU>JMIBP QL QEB ?LLHfP TB?PFQB
(EQQM://K>QROBLC@LAB.@LJ) >P TBII >P BUM>KA >KA RMA>QB QEB MOFKQBA J>QBOF>I. 9LRO CBBA?>@H
FP QORIV >MMOB@F>QBA, PL MIB>PB DBQ FK QLR@E SF> BJ>FI >Q (A>KFBIPEFCCJ>K.KBQ) LO ?V
@LKQOF?RQFKD QL QEB 'FQ(R? OBMLPFQLOV (EQQM://DFQER?.@LJ/PEFCCJ>K/4EB-.>QROB-LC-#LAB/), FK
HBBMFKD TFQE QEB LMBK-PLRO@B PMFOFQ LC QEB MOLGB@Q. 3E>OB VLRO TLOH. +BBM FK QLR@E. ,BQfP ?B
QTL TFQE K>QROB.
#E>MQBO 10. .BRO>I .BQTLOHP
480
Further Reading Further Reading
Books Books
i !IBU>KABO, 2. -@.BFII. ,F=B7=D@9G C: AB=A5@ (C7CACH=CB (EQQM://Q.@L/)10FO>K%).
0OFK@BQLK, .*: 0OFK@BQLK 5KFSBOPFQV 0OBPP, 2002.
i "BKQIBV, 0BQBO. !JC@IH=CB5FM 9G=;B 6M CCADIH9FG (EQQM://Q.@L/8)M7?1WT). 3>K
&O>K@FP@L: -LOD>K +>RCJ>KK 0R?IFPEBOP, 1999.
i "LEK>@HBO, (>OQJRQ, "BKBAFHQ 'OLPP, *RIF> ,>R?, >KA #I>RAFRP ,>WWBOLKF. #9B9F5H=J9
9G=;B: 1=GI5@=N9, ,FC;F5A, 5B8 CF95H9 K=H< ,FC79GG=B; (EQQM://Q.@L/8VBHJ>H,). .BT
9LOH: 0OFK@BQLK !O@EFQB@QRO>I 0OBPP, 2012.
i &I>HB, '>OV 7FIIF>J. /<9 CCADIH5H=CB5@ B95IHM C: *5HIF9: CCADIH9F !LD@CF5H=CBG C:
"F57H5@G, C<5CG, CCAD@9L .MGH9AG, 5B8 A85DH5H=CB (EQQM://Q.@L/+A?4L1:8).
#>J?OFADB, -!: -)4 0OBPP, 1998.
i (>IB, .>QE>K #>?LQ. A6GHF57H=CB =B AFH 5B8 *5HIF9 (EQQM://Q.@L/WQ?11W#,). .BT 9LOH:
$LSBO, 1993.
i (FIAB?O>KAQ, 3QBC>K, >KA !KQELKV *. 4OLJ?>. )5H<9A5H=7G 5B8 +DH=A5@ "CFA
(EQQM://Q.@L/)10FO>K%). .BT 9LOH: 3@FBKQFCF@ !JBOF@>K ,F?O>OV, 1985. $FPQOF?RQBA ?V 7.
(. &OBBJ>K.
i +IFKB, -LOOFP. )5H<9A5H=7G 5B8 H<9 ,<MG=75@ 2CF@8 (EQQM://Q.@L/S843:K'U). .BT 9LOH:
#OLTBII, ;1959<.
i +LAF@BH, $>KKV. )5H<9A5H=7G 5B8 ,<MG=7G :CF ,FC;F5AA9FG (EQQM://Q.@L/VD$A(->H).
(FKDE>J, -!: #E>OIBP 2FSBO -BAF>, 2005.
4EB .>QROB LC #LAB (S005)
481
i -@->ELK, 4ELJ>P !., >KA *LEK 4VIBO "LKKBO. +B .=N9 5B8 (=:9 (EQQM://Q.@L/
%E83+T:"). .BT 9LOH: 3@FBKQFCF@ !JBOF@>K ,F?O>OV, 1983. $FPQOF?RQBA ?V 7. (.
&OBBJ>K.
i ->KABI?OLQ, "BKLFQ ". /<9 "F57H5@ #9CA9HFM C: *5HIF9 (EQQM://Q.@L/G(215P1#). 3>K
&O>K@FP@L: 7. (. &OBBJ>K, 1982.
i 0B>O@B, 0BQBO. .HFI7HIF9 =B *5HIF9 %G 5 .HF5H9;M :CF 9G=;B (EQQM://Q.@L/W>'1-/-@).
#>J?OFADB, -!: -)4 0OBPP, 1980.
i 0B>OPLK, ->QQ. #9B9F5H=J9 AFH (EQQM://Q.@L/?8#7CD/#). 'OBBKTF@E, #4: ->KKFKD
0R?IF@>QFLKP, 2011. $FPQOF?RQBA ?V 0B>OPLK %AR@>QFLK.
i 0ORPFKHFBTF@W, 0OWBJVP[>T, >KA !OFPQFA ,FKABKJ>VBO. /<9 A@;CF=H<A=7 B95IHM C:
,@5BHG (EQQM://Q.@L/HL$7&E*1). .BT 9LOH: 3MOFKDBO-6BOI>D, 1990.
i 2B>P, #>PBV, >KA #E>KAIBO -@7FIIF>JP. "CFA+CC89 =B 9G=;B, AFH, 5B8
AF7<=H97HIF9 (EQQM://Q.@L/1G'DTES5). $BPFDK "OFBCP. .BT 9LOH: 0OFK@BQLK
!O@EFQB@QRO>I 0OBPP, 2010.
i 2B>P, #>PBV, >KA "BK &OV. ,FC79GG=B;: A ,FC;F5AA=B; $5B86CC? :CF 1=GI5@
9G=;B9FG 5B8 AFH=GHG (EQQM://Q.@L/AQ/$A/1M). #>J?OFADB, -!: -)4 0OBPP, 2007.
i 4ELJMPLK, $f!O@V 7BKQTLOQE. +B #FCKH< 5B8 "CFA: /<9 CCAD@9H9 -9J=G98
!8=H=CB (EQQM://Q.@L/SK@7>1R7). .BT 9LOH: $LSBO, 1992.
i 6LDBI., 3QBSBK. (=:9 =B )CJ=B; "@I=8G (EQQM://Q.@L/CV4?6Q>1). 0OFK@BQLK, .*: 0OFK@BQLK
5KFSBOPFQV 0OBPP, 1994.
i 7>AB, $>SFA. (=: MB5A=7 "CFA =B *5HIF9 (EQQM://Q.@L/119$IP$(). 7LLABK "LLHP.
.BT 9LOH: 7>IHBO & #L., 2003.
i 7>QBOJ>K, 4>I?LQ (. AB=A5@ *5J=;5H=CB (EQQM://Q.@L/@2LQS8,:). .BT 9LOH: 3@FBKQFCF@
!JBOF@>K ,F?O>OV, 1989. $FPQOF?RQBA ?V 7. (. &OBBJ>K.
i 7EVQB, ,>K@BILQ ,>T. AGD97HG C: "CFA: A .MADCG=IA CB "CFA =B *5HIF9 5B8 AFH
(EQQM://Q.@L/C75H6,1-). -FAI>KA "LLHP, -" 31. "ILLJFKDQLK: )KAF>K> 5KFSBOPFQV
0OBPP, 1966.
&LO LQEBO ?LLHP QE>Q RPB 0OL@BPPFKD, PBB 0OL@BPPFKD "LLHP (EQQM://TTT.MOL@BPPFKD.LOD/
IB>OKFKD/?LLHP).
Papers and Articles Papers and Articles
i '>I>KQBO, 0EFIFM. "4EB 0OL?IBJ TFQE %SLIRQFLK>OV !OQ )Pj" (EQQM://?FQ.IV/37AEKN)
0>MBO MOBPBKQBA >Q %SL#/-.%4f10: 4EB 7QE %ROLMB>K %SBKQ LK QEB !MMIF@>QFLK LC
&ROQEBO 2B>AFKD
482
.>QROB-FKPMFOBA 4B@EKFNRBP CLO 4BIB@LJJRKF@>QFLK .BQTLOHP >KA LQEBO 0>O>IIBI >KA
$FPQOF?RQBA 3VPQBJP, !MOFI 7-9, 2010.
i '>OAKBO, ->OQFK. "->QEBJ>QF@>I '>JBP: 4EB &>KQ>PQF@ #LJ?FK>QFLKP LC *LEK
#LKT>VfP .BT 3LIFQ>FOB '>JB ,FCB." (EQQM://TTT.F?F?IFL.LOD/IFCBM>QQBOKP/
L@QL?BO1970.EQJI) .7=9BH=:=7 AA9F=75B 229 (/@QL?BO 1970): 120-23.
i 2BBSBP, 7FIIF>J 4. "0>OQF@IB 3VPQBJPd! 4B@EKFNRB CLO -LABIFKD > #I>PP LC &RWWV
/?GB@QP." (EQQM://AI.>@J.LOD/@FQ>QFLK.@CJ?FA=357320) AC) /F5BG57H=CBG CB #F5D<=7G
2:2 (!MOFI 1983): 91-108.
i 3FJP, +>OI. "!OQFCF@F>I %SLIRQFLK CLO #LJMRQBO 'O>MEF@P." (EQQM://TTT.H>OIPFJP.@LJ/
M>MBOP/PFDDO>ME91.EQJI) 0>MBO MOBPBKQBA >Q 3)''2!0( '91: 4EB 18QE !KKR>I
#LKCBOBK@B LK #LJMRQBO 'O>MEF@P >KA )KQBO>@QFSB 4B@EKFNRBP, ,>P 6BD>P, .6, *RIV
28-!RDRPQ 2, 1991.
i ---. "%SLISFKD 6FOQR>I #OB>QROBP." (EQQM://TTT.H>OIPFJP.@LJ/M>MBOP/PFDDO>ME94.MAC)
0>MBO MOBPBKQBA >Q 3)''2!0( '94: 4EB 21PQ !KKR>I #LKCBOBK@B LK #LJMRQBO
'O>MEF@P >KA )KQBO>@QFSB 4B@EKFNRBP, /OI>KAL, &,, *RIV 24-29, 1994.
i ---. "0>OQF@IB !KFJ>QFLK >KA 2BKABOFKD 5PFKD $>Q> 0>O>IIBI #LJMRQ>QFLK."
(EQQM://TTT.H>OIPFJP.@LJ/M>MBOP/0>OQF@IBP3FDDO>ME90.MAC) 0>MBO MOBPBKQBA >Q
3)''2!0( '90: 4EB 17QE !KKR>I #LKCBOBK@B LK #LJMRQBO 'O>MEF@P >KA )KQBO>@QFSB
4B@EKFNRBP, $>II>P, 48, !RDRPQ 6-10, 1990.
4EB .>QROB LC #LAB (S005)
483
Index Index
A
! ILDF@>I @>I@RIRP LC QEB FAB>P FJJFKBKQ FK KBOSLRP
>@QFSFQV (-@#RIIL@E/0FQQP)
445
>@@BIBO>QFLK 49 , 50 , 67 , 69 , 104 , 127 , 131
.BTQLK'P PB@LKA I>T 67
>IDLOFQEJP CLO 50
>KDRI>O, ABQBOJFKFKD 127
A>JMFKD 131
CLO@B >@@RJRI>QFLK >KA 69
OLQ>QFLK 104
>@@BIBO>QFLK >IDLOFQEJP 50 , 53 , 57
@LKPQ>KQ 50
FKQBO>@QFSB 57
O>KALJ 53
>@QFLK PBIB@QFLK 262
>@QFS>QFLK CRK@QFLKP LC KBRO>I KBQTLOHP 449
A85DH5H=CB =B *5HIF5@ 5B8 AFH=:=7=5@ .MGH9AG
((LII>KA)
392
>AA() CRK@QFLK (06B@QLO @I>PP) 33 , 34
FJMIBJBKQ>QFLK LC 34
>AA() CRK@QFLK (6B@2 @I>PP) 195
>AA&LO@B() CRK@QFLK (QLUF@IF?P) 256
>AAFQFLK LMBO>QLO 33
>AAFQFSB ?IBKA JLAB 186
>AA,L@>I() CRK@QFLK (6B@2) 195
>AA0>OQF@IB() CRK@QFLK (QLUF@IF?P) 246
A% :CF #5A9 9J9@CD9FG ("LROD/3BBJ>KK) 445
A@;CF=H<A=7 B95IHM C: ,@5BHG, /<9 (0ORPFKHFBTF@W/
,FKABKJ>VBO)
389
>IFDKJBKQ (CIL@HFKD) 309 , 311
FJMIBJBKQFKD 311
>IME>?BQ (,-PVPQBJ @LJMLKBKQ) 383
>JMIFQRAB 117
>KDIB"BQTBBK() CRK@QFLK (06B@QLO @I>PP) 37
>KDIBP 101 , 102 , 104 , 112
JB>PROFKD FK ABDOBBP 101
JB>PROFKD FK O>AF>KP 102
JLQFLK 104
QEBQ> (^) 112
!KDOV "FOAP 190
>KDRI>O >@@BIBO>QFLK 127
>KDRI>O SBIL@FQV, LP@FII>QFLK TFQE 119
>KLJ>IV ABQB@QFLK 447
>KQP, JLABIFKD CLO 299
>MMIV&LO@B() CRK@QFLK ("LU2$) 232
!OFPQLQIB 64
!OO>V,FPQ @I>PP (*>S>) 149 , 150 , 151 , 153 , 366
)QBO>QLO @I>PP >KA 153
CO>@Q>IP >KA 366
DBKBOF@P, RPFKD 150
OBPFW>?FIFQV LC 151
>OO>VP 8 , 15
)KABU
484
,ZSV CIFDEQP, FJMIBJBKQFKD TFQE 15
@RPQLJ AFPQOF?RQFLKP, @OB>QFKD TFQE 8
>OO>VP (2$) 277
>OOFSFKD ?BE>SFLO 270 , 273
PQBBOFKD CLO@B >KA 273
>OQFCF@F>I FKQBIIFDBK@B 444 , 445
M>QQBOK OB@LDKFQFLK 445
AFH=:=7=5@ %BH9@@=;9B79: A )C89FB ADDFC57< (2RPPBII/
.LOSFD)
445
>Q>K() CRK@QFLK (0OL@BPPFKD) 110
>Q>K2() CRK@QFLK (0OL@BPPFKD) 111
!QQO>@QFLK"BE>SFLO @I>PP (QLUF@IF?P) 256
>RQLKLJLRP >DBKQP 260 , 261 , 262 , 263 , 270 ,
274 , 276 , 282 , 286 , 298 , 306 , 308 , 317
>@QFLK PBIB@QFLK 262
>OOFSFKD ?BE>SFLO 270
@LJ?FK>QFLKP 306
@LJMIBU PVPQBJP >KA 298
ABPFOBA SBIL@FQV 274
ALQ MOLAR@Q 282
BCCF@FBK@V 317
CIL@HFKD 308
CILT CFBIA CLIILTFKD 276
HBV @LJMLKBKQP LC 261
IL@LJLQFLK 263
M>QE CLIILTFKD 286
PQBBOFKD 262
>UFLJ (,-PVPQBJ @LJMLKBKQ) 383
B
?>@HMOLM>D>QFLK 468
?BDFK#LKQ>@Q() CRK@QFLK (0"LU2$) 235
?BDFK3E>MB() CRK@QFLK (0OL@BPPFKD) 217
?BII @ROSB 11
JB>K >KA 11
?F>P FKMRQ, MBO@BMQOLK 450
"FD / .LQ>QFLK 315
"FD / .LQ>QFLK .-3NR>OBA 315
?FK-I>QQF@B PM>QF>I PR?AFSFPFLK 316
?IBKA JLABP 186 , 187
>AAFQFSB 186
IFPQ LC 187
?LAV ("LU2$ BIBJBKQ) 194 , 198 , 199 , 200 , 202 ,
218
"LAV$BC QVMB 198
>QQ>@EFKD CFUQROB BIBJBKQ QL 202
?RFIAFKD 198
?RIIBQ PBQQFKD CLO 200
FKFQF>I PBQQFKDP CLO 200
JRIQFMIB PE>MBP >KA 218
L?GB@Q, @OB>QFKD 200
QVMBP LC 199
?LAV IFPQP, J>FKQ>FKFKD FK 0OL@BPPFKD 203
"LAV$BC QVMB (?LAV BIBJBKQ) 198 , 199 , 209
34!4)# QVMB 209
@LKCFDROFKD 199
?LFAP JLABI 261
?LRK@FKD ?>II PHBQ@E 28 , 35
FJMIBJBKQFKD TFQE SB@QLOP 35
?LRKA>OFBP 209 , 211
@ROSV 211
CFUBA 209
"LROD, $>SFA -. 445
"LU2$ 190 , 192 , 194 , 196 , 209 , 215 , 216 , 241
&FPF@> 192
*"LU2$ >KA 192
0"LU2$ EBIMBO @I>PP 192
06B@QLO SP. 194
0OL@BPPFKD >KA 192
@LJMIBU CLOJP FK 215
@LK@>SB PE>MBP >KA 216
@LLOAFK>QB PVPQBJ SP. 0OL@BPPFKD 196
@LOB BIBJBKQP 194
CFUBA L?GB@QP FK 209
LOABO LC SBOQF@BP 216
LSBOEB>A TFQE 190
QLUF@IF?P SP. 241
RP>DB 192
"O>FQBK?BOD, 6>IBKQFKL 262
?ORQB CLO@B JBQELA 392
4EB .>QROB LC #LAB (S005)
485
C
#>KQLO PBQ 358 , 363 , 386
,-PVPQBJP >KA 386
OB@ROPFLK >KA 363
#>KQLO, 'BLODB 358
#>OQBPF>K @LLOAFK>QBP 112 , 113
MLI>O @LLOAFK>QBP, @LKSBOQFKD COLJ 113
#>QQL, %OFK 190
@BIIRI>O >RQLJ>QLK (>RQLJ>Q>) 324 , 325 , 326 ,
328 , 330 , 334 , 340 , 342 , 351 , 352 , 353
3FBOMF\PHF QOF>KDIB 328
7LICO>J >IDLOFQEJ CLO 325
7LICO>J @I>PPFCF@>QFLK 340
@E>O>@QBOFPQF@P LC 324
@LKQFKRLRP 352
ABCFKBA 324
BIBJBKQ>OV 325
BIBJBKQ>OV, FJMIBJBKQFKD 330
EFPQLOF@>I 353
FJ>DB MOL@BPPFKD 352
JLSFKD @BIIP 353
KBPQFKD 353
KLK-OB@Q>KDRI>O DOFAP >KA 351
MOL?>?FIFPQF@ 352
ORIBPBQP, ABCFKFKD 334
PBIC-OBMIF@>QFKD @BIIP 324
QFJB >KA 326
QTL-AFJBKPFLK>I 342
S>OF>QFLKP LC 351
#E>FK3E>MB @I>PP 211
@LKCFDROFKD 211
@I>PP (0OL@BPPFKD) 2 , 3 , 47
@LKPQOR@QLO 3 , 47
ABCFKBA 2
CRK@QFLK>IFQV 3
@LAB ARMIF@>QFLK, FKEBOFQ>K@B >KA 161
@LBCCF@FBKQ LC COF@QFLK 80
JR (_) 80
@LEBPFLK (CIL@HFKD) 309 , 312
FJMIBJBKQFKD 312
@LIIFPFLKP 190 , 234 , 235 , 241 , 301
"LU2$ >KA 234
>SLFAFKD FK DOLRM ?BE>SFLO 301
?BDFK#LKQ>@Q() CRK@QFLK (0"LU2$) 235
QLUF@IF?P >KA 241
@LJ?FK>QFLKP 306
@LJMIBU PVPQBJP 298 , 299 , 300 , 323 , 342 ,
446
'>JB LC ,FCB >P 342
@BIIRI>O >RQLJ>Q> 323
@LJMBQFQFLK/@LLMBO>QFLK @LJMLKBKQ 300
@LKKB@QFLKFPQ @LJMRQ>QFLK>I PVPQBJ 446
CBBA?>@H @LJMLKBKQ 300
DOLRM ?BE>SFLO 300
HBV MOFK@FMIBP LC 299
KLK-IFKB>OFQV @LJMLKBKQ 299
PRMBOLOD>KFPJP 299
#LJMIBUFQV @I>PP (7LICO>J @I>PPFCF@>QFLK) 341
CCADIH5H=CB5@ B95IHM C: *5HIF9 (&I>HB) 314
@LKKB@QBA PVPQBJP 249 , 253
CLO@B-AFOB@QBA DO>MEP 253
PQOFKDP 249
@LKKB@QFLKFPQ @LJMRQ>QFLK>I PVPQBJ 446
@LKPQO>FK() CRK@QFLK (0OL@BPPFKD) 280
@LKPQOR@QLO 3 , 47 , 75
>ODRJBKQP, >AAFKD QL 75
#LKQ>@Q L?GB@QP (0"LU2$) 236
#LKQ>@Q,FPQBKBO @I>PP (*"LU2$) 234
@LKQFKRLRP (@BIIRI>O >RQLJ>Q>) 352
@LKQO>@Q() CRK@QFLK (0OL@BPPFKD) 149
@LKQOLI (LC MEVPF@>I L?GB@QP) 447
#LKT>V'P '>JB LC ,FCB (+IFPB) 345
#LKT>V, *LEK 342
@LLOAFK>QB PVPQBJP 112 , 196
"LU2$ SP. 0OL@BPPFKD 196
#>OQBPF>K 112
0OL@BPPFKD >KA 112
MLI>O 112
@LLOA0FUBIP4L7LOIA() CRK@QFLK (0"LU2$) 197
@LLOA7LOIA4L0FUBIP() CRK@QFLK (0"LU2$) 198
#LOKBII !BOLK>RQF@>I ,>?LO>QLOV 448
@LP() CRK@QFLK (0OL@BPPFKD) 113
@LPFKB ILLHRM Q>?IBP 318
)KABU
486
#O>VLK 0EVPF@P 190
@OB>QB"LAV() CRK@QFLK (0"LU2$) 200
@OB>QB&FUQROB() CRK@QFLK (0"LU2$) 202
@OLPP() CRK@QFLK (06B@QLO @I>PP) 37
@OLPPLSBO (K>QRO>I PBIB@QFLK >IDLOFQEJP) 400 , 408
FJMIBJBKQFKD 408
D
A>JMFKD 131
A>JMFKD2>QFL PBQQFKD ("LU2$ GLFKQ BIBJBKQ) 223
$>OTFKF>K K>QRO>I PBIB@QFLK 394
ABDOBBP 101 , 103
O>AF>KP, @LKSBOQFKD QL 103
ABIQ> TBFDEQ 454
ABKPFQV 67
ABOFS>QFSBP 238
$BP@>OQBP, 2BKZ 112
ABPFOBA SBIL@FQV 264 , 274
ABPQOLV"LAV() CRK@QFLK (0"LU2$) 208
AFCCBOBKQF>QFLK 238
AFPPFM>QFSB CLO@B 80
AFPQ() CRK@QFLK (06B@QLO @I>PP) 37
AFPQ>K@B GLFKQP 222
$FPQ>K@B*LFKQ$BC ("LU2$ GLFKQ QVMB) 223
AFPQOF?RQFLKP, @RPQLJ 14
AFPQOF?RQFLKP, KLK-RKFCLOJ 7 , 8 , 14 , 15 , 16 , 17
-LKQB #>OIL JBQELA 16
0BOIFK KLFPB 17
@OB>QFKD TFQE >OO>VP 8
@RPQLJ 14
MOL?>?FIFQV >KA 7
NR>IFCVFKD O>KALJ S>IRBP 15
AFPQOF?RQFLKP, KLOJ>I 11
AFS() CRK@QFLK (06B@QLO @I>PP) 41
ALQ MOLAR@Q (06B@QLO) 282 , 283 , 284
ABCFKBA 283
QEBQ> 284
ALQ PVKQ>U 35
ALQ() CRK@QFLK (06B@QLO @I>PP) 37
AO>D CLO@B 83
AVK>JF@ (?LAV QVMB) 199
E
B@LPVPQBJ PFJRI>QFLK DBKBQF@ >IDLOFQEJP 392 , 435 ,
437 , 438 , 439
DBKLQVMB 438
IFCBPM>KP, S>OVFKD 437
MEBKLQVMB 438
OBMOLAR@QFLK 439
PBIB@QFLK 439
BCCF@FBK@V 315 , 316 , 317 , 318 , 319
"FD / .LQ>QFLK 315
"FD / .LQ>QFLK .-3NR>OBA 315
?FK-I>QQF@B PM>QF>I PR?AFSFPFLK 316
J>D3N() CRK@QFLK (06B@QLO @I>PP). 318
PFKB/@LPFKB ILLHRM Q>?IBP 318
QBJMLO>OV L?GB@QP >KA 319
BIBJBKQ>OV @BIIRI>O >RQLJ>Q> 325 , 330 , 332 , 333
, 336
AO>TFKD 336
BADB @>PBP >KA 332
DBKBO>QFLKP, J>FKQ>FKFKD FKQBDOFQV LC 333
FJMIBJBKQFKD 330
BJFQQBO 146
BKA#LKQ>@Q() CRK@QFLK (0"LU2$) 235
BKA3E>MB() CRK@QFLK (0OL@BPPFKD) 217
BNRFIF?OFRJ 64
%R@IFA 27 , 355
%R@IFAB>K DBLJBQOV 355 , 358
CO>@Q>IP >KA 358
%R@IFAB>K SB@QLO 27
%RIBO FKQBDO>QFLK 239 , 240
PVJMIB@QF@ %RIBO ("LU2$) 240
%RIBO, ,BLKE>OA 239
BSLIRQFLK 390 , 391 , 394
$>OTFKF>K K>QRO>I PBIB@QFLK 394
DBKBQF@ >IDLOFQEJP 391
JLABIFKD 390
BSLIRQFLK>OV @LJMRQFKD 392
%SLISBA 6FOQR>I #OB>QROBP (3FJP) 430
BU@IRPFSB LO (8/2) 466
BUFQ @LKAFQFLKP CLO OB@ROPFLK 361
BUM>KA() CRK@QFLK (0OL@BPPFKD) 149
4EB .>QROB LC #LAB (S005)
487
%UMILOFKD %JBODBK@B (2BPKF@H/3FISBOJ>K) 345
BUQBKAP HBVTLOA (0OL@BPPFKD) 164
F
C>@QLOF>I 359
CBBA-CLOT>OA JLABI (KBRO>I KBQTLOHP) 448 , 473
>KFJ>QFKD 473
&FPF@> 192
CFQKBPP CRK@QFLKP (K>QRO>I PBIB@QFLK >IDLOFQEJP) 397 ,
414 , 416 , 417 , 427 , 429 , 436
>SLFA>K@B LC L?PQ>@IBP >KA 427
ABPFDK VLRO LTK 416
B@LPVPQBJ PFJRI>QFLKP >KA 436
BSLISFKD CLO PMB@FCF@ >QQOF?RQBP 429
BUMLKBKQF>I SP. IFKB>O 414
OL?LQF@ BKPI>SBJBKQ LC ERJ>KFQV >KA 417
CFUQROB ("LU2$ BIBJBKQ) 194 , 201 , 202
>QQ>@EFKD QL ?LAV BIBJBKQ 202
@OB>QFKD 201
&I>HB, '>OV 314
CIL@HFKD 308 , 309 , 315 , 316
?FK-I>QQF@B PM>QF>I PR?AFSFPFLK 316
MBOCLOJ>K@B >KA 315
ORIBP LC 309
&IL@HP, (BOAP, >KA 3@ELLIP: ! $FPQOF?RQBA "BE>SFLO>I
-LABI (2BVKLIAP)
308
CILT CFBIA CLIILTFKD 276 , 277
OBPLIRQFLK >KA 277
CIRFA OBPFPQ>K@B, JLABIFKD 83
CLO ILLMP 150
!OO>V,FPQ L?GB@QP >KA 150
BKE>K@BA 150
CLO@B >@@RJRI>QFLK 69
CLO@B-AFOB@QBA DO>MEP 253
CLO@BP 63 , 64 , 68 , 71 , 73 , 77 , 78 , 80 , 83 ,
89 , 127 , 131 , 134 , 173 , 178 , 232 , 260 , 263
(LLHB'P I>T 134
.BTQLK'P I>TP LC JLQFLK 63
>@@RJRI>QFLK LC 68
>MMIV&LO@B() CRK@QFLK 232
>MMIVFKD QL L?GB@QP 71
>MMIVFKD QL PFKDIB L?GB@QP FK > PVPQBJ 178
>RQLKLJLRP >DBKQP >KA 260
@OB>QFKD 73
A>JMFKD 131
ABCFKBA 63
BNRFIF?OFRJ 64
CIRFA OBPFPQ>K@B 83
COF@QFLK, JLABIFKD 80
DO>SFQV, JLABIFKD 77
JLABIP LC, ?RFIAFKD 78
M>OQF@IB PVPQBJP TFQE 173
PMOFKDP 134
PQBBOFKD 263
QBOJFK>I SBIL@FQV 64
QOFDLKLJBQOV >KA 127
RKFSBOP>I DO>SFQ>QFLK>I @LKPQ>KQ 89
CLO@BP, JLABIFKD 77
OB>I CLO@BP 77
CLOJRI>B, BS>IR>QFKD FK @LAB 79
"F57H5@ #9CA9HFM C: *5HIF9, /<9 (->KABI?OLQ) 356
CO>@Q>IP 355 , 356 , 357 , 358 , 366 , 374 , 375 ,
382
+L@E @ROSB 366
,-PVPQBJP >KA 382
ABCFKBA 356
CFKB PQOR@QROB LC 358
OB@ROPFLK 358
PBIC-OBMIF@>QFKD ?BE>SFLO LC 357
PQL@E>PQF@ 358
QO>KPCLOJ>QFLK J>QOFU (0OL@BPPFKD) 375
QOBBP >KA 374
COBNRBK@V (LC LP@FII>QFLKP) 119
COBNRBK@V(W PBQQFKD ("LU2$ GLFKQ BIBJBKQ) 223
COF@QFLK 79 , 80 , 81 , 83 , 84 , 131
>MMIVFKD QL >K L?GB@Q 84
@LBCCF@FBKQ LC COF@QFLK 80
A>JMFKD 131
ABQBOJFKFKD AFOB@QFLK/J>DKFQRAB LC 80
CLOJRI> CLO 79
JLABIFKD TFQE CLOJRI>B 80
JR (_) 80
KLOJ>I CLO@B 81
)KABU
488
OEL (a) 83
CRK@QFLK>IFQV 3
CRK@QFLKP 54
PQ>QF@ SP. KLK-PQ>QF@ 54
G
'>IFIBL 77
'>JB LC ,FCB 324 , 342 , 343 , 344
AO>TFKD 344
ORIBP LC 343
'>OAKBO, ->OQFK 342
'>RPP, #>OI &OFBAOF@E 11
'>RPPF>K AFPQOF?RQFLK 11
DBKBQF@ >IDLOFQEJP 391 , 392 , 394 , 395 , 397 ,
409 , 413 , 414 , 420 , 435
$>OTFKF>K K>QRO>I PBIB@QFLK 394
3J>OQ 2L@HBQP (4ELOM) 420
?RFIAFKD 409
ABCFKBA 391
B@LPVPQBJ PFJRI>QFLK 392 , 435
CFQKBPP >IDLOFQEJP, JLAFCVFKD 414
FKQBO>@QFSB PBIB@QFLK 391
JLAFCVFKD 413
JRQ>QFLK O>QB, S>OVFKD 413
MLMRI>QFLK J>UFJRJ, S>OVFKD 413
MLMRI>QFLKP, @OB>QFKD 395
MROMLPB LC 392
PBIB@QFLK, FJMIBJBKQFKD 397
QO>AFQFLK>I 391
DBKLQVMB (K>QRO>I PBIB@QFLK >IDLOFQEJP) 395 , 417 ,
438
B@LPVPQBJ PFJRI>QFLK 438
JLAFCVFKD 417
DBLJBQOF@ SB@QLO 27
DBQ!KDIB() CRK@QFLK (0"LU2$) 207
DBQ"LAV,FPQ() CRK@QFLK (7LOIA @I>PP) 203
DBQ"LAV0FUBI#LLOA() CRK@QFLK (0"LU2$) 207
DBQ'OLRKA"LAV() CRK@QFLK ("LU2$ GLFKQ BIBJBKQ) 229
DO>SFQV 88 , 89 , 94 , 128 , 244
'O>SFQV"BE>SFLO (QLUF@IF?P) 244
FJMIBJBKQFKD JLABI LC 89
JLABIFKD 88
JLABIFKD OB>IFQV SP. >O?FQO>OV S>IRBP 128
JLABIFKD TFQE QOFDLKLJBQOV 128
MI>@FKD IFJFQP LK JLABI LC 94
RKFSBOP>I DO>SFQ>QFLK>I @LKPQ>KQ 89
'O>SFQV"BE>SFLO @I>PP (QLUF@IF?P) 244
DOFA (@BIIRI>O >RQLJ>Q>) 324
DOLRM ?BE>SFLO 300 , 301 , 306 , 308
@LIIFPFLKP, >SLFAFKD 301
@LJ?FK>QFLKP 306
CIL@HFKD 308
H
EB>AFKD2$() CRK@QFLK (06B@QLO @I>PP) 37 , 112
EBOBAFQV (K>QRO>I PBIB@QFLK) 394 , 399 , 400 , 401
@OLPPLSBO 400
FJMIBJBKQFKD 399
JRQ>QFLK 401
EFPQLOF@>I (@BIIRI>O >RQLJ>Q>) 353
(LADFK, 2L?BOQ 186
(LII>KA, *LEK 392
(LLHB'P I>T 134 , 135
CLOJRI> CLO BUMOBPPFKD 135
(LLHB, 2L?BOQ 134
I
FJ>DB MOL@BPPFKD (@BIIRI>O >RQLJ>Q>) 352
FJ>DB QBUQROBP 183 , 184 , 186
0)J>DB L?GB@QP (0OL@BPPFKD) 184
0.' CLOJ>Q >KA 184
?IBKA JLABP 186
FKCFKFQB JLKHBV QEBLOBJ 392
FKEBOFQ>K@B 144 , 160 , 163 , 164 , 165 , 166
>AAFKD CRK@QFLK>IFQV QL PRMBO@I>PP L?GB@QP 165
BUQBKAP HBVTLOA (0OL@BPPFKD) 164
FJMIBJBKQFKD 166
LSBOOFAFKD PRMBO@I>PP CRK@QFLKP 165
PR?@I>PP 163
PRMBO() CRK@QFLK (0OL@BPPFKD) 164
4EB .>QROB LC #LAB (S005)
489
PRMBO@I>PPBP 163
PVKQ>U CLO 163
FKQBDO>QFLK 238 , 239 , 240
%RIBO FKQBDO>QFLK 239
2RKDB-+RQQ> JBQELA 240
)KQBO>@QFLK TFQE 'OLRMP LC !RQLKLJLRP #E>O>@QBOP
(2BVKLIAP)
316
FKQBO>@QFSB PBIB@QFLK DBKBQF@ >IDLOFQEJP 391 , 431 ,
433 , 434
QFJB I>D >KA 434
RPBO FKQBO>@QFLK >KA 433
FKQBOC>@BP 234
FQBO>QFKD 152 , 153
)QBO>QLO @I>PP (*>S>) 153
OBJLSFKD BIBJBKQP FK CLO ILLMP 152
)QBO>QLO @I>PP (*>S>) 153
F4RKBP SFPR>IFWBO 186
J
*>S> 192
*"LU2$ 192 , 195 , 234
#LKQ>@Q,FPQBKBO @I>PP 234
CRII AL@RJBKQ>QFLK CLO 195
GLFKQ ("LU2$ BIBJBKQ) 194 , 222 , 225 , 228
AFPQ>K@B 222
JLRPB QVMB 228
OBSLIRQB QVMB 225
K
HFKBJ>QF@ (?LAV QVMB) 199 , 231
-LRPB*LFKQP >KA 231
+IFPB, 3QBSBK 345
+L@E @ROSB 366 , 369
FJMIBJBKQFKD 369
+RQQ>, -. 7. 240
L
,-PVPQBJP 382 , 383 , 386
@LJMLKBKQP LC 383
QO>KPI>QFKD FKQL @LAB 386
,>MI>@B, 0FBOOB-3FJLK 11
,>MI>@F>K AFPQOF?RQFLK 11
IB>OKFKD @LKPQ>KQ 455
(95FB=B; ,FC79GG=B; (3EFCCJ>K) 160
IBOM() CRK@QFLK (06B@QLO @I>PP) 37
IFJFQ() CRK@QFLK (06B@QLO @I>PP) 37 , 51
,FKABKJ>VBO PVPQBJP 382
,FKABKJ>VBO, !OFPQFA 382 , 389
IFKB>OIV PBM>O>?IB MOL?IBJP 466
IL@>QFLKP 31
>P SB@QLOP 31
IL@H() CRK@QFLK (QLUF@IF?P) 247
IL@LJLQFLK 263
,LP !I>JLP .>QFLK>I ,>?LO>QLOV 324
,R@>PCFIJ ,QA. 143
,ZSV CIFDEQ 14 , 15
FJMIBJBKQFKD TFQE >OO>VP 15
FJMIBJBKQFKD TFQE NR>IFCVFKD O>KALJ S>IRBP 15
M
J=M S>OF>?IB (6B@2 @I>PP) 220
J>D() CRK@QFLK (06B@QLO @I>PP) 43 , 318
J>D3N() CRK@QFLK SP. 318
->DKBQLPMEBOB 186
J>DKFQRAB (LC SB@QLOP) 42 , 51
IFJFQFKD 51
J>D3N() CRK@QFLK (06B@QLO @I>PP). 318
->KABI?OLQ, "BKLFQ 356
J>M() CRK@QFLK (0OL@BPPFKD) 20 , 117
LP@FII>QFLK >KA 117
->OUBO, 2F@>OA 192
J>PP 67 , 70
JLABIFKD 70
RKFQP LC JB>PROBJBKQ, ABCFKFKD 70
TBFDEQ SP. 67
J>QFKD MLLIP (K>QRO>I PBIB@QFLK) 397 , 405
@OB>QFKD 397
)KABU
490
FJMIBJBKQFKD 405
-@#RIIL@E, 7>OOBK 3. 445
JB>K 11
JBQELAP, PQ>QF@ SP. KLK-PQ>QF@ 54
JFIIFP() CRK@QFLK (0OL@BPPFKD) 117
-LKPQBO @ROSB 367
-LKQB #>OIL JBQELA 16
JLQFLK 45 , 104 , 112
>KDRI>O 104
EB>AFKD2$() CRK@QFLK (06B@QLO @I>PP) 112
JLRPB GLFKQ ("LU2$ GLFKQ QVMB) 228
PBQ4O>KPCLOJ() CRK@QFLK 228
JLRPB GLFKQ ("LU2$ *LFKQ QVMB) 228
JLSFKD @BIIP (@BIIRI>O >RQLJ>Q>) 353
JR (_) 11 , 80
JRIQ() CRK@QFLK (06B@QLO @I>PP) 40
FJMIBJBKQ>QFLK 40
JRQ>QFLK (K>QRO>I PBIB@QFLK >IDLOFQEJP) 401 , 409
FJMIBJBKQFKD 409
O>QB LC 401
N
K>QRO>I CO>@Q>IP 374
K>QRO>I MEBKLJBK> 2 , 7 , 17 , 67 , 70 , 73 , 77 ,
78 , 80 , 83 , 88 , 89 , 127 , 128 , 185 , 260 , 299 ,
300 , 308 , 324 , 355 , 374 , 382 , 383 , 390 , 391
, 394 , 435
$>OTFKF>K K>QRO>I PBIB@QFLK 394
,-PVPQBJP >KA 382
.BTQLK'P PB@LKA I>T, JLABIFKD 67
0BOIFK KLFPB >KA 17
>KQP, JLABIFKD 299
>RQLKLJLRP >DBKQP 260
@BIIRI>O >RQLJ>Q> 324
B@LPVPQBJP, JLABIFKD 435
BSLIRQFLK 390
CIL@HFKD 308
CIRFA OBPFPQ>K@B, JLABIFKD 83
CLO@BP, JLABIFKD 73 , 77
CO>@Q>IP 355
COF@QFLK 80
DBKBQF@ >IDLOFQEJP 391
DO>SFQV 77 , 88 , 89
DOLRM ?BE>SFLO 300
J>PP, JLABIFKD 70
JLABIFKD OB>IFQV SP. >O?FQO>OV S>IRBP 128
JLABIFKD TFQE O>KALJ T>IHP 2
JLABIFKD TFQE QEB O>KALJ() CRK@QFLK 7
MEVPF@P (OB>I TLOIA), JLABIFKD 78
MFSLQP, JLABIFKD 127
MI>KQ DOLTQE, JLABIFKD 383
PJLHB, JLABIFKD TFQE M>OQF@IB PVPQBJP 185
QOBBP >KA 374
K>QRO>I PBIB@QFLK >IDLOFQEJP 394 , 395 , 397 , 398 ,
399
CFQKBPP CRK@QFLKP 397
J>QFKD MLLIP, @OB>QFKD 397
MLMRI>QFLKP, @OB>QFKD 395
MOL?>?FIFQV 398
OBMOLAR@QFLK 399
K>QRO>IIV LOABOBA PBNRBK@B LC KRJ?BOP 17
KBFDE?LOELLA (@BIIRI>O >RQLJ>Q>) 325
KBPQFKD (@BIIRI>O >RQLJ>Q>) 353
KBRO>I KBQTLOHP 444 , 445 , 446 , 447 , 448 , 449 ,
467 , 468 , 473 , 475
>@QFS>QFLK CRK@QFLKP LC 449
>KFJ>QFKD 473
?>@HMOLM>D>QFLK 468
@LKKB@QFLKFPQ @LJMRQ>QFLK>I PVPQBJ 446
AF>DO>JJFKD 468
IB>OKFKD >KA 446
KBQTLOHP LC MBO@BMQOLKP 467
M>QQBOK OB@LDKFQFLK 445
MBO@BMQOLK 448
OB>I SP. PFJRI>QBA 475
OBFKCLO@BJBKQ IB>OKFKD 447
PRMBOSFPBA IB>OKFKD 446
RKPRMBOSFPBA IB>OKFKD 447
RPBP LC 447
*9K '=B8 C: .7=9B79, A (7LICO>J) 325
KBT LMBO>QLO (L?GB@QP) 4
.BTQLK'P CFOPQ I>T 64 , 65
06B@QLO @I>PP >KA 65
.BTQLK'P PB@LKA I>T 67
4EB .>QROB LC #LAB (S005)
491
.BTQLK'P QEFOA I>T 65 , 66
06B@QLO @I>PP >KA 66
.BTQLK, )P>>@ 63
KBUQ'>RPPF>K() CRK@QFLK (2>KALJ @I>PP) 13
ABC>RIQ JB>K/PQ>KA>OA ABSF>QFLK PBQQFKDP LC 13
KLFPB() CRK@QFLK (0OL@BPPFKD) 18
>ODRJBKQP CLO 18
KLFPB$BQ>FI() CRK@QFLK (0OL@BPPFKD) 18
KLK-IFKB>OIV PBM>O>?IB MOL?IBJP 466
KLK-OB@Q>KDRI>O DOFAP (@BIIRI>O >RQLJ>Q>) 351
KLK-RKFCLOJ AFPQOF?RQFLKP 7 , 8 , 14 , 15 , 16 , 17
-LKQB #>OIL JBQELA 16
0BOIFK KLFPB 17
@OB>QFKD TFQE >OO>VP 8
@RPQLJ 14
MOL?>?FIFQV >KA 7
NR>IFCVFKD O>KALJ S>IRBP 15
KLOJ>I AFPQOF?RQFLK 11
KLOJ>I CLO@B 81
KLOJ>I MLFKQP 291 , 295
PBOFBP LC, CLO M>QE CLIILTFKD 295
KLOJ>IFW>QFLK 43 , 398
J>QFKD MLLIP, @OB>QFKD TFQE 398
KLOJ>IFWB() CRK@QFLK (06B@QLO @I>PP) 44
.LOSFD, 0BQBO 445
O
L?GB@Q 2 , 4 , 92 , 349
@BIIP FK @BIIRI>O >RQLJ>Q> >P 349
ABCFKBA 2
FKQBO>@QFLK ?BQTBBK 92
KBT LMBO>QLO 4
L?GB@Q-LOFBKQBA MOLDO>JJFKD 2 , 35 , 72 , 137 ,
144 , 155 , 160 , 168 , 176 , 349 , 419
@BIIRI>O >RQLJ>Q> >KA 349
@I>PP 2
@I>PPBP LC RPBO-ABCFKBA L?GB@QP, @OB>QFKD 155
ALQ PVKQ>U 35
DBKLQVMB/MEBKLQVMB L?GB@QP >KA 419
FKEBOFQ>K@B 144 , 160
FKPQ>K@BP LC PR?@I>PPBP, @OB>QFKD 168
J>FKQ>FKFKD BK@>MPRI>QFLK 176
L?GB@Q 2
MLIVJLOMEFPJ 144 , 160 , 168
OBCBOBK@BP QL SP. @LMFBP LC L?GB@QP 72
OBSFBT LC 2
PQOR@QROBP, @ELPFKD ?BQTBBK 137
LMQFJFW>QFLK 318 , 319
J>D3N() CRK@QFLK (06B@QLO @I>PP). 318
PFKB/@LPFKB ILLHRM Q>?IBP 318
QBJMLO>OV L?GB@QP >KA 319
LP@FII>QFLK 116 , 117 , 119 , 120 , 122 , 124
>JMIFQRAB 117
>KDRI>O SBIL@FQV >KA 119
COBNRBK@V LC 119
LK QTL >UBP 120
MBOFLA 117
PFJMIB E>OJLKF@ JLQFLK 117
PFJRI>QFKD TFQE PFKB @ROSBP 116
S>OVFKD 124
T>SBP 122
LSBOP>JMIFKD 14
P
M>OQF@IB PVPQBJP 143 , 144 , 145 , 146 , 149 , 155 ,
156 , 157 , 170 , 173 , 178 , 185 , 240 , 246
!OO>V,FPQ, RPFKD 149
6BOIBQ FKQBDO>QFLK >KA 240
>AA0>OQF@IB() CRK@QFLK (QLUF@IF?P) 246
>MMIVFKD CLO@B QL PFKDIB M>OQF@IBP FK 178
@I>PP CLO, @OB>QFKD 155
AB>A M>OQF@IBP, @EB@HFKD CLO 146
BJFQQBO 146
CLO@BP >KA 173
IFCBPM>K LC M>OQF@IBP 146
JRIQFMIB PVPQBJP, LOD>KFWFKD 157
LOFDFK MLFKQ (LC M>OQF@IBP) 156
M>OQF@IBP FK 145
MLIVJLOMEFPJ, RPFKD 170
MROMLPB LC 144
PJLHB, JLABIFKD 185
)KABU
492
M>OQF@IBP 145 , 146 , 147 , 178 , 244 , 245
6BOIBQ0>OQF@IB2$ L?GB@Q (QLUF@IF?P) 245
>MMIVFKD CLO@B QL PFKDIB M>OQF@IBP FK 178
AB>QE, @EB@HFKD CLO 146
IFCBPM>K LC 146
QBPQFKD 147
QLUF@IF?P FJMIBJBKQ>QFLK LC 244
M>QE 286
M>QE CLIILTFKD 286 , 288 , 291 , 292 , 294
@ROOBKQ AFPQ>K@B COLJ M>QE, CFKAFKD 288
JRIQFMIB PBDJBKQP 294
KLOJ>I MLFKQP 291
M>QECFKAFKD SP. 286
Q>ODBQ, ABQBOJFKFKD 292
M>QECFKAFKD 286
M>QQBOK OB@LDKFQFLK 445 , 450
MBO@BMQOLK >KA 450
0"LU2$ EBIMBO @I>PP 192 , 196 , 197 , 207
@LLOAFK>QB PVPQBJP, @LKSBOQFKD ?BQTBBK 197
@OB>QB7LOIA() CRK@QFLK 196
DBQ"LAV0FUBI#LLOA() CRK@QFLK (0"LU2$) 207
MBO@BMQOLK 448 , 450 , 455 , 456 , 460 , 466 ,
467
?F>P FKMRQ 450
BOOLO @>I@RI>QFLKP >KA 450
FJMIBJBKQFKD 448
IB>OKFKD @LKPQ>KQ 455
IFKB>OIV PBM>O>?IB MOL?IBJP >KA 466
KBQTLOHP LC 467
M>QQBOK OB@LDKFQFLK TFQE 450
PQBBOFKD >KA 460
QO>FKFKD 456
MBOCLOJ>K@B 315 , 316 , 317 , 318 , 319
"FD / .LQ>QFLK 315
"FD / .LQ>QFLK .-3NR>OBA 315
?FK-I>QQF@B PM>QF>I PR?AFSFPFLK 316
J>D3N() CRK@QFLK (06B@QLO @I>PP). 318
PFKB/@LPFKB ILLHRM Q>?IBP 318
QBJMLO>OV L?GB@QP >KA 319
MBOFLA 117 , 122
ABCFKBA FK MFUBIP O>QEBO QE>K CO>JBP 122
0BOIFK KLFPB 17 , 18 , 20 , 22 , 279
CILT CFBIA CLIILTFKD >KA 279
J>M() CRK@QFLK 20
K>QRO>I MEBKLJBK>, JLABIFKD TFQE 17
KLFPB() CRK@QFLK (0OL@BPPFKD) 18
QTL-AFJBKPFLK>I 22
MEBKLQVMB (K>QRO>I PBIB@QFLK >IDLOFQEJP) 395 , 417 ,
438
B@LPVPQBJ PFJRI>QFLK 438
MEVPF@P 78 , 189 , 190
@LIIFPFLKP 190
JLABIFKD 78
LMBK-PLRO@B IF?O>OFBP CLO 189
MEVPF@P IF?O>OFBP 189 , 190
"LU2$ 190
MF (`) 103
0) S>OF>?IB (0OL@BPPFKD) 103
0)J>DB L?GB@QP (0OL@BPPFKD) 184
0FQQP, 7>IQBO 445
MFSLQP, JLABIFKD 127
MI>KQ DOLTQE, JLABIFKD 383
0.' DO>MEF@ CFIB CLOJ>Q 184
MLI>O @LLOAFK>QBP 112 , 113
#>OQBPF>K @LLOAFK>QBP, @LKSBOQFKD QL 113
0LIVDLK3E>MB @I>PP 215
>P IFPQ LC SB@QLOP 215
MLIVJLOMEFPJ 144 , 160 , 168 , 170
@OB>QFKD L?GB@Q FKPQ>K@BP TFQE 170
MLM->QOFU() CRK@QFLK (0OL@BPPFKD) 375
MLMRI>QFLKP (DBKBQF@ >IDLOFQEJP) 395 , 402 , 435
@OB>QFKD 395
B@LPVPQBJ PFJRI>QFLKP >KA 435
BIBJBKQP LC 395
FJMIBJBKQFKD 402
MLPQ3LISB() CRK@QFLK (0"LU2$) 235
MOB3LISB() CRK@QFLK (0"LU2$) 235
MOL?>?FIFPQF@ (@BIIRI>O >RQLJ>Q>) 352
MOL?>?FIFQV 7 , 8 , 11 , 352 , 392 , 398
@BIIRI>O >RQLJ>Q> ?>PBA LK 352
FKCFKFQB JLKHBV QEBLOBJ 392
JB>K 11
K>QRO>I PBIB@QFLK >IDLOFQEJP >KA 398
KLK-RKFCLOJ AFPQOF?RQFLKP >KA 7
4EB .>QROB LC #LAB (S005)
493
KLOJ>I AFPQOF?RQFLKP 11
PQ>KA>OA ABSF>QFLK 11
MOL?>?FIFQV LC QEB CFQQBPQ 7
0OL@BPPFKD 2 , 12 , 18 , 30 , 46 , 54 , 102 , 103 ,
110 , 111 , 112 , 117 , 163 , 183 , 184 , 192 , 196 ,
203 , 205 , 241
"LU2$ >KA 192
"LU2$ L?GB@QP, >AAFKD QL MOLGB@QP 205
*"LU2$ 192
//0 LKIFKB QRQLOF>I 46
0)J>DB L?GB@QP 184
2>KALJ @I>PP 12
>KDIBP, JB>PROFKD FK 102
>Q>K() CRK@QFLK 110
>Q>K2() CRK@QFLK 111
?LAV IFPQP, J>FKQ>FKFKD 203
@I>PP FKEBOFQ>K@B, PVKQ>U CLO 163
@LLOAFK>QB PVPQBJP >KA 112
@LLOAFK>QB PVPQBJP SP. "LU2$ 196
FK@LOMLO>QFKD FJ>DBP FKQL MOLGB@QP 183
JB>PROFKD QFJB FK 117
KLFPB() CRK@QFLK 18
KLFPB$BQ>FI() CRK@QFLK 18
O>AF>KP() CRK@QFLK 103
OBSFBT LC L?GB@Q-LOFBKQBA MOLDO>JJFKD TFQE 2
OLQ>QFLK QRQLOF>I 103
PQ>QF@ SP. KLK-PQ>QF@ JBQELAP 54
QLUF@IF?P >KA 241
SB@QLOP >KA 30
0ORPFKHFBTF@W, 0OWBJVP[>T 389
MPBRAL-O>KALJ KRJ?BOP 7 , 17
0BOIFK KLFPB >KA 17
MRPE->QOFU() CRK@QFLK (0OL@BPPFKD) 375
06B@QLO @I>PP (0OL@BPPFKD) 30 , 37 , 38 , 40 , 41 ,
43 , 44 , 51 , 65 , 66 , 112 , 194
"LU2$ SP. 194
.BTQLK'P CFOPQ I>T >KA 65
.BTQLK'P QEFOA I>T >KA 66
AFS() CRK@QFLK 41
CRK@QFLK IFPQ CLO 37
EB>AFKD2$() CRK@QFLK 112
IFJFQ() CRK@QFLK 51
J>D() CRK@QFLK 43
J>QEBJ>QF@>I CRK@QFLKP CLO 37
JRIQ() CRK@QFLK 40
KLOJ>IFWB() CRK@QFLK 44
PR?() CRK@QFLK 38
0VQE>DLO>P 42
0VQE>DLOB>K QEBLOBJ 42
Q
NR>IFCVFKD O>KALJ S>IRBP 15 , 16
-LKQB #>OIL JBQELA 16
R
O>AF>KP 102 , 103
@LKSBOQFKD COLJ ABDOBBP 103
O>AF>KP() CRK@QFLK (0OL@BPPFKD) 103
2>KALJ @I>PP (0OL@BPPFKD) 12 , 13
KBUQ'>RPPF>K() CRK@QFLK 13
2>KALJ @I>PP (7LICO>J @I>PPFCF@>QFLK) 341
O>KALJ KRJ?BO DBKBO>QLOP 3 , 6 , 7 , 14
@RPQLJ AFPQOF?RQFLKP, @OB>QFKD 14
KLK-RKFCLOJ AFPQOF?RQFLKP, @OB>QFKD 7
MPBRAL-O>KALJ KRJ?BOP 7
O>KALJ() CRK@QFLK 3
RKFCLOJ KRJ?BO AFPQOF?RQFLKP >KA 6
O>KALJ T>IHP 1 , 14
'>RPPF>K 14
,ZSV CIFDEQ 14
LSBOP>JMIFKD 14
O>KALJ() CRK@QFLK 3 , 7 , 8
K>QRO>I MEBKLJBK>, JLABIFKD TFQE 7
KLK-RKFCLOJ AFPQOF?RQFLKP, @OB>QFKD TFQE 8
O>KALJ2$() CRK@QFLK (06B@QLO @I>PP) 37
O>KALJ3$() CRK@QFLK (06B@QLO @I>PP) 37
OB>I CLO@BP 77
OB@ROPFLK 358 , 359 , 361 , 366
!OO>V,FPQ L?GB@QP >KA 366
BUFQ @LKAFQFLKP 361
C>@QLOF>I 359
)KABU
494
FJMIBJBKQFKD 359
2BBSBP, 7FIIF>J 4. 143
OBFKCLO@BJBKQ IB>OKFKD (KBRO>I KBQTLOHP) 447
OBFKCLO@BJBKQ IB>OKFKD(KBRO>I KBQTLOHP) 463
OBJLSB() CRK@QFLK (!OO>V,FPQ @I>PP) 151
2BMBQFQFLK @I>PP (7LICO>J @I>PPFCF@>QFLK) 340
OBMOLAR@QFLK (K>QRO>I PBIB@QFLK >IDLOFQEJP) 399 , 407
, 439
B@LPVPQBJ PFJRI>QFLK 439
FJMIBJBKQFKD 407
OBMRIPFLK 302
DOLRM ?BE>SFLO >KA 302
2BPKF@H, -FQ@EBI 262 , 345
OBPLIRQFLK, CILT CFBIA CLIILTFKD >KA 277
OBPQ IBKDQE ("LU2$ GLFKQ BIBJBKQ) 223
OBSLIRQB GLFKQ QVMB ("LU2$) 225 , 226
MOLMBOQFBP, @LKCFDROFKD 226
2BSLIRQB*LFKQ$BC L?GB@Q ("LU2$ GLFKQ BIBJBKQ) 226
2BVKLIAP, #O>FD 261 , 286
M>QE CLIILTFKD >IDLOFQEJ 286
OEL (a) 83
2LPBK?I>QQ, &O>KH 448
OLQ>QB() CRK@QFLK (0"LU2$) 207
OLQ>QB() CRK@QFLK (06B@QLO @I>PP) 37
OLQ>QFLK 104 , 109
MLFKQFKD QLT>OAP JLSBJBKQ 109
OLRIBQQB TEBBI MOL?>?FIFQV JBQELA 398
ORIBP (,-PVPQBJ @LJMLKBKQ) 383
ORIBPBQP CLO @BIIRI>O >RQLJ>Q> 334
2RKDB, #. 240
2RKDB-+RQQ> JBQELA 240
2RPPBII, 3QR>OQ *. 445
S
P@>I>O KLQ>QFLK, SP. SB@QLO KLQ>QFLK 33
P@>I>O MOLGB@QFLK 291
P@>I>O0FUBIP4L7LOIA() CRK@QFLK (0"LU2$) 198
P@>I>O7LOIA4L0FUBIP() CRK@QFLK (0"LU2$) 198
3@EJFAQ, +>OPQBK 241
3BBJ>KK, 'IBKK 445
PBIB@QFLK (K>QRO>I PBIB@QFLK >IDLOFQEJP) 394 , 397 ,
404 , 439
B@LPVPQBJ PFJRI>QFLK 439
FJMIBJBKQFKD 397 , 404
PBIC-OBMIF@>QFKD @BIIP 324
PBIC-PFJFI>OFQV LC CO>@Q>IP 357
PBM>O>QFLK (CIL@HFKD) 309 , 310
FJMIBJBKQFKD 310
PBQ'O>SFQV() CRK@QFLK (7LOIA @I>PP) 196
PBQ4O>KPCLOJ() CRK@QFLK ("LU2$) 228
3E>MB ("LU2$ BIBJBKQ) 200 , 201 , 220
ABCFKFKD 201
COF@QFLK >QQOF?RQB 200
IL@>I MLPFQFLK CLO 220
OBPQFQRQFLK >QQOF?RQB 200
PE>MB ("LU2$ BIBJBKQ) 194
PE>MBP 104 , 112 , 113
AFPMI>VFKD 112
JLSFKD TFQE MLI>O @LLOAFK>QBP 113
OLQ>QFKD 104
PELOQ O>KDB OBI>QFLKPEFMP 299 , 310
@LJMIBU PVPQBJP 299
CIL@HFKD ?BE>SFLO >KA 310
3FBOMF\PHF QOF>KDIB 328
3FBOMF\PHF, 7>@[>T 328
PFDJ> (b) 11
PFDK>I MOL@BPPFKD 447
3FISBOJ>K, "OF>K 345
PFJMIB E>OJLKF@ JLQFLK 117
3FJP, +>OI 430
PFK() CRK@QFLK (0OL@BPPFKD) 113
PFKB ILLHRM Q>?IBP 318
PFWB() CRK@QFLK (!OO>V,FPQ @I>PP) 152
3J>OQ 2L@HBQP (4ELOM) 420
PLCQ PBKPLOP 447
GC<75<HC5 108
PMIF@B() CRK@QFLK (0OL@BPPFKD) 149
PMOFKDP 134 , 135 , 136 , 246 , 247
(LLHB'P I>T 134
6BOIBQ#LKPQO>FKBA3MOFKD @I>PP (QLUF@IF?P) 246
6BOIBQ-FK$FPQ>K@B3MOFKD @I>PP (QLUF@IF?P) 246
6BOIBQ3MOFKD @I>PP (QLUF@IF?P) 246
4EB .>QROB LC #LAB (S005)
495
AFOB@QFLK LC CLO@B, ABQBOJFKFKD 136
IL@H() CRK@QFLK (QLUF@IF?P) 247
J>DKFQRAB LC CLO@B, ABQBOJFKFKD 135
OBPQ IBKDQE 136
QLUF@IF?P >KA 246
PQ>KA>OA ABSF>QFLK 11 , 12
@>I@RI>QFKD 12
S>OF>K@B 12
3Q>O 4OBH )): 4EB 7O>QE LC +E>K (1982) 143
PQ>QB (@BIIRI>O >RQLJ>Q>) 324
PQ>QF@ (?LAV QVMB) 199 , 209
PQ>QF@ CRK@QFLKP 55
PQBBOFKD ?BE>SFLOP 274 , 276 , 302 , 460
CILT CFBIA CLIILTFKD 276
DOLRM ?BE>SFLO >KA 302
MBO@BMQOLK CLO 460
T>KABOFKD 274
3QBBOFKD "BE>SFLOP CLO !RQLKLJLRP #E>O>@QBOP
(2BVKLIAP)
262
PQBBOFKD CLO@B 262 , 264 , 266 , 273
>OOFSFKD ?BE>SFLO >KA 273
ABPFOBA SBIL@FQV 264
J>DKFQRAB LC 266
PQBBOFKD MBO@BMQOLK 460 , 463
OBFKCLO@BJBKQ IB>OKFKD(KBRO>I KBQTLOHP) 463
PQBM() CRK@QFLK ("LU2$) 205
PQL@E>PQF@ CO>@Q>IP 358 , 374
QOBBP >P 374
3QOFKD"RCCBO @I>PP 385
PQOFKDP 251 , 385
3QOFKD"RCCBO @I>PP SP. 385
E>KDFKD COLJ CFUBA MLFKQP 251
PR?() CRK@QFLK (06B@QLO @I>PP) 38
PR?@I>PP 163 , 165
>AAFKD CRK@QFLK>IFQV QL PRMBO@I>PP L?GB@QP 165
PR?PBQ() CRK@QFLK (0OL@BPPFKD) 149
PRMBO() CRK@QFLK(0OL@BPPFKD) 164
PRMBO@I>PPBP 163 , 165 , 169 , 170
>AAFKD CRK@QFLK>IFQV TFQEFK PR?@I>PPBP 165
LSBOOFAFKD CRK@QFLKP COLJ 165 , 170
MLIVJLOMEFPJ >KA 169
PRMBOLOD>KFPJP 299
PRMBOSFPBA IB>OKFKD (KBRO>I KBQTLOHP) 446
PVJMIB@QF@ %RIBO ("LU2$) 240
T
Q>KDBKQ 110 , 111 , 112
>Q>K() CRK@QFLK (>O@Q>KDBKQ) 110
>Q>K2() CRK@QFLK 111
EB>AFKD2$() CRK@QFLK (06B@QLO @I>PP) 112
QBOJFK>I SBIL@FQV 64
QEBQ> 284
ALQ MOLAR@Q >KA 284
QEBQ> (^) 112
4ELOM, *BO 420
QFJB 117 , 326
@BIIRI>O >RQLJ>Q> >KA 326
JFIIFP() CRK@QFLK, JB>PROFKD TFQE 117
QFJB PBOFBP MOBAF@QFLK 447
QLUF@IF?P 241 , 242 , 244 , 246 , 249 , 253 , 256
!QQO>@QFLK"BE>SFLO @I>PP 256
"LU2$ SP. 241
6BOIBQ0EVPF@P @I>PP 244
6BOIBQ0EVPF@P2$ @I>PP 244
>QQO>@QFLK/OBMRIPFLK ?BE>SFLOP >KA 256
@LKKB@QBA PVPQBJP 249
ALTKIL>AFKD 242
CLO@B-AFOB@QBA DO>MEP 253
M>OQF@IBP, FJMIBJBKQFKD FK 244
PMOFKDP 246
TLOIA, ?RFIAFKD 244
QO>AFQFLK>I DBKBQF@ >IDLOFQEJP 391
QO>KPCLOJ>QFLK J>QOFU (0OL@BPPFKD) 375
4O>KPCLOJ>QFLKP QRQLOF>I (0OL@BPPFKD) 375
QO>KPI>QB() CRK@QFLK (0"LU2$) 207
QOBBP 374
QOFDLKLJBQOV 108 , 110 , 113 , 127
>Q>K() CRK@QFLK 110
@LP() CRK@QFLK (0OL@BPPFKD) 113
CLO@BP >KA 127
PFK() CRK@QFLK (0OL@BPPFKD) 113
GC<75<HC5 108
)KABU
496
Q>KDBKQ 110
4OLK (1982) 17
4ROQIB DO>MEF@P 386
/IFH@9G, /9FA=H9G, 5B8 /F5::=7 &5AG (2BPKF@H) 262
QTL-AFJBKPFLK>I @BIIRI>O >RQLJ>Q> 342 , 345
FJMIBJBKQFKD 345
U
5I>J, 3Q>KFP[>T 324
RKFCLOJ KRJ?BO AFPQOF?RQFLKP 6
5KFCLOJFQV @I>PP (7LICO>J @I>PPFCF@>QFLK) 340
RKFQ SB@QLOP 43
RKFSBOP>I DO>SFQ>QFLK>I @LKPQ>KQ 89
RKPRMBOSFPBA IB>OKFKD (KBRO>I KBQTLOHP) 447
RMA>QB() CRK@QFLK (QLUF@IF?P) 244
V
S>OF>K@B 12
S>OF>QFLK (K>QRO>I PBIB@QFLK) 394
6B@2 ("LU2$ BIBJBKQ) 194 , 195
>AAFKD SB@QLOP TFQE 195
J>KFQRAB, CFKAFKD 195
JRIQFMIVFKD SB@QLOP TFQE 195
KLOJ>IFWFKD SB@QLOP 195
P@>IFKD SB@QLOP TFQE 195
6B@2$ (QLUF@IF?P QVMB) 243 , 246
6BOIBQ0>OQF@IB2$ @I>PP >KA 246
J>QE CRK@QFLKP CLO 243
6B@3$ (QLUF@IF?P QVMB) 243
SB@QLO KLQ>QFLK, SP. P@>I>O KLQ>QFLK 33
SB@QLOP 27 , 28 , 30 , 31 , 33 , 39 , 40 , 42 , 43 ,
45 , 49 , 109 , 110 , 194 , 278 , 282
0OL@BPPFKD >KA 30
6B@2 ("LU2$ BIBJBKQ) 194
>@@BIBO>QFLK 49
>AAFKD 33
>P OFDEQ QOF>KDIBP 109
>PPL@F>QFSB/AFPQOF?RQFSB ORIBP CLO JRIQFMIF@>QFLK/
AFSFPFLK LC
42
?LRK@FKD ?>II PHBQ@E 28
@LJJRQ>QFSB/>PPL@F>QFSB ORIBP LC >AAFQFLK/
PR?QO>@QFLK TFQE
39
ABCFKBA 27
ALQ MOLAR@Q 282
CILT CFBIAP, @LJMRQFKD CLO 278
IL@>QFLKP >KA 31
J>DKFQRAB 42
JLQFLK, FJMIBJBKQFKD TFQE 33
JRIQFMIVFKD 40
KLOJ>IFW>QFLK 43
KLQ>QFLK 27
P@>IFKD 40
Q>KDBKQ 110
RKFQ SB@QLOP 43
SBIL@FQV >KA 31 , 45
19<=7@9G: !LD9F=A9BHG =B .MBH<9H=7 ,GM7<C@C;M
("O>FQBK?BOD)
262
SBIL@FQV 31 , 45 , 49 , 51 , 274
>@@BIBO>QFLK 49
>P SB@QLO 31
ABPFOBA, CLO >RQLKLJLRP >DBKQP 274
IFJFQFKD 51
6BOIBQ FKQBDO>QFLK 240 , 241
QLUF@IF?P 241
6BOIBQ#LKPQO>FKBA3MOFKD @I>PP (QLUF@IF?P) 246
6BOIBQ-FK$FPQ>K@B3MOFKD @I>PP (QLUF@IF?P) 246
6BOIBQ0>OQF@IB2$ L?GB@Q (QLUF@IF?P) 245
6BOIBQ0EVPF@P @I>PP (QLUF@IF?P) 242 , 244
@LOB BIBJBKQP LC 242
6BOIBQ0EVPF@P2$ @I>PP (QLUF@IF?P) 244
6BOIBQ3MOFKD @I>PP (QLUF@IF?P) 246
SFP@LRP CLO@B 83
SLK .BRJ>KK, *LEK 324
W
T>KABOFKD ?BE>SFLO (2BVKLIAP) 274
T>SBP 122 , 124
>KDRI>O SBIL@FQV, ABCFKFKD TFQE 122
S>OVFKD 124
4EB .>QROB LC #LAB (S005)
497
TBFDEQ 67 , 446
J>PP SP. 67
KBRO>I KBQTLOHP >KA 446
TEBBI LC CLOQRKB MOL?>?FIFQV JBQELA 398
7LICO>J @I>PPFCF@>QFLK 340 , 341
#LJMIBUFQV @I>PP 341
2>KALJ @I>PP 341
2BMBQFQFLK @I>PP 340
5KFCLOJFQV @I>PP 340
7LICO>J, 3QBMEBK 325 , 340
7LICO>J @I>PPFCF@>QFLK 340
BIBJBKQ>OV @BIIRI>O >RQLJ>Q> >IDLOFQEJ 325
7LOIA @I>PP ("LU2$) 194 , 196 , 203
@OB>QB7LOIA() CRK@QFLK (0"LU2$) 196
DBQ"LAV,FPQ() CRK@QFLK 203
X
8/2 (BU@IRPFSB LO) 466
)KABU
498