Constraining Movement of Agents to a Home Range in Netlogo - 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

Related

How to specifically change a variable of an agent in the agentset?

I am trying to change a variable (score) of a particular agent in the agent set if it meets the specific condition of a patch. This is called by an another agent. To be more clear. My idea is for instance if there is a breed (horse) and it sees the patch (grass) and it is standing on another breed (vertices - since horse move along a path connected by nodes represented by vertices) - a score variable to added to vertices-own where if the grass quality <=3, it would add a score to the vertex on which it stands.
ask horses[
ask patches in-cone 50 60 [
if grass-quality <= 3 ask vertices with [min-one-of vertices in-radius 0 [distance myself] [set vertex-score vertex-score + 1 ]]]]
I know something is wrong with this code logic. I am trying to convert my mentioned thought into codes. Kindly suggest me.
Thank you all.
Regards,
Heng wah
NetLogo agent (turtle) positions are continuous numbers so it is generally wrong to try and say something like 'if another turtle is where I am'. While you may have got there using move-to, it's probably safer to have the horse identify a vertex that is very close to it rather than in the exact position. You have used radius 0 but I'm going to change that to 0.001 to allow for potential errors in position.
ask horses
[ if any? patches in-cone 50 60 with [ grass-quality <= 3 ]
[ let my-vertex min-one-of vertices in-radius 0.001 [distance myself]
ask my-vertex
[ set vertex-score vertex-score + 1 ]
]
]
]
This is not tested, but I have simply reorganised your code. You had some bracketing issues and you were also asking vertices to find the closest vertex (which would have been itself), rather than having the horse find the closest vertex.
It's also not necessary to separate the let and the ask but I thought that would be easier for you to see how it works.

NetLogo: distance refering agent

I'm new in NetLogo and this may be a too obvious question, but I don't see how to test if what I am doing is right.
I'm making a selection of an agent of breed1 (turtles) based on its distance to breed2 (crocodiles). I would like the crocodile to choose one turtle randomly from the ones around it, but with a higher probability to be chosen the closer the turtle is. Thus, I am using rnd extension and distance command.
My question is if the distance command is referring to the right agent (i.e. distance between the crocodile and the turtles):
ask crocodiles [
let potential_preys turtles in-radius max_distance
let selected_prey rnd:weighted-one-of potential_preys [ (1 - ( distance ? / max_distance ) ) ]
ask selected_prey [
scape
]
]
Before I get to your question, there is another problem I noticed with your code.
I had never realized this before, but NetLogo's semantics can make it tricky to model actual turtles! (At least when other breeds are involved.)
What I mean by this is that turtles refer to all turtles in the model, regardless of their breed. It means that, in your case, crocodiles are included in turtles, so when you say:
let potential_preys turtles in-radius max_distance
...crocodiles can be included in the potential preys!
Getting around this is easy enough, though: just choose another name for the breed that represent actual turtles in your model, e.g.:
breed [ tortoises tortoise ]
And then you can write:
let potential_preys tortoises in-radius max_distance
And now, for your question regarding distance, I think what you want is the distance to myself, where myself would be the crocodile that is selecting its prey. The myself primitive refers to the agent in the "outer" context of the block where you use it, i.e., the "calling" agent.
This gives you something like:
let selected_prey rnd:weighted-one-of potential_preys [
1 - (distance myself / max_distance)
]
Haha, I didn't thought about the turtles detail, indeed... ^^
Anyway it was an example, not the actual name of my breeds, so no problem, but thanks for noticing it!
Regarding the question itself, I also think myself will do, so I'll keep it like this, but now with higher confidence :D
Thanks Nicolas!

Netlogo - How to move a turtle to top?

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.

Is there an else command in netlogo?

I am trying to create a program in netlogo where there are blocks that come down the screen and when their y-coordinate reaches a certain value they reverse their direction and move in the opposite way.
So far I was able to make them move in one direction and then switch directions when they reach the critical y-coordinate value, but once they take one step in the reverse direction it glitches and they get stuck moving one step forward and one step backward.
I wanted to know if there was an else command in netlogo so I could specify that if the while command wasn't fulfilled it could reverse its direction and move without glitching.
Here is my code.
to maze
while [abs pycor < 16 ] [fd 1 wait .1]
bk 1 wait .1
end
There is no separate else keyword in NetLogo, but the ifelse command allows you to specify two blocks: one that is executed if the condition is true, and another (the "else" block) that is executed if the condition is false.
It seems, however, like you should rethink your general approach to the problem. Turtles in NetLogo always face in a particular direction, and you could take advantage of that: instead of having them "back up", you could have them turn around.
Also, it's generally ill-advised to try to do things in a while loop. If you want your turtles to repeat a behavior, a "forever button" is usually the way to go.
In the following example, you should call the go procedure from a forever button:
to setup
clear-all
ask patches with [ pycor = max-pycor - 1 ] [
sprout 1 [
set heading 180 ; head down
]
]
reset-ticks
end
to go
ask turtles [
if abs pycor = max-pycor [
rt 180 ; turn around!
]
fd 1
]
tick
end
This probably doesn't achieve exactly what you wanted, but there is a good chance that you can modify it to fit your needs.
Also note that this will work better using tick-based updates.

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