NetLogo - growing territory - netlogo

I'm trying to expand animal territories such that the neighboring patches surrounding an animal's existing territory are assessed by some criteria (i.e., food quality and if other animal's territory is there) and then added to the total size at each time step. Using the following post as a template (http://netlogo-users.18673.x6.nabble.com/What-is-more-efficient-to-acquire-patches-by-expanding-search-radius-or-moving-td5003711.html) I was able to have animals assess the neighbors around their individual location and determine if the neighbors were taken by anybody (i.e., just TRUE/FALSE not identity of the animal with the territory) and move into a grid cell with the highest "quality" that was not taken. Instead I'd like for the animal to assess it's entire territory and expand to any grid cell that fits the criteria. Also, I'd like to have the animal identity assigned to patches that fall within its territory so that, for example, an animal can not go into the territory of a dominant animal but can of a subordinate. I tried to utilize the flood fill idea but could not quite get it right. Any suggestions or help would be great. Below is what I have so far.
breed [animals]
animals-own [ orig territory food status] ; turtle's original patch, patch-set of territory, status (higher number more dominant) of the animal to other competitors (is not incorporated currently)
patches-own [ taken? hsi] ; true if patch is in territory of a turtle
to setup
clear-all
ask patches [ set taken? false
set hsi random 5
set pcolor scale-color (black) hsi 1 4]
let $colors [red pink yellow blue orange brown gray violet sky lime]
ask n-of 10 patches
[ sprout-animals 1
[ set orig patch-here
set territory patch-set orig
set status random 4
set color item who $colors
set pcolor color
]
set taken? true
]
reset-ticks
end
to go
if all? animals [food >= 150] [ stop ]
if ticks = 50 [ stop ]
ask animals [ expand ]
tick
end
to expand
if food < 150
[
let $p neighbors of [territory] with ([not taken?] and [hsi > 2]) ; expects agentset but territory is a patch-set
set territory (patch-set territory $p)
set pcolor [pcolor] of myself
set food sum [hsi] of territory
]
end

It seems sensible to me that as you suggest, each patch should know whose territory it is part of. So instead of patches-own [taken?] where taken? is true or false, I would suggest patches-own [owner].
In your setup procedure, you do ask patches [ set owner nobody ], and then when a turtle takes ownership of a patch, it sets that patch's owner to itself. So in your setup procedure you'd add set owner self (to change the owner of the patch is standing on) and in your expand procedure, do ask $p [ set owner myself ].
You'll have to be careful to keep the territory information stored in the turtles in sync with the owner information stored in the patches, but assuming you get that right, then problems such as "an animal can not go into the territory of a dominant animal" become easy because when an animal is considering stepping into a patch, it can trivially see who that patch's owner is, with code such as:
let target patch-ahead 1
let defender [owner] of target
if not is-turtle? owner or [status] of owner < status [
...
]

Related

Give agents on patch the same value that patches have NETLOGO

I have a model where there are multiple animals in a den
patches-own [den-ID]
where the dens are pink and the number of dens is controlled with a slider global var
ask patches with [pcolor = pink] [set den-ID random n-dens]
Now, I want agents that spawn on that patch to set their own parameter "family" to a value equal to the den-ID that they spawn on - therefore having a group of animals that can be traced back to a den site and all share a home range.
I have tried many solutions but cant get anything to work.
basically something like
set family (family = den-id of patch-here)
thanks
The answer: ask turtles [ set family patch-here ]
Down below is how we arrived there
You seem a bit confused in your Netlogo syntax.
= is only used for logical expressions in Netlogo, not for setting variables. For that you use set.
If you want to use of to access variables of another agent, you need to encase the variable in square brackets: [den-id] of patch-here.
The solution you are looking for looks something like the following:
ask turtles [ set family [den-id] of patch-here ]
It can be made even easier. A turtle can directly access the variables of a patch that they are on without needing to name that patch.
ask turtles [ set family den-id ]
I noticed another thing in your code. Nothing in set den-ID random n-dens prevents multiple dens of having the same number. There are numerous ways of doing it, for example with a while loop where you increment the current-ID each time.
set current-ID 1
while [any? patches with [pcolor = pink and den-ID = 0]] [
ask one-of patches with [pcolor = pink and den-ID = 0] [set den-ID current-ID]
set current-ID current-ID + 1
]
You could even opt to forget about den-id and just let the turtles remember their den itself:
ask turtles [ set family patch-here ]

In Netlogo, how do you move a turtle to the other end of its link?

I've created two breeds of turtles in my simulation: one is a regular turtle and the other is a halo that is intended to overlap each turtle. Whenever a turtle is hatched (either created as part of the setup procedure or created with netlogo's hatch function), a halo is also hatched and linked by calling a separate make-halo function.
create turtles turtle-initial-number
;;(all the turtle genes are set here)
if halos-enabled [make-halo]
to make-halo
hatch-halos 1
[ set size sight-radius * 2 + 1
set shape "square"
set color lput 64 extract-rgb color
__set-line-thickness 0.5
create-link-from myself
[ tie
hide-link ] ]
end
Due to some interactions I've implemented, sometimes the turtles and the halos become detached from one another, so I'd like to add a step at the end of each tick where all halos snap back to their turtles where they belong. Is there a way to move the halo or set its coordinates to the turtle at the other end of the link?
The other option is to solve whatever is happening when the disconnect occurs. I have another breed of turtle (people) who can "push" others with this push-away function below. Turtles (and their halos) occupying the 9 squares in front of the person are pushed forward along the same direction at the person is facing. When they are pushed, for some reason the turtle isn't at the centre of the halo anymore.
to push-away
ask people [
let push-dir heading
ask patch-ahead 2
[ask turtles-here
[set heading push-dir
fd 2]
ask neighbors
[ask turtles-here
[set heading push-dir
fd 2]
]
]
]
end
In theory, the tie should link the movements. But to snap the halo to its turtle, you can ask the halo to move-to the turtle. The only trick will be identifying the correct turtle and you haven't shown enough of your code for me to sort out the identification for you.
I suggest you actually add a variable to the halos that records their turtle rather than using a link. If the link has no other purpose, there is no need to create all those extra model entities. You would use it like this:
halos-own [my-owner]
to make-halo
hatch-halos 1
[ set size sight-radius * 2 + 1
set shape "square"
set color lput 64 extract-rgb color
__set-line-thickness 0.5
set my-owner myself ; this is the new line
]
end
to push-away
<all the code you have already>
ask halos
[ move-to my-owner
]
end

How would I "target" patches within a certain area?

I have set up agents and nodes to represent people and stores and it is my intention that the agents will "target" the store in their "awareness" space with the highest value ("vulnerability"). I've largely coded what I have so far through trial and error however setting the turtle's target to the patch with the highest value within a 10 unit radius is a hurdle I can't get over. Currently they target the patch with the highest value regardless of its position in the world. Could somebody suggest what I might consider to achieve this please? I have pasted what I have written so far for reference.
Thanks.
breed [shoplifters a-shoplifter]
patches-own [vulnerability]
shoplifters-own [target
awareness]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
setup-stores
end
to setup-stores
ask n-of num-stores patches [ set pcolor lime ] ;; create 'num-stores' randomly
ask patches [
if pcolor = lime
[ set vulnerability random 100
]
]
end
to setup-turtles
setup-shoplifters
setup-target
end
to setup-shoplifters
create-shoplifters num-shoplifters [ ;; create 'num-turtles' shoplifters randomly
set xcor random-xcor
set ycor random-ycor
set shape "person"
set color red
]
end
to setup-awareness
ask turtles [
set awareness
patches in-radius 10
]
end
to setup-target
ask turtles [
set target
max-one-of patches [vulnerability]
]
end
You are on the right track using max-one-of. At the moment, however, you are sending patches as the space to search through to look for the one with maximum vulnerability value, when you really want patches in-radius 10. So you could simply do this:
to setup-target
ask turtles [
set target max-one-of patches in-radius 10 [vulnerability]
]
end
However, this is going to be inefficient because NetLogo will have to first work out which are the patches within the radius. You have already asked the turtles to work this out and assign it to their variable 'awareness'. What you really want to do is therefore:
to setup-target
ask shoplifters [
set target max-one-of patches awareness [vulnerability]
]
end
Note that I also changed ask turtles to ask shoplifters. It is only shoplifters who have the attribute 'target' so you should only be asking them to calculate it. Same thing goes for 'awareness'. At the moment you don't have any other breeds so it's not causing an error, but it is good practice to use the breed, otherwise there is no point in creating it.

NetLogo: measure real travel distances around obstacles before choosing destination

I'm modeling territory colonization in Netlogo. Turtle 0 selects a territory center, then builds a territory by adding patches in order of value. Value is based on benefit / cost, where benefit is amount of food in the patch and cost is the distance from the territory center. Turtle 0 finishes building its territory once the summed patch values meets a threshold. Next, Turtle 1 sprouts and repeats the process to select its own territory, then Turtle 2, and so on. Importantly, new turtles should avoid traveling across other territories and cannot picked already-owned patches.
The Problem: Turtles need to travel around patches that are already owned. A patch's value (benefit/cost) should account for costs accurately based on the real travel distances required, not euclidean distance.
IMAGE: As a visual, here is one result from running this model. The turtle in lime green has a huge territory that required traveling over other territories and did not account for actual travel costs around what should be considered obstacles.
How might I code this to account for actual travel distance required (i.e., around obstacles of the existing home ranges) to have turtles pick patches in order of real value? Some code is below. Any ideas? Thanks in advance!
patches-own [
benefit ;; ranges 0.1-1 and represents food available in the patch
owner ] ;; patches are owned once selected for a territory
turtles-own
[ sum-value ] ;; sum of values of patches in my territory
globals [threshold = 25]
to setup
ask patches [ set owner nobody ]
end
to go
ask turtles [build-territory]
tick
end
to build-territory
if sum-value > threshold [stop] ;; keeps picking patches for territory until I've met my threshold
pick-patch
reproduce ;; sprouts a new hatchling when done building territory
end
to pick-patch
let _destination highest-value ;; this is calculated in reporters, below
ifelse _destination != nobody [
face _destination forward 1 ;; turtle will need to avoid obstacles here, haven't figured that out yet.
if patch-here = _destination
[ claim-patch _destination ]
]
[stop] ;; if there is no _destination
end
to claim-patch [_patch]
ask _patch [set owner myself]
set sum-value sum-value + (benefit / (distance start-patch))
set pcolor [color] of self
;; etc....
end
to reproduce
if sum-value > threshold [ ask patch 75 75 [sprout 1 ] ]
end
;;;; --reporters for calculations:--
to-report highest-value ;; calculates value, benefit / cost.
let available-destinations edge-patches
report max-one-of available-destinations [benefit-to-me / cost-to-me]
end
to-report edge-patches ;; constrains to searching along edge of territory so it remains contiguous.
report (patch-set [neighbors4] of territory) with [owner = nobody]
end
to-report benefit-to-me
report mean [benefit] of patches in-radius 2 ;; this is basically a moving windows analysis to find a cluster of high-benefit patches.
end
to-report cost-to-me
report distance myself ;; this is where turtle needs to account for actual travel costs (non-euclidean distance), including around other territories. how to accomplish?
end

Changing Node ID with every Setup in Netlogo

We try to show a simple infection via Netlogo. For our purpose we need to start the infection with the same turtle for several times.
But right now with every setup another turtle begins with the infection. We already tried to work with the Node ID, but unfortunately the ID of the different turtles changes with every setup, too. We are out of ideas but
maybe there is a way to sove this problem I am happy for any answers :)
This is our Code so far:
extensions [nw]
globals
[
num-informed
informed-size
]
turtles-own
[
informed?
]
to setup
clear-all
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [ set size 1.5 ]
layout-radial turtles links turtle 61
ask turtles [set color red]
ask turtles [set shape "dot"]
ask links [set color grey + 1.5]
ask patches [set pcolor white]
ask turtles [set label-color black]
ask turtles [set informed? false]
ask turtle 72
[
set informed? true
set color green
]
set num-informed 1
set informed-size 2
reset-ticks
nw:save-graphml "JKnachnamennetlogo.graphml"
end
to spread
if (count turtles with [informed? = true] > .7 * count turtles) [stop]
ask turtles with [ informed? = true ]
[
ask link-neighbors with [not informed?]
[
if (random-float 1 <= 0.01)
[
set informed? true
show-turtle
set color green
]
]
]
set num-informed count turtles with [informed? = true]
tick
end
Thank you a lot.
I am a little unclear so am giving bits of different answers for different situations.
If the turtles are different each time, what do you mean by 'the same turtle'. For example, do you mean the turtle in a particular position? If so, you could select the turtle on the appropriate patch.
If it doesn't matter which particular turtle it is (just that it's the same turtle), then the simplest approach is to set the random-seed. Then every time you run any random process (including choosing one-of the turtles to select the starting infection, or ask turtles to do something), NetLogo will use the same chain of random numbers. Of course, if you are still building your model, then adding new pieces of code that change how many calls are made to the random number generator will lead to a different chain, but rerunning with the same code will give the identical run.
You may need to use with-local-randomness and random-seed new-seed if you want to have some parts actually change.
The problem is that nw does not store the WHO variable this is to avoid conflict with already existing turtles in a model.
A work-around would be assigning each turtle a separate id variable and setting that to who.
turtles-own [informed? id]
in turtles creation asign them each the id thus
set id who
you may want to write a conversion procedure like this
to convert
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [set id who]
nw:save-graphml file-name "JK_nachnamen(id).graphml"
end
and use the copy. Of course you would not use
turtle 74
but
one-of turtles with [id = 74]