How to make agent stay at certain patch in netlogo within certain ticks? - netlogo

I'm doing code for making agents roaming around the world to forage.
After they find it, they should stay at the patch where they find the food for a certain time. But I have a problem to make them stay on the food patch for a certain time.
Certain time: each turtle has its own variable "s", and "s" is a random number generated by NetLogo and the number shouldn't change within each tick, and I have a problem to make "s" doesn't change.
For example, turtle 1 has "s"=60, after turtle 1 find the food, it will stay on the food for 60 ticks. After turtle 1 has stayed for 60 ticks there, it will leave the food and the value of "s" will be reset again to another random value. And each turtle has a different value of "s"
I tried to use timer coding, but the timer only decreases each time the agent come across the food (like they just walk on the food instead of staying there) I can't use bk 1 or fd -1 command because the turtle just keeps on moving forward after they walk backwards for 1 tick.
Any help is appreciated.
here is the code:
turtles-own
[
food
location
s
]
patches-own
[
food-quality
]
to go
move
forage
end
to move
ask turtles
[fd 1]
end
to forage
set s random 100
ask turtles
[ [ if food < food-quality ;meaning that turtle will only keep the best food's value each time they found food
[set food food-quality ;food-quality is patch-own, the food's value
set location patch-here]
if s >= 50
[stay] ]
]
]
end
to stay
set s s - 1 ;decrement-timer
if s = 0
[move
set label ""
reset-s
]
end
to reset-count-down
set s random 100
end
Edit: making the question clearer and update some code.

I think you have a logic problem rather than a coding problem, but it's not particularly clear. If your variable 's' is supposed to be something like a stay-timer (much more descriptive name), then don't set it at the beginning of the forage procedure. Instead, set it when the turtle finds food and then only move/forage if it's not being held by the stay-timer. You also don't have tick anywhere in your code, so you are not advancing the clock. I think you want something like this:
to forage
ask turtles
[ if food < food-quality ; has found food that is more than currently owned
[ set stay-timer random 5 + 20 ; sets number of ticks to stay between 5 and 24
set food food-quality ; food-quality is patch-own, the food's value
set location patch-here
]
set stay-timer stay-timer - 1
]
end
to go
ask turtles with [stay-timer = 0] [ move ]
forage
tick
end

Related

How to choose the second highest value of a patch variable in NetLogo 6.2?

I'm having difficulty doing the following code: I have a piece of code that turtles inside an in-cone choose a patch that has resource > 30. When, the patch that turtle was the one that had the highest value a turtle did not move. So I put the line of code using the "other" command. However, what happens now is that if the patch the turtle is in has the highest resource value, it chooses another patch that has resource > 30. The problem is that there is, for example, a patch on the turtle's side that has resource value = 51 and another one that has a value of 31 and she chooses 31. What I would like to implement is: if the patch the turtle is in is the one with the highest resource value (and the turtle has already collected this resource) she would choose another neighbor patch that had the second highest resource value. I tried using max-one-of but got an error: "MAX-ONE-OF expected 2 inputs, an agentset and a number block.
Does anyone have any ideas, how can I solve this?
Thanks in advance
to go
ask turtles
[
let availablePatch patches in-cone 5 90 with [ resource-value > 30 ]
ask patch-here [ set availablePatch other availablePatch ] ;; remove the patch it is in, because if the patch it is in is the one with the highest value within your range of vision, the turtle does not move
; ask patch-here [ set availablePatch other max-one-of [ availablePatch ] ]
let neighAvailable count availablePatch
ifelse neighAvailable = 0
[
move-around
]
[
let target max-one-of availablePatch [ resource-value ]
face target move-to target
set step-count step-count + 1
]
]
end
to move-around
right random 360
let step-length 2
forward step-length
end
As is often the case with new programmers, you are too far caught up in a specific thought pattern. So you are making the problem really technical and the code bloated, when code should always reflect what you are trying to do. What you want is simple, so the code should be simple. try to zoom out and think of other options.
If I understand correctly, turtles should pick high-resource patches to exploit them/gather their recources. But they should not pick the same patch twice.
possible solutions that make sense:
-after a turtle has exploited a patch, resources should be below 30. that way it won't be a candidate. if it is not below 30, moving doesn't seem to make a lot of sense anyway.
-use a patches-own variable "exploited" that you set to "true" after the turtle has moved there, and to "false" after the turtle has left. Then you can use with [ resource-value > 30 & exploited = false ] instead of the current with check.

How to kill and regenerate turtles every set number of ticks

The model I am working on simulates a group of workers working on a team project. I am trying to add a on/off switch titled "replacement." The intended outcomes are:
When it is on:
30% of existing turtles of breed employees die, and equal number of new turtles are introduced.
When it is off:
No turtles die, and all turtles generated at setup will work on the project from start to finish.
The purpose of killing off the turtles is the turtle-own variable "skills." I am trying to simulate a scenario where people resign from a company and new people are hired, which will result in a change of skill level.
If it is easier to change the skill level of 30% of turtles every 20 ticks instead of killing/regenerating, that would be perfect, but I was unable to figure it out.
My attempt at this problem is shown below. It runs without any errors, but I confirmed that it's not killing/respawning 30% of turtles every set number of ticks. Any helpful guidance will be much appreciated.
to go
if all? patches [ workload = 0 ] [ stop ]
ifelse replacement [hire] [continue]
recolor
tick
end
to hire
if ticks > 0 and ticks mod 20 = 0 [
ask n-of (count employees * 0.30) employees [die]
create-turtles number_of_workers * 0.30 [
set breed employees
setxy random-xcor random-ycor
set shape "person"
set color black
set size 1
set skills random-float 1]
]
ask employees [move]
ask leaders [move]
end
to continue
ask employees [move]
ask leaders [move]
end
I ended up finding the answer on my own. Removing "ticks > 0 and" from the hire function solved it :)
From your description of the problem, I am not sure why removing the if ticks > 0 and solved it. I am wondering if you perhaps had the "replacement" variable set to false, and the hire procedure would not have been called.
Regardless, you also asked about just changing the skill set every 20 ticks. You could do this:
to go
if all? patches [ workload = 0 ] [ stop ]
if replacement? and ticks mod 20 = 0 [hire]
move-people
recolor
tick
end
to hire
ask n-of (count employees * 0.30) employees
[ set breed employees
set skills random-float 1
]
end
to move-people
ask employees [move]
ask leaders [move]
end
I also reorganised slightly in a way that is intended to make it easier to debug. You had the moving in both the "hire" and "continue" procedures. Clearly you want that to always happen, so that is now explicit in the "go" procedure (and I changed the name to be more descriptive).
Now the "hire" procedure simply changes the skills of existing employees. Note, however, that any other variables (such as leader, size, colour and position) are not affected. If you go this approach, you may want to indicate in some way that they are new hires.
The "hire" procedure is now clearly called when the "replacement?" switch is on and also a multiple of 20 ticks. If you have part of the condition in the calling procedure ("go") and part in the procedure itself ("hire") then it's easy to forget the existence of whichever half you are not looking at.

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.

In NetLogo how agents actions are performed?

In my go function I have a few conditions which might be true and then call an action, if more than one condition are true does it mean in one tick agents can do more than one action ? or in another case I have a function that agents have to move to a target and after facing the target agent should find the distance and fd 1 until reaches the target, does it mean it should take n ticks to complete?
to move [t]
face t
let n distance self t
repeat n
[fd 1]
end
to go
action 1 = > [move]
action 2
action 3
tick
end
I need the agents to do only one task at the time, and I am not sure how to make sure that for example not all the agents have the same target! sorry if these questions are out of context but I am new to Multi-agent modeling.
This is a great question!
First, specific answers:
if more than one condition are true does it mean in one tick agents can do more than one action ?
Just to be clear, are you talking about a situation like this?
to go
ask turtles [ move ]
tick
end
to move
if xcor > 5 [ fd 1 ]
if ycor > 7 [ rt 15 ]
if color = red [ bk 2 ]
end
If so, then yes, if xcor > 5, ycor > 7, and color = red are all true, the turtle will move forward 1, turn right 15, and move backward 2. The main way to prevent this is by using a sequence of ifelses:
to move
ifelse xcor > 5 [
fd 1
] [
ifelse ycor > 7 [
rt 15
] [
if color = red [ bk 2 ]
]
]
end
That way, the second condition (ycor > 7) will only be looked at if the first condition (xcor > 5) is false. Similarly, the third condition will only be looked at if the first two fail.
I have a function that agents have to move to a target and after facing the target agent should find the distance and fd 1 until reaches the target, does it mean it should take n ticks to complete?
I would recommend taking n ticks to complete it. Otherwise, only one turtle will move to its target at a time! However, repeat n [ fd 1 ] will make the turtle go forward n all at once. In fact, it's the same as fd n. The repeat block will finish before the turtle is done performing the move procedure.
You can edit your move function as follows to get it to take n ticks:
to move [t]
face t
fd 1
end
Then, just have a condition to look for when the turtle gets to its target (for example, distance t < 1) and then do something accordingly.
I recommend just messing around with these various techniques with multiple turtles using them so you can get a feel for the differences.
I am not sure how to make sure that for example not all the agents have the same target!
This depends quite a bit on context. Supposing there is a turtles-own variable called target that stores each turtle's target, you could do something like this:
to-report get-available-target [ possible-targets ]
report one-of possible-targets with [ not any? turtles with [ target = myself ] ]
end
get-available-target will report a random agent from possible-targets that is not anyone's target. one-of just gets random agent from an agentset.
How can I schedule tasks in netlogo, I have conditions and actions but how can I make the agents do one task at the time?
Often this just takes care of itself. For example, say, when an agent is hungry, it should walk over to a food supply and eat. As it's walking over, you don't want it to go off and do anything. However, it will still be hungry, so if you've used an ifelse, it will just keep going to the food supply. Thus, your agent stays focused for free! In general, it's best to design your conditions such that they remain in effect until the agent addresses them. You can order them in an ifelse chain or something similar to establish the general priority of actions (for instance, if the agent walking to the food supply is threatened by a predator, it should still run away).
If you really want to the agent to do something for several ticks, you'll basically just extend this same idea, but in a more artificial way. For instance, if an agent should walk forward three times, have a turtles-own variable set to three. If that variable is greater than zero, the agent walks forward and decreases that variable, and doesn't do anything else.
#Bryan Head already answered your question, I had the same problem when I started Netlogo and agent based modeling, So I have added a current-task variable and in my Go Procedure I have added a function to check all the conditions and set the task of agents and when the task is completed the variable is set to "" again. This might be useful for your case as well.
if current-task = "move" [move]
if current-task = "A1" [A1]
if current-task = "..." [...]
if current-task = "" [select-current-task]

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.