Supply argument to reporter

dafeng2006 注册会员
2023-01-26 00:47

The NetLogo Way

[ ] of

Note: I use "agents" below, since this answer applies to any NetLogo agent, be it turtles, turtle breeds, patches, or links.

Rather than passing an agent variable (or expression) to a procedure and then somehow make the agents make a list and then act on the list, NetLogo makes it easy to first make the list from an expression evaluated individually by every agent in an agentset, so you can pass that list to some reporter. The syntax (as you know) is:

;; return list of 
;; calculated by each member of agentset
[ expression ] of agentset

So, if this is your reporter:

to-report gini [ samples ] 
  ;; samples will be sorted here, don't pre-sort!
  ;; best guess -- please correct if wrong
  ;; source:
  ;; https://en.wikipedia.org/wiki/Gini_coefficient#Calculation
  let n length samples 
  let indexes (range 1 (n + 1))
  let s1 2 * sum (map  [[i y] -> i * y] indexes sort samples)
  let s2 n * sum samples
  let G (s1 / s2) - ((n + 1) / n) 
  report G 

Then you might calculate the gini for your various measures like this:

print gini [ measure-1 ] of turtles
print gini [ measure-2 ] of turtles
print gini [ measure-3 ] of turtles

Which makes sense, since gini is a function that takes a set of samples. And this lets you easily use other sub-populations or breeds or whatever.

"Abbreviated" Syntax

I am about to lead you "down the garden path," so if you like what you've seen so far, you can stop here. If you want a little chuckle, read on.

Perhaps the above is just too verbose for you, and you really, really, want to get rid of "of turtles?" (even though you might later want to apply the generic function even more generically to sub-populations?)

Well, you could. We could write it using an "anonymous reporter":

print gini [-> houses ]

And then gini might look like this:

to-report gini [ sample-expression ]
  let samples sort [ run-result sample-expression ] of turtles
  ;;; ... the rest of your gini function here
  report G

OK, but you've embedded "of turtles" in your function. But what if later you want to evaluate this function for some sub-population or perhaps use patches instead of turtles?

You might fix that by taking the agentset as an input, as well.

to-report gini [ anon-reporter source ]
  let samples [ run-result anon-reporter ] of source
  ;; etc

and then write:

print gini [-> measure-1 ] turtles
print gini [-> measure-2 ] turtles
print gini [-> measure-3 ] turtles

Ugh, a bit awkward. Let's add a little syntax candy:

to-report of_ [ agents ] report agents end

NOW we can write:

print gini [-> measure-1 ] of_ turtles
print gini [-> measure-2 ] of_ turtles
print gini [-> measure-3 ] of_ turtles

Oh, Dear. That looks rather familiar, doesn't it? But rather more verbose than when we started.

So now we are back to:

print gini [ measure-1 ] of turtles
dls081x 注册会员
2023-01-26 00:47

You can use runresult, provided you are happy to pass the procedure's argument as a string:

to-report means [varname]
  report mean [runresult varname] of turtles

Trying it in the Command Center:

observer> setup
observer> repeat 15 [go]
observer> show means "houses"
observer: 7.4
ofhzxzg 注册会员
2023-01-26 00:47

As the error states, you are trying to pass a turtle-only variable (houses) to a procedure that is being used in observer context which doesn't work because Netlogo will try passing the variable to the procedure before actually running the procedure.

Instead, you could use the following code. With this example, you first use the "of"-primitive to extract the houses/cars variables as a list. Only then do you pass them to the "means"-procedure, which is being run by the observer.

to-report means [ param ]
  report mean param 
show means [houses] of turtles
show means [cars] of turtles

About the Author

Question Info

Publish Time
2023-01-26 00:47
Update Time
2023-01-26 00:47