How can I save the location of a patch after I have executed some command?
example:
ask myteam[
set turtle-food-value patch-food-value; save the value of food into turtles
set location patch-here
if turtle-food-value != 0
[let x1 max [turtle-food-value] of myteam ; max for group
set best-food x1
.......
]
how to get or save x1's location? (Since x1 return number instead of location)
Thank you in advance.
What you are really wanting to do is work out which turtle has found the best food and then get both the value and location of that food from the turtle. The way you have tried to do it takes the best value, but doesn't remember which turtle had it so you don't know which turtle to query for the location. You want the primitive with-max to identify the turtle and then ask that turtle for the food value and location.
Related
I'm trying to design a model for the spread of infection from person to environment.
Turtles have a hand contamination variable that shows the percentage of their hands that are contaminated. I'd like to give this number to patches that they're passing, but I'm getting an error saying it's a turtle-only variable.
Is it possible to give a hand contamination number to the patch?
This is part of my code:
turtles-own [hand contamination]
patches-own [p-contamination]
ask patches [set p-contamination hand-contamination]
A patch can't ever refer to turtle variables directly: What if there is more than one turtle there...which one? What if there are none?
However, a turtle can access the variables of the patch it is standing on. So you would probably do this from the turtle's point of view: I think this also makes sense, logically, since it is the turtle visiting the patch, and contaminating it.
;; turtles contaminate the patch they are standing on
ask turtles [ set p-contamination hand-contamination]
Note that if there is more than one turtle on a patch, they will overwrite each other's values. So, you may need to add the amount, or otherwise blend the two values, rather than replace it.
If there are more turtles than patches, or you really want the patch to be the thing that in control, the patch can look for turtles and acess their variables with OF:
ask patches
[ let visitors turtles-here
if any? visitors
[ set p-contamination ..some expression..
So, there it depends on your needs, and you have to decide what that value is.
There is only ever at most one turtle:
[ Contamination ] of one-of visitors
Even if many turtles, pick one at random:
[ contamination ] of one-of visitors
Use the value of the most-contaminated visitor:
(max (sentence [ contamination ] of visitors))
Average the values of contamination
(mean (sentence [ contamination ] of visitors))
...or some other expression that you choose
Again, this is all overwriting the patch variable. If you need to take the patchs' current values for that variable, you need to decide how:
If already contaminated, should it:
leave value alone
add turtle value to current value of P-Contamination
save the max of the two values
save the mean of the two values
blend them in some other way
This is the code of a 2 player game that I manipulated
o play-the-game
if (any-friends-nearby?) [gain-energy]
if (any-opponents-nearby?) [fight-opponent]
end
to-report any-friends-nearby?
report (any? (turtles-on neighbors4) with [breed = [breed] of myself])
end
to-report any-opponents-nearby?
report (any? (turtles-on neighbors4) with [breed != [breed] of myself])
end
to gain-energy
set similar-nearby count ( turtles-on neighbors4 )
with
[color = [color] of myself]
set total-nearby count (turtles-on neighbors)
;
;
if (similar-nearby >= total-nearby - similar-nearby)
[set energy energy + 5]
end
to fight-opponent
let my-breed [breed] of green-players
let my-color [color] of green-players
let opponent-breed [breed] of red-players
;
;;
ask my-breed
[check-random-winner]
end
to check-random-winner
let pick random-float 2
let winner nobody
ask turtles
[if winner = nobody
[ ifelse size > pick
[set winner self ]
[set pick pick - size] ] ]
end
to change-opponent
ask red-players
[ set breed green-players
set color green ]
end
Sorry if it's a bit long but when I setup up and then press go "ASK expected input to be an agent or agentset but got the list [green-players...]"
How can I fix this?
Also I'm very new to Netlogo and StackOverflow, apologies if I haven't asked my question properly.
The error message tells you that you are passing a list (more specifically, a list of breeds) to ask, when it comes to ask my-breed.
This is because the my-breed local variable is determined by
let my-breed [breed] of green-players
Let's see what we have there:
breed is a turtles-own variable: it holds the turtle's breed, and being an agents' variable can be used as a reported in the of construct (see below).
of is a reporter: it takes a reporter (normally an agents' variable) on its left (in your case: breed) and either an agent or an agentset on its right (in your case: green-players). What of reports (i.e. what it outputs) is...
... a single value if there is an agent on the right.
... a list of values if there is an agentset on the right*.
*Think about it: if I ask the color of your eyes (you are a single person, i.e. a single agent), you will tell me a single color. But if I ask the color of your friends' eyes (your friends are a group of people, i.e. an agentset), the only way for you to answer my question is to tell me a list of colors.
green-players is an agentset: all of the agents whose breed is green-players (note that for NetLogo green-players is an agentset even if it contains 1 or 0 agents).
From this, we can see that in this case of reports a list of breeds, because it reports the breed of every agent that is part of green-players, hence it will report the list [green-players green-players green-players green-players ... ] which is as long as the number of green-players in the model. You can verify this by clicking setup and then running [breed] of green-players in the Command Center.
This is a list of breeds (which can also be seen as a list of agentsets), which is not an agent or an agentset (which are the only possible targets of ask).
((note that the exact same thing happens with let my-color [color] of green-players and let opponent-breed [breed] of red-players))
So, how do you construct an agentset based on a variable? The most common way to do it is by using with (read here why).
But how can you fix your code? I don't know because I don't understand what you want to achieve.
I am not sure how you would want to use it in the code you posted, as I'm not even sure you need to use ask in fight-opponent (let alone ask an agentset).
Your fight-opponent procedure is such that, apart from the problem we just discussed, the "my-" things (i.e. my-breed and my-color) always refer to the green players while opponent-breed always refers to the red players - even if fight-opponent is run by a red player! And also, it is not clear what you want to achieve with the check-random-winner procedure and if you want this procedure to be ran by an entire breed. These things make it quite confusing to understand how you could want to fix the fight-opponent procedure.
For example: who do you want to run the check-random-winner command?
A combination of two things would be beneficial: develop your model one step at a time and make sure that every new little piece of code does exactly what you expect it to do; also, when you ask for how to fix something it is useful that you explain what you want your code to do. By doing these two things I believe it will be a lot easier to answer your questions
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.
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
Goal: I am attempting to make a turtle pick a destination, then continue walking towards it until the destination is reached. At that point, the turtle returns to its original patch and picks another destination, walks towards it, repeat.
Issue: The selected destination sometimes changes while the turtle is walking towards it. I need some means of telling the turtle to hold the original destination until it reaches it.
Details: Here is my relevant code. Turtles are building territories. They have a territory center-point ("start-patch") from which they choose a destination to walk to and claim. Destination is based on the patch with "highest-value" where value should be the patch's benefit ("benefit-to-me") divided by the distance away from the start-patch ("cost-to-me"). I think turtles are constantly reassessing cost-to-me while walking, however. They shouldn't do this--highest-value must be assessed while standing on the start-patch.
How can I fix this so a turtle assesses highest-value while standing on the start-patch, sets a destination, and moves towards it until reached?
patches-own
[
owner ;; once part of a territory, owner becomes the turtle.
benefit ;; i.e., food available in a patch; used to assess "highest-value" to the turtle.
]
turtles-own
[
start-patch ;; the territory center; turtle returns here after reaching destination.
destination ;; the patch turtle wants to claim for its territory.
territory ;; the patches the turtle owns.
]
to go
tick
ask turtles
[
pick-patch
]
end
to pick-patch
set destination highest-value ;; calculated in reporters, below.
ifelse destination != nobody [
ask destination [set pcolor red] ;; reveals that destination changes occasionally before original destination is reached.
travel]
[give-up] ;; (will reposition start-patch to a new site if no destinations available.)
end
to travel
face destination forward 1 ;; **should** keep original destination, but it doesn't.
if patch-here = destination
[update-territory
move-to start-patch ] ;; return to the start-patch, and should only NOW assess new destination.
end
to update-territory
set owner self ;; and so on....
end
;;;---Reporters for highest-value:---
to-report highest-value ;; this appears to be changing while turtle moves...how fix this?
let available-destinations edge-patches
report max-one-of available-destinations [benefit-to-me / cost-to-me]
end
to-report benefit-to-me
report mean [benefit] of patches in-radius 1 ;; i.e., moving window to find high-benefit cluster
end
to-report cost-to-me
report distance myself
end
to-report edge-patches
report (patch-set [neighbors4] of territory) with [owner = nobody]
end
(Note: instead of "forward 1," I realize I could just use "move-to." I will eventually build in obstacles, however, and turtles will need to walk towards the destination to check for obstacles.)
Update: I think the issue could be addressed within the "cost-to-me" reporter? I tried making this change:
to-report cost-to-me
report distance [start-patch] of myself
end
Should this accomplish what I'm after? It would take away the "distance myself" part so that this cost remains constant. The other idea I've had is that "pick-patch" or "travel" may need something along the lines of "ifelse patch-here != destination [forward 1...]" but that doesn't seem to work either.
I tried the "while" loop idea recommended below (thanks!) and that seems to introduce a new host of odd behavior. I'm not sure how to code that out if I go that route. Something like this doesn't work (they just stop moving):
to travel
while [distance destination > 1]
[face destination forward 1]
if patch-here = destination
[update-territory
move-to start-patch ]
end
I'm new to this; thanks in advance for any help!
Second update: I think that the change I made in the previous update (report distance [start-patch] of myself) fixed part of my problem (assuming that line makes sense?), but left one issue. If there is a tie among patches with highest-value, the turtle still switches destination midway to its selected patch. So it still goes back to the original problem of having the turtle set and keep a destination until it is reached. Any ideas on how to fix this?
The difficulty with using while is that it will move the whole way during the tick. Since the turtle returns to the start patch, why don't you simply add a condition that it only picks out a destination when it is at the start patch? So the code would look like this:
to go
tick
ask turtles
[ if patch-here = start-patch [pick-patch]
]
end
You're right- every time a turtle runs pick-patch, it goes through the step of setting destination to highest-value. Then, it'll move forward one and check if it has arrived. At that point, whether or not it has reached its destination, the other turtles (if there are any) will have a chance to run pick-patch. Once all other turtles have done so, your original turtle will again set its destination to a freshly assessed highest value. So, since highest-value is dependent on distance, and the turtle's spatial coordinates change as it moves, some other patch might have the highest-value from the turtle's new position.
One way you could accomplish what you are after is to use while so that your turtle stays within the procedure until whatever criteria you designate are reached. For a very simple example:
to move-until
ask turtles [
let start-patch patch-here
let destination patch-ahead 10
while [ distance destination > 1 ] [
fd 1
]
]
end
Obviously you will have to modify that to suit your needs, but it should get you started.