Dr. B R Ambedkar National Institute of Technology-Jalandhar GT Road Bye ass! Jalandhar -"##$""%un&ab'
Assignment( $) *ystem *imulation and +odeling ,*-#$# Deartment of ,omuter *cience and /ngineering Jan-June 0$"1
*ubmitted to(
*ubmitted by(
Mr. Manoj Kumar Assistant Professor Dept. of CS
Raj Kunwar Singh 12103060 CS !"ina# $ear%
1
,ontents *r. No.
R2GRA+
AG/ No.
.
&rite a Program to 'mp#ement Ran(om )um*er +enerator &rite a Program to 'mp#ement ,inear Congruentia# +enerator &rite a Program to 'mp#ement esting esting of Ran(om )um*ers.!Chi s/uare% &rite a Program to 'mp#ement Monte Car#o Simu#ation. &rite a Program to 'mp#ement Simu#ation of ,A+ Mo(e#. &rite a Program to 'mp#ement C4&4 Mo(e# &rite a Program to 'mp#ement Simu#ation of Sing#e 5ueue Serer S$stem &rite a Program to 'mp#ement Simu#ation of 2 5ueue Serer S$stem &rite a Program to 'mp#ement Simu#ation of 'nentor$ S$stem
33
10.
&rite a Program to 'mp#ement Simu#ation of e#ephoni8 s$stem.
3-
1. 2.
3. . -. 6. .
7.
*IGNAT3R/
3 -
11 13 16
23
2
". 4rite a rogram for rolling a fair dice that should generate a discrete random 5ariable! and dra6 the grah sho6ing the number of "7s 07s )7s819s. Theory ( A ran(om num*er generator !R)+% is a 8omputationa# 8omputationa# or or ph$si8a# (ei8e (esigne( to generate a se/uen8e of num*ers or s$m*o#s that 8an not *e reasona*#$ pre(i8te( *etter than *$ a ran(om ran(om 8han8e. 8han8e. 9arious app#i8ations of ran(omness hae ran(omness hae #e( to the (ee#opment of seera# (ifferent metho(s for generating ran(om (ata: of whi8h some hae e;iste( sin8e an8ient times: in8#u(ing (i8e (i8e:: 8oin f#ipping: f#ipping: the shuff#ing shuff#ing of of p#a$ing p#a$ing 8ar(s an( 8ar(s an( man$ other te8hni/ues. 4e8ause of the me8hani8a# nature of these te8hni/ues: generating #arge num*ers of suffi8ient#$ ran(om num*ers !important in statisti8s% re/uire( a #ot of wor< an(=or time. hus: resu#ts wou#( sometimes *e 8o##e8te( an( (istri*ute( as ran(om num*er ta*#es. ta*#es. )owa(a$s: after the a(ent of 8omputationa# ran(om>num*er generators: a growing num*er of goernment>run #otteries #otteries an( an( #otter$ games hae starte( using R)+s instea( of more tra(itiona# (rawing>metho(s. R)+s are a#so use( to (etermine the o( (s of mo(ern s#ot ma8hines ,ode(
?in8#u(e@*its=st(8.hB using namespa8e st( int main!% sran( !time!)E,,%% int n:i:; int CntFG memset!Cnt:0:siHeof!Cnt%% 8out@@Inter the num*er of times $ou want to Ro## the (i8eJnI 8inBBn for!iL0i@ni% ;Lran(!%6 ; CntF;G N for!int iL1i@i% 8out@@ICount of I@@i@@I LI@@CntFiG@@en(# return 0 N
3
2utut(
Grah(
4
0. 4rite a rogram to imlement :inear ,ongruential Generator. Theory(
A #inear 8ongruentia# generator !,C+% is an a#gorithm that $ie#(s a se/uen8e of pseu(o> ran(omiHe( num*ers 8a#8u#ate( with a (is8ontinuous pie8ewise #inear e/uation. he metho( represents one of the o#(est an( *est>*it trun8ation. he generator is (efine( *$ the re8urren8e re#ationJ where
is the se/uen8e of pseu(oran(om a#ues: an( O the Imo(u#usI O the Imu#tip#ierI O the Iin8rementI
O the Isee(I or Istart a#ueI are integer 8onstants that spe8if$ the generator. iL!aQi>1 8%mo( m ,ode(
?in8#u(e@*its=st(8.hB using namespa8e st( int main!% int a:8:m:H1:H2 8out@@Inter the a#ue of a:8:m an( 0nI 8inBBaBB8BBmBBH1 8out@@Inter the num*er of Ran(om 9aria*#esnI int n:i 8inBBn for!iL0i@ni% H2L!aQH18%m 8out@@I9a#ue of I@@i1@@I is I@@H2@@en(# H1LH2 N return 0 N
5
2utut(
6
). 4rite a rogram to imlement ,hi-*;uare test
Theory( A 8hi>s/uare( test: a#so referre( to as test !or 8hi>s/uare test%: is an$ statisti8a# h$pothesis test in whi8h the samp#ing (istri*ution of the test statisti8 is a 8hi>s/uare (istri*ution when the nu## h$pothesis is true. Chi>s/uare( tests are often 8onstru8te( from a sum of s/uare( errors: or through the samp#e arian8e. A 8hi>s/uare( test 8an then *e use( to reje8t the nu## h $pothesis that the (ata are in(epen(ent. he 8hi>s/uare( test is use( to (etermine whether there is a signifi8ant (ifferen8e *etween the e;pe8te( fre/uen8ies an( the o*sere( fre/uen8ies in one or more 8ategories.
,ode(
?in8#u(e@*its=st(8.hB using namespa8e st( int main!% int i:n 8out@@Inter the #oop 8ountnI 8inBBn f#oat oF10G: tL0 f#oat 8hiL0:e 8out@@Innter the o*sere( fre/uen8iesnI for!iL0i@ni% 8inBBoFiG tLtoFiG N for!iL0i@ni% tLtoFiG N eLt=n 8out@@en(#@@t@@en(#@@e@@en(# for!iL0i@ni% 8hiL8hi!pow!!oFiG>e%:2%=e% N 8out@@I9a#ue of Chi is I@@8hi return 0 N
7
2utut (
8
#. 4rite a rogram to imlement +onte ,arlo *imulation. Theory( Monte Car#o metho(s !or Monte Car#o e;periments% are a *roa( 8#ass of 8omputationa# a#gorithms that re#$ on repeate( ran(om samp#ing to o*tain numeri8a# resu#ts. he$ are often use( in ph$si8a# an( mathemati8a# pro*#ems an( are most usefu# when it is (iffi8u#t or impossi*#e to use other mathemati8a# metho(s. Monte Car#o metho(s are main#$ use( in three (istin8t pro*#em 8#assesJ optimiHation: numeri8a# integration: an( generating (raws from a pro*a*i#it$ (istri*ution. Monte Car#o metho(s ar$: *ut ten( to fo##ow a parti8u#ar patternJ 1. Define a (omain of possi*#e inputs. 2. +enerate inputs ran(om#$ from a pro*a*i#it$ (istri*ution oer the (omain. 3. Perform a (eterministi8 8omputation on the inputs. . Aggregate the resu#ts. "or e;amp#e: 8onsi(er a 8ir8#e ins8ri*e( in a unit s/uare. +ien that the 8ir8#e an( the s/uare hae a ratio of areas that is =: the a#ue of 8an *e appro;imate( using a Monte Car#o metho(J 1. Draw a s/uare on the groun(: then ins8ri*e a 8ir8#e within it. 2. Eniform#$ s8atter some o*je8ts of uniform siHe !grains of ri8e or san(% oer the s/uare. 3. Count the num*er of o*je8ts insi(e the 8ir8#e an( the tota# num*er of o*je8ts. . he ratio of the two 8ounts is an estimate of the ratio of the two areas: whi8h is =. Mu#tip#$ the resu#t *$ to estimate . 'n this pro8e(ure the (omain of inputs is the s/uare that 8ir8ums8ri*es our 8ir8#e. &e generate ran(om inputs *$ s8attering grains oer the s/uare then perform a 8omputation on ea8h input !test whether it fa##s within the 8ir8#e%. "ina##$: we aggregate the resu#ts to o*tain our fina# resu#t: the appro;imation of . ,ode(
?in8#u(e@*its=st(8.hB using namespa8e st( int main!% f#oat r:n:):;:$: 8out@@Inter the no of triesI@@en(# 8inBB) nL0 8out@@Inter Ra(ius of 8ir8#eI@@en(# 8inBBr 8out@@en(# sran(!time!)E,,%% for!L0@)% ;Lran(!%100 ;L;=100 $Lran(!%100 $L$=100 ;L;Qr $L$Qr 9
if!!!;Q;%!$Q$%%@L!rQr%% n
N f#oat pi piLQn=) 8out@@Ihe a#ue of P' 8a#8u#ate( *$ Monte Car#o Simu#ation is I@@pi return 0 N
2utut(
10
<. 4rite a rogram to imlement :AG +odel. Theory J Man$ e8onometri8 mo(e#s are dynamic: using #agge( aria*#es to in8orporate fee(*a8< oer time. 4$ 8ontrast: static time series mo(e#s represent s$stems that respon( e;8#usie#$ to 8urrent eents. ,agge( aria*#es 8ome in seera# t$pesJ Distri*ute( ,ag !D,% aria*#es are #agge( a#ues of o*sere( e;ogenous pre(i8tor aria*#es . Autoregressie !AR% aria*#es are #agge( a#ues of o*sere( en(ogenous response aria*#es . Moing Aerage !MA% aria*#es are #agge( a#ues of uno*sere( sto8hasti8 innoations pro8esses . D$nami8 mo(e#s are often 8onstru8te( using #inear 8o m*inations of (ifferent t$pes of #agge( aria*#es: to 8reate ARMA: ARD,: an( other h$*ri(s. he mo(e#ing goa#: in ea8h 8ase: is to ref#e8t important intera8tions among re#eant e8onomi8 fa8tors: a88urate#$ an( 8on8ise#$. Some mo(e#s: su8h as seasona# mo(e#s: use #ags at (istin8t perio(s in the (ata. A (istri*ute(>#ag mo(e# is a ($nami8 mo(e# in whi8h the effe8t of a regressor ; on $ o88urs oer time rather than a## at on8e. ,ode(
?in8#u(e@*its=st(8.hB using namespa8e st( int main!% (ou*#e ':r:;:C int i:n 8out@@I"or Tow man$ $ears (o $ou want to 8a#8u#ateI@@en(# 8inBBn int $earsFnG 8out@@Inter the $earsJI@@en(# for!iL0i@ni% 8inBB$earsFiG int +FnG 8out@@Inter the a#ues of +I@@en(# for!iL0i@ni% 8inBB+FiG 8out@@Inter the a#ue of rI@@en(# 8inBBr for!iL0i@ni% 8out@@I"or $ear I@@$earsFiG@@en(# 11
'L2!0.1%Qr rL-.-!2.2%Q!'+FiG% ;L!0.2%Qr CL20!0.%Q!r>;% 8out@@I9a#ue of 'J I@@'@@I 9a#ue of CJ I@@C@@en(#@@en(# N
N 2utut(
12
1. 4rite a rogram to Imlement ,2B4/B +odel. Theory( he 8o*we* mo(e# or 8o*we* theor$ is an e8onomi8 mo(e# that e;p#ains wh$ pri8es might *e su*je8t to perio(i8 f#u8tuations in 8ertain t$pes of mar*QP S L 8(QP 8out@@IPI@@ItI@@IDI@@ItI@@ISI@@en(# 8out@@P@@ItI@@D@@ItI@@S@@en(# for!int iL0i@10i% D L S P L !a>D%=* S L 8(QP 8out@@P@@ItI@@D@@ItI@@S@@en(# N return 0 N
2utut(
13
Grahs(
14
=. 4rite a rogram to Imlement *imulation of *ingle *er5er >ueueing +odel Theory( his e;amp#e shows how to mo(e# a sing#e>/ueue sing#e>serer s$stem with a sing#e traffi8 sour8e an( an infinite storage 8apa8it$. 'n the notation: the M stan(s for Mar
•
• • • •
seri8e time (istri*ution: an( one serer. 5ueuing theor$ proi(es e;a8t theoreti8a# resu#ts for some performan8e measures of an M=M=1 /ueuing s$stem an( this mo(e# maarria# time% Seri8e times are a88or(ing to a (istri*ution Arria# rate must *e #ess than seri8e rate !sta*#e s$stem% therwise waiting #ine wi## grow un*oun(e( !unsta*#e s$stem% *tructure( he mo(e# in8#u(es the 8omponents #iste( *e#owJ Time Based /ntity Generator block( 't mo(e#s a Poisson arria# pro8ess *$ generating entities !a#so ueue block( 't stores entities that hae $et to *e sere(. *ingle *er5er block( 't mo(e#s a serer whose seri8e time has an e;ponentia# (istri*ution.
,ode( import jaa.uti#.Q import jaa.te;t.Q import jaa.uti#.Q 8#ass Ran(oo# stati8 fina# #ong m L 21736, stati8 fina# #ong a L 721, stati8 fina# #ong / L 77, stati8 fina# #ong r L 33, stati8 #ong rVsee( L 123-67, stati8 Ran(om ran( L new Ran(om !rVsee(% 16
pu*#i8 stati8 oi( setSee( !#ong see(% rVsee( L see( ran( L new Ran(om !rVsee(% N pu*#i8 stati8 (ou*#e uniform !% #ong hi L rVsee( = / #ong #o L rVsee( > / Q hi #ong t L a Q #o > r Q hi if !t B 0% rVsee( L t e#se rVsee( L t m return ! !(ou*#e% rVsee( = !(ou*#e% m % N pu*#i8 stati8 (ou*#e uniform !(ou*#e a: (ou*#e *% if !* B a% return ! a !*>a% Q uniform!% % e#se S$stem.out.print#n !IRRR in uniform!(ou*#e:(ou*#e%JaLIaI:*LI*% return 0 N N pu*#i8 stati8 #ong uniform !#ong a: #ong *% if !* B a% (ou*#e ; L uniform !% #ong 8 L ! a !#ong% Math.f#oor!!*>a1%Q;% % return 8 N e#se if !a LL *% return a e#se S$stem.out.print#n !IRRRJ in uniform!#ong:#ong%JaLIaI:*LI*% return 0 N N pu*#i8 stati8 int uniform !int a: int *% return !int% uniform !!#ong% a: !#ong% *% N pu*#i8 stati8 (ou*#e e;ponentia# !(ou*#e #am*(a% return !1.0 = #am*(a% Q !>Math.#og!1.0 > uniform!%%% N pu*#i8 stati8 (ou*#e gaussian !% return ran(.ne;t+aussian !% N pu*#i8 stati8 (ou*#e gaussian !(ou*#e mean: (ou*#e st(Deiation% (ou*#e ; L gaussian !% 17
return mean ; Q st(Deiation N N pu*#i8 8#ass 5ueue (ou*#e arria#Rate L 0.- (ou*#e seri8eRate L 1.0 ,in
oi( han(#eDeparture !ent e% numDepartures Customer 8 L /ueue.remoe"irst !% (ou*#e time'nS$stem L 8#o8< > 8.arria#ime tota#S$stemime L time'nS$stem if !/ueue.siHe!% B 0% Customer waitingCust L /ueue.get !0% (ou*#e waitime L 8#o8< > waitingCust.arria#ime tota#&aitime L waitime s8he(u#eDeparture !% N N oi( s8he(u#eArria# !% (ou*#e ne;tArria#ime L8#o8< ran(om'nterarria#ime!% eent,ist.a(( !new ent !ne;tArria#ime: ent.ARR'9A,%% N oi( s8he(u#eDeparture !% (ou*#e ne;tDepartureime L 8#o8< ran(omSeri8eime !% eent,ist.a(( !new ent !ne;tDepartureime: ent.DPARER%% N (ou*#e ran(om'nterarria#ime !% return e;ponentia# !arria#Rate% N (ou*#e ran(omSeri8eime !% return e;ponentia# !seri8eRate% N (ou*#e e;ponentia# !(ou*#e gamma% return !1.0 = gamma% Q !>Math.#og!1.0 > Ran(oo#.uniform!%%% N oi( stats !% if !numDepartures LL 0% return N ag&aitime L tota#&aitime = numDepartures agS$stemime L tota#S$stemime = numDepartures N pu*#i8 String toString !% String resu#ts L ISimu#ation resu#tsJI resu#ts L In numArria#sJ I numArria#s resu#ts L In numDeparturesJ I numDepartures resu#ts L In ag &aitJ I ag&aitime resu#ts L In ag S$stem imeJ I agS$stemime return resu#ts N pu*#i8 stati8 oi( main !StringFG arg% 5ueue /ueue L new 5ueue !% 19
/ueue.simu#ate !1000% S$stem.out.print#n !/ueue% N N
== C#ass Customer !one instan8e per 8ustomer% stores whateer we == nee( for ea8h 8ustomer. Sin8e we 8o##e8t statisti8s on waiting == time at the time of (eparture: we nee( to re8or( when a == 8ustomer arries. 8#ass Customer (ou*#e arria#ime pu*#i8 Customer !(ou*#e arria#ime% this.arria#ime L arria#ime N N == C#ass ent has eer$thing we nee( for an eentJ the t$pe of == eent: an( when it o88urs. o use XaaUs Priorit$5ueue: we nee( == hae this 8#ass imp#ement the Compara*#e interfa8e where == one eent is I#essI if it o88urs sooner. 8#ass ent imp#ements Compara*#e pu*#i8 stati8 int ARR'9A, L 1 pu*#i8 stati8 int DPARER L 2 int t$pe L >1 == Arria# or (eparture. (ou*#e eentime == &hen it o88urs. pu*#i8 ent !(ou*#e eentime: int t$pe% this.eentime L eentime this.t$pe L t$pe N pu*#i8 int 8ompareo !*je8t o*j% ent e L !ent% o*j if !eentime @ e.eentime% return >1 N e#se if !eentime B e.eentime% return 1 N e#se return 0 N N pu*#i8 *oo#ean e/ua#s !*je8t o*j% return !8ompareo!o*j% LL 0% N 20
N
2ututJ
21
. 4rite a rogram to imlement *imulation of 0 *er5er >ueueing *ystem Theory(
Tere are some e;amp#es of wa$s to 8om*ine "'" 5ueue an( Sing#e Serer *#o8Serer Pairs Para##e# 5ueue>Serer Pairs as A#ternaties Para##e# 5ueue>Serer Pairs in Mu#ti8asting Seria# Conne8tion of 5ueues Para##e# Conne8tion of 5ueues Serial Queue-Server Pairs
wo /ueue>serer pairs 8onne8te( in series represent su88essie operations that an entit$ un(ergoes. "or e;amp#e: parts on an assem*#$ #ine are pro8esse( se/uentia##$ *$ two ma8hines.
&hi#e $ou might a#ternatie#$ mo(e# the situation as a pair of serers without a /ueue *etween them: the a*sen8e of the /ueue means that if the first serer 8omp#etes seri8e on an entit$ *efore the se8on( serer is aai#a*#e: the entit$ must sta$ in the first serer past the en( of seri8e an( the first serer 8annot a88ept a new entit$ for seri8e unti# the se8on( serer *e8omes aai#a*#e.
Parallel Queue-Server Pairs as Alternatives
22
wo /ueue>serer pairs 8onne8te( in para##e#: in whi8h ea8h entit$ arries at one or the other: represent a#ternatie operations. "or e;amp#e: ehi8#es wait in #ine for one of seera# to##*ooths at a to## p#aHa.
Parallel Queue-Server Pairs in Multicasting
wo /ueue>serer pairs 8onne8te( in para##e#: in whi8h a 8op$ of ea8h entit$ arries at *oth: represent a mu#ti8asting situation su8h as sen(ing a message to mu#tip#e re8ipients. )ote that 8op$ing entities might not ma
Serial Connection of Queues
wo /ueues 8onne8te( in series might *e usefu# if $ou are using entities to mo(e# items that ph$si8a##$ e;perien8e two (istin8t sets of 8on(itions whi#e in storage. "or e;amp#e: a((itiona# inentor$ items that oerf#ow one storage area hae to sta$ in another storage area in whi8h a #ess we##>regu#ate( temperature affe8ts the itemsU #ong>term /ua#it$. Mo(e#ing the two storage
23
areas as (istin8t /ueue *#o8
Parallel Connection of Queues
wo /ueues 8onne8te( in para##e#: in whi8h ea8h entit$ arries at one or the other: represent a#ternatie paths for waiting. he paths might #ea( to (ifferent operations: su8h as a #ine of ehi8#es waiting for a to##*ooth mo(e#e( an( a #ine of ehi8#es waiting on a jamme( e;it ramp of the freewa$. ou might mo(e# the to##*ooth as a serer an( the traffi8 jam as a gate.
AssumptionsJ non homogeneous V!t% Poisson arria#sSeri8e at serer 1: then *$ serer 2 seri8e for ea8h 8ustomer seri8e times are R9s with (istri*ution +1 an( +2no 8ustomers after Vna# arria# time . ,ode(
import jaa.uti#.Q pu*#i8 8#ass Simu#ation
24
== statisti8sJ stati8 (ou*#e meanArria# L 1 stati8 (ou*#e meanSeri8e L 0.- stati8 int ma;)um*erfas
pu*#i8 stati8 oi( main !String argsFG% if !args.#ength LL 0% S$stem.err.print#n!IParametersJ nI I 1. Mean inter>arria# time nI I 2. Mean seri8e time nI I 3. )um*er of tas
N 8at8h !)um*er"ormat;8eption e% S$stem.err.print#n!ISe8on( argument must *e a f#oating point a#ueI% S$stem.e;it!1% N N if !args.#ength B 2% tr$ ma;)um*erfas
26
== printing resu#tsJ S$stem.out.print#n!ISerer 1JI% S$stem.out.print#n!IEti#iHation I serer1.getEti#iHation!%% S$stem.out.print#n!I&aiting ime I serer1.getAg&aitingime!%% S$stem.out.print#n!I5ueue ,ength I serer1.getAg5ueue,ength!%% S$stem.out.print#n!ISerer 2JI% S$stem.out.print#n!IEti#iHation I serer2.getEti#iHation!%% S$stem.out.print#n!I&aiting ime I serer2.getAg&aitingime!%% S$stem.out.print#n!I5ueue ,ength I serer2.getAg5ueue,ength!%% =Q == *rief outputJ S$stem.out.print#n!ISerer1J I serer1.getEti#iHation!% I I serer1.getAg&aitingime!% I I serer1.getAg5ueue,ength!% % S$stem.out.print#n!ISerer2J I serer2.getEti#iHation!% I I serer2.getAg&aitingime!% I I serer2.getAg5ueue,ength!% % Q= N
stati8 pu*#i8 (ou*#e get)e;tArria#!% return simime ran(om;ponentia#!rng1: 1=meanArria#% N stati8 pu*#i8 (ou*#e get)e;tDeparture!% return simime ran(om;ponentia#!rng2: 1=meanSeri8e% N stati8 (ou*#e ran(om;ponentia#!Ran(om rng: (ou*#e #am*(a% return Math.#og! rng.ne;tDou*#e!% % = >#am*(a N stati8 pu*#i8 oi( setSimime!(ou*#e t% simime L t N stati8 pu*#i8 (ou*#e getSimime!% return simime N N
27
a*stra8t 8#ass ent imp#ements Compara*#e@entB prote8te( stati8 int 8ounterL0 prote8te( (ou*#e s8he(u#e(ime prote8te( int num*er == eent num*er !use( for tie *rea1 e#se if !this.s8he(u#e(ime B e.s8he(u#e(ime% return 1 e#se if !this.num*er @ e.num*er% return >1 e#se return 1 N pu*#i8 String toString!% return Ient )o. I num*er I: s8he(u#e( time L I s8he(u#e(ime N N 8#ass Arria#ent e;ten(s ent pu*#i8 Arria#ent!(ou*#e s8he(u#e(ime% super!s8he(u#e(ime% N pu*#i8 String toString!% return IArria# ent: )o. I num*er I: s8he(u#e( time L I s8he(u#e(ime N N 8#ass Departureent e;ten(s ent pu*#i8 Serer serer pu*#i8 Departureent!(ou*#e s8he(u#e(ime: Serer serer% super!s8he(u#e(ime% this.serer L serer 28
N pu*#i8 String toString!% return IDeparture ent: )o. I num*er I: s8he(u#e( time L I s8he(u#e(ime N N
8#ass Dispat8her Priorit$5ueue@entB eentSet == pointer to eent set Serer serer1: serer2 == pointers to serers Serer 8urrentSerer pu*#i8 Dispat8her!Priorit$5ueue@entB eentSet: Serer s1: Serer s2% serer1 L s1 serer2 L s2 8urrentSerer L s1 this.eentSet L eentSet N pu*#i8 oi( han(#eent!ent e% Simu#ation.setSimime!e.getS8he(u#e(ime!%% S$stem.out.print#n!ITan(#ing eentJ I e% if !e instan8eof Arria#ent% han(#eArria#! !Arria#ent%e % e#se han(#eDeparture! !Departureent%e % N pu*#i8 oi( han(#eArria#!Arria#ent e% == as< assignment ru#eJ == 'f *oth serers are i(#e: then tas
== as< assignmentJ 8urrentSerer.assignas
== 'f there is on#$ one tas<: then the (eparture 8an *e s8he(u#e( if !8urrentSerer.get5ueue,ength!% LL 1% eentSet.a((! new Departureent! Simu#ation.get)e;tDeparture!%: 8urrentSerer % % == generate the ne;t eent of the same t$pe eentSet.a((! new Arria#ent! Simu#ation.get)e;tArria#!% % % Simu#ation.tas
8#ass as< pu*#i8 (ou*#e arria#ime pu*#i8 (ou*#e a8tiationime pu*#i8 as
8#ass Serer ,in
as< t L /ueue.po##!% == 8a#8u#ate statisti8sJ tas t.a8tiationime waitimeota# L t.a8tiationime > t.arria#ime 8umu#ate(5ueue,ength L !t.a8tiationime > t.arria#ime% Q !/ueue.siHe!%1% S$stem.out.print#n!Ia8tiationJ I t.a8tiationime I arria#J I t.arria#ime% == if the /ueue is not empt$: then the ne;t tas< is a8tiate( if !Y/ueue.ismpt$!%% !!as<%/ueue.pee
2utut(
31
32
. 4rite a rogram to imlement *imulation of In5entory *ystem. Theory( Some amount M of goo(s is in sto8< to *egin with. As sa#es 8ontinue: the sto8< (e8reases. At the pre>(efine( intera#: N : the sto8< #ee# is 8he8 or(ere(. 't is possi*#e that the inentor$ *e8omes negatie: me aning the goo(s is in shortage. • he lead time: whi8h is the time *etween the issuan8e of re>or(er an( the arria# of the •
goo(s: 8an *e 8onsi(ere( as Hero in man$ 8ases. ;amp#eJ he newspaper se##erUs pro*#em he paper se##er *u$s the papers for 33 8ents ea8h an( se##s for -0 8ents ea8h. • he papers not so#( at the en( of the (a$ are so#( as s8rap for - 8ents ea8h. • )ewspapers 8an *e pur8hase( in *un(#es of 10. hus the paper se##er 8an *u$ 0: -0: 60: • an( so on. Profit is 8a#8u#ate( as • profit L reenue > 8ost > #ost profit from e;8ess (eman( sa#age . ;amp#e of Simu#ation of an !M:)% inentor$ s$stem. M is the ma;imum inentor$ #ee#: assume it is 11 units. • ) is the #ength of reiew perio(: assume it is - (a$s. • he initia# inentor$ is 3 units: an( an initia# or(er of 7 units is s8he(u#e( to arrie in 2 • (a$s. his is the initia# setting of the simu#ation. ,ode( ?in8#u(e@*its=st(8.hB using namespa8e st( int main!% f#oat D: [: : S<: S<: ED: DDt: Ct int i Ct L 0 S< L 12- iL1 ED L 0.0 DDt L 0 printf!Inn nter the a#ues of ; an( $tI% s8anf!If fI: Z[: Z% whi#e!i@L200%
if!DDt LL i% S< L S< ED L0 N D L ran(!% if !D @LS<% S< L S< > D Ct L Ct S< Q 0.- N e#se Ct L Ct !D > S<% Q 20 S< L 0 N S< L S< ED 33
if !S<@L[% ED L DDt L i 3 Ct L Ct = 70 N i L i 1 N printf!Inn [ L fn Lfn LfnnI: [: : Ct% N
2utut(
34
"$. *imulation of T/:/C2NI, **T/+ Theory(
Simp#e simu#ation of a te#ephon$ s$stem in whi8h 8ustomers ma up *oth #o8a# an( #ong (istan8e 8a##s. he app#i8ation ar8hite8ture is in three #a$ers. he *asi8 o*je8ts proi(e *asi8 fun8tiona#it$ to simu#ate 8ustomers: 8a##s an( 8onne8tions !regu#ar 8a##s hae one 8onne8tion: 8onferen8e 8a##s hae more than one%. he timing feature is 8on8erne( with timing the 8onne8tions an(
35
C#ass Customer Customer has metho(s 8a##!%: pi8
36
pu*#i8 Ca## 8a##!Customer re8eier% Ca## 8a## L new Ca##!this: re8eier% a((Ca##!8a##% return 8a## N =QQ Q pi8< up a 8a## Q= pu*#i8 oi( pi8> 8onferen8e them Q PRJ 8a##1.in8#u(es!this% Q 8a##2.in8#u(es!this% Q 8a##1.8onne8te(!% Q 8a##2.8onne8te(!% Q PSJ 8a##1 in8#u(es a## 8ustomers 8onne8te( *$ 8a##1\pre an( 8a##2\pre Q= pu*#i8 oi( merge!Ca## 8a##1: Ca## 8a##2% 8a##1.merge!8a##2% remoeCa##!8a##2% N N C#ass Ca## Ca##s are 8reate( with a 8a##er an( re8eier who are 8ustomers. 'f the 8a##er an( re8eier hae the same area then the 8a## 8an *e esta*#ishe( with a ,o8a# 8onne8tion !see *e#ow%: otherwise a ,ongDistan8e 8onne8tion is re/uire(. A 8a## 8omprises a num*er of 8onne8tions *etween 8ustomers. 'nitia##$ there is on#$ the 8onne8tion *etween the 8a##er an( re8eier *ut a((itiona# 8onne8tions 8an *e a((e( if 8a##s are merge( to form 8onferen8e 8a##s. pu*#i8 8#ass Ca## priate Customer 8a##er: re8eier priate 9e8tor 8onne8tions L new 9e8tor!% pu*#i8 Ca##!Customer 8a##er: Customer re8eier% this.8a##er L 8a##er 37
this.re8eier L re8eier Conne8tion 8 if !re8eier.#o8a#o!8a##er%% 8 L new ,o8a#!8a##er: re8eier% N e#se 8 L new ,ongDistan8e!8a##er: re8eier% N 8onne8tions.a((!8% N pu*#i8 oi( pi8
a*stra8t 8#ass Conne8tion pu*#i8 stati8 fina# int P)D')+ L 0 pu*#i8 stati8 fina# int CMP, L 1 pu*#i8 stati8 fina# int DRPPD L 2 Customer 8a##er: re8eier priateint state L P)D')+ =QQ Q Creatte a new Conne8tion *etween a an( * Q= Conne8tion!Customer a: Customer *% this.8a##er L a this.re8eier L * N =QQ Q what is the state of the 8onne8tionW Q= pu*#i8intgetState!% return state N =QQ Q get the 8ustomer who initiate( this 8onne8tion Q= pu*#i8 oi( merge!Ca## other% for!numeration e L other.8onne8tions.e#ements!% e.hasMore#ements!%% Conne8tion 8onn L !Conne8tion%e.ne;t#ement!% other.8onne8tions.remoe!8onn% 8onne8tions.a((#ement!8onn% N N N C#ass Conne8tion Conne8tion mo(e#s the ph$si8a# (etai#s of esta*#ishing a 8onne 8tion *etween 8ustomers. 't (oes this with a simp#e state ma8hine !8onne8tions are initia##$ P)D')+: then CMP,D an( fina##$ DRPPD%. Messages are printe( to the 8onso#e so that the state of 8onne8tions 8an *e o*sere(. Conne8tion is an a*stra8t 8#ass with two 8on8 rete su*8#assesJ ,o8a# an( ,ongDistan8e. a*stra8t 8#ass Conne8tion pu*#i8 stati8 fina# int P)D')+ L 0 pu*#i8 stati8 fina# int CMP, L 1 pu*#i8 stati8 fina# int DRPPD L 2
39
Customer 8a##er: re8eier priateint state L P)D')+ =QQ Q Creatte a new Conne8tion *etween a an( * Q= Conne8tion!Customer a: Customer *% this.8a##er L a this.re8eier L * N =QQ Q what is the state of the 8onne8tionW Q= pu*#i8intgetState!% return state N =QQ Q get the 8ustomer who initiate( this 8onne8tion Q= pu*#i8 Customer getCa##er!% return 8a##er N pu*#i8 Customer getRe8eier!% return re8eier N oi( 8omp#ete!% state L CMP, S$stem.out.print#n!I8onne8tion 8omp#ete(I% N =QQ Q Ca##e( when the 8onne8tion is (roppe( from a 8a##. 's inten(e( to Q free up an$ resour8es the 8onne8tion was 8onsuming. Q= oi( (rop!% state L DRPPD S$stem.out.print#n!I8onne8tion (roppe(I% N =QQ Q 's 8ustomer 8 8onne8te( *$ this 8onne8tionW Q= pu*#i8*oo#ean 8onne8ts!Customer 8% return !8a##er LL 8 ]] re8eier LL 8% N N C#ass ,o8a# !in Conne8tion.jaa% 8#ass ,o8a# e;ten(s Conne8tion ,o8a#!Customer a: Customer *% 40