Let's Learn Python #19 - Finite-State Machines (FSM)

preview_player
Показать описание
This week, I cover what a Finite-State Machine (FSM) is, how to plan one out, how to create two different ones and why we use them!

LINK TO AI BOOK:

Please leave me a comment or question below! Like and Subscribe to show your support! :D

=========================================

Music by Juto: Link Coming Soon!

=========================================
--- SUPER TUTORIAL LIST!!! ---

PLAYLISTS

=========================================
Рекомендации по теме
Комментарии
Автор

I love your videos!! I love these advanced programming concepts unlike the other tutorials that keep going on about loops and stuff. Make a python 3 version of these and add more things please! Thank you!

pinch-of-salt
Автор

This is an excellent tutorial. I had to watch the whole thing about 3 times but was able to use your method to create my own functioning FSM in about an hour. I appreciate it!

mag
Автор

Thanks for this video! I have your book, but the link to the source code leads to nowhere... Is there another place where I can download the scripts?

VeronikaAlexander
Автор

In the second example you need to initialize the current state for the RobotMaid, example: r.FSM.SetState("Sleep"), otherwise you will get: AttributeError: 'NoneType' object has no attribute 'Execute'

californiaesnuestra
Автор

Hey Trevor, I like your video. I have a small bit of feedback: For your actual code, you might find that using a monospaced font makes it easier to read.

bluegiger
Автор

well that's a clear and not so long way to explain how to implement a FSM.
thanks a lot

steampunk
Автор

I like your series on Python.  This and another called "Python By Osmosis" are the most informative nuggets of Python I've found on YouTube broken down in 5-10 minute bytes.  Would you be interested in doing some videos on Rasberry Pi DIY projects in Python?

vzuzukin
Автор

Hi, thank you for the video. Where can we find the files/codes in the video? Thanks

billma
Автор

Thanks for creating this! It helped me understand the concept better. cheers!

RobbieNerve
Автор

Can you please share the code you used in the example, please?

PrathamGupta
Автор

Hello! Thanks for an really nice tutorial, but I have one question (If you see this)

If I would want to have multiple robot maids with unique IDs (As in the book "Programming Game AI by Example"), should I make the Char an actual class and have it produce A unique ID for each?
Or should I simply make a Dictionary in the main function that stores each object with a unique ID assigned as the Key?

Which would you recommend?

torylva
Автор

Hey, Trevor. I want to ask. Why I just showing "Transitioning..." when I run the code. Where i had created mistakes. Thank you very much
from random import randint
from time import clock


State = type("State", (object, ), {})

class LightOn(State):
def Execute(self):
print ("Light is On!")

class LightOff(State):
def Execute(self):
print ("Light is Off!")



class Transition(object):
def __init__(self, toState):
self.toState = toState

def Execute(self):
print ("Transitioning...")




class SimpleFSM(object):
def __init__(self, char):
self.char = char
self.states = {}
self.transitions = {}
self.curState = None
self.trans = None

def SetState(self, stateName):
self.curState = self.states[stateName]

def Transition(self, transName):
self.trans = self.transitions[transName]

def Execute(self):
if (self.trans):
self.trans.Execute()

self.trans = None
self.curState.Execute



class Char(object):
def __init__(self):
self.FSM = SimpleFSM(self)
self.LightOn = True



if __name__ == "__main__":
light = Char()

light.FSM.states["On"] = LightOn()
light.FSM.states["Off"] = LightOff()
light.FSM.transitions["toOn"] = Transition("On")
= Transition("Off")

light.FSM.SetState("On")

for i in range(20):
startTime = clock()
timeInterval = 1
while (startTime + timeInterval > clock()):
pass
if (randint(0, 2)):
if (light.LightOn):
light.FSM.Transition("toOff")
light.LightOn = False
else:
light.FSM.Transition("toOn")
light.LightOn = True
light.FSM.Execute()

sofianfadli
Автор

this video was really helpful. thank you!

dsdsdsds
Автор

Lovely tutorial Man!!
Can we use customised ContextManagers for enter and exit states?

daydreamer
Автор

Hi Trevor, Thanks for this video it was really helpful. As i am building a FSM for one of my projects...
I tried implementing the code but i keep getting this error: self.FSM.AddState("Sleep", Sleep(self.FSM))
AttributeError: 'FSM' object has no attribute 'AddState'

i checked for spelling but everything seems fine. Could you help please?

abhinabmohanty
Автор

it is a great idea of F.S.M. I had try the codes of robotfsm.py In raspberrypi. it works good, but fail some times after running for five or ten seconds. do you have any idea to fix it? I had checked with the video carefully with/nothing wrong. Thankyou your tutorial!

danielchan
Автор

thank you very much, your tutorial is very helpful

fuhodev
Автор

Is it possible for us to see the full source code? I keep on comparing your lightbulb FSM to the one I made, and keep on running into this error: 

Traceback (most recent call last):
  File Learn\lightbulb_FSM.py", line 76, in <module>
    light.FSM.Transition("toOn")
  File Learn\lightbulb_FSM.py", line 38, in Transition
    self.trans = self.transitions[transName]
KeyError: 'toOn'

Is it just a Python 3.x problem, or is it just a problem in my program? 

thestarseekers
Автор

If I have a complex game and I have there for example state for menu, for play, for exit, etc. I need to create a different state machine to handle player states and a different state machine to handle menu state and other things? A game event in a MMORPG can be a state to? 

alexandrumarianlita
Автор

Amazing tutorial like your series :)
just one thing though in actual state e.g. CleanDishes you don't need __init__.

AnuragBisht