I want to ask how can I connect between the turtles and their origin patch
I create the turtles by using
sprout-turtles 2 ;; so their origin is not (0 0)
I want to have an action only happen when they are in their origin patch otherwise another action will happen.
You need to add a turtles-own variable. origin would be a name for it. When the turtles are created, you just need to have them set that variable to patch-here:
set origin patch-here
Then, you can check if they're at their origin patch with: patch-here = origin.
Related
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.
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?
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.
i have two breeds
breed[nodes]
breed [messages]
nodes-own [data]
messages-own [basedata]
i have a network with n nodes. each node has a specific data. im selecting a random turtle as sink.in my network data distributed from a turtle(first all turtles are nodes) to sink.each node that is in the way becomes a message.and each message has a memory space too keep those data that pass from that message.messages basedata keep the original data.
i want sink find the nearest node that has for example "D1".
to setup
setup1
setup-spatially-clustered-network ;create links
ask links [set color white]
end
to setup1
__clear-all-and-reset-ticks
create-nodes number-of-nodes
[
setxy (random-xcor * 0.95) (random-ycor * 0.95)
set shape "circle"
set color green
set value ["D1" "D2" "D3" "D4" "D5" "D6"]
set data one-of value
set label data
set dontpick false
set visit false
]
end
to test1
ask one-of turtles
[
set color red
set label "sink"
set nodenumberdestination who
]
ask min-one-of turtles with [(data = "D1") or (basedata = "D1")][distance turtle nodenumberdestination]
[
]
error : NODES breed does not own variable BASEDATA
There is a conflict between your stated goals in your question and your code, which suggests you have not finished thinking through what you are doing. (The code you posted is also incomplete; for example, it does not include your globals declaration nor setup-messages.) So first a question: did you really mean to use turtles instead of nodes in test1? If yes, that means you allow a message to become your sink. So I'll assume no, as suggested by your actual question. Introduce a new global sink and
to test1a
ask one-of nodes [ ;move this to setup!
set sink self
set label "sink" set color red
]
ask sink [
let _choice min-one-of (other nodes with [data = "D1"]) [distance myself]
ask _choice [] ;do something
]
end
That answers your question as asked. If you really wanted a choice out of all turtles, as suggested by your posted code, you'll have to ask a new question.
the problem is when you do
ask min-one-of turtle with [(data = "D1") or (basedata = "D1")]
it could refers to either nodes and message, while each other doesn't share the same variable.
For example: when it runs on nodes, it will return error when checking [(basedata="D1")] since it doesn't own basedata. Likewise, when it runs on message it will return error when checking [(data="D1")] as well. However, since you haven't created any messages so it will always returning error : NODES breed does not own variable BASEDATA
So, you need to be specific when referring to agentset because any command blocks including variable used will be sensitive to as whose the variables are belong to.
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.