How to get the value of a property of a random turtle? - netlogo

I am creating an agent based model wherein I have a Boolean property called feature? for every turtle/agent. I need to set/ copy the value of feature? of a random turtle to another.
How do i achieve this? How do i complete this code:
ask turtles[
set feature? one-of other...

You could try
ask turtles [
ask one-of other turtles [set feature? [feature?] of myself]
]
But this asks all turtles to transfer their feature? to a random turtle other than themselves, which means that their own feature? could be reset by other turtles before they transfer it to another, and any given turtle could receive a feature? from more than one turtle. Is that what you want, or are you asking only certain turtles to do this?

Related

NetLogo : getting turtles to direct-link if a condition is true

I'm trying to implement YOYO leader election algorithm in netlogo
first step in this algorithm is to orient links ( direct link )from the minimum to the maximumbut between neighbors only !
I tried the command
[`ask turtles with [ [ who ] of self < [who] of one-of link-neighbors ]
create-direct-to turtle [who] of one-of link-neighbors ]`
this creates direct link from min to max ( neighbors ) but also creates a direct link from max to min ( neighbors )
and I don't know what's wrong :(
here's a screenshot , if you notice theres' a direct link from 0 to 2 and also from 2 to 0 and my goal is to have only from 0 to 2
Your problem is that every time you do one-of, it randomly selects. So you test against a random link-neighbor in the first line, find it's true and then randomly select a link-neighbor to connect to.
[ ask turtles with [ [ who ] of self < [who] of one-of link-neighbors ]
create-direct-to turtle [who] of one-of link-neighbors
]
More generally, this seems an odd way to achieve your goal. To start with, link-neighbors are the turtles that the turtle is already linked to. link is the generic name for all link breeds (I think you have created a breed called direct-link).
I am not entirely clear what you mean by minimum and maximum since your code is just from smaller to larger who value, regardless of what other who values are available. If you want to create a link from every turtle to every turtle with a higher who value, here is some code:
ask turtles
[ let targets turtles with [who > [who] of myself]
create-links-to targets
]
In general, it is bad practice to use who in NetLogo code. who is a completely arbitrary identifier that simply tracks the order that turtles are created. If you have turtles that die, then your code may crash because it is referring to a turtle that no longer exists. Or perhaps at some point you will have two breeds of turtles - who doesn't care if your turtle is a person or a dog or a factory or...
This may be one of the very few exceptions, but you might want to think about what you are intending who to mean. For example, as this is a leadership model, perhaps you could have a variable called 'charisma' and all the links are from turtles with lower values of charisma to higher values of charisma.

How to have patches "belong" to turtle

I am brand new to Netlogo and am coding a world with caching animals. They will go to their caches (I set them as blue patches) if their energy value falls below 10. They can find these caches based on a random 'memory' value given to them which is used as an in-radius value. That way, they will face and go towards a cache within their in-radius memory if they are about to die. I am starting to incorporate a home-base system where the turtle remains in a smaller area with their own caches. Does anyone know how I can make a patch belong to an individual turtle? This would allow turtles to have their specific caches in their territory. I have looked into using links and breeds, but links are only between turtles and making the individual breeds for the 50+ turtles at a time seems ineffective and complex. I apologize I don't have any code attempting to have a cache (patch) belong to a turtle, I don't know where to start. Any ideas?
If you want a turtle to remember anything (patches or income or anything else), then you need to assign a variable in a turtles-own statement and then set the value appropriately. Here's some example code fragments. They won't work, and you actual code would likely look a lot different because you will need some design about the conditions under which a cache will be assigned, but they show you what a variable solution looks like.
turtles-own
[ my-caches
]
set my-caches (patch-set my-caches patch-here) ; code when a turtle finds a new cache site
If you want a patch that belongs to a turtle to make that patch unavailable to other turtles, then also set up a patch variable to store its owner.
patches-own
[ my-owner
]
ask turtles
[ if [my-owner] of patch-here = nobody [set my-caches (patch-set my-caches patch-here)]
ask patch-here [set my-owner myself]
]
I suggest you do several NetLogo tutorials, then look at some library models (and understand them) before starting your own model. You need to understand basic concepts like turtles/patches, variables, ticks before trying to build a model.

How can attribute of one node of a link be copied from the other node of the same link?

I would like to create a link between a turtle of one breed and another turtle of another breed, and then to copy the value of an attribute of the turtle at one node of the link into an attribute of the other turtle at the other node of the same link.
I tried:
to go
ask one-of apples [
create-link-to one-of oranges
ask-mood]
end
to ask-mood
set others-mood [my-mood] of other-end
end
And (obviously) got the error message "Only a link can get the OTHER-END from a turtle."
Please could you advice the code that I need? Thank you.
It's difficult to give an answer without a working example. You have a bit of a conceptual problem - if two edges link to the same turtle, which turtle is supposed to provide the value for the others-mood? In the following code I have ignored that and just randomly selected the one to provide the value.
You need to change contexts by selecting a link and then the other end of the link is available. See the following for a full model that you can adapt to your code.
turtles-own [mood others-mood]
to setup
clear-all
create-turtles 10
[ setxy random-xcor random-ycor
set mood random-float 1
]
ask turtles
[ create-link-to one-of other turtles
set others-mood [mood] of [other-end] of one-of my-links
]
end

getting turtles to consider other turtle's variables

I am new to Netlogo and have become stumped with a problem.
I have written a model where one breed of turtles (wombats) randomly chose to move-to different burrows (a second breed of turtles). However, I now wish to make it so that the quality (represented by a number) of the burrow currently on the same patch as a wombat influences the probability of the wombat moving to a different burrow. I had envisaged this to look similar to:
ask wombats[
if random-float 100 >= burrow-fidelity * ( quality one-of burrows burrows-here / 2)
[move-to one-of burrows with [not any? wombats-here]]
]
however this does not work.
Is there anyway that I make the wombat report the size of the burrow currently sharing the same patch (there is only ever one burrow per a patch) and then make the wombat incorporate the reported value into the above equation where "quality one-of burrows burrows-here" is?
Thank you in advance
To report the value of another agent's variable, use the of keyword:
[ quality ] of one-of burrows-here

Leader selection from crowd of turtles NetLogo

Is there any way that i should ask / make the circled one turtles( robots ) that you are leaders now (i.e. setting is-leader? true for them ). As i am using Robots-own [ is-leader? ]
For reference please see this image.
Given your elaboration on your goal in your comments, but noting that Seth is (as usual) completely correct in his warnings, you could try the following:
turtles-own [is-leader?]
to setup
ca
ask n-of 50 patches [sprout 1]
choose-leaders
end
to choose-leaders
ask max-n-of 5 turtles [count turtles in-radius 3] [
set is-leader? true
]
end
I can see no commonality between the circled turtles, except that they are circled. I will therefore interpret the question to ask about how to interact with turtles via the GUI. If you right click on a turtle, you can pick it from a menu, and then choose "Inspect" from the next menu. This will bring up a dialogue box that includes all the turtle's attributes, including its custom attributes. You can use this to set its is-leader? attribute to true.
hth.