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.
Related
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
I have a programme that sets up a number of different breeds of turtles. Each breed needs to have a leader turtle and a follower turtle. I have assigned these as global variables as they come up a lot in the code further down.
I need to assign these variables to turtles in the breeds and then create a link from the leader to the follower. There are a lot of conditions in the interface that determine how many and which breeds are created so i cannot assign by turtle number.
I am receiving an error (not all of the time) 'turtle cannot link with itself' which i presume occurs when they overwrite the first set command and assign the same turtle to the two variables. Does anybody know a condition i can put in that will allow it to set up everytime without the error. ( I have tried if statements, is-turtle?, one-of other, other)
breed [flinks flink] ;; linked turtles that will turn away from sources
globals [
flink-leader
flink-followers]
to set-up
clear-all
setup-turtles
reset-ticks
end
to setup-turtles
create-flinks 2 [
set flink-leader one-of flinks
set flink-followers one-of other flinks
ask flink-followers [create-link-with flink-leader]
ask flink-followers [set color pink]
ask flink-leader [
setxy 10 4]
ask flink-followers [
setxy 19.5 4]
]
end
to go
fd 1
end
There would be many different ways to approach this. Here is one that doesn't stray too far from the code you have provided:
to setup-turtles
create-flinks 2
set flink-leader one-of flinks
ask flink-leader [
set flink-followers one-of other flinks
setxy 10 4
]
ask flink-followers [
create-link-with flink-leader
setxy 19.5 4
set color pink
]
end
Note that your intuition about using other to make sure that the follower(s) is/are different from the leader was correct.
To understand what was going on, you need to grasp the notion of "context" in NetLogo. Some primitives, like ask, of and create-turtles, are "context switching": one of their argument is a code block (the part between [ and ]) that runs in the context of a particular turtle. Other primitives depend on the context in which the code is running: the primitive named other, for example, will report all the agents from a given agentset, except the one in the context of which the block is running.
In your version, you wrapped most of the code inside a code block provided for create-flinks. That meant that the code block was run once for each turtle that was created. So your calls to set flink-leader, set flink-followers and so on were all run twice, each time in a different turtle context. Can you see how that was messing things up?
Keeping track of the different context in NetLogo can be challenging at first (the frequent confusion between self/myself being a case in point), but once you get it, it should become easy and natural.
One last point as an addendum. You say:
i cannot assign by turtle number
Good! Never¹ assign anything by turtle number! It leads to brittle, error prone, more complex, less general, unnetlogoish code. If you think you need to use turtle numbers anywhere in your code, come ask another question here. Someone will most likely suggest a better way to do it.
¹ Well, almost never.
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?
For example, I have 10 turtles at one patch, how do I move a specific turtle (turtle with [color = red]) to the top?
Thank you for your help!
I will assume that the question is about the "z-order" of the turtles and that "moving a turtle to the top", means "have it painted on top of the other ones".
There are two factors that determine the painting order in NetLogo: breeds, and ẁho numbers. Breeds have precedence. As per the Breeds section in the Programming Guide:
The order in which breeds are declared is also the order in which they are layered in the view. So breeds defined later will appear on top of breeds defined earlier;
Turtles within the same breed are painted in their order of creation (inferable in NetLogo by their who number): the older ones are painted first, and the newer ones are painted on top.
The order of creation is not modifiable, but if nothing in your code is holding on to turtle references or who numbers (the latter being inadvisable anyway), you could use hatch to create a clone of a turtle and then kill the old one immediately. For example:
to setup
clear-all
create-ordered-turtles 10 [ set size 10 ]
ask turtles with [ color = red ] [
hatch 1
die
]
end
The last line would bring all the red turtles (only one in this case) on top.
What if you can't do that, for some reason? Then you can use breeds:
breed [ background-turtles background-turtle ]
breed [ foreground-turtles foreground-turtle ]
to setup
clear-all
create-ordered-background-turtles 10 [ set size 10 ]
ask turtles with [ color = red ] [
set breed foreground-turtles
]
end
You would need as many breeds as you want "layers" of turtles. This may or may not be convenient. The best approach will depend on your specific use case.
There is some ambiguity.
To move the turtle to the top of the patch
Set ycor pycor + .5
To move it to the top of the view
Set ycor max-pycor
To make it the top of the stack in the photoshop kind of way. Not so easy.
turtles are displayed in order of their who ids. Who ids can not be changed. So if you want red to be on top either create it last or have it swap values with the turtle on top. Sorry.
Hello i will try to be quick
I have a room with a fire that expands , and i have two exits , all i want to do is say to agents that if a door is blocked by fire then to go to the other one. i came up with something like this but not result.
to doorblock
show count neighbors with [pcolor = 77] ;; the patch color of the two doors
end
;;to go
ask smarts [ ;;smarts are the agents inside the room that need to get oout
if [ doorblock > 5 ]
[ set target one-of sexits]] ;;sexits is the other door
Anyone got a better idea? Thanks
OK, so if I understood correctly, you want your agents to take a look at the door that is their current target, check if that door has more than 5 fire agents around it, and choose another target door if that is the case.
If your fire agents are just red turtles (with no specific breed), you probably want something like this:
ask smarts [
if count ([ turtles-on neighbors ] of target) with [ color = red ] > 5 [
if-else ([ breed ] of target = sexits )
[ set target one-of nexits ]
[ set target one-of sexits ]
]
]
The key primitives here are:
neighbors, that will give you the patches around a turtle (the patches around target, in this case)
turtles-on, that will give you the turtles that are on members of a patch set (here, that will be the turtles that are on the patches that are the neighbors of target)
and finally, with allows you to get only the turtles from an agentset that satisfy some condition (here, we use it to get only the red turtles that represent the fires).
You should also try to understand the of primitive.
And I guessed you wanted to assign a new target that was of a different breed than the previous one (south if north, north if south) but that's up to you.