I have sorted all the turtles by the value of a property called point.
now I want a plot of point versus turtle number. how do I do this?
turtles-own [ point ]
to setup
ca
crt 100
reset-ticks
end
to go
repeat 100[
ask turtles[
if random 10 = 1[
set point point + 1
]
]
];;sorting
let array sort-on [point] turtles
tick
end
By "turtle number", I assume you mean the location in the list. Then replace let array sort-on [point] turtles with plotByOrder where
to plotByOrder
clear-plot
let pts sort [point] of turtles
foreach pts [[pt] -> plot pt]
end
Of course, you will need to have created a plot in the interface, and this assumes it is the current plot.
Related
I am new to Netlogo and learning a model is about animals moving around to eat grass based on the grazing model from NetLogo. The moving behavior is based on the biomass's richness, so it is not just impacting at right angles.
Given the conditions, I don't know how to add a monitor in my model that calculates the total distance a turtle moved by traveling each pixel.
Move specifically, I wonder if there is a way to calculate the total length of the pen-marked lines. (Show in the picture)
enter image description here
The basic setting of move is
to move-turtles
uphill-biomass
forward 1
set distance-traveled (distance-traveled + 1)
if not can-move? 1 [ rt random 150 ]
end
to uphill-biomass
let biomass-ahead biomass-scent-at-angle 0
let biomass-right biomass-scent-at-angle 35
let biomass-left biomass-scent-at-angle -35
if (biomass-right = biomass-ahead) and (biomass-left = biomass-ahead) [ wiggle ]
if (biomass-right > biomass-ahead) or (biomass-left > biomass-ahead)
[ ifelse biomass-right > biomass-left
[ rt 35 ]
[ lt 35 ] ]
end
Thank you very much for any helps!
You need to use some turtle variables with the turtles-own primitive and do the distance calculation by hand.
The easiest way to do such a calculation is to save the coordinates between a turtle's previous coordinates with turtle variables and then use the distancexy primitive (link for dictionary entry: https://ccl.northwestern.edu/netlogo/docs/dictionary.html#distancexy)
Here's a simple implementation:
turtles-own [last-x last-y distance-traveled]
move-turtle
uphill-biomass
set last-x xcor
set last-y ycor
forward 1
set distance-traveled (distance-traveled + (distancexy last-x last-y))
if not can-move? 1 [ rt random 150 ]
end
I hope this answers your question.
i am struggling with a way to find the turtle with the most neigbors in a radius of 3 and change its color. At the moment i tried something with a while loop that increases an the id of the turtles and tests if the number of neighbors is higher than the last one. But It's causing anf infinite loop and travels only between the id 0 and 1.
I can't seem to find where the error comes from, Here's the code i wrote:
to election
while [var1 < 9][
ask turtle var1 [
set voisin count turtles in-radius 3
if voisin > maxi [
set maxi voisin
set idmax var1
]
show voisin
show idmax
set var1 var1 + 1
set color pink
]
]
ask turtle idmax[
set color green
]
end
You can do this with a single statement,
ask max-one-of turtles [count other turtles in-radius 3] [set color green]
max-one-of finds the turtle with the maximum value of the expression in brackets. (If there are multiple turtles with that maximum value, then a random one of those with the maximum value is chosen.) In the brackets, each turtle evaluates the number of other turtles in radius 3. The other is not strictly necessary. Without it, every turtle will count itself, adding one to the count.
I want to create a (scale-free) network where each turtle is connected to a specific number, say four, of other turtles. To allow some turtles to have many connections while others have few (but not less than four), my thought was to let each turtle have four out-links and let in-links would necessarily vary. Using the code for building a Barabasi-Albert scale-free network available on pg. 131 in Scott and Koehler's "A Field Guide to NetLogo" as a starting point, my code is included below.
My question is how to make each turtle connnect to four other turtles.
breed [liberals liberal]
breed [conservatives conservative]
to setup
clear-all
set-default-shape turtles "circle"
create-liberals 5 [
set adopt? false
let n count turtles
set color blue
]
create-conservatives 20 [
set adopt? false
let n count turtles
set color red
]
let m 4
let p .05
set my-threshold my-threshold
ask turtles[
let me self
let degrees max-n-of m turtles [count link-neighbors]
foreach (sort degrees) [ [?] ->
let chance random-float 1.0
if ( ? != self) and (chance < p ) [
ask self [ create-link-to ? ]
]
]
]
ask turtles with [(count out-link-neighbors) = 0 ] [
let degrees max-n-of m turtles [ count link-neighbors ]
let t one-of degrees
foreach (sort degrees) [
ask self [ if ( t != self) [ create-link-to t] ]
]
]
reset-ticks
end
The code above is part of my attempt to recreate Paul Ormerod's model, available at https://onlinelibrary.wiley.com/doi/abs/10.1111/j.1468-0270.2006.00611.x
Since you are doing a directed network, you won't have any issues with simply telling each turtle to select 4 other turtles and connect to them. The code for that would be:
ask turtles
[ create-links-to n-of 4 other turtles
]
Note that this is much more difficult in undirected networks because the links they 'receive' lead to too many links overall. Then you need to do something like NetLogo Efficient way to create fixed number of links
However, this will not get to your stated goal of a preferential attachment (scale-free, Barabasi-Albert or whatever you want to call it) degree distribution. The mechanism that generates that outcome is that the turtles select the turtles to make links with using weighted random selection, with the weight a normalised degree. You need the rnd extension and that gets you the weighted-n-of primitive.
I also noted some general issues with your code that are related to (I think) confusion about how the preferential attachment algorithm works and/or how NetLogo works. The weighted-n-of primitive will get rid of your need to look at random numbers and should simplify your code a lot. However, I am not clear what let degrees max-n-of m turtles [count link-neighbors] is supposed to do, but it appears to be creating a list of the four highest degree turtles. But the preferential attachment algorithm allows links to be created with even low degree nodes, just with lower probability.
I'm trying to create a simple simulation of agents moving randomly but avoiding certain obstacles. I want them to store the coordinates of the places they've been so they don't go there again. This is part of what I have so far:
to move-agent
let move random 3
if (move = 0) []
if (move = 1)[ left-turn ]
if (move = 2)[ right-turn ]
set xint int xcor ;;here i'm storing the coordinates as integers
set yint int ycor
set xylist (xint) (yint)
go-forward
end
to xy_list
set xy_list []
set xy_list fput 0 xy_list ;;populating new list with 0
end
However, it keeps giving me a "SET expected 2 inputs" error. Can anyone help me with this?
It looks like you are incorrectly using xy_list as both a variable and a turtle variable.
I don't see the need for the xy_list procedure - Keep it as a turtle variable. Make sure xy_list is in the turtles-own list:
turtles-own [xy_list]
initialize it to an empty list when you create a turtle. eg:
crt 1 [set xy_list []]
When a turtle moves, you could add their current position as an xcor, ycor list with:
set xy_list fput (list int xcor int ycor) xy_list
You will then need to check if that coordinate already exists in the list before moving there.
However, as you are using integer coordinates, it would be a lot easier to use a patch-set to keep track of a turtle's history. You could try this:
turtles-own [history]
to setup
ca
crt 3 [set history (patch-set patch-here) pd]
end
to go
ask turtles [
let candidates neighbors with [not member? self [history] of myself]
ifelse any? candidates
[move-to one-of candidates stamp
set history (patch-set history patch-here)]
[die]
]
end
I am teaching myself how to create ABMs in Netlogo using the book of Railsback & Grimm 2012. I am having trouble with one book exercise which is on butterflies following "virtual" corridors. Basic idea is that butterflies go uphill for mating using the differences in height as guide. I need to calculate the width of the corridors dividing the number of patches used by the butterflies over the average distance the butterflies fly from the start patch to the end patch. I am
struggling with plotting this corridor width, which I am coding like this:
to-report corridor-width
let patches-visited count patches with [used?]
let mean-distance mean [distance start-patch] of turtles
report patches-visited / mean-distance
I then created a plot in the interface with the command:
plot corridor-width
The error message I get reads:
Division by zero. error while observer running / called by procedure
CORRIDOR-WIDTH called by plot 'Corridor width' pen 'default' update
code called by procedure SETUP called by Button 'setup'
I believe there is something wrong with the way I am coding distance start-patch but I have surfed the web and looked at several codes and I cannot spot my mistake. My whole code looks like this:
globals [ q ] ;; q is the probability that butterfly moves directly to highest patch
turtles-own [ start-patch ]
patches-own [ elevation used? ] ;; patches property of elevation and whether the patch has been used by butterfly or not.
to setup
ca
;; Let's create patches and asign them an elevation and color by using ask patches statement
ask patches
[
;; Elevation decreases linearly with distance from the center of hills. Hills are at (30,30) and
;; (120,120) coordinates. The first hill is 100 units high whereas the second one is 50
let elev1 100 - distancexy 30 30
let elev2 50 - distancexy 120 100
ifelse elev1 > elev2
[ set elevation elev1 ]
[ set elevation elev2 ]
set pcolor scale-color green elevation 0 100
set used? false
]
;; Create 50 butterflies
crt 50
ask turtles [
set size 6
;; set their initial location as their initial patch
setxy random-pxcor random-pycor
set start-patch patch-here
;; have the butterfly draw its path with the pen-down statement
pen-down
]
reset-ticks
;; Initialize the q parameter
set q 0.4
end
;; The master schedule
to go
ask turtles [ move ]
plot corridor-width
tick
if ticks >= 1000
[
let final-corridor-width corridor-width
write "Corridor width: " print final-corridor-width
;export-plot "Corridor width" (word "Corridor-width-output-for-q-" q ".csv")
stop
]
end
;; let's code the butterfly procedure of movement
to move
if elevation >=
[ elevation ] of max-one-of neighbors [ elevation ]
[ stop ]
ifelse random-float 1 < q ;; Decide whether to move to the highest sorrounding
;; patch with p=q
[ uphill elevation ] ;; move deterministically uphill
[ move-to one-of neighbors ] ;; or move randomly
set used? true
end
to-report corridor-width
let patches-visited count patches with [used?]
let mean-distance mean [distance start-patch] of turtles
report patches-visited / mean-distance
end
What happens when the mean-distance is 0?
let mean-distance mean [distance start-patch] of turtles
Essentially, in your setup, you set all the turtle's start-patch to their current patch. So, if you ask all the turtles how far away they are from their start patch, they will all tell you 0 units away.
So, [distance start-patch] of turtles is filled with a list of all 0's.
Thus, a mean of a list of all 0s is 0 causing your divide by 0 error.
Maybe in this situation, you want to report 0 instead...so
ifelse mean-distance = 0
[ report 0]
[report patches-visited / mean-distance]