Creating lists based on operations on turtle-own variables - list

I have 3 turtles and each has a size variable. I want to create a list of dimension 3 which will basically be % support size of each turtle with respect to the total size. List should look like
[turtle1-size / sum[size] of all turtles
turtle2-size / sum[size] of all turtles
turtle3-size / sum[size] of all turtle]
I know there are turtles-own and patches-own list but that will only contain the list of the turtle/patch names itself right?
Thanks, appreciate the help.
Regards

Here's one solution:
to-report proportions
let total-size sum [size] of turtles
report (list [size / total-size] of turtle 0
[size / total-size] of turtle 1
[size / total-size] of turtle 2)
end
let's try it:
observer> clear-all
observer> create-turtles 3 [ set size who ]
observer> show proportions
observer: [0 0.3333333333333333 0.6666666666666666]
Looks right to me.

Related

Netlogo Lists: detecting the patch in a list with max distance to one specific patch - for each turtle

I want to solve a problem in Netlogo that so far exceeds my programming skills. I want to built a list for each turtle which contains the furthest patch the turtle went on this "day". So far I tried to built a list were all patches are stored for each turtle. Now I want to calculate - for each turtle - the patch from this list that has maximum distance from its home (hide). I want to empty the list every night (thats not mandatory) Thats my code so far:
let temp-visited-patch-list lput patch-here temp-visited-patch-list
if period = night
[
[foreach [temp-visited-patch-list] [x -> set visited-patch-list lput (max x [distance hide]) visited-patch-list]]
let temp-visited-patch-list []
]
So I am not that far to extract the values for each turtle seperatly - and even the part I posted does not work. I get an "expected command" error. I would be very thankful for any suggestions to solve this problem.
Best regards
Olivia
This code has the turtles move, remember their furthest patch and turns those patches red and calculates average distance. It should help orient you how to use patches and turtle attributes to solve your question.
globals [home-patch]
turtles-own
[ farpatch
maxdistance
]
to setup
clear-all
set home-patch one-of patches
ask home-patch
[ set pcolor blue
sprout 20
]
reset-ticks
end
to go
repeat 20 [movement]
ask (patch-set [farpatch] of turtles) [set pcolor red]
type "Average max distance:" print mean [maxdistance] of turtles
tick
end
to movement
ask turtles
[ set heading random 360
forward 1
if distance home-patch > maxdistance
[ set maxdistance distance home-patch
set farpatch patch-here
]
]
end

Chemotaxis in NetLogo

I have made a gradient, and I am quite unsure as to how I can make my turtle(s) attracted to one end and also behave a different way. For instance, from light blue to dark blue. Dark blue would be where the food is (chemoattractant), and in this zone, the bacterias would prefer to run randomly or be more energetic. While, in the light blue area, there would be fewer turtles and their movements would be slow or "tumbling."
Overall, how do I make the turtles sense the chemoattractant or the gradient in the patch?
If pcolors were floating point you could just search the immediate neighbors and use
ask turtles [ uphill pcolor ]
but that won't work if the gradient is subtle. The turtles might just sit there.
If you store a floating-point patch variable "exact_color" , say, the next code will work but the angle of motion is always some multiple of 45 degrees.
ask turtles [ uphill exact_color]
So if you generalize the search to a larger radius to determine the local gradient,
this would work:
let neighborhood patches with [ distance myself <= search_radius]
let p max-one-of neighborhood [exact_color]
if [exact_color] of p > exact_color [ face p move-to p]
Here's a working implementation that assigns the color gradient then tracks it.
globals [ scentXC scentYC search_radius ] ;; location of center of maximum scent
patches-own [ exact_color ];
to setup
clear-all
print ("This assumes the world does not wrap");
;; decide to put center of attraction at ( 14, 7 )
set scentXC 14;
set scentYC 7;
;; search a larger radius for gradient since it may be low
set search_radius 5;
color_code_patches
;; next line can visually check work so far
;; ask N-of 20 patches [ set plabel pcolor]
create-turtles 10 [ setxy random-pxcor random-pycor set size 2 set shape "arrow" pen-down];
reset-ticks
end
to go
;; ask turtles [ uphill pcolor ] ;; fails because the gradient is too low
;; following works but the angle of motion is always a multiple of 45 degrees
;; ask turtles [ pen-down uphill exact_color] ;; move one step uphill
;; the following searches a larger neighborhood for the uphill direction
ask turtles
[
;; set search_radius 1.45 to simply search neighbors, ie "uphill exact_color"
let neighborhood patches with [ distance myself <= search_radius]
let p max-one-of neighborhood [exact_color] ;; or neighbors4
if [exact_color] of p > exact_color [ face p move-to p]
]
end
to color_code_patches
no-display
ask patches [
let blueness 0 ;; initialize to make the interpreter happy
let dist distancexy scentXC scentYC
ifelse (dist <= 1) [ set blueness 250 ]
[ set blueness (250 / dist ) ]
set exact_color blueness;
set blueness round blueness;
;;set pcolor [ 0 0 blueness] ;; for unknown reason this doesn't work so more complex
let color_assign ( word "set pcolor [ 0 0 " blueness " ] ")
run color_assign
]
display
end

Netlogo: How to transfer lists belonging to neighbours to a matrix?

A list called ownList2 consists of two parameters. Flockmates are all the neighbours in a given radius of the neighbour. I tried this code in version 6.0. But it doesn't work.
Basically, I want to put a list of equal dimension into a matrix. Is there something wrong I am doing? Or someone could improve the code piece?
ask turtles[set ownList2 (list who sensed)]
;sensed is sensor value of a turtle with respect to the patch.
;ownList2 is like a message of two bytes,
;first byte mentioning the identity of the itself
;second byte mentioning the value of the sensor.
ask turtles[
foreach (list flockmates)
[
i -> set m45 matrix:to-column-list ( list [ownList2] of i )
]
]
Result:
For turtle-0 with neighbours 1, 2, 3:
ownList2 ~ [1 200]
[2 400]
[3 900]
The m43 for turtle-0 should look like
[[1 200][2 400][3 900]]
Thanks for adding that information. Here is a toy example that might do what you need:
extensions [ matrix ]
turtles-own [ ownlist2 sensed m45 ]
to setup
ca
; Setup example turtles as per question
foreach [ 100 200 400 900 ] [
n ->
crt 1 [
while [ any? other turtles-here ] [
move-to one-of neighbors4
]
set sensed n
set ownlist2 ( list who sensed )
]
]
; Get the turtles to create matrices from the ownlists of
; sorted other turtles
ask turtles [
set m45 matrix:from-column-list map [ i -> [ ownlist2] of i ] sort other turtles
]
; Example output:
ask turtle 0 [
print "Turtle 0's m45:"
print matrix:pretty-print-text m45
]
reset-ticks
end
Output example:
Turtle 0's m45:
[[ 1 2 3 ]
[ 200 400 900 ]]

Netlogo: How can I add some conditions by using the with, if statement or else?

I would like to count the number of turtles stopped on the road, and I want to take that X coordinate information and let it be the queue length. The following is the sample program.
ask turtles with [ not right-end ] ;a flag "right-end" to the red turtle for differentiation to the other blue turtles
[
ask turtles with [ speed = 0 ] ;the speed is 0 means stopped
[
set top max-one-of turtles [who] ;get a turtle with biggest id
set topx [xcor] of top
set L count turtles with [xcor > topx] ; L is the queue length of Little's Law
]
]
I am having trouble understanding exactly what you want to do, but I think it is something like this:
to-report countRightmost ;global context
let ts (turtles with [speed = 0 and not right-end] )
let top max-one-of turtles [who]
let topx [xcor] of top
report count ts with [xcor > topx]
end

netlogo comparing turtle variables

I'm writing a program which groups turtles into different groups.
I am trying to write a command where if there is a turtle in a different group within a 1 until radius of the turtle the command runs.
This is what I have
to confront
ask turtles
[if [ group ] of turtles in-cone 1 180 != group
[set color brown]]
end
However the command is coming out true even when there is not a turtle in a different group nearby.
may be something like :
to confront
ask turtles
if any? turtles in radius 2 [
if any? turtles in radius 2 with [group != [group] of myself][
set color brown
]
]
end
I don't know if it can work directly (I haven't test this code) but I think it's a way