TA: Discuss NetLogo Programming

289 views
Skip to first unread message

Alan Isaac

unread,
May 13, 2014, 10:49:35 AM5/13/14
to au-ec...@googlegroups.com
This course will have a TA, Dongping Xie. Please use this thread to discuss NetLogo programming with Dongping. This can include questions about the assignments. Naturally Dongping cannot simply provide answers for the assignments, but he can provide hints and guidance.

I will not follow this thread; it is not part of the graded discussions.  Additionally, you should feel free to take "off list" any discussions that you believe are of interest only to you and not to the rest of the class.  However, please realize that most questions about NetLogo programming will interest other members of the class.

Thank you,
Alan Isaac

Dongping Xie

unread,
May 13, 2014, 12:00:40 PM5/13/14
to au-ec...@googlegroups.com
Hi Class,

My name is Dongping Xie. I will be your TA for this exciting summer course.

First, let me introduce myself a bit. I am currently a 5th year PhD student in Econ. My particular interests lie in monetary policy transmission and banking crisis. I am also interested in research fields like computational finance, game theory and cyber security. 

Second, I am happy to help you with any problems that you encounter during the study. I guess most of you will spend a lot of time in familiarizing yourself with NetLogo in the first week or so. I will be right here to assist you to get hands on it. Please feel free to ask questions. If you would like to use another software for your course project, I am also willing to help you to my best. The softwares that I am familiar with include Python, Matlab, Mathematica, NetLogo.

Enjoy the course and the summer,
Dongping

Alan G Isaac

unread,
May 13, 2014, 12:10:13 PM5/13/14
to AU-ECON-ABS
I should mention that in addition to the TA for this course (Dongping Xie),
NetLogo support can also be sought from Eric Charles of the CTRL:
http://www.american.edu/ctrl/
For those who are near campus, I believe NetLogo is installed on
all the lab computers. (Please let me know immediately of any
problems.)

Alan Isaac

Dongping Xie

unread,
May 16, 2014, 1:43:06 PM5/16/14
to au-ec...@googlegroups.com
Hi Class,

This is just a reminder that if you need any help with NetLogo. Feel free to ask questions here.

Thanks,
Dongping

Dongping Xie

unread,
May 18, 2014, 9:00:48 PM5/18/14
to au-ec...@googlegroups.com
Hi Class,

This is an announcement that I will be offline for the next 48 hours or so. Please do not hesitate to ask questions in this thread. I will catch up with you as soon as I am back to computer. 

Thanks,
Dongping

Sebastien Lundby-Thomas

unread,
May 20, 2014, 5:29:55 PM5/20/14
to au-ec...@googlegroups.com
Im having trouble setting the globals for the coinflip part 1 for some reason, I'm going back over the guide to see what I am missing, but some guidance would be helpful.

Alan Isaac

unread,
May 20, 2014, 9:05:15 PM5/20/14
to au-ec...@googlegroups.com
Global Variables

When you ask a programming question, you need to provide enough information for the reader to understand what you are doing.  I cannot tell from your question what you are doing, so I have to guess.  This means I will probably not address your problem.

Recall from my first lecture on NetLogo
that you must declare global variables at the top of the Code tab, using the globals command.
After that, they are accessible everywhere in your program.

Keep in mind that you must write code in the Code tab inside procedures and reporters.

hth,
Alan Isaac

Kevin Carrig

unread,
May 20, 2014, 9:55:57 PM5/20/14
to au-ec...@googlegroups.com
I am working through Tutorial Three before attempting the coin-flip simulation. Could you provide some clarification on the ordering of syntax in NetLogo? For example, take the following code snippet:
                               
to go
  move-turtles
  tick
end

to move-turtles
  ask turtles [
    right random 360
    forward 10
  ]
end
                            

In the "to go" call we initial the command "move-turtles", but this command is not defined until the following section. Could you discuss how NetLogo compiles the code tab and applies some order of operations in executing each procedure?

Alan Isaac

unread,
May 21, 2014, 12:49:40 AM5/21/14
to au-ec...@googlegroups.com
Delayed Execution

This is a facility we want in a programming language.  We need to be able to define functions that call other functions (or even themselves).  While it may seem that go calls move-turtles before move-turtles is defined, it does not.  The reason is that execution of the procedure body is delayed until the procedure is called.   So all you have is two procedure definitions, and (so far) neither procedure is called.  Of course you can change to the Command Center and call go, but then both definitions will be available.

hth,
Alan Isaac

Dongping Xie

unread,
May 21, 2014, 8:38:01 PM5/21/14
to au-ec...@googlegroups.com
Hi Class,

I am back to my computer. Thank Professor Isaac for answering questions when I was away. Since he has given detailed explanations to a couple questions, I am not going to repeat them. Again, please feel free to ask questions about your NetLogo study, homework, etc. in this thread.

Thanks,
Dongping

Kevin Carrig

unread,
May 21, 2014, 11:20:26 PM5/21/14
to au-ec...@googlegroups.com
This is a quick question in regards to the coin-flip simulation. Is there a best practice to debug a "expected keyword" error in the Code Tab? My code is as follows:

globals [ n-heads n-tails x ]

to setup
  clear-all
end

let n-heads 0
let n-tails 0
let x 0

set x (random-float 1)

to go
  [ifelse x <0.5 
    [set n-heads n-heads+1]  
    [set n-tails n-tails+1] ]

Dongping Xie

unread,
May 22, 2014, 3:34:30 AM5/22/14
to au-ec...@googlegroups.com
Hi Kevin,

When you click "check" at the Code tab, the line highlighted points you to the location of the bug. 

You code has a few problems:
(1) let is only used to declare a local variable. 
(2) When you declare a global variable, its default initial value is 0. When you call clear-all in the setup all global variables will be reset to initial values. If you would like to initialize a variable with a value different than 0, you can use set in the setup, after clear-all
(3) You missed a few white spaces.
(4) The random number x should be generated each time you call go. Otherwise, the value of x is always fixed at the initial random number and the simulation becomes deterministic. 
(5) The code block should start after ifelse statement rather than before.
(6) You missed an end in the end.

I modified your code based on (1) - (6) as following:

globals [ n-heads n-tails x ]

to setup
  clear-all
  set n-heads 0 ;; This line is not necessary
  set n-tails 0 ;; This line is not necessary
end

to go
  set x random-float 1
  ifelse x < 0.5 
    [set n-heads n-heads + 1]  
    [set n-tails n-tails + 1] 
end

Please let me know if I answered your question.

Dongping

Kevin Carrig

unread,
May 23, 2014, 8:27:39 AM5/23/14
to au-ec...@googlegroups.com
This is all very helpful-thanks!

Natalie Chambers

unread,
May 24, 2014, 9:54:32 PM5/24/14
to au-ec...@googlegroups.com

I am working on the Coin Toss and I am wondering what the idea behind asking patch to flip the coin verses asking the observer? When is it better to ask different agents to perform the task over the observer? 

Amanda Saville

unread,
May 24, 2014, 10:31:39 PM5/24/14
to au-ec...@googlegroups.com
With regards to the coin-toss exercise, I ran into a few challenges during the exercise:

Firstly, when adding sliders in Part II, I wasn't really sure how to add n-flips and n-trials to my go procedure.  Instead I added new procedures that seemed to work fine--but they were not under go procedure.

Secondly, I'm having trouble implementing #6 on Part III.  As in tutorial #3, I asked patches to count patches with pcolor green, but Netlogo is giving me an error with "count" highlighted (error: expected command).  Does this have to do with the fact that I am asking patches to count green patches rather than the observer?

Lastly, I'm not sure if I'm just tired from a long week, but I am just not understanding what is meant by "append that number to the n-heads list."

My code (I haven't yet added the last slider) is posted below for review.  Any advice or direction regarding these questions would be appreciated!

patches-own
[
  n-heads
  n-tails
]

to setup
  ca
end

to go
  ask patches [flip-coin]
  ask patches [count patches with [pcolor = green]]
end

to flip-coin
  ifelse (random-float 1 < 0.5)
  [set pcolor green]
  [set pcolor red]
end

to go100
   repeat 100 [go]
end

to OneTrial
  repeat n-flips [go]
end

to Ntrials
  repeat n-trials [go]
end

Dongping Xie

unread,
May 26, 2014, 5:21:54 AM5/26/14
to au-ec...@googlegroups.com
When you want to perform the task in the observer, you should define the variable as a global variable, that is, you should include it in globals. Global variables belong to the observer. There is only one value for each global variable and it can be accessed, read and set by all agents in the environment. 

You can also define a patch variable, i.e. only belongs to that patch, in patch-own. In this case, the variable is exclusive for agents who are standing inside the patch. 

It is not obvious in the coin-flip simulation the difference between the two. But you will know better as you move on. Just keep in mind that you would like to restrict the accessibility of the variable to a specific patch, turtle or link, use patch-own, turtle-own, link-own respectively. If this variable applies to every agent in the your artificial world, use globals.

Does this make sense?

Dongping Xie

unread,
May 26, 2014, 5:57:44 AM5/26/14
to au-ec...@googlegroups.com
Hi Amanda,

Please see the inline below ...


On Sunday, May 25, 2014 10:31:39 AM UTC+8, Amanda Saville wrote:
With regards to the coin-toss exercise, I ran into a few challenges during the exercise:

Firstly, when adding sliders in Part II, I wasn't really sure how to add n-flips and n-trials to my go procedure.  Instead I added new procedures that seemed to work fine--but they were not under go procedure.


Part II requires you flip the coin n-flips times in the go procedure. You can do it like this:

to go
  ask patch 0 0 [repeat n-flips [flip-coin]]
end

In part II, you only ask the specific patch 0 0 to flip the coin. You use the loop with repeat. 

n-trials asks you do run go multiple times, so it is not included in go. You should have another procedure that repeat go n-trials times.
 
Secondly, I'm having trouble implementing #6 on Part III.  As in tutorial #3, I asked patches to count patches with pcolor green, but Netlogo is giving me an error with "count" highlighted (error: expected command).  Does this have to do with the fact that I am asking patches to count green patches rather than the observer?


You are correct. Delete "ask patches". You should also use show in front in order to display the number. 

to go
  ask patches [flip-coin]
  show count patches with [pcolor = green]
end

 
Lastly, I'm not sure if I'm just tired from a long week, but I am just not understanding what is meant by "append that number to the n-heads list."


You counted the number of patches with green color, which represents the number of heads, right? You should create a list and append the number to the list. 
You do this again and again. Then you will have a sequence of length n-trials of numbers. Each one is the number of green patches in that trial. 

Hope this helps. Please let me know if anything is unclear.

Alan Isaac

unread,
May 27, 2014, 8:24:20 AM5/27/14
to au-ec...@googlegroups.com
There are a variety of ways to include tests in your programs to verify your code.  One useful way is to use print and show to inspect the outcomes being produced by your code.  Often a more useful way is to test variables to ensure they have the expected values.  In NetLogo, you can use error to do this:
http://ccl.northwestern.edu/netlogo/5.0/docs/dictionary.html#error

Here is an oversimplified example of the use of error:

let x 50 if (x != 51) [error "oops"]

If you Run that in the Command Center, what happens?

You should use error a lot in your code to test that it is producing the values your expect.

Alan Isaac

unread,
May 27, 2014, 9:52:55 PM5/27/14
to au-ec...@googlegroups.com
Coin Flipping

The homework page now includes an "answer sketch" for Part I and Part II of the Coin Flipping exercise.  Ordinarily, this exercise receives much more discussion, and I hope the posted code stimulates additional questions.

Amanda Saville

unread,
May 28, 2014, 1:10:40 AM5/28/14
to au-ec...@googlegroups.com
Hi Dongping,

Thank you so much, this was extremely helpful!

Alan Isaac

unread,
May 29, 2014, 12:53:25 PM5/29/14
to au-ec...@googlegroups.com
Remember, you can discuss any aspect of NetLogo programming in this (TA supported!) thread.

Additionally, Eric Charles will be available in the Hurst Lab tomorrow morning (Friday) at 9:30am to provide NetLogo support. Contact him (http://www.american.edu/profiles/staff/echarles.cfm) if you would like to attend. 

The TA (Dongping) and CTRL (Eric Charles) can also arrange Skype or Collaborate sessions for those who are not in town but would prefer not to use email.

Amanda Saville

unread,
May 29, 2014, 9:10:40 PM5/29/14
to au-ec...@googlegroups.com
I think this may be a seemingly random question, but using the question mark in a command (for example: show n-values 5 [?]) just prompts the program to produce random integers, correct?

Dongping Xie

unread,
May 29, 2014, 9:36:04 PM5/29/14
to au-ec...@googlegroups.com
show n-values 5 [?] should produce [0 1 2 3 4]. It basically says show the first 5 numbers in order. Remember that NetLogo starts with 0.
The question mark usually refers to the inputs to the task. 

Alan Isaac

unread,
May 30, 2014, 10:47:07 AM5/30/14
to au-ec...@googlegroups.com
n-values

I recognize that it can be initially puzzling to use n-values.
http://ccl.northwestern.edu/netlogo/docs/dictionary.html#n-values

The command n-values 5 [?] reports a list of 5 successive values, starting at 0.  That is, [0 1 2 3 4].

Note that n-values is a command that takes two arguments: a size (here 5), and a reporter task (here [?]). The question mark will take on successive values starting at 0, and these successive values determine the successive values of the reporter task, which constitute the reported list.

So for example, to produce a list of the squares of 0 through 4 we can use  n-values 5 [? * ?]. (Try it!)

Alan Isaac

unread,
May 30, 2014, 10:59:25 AM5/30/14
to au-ec...@googlegroups.com
Lists

Elaborating a bit on my last post...


Introduction to Lists
---------------------

- ordered collection of objects
- zero-based indexing: ``print item 0 mylist``
- immutable: you cannot change a list,
  but you can use a list as the basis of a new list

  ``remove-item 0 mylist`` is the same as ``but-first mylist``:
  it reports a *new list* that copies all items of ``mylist`` except the first.
  It does *not* change ``mylist``!


Creating Lists
---------------------

list constants between brackets:
   ``let mylist [0 1 2 3]``
use ``list`` to make a list from variables:
   ``let a 0 let b 1 let mylist (list a b)``
use the ``sentence`` primitive to copy, append to or concatenate:
   - copy: ``(sentence [0 1 2])``

   - append: ``(sentence [0 1] 2 3)``

   - concatenate: ``sentence [0 1] [2 3]``


n-values
--------

Newcomers to NetLogo often find it puzzling to use ``n-values``.
Read the documentation carefully.
http://ccl.northwestern.edu/netlogo/docs/dictionary.html#n-values

Consider the command ``n-values 5 [?]``.
This reports ``[0 1 2 3 4]``:

a list of 5 successive values, starting at 0.

Note that ``n-values`` takes two arguments:
an integer size (here ``5``),

and a “reporter task” (here ``[?]``).

The question mark in the reporter task is a special NetLogo variable
that cannot be set directly by the user.
The values taken by this special variable are determined by the command.
When we use the ``n-values`` command,
the question mark will take on successive integer values, starting at 0.
(The number of values is determined by the size argument.)

These successive values that NetLogo assigns to ``?``

determine the successive values of the reporter task,
which constitute the reported list.

Example: to produce a list of the squares of 0 through 9 we can use
``n-values 10 [? * ?]``.

You are not required to use the question mark if you do not need it.

Example: ``n-values 5 [random 2]``

Adaora Isichei

unread,
May 30, 2014, 2:42:09 PM5/30/14
to au-ec...@googlegroups.com
For the Gambler assignment ,so my "go" button shows an error, which states that Go should have two inputs. Below are my commands for go and playonce

to go [#player1 #player2]
   playonce #player1 #player2 ;; gamble
 end


 to playonce [ #player1 #player2 ]
   ifelse ( random-float 1 < 0.5 ) [
   transfer #player1 #player2 1
   ][
   transfer #player2 #player1 1
   ]
 end

Alan Isaac

unread,
May 30, 2014, 3:26:24 PM5/30/14
to au-ec...@googlegroups.com
Go and Arguments

The NetLogo error messages are usually pretty explicit.  When you are told you have an error of a certain kind, you should start by changing your code.  In this case your Go button simply calls your go procedure, but your go procedure requires two arguments.  So that is not going to work.  Before you move on, it is important to see why this will not work, and that NetLogo is simply telling you that you are trying to do something that cannot work. What you are doing with your Go button is no different than if you changed (in the button) go to playonce.  Try it: it will fail in exactly the same way. 

Now if you change the Go button to hold playonce 0 1, then that will work.  But of course you do not just want to play once; you want to ruin one of the players.  So you need to think about that too.

I recommend reading through the Gambler's Ruin notes again, slowly.


Message has been deleted

Adaora Isichei

unread,
May 30, 2014, 3:30:05 PM5/30/14
to au-ec...@googlegroups.com

Also what command do we use to track the " wealth history" of one player?

Alan Isaac

unread,
May 30, 2014, 7:04:22 PM5/30/14
to au-ec...@googlegroups.com
Wealth History

What did you learn about this in Coin Flipping Part III?

Ask more questions after you review that.


Dongping Xie

unread,
Jun 1, 2014, 2:06:29 AM6/1/14
to au-ec...@googlegroups.com
The Go button that links to the go procedure requires no inputs. So your go procedure shouldn't take any inputs either. Any variables inside the go procedure should all be predefined. Because you probably have created agents in the setup, you can substitute the agent indices (0 and 1) for #player1 and #player2.

Alan Isaac

unread,
Jun 8, 2014, 11:48:31 AM6/8/14
to au-ec...@googlegroups.com
Please use this thread to ask programming questions!  You should of course feel free to ask questions about the "answer sketches" I posted for the Coin Flipping exercise and the Gambler's Ruin assignment.  Do not leave your questions unanswered!

Alan Isaac

unread,
Jun 9, 2014, 11:04:42 AM6/9/14
to au-ec...@googlegroups.com
Remember, you can discuss any aspect of NetLogo programming in this (TA supported!) thread.

Additionally, Eric Charles is available in the Hurst Lab to provide NetLogo support. Contact him via http://www.american.edu/profiles/staff/echarles.cfm

The TA (Dongping) and CTRL (Eric Charles) can also arrange Skype or Collaborate sessions for those who are not in town but would prefer not to use email.

Finally, I encourage all students to commit their code to the Subversion repository, so that it is easier to discuss.  (Grad students must do this.)

Amanda Saville

unread,
Jun 10, 2014, 11:54:29 PM6/10/14
to au-ec...@googlegroups.com
Hi there,

I'm working on the Segregation project, and I am confused by the mention of "random-seed."  I read the definition of random-seed in the Netlogo dictionary, the discussion of random numbers in the dictionary, Professor Isaac's experiment design notes, behaviorspace netlogo notes.  Despite having read all of this, I am still not sure what a random-seed is or how to use one. Maybe that is too broad, but I don't even know how else I could revise my question to be more specific.

Dongping Xie

unread,
Jun 11, 2014, 4:54:45 AM6/11/14
to au-ec...@googlegroups.com
Hi,

Setting the seed for the random number generator allows the user to reproduce the exact results at any time. Under the same random seed, you should generate the identical series of random numbers each time.

For example, now you want to generate 10 white noises. You set your random seed to be 109.

random-seed 109
repeat 10 [show random-float 1]

We call this first series as S1. Now you again generate another series S2 by repeating ONLY the second command. S2 is different from S1.
If you reset the random seed (i.e. execute the both lines above), you get an identical series S3 to S1. That is,

random-seed 109
repeat 10 [show random-float 1] ;; get S1

repeat 10 [show random-float 1] ;; get S2, S2 != S1

random-seed 109
repeat 10 [show random-float 1] ;; get S3, S3 = S1


When you use the BehaviorSpace, you should probably set the random seed in the "Vary variables as follows" dialog as:

random-seed 559

The seed cannot exceed the random seed range.

Because you question is not very specific, I hope my answer can help. If not, please feel free to ask more questions.

Dongping

Alan Isaac

unread,
Jun 11, 2014, 8:06:20 AM6/11/14
to au-ec...@googlegroups.com
random-seed

Elaborating on what Dongping said,
if you set the value of ``random-seed`` in BehaviorSpace, it is reset to that value before each run
(not once per experiment).  This is a problem when you create replicates, since you want each
replicate to have its own seed.  Workaround: set the value during setup as a function
of ``behaviorspace-run-number``.  E.g., let seed behaviorspace-run-number random-seed seed
This will work because setup is called for every replicate (assuming you call it in your
BehaviorSpace setup dialog).

Amanda Saville

unread,
Jun 11, 2014, 9:34:11 AM6/11/14
to au-ec...@googlegroups.com
I'm just not even sure what my random-seed is supposed to be in my model... If I'm varying the variables at the top of behavior space, am I not supposed to do: ["number" 200 500 1000 1500 2000] because these are the values that should be randomly generated using the seed?

And looking at the netlogo dictionary, doesn't behaviorspace-run-number just report the number of runs the model is executing?

Amanda Saville

unread,
Jun 11, 2014, 9:44:08 AM6/11/14
to au-ec...@googlegroups.com
Also, I'm sorry that I feel just totally confused about this, despite having read everything.  Maybe it is because coding is still completely new, and I still have a lot of trouble thinking through the logic of some of the things we do in this course.

Alan Isaac

unread,
Jun 11, 2014, 9:55:41 AM6/11/14
to au-ec...@googlegroups.com
The whole point of this thread is to resolve programming questions.  The more questions the better!

Alan Isaac

unread,
Jun 11, 2014, 10:08:29 AM6/11/14
to au-ec...@googlegroups.com
behavior-space-run-number

A global variable declared in a slider (**instead** of in the globals declaration) may be called a parameter.  When you create a NetLogo experiment, you specify what scenarios (combinations of parameter values that could be set with your sliders) you want to consider, and how many replicates (which NetLogo calls "repetitions") you want for each scenario.  The number of runs is the number of scenarios times the number of replicates.  Each run gets its own behavior-space-run-number.  You can use that number to set the random seed, as in my previous post.

Note that each replicate of a scenario has *all* the same parameter values.  But you want the random behavior to be different.  Here are examples of common commands that include randomness (and so are affected by the random seed): ask, n-of, one-of, random, random-float.

Whether a particular global variable is set randomly or is part of your parameter sweep is up to you.  Usually you put in sliders (**instead** of in the globals declaration) those variables  you will "sweep" by considering specific values.

This will become clearer by trying it.

Am I answering your question?

Amanda Saville

unread,
Jun 11, 2014, 12:02:06 PM6/11/14
to au-ec...@googlegroups.com
Okay, just to clarify what I do understand at this point:

The globals that are declared in sliders are generally those we would want to "sweep." (Sweep meaning we'd want to designate specific values for each to test in our experiments).  Although if we choose to randomly generate values for these parameters that is a personal choice.

We want to extract the same results for each experiment as a whole.  To do this, we must control the randomness in the experiment using random-seed.  This would mean that every time the experiment would be run, the "randomness" would be the same according to its behaviorspace-run-number.  So for example, if the experiment is on run 98 of 125, all randomness would be the same for every run 98 of 125 each time the experiment is run (I use 125 as my total number of runs because runs=scenariosXreplicates, and in our example we have five values for each parameter times five replicates).

So with regards to the segregation model, the only aspects that will be randomized will be 1)  which turtles are which color initially (n-of), and 2) which patches these turtles move to if they are unhappy (ask).

To set the random-seed, we should be setting it in the procedure code for setup, not in BehaviorSpace because of clear-all, and because our BehaviorSpace calls setup anyway.

To set the random-seed in setup, I will need to use let seed behaviorspace-run-number random-seed seed, which I'm not so sure about but am attempting anyway.

Besides the last bit, am I still missing anything, or is my current understanding of random-seed correct?

Amanda Saville

unread,
Jun 11, 2014, 12:12:10 PM6/11/14
to au-ec...@googlegroups.com
Okay, I have now run my experiment twice, saving the output to two different spreadsheets so I could compare.  My results are the same for each column now, so hopefully this means I have implemented random-seed correctly...

If yes, then thank you very much Dongping and Dr. Isaac for walking me through my probably over-complicated struggle to understand random-seed.  If not, still thank you because I am probably much closer than I would have been without your help.

Alan Isaac

unread,
Jun 11, 2014, 2:04:11 PM6/11/14
to au-ec...@googlegroups.com
random-seed

Yes, identical spreadsheets is what you should see, once you set the seed as we have discussed.  Yea!

Alan Isaac

unread,
Jun 11, 2014, 2:17:26 PM6/11/14
to au-ec...@googlegroups.com
This has two parts: let seed behaviorspace-run-number defines a new local variable seed,
and then random-seed seed sets the random seed to the value of the local variable seed.
Hope that's clear,
Alan Isaac

Amanda Saville

unread,
Jun 11, 2014, 3:08:18 PM6/11/14
to au-ec...@googlegroups.com
Yes, thank you.  It is very clear now--it is one of those things that once you understand, you feel like you can't remember why you didn't! 

Sebastien Lundby-Thomas

unread,
Jun 15, 2014, 8:08:20 AM6/15/14
to au-ec...@googlegroups.com
Dongping can we set up a skype session? I am having some troubles making sure I am understanding how to use the coding for my main project which is based on the financial Bubbles model that already exists. 

On Tuesday, May 13, 2014 10:49:35 AM UTC-4, Alan Isaac wrote:
This course will have a TA, Dongping Xie. Please use this thread to discuss NetLogo programming with Dongping. This can include questions about the assignments. Naturally Dongping cannot simply provide answers for the assignments, but he can provide hints and guidance.

I will not follow this thread; it is not part of the graded discussions.  Additionally, you should feel free to take "off list" any discussions that you believe are of interest only to you and not to the rest of the class.  However, please realize that most questions about NetLogo programming will interest other members of the class.

Thank you,
Alan Isaac

Dongping Xie

unread,
Jun 15, 2014, 9:49:24 AM6/15/14
to au-ec...@googlegroups.com
Hi,

Dongping can we set up a skype session? I am having some troubles making sure I am understanding how to use the coding for my main project which is based on the financial Bubbles model that already exists. 

Sure thing. Please send me an email at dx4...@student.american.edu to propose a time for Skype call. If you can send me a list of questions that you want me to help you with and the code you have had, it will be much helpful.

Thanks,
Dongping 

Amanda Saville

unread,
Jun 15, 2014, 10:59:43 PM6/15/14
to au-ec...@googlegroups.com
Hi all,

I'm working on the First Approach to the Template Model and I have two questions:

1)  I realize that my world is too big, and that I've fallen into the "common mistake" of using resize-world with min x and y coordinates of -50 and max x and y coordinates of 50... but I'm not really understanding why this is incorrect.  Does this not make a grid of 100 x 100?

2)  Is there supposed to be a specific stopping criteria for our move procedures?  When I reread Part I, I feel like there should be 1) a possibility for a full Moore neighborhood to be occupied, but given my current world that seems unlikely and 2) that there should be some limitations on move if the neighborhood is full.

Has anyone else found themselves thinking about these same issues?  If so, any advice for how you overcame them or ideas of where to look to maybe spark an "aha" moment?

Thanks!

Alan Isaac

unread,
Jun 16, 2014, 7:38:18 AM6/16/14
to au-ec...@googlegroups.com
Zero-Based Indexing

Place your hand palm down with finger parallel to a wall, and slide it all the way up to the wall.  Label each finger with the distance (in fingers) it is from the wall.  What are the labels?
https://subversion.american.edu/aisaac/notes/netlogo-intro.xhtml#arrays

Alan Isaac

unread,
Jun 16, 2014, 7:43:56 AM6/16/14
to au-ec...@googlegroups.com
Stopping Condition

*First Approach* does not introduce a stopping condition until Part IV.

To start and stop your simulation in the meantime, you can make a `Go` button that runs one iteration of your simulation.  Make it a "forever button".
https://subversion.american.edu/aisaac/notes/netlogo-intro.xhtml#stopping-forever-buttons


Amanda Saville

unread,
Jun 16, 2014, 4:36:56 PM6/16/14
to au-ec...@googlegroups.com
I have two quick questions about correct notation: when do we use one semicolon, and when do we use two?  And secondly: when commenting our global variables, does it suffice to state something like "initial wealth of each patch?"

Thanks!

Alan G Isaac

unread,
Jun 16, 2014, 5:31:58 PM6/16/14
to au-ec...@googlegroups.com
Any more than one semicolon is redundant.
It is for emphasis only.
But it is common to use two semicolons for greater visual emphasis.

And yes, that is a fine example of a comment.

Alan Isaac

Natalie Chambers

unread,
Jun 19, 2014, 10:30:19 AM6/19/14
to au-ec...@googlegroups.com
This question is very specific to our model: Amanda and I have reread this article numerous times but are having trouble understanding a few key variables that are not defined within the paper but are necessary for our code equations. Perhaps they are well known parameters that we just don't know. But if possible could you explain:

Beta (β) : right after equation 1, the authors explain  0 < β < 1 but they don't indicated what beta represents and it is used in several of the following equations. 
Right after equation 2, the authors use the following parameters but don't explain their meaning
Gamma (γ): 0 < γ < 1
Theta (θ) : 0 < θ < 1

It would be very helpful for us to understand these terms to complete our code (with accuracy)

Thank you

Alan Isaac

unread,
Jun 19, 2014, 11:10:32 AM6/19/14
to au-ec...@googlegroups.com
Paper Specific Questions (Alfaro et al 2000)

Since you posted the link (http://www.people.hbs.edu/lalfaro/fdipaper25.pdf ) in a timely manner, ideally your classmates will have read the paper and could help you with this question.  However, since it is a math-related question, I'm going to pitch in.

Production in the FDI sector is Cobb-Douglas, and beta is the production function parameter. See equation (1). Intuitively, it is the labor elasticity of output in that sector.  If they do not suggest a value, try a value around 2/3 (as justified by labor's share in production in the national income accounts).

Theta plays a similar role for entreprenuerial production.  It is the capital elasticity of production.  You can try a value of 1/3 if they do not suggest a value.  Gamma is the elasticity with respect to the fixed stock of capital. Again, go ahead and use 1/3 if they do not suggest a value.

I do not recall any suggested values for these parameters in the paper, but perhaps there is some suggestion in the empirical results.


Amanda Saville

unread,
Jun 19, 2014, 8:16:46 PM6/19/14
to au-ec...@googlegroups.com
Dr. Isaac,

Thank you for addressing these questions.  Natalie and I are working on our code currently and we have some additional questions:

In your comments on our code, you say that the paper starts with shared wealth values.  After reviewing the paper, I see that it says:

Agents are all endowed with some initial wealth bit−1 on which they can earn the international return r at the end of the period should they choose to invest it in capital markets. Initially, we assume that bit−1 bt−1 for all i.


As agents increase their wealth attributes, they would then consume and leave bequests (with which they would start the next period), that would vary depending on their income levels through the periods.  Thus, I just want to confirm that I am understanding correctly that all agents will start with the same value of initial wealth (bt−1) which would then later become (bit−1).

Secondly, you commented:

    ;;ai: (where will they be used?) 
;;ai: for now it is not clear you need YDOM and YFDI to be global

But it was our understanding that we would want to plot economic growth over time, with economic growth being the percent change in the sums of YDOM and YFDI.  Without these two variables, I'm not sure how else we would calculate economic growth, since the equations for output in the two sectors are different.




Natalie Chambers

unread,
Jun 19, 2014, 8:19:21 PM6/19/14
to au-ec...@googlegroups.com
Integrals in Netlogo

Thank you for that detailed response. That is very helpful. In our paper there are a couple of equations with integrals which we generally understand but are unsure about how to represent them in netlogo. Since they are relatively basic integrals my initial thought was to represent them using some form of the sum function but I thought it would be more accurate to use an actual integral function. I searched a bit online and found this netlogo integral function on an online forum. (http://netlogo-users.18673.x6.nabble.com/Integration-in-NetLogo-td5003373.html). This seems to be an extension created by another professor so maybe it is not accessible or legitimate, if not does anyone know if there is an integral function on netlogo?


let  fnctn  task  
[function7 ?]

let  intgrl  numanal:romberg-integrate  0.0  3.0  fnctn

to-report function7 [ x ]
report  e ^ (-3 * x)
end

numanal:romberg-inegrate takes three arguments, the lower and upper bounds of the definite integral (here 0.0 and 3.0), and a task that evaluates the function at any given value of the integrating variable.  In the example above, the function to be integrated is e^(-3x) and is evaluated in the reporter, function7, which is then assigned to the task variable, fnctn.  romberg-integrate returns the value of the definite integral.

Alan Isaac

unread,
Jun 19, 2014, 9:04:54 PM6/19/14
to au-ec...@googlegroups.com
Integrals Are Sums

First of all, it's great you found the numanal extension, which can be very useful.  However in this case, the integral in the theory chapter just represents a sum across a continuum of agents.  When you move to your simulation framework, you have a finite number of discrete agents, and you just sum across them.

Alan Isaac

unread,
Jun 19, 2014, 9:10:28 PM6/19/14
to au-ec...@googlegroups.com
Paper Specific Questions (Alfaro et al 2000)

Initially shared wealth: "Initially, we assume that bi,t1 = bt1 for all i."  One thing that was not clear in the paper is whether they are actually evolving wealth over time, so that "initially" just means "at time 0".  I assume that is what you will need: after the first period, wealth will have to evolve.  (Note that agents only live one period, but that seems purely interpretive: the same patch represents a "different" agent each period.

Alan Isaac

unread,
Jun 19, 2014, 9:14:09 PM6/19/14
to au-ec...@googlegroups.com
Global Variables vs Procedures

NetLogo's structure encourages the use of global variables, but they are "dangerous".  If you will just compute them once per tick for plotting purposes, you can move that computation into a separate procedure.  Of course if you end up computing the *same* value over and over, this becomes inefficient, and it can make sense to use a global variable.

These comments do not imply any requirement on how you decide to approach this!

Amanda Saville

unread,
Jun 19, 2014, 9:56:08 PM6/19/14
to au-ec...@googlegroups.com
One thing that was not clear in the paper is whether they are actually evolving wealth over time, so that "initially" just means "at time 0". 
 
So if we have all of our patches start with the same initial wealth, should we just be picking a value between 0 and 1?  Or are there other ideas on how this should be determined?

Not to bombard the thread with questions regarding our specific project, but I'm also not sure how we can set total factor productivity.  I'm not finding a suggested value for this constant in the paper, and I know we wouldn't want to set TFP too high or too low... or maybe it wouldn't matter because that isn't what our simulation is testing anyway?

Natalie Chambers

unread,
Jun 19, 2014, 9:57:48 PM6/19/14
to au-ec...@googlegroups.com
Ok I thought sum function may work but how do we set the finite number of discrete agents (0 to ε*)?

 To sum for labor in FDI, equation (7), would the function be:

sum (Yi,t / (tB(K(f,t) ^ di) ^ θ * S ^ γ)) 0 ability

where 0 ability are the bounds for the sum?

Natalie Chambers

unread,
Jun 19, 2014, 10:38:42 PM6/19/14
to au-ec...@googlegroups.com
Sorry we have an additional question. We are getting into the nitty-gritty of the equations now so we are discovering some gaps in our knowledge about the variables and the equations. The wage equation, right under equation 2, is comprised on all constants but we think that wage should vary in order for flow between the sectors. Doesn't it make sense for the wage to change after each period?

Alan G Isaac

unread,
Jun 19, 2014, 10:52:23 PM6/19/14
to au-ec...@googlegroups.com
*Paper Specific Questions*

When they do not specify parameter values, you will have to either choose an arbitrary value, or try to calibrate a value. (E.g., you can look at
their empirical work to see if it provides guidance.)

The number of agents is arbitrary. I suggested using smallish numbers like 100 (10x10) or 1000 (25x40). You should write a procedure to report the
output, and then you can `` sum [output] of patches``.

hth,
Alan Isaac

Alan G Isaac

unread,
Jun 19, 2014, 10:54:54 PM6/19/14
to au-ec...@googlegroups.com
*Paper Specific Questions*

Working from memory ... each period the wage clears the market in the theory paper.
In your simulation -- I'm pretty sure I mentioned this before -- you will probably want
to base labor supply on an expected wage (perhaps the previous period's wage).

Alan Isaac

unread,
Jun 20, 2014, 8:12:42 AM6/20/14
to au-ec...@googlegroups.com
Paper Specific Questions (Alfaro et al 2000)

The value ε* is the break-even level of productivity.  See equation (6) for its determination.  Once you compute it, you can just test against it. However note that if b(t-1) evolves, then ε* can be different for each agent. As I said before, I do not think they actually follow the evolution of b(t) in their theory section. For you two this is no problem however: you can just do the test each period for each agent.

Hth.

Btw, just out of curiosity, what method did you use to input the ε character in your message?

Natalie Chambers

unread,
Jun 21, 2014, 7:33:12 PM6/21/14
to au-ec...@googlegroups.com
I got the ε character just by copying and pasting from the paper. I wanted to follow up the previous question I asked about converting the integral function to a sum command in netlogo. How do you incorporate the bounds of the integral when you make a sum function?

Alan Isaac

unread,
Jun 22, 2014, 9:17:46 AM6/22/14
to au-ec...@googlegroups.com
Integrals Are Sums

An integral is just a sum. The role of the bounds on the integral are just to "index" the values you will sum over.  You can also write the bounds of an integral as a set: sum f(ei) for ei>estar. In the simplest case, `f(ei)=ei` and this becomes `sum ei for ei>estar`.  In NetLogo, if the ei are a patche attribute, this becomes

   sum [ei] of patches with [ei > estar]

Am I answering your question?


Amanda Saville

unread,
Jun 22, 2014, 1:24:10 PM6/22/14
to au-ec...@googlegroups.com
As promised, Natalie and I have more questions regarding our project.  

We are getting an error message stating: "Expected command" on our new line of code using sum. 

Our code says:

to determine-labor
  sum [ability] of patches with [break-even > ability > 0]
end

Any ideas why this would be happening?  Thank you!

Amanda Saville

unread,
Jun 22, 2014, 1:37:12 PM6/22/14
to au-ec...@googlegroups.com
We see now that it should say:

to determine-labor
  set labor sum [ability] of patches with [break-even > ability > 0]
end

But we are running into new issues if we do not have labor and capital as global variables.  Will we need to set them locally and combine all of our smaller sub-procedures?

Alan Isaac

unread,
Jun 22, 2014, 2:38:52 PM6/22/14
to au-ec...@googlegroups.com
For the labor supply, I think you want something more like this:

to-report labor-supply
  report sum
[ability] of patches with [ability < break-even]
end



Alan Isaac

unread,
Jun 22, 2014, 2:49:07 PM6/22/14
to au-ec...@googlegroups.com
Paper Specific Questions (Alfaro et al)

Regarding the capital stock, the problem is that the MPK depends on the labor input.

Here is one approach: use "expected" labor input, setting that equal to the labor input from the previous tick.  That allows you to just use their equation (2), but with Le instead of L.

Does that get you there?

Natalie Chambers

unread,
Jun 22, 2014, 3:04:54 PM6/22/14
to au-ec...@googlegroups.com
Doing that, we would need to set an initial labor correct? We considered setting labor initially to 100 since that is the total number of patches but we didn't know if that would skew the model

Alan Isaac

unread,
Jun 22, 2014, 5:25:54 PM6/22/14
to au-ec...@googlegroups.com
Paper Specific Questions (Alfaro et al)

You would need to set an initial *expected* labor, yes.  (See the comment in the source code.)

Alan Isaac



Natalie asked:

Matthew Reardon

unread,
Jun 22, 2014, 9:45:20 PM6/22/14
to au-ec...@googlegroups.com
When using the create-turtles command, are the steps taken within this block limited only to the turtles created in this block?  

for example, consider the following slightly modified (for simplicity ) code:

create-turtles nturtles
 [
    set color white
    setxy random-xcor random-ycor
    set productivity random-float 1
 if  ( productivity < .5 ) [die]
  ]

does the if/[die] command only apply to the nturtles just created or does this apply to all turtles that exist within the model?

For example, if I inserted "count turtles" within this block, would that count the number of turtles just created or the number of total turtles? 

Alan Isaac

unread,
Jun 22, 2014, 10:31:02 PM6/22/14
to au-ec...@googlegroups.com
The answer to both questions is yes.  But here is how the second part would work: *each* of the created turtles would count all the turtles.  (Note that `turtles` is the agentset of all turtles.)

Hope that's clear,
Alan Isaac



On Sunday, June 22, 2014 9:45:20 PM UTC-4, Matthew Reardon wrote:
When using the create-turtles command, are the steps taken within this block limited only to the turtles created in this block?  

Matthew Reardon

unread,
Jun 22, 2014, 11:31:08 PM6/22/14
to au-ec...@googlegroups.com
Aahh, so including a counter ( set howmany howmany + 1) within the block would essentially work the same as counting iterations within a foreach loop.  Just as useful for my purposes.

On Tuesday, May 13, 2014 10:49:35 AM UTC-4, Alan Isaac wrote:
This course will have a TA, Dongping Xie. Please use this thread to discuss NetLogo programming with Dongping. This can include questions about the assignments. Naturally Dongping cannot simply provide answers for the assignments, but he can provide hints and guidance.

I will not follow this thread; it is not part of the graded discussions.  Additionally, you should feel free to take "off list" any discussions that you believe are of interest only to you and not to the rest of the class.  However, please realize that most questions about NetLogo programming will interest other members of the class.

Thank you,
Alan Isaac

Alan Isaac

unread,
Jun 23, 2014, 8:18:28 AM6/23/14
to au-ec...@googlegroups.com
create block

Yes, the block is executed once per turtle created.  It is like using an ask command just for the set of newly created turtles.

Alan Isaac

Alan Isaac

unread,
Jun 26, 2014, 1:20:12 PM6/26/14
to au-ec...@googlegroups.com
n-of

When you use `n-of mynumber myagents`, you need to make sure `myagents`
has at least ``mynumber`` members, or you will get an error.

You may want to use something like

let %n min (list mynumber count myagents)
ask n
-of %n myagents [...]



Reply all
Reply to author
Forward
0 new messages