Netlogo random ycor - netlogo

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.

Related

Netlogo let turtles random walk within an area

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.

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

Why won't my breed walk?

I am building a model population within a bioreactor, building on a basic tutorial telling how to eat, reproduce, and die, etc. While tinkering, my turtles stopped walking. I suspect it has to do with how to ask the different breeds to do things?
Edit: For some reason my contaminants aren't "wheels" either
turtles-own [ energy ]
patches-own [ nutrition ]
breed [ Xanthomonas Xanthomona ]
breed [ contaminants contaminant ]
globals
[ xanthan biomass ]
to setup
clear-all
setup-patches
set-default-shape Xanthomonas "bug"
set-default-shape contaminants "wheel"
crt num-Xanthomonas
[set color yellow
set energy 10
setxy random-xcor random-ycor
]
foul
determine-biomass
reset-ticks
;; begins defining a procedure named "setup"
;; resets the world to an initial, empty state
;; creates 100 turtles, they start out standing at the origin 0,0
;; set default shape so I don't have to tell it every time
;; A turtle's color variable is random until specified
;; setxy command with next two numbers as inputs
;; chooses random reporters for allowable x and y coordinates
End
to setup-patches
ask patches
[ set pcolor green
set nutrition 50
]
;; every patch starts with 50 nutrition, the color indicates it for us
end
to foul
set-default-shape contaminants "wheel"
if Contamination?
[ crt num-contaminants
[ set color red
setxy random-xcor random-ycor
]
compete ]
end
to go
if ticks >= 2000 [ stop ]
if count turtles > 2000 [stop]
if count turtles = 0 [stop]
feed
move-turtles
ask turtles
[eat-glucose]
ask Xanthomonas
[reproduce]
check-death
tick
end
to determine-biomass
ifelse Contamination?
[set biomass num-Xanthomonas + num-contaminants
]
[set biomass num-Xanthomonas ]
end
to move-turtles
;; says that each turtle should run the commands in the brackets
;; random doesn't include the number you give it as a possible result
;; uses a reporter, each turtle picks a random whole number between 0 and 359
;; makes the turtle move forward one step
;;specify what you're defining will lose 1 energy per step
ask Xanthomonas
[ right random 360
forward 1
set energy energy - 1 ]
end
to Feed
if Continuous-feed?
[ ask patches
[if random 100 < 3
[set pcolor green
set nutrition nutrition + 50
] ] ]
end
to eat-glucose
ask Xanthomonas
[ if pcolor = green
[ set energy energy + 10
set nutrition nutrition - 50
set pcolor gray
set biomass biomass + 1
] ]
ifelse show-energy?
[ set label energy ]
[ set label "" ]
;;ask turtles before "if"
;;if when true, and only then will the turtle run the commands inside the brackets (otherwise it skips them)
;;true/false questions, if true, will do first set of brackets
;;false means turtle runs commands in second set of bracket
;;energy (elements) will default to zero
end
to reproduce
ask Xanthomonas
[ if energy > 50
[set energy energy - 50
set xanthan xanthan + 1
hatch-Xanthomonas 1
[set biomass biomass + 1
rt random-float 360 fd 1
] ] ]
end
to check-death
ask Xanthomonas
[ if energy < 0
[ die ]
]
end
to reinoculate
ask patches [
if random 100 < 10
[ set pcolor green
]
]
end
to Contaminate
crt num-contaminants
[ set color red
setxy random-xcor random-ycor
]
end
to compete
ask contaminants
[ if energy > 50
[set energy energy - 50
set xanthan xanthan + 1
hatch-contaminants 1
[ set color red
set biomass biomass + 1
rt random-float 360 fd 1
] ] ]
end
okay, your basic problem is the crt command. This is short for create-turtles. You have two breeds here and you have defined them. However, when you create the turtles, you are not telling NetLogo which breed to create.
What this means is that you need to make a couple of minor changes to specify the breed. If you look in the dictionary, you will see the command is create-<breed>. Where you have crt num-Xanthomonas, change it to create-Xanthomonas num-Xanthomonas and similarly where you create the contaminants.

Netlogo: simulation of cellular differentiation pattern?

I am trying to simulate the differentiation pattern of a simple organism. This is how I'd like my breeds and variables to work:
Breeds: vegetatives and heterocysts. Vegetatives can divide, heterocysts can't. Vegetatives can become heterocysts. Ideally, once a heterocyst is formed, the closer a vegetative is to it, the less likely it is for it to become a heterocyst in turn.
Variables:
age: + 1 per tick, - 1 for newly-hatched turtles
bump: A means by which to displace all the turtles located 'ahead' of a newly-hatched turtle, so they don't overlap. I imagined the system a bit like a Newton's Cradle (http://s.hswstatic.com/gif/newtons-cradle-1.jpg)
pro: promoter. Accumulates partially randomly. Once it reaches a certain value ('concentration'), a vegetative would change breed to become a heterocyst. Value decreased by inh.
proL: label for pro, with rounded values.
inh: inhibitor. Ideally this value should form a 'gradient' (highest in turtles near heterocysts, lowest further away).
The obvious problem that I can see is that I get a lot of contiguous heterocysts. Which is sort of what I've been trying to avoid. But I can't see what went wrong...Please help?
to setup
clear-all
setup-turtles
reset-ticks
ask turtles [ set size 1 ]
end
to setup-turtles
create-vegetatives 1
ask turtles [
setxy random-xcor random-ycor
set shape "circle"
set color 65]
end
to go
divide
add-age
move
differentiate
tick
end
turtles-own [age
bump
inh
pro
proL]
breed [vegetatives vegetative]
breed [heterocysts heterocyst]
to add-age
ask turtles [
set age age + 1
ifelse show-age?
[ set label age ]
[ set label "" ]
]
end
to divide
ask vegetatives [
if random 100 < 2 [
hatch 1[
set bump 1
set age age - 1
set inh 0
]
]]
end
;;Trying to get only one turtle per patch, making the others move
to move
ask turtles[
while [bump = 1] [
ifelse not any? turtles-on patch-right-and-ahead 180 1[
rt 180
fd 1
set bump 0
if any? other turtles-here[
ask other turtles-here
[set bump 1]
]
]
[fd 1
set bump 0
if any? other turtles-here[
ask other turtles-here[
set bump 1]
]
]
]]
end
to differentiate
ask turtles[
set pro (pro - inh + (random 2))
set proL round pro
ifelse show-proL?
[ set label proL ]
[ set label "" ]
create-links-with other turtles-on patch-ahead 1
create-links-with other turtles-on patch-right-and-ahead 180 1
if breed = vegetatives [
if any? link-neighbors[
ifelse any? link-neighbors with [breed = heterocysts]
[]
[set inh mean [inh] of link-neighbors]
]
if any? vegetatives with [pro > 50]
[ask vegetatives with [pro > 50]
[set breed heterocysts
set color brown
set shape "circle"
if any? link-neighbors[
ask link-neighbors with [breed != heterocysts]
[set inh 2]]
]]
]]
clear-links
end