Netlogo - How to move a turtle to top? - netlogo

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.

Related

NetLogo: Using global variables with breeds and links

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.

Constraining Movement of Agents to a Home Range in Netlogo

I'm relatively new to NetLogo, and I'm working to model moose density in New Hampshire and its correlation to winter tick parasitism.
I'd like to program my moose agents to move randomly within a set home range (~5km2), that originates from the randomly chosen patch they first enter the model on.
I'm not really sure how to bound agents based on area, rather than just patch color... Any suggestions on how to do this would be most appreciated!
Thank you!
General stackoverflow tip: typically, stackoverflow encourages specific programming questions. So including the code you've actually tried so far is generally preferred.
Alright, on to your problem.
One really simple way to do this is, first, store the mooses' starting patch. Second, when the moose is moving around, check the distance to the starting patch. If the distance exceeds the starting amount, have the moose towards the starting patch. Here's some template code to give you ideas:
breed [ mooses moose ]
mooses-own [
starting-patch
]
to setup
clear-all
;; only using one moose as it's easier to see the behavior
create-mooses 1 [
setxy random-xcor random-ycor
set starting-patch patch-here
]
reset-ticks
end
to go
ask mooses [
move
]
tick
end
to move
;; If farther than 10 patches from starting patching, take a step towards starting patch, otherwise, move randomly
ifelse distance starting-patch > 10 [
face starting-patch
] [
rt random 90
lt random 90
]
fd 1
end

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.

Count neighbors turtles of a specific patch and report true or false

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.

Turtle that has no effect on other turtles implementation but speeds up the reaction

I am using an existing model in netlogo called Chemical Equilibrium and am adding some more code. I want to add turtles (catalyst) which have no effect on the reaction/other turtles but speeds up the FORWARD reaction, which has been defined as follows:
to react-forward [t]
ask t [ set color red ]
set color green
rt random-float 360
jump 2
end
I was thinking that I should put a switch and a slider, make the turtles into whitemols or I do a turtles-own [catalyst] and then define that like I have done with temperature and pressure. I tried the following but it didnt work.
turtles-own [speed catalyst]
crt whitemols
[ set color white
randomize
set speed 1
]
I know the above code is incorrect but am not sure how to code this particular feature.
There are many ways to do this, of course. I can't tell what is going on in your program from the little snipped you include.
One way would be to have the catalyst be of a different breed:
breed [catalysts catalyst]
breed [chemical-x chemical-x]
;and so on
;then the forward reaction is sped up by the existence of catalysts
to react-forward
let num-catalysts count catalysts
;speed up by num-catalysts
;...
end