How do i keep track of visited patches. A turtle is moving every where and i need to keep track of it that which patch is visited by a turtle or which is un-visited.
There are many ways to do this and their utility depends on your needs. Here is one way, using a list for memory.
turtles-own [memory]
to setup
ca
crt 1 [set memory (list patch-here)]
end
to move ;;turtle proc
let unvisited patches with [not member? self [memory] of myself]
move-to one-of unvisited
set memory lput patch-here memory
end
In this simple example, the memory list grows without bound. In a real application, you probably will want to remove old memories after a certain length.
Related
I am building a model in Netlogo where I am simulating the spread of a virus. I have looked at other virus models but so far haven't found a solution to my problem.
I would like each agent interaction to have a specific transmission-probability, based on the susceptible agent's susceptibility (agent attribute), the infected agent's (can be more than one) probability of transmitting the virus (agent attribute), as well as the time spent in proximity. Basically P(getting infected)= infectivity * time in proximity * susceptibility. So for each tick that a susceptible turtle is close to a infected one the probability of getting infected should increase. So far I have only managed to use susceptibility when creating the procedure with susceptible agents as callers.
Here is my code for this procedure so far:
to transmit; procedure for transmitting the disease to nearby people, inspired by epiDEM
let caller self ; adress the susceptible turtle by the name caller
if susceptible? = TRUE
[
let nearby-infected turtles in-radius 2 with [infected? = TRUE]
if nearby-infected != nobody
[
let transmission-risk age-susceptibility ;here I want to include protective-measures (attribute of the possibly several infected turtles) and time-exposed
if random-float 1 < transmission-risk[
set infected? TRUE set susceptible? FALSE]
]
]
end
I am really having a hard time on how to go about this, I am not sure how to address the attributes of the turtles within the radius and how I could measure the time of exposure. I was thinking of creating links that have exposure time as an attribute that will die when the infected is no longer in the radius. But I am not sure how to ask the links to die when the turtles are further away from each other than a 2 patch radius. Also I would prefer to not use links to keep the model runnning faster so if there is another solution I would be very happy to hear it :)
For the option with the links I wanted to try something like this:
to transmit; procedure for transmitting the disease to nearby people
ifelse link-neighbors (in-radius 2 = FALSE)
[ask link [die]]
[set link-age link-age + 1]
let caller self ; adress the susceptible turtle by the name caller
if susceptible? = TRUE
[
let nearby-infected turtles in-radius 2 with [infected? = TRUE]
if nearby-infected != nobody
[create-links-with nearby-infected
let transmission-risk age-susceptibility ;include protective-measures and time-exposed using links to address the turtles
if random-float 1 < transmission-risk[
set infected? TRUE set susceptible? FALSE]
]
]
However I for starters just don't understand how to address the links of the turtles further away to ask them to die, I have changed it after the error messages I've gotten but just can't seem to get it to work.
Is it even possible to do what I want in Netlogo?
Very thankful for any hints!
I'm trying to implement YOYO leader election algorithm in netlogo
first step in this algorithm is to orient links ( direct link )from the minimum to the maximumbut between neighbors only !
I tried the command
[`ask turtles with [ [ who ] of self < [who] of one-of link-neighbors ]
create-direct-to turtle [who] of one-of link-neighbors ]`
this creates direct link from min to max ( neighbors ) but also creates a direct link from max to min ( neighbors )
and I don't know what's wrong :(
here's a screenshot , if you notice theres' a direct link from 0 to 2 and also from 2 to 0 and my goal is to have only from 0 to 2
Your problem is that every time you do one-of, it randomly selects. So you test against a random link-neighbor in the first line, find it's true and then randomly select a link-neighbor to connect to.
[ ask turtles with [ [ who ] of self < [who] of one-of link-neighbors ]
create-direct-to turtle [who] of one-of link-neighbors
]
More generally, this seems an odd way to achieve your goal. To start with, link-neighbors are the turtles that the turtle is already linked to. link is the generic name for all link breeds (I think you have created a breed called direct-link).
I am not entirely clear what you mean by minimum and maximum since your code is just from smaller to larger who value, regardless of what other who values are available. If you want to create a link from every turtle to every turtle with a higher who value, here is some code:
ask turtles
[ let targets turtles with [who > [who] of myself]
create-links-to targets
]
In general, it is bad practice to use who in NetLogo code. who is a completely arbitrary identifier that simply tracks the order that turtles are created. If you have turtles that die, then your code may crash because it is referring to a turtle that no longer exists. Or perhaps at some point you will have two breeds of turtles - who doesn't care if your turtle is a person or a dog or a factory or...
This may be one of the very few exceptions, but you might want to think about what you are intending who to mean. For example, as this is a leadership model, perhaps you could have a variable called 'charisma' and all the links are from turtles with lower values of charisma to higher values of charisma.
I am brand new to Netlogo and am coding a world with caching animals. They will go to their caches (I set them as blue patches) if their energy value falls below 10. They can find these caches based on a random 'memory' value given to them which is used as an in-radius value. That way, they will face and go towards a cache within their in-radius memory if they are about to die. I am starting to incorporate a home-base system where the turtle remains in a smaller area with their own caches. Does anyone know how I can make a patch belong to an individual turtle? This would allow turtles to have their specific caches in their territory. I have looked into using links and breeds, but links are only between turtles and making the individual breeds for the 50+ turtles at a time seems ineffective and complex. I apologize I don't have any code attempting to have a cache (patch) belong to a turtle, I don't know where to start. Any ideas?
If you want a turtle to remember anything (patches or income or anything else), then you need to assign a variable in a turtles-own statement and then set the value appropriately. Here's some example code fragments. They won't work, and you actual code would likely look a lot different because you will need some design about the conditions under which a cache will be assigned, but they show you what a variable solution looks like.
turtles-own
[ my-caches
]
set my-caches (patch-set my-caches patch-here) ; code when a turtle finds a new cache site
If you want a patch that belongs to a turtle to make that patch unavailable to other turtles, then also set up a patch variable to store its owner.
patches-own
[ my-owner
]
ask turtles
[ if [my-owner] of patch-here = nobody [set my-caches (patch-set my-caches patch-here)]
ask patch-here [set my-owner myself]
]
I suggest you do several NetLogo tutorials, then look at some library models (and understand them) before starting your own model. You need to understand basic concepts like turtles/patches, variables, ticks before trying to build a model.
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 am new to Netlogo and have become stumped with a problem.
I have written a model where one breed of turtles (wombats) randomly chose to move-to different burrows (a second breed of turtles). However, I now wish to make it so that the quality (represented by a number) of the burrow currently on the same patch as a wombat influences the probability of the wombat moving to a different burrow. I had envisaged this to look similar to:
ask wombats[
if random-float 100 >= burrow-fidelity * ( quality one-of burrows burrows-here / 2)
[move-to one-of burrows with [not any? wombats-here]]
]
however this does not work.
Is there anyway that I make the wombat report the size of the burrow currently sharing the same patch (there is only ever one burrow per a patch) and then make the wombat incorporate the reported value into the above equation where "quality one-of burrows burrows-here" is?
Thank you in advance
To report the value of another agent's variable, use the of keyword:
[ quality ] of one-of burrows-here