The third phase of behavior planning - 4. Formalized FSM


Let's consider a simple vending machine where everything costs 20 cents. Suppose this vending machine only needs nickels and coins, but nothing bigger or smaller.

We can then simulate the state of this vending machine based on the amount that has been deposited. The starting state will be zero cents. There are two possibilities.

We can put in nickels, five cents or we can put in a dollar to get the state to 10 cents.

Before we think about it, the rest of the transition is fairly straightforward. What if we were at 15 cents and someone put in a dime.

We can count it as 20. But let's just say that this machine needs the exact change so that a dime will fall off the small tray at the bottom of the machine.

As you can see, finite state machines are very simple in concept. So why talk about them?

They have their strengths and weaknesses. Let's start with our strengths.

First, finite state machines are easy to reason about . They are basically self-documenting because they map the logical state of the system directly to the physical state.

When the nickel enters the vending machine, the state changes to a state that is 5 cents larger than the current state.

Next, they are maintainable . If we want to tune this machine, everything costs

If you only add one state, then a quarter of things becomes trivial.

This brings us to the weakness of finite state machines. The main point is that they are easy to abuse.

If they are not well designed to start or if there is a problem , we just add another state which can lead to some sloppy code and unstable logic.

In practice this means that the finite state machine can grow as the state cardinality increases, which is difficult to maintain.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325370636&siteId=291194637