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

Learn Texas Holdem Video Source & Info:

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! 😀

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

http://www.facebook.com/TPayneExperience

http://www.instagram.com/TPayneExperience

Music by Juto: Link Coming Soon!

=========================================
— SUPER TUTORIAL LIST!!! —

LLP #25 – UI with PyQt & OpenGL – http://youtu.be/eJveTXSXs8Q
LLP #24 – UI with Python, PyQt & Qt Designer – http://youtu.be/GLqrzLIIW2E
LLP #23 – Regular Expressions – http://youtu.be/ZdDOauFIDkw
LLP #22 – Multithreading – http://youtu.be/i1SW4q9yUEs
LLP #21 – Min Max Algorithm – http://youtu.be/fInYh90YMJU
LLP #20 – A* Algorithm – http://youtu.be/ob4faIum4kQ
LLP #19 – Finite-State Machines (FSM) – http://youtu.be/E45v2dD3IQU
LLP #18 – Factory and @classmethod – http://youtu.be/flOXIdWUpmU
LLP #17 – Type Class Creation, Metaclass – http://youtu.be/pd4Lm_WLJpM
LLP #16 – Singletons – http://youtu.be/6IV_FYx6MQA
LLP #15 – Nesting Functions and Decorators – http://youtu.be/fVon4QaY4wo
LLP #14 – *Args, **Kwargs – http://youtu.be/WWm5DxTzLuk
LLP #13 – UML – Unified Modeling Language – http://youtu.be/U3B5z2HQlaQ
LLP #12 – Abstract Classes, Multiple Inheritance – OOP 3 of 3 – http://youtu.be/rOaRMW8jYOo
LLP #11 – Overriding & File Mng. – OOP 2 of 3 – http://youtu.be/TF_y8Gta0vY
LLP #10 – Inheritance – OOP 1 of 3 – http://youtu.be/pxbdnrjf-Uc
LLP #9 – Creating Text Files – http://youtu.be/DRZdfd5_rdg
LLP #8 of 8 – Classes – http://youtu.be/trOZBgZ8F_c
LLP #7 of 8 – Files and User Input – http://youtu.be/0ury8KHQdL4
LLP #6 of 8 – Functions – http://youtu.be/qO4ZN5uZSVg
LLP #5 – Exception Handling – http://youtu.be/hrR0WrQMhSs
LLP #4 – Loops – http://youtu.be/6HWK6O4-28E
LLP #3 – Conditionals, If, Else, Elif – http://youtu.be/mQrci1kAwh4
LLP #2 – Strings, Lists, Tuples and Dictionaries – http://youtu.be/19EfbO5D_8s
LLP #1 – Integers, Floats and Maths – http://youtu.be/D48iCw3WWpI

PLAYLISTS

BASICS – https://www.youtube.com/playlist?list=PL82YdDfxhWsDJTq5f0Ae7M7yGcA26wevJ
OBJECT ORIENTED PROGRAMMING – https://www.youtube.com/playlist?list=PL82YdDfxhWsAyY3iNNDC1kUKWAJibUGi6
SCRIPTING PLAYLIST – https://www.youtube.com/playlist?list=PL82YdDfxhWsC-3kdTKK2_mwbNdBfVvb_M

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

Source: YouTube

Share this video:
Let’s Learn Python #19 – Finite-State Machines (FSM)

10 thoughts on “Let’s Learn Python #19 – Finite-State Machines (FSM)

  1. Great tutorial, but I have a problem, could you help me please? I keep getting this error:
    File "C:Python27RobotMaid_FSM.py", line 143, in <module>
    r = RobotMaid()
    File "C:Python27RobotMaid_FSM.py", line 129, in _init_
    self.FSM.AddState("Sleep", Sleep(self.FSM))
    AttributeError: 'FSM' object has no attribute 'AddState'

    Please any help would be very much appreciated. Thank you.

  2. 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?

  3. 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.SetState(self.trans.toState)
    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")
    light.FSM.transitions["toOff"] = 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()

  4. 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.

  5. 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?

  6. just in case:

    from random import randint

    import time

    #creating a class with type

    ######################################################################

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

    class LightOn(State):

    def Execute(self):

    print('the light is on')

    class LightOff(State):

    def Execute(self):

    print('the light is off')

    #############################################

    ##transition###################

    class transition(object):

    def __init__(self,toState):

    self.toState = toState

    def Execute(self):

    print("Transition")

    ###############################################

    #################creating FSm###############################

    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.SetState(self.trans.toState)

    self.trans = None

    self.curState.Execute()

    #############################character class################

    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")

    light.FSM.transitions["toOff"] = transition("Off")

    light.FSM.SetState("On")

    for i in range(5):

    startTime = time.perf_counter()

    timeInterval = 1

    while startTime+timeInterval>time.perf_counter():

    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()

Comments are closed.