Understanding Finite State Machines (or Finite-State Automaton)

  Переглядів 22,050

Gary Explains

Gary Explains

День тому

A Finite State Machine can, at any given time, be in exactly one of a fixed number of states. The machine can transition from one state to another depending some input data or event.
FSM's are sometimes called a finite-state automaton, finite automaton, or just simply a state machine.
Introduction to Android app development: www.dgitacademy.com
Let Me Explain T-shirt: teespring.com/gary-explains-l...
Twitter: / garyexplains
Instagram: / garyexplains
#garyexplains

КОМЕНТАРІ: 100
@nikhilraojl
@nikhilraojl 3 роки тому
What a timing! I just started Cpython internals book which talks about Finite State Automaton very briefly. You video made it much more clear. Thanks Gary
@AshokKumar-bu2gk
@AshokKumar-bu2gk 3 роки тому
Happy to get more content from you.awesome stuff sir !
@jayk806
@jayk806 2 роки тому
Great explanation. Really inspiring, actually. Thanks!
@GaryExplains
@GaryExplains 2 роки тому
Glad it was helpful!
@filker0
@filker0 3 роки тому
Additional videos on the topic would be good. I write my states rather differently, and have variations based on the nature of the input data. For lexical scanning like your number validation FSM, I would have the accepting states all transition to the "valid" terminal state on the EOI (end of input) state. All states would transition to the "error" state if any unrecognized input was received, and for the non-accepting states, EOI isn't recognized. It might be fun to show how FSMs of this sort are mapped to regular expression syntax, since an RE is often the best way to parse input tokens from a character stream. Of course, FSMs are useful for a lot of things that RE can't be used.
@durragas4671
@durragas4671 3 роки тому
I should be studying for my Digital Systems exam but I'm browsing UKposts instead. Then I see this video. Perfect timing.
@GaryExplains
@GaryExplains 3 роки тому
Now you can do both!!! 😁
@devjock
@devjock 3 роки тому
This man just headfakes us all into compiler design, and it's beautiful!
@RobertBartlettBaron
@RobertBartlettBaron 2 роки тому
Sorry, Gary get gets close to, but not quite there. Sure, if you have had a course in compiler design, you already know this - however, if you haven't had such a course then I suspect making the connection is rather difficult.
@devjock
@devjock 2 роки тому
@@RobertBartlettBaron You have a point that it's not quite there, but to be fair (and pardon me if this is very apparent, but) if he'd be explaining compiler design, then the video would've been named "Compiler design". Not making the connection is fine. These are soft-skills that will come to the surface whenever people do decide to take a course in compiler design, and subsequently, they'll pick up on the intricacies much better. RIght now it's handily packaged in an entertaining video, and the knowledge will hang around until it's needed. (insert witty reference to interrupt handling) But yeah, that's exactly why I called it a headfake. Gary makes us learn skills that are potentially useful in more than one field.
@RobertBartlettBaron
@RobertBartlettBaron 2 роки тому
@@devjock I realize he is not explaining compiler design (see the original post for context), but he also doesn't mention the relationships between regular expressions and finite state machines, nor does he mention common applications that they are used in.
@nsxlai2000
@nsxlai2000 3 роки тому
Yes please do a video on FSM using GO... THANK YOU!
@prabhatyadav8189
@prabhatyadav8189 2 роки тому
Great explanation....🙂😀made complex term ...far easier...
@GaryExplains
@GaryExplains 2 роки тому
Glad to hear that.
@MarcosScheeren
@MarcosScheeren 3 роки тому
These are totally underrated!
@GaryExplains
@GaryExplains 3 роки тому
Tell me about it! 😊
@MarcosScheeren
@MarcosScheeren 3 роки тому
@@GaryExplains Also check the Xstate JS/TypeScript lib, it's really nice and has a cool SM visualizer
@2008abba
@2008abba 3 роки тому
I've never heard of FSM, I'd watch a second video on this topic
@middleclasspoor
@middleclasspoor 3 роки тому
This makes me so happy for high level languages! I don't know how you low level guys do it. I know this is an example of a fundamental computer science concept but if I had to program in this manner all of the time I would lose my mind! That's why I will never touch assembler! For me personally I don't care for anymore on the topic but the more sophisticated in your audience certainly may. Anyway, thanks for the great info. I'm really enjoying your channel!
@xtremegameuser
@xtremegameuser 3 роки тому
Hey Gary, In few days I will have an exam for digital system and I'm revising for fsm and can't believe you uploaded this video. Thank you. Will watch this to understand it better.
@GaryExplains
@GaryExplains 3 роки тому
Best of luck!
@cmdlp4178
@cmdlp4178 3 роки тому
What about PDA, push down automata, FSM extended by a stack. You could use it to verify/parse JSON.
@IAmPattycakes
@IAmPattycakes 3 роки тому
FSMs are a basic but fundamental concept that's the backbone of most software. I'm not really the audience who needs it, but I'd like to see more videos like this that cover basic design principles. Or FSMs in Go.
@carnright
@carnright 2 роки тому
Awesome! Reminds me of railroad diagram
@yoman9446
@yoman9446 3 роки тому
Thank you!
@GaryExplains
@GaryExplains 3 роки тому
You're welcome!
@WatchCuriousCat
@WatchCuriousCat 3 роки тому
More please!
@CedLePingouin
@CedLePingouin 3 роки тому
Hi Gary. I was a bit surprised that you made a video about FSMs, but as a programmer I really enjoyed it, and I think you explained it very well. (btw I also enjoyed the videos you made a while ago about writing a "software CPU") I'd like to see more videos about FSMs, in C (which I know but I find the subject interesting) and Rust (which I barely know, and am curious about). I think a video about FSMs for Game AI in Python would be very interesting as well.
@GaryExplains
@GaryExplains 3 роки тому
I am glad you enjoyed it. Thanks for the feedback about future videos. One question, if I may, why were you surprised that I made a video about FSMs?
@CedLePingouin
@CedLePingouin 3 роки тому
@@GaryExplains I was surprised because I've mostly seen videos about hardware stuff (which I also like) on your channel lately. Even though there has been programming videos before, of course. But it was a pleasant surprise :)
@GaryExplains
@GaryExplains 3 роки тому
@@CedLePingouin Understood. Thanks. Yes programming videos are also part of my repertoire. I have videos on C, Rust, Python, linked lists, sorting, and more.
@tenprinthello8127
@tenprinthello8127 Рік тому
This is irrelevant to the purpose of this video, but in Canada, there is no yellow light when transitioning back to green. I learned as a small child riding with adults that you could anticipate the green light by observing the orthogonal lights' yellows, but if you're at a red here, don't expect any help, the green will just pounce upon you with no warning.
@ashwani_kumar_rai
@ashwani_kumar_rai 3 роки тому
Hi garry thanks for this topic was wondering for a video where how regular expression are implemented. Not how to use matcher in java or c rather how to write own regular expression matcher logic .
@RPG_ash
@RPG_ash 3 роки тому
Interesting!
@electron7373
@electron7373 Рік тому
Game AI state machine would be good and more computer science topics welcome. Thank for the good clear explanation of a potentially confusing topic.
@lapidations
@lapidations 2 роки тому
Honestly, everything. Please do a series on FSMs!
@GaryExplains
@GaryExplains 2 роки тому
Sadly this video didn't get many views so I won't be doing another FSM video soon. 😢
@lapidations
@lapidations 2 роки тому
@@GaryExplains I looked at the views after commenting, I figured that would be your answer. But if it makes you feel happy, this video helped me more than any other in this year.
@lapidations
@lapidations 2 роки тому
@@GaryExplains Actually, what I really wanted is a series on practical yet very simple applications for each design pattern.
@marathigooner3129
@marathigooner3129 3 роки тому
Would like if you did a some videos or a series on FSM for AI using python. If you could also explain some Graph Search Algorithms like RBFS in that series then that would be great
@galdutro
@galdutro 2 роки тому
It would be nice to have future videos on context free grammars and Chomsky hierarchy!
@Flankymanga
@Flankymanga 3 роки тому
I think finite state machines are very important in device firmware thus showing one written in C for the audience would be beneficiary.
@robinholmes785
@robinholmes785 2 роки тому
Thank you! Python, Control logic?? Keep it coming :-)
@hamishhenderson2191
@hamishhenderson2191 Рік тому
Would love to see this in C as well
@spiritlynxs
@spiritlynxs Рік тому
Excellent video, thx. Can you please include a link to your github site for this?
@larrygall5831
@larrygall5831 3 роки тому
Where do you have "Red-Amber" before green? In the U.S. it's just green -> Amber, then Red (and back to green).
@GaryExplains
@GaryExplains 3 роки тому
In Europe. It helps the driver know which way the traffic light will go next. If you see just amber then you know red is next.
@YoutubeBorkedMyOldHandle_why
@YoutubeBorkedMyOldHandle_why 2 роки тому
Same for Canada, except Quebec of course, where amber is just a decoration. No need for red-amber, red-green, or pink etc.
@waynestewart1919
@waynestewart1919 3 роки тому
Python FSM for Game AI. That would be awesome.
@1MarkKeller
@1MarkKeller 2 роки тому
*GARY!!!* Good evening Professor! Good evening fellow classmates! Stay safe out there everyone!!!
@GaryExplains
@GaryExplains 2 роки тому
MARK!
@walkinmn
@walkinmn 3 роки тому
I´m learning Python so I would love more videos using it
@Quarky_
@Quarky_ 2 роки тому
C or Rust examples would be amazing!
@GaryExplains
@GaryExplains 2 роки тому
I published the C one already 👍
@Quarky_
@Quarky_ 2 роки тому
@@GaryExplains oh, I guess I missed the notification. I'll look for it! Thanks to UKposts's stupid recommendations, now I have too many channels with notifications enabled :(
@32_bits
@32_bits 3 роки тому
FSM in C and comparing to Python for a simple example would be usefull. Having written many large and complex progams running on MCU's without FSM's it would be good to know the best preactice process / steps for using FSM's.
@prabhatyadav8189
@prabhatyadav8189 2 роки тому
More is good 😄
@camryhsalem5139
@camryhsalem5139 3 роки тому
Fsm is everything
@fatimahaljaafari6739
@fatimahaljaafari6739 2 роки тому
We want to see similar in C. If it for FSM for protocol will be great
@GaryExplains
@GaryExplains 2 роки тому
I published the C FSM video already! 👍
@excitedbox5705
@excitedbox5705 2 роки тому
yes MORE
@TreeLuvBurdpu
@TreeLuvBurdpu 2 роки тому
The thumbnail seems to indicate a relationship between finite state machines and indexable fingers. I wanted to know about that.
@ShubhamBhushanCC
@ShubhamBhushanCC 3 роки тому
More Videos on CS topics please. And in python
@2008abba
@2008abba 3 роки тому
Second thought, I don't think a person who wasn't interested in this topic would watch the whole video and then comments after, they'd just move onto something else.
@stepans9183
@stepans9183 3 роки тому
Thanks Gary! I cannot find the code on github :(
@GaryExplains
@GaryExplains 3 роки тому
A Google search for "Gary explains GitHub" should be sufficient. Here is the link github.com/garyexplains/examples/tree/master/FSM
@osbaldotheVtenman
@osbaldotheVtenman 3 роки тому
I would like to see this applied in C!
@ChandrapalSd
@ChandrapalSd 3 роки тому
Who is stopping you from doing that ?
@chudchadanstud
@chudchadanstud 3 роки тому
It's already in C and its 1000x easier. Just use switch statements.
@osbaldotheVtenman
@osbaldotheVtenman 3 роки тому
@@ChandrapalSd Nothing, but I would like to see how Gary goes about coding it. Maybe I'd learn a thing or two!
@Hybrid.Robotics
@Hybrid.Robotics 3 роки тому
The "-1."or such as "`123." are valid. In fact, Python *does* accept these as valid.
@johnybro250
@johnybro250 3 роки тому
Maybe more real world problems with fsm and then move onto the game?. Bcoz I feel like it would be too complex.
@treyquattro
@treyquattro 3 роки тому
"sad puppy state" - that's where I live!
@randreas69
@randreas69 2 роки тому
Wouldn't mind at all to do this in another language. Wait.. Norwegian? Just kidding. But my teacher was a stickler for grammar as we did something like this on the BBC Micro and Turbo Pascal at the end of the 80s. We had bespoke norwegian made machines with around 220 Kb, the odd memory was for some graphics indeed.
@ShahJr
@ShahJr 3 роки тому
Python FSM game please!
@la6mp
@la6mp 3 роки тому
FSM in C, please! Great video, thanks!
@alaingraham
@alaingraham 3 роки тому
Part-time traffic lights (they do exist) have an end.
@muddyexport5639
@muddyexport5639 2 роки тому
RUST please.
@rukey3001
@rukey3001 2 роки тому
yes AI + FSM + Python
@fabiosallesctba
@fabiosallesctba 3 роки тому
Hey Gary please just keep on using Python! 😁👍🏼
@thaernejem7317
@thaernejem7317 3 роки тому
Python games AI please
@jamesgoacher1606
@jamesgoacher1606 3 роки тому
I have used this for probably ALL my working life, wasn't called FSM though it was called Logic. Initially Relay Logic then Solid State Logic, then PDP8 then PLC(Ladder or CSF) and then Computer directly. Then retirement. I have drawn a State Machine for a Client but only after the fact. The Loop Back was illustrated by a on the connecting line btw. Problem with basic State Machines is that they insist upon only a single change in State whereas my field of Machine Control often requires multiple Branching and coming back together. That requires passing to another State Machine (not sure why 'Finite' in there), not sure at this moment how you would comeback together. I preferred roughing out a program using a sort of pseudo 'C' in which what you are showing would probably be a 'Case' statement. I am almost twenty years out of date now and certainly not 'Current' and Python has come along during that time and do not like it very much. Try a version of 'C' or Assembler (my preferred) and by inference the Arduino version of 'C'. The machines I wrote Control for could quite happily mangle you or more importantly the Operator very badly so this is not a flippant subject for me.
@filker0
@filker0 3 роки тому
Ladder logic is used to implement state machines. Sometimes many simultaneous state machines. Ladder Logic also deals with analog signals pretty well, whereas most FSM you're going to see written in Python will be dealing with far less complex "real world" situations, since the beginner is not really ready to deal with hysteresis, feedback loops, and process control timing, where a difference of 0.0002 seconds (200 microseconds) might be the difference of diverting a bottle on a conveyer that failed visual inspection into a reject bin or kicking the bottle so hard that it smashes on the wall (or hits a factory worker if you're very unlucky). I have great respect for PLCs, Logix controllers, and other such devices, and for the people who program them well. I studied various forms of state machines in college, did some embedded programming and compiler development in various jobs after college, and over the years, most of my significant work involved extensive use of state machines. It was about 20 years after I graduated from college that I first started working with PLCs, but I picked it up pretty quickly. Still, it took me a while so that my code could compensate for cranky pneumatic actuators that changed performance significantly from when the bottle inspector was turned on to about 15 minutes of running once the rubber pressure hoses had stopped softening. The reason they're called "Finite" is because there are a countable number of defined states the state machine can be in. There are many kinds of FSMs, ranging from simple accepting automata which have a single "start" state and have terminal states, and really don't branch a lot, but there are much more complex FSM. Often, it is easier to create multiple linked state machines, each having its own independent state, but with synchronization points where they can affect one another. Deterministic Finite Autonoma (DFAs) are predictable and will always behave the same for a given set of inputs, but there are other kinds that, although the state transitions are well defined, the outcome for a given set of inputs can be different based on some external influence such as a random number generator. There are push-down autonoma that have a state stack (Turing machines), "Finite State Machines with Oracle" that depend on an external process to determine state changes, and so on. Regular Expressions are "Accepting Finite State Machines", and are good for parsing serial input data. My typical FSM for power sequencing of a circuit board defines each state in terms of the entry conditions, a list of actions to take on entry to the state (handled by an "enter state" procedure), a list of exit conditions each with the state that condition causes transition to, and an "evaluate" procedure that is called or event (timer, interrupt, etc.) handling, and in which the exit conditions are evaluated and the exit handling and transition to the next state is signaled when a matching condition is found. In C or Assembly, all states are represented as 'structs' that have function pointers for the "enter state" and "evaluate" entries (some also have a "reset" function pointer), and the FSM is driven by an outer "super-loop" that may run multiple parallel FSMs for different purposes (eg, asynchronous communication, watchdogs, key decode, A/D, etc.). What I do is often safety and time critical, and frankly, a "switch" statement isn't going to cut it. I always start out with a directed graph of the states, labeling all of the edges with the conditions under which the that edge is followed. A lot of analysis gets done on the graph before the first line of C code is written, though typically, the graph gets modified as the interactions between the FSM implementation and the hardware it runs in become better understood. After burning out a few $800.00+ FPGAs, you learn to be very careful about FSM design. Many real-world FSM are most easily documented as a set of macro-states which are each smaller FSM made of of "micro-states". In these cases, rather than having a lot of parallel FSM that interact, you can think of it as either a birds-eye-view of a single gigantic FSM or an FSM made of "black box" states that are much simpler than the top-level FSM in which they exist. I'd much rather do formal documentation for 16 FSM each with around 10 states than one FSM with 160 states.
@jamesgoacher1606
@jamesgoacher1606 3 роки тому
That is a lot of information and I wxpect I know most of the things you are referring to by different descriptions. I am not comfortable with expressive abbreviations and those things which sound cute as well so I just use them as is. I do not prefer Ladder Logic if there is a choice. Seimens PLC, which I used often had a Logic Block Form and a sort of Assembler form They called them Control Flow System (CFS) and Statement List (STL) which were far more flexible and "Leaner". The last version I used almost twenty years ago, S7 could even be considered Object Type with a bit of imagination using local and global variables very nicely. I do not recall a State Machine type of function. I created Step Functions (the design is within their literature) with Set/Reset blocks formed into Sequence Steps with the time critical stuff going off in dedicated modules and in the background. I do not believe that you can get a meaningful timer of less than a tenth, maybe 0.05 with a PLC without very careful program layout because of the Scan Time Issue. The last time I added a scan time indicator into a program (using an S5-115 generation unit at the basic end of the range) where I was concerned about reaction time and wanted to check before I decided to use an expensive interupt type module it was about 15ms and I only need 30ms so I stayed with the cheaper solution. Processors are much faster nowadays up from 4.77MHz to lots of GHz now so I expect that PLC have speeded up too. My current knowledge of the subject remains the same as twenty years ago, not kept up-to-date. I will read your reply again more carefully. It was interesting.
@lucutes2936
@lucutes2936 10 місяців тому
ZOV
@chudchadanstud
@chudchadanstud 3 роки тому
Imagine using python for fsm lol. The absolute state of python. They're regretting not having switch cases. You don't need to think with swtch-cases
@GaryExplains
@GaryExplains 3 роки тому
Hmmm. I don't see how a switch statement helps. A dictionary is much more elegant.
@chudchadanstud
@chudchadanstud 3 роки тому
@@GaryExplains It's much easier to do: switch(state) { case 0: (do something here) state=1; case 1: (do something else here) state=nth; case nth: ... default: printf("I'm done."); break; } Than to do all that python stuff. Enums make it much better. Python seriously needs switch statements. They just jump to a point in memory rather than check conditions.
@GaryExplains
@GaryExplains 3 роки тому
@@chudchadanstud No, sorry, I don't agree. My Python code is just one line to know what handler to call to "do something". 1 line for 5 states and 1 line for 25 states, always 1 line. That case statement is hideous. Imagine if you had 50 states that would be a sprawling mess. Unreadable and unmaintainable.
@GaryExplains
@GaryExplains 3 роки тому
@@chudchadanstud Also, the idea is to have a FSM that can be dynamically created, your switch statement is a hardcoded nightmare. Even in C you should be using a table of some kind.
@chudchadanstud
@chudchadanstud 3 роки тому
@@GaryExplains Yes, you can work around it in many ways using, enums, abstract data structures, functions, pointers, OOP principals etc. It's also why I mentioned enums, with enums you don't have to use literals which will make tracking down things difficult just read the enum. An IDE can can track down every reference of an enum or auto complete. Order in the switchs statement matters less. It's also not a string so you'll notice typos before hitting compile.
@babaksafaei722
@babaksafaei722 Рік тому
This is so confusing!
@ShahJr
@ShahJr 3 роки тому
Python FSM game please!
Computers Without Memory - Computerphile
8:52
Computerphile
Переглядів 333 тис.
Такого от бабушки мы не ожидали 😂
01:00
SUPER PRAYER (all 4 shorts) Steve, Herobrine & Alex
00:27
Sam Green
Переглядів 12 млн
Блоховирус !🦠 #симба #тигра #булли
00:57
Симбочка Пимпочка
Переглядів 8 млн
Анна Трінчер - Бар за баром (Official Music Video)
02:38
Анна Трінчер
Переглядів 725 тис.
A-Level Comp Sci: Finite State Machine
8:21
justAlevel
Переглядів 93 тис.
How NES Games Use State Machines For Everything
8:21
NesHacker
Переглядів 29 тис.
Floating Point Numbers - This is Where Things Get Weird!
15:11
Gary Explains
Переглядів 2,2 тис.
What is a State Machine?
7:03
Chris Oliver
Переглядів 17 тис.
NTFS vs FAT32 vs exFAT - Everything You Need To Know
14:16
Gary Explains
Переглядів 106 тис.
Let's Learn Python #19 - Finite-State Machines (FSM)
22:11
Trevor Payne
Переглядів 82 тис.
Understanding Mesh Networking (feat. MikroTik Audience)
17:12
Gary Explains
Переглядів 53 тис.
Finite State Machines
12:25
FREGE: A Logic Course Elaine Rich, Alan Cline
Переглядів 19 тис.
Такого от бабушки мы не ожидали 😂
01:00