34
“Tasks” in NetLogo 5.0beta1 March 21, 2011

“Tasks” in NetLogo 5.0beta1

Embed Size (px)

Citation preview

Page 1: “Tasks” in NetLogo 5.0beta1

“Tasks” in NetLogo 5.0beta1

March 21, 2011

Page 2: “Tasks” in NetLogo 5.0beta1

What are tasks?

chunks of code to be run once,run repeatedly, passed around,

stored in variables, etc.

Page 3: “Tasks” in NetLogo 5.0beta1

Known elsewhere as

closures

first-class functions

lambda

Page 4: “Tasks” in NetLogo 5.0beta1

Tasks in NetLogo 5.0

2 new data types

3 new primitives

8 primitives altered

Page 5: “Tasks” in NetLogo 5.0beta1

2 new data types

Command tasks

Reporter tasks

Page 6: “Tasks” in NetLogo 5.0beta1

3 new primitives

task

is-command-task?

is-reporter-task?

Page 7: “Tasks” in NetLogo 5.0beta1

6 altered primitives

map, filter, reduce,

n-values, sort-by

foreach

Page 8: “Tasks” in NetLogo 5.0beta1

2 more altered primitives

run

runresult

Page 9: “Tasks” in NetLogo 5.0beta1

NOT altered (for now)

ifelse, while, repeat, loop, ...

Page 10: “Tasks” in NetLogo 5.0beta1

NOT altered (for now)

ask, of, with, all?, ...

Page 11: “Tasks” in NetLogo 5.0beta1

Backward compatible syntax

square brackets

inputs: ?1 (aka ?), ?2, ?3, ...

Page 12: “Tasks” in NetLogo 5.0beta1

Implicit task syntax

map [? * ?] [1 2 3] => [1 4 9]

filter [? mod 2 = 0] [1 2 3 4] => [2 4]

foreach sort turtles [ print ? ]

Page 13: “Tasks” in NetLogo 5.0beta1

Explicit task syntax

set next-task task [ fd 1 ]

set coloring task [ ifelse-value (wealth > 100) [ red ] [ blue ] ]

Page 14: “Tasks” in NetLogo 5.0beta1

Disambiguation of task type

command or reporter?look at first token after the opening

bracket (skipping left parens)

Page 15: “Tasks” in NetLogo 5.0beta1

No tasks “escape”

unless you use the explicit syntax

Page 16: “Tasks” in NetLogo 5.0beta1

Running tasks

run task [ print 5 ]

=> 5

print runresult task [5]

=> 5

Page 17: “Tasks” in NetLogo 5.0beta1

Running tasks (variadic)

let printer task [ print ?1 + ?2 ]

(run print 2 3)

=> 5

let square task [? * ?]

print (runresult square 5)

=> 25

Page 18: “Tasks” in NetLogo 5.0beta1

Concise task syntax

task die short for task [ die ]

task fd short for task [ fd ? ]

task + short for task [?1 + ?2]

Page 19: “Tasks” in NetLogo 5.0beta1

Concise + implicit syntax

map abs [1 -2 3 -4] => [1 2 3 4]

reduce + [1 2 3 4] => 10

filter is-number? [1 “x” 3] => [1 3]

Page 20: “Tasks” in NetLogo 5.0beta1

Extra inputs are ignored

(runresult task [? * 2] 1 10 100)=> 2

Page 21: “Tasks” in NetLogo 5.0beta1

Tasks are closures

close over procedure inputs

close over local variables

Page 22: “Tasks” in NetLogo 5.0beta1

Closing over procedure inputs

to-report printer [x]

report task [ print x ]

end

run printer 5

=> 5

Page 23: “Tasks” in NetLogo 5.0beta1

Closing over local variables

let x 5

let y task [ print x ]

run y

=> 5

Page 24: “Tasks” in NetLogo 5.0beta1

Close over bindings, not values

let x 5

let y task [x]

set x 10

print runresult y

=> 10

Page 25: “Tasks” in NetLogo 5.0beta1

Current agent not closed over

globals [g]

ask turtle 0 [ set g task [ print who ] ]

ask turtle 1 [ run g ]

=> 1

Page 26: “Tasks” in NetLogo 5.0beta1

Nonlocal exits

stop and report refer to dynamically enclosing procedure, not enclosing task

(backwards compatible)

Page 27: “Tasks” in NetLogo 5.0beta1

It’s fast

100x faster than compiling a string

Page 28: “Tasks” in NetLogo 5.0beta1

Models using explicit tasks

State Machine ExampleTermites 3D

(afraid that’s all at the moment)

Page 29: “Tasks” in NetLogo 5.0beta1

Use tasks, not strings!

but, still need strings with run/runresult for running code

entered by end user

Page 30: “Tasks” in NetLogo 5.0beta1

Possible future work:named inputs

[x => x * x]

(or something like that?not sure yet)

Page 31: “Tasks” in NetLogo 5.0beta1

Possible future work:complex reporter tasks

task [

let x ...

...

report ...

]

Page 32: “Tasks” in NetLogo 5.0beta1

Wouldn’t it be cool if?:automatic updating

set label task [energy]

set color task [ifelse-value (wealth < 100)

[red] [blue] ]

Page 33: “Tasks” in NetLogo 5.0beta1

Wouldn’t it be cool if?:scheduling

in-ticks 5 task [ fd 1 ]

Page 34: “Tasks” in NetLogo 5.0beta1

Questions?

http://groups.yahoo.com/group/netlogo-users