Imagine that you walk into the elevator of this tall building, press a button for a floor, and then the elevator automatically moves and eventually stops at the floor you want. This series of actions may seem simple, but behind it lies a complex system called a finite state machine (FSM), which is responsible for ensuring that the elevator can enter and exit each floor at the right time. In this article, we will take a deep dive into the working principles of finite state machines and reveal the intelligent design behind elevators.
A finite state machine is a mathematical computation model that can be in one of a finite number of states at any given moment. These states transition between each other through input.
The core components of a finite state machine include: states, initial states, and inputs that trigger transitions. The elevator's finite state machine keeps track of the current floor and the floor button pressed by the passenger to determine the next action. Of course, the types of finite state machines are divided into deterministic finite state machines (DFA) and non-deterministic finite state machines (NFA). DFA means that each state has a definite transition path for each possible input, while NFA may have multiple transition paths.
For an elevator, the status includes the floor it is currently on and whether it is running. Assume that the elevator is currently on the 2nd floor. When a passenger presses the button for the 5th floor, the state and transition process are as follows:
The behavior of an elevator can be visualized using a state transition diagram, which helps us understand the interactions between different states.
The nodes of a state transition diagram represent different states, while arrows describe the transitions between states. For example, the arrow from the 2nd floor to the 3rd floor indicates the action of pressing the up button. Such graphical representation makes it easier for us to understand the behavior and logic of the elevator in different states.
In addition to elevators, finite state machine models are also widely used in other devices, such as vending machines and traffic lights. In these scenarios, the FSM is responsible for controlling the different actions of the system to ensure that they can only be executed under appropriate conditions. For example, traffic lights use state machines to determine when to change colors, thereby controlling the safe and efficient flow of traffic.
Through these examples, we can see that finite state machines are the basis of many automated systems. It enables the system to respond to changes in the external environment, thereby improving efficiency and convenience.
All of these advances allow us to think about how future automated systems will further mimic human behavior and decision-making processes to solve various challenges in daily life and improve our quality of life?