Netlogo let turtles random walk within an area - netlogo

Suppose I have a beach and an ocean, denoted by patch colors gray and cyan respectively. I have some turtles (fish) whose starting location I constrained randomly somewhere in the ocean. Now each tick the fish move following a random walk with step length drawn from a normal distribution (mean=0, SD=1). Logically, when moving the fish should stay within the ocean and not move onto the beach. I tried the same while [] that i used to constrain the starting locations of fish to the ocean, but this doesn't seem to work here i.e. the fish don't move at all. Any thoughts?
EDIT: In addition to having 2 fishes in the water, i have 2 other turtles (e.g., cow and sheep) on land that, similarly to the fishes, cannot/shouldn't enter the water when they move (in fact I have 3 such 'habitats' total where turtles shouldn't leave the habitats in which they started). When I incorporate the solution by LeirsW for the fish and for the cows/sheep, my Netlogo freezes and i have to force quit. Any thoughts on this?
EDIT 2: the x0 and y0 are no mistakes and need to be kept in the code.
Updated code below:
breed [ fishes fish ]
fishes-own
[
x0 y0 xcur ycur
]
to setup
clear-all
reset-ticks
ask patches [set pcolor gray]
ask patches with [ pycor < (max-pycor / 2) ] [ set pcolor cyan ]
create-fishes 4
[
setxy random-xcor random-ycor
set x0 xcor
set y0 ycor
ifelse who <= ( 3 * 0.5 )
[ set shape "fish" set size 2 ]
[ set shape "fish" set size 3 ]
set color white
while [ pcolor != cyan ] [ setxy random-xcor random-ycor ]
]
create-fishes 4
[
setxy random-xcor random-ycor
set x0 xcor
set y0 ycor
ifelse who <= ( 4 - 1 + 4 * 0.5 )
[ set shape "sheep" set size 2 set color yellow ]
[ set shape "cow" set size 2 set color yellow]
;set color white
while [ pcolor != gray ] [ setxy random-xcor random-ycor ] ]
end
to go
fish-move
tick
end
to fish-move
ask fishes with [pcolor = cyan]
[
ifelse (random-float 1) < 0.95
[
let destination patch (x0 + random-normal 0 1) (y0 + random-normal 0 1)
while [ [pcolor] of destination != cyan ]
[
set destination patch (x0 + random-normal 0 1) (y0 + random-normal 0 1)
]
move-to destination
set xcur xcor
set ycur ycor
]
[
let destination patch random-xcor random-ycor
while [ [pcolor] of destination != cyan ]
[
set destination patch random-xcor random-ycor
]
move-to destination
set xcur xcor
set ycur ycor
]
]
ask fishes with [pcolor = gray]
[
ifelse (random-float 1) < 0.95
[
let destination patch (x0 + random-normal 0 1) (y0 + random-normal 0 1)
while [ [pcolor] of destination != gray ]
[
set destination patch (x0 + random-normal 0 1) (y0 + random-normal 0 1)
]
move-to destination
set xcur xcor
set ycur ycor
]
[
let destination patch random-xcor random-ycor
while [ [pcolor] of destination != gray ]
[
set destination patch random-xcor random-ycor
]
move-to destination
set xcur xcor
set ycur ycor
]
]
end

The problem with your code is that you ask your fishes to only move while they are on a non-cyan tile. Since they all start on a cyan tile, non of them will move.
A first way to go about this would be to simply ask them to move once, and then move again as long as they are on the grey
to fish-move
ask fishes
[
setxy (x0 + random-normal 0 1 ) (y0 + random-normal 0 1 )
while [ pcolor != cyan ]
[
setxy (x0 + random-normal 0 1 ) (y0 + random-normal 0 1 )
]
]
end
Now this brings with it its own set of problems, because it means that fishes that come near the grey may move onto it and travel a long distance before finally ending in a cyan tile again. That is why I prefer to split the problem into three parts: Choosing the destination, evaluating the destination, moving to the destination.
For this, you can use let to create a local variable that is creatively named "destination":
let destination patch (xcor + random-normal 0 1 ) (ycor + random-normal 0 1 )
Then we continue by evaluating whether or not destination is a viable patch and, if necessary, choosing a new destination:
while [ [pcolor] of destination != cyan ] [ set destination <...> ]
And then finally, after a satisfying destination has been found, moving to it:
move-to destination
Now I did also notice that you give each fish a x0 and y0 and you never update them. Are the fish meant to continuously hover around the same patch? I assumed not so I removed them and let the fishes determine their destination based on their current location instead (xcor and ycor)
breed [ fishes fish ]
to setup
clear-all
reset-ticks
ask patches [set pcolor gray]
ask patches with [ pycor < (max-pycor / 2) ] [ set pcolor cyan ]
create-fishes 4
[
setxy random-xcor random-ycor
set shape "fish" set size 2.5 set color white
while [ pcolor != cyan ] [ setxy random-xcor random-ycor ]
]
end
to go
fish-move
tick
end
to fish-move
ask fishes
[let destination patch (xcor + random-normal 0 1 ) (ycor + random-normal 0 1 )
while [ [pcolor] of destination != cyan ]
[
set destination patch (xcor + random-normal 0 1 ) (ycor + random-normal 0 1 )
]
move-to destination
]
end
As a last note, you could also use
let destination patch-at (random-normal 0 1) (random-normal 0 1)
which is a slightly shorter way of writing it.

Note: As the question has been changed, I feel it warrants a new answer rather than editing the previous answer which is in and off itself still correct. However, as I have only joined stackoverflow relatively recently, I will follow someone elses judgement and append this to my previous post if requested.
In your new version, the x0 and y0 are creating your problems. You define them before shuffeling your turtles around to find a good initial position. This means that a fish can have its x0 and y0 set to the land. Since your code has them looking for a destination around that point, rejecting all gray destinations, you will be stuck in an endless searching. Moving x0 and y0 to the end of the create-fishes block solves this problem.
create-fishes 4
[
setxy random-xcor random-ycor
ifelse who <= ( 3 * 0.5 ) ; Set proportion of fishes in lagoon determined by slider
[ set shape "fish" set size 2 ]
[ set shape "fish" set size 3 ]
set color white
while [ pcolor != cyan ] [ setxy random-xcor random-ycor ]
set x0 xcor
set y0 ycor
]
Now for some further general suggestions
I added a fishes-own variable called habitat. This variable is set to a color at creating of the fishes and determines which color of patches they can move to. By making this a variable tied to the turtles, you don't need a new movement code for each new type of turtle.
I added Matteo's suggestion of more efficiently choosing their initial patch by having them move-to one-of patches with [pcolor = [habitat] of myself].
I grouped all the initial moving together in a separate code block, just to not have to rewrite it for every different type of turtle you want to add.
Do xcur and ycur have any specific goal? You can already access xcor and ycor on all turtles so I don't see any specific need for them.
Is there any specific goal for the fishes breed? Otherwise you could just use the general agentset turtles instead and define turtles-own instead of fishes-own. Even if you have multiple breeds, any variable defined as turtles-own can also be used by your breeds so you won't have to define them for each breed separately.
breed [ fishes fish ]
fishes-own
[
x0 y0 xcur ycur habitat
]
to setup
clear-all
reset-ticks
ask patches [set pcolor gray]
ask patches with [ pycor < (max-pycor / 2) ] [ set pcolor cyan ]
create-fishes 4 ;create the water animals
[
ifelse who <= ( 3 * 0.5 )
[ set shape "fish" set size 2 ]
[ set shape "fish" set size 3 ]
set color white
set habitat cyan
]
create-fishes 4 ;create the land animals
[
ifelse who <= ( 4 - 1 + 4 * 0.5 )
[ set shape "sheep" set size 2 ]
[ set shape "cow" set size 2 ]
set color yellow
set habitat gray
]
ask fishes [ ;move all animals to their prefered habitat
move-to one-of patches with [pcolor = [habitat] of myself]
set x0 xcor
set y0 ycor
]
end
to go
fish-move
tick
end
to fish-move
ask fishes
[
ifelse (random-float 1) < 0.95
[
let destination patch (x0 + random-normal 0 1) (y0 + random-normal 0 1)
while [ [pcolor] of destination != habitat ]
[
set destination patch (x0 + random-normal 0 1) (y0 + random-normal 0 1)
]
move-to destination
set xcur xcor
set ycur ycor
]
[
let destination patch random-xcor random-ycor
while [ [pcolor] of destination != habitat ]
[
set destination patch random-xcor random-ycor
]
move-to destination
set xcur xcor
set ycur ycor
]
]
end

In NetLogo, patches are identified with integer coordinates. That is, patch 0.4 0.4 is the same as patch 0 0. NetLogo rounds the given coordinates to the nearest integer when locating patches. So, if your code involves adding a random value between 0 and 1 to each of the coordinates of a patch, chances are that the newly calculated coordinates refer to the same old patch. This may justify why turtles given a newly calculated destination seem to remain in their old place.
An alternative approach to your problem is to ask turtles to move forward by a distance. Here is an example in which I tell turtles to check where it is headed, and to change course if necessary:
ask turtles with [pcolor = cyan] [
let dist random-normal 0 1
while ([pcolor] of patch-at-heading-and-distance heading dist) != cyan [
set heading random-float 360
]
fd dist
]
In the above code, variable dist is defined as the distance of movement (with negative values causing backward movement), heading is the property of turtles which indicates their orientation, and fd is the NetLogo function that tells turtles to move forward in the direction of their heading.
As a side note, random-normal and random-float are two different functions. Specifically, note that random-normal 0 1 draws random numbers from a normal distribution with mean 0 and standard deviation 1, whereas random-float 1 gives a random number between 0 and 1.

Related

Netlogo random ycor

How i can make a random ycor higher/lower than a value?I have traffic 2 sample and i added some sheeps on the grass but i want them to respawn and only on the grass randomly not on the road.And what should i change so that sheeps can`t never reach the streets?
breed [sheeps sheep]
to setup
clear-all
draw-road
setup-vans
create-or-remove-cars
set selected-car one-of turtles
ask selected-car [ set color red ]
set selected-van one-of turtles
ask selected-van [ set color red ]
reset-ticks
set-default-shape turtles "sheep"
create-sheeps number-of-sheeps [
set color white
set xcor random-pxcor
set ycor -4
set energy random 10
]
end
to-report free [ road-patches ] ; turtle procedure
let this-car self
report road-patches with [
not any? turtles-here with [ self != this-car ]
]
end
to go
ask turtles [ move-forward ]
ask turtles with [ ycor != target-lane ] [ move-to-target-lane ]
ask sheeps
[
move
forward 1
]
tick
end
to move
rt random 100
lt random 100
fd 20
set energy energy - 0.5
end
You have two questions here. Let me answer the first.
set ycor some-value + random-float (max-pycor + 0.5 - some-value)
where some-value is the value you want pycor to be greater than. Similarly,
set ycor some-value - random-float (some-value - min-pycor - 0.5)
where some-value is the value you want pycor to be less than.
max-pycor + 0.5 is the maximum possible value of ycor and min-pycor - 0.5 is the minimum possible value.

Create a gradient for a turtle swarm

I created a rectangular turtle grid using a total of 1000 turtles.
let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape [ sprout 1 ]
Now I need to create a sort of gradient, that will give a sense of distance to my turtle swarm. Given a "seed" with value 0, it emits a message with its value 0 in a certain talk_radius.
The turtles inside this talk_radius compute their distance to the seed. If the distance is less than a fixed value (called gradient_distance) the turtle will assume 1 as gradient_value, emitting a message with its value.
The other turtles do the same. So everyone will take the value x + 1, where x is the lowest value of the turtle within the gradient_distance, as shown in the picture
This is the relative algorithm in pseudo code:
loop
if gradient seed = TRUE then // check if robot is designated as gradient source
gradient value(self) = 0
else
gradient value(self) = GRADIENT MAX
for all neighbors n do
if measured distance(n) < G then // G represents the gradient-distance and GRADIENT MAX is infinity
if gradient value(n) < gradient value(self) then
gradient value(self) = gradient value(n)
gradient value(self) = gradient value(self) + 1
transmit gradient value(self)
And that's my implementation in netlogo:
globals [talk_radius gradient_max gradient_distance]
turtles-own [gradient_seed gradient_value]
to setup
ca
resize-world -60 60 -20 20
crt 1000
let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape [ sprout 1 ]
set talk_radius 4
set gradient_max 100000
set gradient_distance 1
ask turtles
[set shape "circle"]
ask turtles-on patch -50 5
[set gradient_seed true]
end
to gradient-formation
while [true]
[
ask turtles
[
ifelse (gradient_seed = true)
[
set gradient_value 0
]
[
set gradient_value gradient_max
set color scale-color green gradient_value 0 120
ask (other turtles) in-radius talk_radius with [distance myself <= gradient_distance] ;; i consider all the turtle in talk_radius having the right gradient_distance
[
let a ([gradient_value] of self) ;; "of self" is not necessary but helped me for a better comprehension
if (a < ([gradient_value] of myself))
[
ask myself [set gradient_value a]
]
]
set gradient_value (gradient_value + 1)
]
set color scale-color green gradient_value 0 120
]
]
end
I used a scale-color in order to have a feedback of what i have done, as you can see in the image.
And now the problem: instead of let a ([gradient_value] of self), i tried set a ([gradient_value] of self) adding a to the turtle variable (I added a in the turtle-own list on top).
I thought the result would have been the same, but instead i got a constantly increasing gradient_value for every turtle as you can see in the image(the color white denotes a very high gradient_value).
Why this difference? Thank you in advance and sorry for the long question.
EDITED EXTENSIVELY in response to discussion that refined the problem
First, I would like to start with a simpler version of the code. I believe this is exactly the same as yours without the while[true]. I removed the extra 1000 turtles you are creating, and separated the ifelse on whether a seed into two separate ask statements, for clarity. I also moved the colouring until after the value calculation is complete.
globals [talk_radius gradient_max gradient_distance]
turtles-own [gradient_seed? gradient_value]
to setup
clear-all
resize-world -60 60 -20 20
let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape
[ sprout 1
[ set shape "circle"
set gradient_seed? false
]
]
set talk_radius 4
set gradient_max 100000
set gradient_distance 1
repeat 10 [ gradient-formation ]
end
to gradient-formation
ask turtles-on patch -50 5
[ set gradient_seed? true
set gradient_value 0
]
ask turtles with [not gradient_seed?]
[ set gradient_value gradient_max
ask (other turtles) in-radius talk_radius with [distance myself <= gradient_distance]
[ let my-gradval ([gradient_value] of self)
if my-gradval < [gradient_value] of myself
[ ask myself [set gradient_value my-gradval]
]
]
set gradient_value (gradient_value + 1)
]
ask turtles [set color scale-color green gradient_value 0 120 ]
end
There is a conceptual issue here. Until a turtle has its gradient_value calculated, it is 0. This means a large number of turtles will have a 0 turtle nearby and then have their own gradient_value as 1. It does not produce a colour gradient. To get around this, you need to run the gradient-formation several times. The approach in your code of while [true] introduces an infinite loop. Instead, you can repeat an arbitrary number of times (10 in the code above).
The problem with let versus set + turtles-won is that the set with turtles-own creates 1000 copies of gradient_value - one for each turtle. The let version creates a (temporary) global variable that all turtles access. So when you use set, you are setting it for that turtle, not as a general access number. I think what is happening is that the line set gradient_value my-gradval is accessing the wrong turtle's copy of my-gradval.
But, from the discussion, the purpose of the code that is causing the problem is to find a local minimum. There is a much more direct way of doing that.
to gradient-formation
ask turtles-on patch -50 5
[ set gradient_seed? true
set gradient_value 0
]
ask turtles with [not gradient_seed?]
[ set gradient_value 1 + min [gradient_value] of
other turtles in-radius min (list talk_radius gradient_distance)
]
ask turtles [set color scale-color green gradient_value 0 120 ]
end
ADDED a minimum working example to show the differences.
This is the let (global variable) version
turtles-own [testval]
to testme
clear-all
create-turtles 500
[ setxy random-xcor random-ycor
set color blue
set testval 1 + random 10
]
ask one-of turtles
[ set color red
inspect self
type "testval of asking turtle is " print testval
ask turtles-on neighbors
[ set color yellow
let my-testval [testval] of self ;; creates a temp global variable
type "my-testval is " print my-testval
if my-testval < [testval] of myself
[ ask myself
[ set testval my-testval ;; copies the global variable value
]
]
]
]
end
This is the set (turtle attribute) version
turtles-own [testval my-testval]
to testme
clear-all
create-turtles 500
[ setxy random-xcor random-ycor
set color blue
set testval 1 + random 10
]
ask one-of turtles
[ set color red
inspect self
type "testval of asking turtle is " print testval
ask turtles-on neighbors
[ set color yellow
set my-testval [testval] of self
type "my-testval is " print my-testval
if my-testval < [testval] of myself
[ ask myself
[ set testval my-testval ;; copies value from one attribute to other
]
]
]
]
end

NetLogo: How to make a turtle recognise any shade of one color?

I'm using NetLogo for the first time and need to lay out a simple programme where i have one light source that diffuses light out beyond its source patch and one turtle that will avoid the light.
I can achieve this by using basic 'set pcolor yellow' and then use 'if patch-ahead [pcolor] = yellow [right 45][fd speed]' type command. However this doesn't give me diffused light.
By adapting the HeatBugs code, i can diffuse the color out past the source patch, however the roaming turtle no longer recognises the color as yellow, i think, as it is a scale-color. I tried setting the code to != black but this also doesn't work. I'm assuming it's because the patches are being recolored after each tick.
Is there a way to make the turtle recognise the patches of diffused color so as to avoid them? Or a simpler way to diffuse the light out. (i want a variable intensity so using neighbors and yellow -1 won't do it.)
Here's the code i have so far: (this is a condensed version as i have other things happening in the main body, so i apologise if it isn't clear)
globals [ color-by-unhappiness? ]
turtles-own[
speed
speed-limit
speed-min
ideal-temp ;; The temperature I want to be at
output-heat ;; How much heat I emit per time step
unhappiness ;; The magnitude of the difference between my ideal
;; temperature and the actual current temperature here
]
patches-own[
temp
]
to setup
clear-all
setup-turtles
;;creating diffused light
set color-by-unhappiness? false ;; button
ask n-of number-of-lights patches [
sprout 1 [
set color white
set shape "circle"
set ideal-temp min-ideal-temp + random (max-ideal-temp - min- ideal-temp) ;;these are all sliders
set output-heat min-output-heat + random (max-output-heat - min- output-heat) ;;these are all sliders
set unhappiness abs (ideal-temp - temp) ;;ideal-temp is a button
color-by-ideal-temp
set size 2
]
]
reset-ticks
end
to setup-turtles
create-fears number-of-fears [
set color violet
set shape "circle"
setxy random-xcor random-ycor
set speed 0.1 + random-float 0.9
set speed-limit 1
set speed-min 0.00
]
end
to go
ask turtles [
if speed > speed-limit [set speed speed-limit]
fd speed
ask fears[
if patch-ahead 1 = nobody [rt 135]
if patch-right-and-ahead 45 1 != nobody and [pcolor] of patch-right-and-ahead 45 1 != black[left 45]
if patch-left-and-ahead 45 1 != nobody and [pcolor] of patch-left-and-ahead 45 1 != black[right 45]
ifelse [pcolor] of patch-here = yellow [set speed speed-min][fd speed]
]
if not any? turtles [ stop ]
;; diffuse heat through world
diffuse temp diffusion-rate
ask patches [ set temp temp * (1 - evaporation-rate) ]
ask turtles [ set temp temp + output-heat ask bugs [bug-move patch-here]]
recolor-turtles
recolor-patches
tick
end
to recolor-patches
ask patches [ set pcolor scale-color yellow temp 0 150 ]
]
end
I can't use your code as-is; check out the MCVE guidelines for some tips on reducing your code to just the necessary parts.
Color in Netlogo can given as a string, but it's also just a range of numbers. If you look at Tools > Color Swatches, you will see that the range of "Yellow" colors corresponds roughly to 40 ~ 50. So if you want to, you can just have them evaluate patch color using a numerical range rather than the color name. So, using this unnecessarily complicated example setup:
patches-own [ light? temp]
to setup
ca
ask patches [
set light? false
]
ask n-of 5 patches [
set light? true
set temp 150
]
recolor-patches
crt 10 [
move-to one-of patches with [ not ( pcolor > 40 and pcolor < 49 ) ]
]
reset-ticks
end
to recolor-patches
ask n-of 3 patches with [ light? ] [
if temp < 20 [
set temp temp + random 20
]
]
repeat 5 [
diffuse temp 0.1
]
ask patches [
ifelse temp > 0.25 [
set temp temp - 0.005
] [
set temp 0
]
set pcolor scale-color yellow temp 0 15
]
end
You can ask your turtles to move and just avoid patches that fall in that numerical range:
to go
recolor-patches
ask turtles [
ifelse [pcolor] of patch-ahead 1 > 40 and [pcolor] of patch-ahead 1 < 49 [
let target min-one-of neighbors [pcolor]
if target != nobody [
face target
fd 1
]
] [
rt random 60 - 30
fd 1
]
]
tick
end
EDIT
As Seth Tisue pointed out, the shade-of? primitive can accomplish what the greater than / less than logical statement does:
to go
recolor-patches
ask turtles [
ifelse shade-of? ( [pcolor] of patch-ahead 1 ) yellow [
let target min-one-of neighbors [pcolor]
if target != nobody [
face target
fd 1
]
] [
rt random 60 - 30
fd 1
]
]
tick
end
However, this does require a slight modification to the recolor-patches procedure, as scale-color sets the base color to 40 (in the case of 'yellow'); just ask patches with that pcolor to set their color to black (0) so that movement works as expected here:
to recolor-patches
ask n-of 3 patches with [ light? ] [
if temp < 20 [
set temp temp + random 20
]
]
repeat 5 [
diffuse temp 0.1
]
ask patches [
ifelse temp > 0.25 [
set temp temp - 0.005
] [
set temp 0
]
set pcolor scale-color yellow temp 0 15
if pcolor = 40 [
set pcolor black
]
]
end

Netlogo - Ordered Movement

I have the following error in Netlogo and I'm unsure why. I am trying to make the turtles move around in a ordered fashion but I keep getting an error when I change the d angle: "FACE expected input to be an agent but got NOBODY instead".
Any help would be appreciated.
globals [Angles]
to setup
clear-all
create-turtles amount [ setxy random-xcor random-ycor ]
ask turtles [
set Angles (random 360)
]
reset-ticks
end
to monitor
show count patches
show ticks
end
to go
if (all? patches [pcolor = yellow]) [stop]
ask turtles [
face min-one-of patches with [ pcolor = black ] [ distance myself ]
;; This line of code tells the turtle to head towards the nearest patch containing the colour of black.
set Angle d Angle * 1 - Angle
rightt Angle
forwardd 1
ifelse show-travel-line? [pen-down][pen-up]
set color red
if pcolor = black [
set pcolor yellow
]
]
tick
end
You can unveil the problem but running this test:
to test
ca
crt 1
let x -10E307 * 10
show x
ask turtle 0 [rt x]
inspect turtle 0
end
You will see that the heading is NaN because you gave it a turn of -Infinity. Now if you move the turtle, the xcor and ycor will become Nan.
To avoid this problem, you need to limit the values taken by angle. For example,
globals [turn angle]
to setup
clear-all
set turn random-float 1
create-turtles 10 [
setxy random-xcor random-ycor
set color red
pen-down
]
reset-ticks
end
to go
if (all? patches [pcolor = yellow]) [stop]
ask turtles [
part1
part2
if pcolor = black [
set pcolor yellow
]
]
tick
end
to part1
let _patches (patches with [ pcolor = black ])
face min-one-of _patches [ distance myself ]
end
to part2
set turn (4 * turn * (1 - turn))
set angle turn * 360
rt angle
fd 1
end

How can I include the effect of patches age in netlogo?

How can I accomplish the following in the code below:
patches change color reflective of their distance from the row "min-pycor"
For example, colors alternate from yellow to red and then to black (signifying death).
But this should take into account that the production of yellow patches > red > black.
turtles-own
[
stem? ;; true for stem cells, false for transitory cells
age ;; age of cell. changes color with age
metastatic? ;; false for progeny of stem cell 0, true for progeny of stem cell 1
]
globals
[
cell-count
]
to setup
clear-all
set-default-shape turtles "square"
ask patches[
if pycor = min-pycor [
ifelse random 10 <= 2
[set pcolor white]
[sprout 1 [set shape "square" set color blue] ]
]
]
evaluate-params
reset-ticks
end
to go
ask patches with [pcolor = yellow]
[if count neighbors with [pcolor = black] > 0
[ask one-of neighbors with [pcolor = black][set pcolor yellow]
]
]
ask patches with [pcolor = white]
[if count neighbors with [pcolor = black] > 0
[ask one-of neighbors with [pcolor = black][set pcolor yellow]
]
]
tick
end
;;transitional cells move and hatch more. Turtle proc.
to move-transitional-cells
if (not stem?)
[
set color ( red + 0.25 * age )
fd 1
if (age < 6)
[
hatch 1
[ ;amplification
rt random-float 360
fd 1
]
]
]
end
to mitosis ;; turtle proc. - stem cells only
if stem?
[
hatch 1
[
fd 1
set color red
set stem? false
ifelse (who = 1)
[ set age 16 ]
[ set age 0 ]
]
]
end
to death ;; turtle proc.
if (not stem?) and (not metastatic?) and (age > 20)
[ die ]
if (not stem?) and metastatic? and (age > 4)
[ die ]
end
to evaluate-params
set cell-count count turtles ;cell count
if (cell-count <= 0)
[ stop ]
end
to kill-original-stem-cell
ask turtle 0
[ die ]
end
to kill-moving-stem-cell
ask turtle 1
[ die ]
end
to kill-transitory-cells
ask turtles with [ age < 10 and not stem? ]
[ die ]
end
You seem to have two conflicting requirements, the color change based on proximity in your code, and the color change based on PYCOR that you ask about.
Ignoring the code for a moment, we can set color based on PYCOR in many ways. For example, we can create bands of color, we can create dithered interminglings of color, or we can create a "smooth" transition between colors.
The first is easy. We can use an IFELSE structure. This example creates even bands, but you can change the "divide" variables to create bands of any height.
let color1 red
let color2 yellow
let color3 black
let divide1 (min-pycor + world-height * 0.33)
let divide2 (min-pycor + world-height * 0.66)
ask patches
[ ifelse pycor < divide1 [ set pcolor color1 ][
ifelse pycor < divide2 [ set pcolor color2 ][
set pcolor color3
]]
]
We can also do it in a mathy way. This example creates even bands.
let colors [ red yellow black ]
let bands length colors
let band-height floor ( world-height / bands + .5 )
;; pycor - min-pycor shifts the range from 0 to world-height
ask patches [ set pcolor item ( floor ( ( pycor - min-pycor ) / band-height ) ) colors ]
We can create dithered bands by introducing a random element to the pycor. we also have to add some tests to keep the random numbers in range.
let colors [ red yellow black ]
let bands length colors
let band-height floor (world-height / bands + .5)
let dither band-height * .5 ;; adjust this to change dithery-ness
ask patches
[ let py pycor - min-pycor + dither - random-float ( dither * 2 )
;; you might want to really study the above line to fully grok what's happening there
if py < 0 [ set py 0 ]
if py > world-height [ set py world-height ]
set pcolor item ( floor ( py / band-height ) ) colors
]
Graduated (gradiant) bands are tougher, because the NetLogo color space doesn't do gradual shifts in hue, only tint and shade, so there's really no way to get from red to yellow that way. So we would have to use RGB (three value list)colors, instead of NetLogo (single value) colors. And that's beyond what I'm willing to type out at the moment, so there you go--left as an exersize.