10.07.2015 Views

State Machines Finite State Machine Real World Examples - Etsu

State Machines Finite State Machine Real World Examples - Etsu

State Machines Finite State Machine Real World Examples - Etsu

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CSCI 1900Discrete StructuresGraphs and <strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong>Reading: Kolman,Sections 8.1 and 10.3<strong>State</strong> <strong><strong>Machine</strong>s</strong>• A machine that has input, an internal memorythat can keep track of information about theinput history, and an optional output is called astate machine. (Paraphrased from textbook, p.390)• “The complete internal condition of the [state]machine and all of its memory, at any particulartime, is said to constitute the state of themachine at that time.” (Quoted from textbook, p.390)CSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 1CSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 2More Informal Definition of a <strong>State</strong>• The current condition of any machine isdefined by certain properties including:– the inputs that brought the machine to thisstate;– the current output of the machine; and– the response the machine will have to newinputs.• These conditions are referred to as states.<strong>Finite</strong> <strong>State</strong> <strong>Machine</strong>• Assume that we have a finite set of statesthat a machine can be in, S, and a finiteset of possible inputs to that machine, I.• A machine is a <strong>Finite</strong> <strong>State</strong> <strong>Machine</strong>(FSM) if when any input from the set I isinput to the machine causing it to changestate, the state it changes to will becontained in the finite set of states, S.CSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 3CSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 4<strong>Real</strong> <strong>World</strong> <strong>Examples</strong>• All of the math aside, many common itemsor even non-technical tasks can bemodeled with a finite state machine.• <strong>Examples</strong>:– Soda machine– Software applications– Spell checker– Driving to school/workExample – Soda Vending <strong>Machine</strong>• Assume a soda vending machine that acceptsonly nickles, dimes, and quarters sells only onekind of soda for 25¢ a piece.• Identify all of the possible states• 0¢: no money at all – waiting for any money• 5¢, 10¢, 15¢, and 20¢: some money, but not enough– waiting for coin return or more money.• 25¢, 30¢, 35¢, 40¢, and 45¢: enough money – waitingfor select button, or coin return button• <strong>Finite</strong> states (10) and finite inputs (5¢, 10¢, 25¢,select button and coin return button)CSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 5CSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 61


Soda <strong>Machine</strong> Block Diagram<strong>State</strong> TransitionsNicklesDimesQuartersSelect ButtonCoin ReturnSoda<strong>Machine</strong>SodaChange• A state transition is the definition of the destinationstate based on the current state and the system input.• A state transition must be defined for every input outof every state.• Exactly one destination state is defined for everyallowable input.• Example: In testing software for quality control, everypossible user input from every possible state must beconsidered even if the input makes no sense. Inaddition, no user input can take the system to morethan one state.CSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 7CSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 8<strong>State</strong> Transition Functions• The function, f x , that defines which statethe machine will go to after an input, x, iscalled the state transition function.• Denoted f x (s) where x denotes the inputand s denotes the current state.• Example: If the soda machine is in thestate representing 10¢ received, insertinga nickle will move it to the staterepresenting 15¢ received.f Nickle (10¢) = 15¢Set of <strong>State</strong> Transitions = Relation• Since the state transition function defines everytransition of an FSM, a set of tuples of the form(s m , s n ) can be created where s m is the state wherea transition starts and s n is the state where thattransition ends.• Assume a relation R M is the set of tuples describedabove. An FSM then can be defined as the set ofall states, S, the set of inputs, I, and the relationdefining the state transitions, R M .R M = {(s m , fs m (x)) | ∀ x ∈ I and ∀ s m ∈ S}CSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 9CSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 10<strong>State</strong> Transistion Table• A table summarizing all of the statetransitions of a finite state machine is calleda state transition table.• Each row of a state transition tablerepresents the current state while thecolumns for that row show the destinationor next states based on different possibleinputs.Soda <strong>Machine</strong> <strong>State</strong> Transition TableCurrentstate0¢5¢10¢15¢20¢25¢30¢35¢40¢45¢Nickle5¢ 110¢ 115¢ 120¢ 125¢ 125¢ 230¢ 235¢ 240¢ 245¢ 2Dime10¢ 115¢ 120¢ 125¢ 130¢ 125¢ 230¢ 235¢ 240¢ 245¢ 21 – Accepts inserted money2 – Any money inserted is returnedQuarter25¢ 130¢ 135¢ 140¢ 145¢ 125¢ 230¢ 235¢ 240¢ 245¢ 2Selectbutton0¢5¢10¢15¢20¢0¢ 30¢ 30¢ 30¢ 30¢ 3Coin returnbutton0¢ 40¢ 40¢ 40¢ 40¢ 40¢ 40¢ 40¢ 40¢ 40¢ 43 – Soda delivered and change returned4 – All inserted money is returnedCSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 11CSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 122


FSM Labeled DigraphSince a relation R M can be defined for an FSM using astate transition function, then a digraph can be createdto represent the relation. It must be a labeled digraphto indicate which input specifies which transitionCurrentstate a b R M = {(s 0 , f a (s 0 ) = s 0 ),s 0 s 0 s 1s 1 s 2 s 0s 2s 1s 2(s 0 , f b (s 0 ) = s 1 ),(s 1 , f a (s 1 ) = s 2 ),(s 1 , f b (s 1 ) = s 0 ),(s 2 , f a (s 2 ) = s 1 ),(s 2 , f b (s 2 ) = s 2 )}FSM Labeled Digraph (continued)as 1bsb0aas 2bCSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 13CSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 14Soda <strong>Machine</strong> Digraph(Select button transitions are missing)25¢0¢QDN5¢N10¢NDQDQD15¢ QNND20¢ Q30¢35¢40¢45¢Coin return buttonCSCI 1900 – Discrete Structures<strong>Finite</strong> <strong>State</strong> <strong><strong>Machine</strong>s</strong> – Page 153

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!