I am trying to create a generator that uses the (corrected) Barabasi-Albert algorithm for a preferential attachment network in NetLogo. There are two parameters: number of (eventual) nodes, and number of edges added per node. The network extension has a version, but it is restricted to the case with 1 edge added per node.
Simplified complete model:
extensions [rnd]
to makeNW-BA
clear-all
let new-edges 4
let popn 25
create-turtles new-edges [ setxy random-xcor random-ycor ]
ask turtles [ create-links-with other turtles with [not link-neighbor? myself] ]
repeat popn - count turtles
[ let targets rnd:weighted-n-of new-edges turtles [ count my-links ]
create-turtles 1
[ setxy random-xcor random-ycor
create-links-with targets
]
]
end
The line let targets rnd:weighted-n-of degree turtles [ count my-links ] is creating a java error (ClassCastException) while observer running _asm_proceduremakenwba_setprocedurevariable_11. This is the first time I have used the rnd extension so I don't know if the problem is my coding, or there's actually a bug causing the java error.
UPDATE
I have now set up a turtles-own variable for degree (ie count my-links) and trying to do let targets rnd:weighted-n-of new-edges turtles [ degree ]. This gets me a NetLogo error instead, that the observer can't access a turtle variable without specifying which turtle. However, trying to add of self doesn't help.
Does this produce the desired network?
let targets rnd:weighted-n-of new-edges turtles [ [count my-links] of ? ]
It is hard for me to eyeball it with new-eges set to 4. When I set it to 1 it seems to make a preferential attachment network.
It appears that (and let the voice of God disagree with me) [rnd:weighted-n-of] was designed to work with lists and has a hidden foreach or map in it resulting it an error in casting when given an agent set.
Related
In Netlogo there are some default models, including the lattice grid.
I am wondering how to add three types/breeds (40%, 40%, 20%) of turtles connected to each others only at the beginning, then, when tick > 0, some new links can be created between turtles of different type.
In the models library in Netlogo, I found the code for a regular lattice:
to setup-square
clear-all
ask patches [ sprout 1 ]
ask turtles [ create-links-with turtles-on neighbors4 ]
reset-ticks
end
Instead of turtles, I should consider the three types (types1, types2, types3) in order to have a lattice with:
40% of turtles as breed type1 (red);
40% of turtles as breed type2 (blue);
20% of turtles as breed type3 (white).
Initially, all the turtles above are connected to other turtles of the same type. Links between turtles may change through time, so links between turtles of different breeds can exist when tick>0.
I would do as follows (instead of ask turtles [...]):
ask types1 [ create-links-with types1-on neighbors4 ]
ask types2 [ create-links-with types2-on neighbors4 ]
...
but the problem is that I have no control on the proportion of turtles for each type. Also, a condition on the breed of the neighbors of a turtle should be considered as well.
How can I set the proportion of these types and make connections only between turtles on the same type, then mix links?
There are several ways to do this, some of them more exact than others. It is especially important to realise that you can change a turtles breed after it has been created, simply by asking it to set breed <breed>.
In the first example, I give all turtles type1 as they are created, and then change a predefined number of them to another type. Keep in mind how you get that number as rounding errors can have an effect here (see further down for the counts I had). It is important that you give them all a breed at first, as this version will not work with ask n-of <percentage> turtles [set breed <breed>]. That is because the agentset turtles will always contain all turtles, even those who you have given a different breed.
breed [types1 type1]
breed [types2 type2]
breed [types3 type3]
to setup-shape
set-default-shape types1 "default"
set-default-shape types2 "x"
set-default-shape types3 "square"
end
to setup-1
ca
setup-shape
ask patches [sprout-types1 1 ]
let total count turtles
ask n-of (total * 0.4) types1 [set breed types2]
ask n-of (total * 0.2) types1 [set breed types3]
end
A second version that works with a more sochastic approach is to let each newly created turtle choose random number, and give it a breed depending on the number. Keep in mind that the proportions will vary more, as they are determined by chance rather than exact numbers.
to setup-2
ca
setup-shape
ask patches [ sprout 1 [
let number random-float 1.0
show number
(ifelse number < 0.4 [set breed types1]
number < 0.8 [set breed types2]
[set breed types3])
]
]
end
The counts I had of the different versions for each type. Notice that in version 1, type1 and type2 have a different count because I let all turtles start with the breed types1.
to count-types
;standard world of 33x33
show count types1 ; version 1: 437 , version 2: 454
show count types2 ; version 1: 435 , version 2: 432
show count types3 ; version 1: 217 , version 2: 203
end
I am very new to Netlogo and I am trying to simulate culling of turtles within a certain region based on the characteristics of the patch. Within my model landscape, I have one single patch that is orange. I want 50% of turtles within 5 pixels of that patch to die. I would also like this process to continue every year after the first year of the simulation (this part I think I have). Can anyone help me?
if d = 10 [
if year >= 2 [
let ring nobody
set ring patches in-radius 5 patches with [pcolor = orange]
ask turtles-on ring [die]
]
]
in-radius reports agents that are within a certain distance from the caller, so the orange patch must be the one calling in-radius.
The code in your example is not reproducible, so I'll make an arbitrary example:
to setup
clear-all
ask one-of patches [
set pcolor orange
]
create-turtles 100 [
setxy random-xcor random-ycor
]
end
to go
ask patches with [pcolor = orange] [
ask turtles-on patches in-radius 5 [
if (random 2 < 1) [die]
]
]
end
Note that in-radius reports agents within a certain distance, where the unit of measure of distance is a patch`s side.
Identifying patches within a certain distance and then asking all turtles on those patches to die (as in the example above, which follows the approach in your question) has a different effect than asking all turtles within a certain distance to die.
Which of the two approaches fits your case is something that you have to decide. Below, the code that would address turtles directly:
to go
ask patches with [pcolor = orange] [
ask turtles in-radius 5 [
if (random 2 < 1) [die]
]
]
end
We're creating code and we're having trouble doing the following:
We would like to create a specific number of turtles (for example 100 turtles) based on the following criteria:
distance between agents must be greater than or equal to 2
We've already tried:
to setup
ask n-of 100 patches [
if not any? turtles in-radius 2 [
sprout-turtles 1 [ setup-turtles ] ]
]
]
end
to setup-turtles
set energy 0
set size 1
set color orange
end
it didn't work, because less than 100 agents are being born even though the world holds the required amount of agents, which in this case is 100
Does anyone have any suggestions on how we can resolve this issue?
Thanks in advance
The approach you outline is running into issues because you are asking patches to sprout turtles if they meet some condition. Because the patches are operating in a random sequential order, you are selecting some patches that, by the time it is their turn to act, no longer fulfill the condition to sprout since other nearby patches have already sprouted a nearby turtle.
One option is to use a while loop to keep trying to sprout turtles until the desired number is reached:
to setup
ca
while [ count turtles < 100 ] [
ask one-of patches with [ not any? turtles in-radius 2 ] [
sprout 1
]
]
reset-ticks
end
Be careful with while loops- if you do not code such that the while condition can eventually become false, your model will run forever stuck in the loop.
Another option that will give a more explicit error if it fails would be to just create your number of turtles, then have them move to a space that fulfills the condition:
to setup-2
ca
crt 100 [
move-to one-of patches with [ not any? turtles in-radius 2 ]
]
reset-ticks
end
I have N groups of turtles which own the variable group-id (1 to N, N is defined at setup, each group has 25 turtles).
At the beginning each group of turtles spawns on a random patch with variable patch-group-id that matches the turtle group-id, like so:
let i 0
while [ i < n_groups ] [
let x random 100
let y random 100
ask patch x y [ set patch-group-id i set pcolor gray ]
create-turtles 25 [
set group-id i
setxy x y
]
set i i + 1
]
The turtles move around but at a later step in the process I would like them to move back to their ‘home’ patch. Other turtles may also change their group-id and therefore their home patch and I would like them to move there too.
Is there any way to do a comparison along the lines of…
Ask turtles [
Let target one-of patches with [patch-group-id = group-id]
]
And then ask the turtle to move-to or jump to the target?
NetLogo doesn’t like this (‘Min-one-of expected this input to be a number block, but got a true/false block instead’) and I think there are probably other issues too.
patch-group-id is a variable owned by patches and group-id is owned by turtles. So you need to tell NetLogo which group-id the patches are trying to match to. Try this:
ask turtles
[ let target one-of patches with [patch-group-id = [group-id] of myself]
]
Another way to do this is to drop the idea of group-id and simply have each turtle remember its home patch. Conceptually, this implements the idea that a group is entirely defined by its home patch. So your setup would look something like:
turtles-own
[ my-home
]
to setup
clear-all
ask n-of n-groups patches
[ sprout turtles 25
[ set my-home patch-here
]
set pcolor gray
]
end
Then you never need to construct the target, you simply get then to go to their variable my-home.
If you went down this path, you would also need to change the code that uses group-id. For example, you said that sometimes the turtles change their group-id, instead of set group-id [group-id] of ... you would have set my-home [my-home] of ...
We try to show a simple infection via Netlogo. For our purpose we need to start the infection with the same turtle for several times.
But right now with every setup another turtle begins with the infection. We already tried to work with the Node ID, but unfortunately the ID of the different turtles changes with every setup, too. We are out of ideas but
maybe there is a way to sove this problem I am happy for any answers :)
This is our Code so far:
extensions [nw]
globals
[
num-informed
informed-size
]
turtles-own
[
informed?
]
to setup
clear-all
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [ set size 1.5 ]
layout-radial turtles links turtle 61
ask turtles [set color red]
ask turtles [set shape "dot"]
ask links [set color grey + 1.5]
ask patches [set pcolor white]
ask turtles [set label-color black]
ask turtles [set informed? false]
ask turtle 72
[
set informed? true
set color green
]
set num-informed 1
set informed-size 2
reset-ticks
nw:save-graphml "JKnachnamennetlogo.graphml"
end
to spread
if (count turtles with [informed? = true] > .7 * count turtles) [stop]
ask turtles with [ informed? = true ]
[
ask link-neighbors with [not informed?]
[
if (random-float 1 <= 0.01)
[
set informed? true
show-turtle
set color green
]
]
]
set num-informed count turtles with [informed? = true]
tick
end
Thank you a lot.
I am a little unclear so am giving bits of different answers for different situations.
If the turtles are different each time, what do you mean by 'the same turtle'. For example, do you mean the turtle in a particular position? If so, you could select the turtle on the appropriate patch.
If it doesn't matter which particular turtle it is (just that it's the same turtle), then the simplest approach is to set the random-seed. Then every time you run any random process (including choosing one-of the turtles to select the starting infection, or ask turtles to do something), NetLogo will use the same chain of random numbers. Of course, if you are still building your model, then adding new pieces of code that change how many calls are made to the random number generator will lead to a different chain, but rerunning with the same code will give the identical run.
You may need to use with-local-randomness and random-seed new-seed if you want to have some parts actually change.
The problem is that nw does not store the WHO variable this is to avoid conflict with already existing turtles in a model.
A work-around would be assigning each turtle a separate id variable and setting that to who.
turtles-own [informed? id]
in turtles creation asign them each the id thus
set id who
you may want to write a conversion procedure like this
to convert
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [set id who]
nw:save-graphml file-name "JK_nachnamen(id).graphml"
end
and use the copy. Of course you would not use
turtle 74
but
one-of turtles with [id = 74]