Having turtles verify if they own a link / have a neighbor turtle - netlogo

Is there a way for turtles to verify if they have links or neighbors?
I need a way for turtles to check if they have a link. If the turtle does, then I need it change the variable SocialST to 1. If the turtle does not, then it should skip. Here is what I have devised so far...
to SocialStructure
ask turtles with [link-neighbors]
[if (abs([SN] of self - [SN] of one-of link-neighbors) >= Soctol ) [Set SocialST 1]]
end

Making minimal changes to your code:
to updateSocialStructure
ask turtles with [any? link-neighbors] [
if (abs([SN - [SN] of one-of link-neighbors) >= Soctol )) [Set SocialST 1]
]
end
But do you really want one-of?

Related

Netlogo Creating a Link with a turtle when they both hit the same patch

Maybe i worded the question wrong but what i want to do in the code is when another turtle meets another turtle they create links with each other
I know its
to go
tick
make-circle
move
if ticks >= timer1 [stop]
end
to move
ask turtles
[
create-links-with other turtles ;here is where i want to put the code
set heading random 360 fd 1]
create-network
end
to create-network
ask links [
set thickness 0.01 * counter
if [patch-here] of end1 = [patch-here] of end2
[set counter (counter + 1)]
]
end
but im not sure how to word it correctly to link when they meet how do i do that
Establish a breed variable for your counts. Then create a link between all turtles that are on the same patch with other turtles-here. Then increment that count variable for when the others turtles have met the original calling turtle. I'll note that I increment the count variable by .5 because each turtle in the link will increment it (there's 2 turtles, so .5 * 2 = 1).
links-own [meets]
to setup
clear-all
crt 100 [setxy random-xcor random-ycor ]
end
to go
ask turtles [fd 1]
ask turtles [rt random 90]
ask turtles [ create-links-with other turtles-here]
ask turtles [ ask other turtles-here [ask link-with myself [ set meets meets + .5]]]
end

Initialize Netlogo world with two breeds; only one turtle per patch

I'm trying to set up a world in Netlogo where there are two breeds, but there is only one turtle per patch:
breed [supras supra]
breed [subs sub]
turtles-own [age]
subs-own [status]
to setup
clear-all
;; Color the patches so they're easier to see
ask patches [ set pcolor random-float 2 ]
;; num-turtles patches will sprout one turtle each
ask n-of (num-turtles / 2) patches [
if not any? turtles-on patch-set self [
sprout-subs 1
]
]
ask n-of (num-turtles / 2) patches [
if not any? turtles-on patch-set self [
sprout-supras 1
]
]
;; Set breed colors and own-variables
ask subs [
set color blue
set shape "dot"
]
ask supras [
set color pink
set shape "dot"
]
reset-ticks
end
to go
ask turtles [
fd 1
]
tick
end
This seems to work but I can't quite tell if it's technically correct. What would be a good test to write to make sure I don't have some patches with multiple turtles on initialization?
I am actually going to suggest a different approach; instead of randomly selecting some patches for one breed and some patches for the other and trying to avoid each other, you can just select the full number of patches to sprout initially and then convert half your turtles into the other breed.
globals [num-turtles]
breed [supras supra]
breed [subs sub]
turtles-own [age]
subs-own [status]
to setup
clear-all
set num-turtles 99
ask n-of num-turtles patches [sprout-subs 1]
ask n-of (num-turtles / 2) subs [set breed supras]
<procedures to set colours etc>
end
Try to strip your code down to what is needed for a complete example.
globals [num-turtles]
breed [supras supra]
breed [subs sub]
turtles-own [age]
subs-own [status]
to setup
clear-all
set num-turtles 99
;; num-turtles patches will sprout one turtle each
ask n-of (num-turtles / 2) patches [sprout-subs 1]
ask n-of (num-turtles / 2) patches with [not any? turtles-here] [
sprout-supras 1
]
end
to test-setup
if (int (num-turtles / 2) != count supras) [error "setup error: supras"]
if (int (num-turtles / 2) != count subs) [error "setup error: subs"]
if any? patches with [count turtles-here > 1] [error "setup error: patches"]
end

How to set myheading equal to leader's heading in Netlogo

what i'm trying to do is:
Pseudo code
to flock
check flockmates
if find any leader inside the flockmates
change myheading to leader's heading
else
follow flocking rule [separate, allign, cohesion]
end
Below is code that I use.
turtles-own
[ flockmates
nearest-neighbor
leader?
leader
]
to setup
__clear-all-and-reset-ticks
ask n-of population patches with [ pcolor = blue]
[sprout 1
[set color white
set size 0.6
set leader? false]
]
choose-leaders
end
to choose-leaders
ask n-of ((percent_of_leader / 100) * population ) turtles
[set leader? true
set color black
set size 0.6
set leader self
]
end
to go
ask turtles [flock]
end
to flock
find-flockmates
let nearby-leaders turtles with [leader? ]
ifelse any? nearby-leaders
[ set heading [heading] of nearby-leaders]
[ find-nearest-neighbor
ifelse distance nearest-neighbor < minimum-separation
[separate]
[ if any? flockmates
[align
cohere ]]]
end
to find-flockmates ;; turtle procedure
set flockmates other turtles in-cone vision vision-angle
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
however when I run the code, this error message pop-up can't set turtles variable HEADING to non-number [147]. and it point to this code [set heading [heading] of nearby-leaders]. what did i do wrong here? really appreciate if someone can help.
Because nearby-leaders in your code is a turtle-set, you should use one-of:
set heading [heading] of one-of nearby-leaders

How a patch to display the number of times that turtles have visited it?

I would like to show how many times a patch has been visited by turtles after the simulation.
ask patches with [pcolor = lime] [
if count turtles-here > 0
[set counter (counter + 1)]
set plabel counter
]
Something looks like that. Each patch's value will be increased when a turtle visit it. In the end of simulation, each patch will show the different number of times that turtles have visited it. Thanks.
Your solution seems fine. You just need to give patches a counter attribute. For example,
patches-own [counter]
to setup
ask n-of 50 patches [set pcolor lime sprout 1]
ask patches [count-visits]
end
to go
ask turtles [move-to one-of patches]
ask patches [count-visits]
end
to count-visits ;; patch proc
if (pcolor = lime) [
if count turtles-here > 0 [
set counter (counter + 1)
]
set plabel counter
]
end

How to avoid turtles revisiting the patch on which they were last time?

Turtles stay on patches for 60 ticks, and then move to another target patch. How to avoid turtles revisiting the patch on which they were last time? Thanks
Hi Seth and Frank,
Thank you very much for your reply. I am sorry I did not describe the questions in detail.
Turtles will not visit the patch that they were on the last tick, and will move to another nearest patch instead in next tick. The following codes mean they find the nearest patch, and move on to it.
What I would want to do is the turtle will find the nearest patch again in the next tick. They will move to other alternative that is nearest to themselves, if the nearest patch is still the same one that they were on the last tick. Thanks
let closest-leaf min-one-of (patches in-radius 1 with [pcolor = lime]) [distance myself]
face closest-leaf
fd distance closest-leaf
A good way is to have a turtles-own variable of patches visited that can be maintained (remember to initialize it to the empty list when you create the turtle).
turtles-own [ patches-visited ]
to setup
...
ask turtles [ set patches-visited [] ]
...
end
to move
let potential-targets filter [ not member? ? patches-visited ] target-patches
let target-patch one-of potential-targets
if target-patch != NOBODY [
set patches-visited fput target-patch patches-visited
; move to target patch
]
end
Add a get-target reporter.
to-report get-target ;;turtle proc
let %close patches in-radius 1 with [pcolor = lime and self != [patch-here] of myself]
ifelse (any? %close) [
report min-one-of %close [distance myself]
] [
report nobody
]
end
Now you can easily change your mind about how to pick a target. You can then move to the target (if one exists).
to move ;; turtle proc
let %target get-target
ifelse (%target = nobody) [
;handle this case
] [
face %target move-to %target
]
end