0

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
desertnaut
  • 57,590
  • 26
  • 140
  • 166
accismns
  • 37
  • 5

2 Answers2

0

The syntax for this is not so simple but once you understand it, it's very easy. You need to use the 2nd syntax shown in the NetLogo Dictionary for "foreach", the one that uses "->" to assign a temporary name to the list item you are processing. For you, use:

 foreach exits-list [ next-exit ->  ; Now "next-exit" is the next item on exit-list
    let distance-to-exit distance patch-here next-exit
    ...
    ]
Steve Railsback
  • 1,251
  • 6
  • 7
  • In netlogo, how do I copy the error message? Right-clicking does not work and does not even let me select the error message. – accismns May 19 '23 at 14:00
  • In NetLogo, a mouse-select over the Command Centre + mouse-RightClick menu with options to copy works ( see the answer below ) --- I.e. what O/S + what WindowsManager does your localhost operate on? – user3666197 May 19 '23 at 14:18
  • I use windows 11, i need to copy this error: https://imgur.com/a/voIQtJb – accismns May 19 '23 at 14:20
0

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

halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92