Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetLogo: the meaning of TO-REPORT explained for dummies?

I have a problem to understand the role of to-report and report in NetLogo, even it seems pretty useful and I can't really find a help written in "human style" language.

In NetLogo dictionnary http://ccl.northwestern.edu/netlogo/docs/dictionary.html#report I can find definitions for to-report :

to-report procedure-name
to-report procedure-name [input1 ...]
Used to begin a reporter procedure.

The body of the procedure should use report to report a value for the procedure. See report. 

and for report:

report value
Immediately exits from the current to-report procedure and reports value as the result of that procedure. report and to-report are always used in conjunction with each other. See to-report for a discussion of how to use them.

So, it seems to-report and report calculate some value and report it.

Thus, when I try add

to-report average [a b c]
  report (a + b + c) / 2
end

to my code, and then use the average variable somewhere in my code p.e.:

to go 
  ...
  print average 
  tick
end

I've got an error: AVERAGE expected 3 inputs. When I try to create my variables [a b c] in globals [a b c] I've got an error There is already a global variable called A. If I define my variables [a b c] within to-report procedure:

to-report average [a b c]
    set a 1
  set b 2
  set c 3
  report (a + b + c) / 2
end

My error is again AVERAGE expected 3 inputs.

Thus, how can I simply test the usefulness of to-report procedure? And where to place it correctly in my code to see what it is really doing? From Urban Suite - Economic Disparity (http://ccl.northwestern.edu/netlogo/models/UrbanSuite-EconomicDisparity) I see that to-report is used to calculate values related to each patch:

to-report patch-utility-for-poor
    report ( ( 1 / (sddist / 100 + 0.1) ) ^ ( 1 - poor-price-priority ) ) * ( ( 1 / price ) ^ ( 1 + poor-price-priority ) )
end

however this reported value is not directly defined as patch variable which increase my confusion...

enter image description here

Thank you !

like image 745
maycca Avatar asked May 03 '16 19:05

maycca


People also ask

What does to report do in NetLogo?

A reporter is a predefined procedure that returns a value and, unlike a command, cannot be a stand-alone code element. NetLogo has some useful built-in reporters that cannot be changed directly such as ticks , pi , e , world-width , and world-height .

What code does NetLogo use?

What programming language was NetLogo written in? NetLogo is written mostly in Scala, with some parts in Java. (Scala code compiles to Java byte code and is fully interoperable with Java and other JVM languages.)

What are variables in NetLogo?

Variables. Agent variables are places to store values (such as numbers) in an agent. An agent variable can be a global variable, a turtle variable, a patch variable, or a link variable. If a variable is a global variable, there is only one value for the variable, and every agent can access it.

What are globals NetLogo?

globals is a primitive that we use to define custom global variables in NetLogo. A global variable is a variable that has the same value for all the agents in the model across all procedures. You can define your custom global variables by writing globals followed by brackets [] .


1 Answers

A function can take some input (usually one or more variables or values) and return some output (usually a single value). You can specify that a function returns a value using to-report in your function header and report returns the actual value.

Your error is due to the fact that you never passed in arguments to your average function

to go 
  ...
  print average 
  tick
end

should be

to go 
  ...
  print average 5 2 3 ;;a = 5, b = 2, c =3
  tick
end

Inside your average function, you should not reassign values of a,b,and c.

You should use report whenever you want to return a result from a function.

like image 199
mattsap Avatar answered Sep 21 '22 13:09

mattsap