Search code examples
simulationnetlogocellular-automata

Error: Expected a Literal Value When Accessing Patch Coordinates in a List


I'm writing a NetLogo code to simulate the movement of passengers in an room.

I have defined a list of exits exits-list as a list of patches, and I'm trying to access the x and y coordinates of each patch in exits-list to calculate the distance between the passenger's current position and each exit. However, when I try to access the x and y coordinates of each patch using the syntax [patch-xcor] of patch and [patch-ycor] of patch, I get an error that says :

expected a literal value
in "let exit-patch [patch-xcor item 0 ?, patch-ycor item 1 ?]"

How can I solve this problem and correctly access the x and y coordinates of each patch in exits-list?

to initialize-exits
   set exit1 patch 64 -13
   set exit2 patch 13 -54
   set exits-list (list exit1 exit2)
end

to initialize-passengers
   create-passengers passenger-count [
          set shape          "person business"
          set size            2
          set color           yellow
          set in-seat?        true
          set safe?           false
          set dead?           false
          set panic?          false
          set current-heading 0
          set target-exit     nobody
          set my-exits-list   []

          choose-exit patch-here       ; Add this line to set the initial target-exit
  ]
  ask passengers [
      let target one-of patches with [accessible?]
      face target
      move-to target
  ]
end

to choose-exit [current-position]
   let possible-exits []
   let shortest-distance 10000
   let nearest-exit nobody
   foreach exits-list [
       let exit-patch [patch-xcor item 0 ?, patch-ycor item 1 ?]
       let distance distance current-position exit-patch
       if  distance < shortest-distance [
           set shortest-distance distance
           set nearest-exit      exit-patch
           set possible-exits    []
       ]
       if  distance = shortest-distance [
           set possible-exits lput exit-patch possible-exits
       ]
   ]
   if possible-exits is-empty [
      print "No exits found."
      return
   ]
   let lucky-exit    one-of possible-exits
   set target-exit   lucky-exit
   set my-exits-list lput lucky-exit my-exits-list
   lucky-exit
end

Solution

  • Q : "How can I solve this problem and correctly access the x and y coordinates of each patch in exits-list?"

    Patches own (have) properties names pxcor and pycor

    Your code shall also never overshadow a programming language "reserved" words
    ( a distance being one of such case ) by using them as user-defined variable or property names.

    like here :
    let distance distance current-position exit-patch

    Error messages are mouse-click&drag select-able plus on mouse-RightClick one can copy the selected text :

    like this :
    observer> let exit2 patch 13 -54
    ERROR: Nothing named EXIT1 has been defined.

    enter image description here

    Syntax highlighting and [ Check ]-outputs are not GUI-friendly inside the IDE, yet they serve as an immediate interactive tool to get the errors removed by repair.

    enter image description here

    Bonus

    This mind-map of most & less often syntax-constructors might help:

    enter image description here