Loading ...
Sorry, an error occurred while loading the content.

Re: [netlogo-users] 2 questions and a suggestion

Expand Messages
  • Seth Tisue
    ... Nigel Two queries about how best to do things in NetLogo, and a Nigel suggestion: 1. I have one turtle per patch on all the patches Nigel of the
    Message 1 of 2 , Sep 25 9:40 AM
    • 0 Attachment
      >>>>> "Nigel" == Nigel Gilbert <n.gilbert@...> writes:

      Nigel> Two queries about how best to do things in NetLogo, and a
      Nigel> suggestion: 1. I have one turtle per patch on all the patches
      Nigel> of the NetLogo grid. I want to write a turtle procedure to
      Nigel> report a turtle chosen at random from the 8 turtles in the
      Nigel> local neighborhood. Is this, rather clumsy, code the fastest
      Nigel> and most elegant way to achieve this?

      Nigel> to-report neighbor-turtle ; turtle procedure. Report a turtle
      Nigel> ; chosen at random from my neighbours
      Nigel> locals [ neighbor-patch ]
      Nigel> set neighbor-patch random-one-of neighbors
      Nigel> report one-of turtles-at pxcor-of neighbor-patch pycor-of
      Nigel> neighbor-patch
      Nigel> end

      You might want "random-one-of" here, not "one-of". "value-from" is
      helpful for shortening this:

      to-report neighbor-turtle
      report random-one-of
      value-from (random-one-of neighbors)
      [turtles-here]
      end

      This won't always get you a turtle, though, since the randomly chosen
      patch might not have any turtles on it. If you want to make sure you
      always get a turtle if any patch has one, then you can do this:

      to-report neighbor-turtle
      report random-one-of
      value-from
      random-one-of (neighbors with [any turtles-here])
      [turtles-here]
      end

      (We're considering adding a "turtles-on" primitive to NetLogo so you
      could just write "random-one-of turtles-on neighbors". Note that
      definining "turtles-on" yourself can't be done either
      straightforwardly or efficiently, as far as I can see.)

      Nigel> 2. Other languages define a special type of identifier that
      Nigel> is recognisable as a constant and then the compiler enforces
      Nigel> that the constant's value is not changed. What is the best
      Nigel> way of doing this in NetLogo?

      Currently none -- using a global variable (or a slider) is the best
      you can do. StarLogoT had a special syntax for defining constants,
      but we haven't added it to NetLogo yet.

      Nigel> 3. A suggestion. If you use 'show' as a turtle command, the
      Nigel> value that follows is printed in the command center, preceded
      Nigel> by "(turtle XX): ", where XX is the turtle id. The trouble is
      Nigel> that the id can be 1, 2 or 3 characters long, depending on the
      Nigel> id value, and so successive lines from different turtles are
      Nigel> shifted to the left or right relative to each other. Could
      Nigel> what is printed be followed by sufficient spaces to ensure
      Nigel> that the outputs line up?

      If you need justified output, I'd suggest replacing "show" with an
      alternate version that does what you want:

      to my-show [thing] ;; turtle procedure
      print "(turtle " + (columns who 4) + "): " + thing
      end

      to-report columns [thing width]
      locals [spaces]
      set spaces ""
      set thing word thing "" ;; convert to string
      repeat (width - length thing)
      [ set spaces word spaces " " ]
      report word spaces thing
      end

      ==
      Seth Tisue / seth@... / (847) 467-2814
      lead developer, NetLogo http://ccl.northwestern.edu/netlogo/
      Center for Connected Learning and Computer-Based Modeling
      School of Education and Social Policy / Department of Computer Science
      Northwestern University
    Your message has been successfully submitted and would be delivered to recipients shortly.