State Machine Example

73 views
Skip to first unread message

James Steiner

unread,
Feb 20, 2022, 4:21:16 PM2/20/22
to netlogo-users
There was a semi-old question on StackOverflow that clearly needed a state machine, and I wrote an answer, but as I was about to post the answer, the author deleted the question. But here I have this nice answer. So I'm going to post it here.

***TL;DR: You can use a "STATE MACHINE" to control your turtle's behavior, so they do the right things, in the right order.***

In this code you replace HomePatch every time--but I think you only want to replace homepatch when the turtle begins the trip. Your sample doesn't show when TARGET is set... but that's probably when you should set HomePatch, if you don't set it when the turtle is created.

I think you mean for the turtle to remember where it started, pick a target, take as many steps as needed (over multiple runs of go) to get to the target, do something there, then return step by step to the homepatch--perhaps to pick a new target.

So, during GO, a turtle can be in one of 4 situations, or "states":

  1. TARGETING: picking a target
  2. SEEKING: moving towards the target
  3. ARRIVING: doing something to the target
  4. RETURNING: moving towards the homepatch

For each of these, there is a definite moment when the state ends, and a new state begins.

  1. TARGETING: After picking a target, the state always changes to "SEEKING"
  2. SEEKING: When the step ends on the target, the state changes to "ARRIVING"
  3. ARRIVING: After doing something to the target, the state always changes to  "RETURNING"
  4. RETURNING: When the step ends on the homepage, the state changes to "TARGETING"

When we can organize the behaviors of our agents like this, with distinct actions, and test that tell us when to change behaviors, we can use a "State Machine" to control the agents. A State Machine is just a model that explicitly defines these "states" and the transitions between them.  

Here's what that looks like:

globals
[ speed ;; this may be made a slider
]
turtles-own
[ state  
  target
  homepatch
  green-count
]

to setup
   clear-all
   set speed 1
   ;; make 10% of patches green
   let one-percent (10 / 100)
   ask n-of (count patches * one-percent) patches
   [ set pcolor green ]

   ;; make a few turtles
   create-turtles 10
   [ move-to patch (random-pxcor) (random-pycor)
     set color one-of [ red blue sky yellow orange pink ]
     set size 1
     set shape "dot"
     set state "TARGETING"
   ]
  reset-ticks
end

to do-state-TARGETING
  ;; pick a target, change state to SEEKING
  set homepatch patch-here
  set pcolor (color + 2)
  set plabel green-count
  set shape "dot"
  set target one-of patches with [ pcolor = green ]
  if ( is-patch? target )
  [ ;; if there's a green patch remaining, seek it
    set state "SEEKING"
  ]
end

to do-state-SEEKING
  ;; move one step towards target
  ;; upon reaching the target, change state to ARRIVING
  let d distance target
  if ( d > 0 ) [ face target ]
  if-else ( d < speed ) [ fd d ] [ fd speed ]
  ;; if now on target, state is "arriving"
  if ( patch-here = target ) [ set state "ARRIVING" ]
end

to do-state-ARRIVING
  ;; if still green, count target, make not green
  ;; forget the target
  ;; change state to "RETURNING"
  ifelse ( pcolor = green )
  [ ;; green here
    set green-count green-count + 1
    set pcolor black
    set shape "circle"
  ]
  [ ;; no green here
    set shape "dot"
  ]  
  ;; forget the target
  set target nobody
  ;; new state is RETURNING
  set state "RETURNING"
end

to do-state-RETURNING
  ;; step towards the homepatch
  ;; upon reaching the homepatch, change state to "TARGETING"
  let d distance homepatch
  ifelse ( d > 0 and patch-here != homepatch )
  [ ;; not at homepatch
    face homepatch
    ifelse ( d < speed ) [ fd d ] [ fd speed ]
  ]
  [ ;; reached homepatch
    set state "TARGETING"  
  ]
end

to run-state-machine
  ;; this chooses an action based on the current state.
  ( ifelse
    ( state = "TARGETING" ) [ do-state-TARGETING ]
    ( state = "SEEKING" )   [ do-state-SEEKING ]
    ( state = "ARRIVING" )  [ do-state-ARRIVING ]
    ( state = "RETURNING" ) [ do-state-RETURNING ]
  )
end

to go
  ;; turtles find and visit green patches, turn them white
  ;; if no green patches remain, and all turtles home, stop
  if (
       ( not any? patches with [ pcolor = green ] )
       and
       ( not any? turtles with [ patch-here != homepatch ] )
  )
  [ stop ]
 
  ;; turtles do their thing
  ask turtles [ run-state-machine ]
  tick
end

~~~JAMES
 

Vidushi Patel

unread,
Feb 20, 2022, 7:54:59 PM2/20/22
to James Steiner, netlogo-users
Thanks a lot for sharing this James! 

I was thinking to ask a question and you posted this answer 😊.

Kind regards 
Vidushi 

On 21 Feb 2022, at 5:21 am, James Steiner <grego...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/CADiHOG6XbSLv%3DSyEGkhSfNd88Y2NXraHjW2CF2fUdrPXpL5BRQ%40mail.gmail.com.

James Steiner

unread,
Feb 20, 2022, 8:19:17 PM2/20/22
to Vidushi Patel, netlogo-users
Im glad it may be of use. 👍🏽
Reply all
Reply to author
Forward
0 new messages