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

How to "run" a list

Expand Messages
  • Ken Kahn
    I m starting a project in which I ll be relying heavily upon the run primitive. Unlike most dialects of Logo, NetLogo s run only works on strings (and not
    Message 1 of 8 , Jun 1, 2006
    • 0 Attachment
      I'm starting a project in which I'll be relying heavily upon the "run" primitive. Unlike most dialects of Logo, NetLogo's run only works on strings (and not also lists). I know I can convert a list to a string using "word" but that is expensive and clumsy. And it must be expensive for NetLogo to parse the string rather than use a list.
       
      I then thought I'd be clever and define procedures on the fly instead of using run. (Again most versions of Logo permit this.) So I wrote this test program:
       
      to test
      run "to foo print 2 end"
      foo
      end
       
      and NetLogo complained that foo wasn't defined. So I added
       
      to foo
      print 1
      end
       
      and then when I tried running test I got the following error.
       
      So am I stuck using strings? (I think users should be able to define their own variants of built-in primitives such as "while" and that takes lists as arguments.)
       
      Best,
       
      -ken
       
      error while observer executing RUN in procedure TEST
      syntax error: This doesn't make sense here
      (halted execution of Command Center)
       
      org.nlogo.nvm.EngineException: syntax error: This doesn't make sense here
       at org.nlogo.prim._run.perform(_run.java:40)
       at org.nlogo.nvm.Context.step(Context.java:74)
       at org.nlogo.nvm.Job.step(Job.java:125)
       at org.nlogo.nvm.JobThread.runPrimaryJobs(JobThread.java:169)
       at org.nlogo.nvm.JobThread.run(JobThread.java:87)
       
      NetLogo 3.1
      main: org.nlogo.app.App
      thread: JobThread
      Java VM: 1.5.0_06 (Sun Microsystems Inc.; 1.5.0_06-b05)
      operating system: Windows XP 5.1 (x86 processor)
      JOGL: (3D View not initialized)
      OpenGL Graphics: (3D View not initialized)
      model: Untitled
       
      01:39:36.638 SwitchedTabsEvent (org.nlogo.app.Tabs) AWT-EventQueue-0
      01:39:36.638 RuntimeErrorEvent (org.nlogo.app.App$3) AWT-EventQueue-0
      01:39:36.638 AddJobEvent (org.nlogo.app.cc.CommandLine) AWT-EventQueue-0
      01:39:36.628 OutputEvent (org.nlogo.app.cc.CommandLine) AWT-EventQueue-0
      01:39:36.628 CompiledEvent (org.nlogo.window.CompilerManager) AWT-EventQueue-0
      01:39:36.628 CompileMoreSourceEvent (org.nlogo.app.cc.CommandLine) AWT-EventQueue-0
      01:39:36.437 PeriodicUpdateEvent (org.nlogo.app.App$3) AWT-EventQueue-0
      01:39:36.237 PeriodicUpdateEvent (org.nlogo.app.App$3) AWT-EventQueue-0
      01:39:36.037 PeriodicUpdateEvent (org.nlogo.app.App$3) AWT-EventQueue-0
      01:39:35.836 PeriodicUpdateEvent (org.nlogo.app.App$3) AWT-EventQueue-0
    • Seth Tisue
      ... Ken Unlike most dialects of Logo, NetLogo s run only Ken works on strings (and not also lists). Ken I then thought I d be clever and define procedures
      Message 2 of 8 , Jun 1, 2006
      • 0 Attachment
        >>>>> "Ken" == Ken Kahn <kenkahn@...> writes:

        Ken> Unlike most dialects of Logo, NetLogo's run only
        Ken> works on strings (and not also lists).

        Ken> I then thought I'd be clever and define procedures on the
        Ken> fly [...]

        Sorry, NetLogo's "run" is pretty restricted compared to "run" in
        traditional Logo and is likely to remain that way in the foreseeable
        future. It only works with strings and you can't use it to define or
        redefine procedures. (I'll add a mention of the latter fact to the
        entry in the primitives dictionary.) We have no plans to lift either of
        these restrictions.

        Ken> (I think users should be able to define their own variants of
        Ken> built-in primitives such as "while" and that takes lists as
        Ken> arguments.)

        Sorry, we don't support that, and have no plans to.

        --
        Seth Tisue / seth@...
        lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/
        photos: http://www.flickr.com/photos/tisue/
      • Jim Lyons
        ... Not sure what you are trying to accomplish. If each element of the list is a command, you can use foreach to run them. I created a few turtles in a new
        Message 3 of 8 , Jun 1, 2006
        • 0 Attachment
          Ken Kahn wrote:

          > I'm starting a project in which I'll be relying heavily upon the "run"
          > primitive. Unlike most dialects of Logo, NetLogo's run only works on
          > strings (and not also lists). I know I can convert a list to a string
          > using "word" but that is expensive and clumsy. And it must be
          > expensive for NetLogo to parse the string rather than use a list.

          Not sure what you are trying to accomplish. If each element of the list
          is a command, you can use foreach to run them. I created a few turtles
          in a new model and put this in the Procedures tab:

          to test ;; turtles
          let cmd-list ["rt 15" "fd 3" "lt 15" "fd 3"]
          foreach cmd-list [run ?]
          end

          In the Command Center, type "test" to the turtles. With a few turtles,
          it's instantaneous. With 100 turtles it takes less than a second. If
          the elements of the list are just command names, it should be pretty
          fast to "run" them. Hope this helps.

          Jim Lyons
        • Ken Kahn
          Thanks for your reply. I want to implement a scheduling primitive so that I can say things like do-at-time 5 [rt 15 fd 3 lt 15 fd 3] much like if/while/etc and
          Message 4 of 8 , Jun 2, 2006
          • 0 Attachment
            Thanks for your reply. I want to implement a scheduling primitive so
            that I can say things like

            do-at-time 5 [rt 15 fd 3 lt 15 fd 3]

            much like if/while/etc and I much rather not be forced to say

            ["rt 15" "fd 3" "lt 15" "fd 3"]

            (by the way quoting doesn't work in general, consider
            do-at-time 5 [rt 15 if (x > 0) [fd 3 lt 15 fd 3]]

            But Seth explained that this was a design choice and there are no
            plans to make RUN more flexible or to allow procedures to be created
            on the fly. Oh well.

            Best,

            -ken

            --- In netlogo-users@yahoogroups.com, Jim Lyons <teacherjim42@...>
            wrote:
            >
            > Ken Kahn wrote:
            >
            > > I'm starting a project in which I'll be relying heavily upon
            the "run"
            > > primitive. Unlike most dialects of Logo, NetLogo's run only
            works on
            > > strings (and not also lists). I know I can convert a list to a
            string
            > > using "word" but that is expensive and clumsy. And it must be
            > > expensive for NetLogo to parse the string rather than use a list.
            >
            > Not sure what you are trying to accomplish. If each element of the
            list
            > is a command, you can use foreach to run them. I created a few
            turtles
            > in a new model and put this in the Procedures tab:
            >
            > to test ;; turtles
            > let cmd-list ["rt 15" "fd 3" "lt 15" "fd 3"]
            > foreach cmd-list [run ?]
            > end
            >
            > In the Command Center, type "test" to the turtles. With a few
            turtles,
            > it's instantaneous. With 100 turtles it takes less than a second.
            If
            > the elements of the list are just command names, it should be
            pretty
            > fast to "run" them. Hope this helps.
            >
            > Jim Lyons
            >
          • Jim Lyons
            ... Well, how about [ rt 15 fd 3 lt 15 fd 3 ]? This way, run is only called once. ... Depends on what you make into the string. This works: run if xcor 0
            Message 5 of 8 , Jun 2, 2006
            • 0 Attachment
              Ken Kahn wrote:

              > Thanks for your reply. I want to implement a scheduling primitive so
              > that I can say things like
              >
              > do-at-time 5 [rt 15 fd 3 lt 15 fd 3]
              >
              > much like if/while/etc and I much rather not be forced to say
              >
              > ["rt 15" "fd 3" "lt 15" "fd 3"]

              Well, how about ["rt 15 fd 3 lt 15 fd 3"]? This way, run is only
              called once.
              >
              > (by the way quoting doesn't work in general, consider
              > do-at-time 5 [rt 15 if (x > 0) [fd 3 lt 15 fd 3]]

              Depends on what you make into the string. This works:

              run "if xcor > 0 [fd 3 lt 15 fd 3]"

              > But Seth explained that this was a design choice and there are no
              > plans to make RUN more flexible or to allow procedures to be created
              > on the fly. Oh well.

              But you can create strings and lists on the fly and execute them. I
              experimented a while back with "plants" which had various snippets of
              code in variables that could be recombined in lists and executed. I had
              only dozens of turtles, not hundreds or thousands, so it worked
              perfectly well.

              I don't mean to be argumentative, just wanted to encourage you not to
              give up on NetLogo without trying a few other ideas. I haven't used
              other Logos, so the NetLogo way has come to seem natural to me. (I
              would really like to see the addition of associative arrays though.)
              Best of luck to you in your project.

              Jim Lyons
            • Seth Tisue
              ... Ken Thanks for your reply. I want to implement a scheduling primitive Ken so that I can say things like Ken do-at-time 5 [rt 15 fd 3 lt 15 fd 3] Ken
              Message 6 of 8 , Jun 2, 2006
              • 0 Attachment
                >>>>> "Ken" == Ken Kahn <kenkahn@...> writes:

                Ken> Thanks for your reply. I want to implement a scheduling primitive
                Ken> so that I can say things like

                Ken> do-at-time 5 [rt 15 fd 3 lt 15 fd 3]

                Ken> much like if/while/etc and I much rather not be forced to say

                Ken> ["rt 15" "fd 3" "lt 15" "fd 3"]

                RUN accepts multiple commands in a single string, so you can do:

                run "rt 15 fd 3 lt 15 fd 3"

                Ken> (by the way quoting doesn't work in general, consider do-at-time 5
                Ken> [rt 15 if (x > 0) [fd 3 lt 15 fd 3]]

                Unless I misunderstand what you're trying to do, I think this should work
                just fine with our RUN:

                run "rt 15 if (x > 0) [fd 3 lt 15 fd 3]"

                Here x could be a global variable, or a turtle variable, or even a local
                variable. Here's an example of RUN working with a local variable:

                observer> cct 1 [ let x 5 run "show x" ]
                (turtle 0): 5

                --
                Seth Tisue / seth@...
                lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/
                photos: http://www.flickr.com/photos/tisue/
              • James Steiner
                ... And if that isn t exciting enough, you can even declare local variables inside the run string: cct 1 [ run let n count neighbors with [ blue ] jump n ]
                Message 7 of 8 , Jun 5, 2006
                • 0 Attachment
                  Seth wrote:

                  > Here's an example of RUN working with a local variable:
                  >
                  > observer> cct 1 [ let x 5 run "show x" ]
                  > (turtle 0): 5


                  And if that isn't exciting enough, you can even declare local
                  variables inside the run string:

                  cct 1 [ run "let n count neighbors with [ blue ] jump n" ]

                  ~~James
                  _____________________________________
                  turtlezero.com -- its turtles, all the way down!
                • James Steiner
                  ... Converting a list to a string *is* expensive and slow, and the foreach technique discussed earlier would be better, but just for the sake of completeness
                  Message 8 of 8 , Jun 5, 2006
                  • 0 Attachment
                    On 6/1/06, Ken Kahn <kenkahn@...> wrote:
                    >
                    > I'm starting a project in which I'll be relying heavily upon the "run" primitive.
                    > Unlike most dialects of Logo, NetLogo's run only works on strings
                    > (and not also lists). I know I can convert a list to a string using "word" but
                    > that is expensive and clumsy.
                    >

                    Converting a list to a string *is* expensive and slow, and the foreach
                    technique discussed earlier would be better, but just for the sake of
                    completeness in the archives, here is an example:

                    let command-list [ "rt 4" "jump 5" "set pcolor blue" ]
                    let command-string reduce [ (word ?1 " " ?2) ] command-list

                    Also, depending on the nature of the commands your agents will
                    execute, you may not even want to use actual code in your command
                    list, but rather, code words or numbers or whatever, of your own, that
                    are interpreted elsewhere. Or, your command strings may instead refer
                    to procedures that define the actions. Since the procedures are
                    pre-interpreted, this would reduce the time needed to interpret the
                    command strings, and depending on the complexity of the individual
                    commands, may provide an increase in overal execution speed. *May*.
                    I'd test it to be sure.

                    ;; Example:

                    to rt-5 rt 5 end ;; now only rt-5 needs to be parsed at run-time, not
                    "rt" and "5"
                    to jump-1 jump 1 end ;; now only jump-1 needs to be parsed at run-time

                    ;; build a list of random commands
                    to run-commands
                    let command-list []
                    ;; generate random command sequence of 15 commands
                    let command-choices [ "rt-5" "jump-1" ]
                    repeat 15
                    [ let a-command one-of command-choices
                    set command-list lput a-command command-list
                    ]
                    ;; execute the command-list
                    foreach command-list [ run ? ]
                    end

                    ~~James
                    _____________________________________
                    turtlezero.com -- its turtles, all the way down!
                  Your message has been successfully submitted and would be delivered to recipients shortly.