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

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

Related

calculate the total move distance of one turtle

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.

Divide regions accordingly to physical features

I'm working on a smaller project and got stuck on an issue, I'm not really sure if it's possible to solve it in NetLogo but I want to give StackOverflow a go!
I got a model that divides the world into different parts and randomly add physical features (such as rivers). If a feature goes through the whole region, I want it to separate the region and make into two regions. As an example, in the picture below, I want to separate the purple region into two unique regions accordingly to the physical feature (black).
The code I used to generate the picture above, can be found below.
to setup
ca
;Setting world.
resize-world 0 19 0 19
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1][
while [x <= max-pxcor + 1 ][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 10
]
set x 5
set y y + 5
]
;Generating physical features.
ask n-of 5 patches[ sprout 1[
set pcolor black]
]
let i 0
while [ i < (max-pycor * 2 )][
ask turtles [
fd 1
set pcolor black
ifelse (random 20 <= 1)
[
rt one-of [-90 0 90]
forward 1
]
[
fd 1
set pcolor black
fd 1
set pcolor black
]
set pcolor black
set i i + 1]
]
ask turtles [die]
end
My strategy for handling this is to realize that all we really need to do is "flood" a patch out by color and tag all the found adjacent patches, then repeat for any un-tagged, non-black patches until they are all done.
NetLogo does not have a "flood" command to get all patches adjacent to a patch meeting a criteria, so we make a special reporter of our own to handle it, patches-adjacent. Then it's just easy to ask those patches-adjacent to set their region to the currently chosen region.
I don't love this code, it's a little finicky and would be prone to infinite loops if tweaked incorrectly, but it should work. I bet there is a cleaner way to do this that I'm not thinking of at the moment.
; add a variable to track the different regions
; the default value will be `0` for each patch when `clear-all` is called
patches-own [ region ]
to set-regions
let current-region 1
; only act on non-black patches that haven't yet been assigned a region
let untagged patches with [ region = 0 and pcolor != black ]
while [any? untagged] [
ask one-of untagged [
ask patches-adjacent [
set region current-region
]
]
; update the region and the untagged patches we have left to process
set current-region current-region + 1
set untagged patches with [ region = 0 and pcolor != black ]
]
; this is just to get a view of the regions to quickly see if our code worked, it can be removed
ask patches [ set plabel region ]
end
to-report patches-adjacent
report patches-adjacent-ex (patch-set self) pcolor
end
to-report patches-adjacent-ex [found pc]
let newly-found neighbors4 with [ (not member? self found) and pcolor = pc and region = 0 and pcolor != black ]
set found (patch-set found newly-found)
ask newly-found [
; use recursion to find the patches adjacent to each newly-found one
; relying on updating the `found` agentset as we go to avoid duplicates
; or looping forwarder
set found (patches-adjacent-ex found pc)
]
report found
end
I solved this by using the Patch Clusters model that can be found in the NetLogo model library.

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 can I get an agent to decay as multiple agents feed on it?

In my model I have some agents which act as food items with a set energy. These are fed upon by a number of turtle breeds who each have their own food-energy which is less than the energy of the food item.
The code for the feeding agents is as follows:
to eat
ifelse [food-energy] of myfood > 1.5 [
set food-energy 1.5]
end
and the associated code for the food item to decay is:
to decay
if any? turtles-here [set food-energy
(1.5 * count feeders-here with [myfood = myself]
end
The problem occurs if the energy of the food is not an exact multiple of the amount of energy the feeders can consume. So for example it can go down to 1 and this results in the feeders taking 1.5 units which should be impossible. This is exacerbated when I have different breeds with different food energies (i.e. < or > 1.5).
So my question is how can I get this things to balance?
You need to study the Wolf-Sheep Predation model. This is the first NetLogo tutorial: http://ccl.northwestern.edu/netlogo/docs/tutorial1.html There are five versions of increasing complexity in the NetLogo Models Library, which are covered in chapter 4 of Wilensky and Rand (2015), which you should read.
See some related material here:
http://jasss.soc.surrey.ac.uk/14/2/5.html
Some hints follow, but many details need filling in.
breed [feeders feeder]
patches-own [ food-energy ]
feeders-own [ myfood ]
to setup
ca
ask patches [set food-energy random 50]
create-feeders 500 [
move-to one-of patches
set myfood one-of patches
]
end
to go
ask feeders [move]
ask feeders [feed]
ask patches [growback]
end
to move ;how shd they move?
rt random 20
left random 40
fd 1
;shd movement cost energy?
end
to feed
if (patch-here = myfood) [
let _extracted min (list food-energy 1.5)
set food-energy (food-energy - _extracted)
]
end
to growback
;do you want growback?
end
thanks for your responses. I'll try to implement them. This is one inelegant workaround that worked for me:
to eat
ifelse (food-energy / capacity) < 1 and [meat] of myfood > capacity [
set food-energy 1.5] [set food-energy [meat] of myfood
ask myfood [set shape "square"]]
if (food-energy / capacity) = 1 [
set color white]
if (food-energy > 0 and food-energy / capacity < 1)
[ set color white ]
end
This was initially causing a problem such that when the food energy went down to 0 and I asked it to die any animal looking at the [meat] of myfood lost it's target and I got an error. So I made the animals break this connection once their colour was white.
to ignore
set myfood nobody
set food-energy food-energy * 1
end

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: