Finite State Machines
Finite State Machines
http://robot.unipv.it/toolleeo
Models of computation and abstract machines
It consists of:
1 An unbounded tape divided into cells. Each cell contains a symbol
from some finite alphabet.
2 A head that can read and write symbols on the tape and move the
tape left and right one (and only one) cell at a time.
3 A state register that stores the state of the machine (within a finite
set of states).
Alan Turing
4 A finite table of instructions that, given the current state and the (1912-1954)
symbol it is reading on the tape, tells the machine what to do:
write a symbol, move the head, assume a new state.
Turing Machine in complexity theory
inputs −→ outputs
0 1 1 1 −→ 0 1 0 0
0 1 1 1 1 0 −→ 0 1 0 0 0 1
1 0 1 0 1 0 −→ 0 1 1 1 1 1
Graphical representation of FSM using graphs
Edge-detector example
This graphical representation is knows as state diagram.
A state diagram is a direct graph with a special node representing
the initial state.
Mathematical model of a FSM
A FSM is a five-tuple
(Σ, Γ, S, s0 , δ)
where:
• Σ is the input alphabet (a finite, non-empty set of symbols).
• Γ is the output alphabet (a set of symbols).
• S is a finite, non-empty set of states.
• s0 is the initial state, an element of S.
• δ is the transition function: δ : S × Σ → S × Γ.
Exercise: math model of edge detector
0 1
A B,0 C,0
B B,0 C,1
C B,1 C,0
The notion of state
Transitions between states govern the discrete dynamics of the state machine
and the mapping of inputs to outputs. The FSM evolves in a sequence of
transitions.
We can number these transitions starting from 0 for the initial state.
Specifically, let x : N → S to be a function that gives the state of an FSM at
transition k ∈ N. Let u : N → Σ and y : N → Γ denote that input and output
at each transition. Hence, x(0) ∈ S is the first input and y(0) ∈ Γ the first
output.
|States| = npm
Some state machines will have states can never be reached, so the
set of reachable states – comprising all states that can be reached
from the initial state on some input sequence – may be smaller
than the set of states.
For example, in the traffic light FSM, the c variable has 61 possible
values and there are 4 bubbles, so the total number of combination
is 61 × 4 = 244. The size of the state space is therefore 244.
However, not all of these states are reachable. In particular, while
in the YELLOW state, the count variable will have only one of 6
values in {0, . . . , 5}. The number of reachable states, therefore, is
61 × 3 + 6 = 189.
Determinacy
If for any state of a state machine, there are two distinct transitions
with guards that can evaluate to true in the same reaction, then
the state machine is nondeterminate or nondeterministic.
It is also possible to define machines where there is more than one
initial state: such a state machine is also nondeterminate.
Applications
• modeling unknown aspects of the environment or system
• hiding detail in a specification of the system
• non-deterministic FSMs are more compact than deterministic
FSMs
a classic result in automata theory shows that a
nondeterministic FSM has a related deterministic FSM that is
language equivalent
but the deterministic machine has, in the worst case, many
more states (exponential)
Behaviors, Traces and Computational Trees
Computational tree:
Example of trace:
1/0 1/0 0/1
((1, A, 0), (1, C, 0), (0, C, 1), . . . ) ≡ A −−→ C −−→ C −−→ . . .
Implementation: imperative programming language
Implementation: UML State Machine Diagram
Example: ATM
Reference: http://www.uml-diagrams.org/state-machine-diagrams.html
Implementation: LabVIEW Statecharts
pgoal
piL
WO i
r piH
pstart
essentials:
• motion-to-goal until an obstacle is encountered
• obstacle circumnavigation until the r straight line is
encountered, i.e., the line connecting the starting point and
the goal
• at that point, back to motion-to-goal along the r straight line
Exercise: Bug 2 - hypoteses (1/2)
• Hypoteses:
discretized workspace : each point belongs to a finite set W
dist(P1,P2) : is a function that computes the distance between
P1 and P2
isonR(P1) : is a function that returns true if P1 is on the line r
• Input:
touch: binary variable set by a proximity sensor in front of the
robot
pos: variable in W , updated by a position sensor
• Output (actions):
go() : robot moves along the straight line in front of it
turn(...) : robot rotates; the action is instantaneous
(simplification)
coastObs() : robot proceeds coasting the obstacle
stop() : robot stops
Exercise: Bug 2 - hypoteses (2/2)
• State variables:
hit : variable in W ∪ {N U LL}, which stores the hit point
start : variable in W , which stores the starting point. It is
necessary for calculating the line start-goal
• Parameter:
goal : constant in W
Exercise: Bug 2 - Mealy FSM
Composition of State Machines
The problem of complex systems
We consider:
• concurrent composition
synchronous
asynchronous
• hierarchical composition
Concurrent composition
*A or B *A, B or both
**nondeterministic 1 2
**made by the environment 3 4
How do we resolve
cycles?
s = F (s)
The existence of a fixed point, its uniqueness and methods to find it are very
interesting topics, but they are out of the scope of this course. Check leeseshia.org
for more details.
Hierarchical composition
∀t ∈ Tm , ṡ(t) = a,
We refer to this
systems as
hybrid automata.
Example: sticky masses - problem definition
release
together