diff --git a/assets/finite-state-machines/first_traffic_light_FSM.png b/assets/finite-state-machines/first_traffic_light_FSM.png
new file mode 100644
index 00000000..43d7d0c0
Binary files /dev/null and b/assets/finite-state-machines/first_traffic_light_FSM.png differ
diff --git a/assets/finite-state-machines/intersection_2_ways.png b/assets/finite-state-machines/intersection_2_ways.png
index ecfdc4f2..48f9a1c2 100644
Binary files a/assets/finite-state-machines/intersection_2_ways.png and b/assets/finite-state-machines/intersection_2_ways.png differ
diff --git a/assets/finite-state-machines/simple_traffic_light.png b/assets/finite-state-machines/simple_traffic_light.png
new file mode 100644
index 00000000..1c2e4a76
Binary files /dev/null and b/assets/finite-state-machines/simple_traffic_light.png differ
diff --git a/source/finite-state-machines/ch-finite-state-machines.ptx b/source/finite-state-machines/ch-finite-state-machines.ptx
index 570638f9..54fa4ef4 100644
--- a/source/finite-state-machines/ch-finite-state-machines.ptx
+++ b/source/finite-state-machines/ch-finite-state-machines.ptx
@@ -3,14 +3,16 @@
Finite State Machines
-
In this chapter, we explore a powerful abstract model: finite-state machines.
- Beyond the theory, we'll see how to use Sage to define, model and build, then
- visualize and run a few examples of state machines to solve real-world problems.
-
+
This chapter delves into a powerful abstract model, namely the finite-state machines.
+ Beyond the theoretical framework, the content of this chapter demonstrates the use of Sage to define,
+ model, build, visualize, and execute examples of state machines, showcasing their application in
+ solving real-world problems.
+
+
diff --git a/source/finite-state-machines/sec-extended-example.ptx b/source/finite-state-machines/sec-extended-example.ptx
index b3818a54..199fc19c 100644
--- a/source/finite-state-machines/sec-extended-example.ptx
+++ b/source/finite-state-machines/sec-extended-example.ptx
@@ -1,6 +1,6 @@
- FSM in Action
+ State Machine in Action (cont.)Controlling Traffic Lights and Pedestrian Crossing Signals
diff --git a/source/finite-state-machines/sec-fsm-in-action.ptx b/source/finite-state-machines/sec-fsm-in-action.ptx
new file mode 100644
index 00000000..68da3ac7
--- /dev/null
+++ b/source/finite-state-machines/sec-fsm-in-action.ptx
@@ -0,0 +1,162 @@
+
+
+ State Machine in Action
+
+ state machines
+ model
+
+
+ FSM as Traffic Light Controller
+
The goal here is to design a finite state machine to model and control a simple traffic light system for a two-way road with pedestrian crossing. The traffic light operates with three states: Red (R), Yellow (Y), and Green (G). The pedestrian light also operates with three states: red (r), yellow (y), and green (g). State transitions in the FSM are triggered by a timer input that fires an event after a preset duration.
+
+
+
+
+
Simple street intersection
+
+
+
+
+ Inputs, Outputs and States
+
On the timer expiration event, the FSM must react accordingly, cycle through the specified states: Rg, Ry, Yr, and Gr, and print out the output description of the light state transitions for both traffic and pedestrian lights. The following defines the characteristics of the state machine
+
+
+
+
States: The FSM has the following states:
+
+
State Rg: Traffic light is Red, Pedestrian light is green.
+
State Ry: Traffic light is Red, Pedestrian light is yellow.
+
State Yr: Traffic light is Yellow, Pedestrian light is red.
+
State Gr: Traffic light is Green, Pedestrian light is red.
+
+
+
+
Inputs: Timer event fired when the preset duration expires, triggering the next state transition. The timer preset duration s periodic. Each cycle has 4 lapses as follow: 30sec, followed by another 30sec, then 5sec, and finally 10sec.
+
+
+
Outputs: The FSM shall produce the following outputs:
+
+
+ Traffic light turning Red and the Pedestrian light turning green
+
+
+ Traffic light turning Yellow and the Pedestrian light turning red
+
+
+ Traffic light turning Green and the Pedestrian light turning red
+
+
+ Traffic light turning Red and the Pedestrian light turning green
+
+
+
+
+
+
+
+
+
+ Implementation and Simulation
+
Sage built-in module will be used to define the FSM, display its state transition graph, and run it through a full cycle.
+
+
+ Step 1: Define the FSM
+
+
+ from sage.combinat.finite_state_machine import FSMState, FSMTransition
+
+ # FSM states, inputs and outputs
+ states = ['Gr', 'Yr', 'Rg', 'Ry']
+ inputs = [30, 5, 30, 10]
+ outputs = [
+ 'Traffic light turning Green, Pedestrian light turning red',
+ 'Traffic light turning Yellow, Pedestrian light turning red',
+ 'Traffic light turning Red, Pedestrian light turning green',
+ 'Traffic light turning Red, Pedestrian light turning yellow',
+ ]
+
+ # Create the state machine, define and add the states
+ fsm = FiniteStateMachine()
+
+ # the initial state
+ Gr = FSMState('Gr', is_initial=True)
+ fsm.add_state(Gr)
+
+ # the other states
+ Yr = fsm.add_state('Yr')
+ Rg = fsm.add_state('Rg')
+ Ry = fsm.add_state('Ry')
+
+ # defining 4 transitions, and associating them the state machine
+ # After 30sec, transition from Gr to Yr, set traffic light Yellow and Pedestrian light remains red
+ fsm.add_transition(FSMTransition(Gr, Yr, 30, 'turning Yr'))
+
+ # After 5sec, transition from Yr to Rg, set traffic light to Red, and Pedestrian light turns green
+ fsm.add_transition(FSMTransition(Yr, Rg, 5, 'turning Rg'))
+
+ # After 30sec, transition from Rg to Ry, traffic light remains Red , and Pedestrian light turns yellow
+ fsm.add_transition(FSMTransition(Rg, Ry, 30, 'turning Ry'))
+
+ # After 10sec, transition from Ry to Gr, traffic light turns back to Green , and Pedestrian light turns red
+ fsm.add_transition(FSMTransition(Ry, Gr, 10, 'turning Gr'))
+
+ print(fsm.states())
+ print(fsm.transitions())
+
+
+
+
+
+ Step 2: Display the State Transition Graph
+
+
+ # Display the graph of the FSM
+ fsm.graph().show(
+ figsize=[6, 6],
+ vertex_size=800,
+ edge_labels=True,
+ vertex_labels=True,
+ edge_color=(.2,.4,1),
+ edge_thickness=1.0
+ )
+
+
+
+ This will visualize the FSM as a directed graph with nodes representing states and edges showing transitions.
+
+
+
+
+ Step 3: Simulate a Full Cycle of the FSM
+
The simulation starts in the initial state (Rg) and transitions through all states, printing the action associated with each transition.
+
+
+ # pass in the initial state and the list of inputs
+ *_, outputs_history = fsm.process(
+ initial_state=Gr,
+ input_tape=[30, 5, 30, 10, 30],
+ )
+
+ # print out the outputs of the state machine run
+ [print(_) for _ in outputs_history];
+ print()
+
+
+
+
+
+
One limitation of using Sage built-in module is that it fails handling transitions that weren't defined in the FSM. For instance, in the previous example, if the timer durations pattern for the input does not match the defined transitions, the run will raise an exception, instead of gracefully handling the exception and defaulting to no transition (similarly, the same issue arise if attempting to run the FSM starting at state that is not part of the FSM definition).
+
+
+ *_, outputs_history = fsm.process(
+ initial_state=Gr,
+ # passing inputs not defined in the FSM transitions
+ input_tape=[5, 20, 30, 10],
+ )
+
+ [print(_) for _ in outputs_history];
+
+
+
+
diff --git a/source/finite-state-machines/sec-modeling-finite-state-machines.ptx b/source/finite-state-machines/sec-modeling-finite-state-machines.ptx
index 73afb40f..830f0639 100644
--- a/source/finite-state-machines/sec-modeling-finite-state-machines.ptx
+++ b/source/finite-state-machines/sec-modeling-finite-state-machines.ptx
@@ -8,14 +8,18 @@
- Although Sage does have a dedicated built-in module to handle state
- machines, we can still model, construct, display, and run relatively
- simple state machines, leveraging the general-purpose tools, such as graphs and
- transition matrices, to represent and work with state machines.
+ Although Sage does have a dedicated built-in rich module to handle various types of
+ state machines, it may not always be sufficient to address certain use cases or implement
+ specific custom behaviors of the machine. Additionally, the built-in module allows state
+ machines to be defined and constructed in different ways, providing greater flexibility
+ and making it more suitable from a programmer's point of view, but it may not fully
+ conform to the precise definition given earlier. This highlights that it is still possible
+ to model, construct, display, and run relatively simple state machines by utilizing
+ general-purpose tools, such as graphs and transition matrices, to represent and operate
+ on state machines.
- In this section, we'll explore how to define states, create a state transition graph,
- visualize the state machine, and simulate its execution in Sage.
-
+ This section examines the process of defining states, creating a state transition graph, visualizing
+ the state machine, and simulating its execution in Sage.
diff --git a/source/finite-state-machines/sec-state-machine-definition.ptx b/source/finite-state-machines/sec-state-machine-definition.ptx
index 8df01d9f..7adbdde0 100644
--- a/source/finite-state-machines/sec-state-machine-definition.ptx
+++ b/source/finite-state-machines/sec-state-machine-definition.ptx
@@ -9,7 +9,7 @@
A Finite-State Machine (FSM) is a a computational model that has a finite
set of possible states S, a finite set of possible input symbols (the input
- alphabet) X, a finite set of possible output symbols (the output alphabet)
+ alphabet) X, and a finite set of possible output symbols (the output alphabet)
Z. The machine can exist in one of the states at any time, and based on the
machine input and its current state, it can transition to any other state and produces
an output. The functions that take in the machine current state and its input and map
@@ -88,7 +88,7 @@
w: S \to Z is the output function, which specifies which output
- symbol w(s) \in Z maps to the machine current state s.
+ symbol w(s) \in Z associated with the machine current state s.
@@ -146,8 +146,13 @@
A Deterministic Finite Automaton (DFA) is a simplified automaton where each
state has exactly one transition for each input.
DFAs are typically used for lexical analysis, language recognition, and pattern
- matching (ex. a string-matching system to recognize specific languages or regular
- expressions).
+ matching.
+
@@ -169,7 +174,7 @@
Finite state machines are a foundational concept in computer science, often associated
- with tasks in relation with system designs (circuits and digital computers, algorithms, etc.)
+ with tasks related to system designs (circuits and digital computers, algorithms, etc.)
However, the vast and rich domain of applications of state machines extends far beyond simple
simulations to full control logic of complex industrial processes and workflows. These tasks
can vary in complexity, be as simple as a parity check or a complex as managing traffic patterns,