Reputation: 21
I am building a network in Netlogo so that a link appears with higher probability between proximate actors and actors sharing a language. Link appears if random-float 1 + 0.3 * same.lang > 0.1 * exp(link-length)
, where same.lang
is 0 or 1 if actors share a language.
I already have very slowly working code that firstly builds absolutely dense network and based on probabilities asks links to die. Here it is:
ask turtles [ let fellows other turtles with [not member? self my-in-links ]
create-links-with fellows
]
ask links [ ifelse
[lang1 ] of end1 = [ lang1 ] of end2 or [ lang2 ] of end1 = [ lang2 ] of end2
[set same.lang 1]
[set same.lang 0]]
ask links [
if random-float 1 + 0.3 * same.lang < 0.1 * exp(link-length)
[die]
But I want turtles to firstly estimate probability and only then create links to make code work faster. For that I produced smth like:
ask turtles[
let test-num random-float 1
let turtle.num [ who ] of other turtles
foreach turtle.num
ifelse [ [ lang1 ] of myself = [ lang1 ] of turtle turtle.num or [ lang2 ] of myself = [ lang2 ] of turtle turtle.num ]
[
if ( test-num ) >= (0.1 * exp(distance [turtle turtle.num]) - 0.3)
[ create-link-with turtle turtle.num ]
]
[
if ( test-num ) >= (0.1 * exp(distance [turtle turtle.num]))
[ create-link-with turtle turtle.num ]
]
This code does not work with error message
Expected command
How to improve this code or give me advice how to build up probability based network from the turtles.
Upvotes: 1
Views: 142
Reputation: 17678
You didn't provide information about the specific line generating the error (NetLogo highlights it for you). But one possibility is ifelse [ [ lang1 ] of myself = ...
as this should be ifelse [ lang1 ] of myself = ...
. You can put in a ( bracket for clarity, but the [ bracket is a syntax error. I can't work out why you would get the specific error you said though.
You also asked for comments on the code. If you're looping through agentsets in NetLogo using foreach
, then the first question you should ask yourself is 'why am I using a list?'. The main reasons to use a list are to preserve order or allow duplicates, for example if you want each turtle to keep track of the turtles it meets. These don't apply in your case, so an agentset is a more NetLogo-ish way of doing this. Even if you were using a list, you would use a list of turtles, not of who
numbers, so you don't have to move between the numbers and turtles with
that who
number.
There's also some logic potential problems:
This is untested, but addresses those problems. I think it is wrong though, the nested ask
will lose the reference for myself
so you might need to add a line like let origin myself
and that will allow you to reference the turtle stored as "origin".
ask turtles
[ ask other turtles with [who > [who] of myself]
[ ifelse ([lang1] of myself = lang1) or ([lang2] of myself = [lang2])
[ if random-float 1 <= (0.1 * exp(distance myself) - 0.3)
[ create-link-with myself ]
]
[ if random-float 1 <= (0.1 * exp(distance myself))
[ create-link-with myself ]
]
]
Upvotes: 1