Mixed Model Simulation Tutorial 2008

Embed Size (px)

Citation preview

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    1/23

    HowtoBuildaCombined

    AgentBased/SystemDynamicsModel

    LogicinAny

    2008XJTechnologieswww.anylogic.com

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    2/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    Contents

    Introduction................................................................................................................................................... 3

    Theproblemdefinition.................................................................................................................................. 4

    Theplan......................................................................................................................................................... 5

    Modeldevelopment

    ......................................................................................................................................

    6

    Phase1.Create1000agentswithemptybehavior.................................................................................. 6

    Phase2.Addsimplestatebasedbehaviortotheagents......................................................................... 9

    Phase3.AddtheretailerstockwithsomeinitialamountofproductA................................................. 11

    Phase4.AddSDsupplychainfortheproductA..................................................................................... 12

    Phase5.Visualizethedynamicsofdemandanduserbase.................................................................... 14

    Phase6.Addagentcommunicationtomodelwordofmouth............................................................... 16

    Phase7.

    Add

    product

    discards

    ................................................................................................................

    18

    Phase8.AddproductBtothemodel..................................................................................................... 19

    Phase9.Addproductswitchingtriggeredbywaitingtime.................................................................... 20

    Experimentingwiththemodel.................................................................................................................... 23

    2008XJTechnologieswww.anylogic.com 2

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    3/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    Introduction

    AnyLogicallowsyoutobuildasimulationmodelusingmultiplemethods:SystemDynamics,AgentBased

    andDiscreteEvent(Processcentric)modeling.Moreover,youcancombinedifferentmethodsinone

    model:putagentsintoanenvironmentwhosedynamicsisdefinedinSDstyle,useprocessdiagramsor

    SDto

    define

    internals

    of

    agents,

    etc,

    etc.

    Any

    kind

    of

    mixed

    architecture

    is

    possible

    due

    to

    flexible

    object

    orientedAnyLogicmodelinglanguage.

    Thechoiceofmodelarchitecture(howtopartitionthemodelintocomponents,whattoaggregate,

    whichgranularitytouse,whatbehaviorisbestmappedtoaprocessdiagram,andwhattoastatechart,

    etc)belongsmainlytothedomainofartandintuitionofthemodelerandisoutsidethescopeofthis

    shorttutorial.ThegoalofthistutorialistoshowstepbystephowtobuildacombinedAB+SDmodelin

    AnyLogicusingoneparticulararchitecture.Wewillhighlightthepointsofinteractionofagentsand

    systemdynamicsandtrytoshowthatmodelelementsbelongingtodifferentapproachesliveasingle

    spaceofAnyLogicmodelandcaneasilyaccesseachother.Havingcompletedthistutorialyoushouldbe

    abletobuildvariousmultimethodmodelswithmoreconfidenceandefficiency.

    ThetutorialdetailslevelassumessomefamiliaritywithAnyLogicmodeldevelopmentenvironment:the

    instructionsinmostcasesareofthetype:CreateastatePotentialUserratherthanOpentheModel

    palette,clickontheStateitem,thenclickonthecanvasandenterPotentialUserintheeditbox.

    2008XJTechnologieswww.anylogic.com 3

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    4/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    Theproblemdefinition

    Wewillbuildamodelofaconsumermarketandasupplychain.

    Theassumptionswemakeaboutthemarketaresimilartoonesoftheclassicalmodelsof

    product/innovationdiffusion,e.g.ofBassmodelwithdiscardsandreplacements.Wewillhowever

    considertwocompetingproductsinsteadofone.

    TherearetwoalternativeproductsAandBmanufacturedbydifferent(andcompeting)companies.Theproductsareequivalent,i.e.canreplaceeachother.Theproductpricesare

    equalandthereforedonotmatter.

    Consumers(thereareTotalPopulation=1000ofthem)initiallyarenotusinganyproductsbutallarepotentiallyinterested(arepotentialusers).

    Consumersaresensitivetoadvertizingandtowordofmouth. Advertizinggeneratesthedemandforaproductamongthepotentialusers.Advertizing

    Effectiveness=0.011isthepercentofpotentialusersthatbecomereadytobuyaparticular

    product(A

    or

    B)

    during

    aday.

    Both

    companies

    do

    advertizing.

    Consumerscontacteachother.AconsumercontactsonaverageaContactRate=5otherpeopleperday.

    Duringthosecontactstheusersofproductsmayinfluencepotentialusers.Ifauserofe.g.Acontactsapotentialuser,thelatterwillwanttobuyAwithprobabilityAdoptionFraction=

    0.015,sameforB.

    AnyproductdiscardsinDiscardTime=uniform(17,23)daysandgeneratestheimmediateneedtobuyareplacementofthesamebrand.

    Ifapersonwantstobuye.g.A,butAisnotavailableforMaximumWaitingTime=2days,hebecomesreadytobuyanythingthatisavailable(AorB),sameforB.

    Eachcompany(AandB)hasitsownsupplychainthatdeliversproductstotheendconsumers.The

    supplychainsareverysimpleandworkasfollows:

    Theproductcanbepurchasedbyaconsumeronlyfromtheretailerstock,initiallyholdingacertainamount(InitialRetailerStock=100)ofproduct.

    Theproductismanufacturedbyaproducer.AproducermakesProductionRateproductsperday,andthisratemayvary,e.g.itcanbeadjustedaccordingtothedemand(whichisknownto

    theproducer)

    ThefinishedproductsaredeliveredtotheretailerwithinDeliveryTime=2days.The

    output

    of

    the

    model

    should

    include

    the

    market

    shares

    for

    A

    and

    B,

    the

    demand

    (i.e.

    the

    number

    of

    peoplewhowanttobuywhiletheproduct(s)arenotavailable)andtheinventorylevelsinthesupply

    chains.

    2008XJTechnologieswww.anylogic.com 4

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    5/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    Theplan

    WewillmodeltheconsumermarketinAgentBasedway:eachconsumerwillbeanagent.Thesupply

    chainsforbothproductsAandBwillbedoneusingSystemDynamics.Pleasenotethattheproblem

    definitionallowsmanyotherchoicesthisisjustoneofthem.

    TheMarket

    The

    Supply

    Chain

    Asyouobviouslyknow,thebestwaytodevelopsimulationmodelsistodoititeratively,i.e.inmultiple

    phaseswitharunnablemodelattheendofeachphase.Inourcaseitmakessensetohavethefollowing

    order:

    1. Startwiththemarketmodelandjustoneproduct(A)a. Buildthebehaviormodelofanindividualconsumerb. Populatethemarketwiththeconsumersc. Assumetheproductisavailable

    2. AddsupplychainfortheproductA3. AddproductB

    2008XJTechnologieswww.anylogic.com 5

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    6/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    Modeldevelopment

    Phase1.Create1000agentswithemptybehavior

    InthisphasewewillcreateanewAnyLogicproject,defineanActiveObjectclassforourconsumers,

    drawasimple

    animation

    of

    aconsumer,

    define,

    create

    and

    visualize

    apopulation

    of

    1000

    consumers.

    Theconsumerswillhavenobehavioratthistime,sothemodelwillbedoingnothing.

    1. CreateanewprojectMixedMethodModelABMarketandSDSupplyChainAnewprojectalwayshasoneActiveObjectclassMain,whoseeditoropensautomatically.Mainwillbe

    thetoplevelofourmodelwherewewilldefinetheinteractionbetweentheagentbasedandsystem

    dynamicsparts.Wewillleaveitblankfornowandstartwithdevelopingthemodelofaconsumer.

    2. CreateanewActiveObjectclassConsumerWeneedaseparateclassforconsumers,inwhichwewilldefinetheconsumerbehavior.Havingthat

    behaviorencapsulatedinaclassenablesustocreatemultipleinstancesofconsumers.Theeditorand

    propertiesof

    the

    class

    Consumer

    open

    when

    you

    create

    it.

    3. InthepropertiesoftheclassConsumerspecifythatConsumerisanAgentThiswillenablesomeusefulAnyLogicservicesforagents,forexamplecommunication,space,layout,etc.

    TheiconoftheactiveobjectclassConsumerchanges.

    4. IntheeditorofConsumerdrawavisualrepresentationofaconsumerusinge.g.aCurveshapenearthecoordinateorigin.Youmaywanttousemaximumzoomforfinepositioningofthecurve

    points.SetlinecolorofthecurvetoNoColorandfillcolortogray

    Eachconsumerwillbevisualizedbysuchshape.Thecoloroftheshapeaswellasitsotherpropertiescan

    bedynamicallysetupbyeachindividualconsumer.

    5. DragtheclassConsumerfromtheprojecttreetotheeditorofMain2008XJTechnologieswww.anylogic.com 6

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    7/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    ThiswaywespecifythatinstanceofConsumerisembeddedinthetoplevelobjectMain.Theiconandthe

    presentationofconsumerappearintheeditorofMain.

    6. Movethepresentationoftheembeddedconsumertotheposition(50,250)onMainThiswillbethetopleftcorneroftherectangularspacewherewewilllateronvisualizetheconsumer

    population.Theothermodelelementswillbelocatedaboveit.

    7. SettheReplicationpropertyoftheembeddedconsumerto1000.Changethenameoftheembeddedobjectfromconsumertoconsumers

    ThiswaywetellAnyLogicthatinsideMaintherewillbenotonebut1000consumers.consumerswillbe

    thenameofthatpopulation.Thesquarebrackets[..]appearnearthenametoindicatethereplication.

    8. AddenvironmentobjectfromtheModelpalettetoMain.SetthepropertyEnvironmentoftheconsumer

    population

    to

    environment

    Environmenttakescareofspace,layout,network,communicationofagents.Inourcaseweneed

    environmenttolayouttheagentpresentationsandtomodelwordofmouthbyagenttoagent

    interaction.

    9. RunthemodelYoushouldbeabletoseethepicturesimilartotheonebelow.Thereisnodynamicsinthemodel,so

    nothingchangesovertime.Inthenextstepwewilladjustthewindowsizeandtheagentspaceborders.

    2008XJTechnologieswww.anylogic.com 7

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    8/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    10.IntheprojecttreeexpandtheexperimentitemSimulation:MainandthenexpanditsPresentationsubitem.ClickonFrametoviewitsproperties.Setthewidthoftheframeto1000

    andHeightto600.

    Thisisthewayyouspecifytheinitialwindowsizeofthesimulation.

    11.IntheeditorofMainclickenvironment.IntheAdvancedpageofitspropertiessetspacewidthto900andspaceheightto300.

    Nowtheagentswillbevisualizedinarectangularspacethatfitsthewindow.

    12.RunthemodelagainThelayoutshouldlookbetternow.

    2008XJTechnologieswww.anylogic.com 8

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    9/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    Phase2.Addsimplestatebasedbehaviortotheagents

    Basedontheproblemdefinition,wewilldefinethebehaviorofaconsumerasasequenceofthree

    states:PotentialUser,WantA,UsesA.Wewillassumethattheproductisalwaysavailable,sothe

    transitionfromWantAtoUsesAwillbeunconditionalandimmediate.Theeffectofadvertizingwillbe

    modeledasstochasticdelayassociatedwithatransitionfromPotentialUsertoWantA.Noproduct

    discards

    and

    no

    agent

    to

    agent

    communication

    will

    be

    added

    in

    this

    phase.

    1. IntheeditorofConsumerdrawthreestates(topdown):PotentialUser,WantAandUsesA.DrawthestatechartentrypointpointingtothestatePotentialUser.

    Thestatesinthestatechartarealternative:theagent(inourcasetheconsumer)canonlybeinonestate

    atatime.Thestatechartentrypointmarkstheinitialstate,whichobviouslyisPotentialUser.Thenameof

    thestatechartentrypointisthenameofthestatechart.Ingeneral,anActiveObjectinAnyLogicmayhave

    multiplestatecharts,butnowweneedonlyone.

    2. DrawtransitionfromPotentialUsertoWantAandcallitAdA.DrawtransitionfromWantAtoUsesAandcallitBuyA.CheckthecheckboxShownameforeachtransitionandadjustthe

    positionsoflabelsintheeditor

    Transitionsdefinehowtheobjectchangesitsstates.Atransitionmaybetriggeredbye.g.amessage

    arrival,acondition,ortime.ThetransitionAdAwillmodeltheeffectofadvertizing,andBuyAtheevent

    ofpurchasingtheproductA.

    3. SpecifythefollowingpropertiesforthetransitionAdA:Triggertype:RateandRate:0.0112008XJTechnologieswww.anylogic.com 9

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    10/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    TransitionofratetypeinAnyLogicinthiscaseisthesameastransitiontriggeredbyastochastictimeout

    distributedexponentially.WhenthestatechartentersthestatePotentialUse,adrawfromtheexponential

    distributionismadeandthetimeoutissetup.Therefore,eachconsumerwillhavedifferenttimeof

    adoptionbecauseofadvertizing,sothatonaverage1.1%ofpotentialuserswillwanttobuytheproductin

    onetimeunit(day).

    4. SpecifythefollowingpropertiesforthetransitionBuyA:Trigger:timeoutandTimeout:0.Asweassumethattheproductisalwaysavailable,anybodywhowantstheproductcanimmediatelybuyit

    thereforeoncetheconsumerstatechartentersthestateWantA,itwillproceedtostateUsesAwithzero

    delay.

    5. IntheEntryactionofthestateWantAwrite:curve.setFillColor(pink);In

    the

    Entry

    action

    of

    the

    state

    UsesA

    write:

    curve.setFillColor(

    red

    );

    Thiswaywewillchangethecoloroftheconsumerpresentationtovisualizethechangeofitsstate.Asyou

    cansee,allmodelandpresentationelementsoftheactiveobjectclassareaccessibleonefromanother.

    NotethatifyouhaveusedotherkindofshapetodrawthepresentationofConsumer,youmayneedto

    useothernamefortheshape.

    6. Runthemodel.AfterawhileyoumaypresstheVirtualtimebuttontospeedupthemodel.Youshouldbeabletoseehowthepopulationofconsumersgraduallyturnsredthisistheeffectof

    advertizing.AlthougheachconsumergoesthroughthestateWantA,inwhichheshouldbepink,youwill

    notbeabletoseeitashedoesnotstayinthatstateandimmediatelyproceedstoUsesA.Themodelwill

    stopattime100whensomeconsumersareusersandsomearenot.

    2008XJTechnologieswww.anylogic.com 10

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    11/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    7. OpenthepropertiesofexperimentSimulation:Main.OntheGeneralpagesetRandomseed(uniquesimulationruns).IntheModeltimepagesetStoptime:Never.

    Asourmodelisstochastic(youmayrememberthatthesourceofstochasticityistheratetransitionAdAin

    theconsumersstatecharts),thesimulationresultswilldependontherandomnumbergeneration.By

    settingtheRandomseedyoutellAnylogictousedifferentsequencesofrandomnumbersforeachrun.By

    settingtheStoptimetoneveryoutellAnyLogictoexecutethemodelinfinitelylong(oruntilthereis

    nothingto

    execute).

    8. RunthemodelagainYouwillseethateventuallyallconsumerswillbuytheproduct.

    Phase3.AddtheretailerstockwithsomeinitialamountofproductA

    Inthisphasewewilladdthefirst(actuallytheend)elementofthesupplychainforproductA:the

    retailerstock.The(SystemDynamics)stockwillhavesomeinitialamountofproduct,sosomeconsumers

    willbeabletobuytheproduct.WewillmodifytheconsumerbehaviorsothatthetransitionfromWantA

    toUsesAwillonlybepossibleifthethereisatleastoneunitofproductonstock.Asaresultofa

    consumertaking

    this

    transition

    the

    amount

    of

    available

    product

    will

    be

    decremented.

    This

    is

    the

    first

    pointofinteractionoftheAgentBasedandtheSystemDynamicscomponentsinthemodel.

    1. OpentheeditorofMainandaddastockvariableRetailerStock.Setitsinitialvalueto100.ThisisthefirstSystemDynamicselementinthemodel.WedefineitinMainonthesamelevelwherewe

    haveembeddedthepopulationofagentsconsumers.

    2. GototheeditorofConsumerandchangethepropertiesofthetransitionBuyA:itshouldnowhavetriggertypeCondition,theCondition:get_Main().RetailerStockA>=1andAction:

    get_Main().RetailerStockA;

    ThisishowweimplementtheABSDinteraction:wheninthestateWantAaconsumerconstantly

    monitorsthestockRetailerStockA;whenthestockcontainsatleastoneproduct,thetransitionistaken

    and,

    as

    a

    result,

    one

    unit

    of

    product

    is

    deducted

    from

    the

    stock.

    Please

    note

    that,

    as

    the

    stock

    is

    located

    onelevelupregardingtheconsumerbehavior(attheMainobjectthatcontainstheconsumer),weneed

    firstgettoMainandonlythenwecanaccessthestockthatiswhyweusetheprefixget_Main(),which

    bringsustothecontainerofConsumeroftypeMain.

    2008XJTechnologieswww.anylogic.com 11

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    12/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    3. Runthemodelinvirtualtime(fast)mode.Youwillseethat100consumerswillbeabletobuyA,whileallotherswilleventuallyturnpinkasthe

    retailerisoutofstock.

    Phase4.AddSDsupplychainfortheproductA

    WewilladdtheremainingpartofthesupplychainforA:productionanddelivery.Thesupplychainwill

    beapureSystemDynamicsconstructstartingwithProductionflowintoFactorystockandthenthrough

    theDeliveryflowtotheRetailerstock.Wewillfirstmaketheproductionrateconstantandthen

    variable,depending

    on

    the

    demand

    for

    the

    product.

    This

    will

    be

    asecond

    point

    of

    interaction

    between

    theABandtheSDpartsofthemodel.TofeedthedemandintotheSDpartwewilladdastatisticsitem

    totheconsumerpopulation:itwillcalculatethenumberofconsumersthatareinthestateWantA.The

    productionratewillberecalculatedonceadayaccordingtothatnumber.

    1. IntheeditorofMainaddthestockFactoryStockAtotheleftofRetailerStockA.DrawflowfromFactoryStockAmtoRetailerStockA(bydoubleclickingthesourcestockandclickingatthesink

    stock).CallthecreatedflowDeliveryA.SettheformulaforDeliveryA=FactoryStockA/2

    Thiswaywemodelthedeliverydelayof2daysfromthefactorytotheretailerstock.

    2. AddtheflowvariableProductionAtotheleftofFactoryStockAandletitflowintoFactoryStockA.LettheProductionA=15.

    Wewillassumeaconstantproductionratefornow.

    2008XJTechnologieswww.anylogic.com 12

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    13/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    3. RunthemodelYouwillseethatnowthedemandcanbefullysatisfied,andmoreover,theamountofproducedproductis

    muchgreaterthanneeded.Obviously,wheneverybodyhaspurchasedtheproduct,thesupplychain

    continuestogrowtheretailerstock.

    4. IntheeditorofMainclickontheiconoftheembeddedconsumersandopentheStatisticspageoftheirproperties.AddthestatisticsitemwithnameNWantA.Leavethetypeofstatisticscount.

    Intheconditionfieldwrite:item.statechart.isStateActive(item.WantA)

    Asourconsumermarketiscompletelydisaggregated,weneedtoiteratethroughallagentstofindout

    howmanyofthemwanttobuytheproduct,i.e.areinthestateWantA.Thestatisticsoftypecountdoes

    exactlythat:ititeratesthroughthepopulationandcountshowmanyagentssatisfythegivencondition.In

    theconditionexpressionitemrepresentsthecurrentagent,statechartisthenameoftheconsumers

    statechart,isStateActiveisastandardmethodofstatechart,andWantAisthenameofthestate

    definedwithintheagent,thatiswhyitneedstheprefixitem..

    5. IntheeditorofMainaddeventnearbytheflowProductionA.CallitupdateProductionA.InthepropertiesoftheeventspecifyTriggertype:timeout,Mode:Cyclic,Recurrencetime:1,Action:

    ProductionA=consumers.NWantA();

    ByaddingsucheventwebuildyetanotherlinkbetweentheABandSDmodels:nowatthebeginningof

    eachdaytheproductionratewillbemodifiedaccordingtothenumberofconsumerswhoarewillingto

    buytheproduct.Therecouldbebetterformulasthanrate=demand,butthatsthesimplestone.

    NWantA()is

    the

    statistics

    method

    we

    have

    defined

    in

    the

    consumer

    population

    in

    the

    previous

    step.

    2008XJTechnologieswww.anylogic.com 13

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    14/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    6. IntheformulaforProductionAremove15Nowtheproductionrateisfullycontrolledbytheeventupdatingit,soformulamustberemoved.

    7. Runthemodel.ClickontheProductionAtobringupitsinspectwindowandthenclickswitchittochartmodebyclickingthesmallcharticonintheupperrightcorner.

    Youshouldbeabletoseethatnowtheproductionrateoscillates,whichistypicalforthesupplychains

    withtheirinevitabledelays.

    Phase5.Visualizethedynamicsofdemandanduserbase

    Inthisphasewewillvisualizethedynamicsofunsatisfieddemandgeneratedbythemarketandthe

    numberofusers.Aswearemodelingthemarketinacompletelydisaggregatedway,weneedtoadd

    anotherstatisticsitemstothepopulationofconsumers,namelytheonecountingthenumberofagents

    thatareinUsesAstate.Also,ifwewanttoseethehistoryoftheseoutputs,weneedtocreateacouple

    ofdatasetsandachart,e.g.atimestackchart.

    1. IntheeditorofMainclickontheconsumersandgototheirStatisticspage.AddthesecondstatisticsitemNUseAofcounttypewithexpressionitem.statechart.isStateActive(item.UsesA

    )(which

    you

    can

    copy

    from

    the

    item

    NWantA

    and

    modify).

    2. StayinMainandaddtwodatasetsfromtheAnalysispalette:NWantADSandNUseADS.Tobeabletovisualizethehistoryofacertainvalue,weneedtokeepthathistoryinadatasetobject.

    2008XJTechnologieswww.anylogic.com 14

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    15/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    3. InthepropertiesofNWantADSspecifytheVerticalaxisvalue:consumers.NWantA(),forNUseADScorrespondingly:consumers.NUseA().

    DatasetsinAnyLogicarearraysofpairs(x,y).Ashorizontalaxisvalue(x)istimebydefault,thedataadded

    tothee.g.NWantADSwillbeoftype(t,demandattimet).Bydefaultthecapacityofadatasetis100

    samples.

    4.From

    the

    Analysis

    palette

    drop

    Time

    stack

    chart

    to

    the

    editor

    of

    Main

    Thebestwaytovisualizethedynamicsoffractionsofthepopulationintimeistimestackchart.

    5. Inthepropertiesofthechartaddtwodatasets:NUseADSwithtitleUsersAcolorredandNWantADS

    with

    title

    Demand

    for

    A

    and

    color

    pink.

    Set

    the

    Vertical

    scale

    of

    the

    chart

    as

    Fixed

    to

    1000.

    Thisdefineswhatwillbeshowninthechart.Bydefaultthetimewindowofthechartis100anditwill

    automaticallyupdatethedatasetseach1timeunit.

    6. RunthemodelYouwillseehowtheuserbaseforAgrows.Thesupplychainworkswellandthereisonlyasmallslightly

    oscillatingunsatisfieddemand.

    2008XJTechnologieswww.anylogic.com 15

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    16/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    Phase6.Addagentcommunicationtomodelwordofmouth

    Inthisphaseagentswillstarttalkingtoeachother.Asweareinterestedinusersoftheproducttalking

    topotentialusers,wewillsetupacyclictransitioninthestateUsesA.Thetransitionwillbetaken

    periodicallyandoneachoccurrencetheagentuserwillbesendingamessagetoarandomotheragent

    saying

    that

    product

    A

    is

    good.

    If

    another

    agent

    is

    a

    potential

    user

    (i.e.

    is

    in

    the

    state

    PotentialUser),

    he

    willreacttosuchamessagebychangingitsstatetoWantA.Obviously,wewillachievethisbeaddinga

    transitionfromPotentialUsertoWantAtriggeredbythecorrespondingmessage.

    1. GototheeditorofConsumeranddrawatransitioninsidethestateUsesAsuchthatitstartsandendsonthebordersofthestateandallitssegmentsareinsidethestate.CallitContactA.Set

    thetriggertypeofthetransitiontoRatewithrate=5*0.015.SetitsActiontosend("BuyA!",

    RANDOM);

    Theinternaltransitionisacyclictransitionthatdoesnottakethestatechartoutofthestateinwhichitis

    defined.InthiscasethetransitionContactAwilloccurwiththespecifiedrate.Thevalueofrate(5*0.015)

    needstobefurtherexplained.Wecouldmodelallcontactsstraightforwardly,i.e.5contactsperday,in

    whichcase

    the

    rate

    would

    be

    5.

    But

    we

    know

    that

    not

    all

    contacts

    are

    successful,

    i.e.

    convince

    potential

    userstobuyA.Thereforeherewemodelonlyasuccessfulfractionofcontactsandmakethemmorerare

    bymultiplyingby0.015.Intheactionofthetransitiontheconsumerchoosesanotherrandomconsumer

    (notnecessarilythepotentialuser!)andsendshimatextmessageBuyA!.

    2. GototheAgentpageofthepropertiesoftheConsumerclass.IntheOnmessagereceivedfieldwritestatechart.receiveMessage(msg);

    2008XJTechnologieswww.anylogic.com 16

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    17/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    Inthepreviousstepwehavesetuptheagentsuserstoperiodicallycontactotheragentsbysendingthem

    themessageBuyA!.InthesectionOnmessagereceivedwecanspecifythereactionontheincoming

    messages.Thecodeforwardsthemessagetothestatechart,wherewewillspecifyoneadditional

    transitioninthenextstep.

    3. AddonemoretransitiontothestatechartoftheConsumer:fromPotentialUsertoWantAandcallitWomA.SetthetriggerofthetransitiontoMessage,thetypeofthemessage:String,and

    letthetransitiontofireIfmessageequalsBuyA!.

    Thisisthelaststepinmodelingthewordofmouth.Themessagethatcomesfromanotheragentis

    forwardedthestatechart,and,ifthestatechartisinthestatePotentialUser,causesanimmediate

    transitiontoWantA.Inallotherstatesthemessageisignored.

    4. Runthemodel.Youshouldseethatthemarketsaturationisachievedalotfasternow.ThechartshowsthewellknownS

    shapedcurveoftheproductadoption.Theunsatisfieddemandduringthepeakofinteresttotheproduct

    isquitesignificant.

    2008XJTechnologieswww.anylogic.com 17

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    18/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    Phase7.Addproductdiscards

    Thisisaverysimplephase.Astheproductisdiscardedafteracertainperiodandtheuserneedstobuya

    replacement,wewilladdatransitionfromUsesAtoWantAtriggeredbyaconstanttimeoutDiscard

    Time.HavingdefinedsuchtransitionwerestrictthesojourntimeinthestateUsesAbyDiscardTime.

    1. InthestatechartofaconsumeraddatransitionfromUsesAtoWantA,callitDiscardA.SetthetriggertoTimeoutwithvalueuniform(17,23).

    WhenthestatechartentersthestateUsesA,asampleofstochastictimeoutisevaluatedandcountdownis

    startedforthetransitionDiscardA.Thetimeofusingaparticularproductitem(i.e.thetimespentin

    UsesA)thereforewillbedistributeduniformlyfrom17to23.PleasenotethatthetransitionContactAwill

    notberesettingthattimeoutasitisaninternaltransition.AftertakingDiscardAtheconsumerwillenter

    WantAstate,whichmeansheimmediatelyiswillingtobuyareplacement.

    2. RunthemodelAfterthemarketissaturated(everybodyisusingtheproduct),youwillobservetheperiodicshortagesin

    theproductsupplycausedbytheproductexpirationatthemajorityofusers.Thesupplychainasitisset

    upnowisnotcapableofsatisfyingthesepeaksofdemand.

    2008XJTechnologieswww.anylogic.com 18

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    19/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    Phase8.AddproductBtothemodel

    NowwecanaddtheproductBthatcompeteswithproductA.Wewilldoitbysimplycopyingthesupply

    chainforAandrenamingitselements.Theproductspecificpartoftheconsumersstatechartwillalso

    becopiedsothatPotentialUserwillhavetwoalternatives:transitiontoWantAortoWantB.Atthistime

    we

    will

    not

    model

    the

    maximum

    waiting

    time

    for

    a

    product.

    Two

    new

    statistics

    items

    and

    two

    datasets

    willbeneededtoshowthedynamicsofthemarket.

    1. IntheeditorofConsumerselecttwostatesWantAandUsesAandalltransitions(inotherwords,selectallstatechartelementsexceptforthestatePotentialUserandthestatechartentrypoint.

    Makeacopyoftheselectionandarrangethetransitionsasshowninthescreenshotbelow.

    RenameA1thatappearsintheendingsofthecopiedelementstoB.

    InthetransitionsWomBandContactBchangeBuyA!toBuyB!.

    InthepropertiesofthetransitionBuyBchangeRetailerStockBtoRetailerStockB.

    IntheEntryactionsofstatesWantBandUsesBchangethecolorstolightBlueandblue.

    Inthisstepwehavedefinedanalternativepathoftheconsumerbehavior.Weassumethesame

    advertizingeffectivenessforproductB(0.011)andsamecontactrateandadoptionfraction(obviouslywe

    canchangeanyofthoseparameters).Notethatthenewstatechartstructureallowsonlyinitialchoiceof

    productbyapotentialuser;oncetheinitialchoicehasbeenmade,thereisnowayforaconsumerto

    switch.

    2. IntheeditorofMainclickontheconsumersandgototheStatisticspage.Addtwomorestatisticitems:NWantBandNUseBwithconditions

    2008XJTechnologieswww.anylogic.com 19

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    20/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    item.statechart.isStateActive(item.WantB)anditem.statechart.isStateActive(item.UsesB)

    correspondingly

    ThesestatisticsitemswillcountthedemandandthenumberofusersoftheproductB.

    3. IntheeditorofMainselectthewholesupplychainforproductA(allSDelements)andalsotheeventupdateProductionA,copyitandpastetotheright.Rename:

    eventupdateProductionA1

    to

    updateProductionB,

    stock

    ProductionA1

    to

    ProductionB,

    etc.

    ChangetheactionofupdateProductionBtoProductionB=consumers.NWantB();

    ChangeAtoBinallformulasinthesupplychainforproductB.

    Needlesstosay,allthatcopyingandrenamingdoesnotlookelegant.Andindeed,AnyLogicoffersmuch

    betterwayofreplicatingpiecesofmodel:youcanpackthesupplychainandthecorrespondingeventinto

    anewActiveObjectclass,parameterizeitwithe.g.demand,andinstantiateittwiceinMain:onceforA

    andonceforB.Forsimplicityhoweverwearenotdoingitinthistutorial.

    4. IntheeditorofMainmakeacopyofthetwodatasets.ChangethenamesofthenewitemstoNWantBDSandNUseBDS.Changetheirvaluestoconsumers.NWantB()and

    consumers.NUseB().AddthesedatasetstothechartwithtitlesDemandforBandUsersB.

    5. RunthemodelYouwillseethesymmetricdiffusiondynamicsforAandB.Pleasenotethatthemarketshareisdefined

    onlybytheinitialadoptionandcannotchangelateronbecauseoftheconsumersstatechartstructure.

    Phase9.Addproductswitchingtriggeredbywaitingtime

    Accordingtoourproblemdefinition,ifaconsumerwaitstoolongforaparticularproduct,hebecomes

    impatientandwillbuywhateverisavailable(AorB),soproductswitchingispossible.Theconsumer

    2008XJTechnologieswww.anylogic.com 20

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    21/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    statechartcaneasilybemodifiedtocapturethis:weshouldaddtwotimeouttransitionsfromWantA

    andWantBtoanewstateWantAnything.Fromthatstate,inturn,therewillbetwoalternative

    conditiontypetransitionstoUsesAandUsesBtriggeredbythepurchaseofthecorrespondingproduct.

    1. AddanewstateWantAnythinginthemiddleoftheconsumersstatechart.DrawtwotransitionsfromWantAandfromWantBtothenewstate.CallthemGiveUpWainingAand

    GiveUpWaitingB.BothtransitionsshouldbeofTimeouttypewithtimeout2.Makeacopyofthe

    transitionBuyA(itwillbecalledBuyA1)andplaceitfromWantAnythingtoUsesA.Similarly,

    createacopyofBuyB.Leavethetriggersofthosetransitionsastheyare.IntheEntryactionof

    WantAnythingwritecurve.setFillColor(gold);

    Thetimeouttransitionsrestrictthewaitingtimeforaparticularproductto5.Oncetheconsumergetsto

    thestateWantAnything,hestartsmonitoringavailabilityofbothproductsandwillbuywhateverbecomes

    availablefirst.

    2. IntheeditorofMainclickontheconsumersandaddyetanotherstatisticsitemNWantAnywithconditionitem.statechart.isStateActive(item.WantAnything).Addthecorrespondingdataset

    NWantAnyDSwith

    Vertical

    axis

    value

    consumers.NWantAny()

    and

    add

    that

    dataset

    to

    the

    chart

    withgoldcolor.

    3. RunthemodelYouwillseethattheunsatisfieddemandgrowsovertime.Thisiscausedbythewrongdemand

    estimationsmadebythesupplychains:theproductionrateforeachproductdependsonlyonthenumber

    ofconsumersthatarewaitingforthatparticularproductanddoesnottakeintoaccountthosewhoarein

    thestateWantAnything.

    2008XJTechnologieswww.anylogic.com 21

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    22/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    4. ModifytheactionsofeventsupdateProductionAandupdateProductionBinthefollowingway:writeProductionA=consumers.NWantA()+consumers.NWantAny();andProductionB=

    consumers.NWantB()+consumers.NWantAny();.

    Thiswaywetakeintoaccountthedemandofthoseconsumerswhoarereadytobuyanyproduct.

    5. RunthemodelNow

    the

    supply

    chains

    work

    better,

    although

    there

    are

    slight

    periodic

    shortages.

    You

    can

    also

    see

    that

    the

    marketsharesfluctuatealittlebit,i.e.theproductswitchingoccurs.ButthebalancebetweenAandB

    staysaround50/50becausethemodeliscompletelysymmetric.

    2008XJTechnologieswww.anylogic.com 22

  • 8/8/2019 Mixed Model Simulation Tutorial 2008

    23/23

    HowToBuildaCombinedAgentBased/SystemDynamicsModelinAnyLogic.Tutorial

    Experimentingwiththemodel

    Atthistimethemodelfullyreflectstheproblemdefinitionandyoucandovariousexperimentswithit.

    Forexample,youcanplaywiththesupplychainpoliciesandexplorehowtheyaffecttheresulting

    marketshares.Itmightmakesensetomonitornotonlythedemandsatisfactionbutalsotheinventory

    levelatthestocks(which,obviously,youwishtokeepatminimumlevel).

    Thesupplychaincanbemodeledusingdiscreteeventorprocesstechnology(withAnyLogic

    EnterpriseLibraryblocks)insteadoftheSD.Thenitwillbealoteasiertomodelthedeliverytimeand

    granularity.

    Anotherinterestingsetofexperimentscanbedonewiththeconsumerbehaviorandconsumer

    population.Youcanaddasocialnetworkintothemodelwherecontactscanonlyoccurbetweenpeople

    whoknoweachother.Thiswillobviouslyaffectthediffusionprocess.Youcanaddheterogeneitytothe

    consumersattheparameterslevel(e.g.differentsensitivitytoadvertisingandtowordofmouth,

    differentlevelofbrandloyalty),oratthebehaviorpatternlevel(e.g.someconsumersmaybecome

    frustratedwith

    the

    product,

    spread

    negative

    information,

    etc.).