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
Related
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
How can I ask the specific patch changes the color (black) only for a specific ticks with the timing of Poisson distribution? I am a beginner of Netlogo. The following is a sample program. But in this program the patch has been white colored all the time. Thank you.
ask patch max-pxcor 0 [
set poisson poisson - 1
if poisson < 0 [ ;I have no idea of the good condition setting.
set poisson random-poisson (stop-ticks)
set pcolor black
]
set pcolor white
]
Try this. It creates a counter (conveniently called counter) that decreases each tick and when it hits 1 (you might need 0) turns the patch black then resets the patch to white with a new counter then next tick. The poisson distribution here has a mean of 5, but that can be changed.
globals [counter]
to setup
clear-all
ask patches [set pcolor white]
set counter random-poisson 5
reset-ticks
end
to go
print counter
if-else counter = 1
[ ask patch max-pxcor 0 [set pcolor black]
set counter random-poisson 5
]
[ ask patch max-pxcor 0 [set pcolor white]
set counter counter - 1
]
tick
end
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
I have patches with 3 colors (yellow, green, and red). The green ones have a 'rank' value. I would like to ask a given number ('n-cells') of either yellow or green patches ('candidate-cells') to turn red. I am aware I can do that randomly using the primitive 'n-of', like this:
let candidate-cells ( patches with [ pcolor = yellow or pcolor = green ] )
ask n-of n-cells candidate-cells [ set pcolor red ]
But I would like add priorities to the patches that will be turned red. First, I would like yellow patches to turn red (randomly) but if there are still patches to be turned red after all patches yellow have, I would like green patches with highest 'rank' value to turn red until the number of turned patches reaches the 'n-cells' number. I think this piece of code should work till up its last line:
let candidate-yellow-cells ( pcolor = yellow )
let candidate-green-cells ( pcolor = green )
ask n-of n-cells candidate-yellow-cells [ set pcolor red ]
if n-cells > candidate-yellow-cells [
let left-cells ( n-cells - candidate-yellow-cells )
ask n-of left-cells candidate-green-cells [ set pcolor red ]
But still, I am using 'n-of' again for the green cells, I wonder how could I replace that by something that would pick the 'left-cells' as the green patches with highest 'rank' value. Thank you in advance.
This example should get you there:
globals [n-cells]
patches-own [rank]
to setup
set n-cells 500
ask patches [
set pcolor one-of [yellow green red]
set rank random 3
]
end
to recolor
let _yellows (patches with [pcolor = yellow])
let _ny count _yellows
ifelse (n-cells > _ny) [
ask _yellows [set pcolor red]
let _greens sort-on [(- rank)] (patches with [pcolor = green])
let _ng length _greens
let _n min (list (n-cells - _ny) _ng)
ask patch-set sublist _greens 0 _n [set pcolor red]
][
ask n-of n-cells _yellows [
set pcolor red
]
]
end
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