calculate the total move distance of one turtle - netlogo

I am new to Netlogo and learning a model is about animals moving around to eat grass based on the grazing model from NetLogo. The moving behavior is based on the biomass's richness, so it is not just impacting at right angles.
Given the conditions, I don't know how to add a monitor in my model that calculates the total distance a turtle moved by traveling each pixel.
Move specifically, I wonder if there is a way to calculate the total length of the pen-marked lines. (Show in the picture)
enter image description here
The basic setting of move is
to move-turtles
uphill-biomass
forward 1
set distance-traveled (distance-traveled + 1)
if not can-move? 1 [ rt random 150 ]
end
to uphill-biomass
let biomass-ahead biomass-scent-at-angle 0
let biomass-right biomass-scent-at-angle 35
let biomass-left biomass-scent-at-angle -35
if (biomass-right = biomass-ahead) and (biomass-left = biomass-ahead) [ wiggle ]
if (biomass-right > biomass-ahead) or (biomass-left > biomass-ahead)
[ ifelse biomass-right > biomass-left
[ rt 35 ]
[ lt 35 ] ]
end
Thank you very much for any helps!

You need to use some turtle variables with the turtles-own primitive and do the distance calculation by hand.
The easiest way to do such a calculation is to save the coordinates between a turtle's previous coordinates with turtle variables and then use the distancexy primitive (link for dictionary entry: https://ccl.northwestern.edu/netlogo/docs/dictionary.html#distancexy)
Here's a simple implementation:
turtles-own [last-x last-y distance-traveled]
move-turtle
uphill-biomass
set last-x xcor
set last-y ycor
forward 1
set distance-traveled (distance-traveled + (distancexy last-x last-y))
if not can-move? 1 [ rt random 150 ]
end
I hope this answers your question.

Related

Specifying dispersal distances for turtles in Netlogo

So for part of my model, I want my turtles (in this case, deer, specifically juvenile female deer) to "disperse" according to certain criteria. Here is what I have so far:
to move-dispersing-femjuvs
ask femjuvs [
let natal-range patches in-radius 5
let density (count femadults-on natal-range + count maleadults-on natal-
range + count femjuvs-on natal-range + count malejuvs-on natal-range +
count infants-on natal-range)
let chance-disperse 1 / (1 + exp(2.051 - (0.002605 * density)))
if (random-float 1.001 < chance-disperse) [
let mean-distance (18.703 + (0.02533 * density))
I believe this will give me the proportion of the female juveniles that will disperse, as well as the mean dispersal distance, taken from literature (I realize the if statement is incomplete, this is where I'm stuck). My question is, how do I get the "femjuvs" who are dispersing to actually move? They would move until they reach an unoccupied patch in a radius that does not overlap the "natal range". Ideally, I would want all of the distances of the femjuvs who do disperse to have a mean of "mean-distance". I have "dispersal-distance" as a "femjuvs-own" variable, but I haven't set it to any value.
Any help with any part of this problem is appreciated. Thank you!
Possibly use: "setxy" or "forward"
setxy
If the aim is to update a deer position over time, calculating its coordinates, setxy might be a good option. This isn't so much moving as it is adjusting its coordinates.
ask femjuvs [ setxy <xcor> <ycor> ] ; example
ask femjuvs [ let x-coordinate <some-long-computation>
let y-coordinate <some-longer-computation>
setxy x-coordinate y-coordinate ]
forward
To move a deer, there is a function called "forward" that can be invoked by femjuvs (turtle/breed context):
ask femjuvs [ forward <number> ]
It would move femjuvs n spaces in the direction it is facing, where is the number specified.
Possible additions: (* - relevant functions)
to move-dispersing-femjuvs
ask femjuvs [
let natal-range patches in-radius 5
let density (count femadults-on ...) ; shortened for space
* forward 1 ; move forward one
]
...
end
Also, if dispersing from a given point or coordinate is necessary, having the deer face the point and turn it 180 degrees could work.
ask femjuvs [ face <random-point> ; or facexy <xcor> <ycor>
left 180 ] ; turn the opposite direction
ask femjuvs [ forward 1 ] ; disperse
One thing to note is that if the direction is not set by the developer, NetLogo will store a direction beforehand, which may or may not be helpful.
More details in : https://ccl.northwestern.edu/netlogo/docs/dictionary.html

Variables in different breeds

i'm trying to make an archaeological model, in which hunters are making paintings in shelters, depending on their quality.
breed [shelters shelter]
breed [hunters hunter]
shelters-own [quality paintings]
The value of each shelter quality is set in the setup (with a slider for the actual number of shelters).
create-shelters number-shelters [set quality random 100]
The action of painting-or-not is then defined by random against the quality of each shelter:
to make-painting
ask shelters [
if any? hunters-on patch-here [
if random 100 < quality [set paintings paintings + 1]
]
]
end
Now, I would like to complexify it a bit more: the quality wouldn't be defined by the shelter itself (and thus be the same for every hunter), but by the hunters: each of them would attribute a different quality for each shelter. The action of painting-or-not would still be a test against random, but with this new variable defined by each individual hunter...
But I can't find a way to code it down properly.
Does anyone have a suggestion?
I think I got it right. Basically, I had every hunter to create a matrix filled with random numbers. The size is 33x33, so it goes all over the world (had to set it to start in a corner, or it gets negative coordinates).
create-hunters number-hunters [
set color white
set size 1
setxy random-xcor random-ycor
set hunter-matrix matrix:make-constant 33 33 random 10
]
ask n-of number-shelters patches [
sprout-shelters 1 [
set color one-of base-colors
set size 1
set xpatch xcor set ypatch ycor
]
Then, when they would reach a shelter, the value corresponding to that patch location would be extracted and used to paint-or-not.
ask patches [
ask hunters-here [set quality matrix:get hunter-matrix xpatch ypatch]
let paintings-here sum [paintings] of shelters-here
if any? hunters-here and any? shelters-here [
if random 10 < quality [
ask shelters-here [
set paintings paintings + 1]
]
]
]
I'm still not completely sure it actually does what I think it's doing, tho.

How do I properly use the 'random' function for multiple agents?

In my model I have some agents collecting from another agent who they bump into at random after which they move back to their base. As they move back they drop off some material as defined by the random function. Here's some sample code
to go
ask searchers
[ set energy energy - 1
fd 0.0125
if random-float 1 < (1 / 50)
[ ifelse random 2 = 0
[ rt 45 ]
[ lt 45 ]
]
search
]
end
to search
if any? depots in-radius vision with [color = yellow]
[spread
set energy 0] ;; makes them to back to base
end
to spread
if random 10000 = 1 [hatch-rubbish 1 [ set color white
set shape "circle"
set size 0.5]]
end
If I set the visual radius to something enormous so they can always see the depot the number of bits of rubbish works out.
However if I allow them to move around with a radius of 1, the count of rubbish is much higher than 1 in 10,000.
Why would that make a difference?
Thanks

What is the most efficient way to ask other turtle who can see the caller turtle in NetLogo?

I was wondering if anyone has a good idea to filter turtles who have the caller turtle in their vision and in their cone of vision with the least computational cost?
Now I am using something like this:
Member? Caller-Agent agents in-cone 5 100
or just who are agents who can see me?
I was wondering if anyone knows how can I check if heading of other agents in some radius X is toward the caller agent ?
So , this is the way I did it:
I used abs( towards myself - heading) < 50
I thought it might include cone with angle of 100 since it uses absolute value.
This is just a test program to see if it works:
ask turtles[set label "" set color green]
ask turtle 7 [
set color red
set label "Caller"
ask other (turtles with [distance myself <= 3 and abs( towards myself - heading) < 50 ])
[
set color yellow
]
ask other turtles [
set label (Member? turtle 7 Other turtles in-cone 3 100)
]
]
and this one cone 80 and abs( towards myself - heading) < 40 :
There is a problem with this approach:
if an agent is as same location of the caller then I will get the error that there is no heading defined to same point or something like that!
Error is : No heading is defined from a point (7,7) to that same point. error while turtle 28 running TOWARDS
Update:
[(distance myself <= 3 and distance myself > 0 and abs( subtract-headings towards myself heading ) < 60) or distance myself = 0 ]

Trying to get factions to arrange in segments? NetLogo code advice

I've made a model that aranges factions (turtles in different colours) in a circle.
At the moment they arrange randomly, was wondering if someone could help me arrange them so, for example, red occupies the first 90 degrees, blue the next 90 degrees, etc (on setup).
Here's my code...
ask patch 0 0
[ ask patches in-radius ( max-pxcor * .9) with [ random-float 100 < density ]
[ sprout 1
[ set breed cons
set shape "circle"
set faction random factions
set heading random 360
set size 1
]
]
]
.. guessing I will have to do 360 / fractions, but not sure how to phrase it, if someone could help me out that'd be great. Thanks!
The NetLogo primitive that's the closest to what you want to do is in-cone, which reports the set of turtles that are in the "cone of vision" of another turtle. But your "pie slices" should just be relative to patch 0 0, not to another turtle! No problem: just make a temporary turtle at patch 0 0, use it to get turtles in-cone with the appropriate angle, and kill your temporary turtle.
The following procedure can be used "as is" with your code (just call it from your setup procedure after creating your turtles exactly as you were doing before):
to assign-factions
let angle 360 / factions
foreach n-values factions [?] [
ask patch 0 0 [
sprout 1 [
set heading ? * angle
ask turtles in-cone max-pxcor angle [ set faction ? + 1 ]
die
]
]
]
end
The code is pretty straightforward, except for maybe the more obscure n-values. You could replace it with a while loop if you prefer, but it's really just counting from 0 to factions.
Here is what you'd get with 5 factions: