Using gdf file with netlogo nw extension - netlogo

I have successfully loaded a gdf file using the nw extension in NetLogo.
How can I limit the number of turtles?
Attached the code I wrote:
exextensions [ nw ]
breed [ orgs org ]
directed-link-breed [ likes like ]
to setup
ca
nw:load-gdf "d://netlogoWork/j14clean.gdf" orgs likes
end

Related

Getting turtles to spend energy while moving on a least cost path in a network environment

I’m creating a model in which turtles choose and traverse a least cost path in a network environment, one node at a time.
I’m using the nw:turtles-on-weighted-path-to primitive from the NW extension to let my turtles choose a path. Relevant snippets of my code (which so far works fine) are below:
undirected-link-breed [ routes route ]
undirected-link-breed [ tracts tract ]
breed [ stalkers stalker ]
stalkers-own [ energy ]
links-own [ difficulty ]
to setup-network
ask routes [
set thickness 2
set color white
set difficulty 1
]
ask tracts [
set thickness 2
set color black
set difficulty 2
]
end
to enter-the-zone
ask stalkers [
let path nobody
let target one-of nodes-on patch -10 42
ask nodes-on patch-here [ set path but-first nw:turtles-on-weighted-path-to one-of nodes-on patch -10 42 difficulty ]
ifelse patch-here != ( patch -10 42 )
[
face first path
move-to first path
]
[ return_to_base
]
]
tick
end
to return_to_base
move-to patch -258 -231
end
However, I would like to add an “energy” attribute to my turtles that depletes a little every time they reach a new node. The catch is: I would like this loss of energy to be equal to the difficulty of the link between the node they are now and the node in which they just were.
What would be the best way to implement that?
Thanks in advance for the help.

Copy the link breed variable in the patch below

I have a network of nodes and links. This figure
is a capture of the world. The graph represents streets of a city. I have imported a shapefile with the gis extension. The gray lines are links, black dots are nodes and red dots represent people. The people move heading to the next node. In a street corner, the red dot chooses next street by examining the variable popularity owned by the link.
The links breed has a variable, popularity, whose value I would like to copy in the patches that are below.
If I try, for example, something like this to access patches under links will produce an error
ask links [show [(list pxcor pycor)] of patch-here]
Another approach can be to access links variable popularity from patches, but I do not know how to do it.
The reason why I want this is because I want to write in a file a matrix of popularity values and its position in the matrix should correspond with the position of the link in the world. Thus, the patches below the links would give me the matrix form. I have a procedure that for each patch writes the value of the patch in a file. However, I do not know how to pass the popularityvalue from the link to the patch below it.
Is there any way to copy a link owned variable to its patch?
Regards
If someone has a better way of doing this (or can simplify my code), feel free. Here is a complete working example. Copy it into an empty NetLogo model and run it to see it work.
The setup procedure just creates some nodes and links with appropriate test values and then calls the transfer-link-values procedure, which does what I think you want. The setup procedure then puts the values into the patch labels to display them and see the results.
The way the transfer-link-values procedure works is to create a turtle at one end of the link, and that turtle moves toward the other end of the link transferring the value as it goes. When it gets to the other end, the turtle dies.
patches-own [patch-popularity]
links-own [link-popularity]
to setup
clear-all
create-turtles 10 [ setxy random-xcor random-ycor]
while [ any? turtles with [not any? my-links] ]
[ let to-pair turtles with [not any? my-links]
let thisNode one-of to-pair
ask thisNode
[ create-link-with one-of other to-pair
[ set link-popularity 5 + random 5 ]
]
]
transfer-link-values
ask patches [ if patch-popularity != 0 [set plabel patch-popularity ] ]
end
to transfer-link-values
ask links
[ let start-node one-of both-ends
let this-link self
let end-node nobody
ask start-node [ set end-node [other-end] of this-link ]
let transfer-value link-popularity
ask start-node
[ hatch 1
[ face end-node
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
while [ not member? end-node turtles-here ]
[ forward 1
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
]
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
die
]
]
]
end

Netlogo: Set specific setxy patern with set number of turtles?

Is it possible to create a set number of turtles, from a file, to have their own patches? Like always be in the same location?
I've got 106 turtles I'm reading in from a file and I was hoping to have them be created on their own patches, like a square latice kind of thing. I want to be able to look at the model world and easily identify a turtle.
file-open "turtledata_A.txt"
show file-read-line
while [not file-at-end?]
[
set param read-from-string (word "[" file-read-line "]")
create-turtles 1 [setxy ??]
]
file-close
]
Probably easiest to use the csv extension and just add xy data to the file you're reading in. For example, if you have a turtle_data.csv file that looks like:
param-to-read,x,y
John,-10,10
Billy,-5,5
Bob,0,0
Simon,5,-5
Michael,10,-10
You can do:
extensions [ csv ]
turtles-own [ param ]
to setup
ca
reset-ticks
file-close-all
file-open "turtle_data.csv"
;; read the headings line in to skip it for data extraction
let headings csv:from-row file-read-line
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
set param item 0 data
setxy item 1 data item 2 data
]
]
file-close-all
end
which would give you something like:
Then you can modify the x and y values in your .csv file to place your turtles where you want them. Would that work?
Of course, you can add other columns in the .csv file (like color, size, shape, etc) that will help you identify turtles at a glance.

Netlogo - identifying a subset of an agentset

I spent all afternoon trying to work out with a part of my code and I don't seem to be getting anywhere. Basically, I'm trying to create a social network on model setup. Each person in the model starts off with a set of people that are nearby to them people-nearby. It is from this set that people choose who to connect with:
create-people population-size
[
set people-nearby turtle-set other people in-radius neighborhood-radius
]
to create-network
let num-links round (average-node-degree * population-size) / 2
while [ count links < num-links and count people with [length sort people-nearby > 0] > 0 ]
[ ask one-of people
[ *... initiate probabilistic link creation process...*
create-unlink-with chosen-friend
Once person A has connected to someone (ie. person B), person B is removed from person A's people-nearby set. I'm having trouble with this portion of the code where the people-nearby set is updated by excluding all nearby people that are members of the unlink-neighbors set (i.e., those to whom person A is already connected - this set including person B):
ifelse count turtle-set people-nearby > 1
[ let nearby-people-not-linked-to-me ( turtle-set people-nearby with [ not member? self [ turtle-set unlink-neighbors ] of myself ] )
set people-nearby nearby-people-not-linked-to-me ]
[ set people-nearby [ ] ]
For some reason this error keeps popping up:
"WITH expected input to be an agentset but got the list [(person 0) (person 1) (person 3) (person 4)] instead." whenever
people-nearby with [ not member? self [ turtle-set unlink-neighbors ] of myself is called.
I looked up so many posts but can't seem to get the form of the argument right so that it stops showing this error.
Can anyone help me fix this please? (Oh and it's my first post so apologies if I haven't set up the issue properly)
When you submit code, try to submit what is needed to recreate your problem- check out the asking help page, and specifically the section on helping others reproduce your problem. As is, I think your problem comes from using turtle-set. That primitive is mostly used to combine agentsets, not to query them. So in your line:
( turtle-set people-nearby with [ not member? self [ turtle-set unlink-neighbors ] of myself ] )
there is an syntax issue related to turtle-set. The error itself is saying that you have not returned an agentset but a list of agents, which behave differently.
If I understand correctly, you want all people to have a variable that contains all people within a radius of themselves: "people-nearby". Then, you want the people to form a link with one of their "neighbor" turtles. Finally, you want the people to update their "people-nearby" variable to exclude the person to whom they just formed a link. Below is some code with comments where I tried to follow those steps- obviously your variables will be different, but it may get you started. Let me know if I need to clarify anything or if I missed a step.
breed [ people person ]
turtles-own [ people-nearby ]
to setup
ca
reset-ticks
create-people 70 [
setxy (random 30 - 15) (random 30 - 15)
]
; do this after all turtles have spawned
ask people [
set people-nearby other people in-radius 3
]
end
to create-links
let num-links 10
;; Create a temporary agentset out of turtles that have people nearby
let turtles-with-neighbors turtles with [ any? people-nearby ]
; ask some number of the temporary agentset:
ask n-of num-links turtles-with-neighbors [
;; This just makes it easy to identify the turtle that causes the link
ask patches in-radius 3 [
set pcolor white
]
; create a link to one of the nearby people
create-link-to one-of people-nearby
; newly set people-nearby to only include turtles in radius
; that are not linked-to from the currently acting turtle
set people-nearby other people in-radius 3 with [ not member? self [ out-link-neighbors ] of myself ]
ask people-nearby [ set size 0.5 ]
]
end

NW: extension turtle-in-radius

I have created a graph to mirror that of a road network (i.e. G = (V, E)). This comprises a number of junction, terminus and intermediate nodes (V). Terminus nodes have 1 link (E), intermediate nodes 2 and junction nodes more than 2.
What I am trying to do is identify the separate parts of the graph that form connections between either terminus-junction or junction-junction nodes. I was thinking of using nw:turtles-in-radius to do this, but this requires a fixed search range to be specified. I was wondering does anyone
have an idea how to identify how far other junction/terminus node are
away from the searching node, such that i can specify it in the turtles-in-radius function?
or have an idea for a better way of identifying the network sections?
Once I have identified these sections I will then store the turtles located along them in a list for later operations.
I don't think nw:turtles-in-radius will help you much, here. It's not a simple problem. I found a fairly convoluted way of doing it. Perhaps someone else will come up with something simpler.
The setup is just there for testing:
extensions [ nw ]
to setup
clear-all
; generate a simple network for testing
nw:generate-ring turtles links 5
ask n-of 2 turtles [
hatch 1 [
create-link-with myself
hatch 1 [ create-link-with myself ]
]
]
ask turtles [ set label who ]
repeat 1000 [ layout-spring turtles links 0.2 5 1 ]
end
The rest is made from a bunch of reporters that compose in a fairly functional way:
to go
let nodes [ self ] of turtles with [ not is-intermediate? ]
let sections unique-sections reduce sentence map my-sections nodes
foreach sections print
end
to-report my-sections [ node ]
report map [ section-from node ? ] [ sort link-neighbors ] of node
end
to-report section-from [ n1 n2 ]
report ifelse-value [ is-intermediate? ] of n2 [
fput n1 section-from n2
[ one-of link-neighbors with [ self != n1 ] ] of n2
] [
list n1 n2
]
end
to-report is-intermediate?
report count my-links = 2
end
to-report unique-sections [ all-sections ]
let sections []
foreach all-sections [
if not member? reverse ? sections [
set sections lput ? sections
]
]
report sections
end
You can drop the call to unique-sections if you don't need them to be unique.
First thanks to Nicolas.
In the end, after looking at graph theory a bit more, I decided to use the weak component cluster route. To identify clusters i set the context as only nodes with two connections. So removing junctions and terminus nodes. I then used nw:weak-component-clusters. This gave me a list of turtles present in each component. I then iterated over this list and gave each turtle-set a unique identifier. I now have a list of nodes which knows who else it links with.