Why are all my turtles dying when I ask only one of them to? - netlogo

I am trying to make a program where if a turtle detects a turtle ahead of it, it dies. Simple, but for some reason whenever one instance of this occurs, all of my turtles die, and I can't figure out how to correct this.
Here is my code:
to setup
ca
ask patches
[ set pcolor white
]
end
to spawn
crt 1
[ set color random 140
setxy random-xcor random-ycor
]
end
to wiggle
lt 100
rt 100
ifelse not any? turtles-on patch-ahead 1
[ fd 1
set pcolor color
]
[ die
]
end
to go
ask turtles
[ wiggle
]
end
It seems that ifelse not any? turtles-on patch-ahead 1 at one point always starts to evaluate as false, and I don't understand why.

I think the following example might shed a bit of light on what's happening to you:
to setup
clear-all
create-turtles 1 [
set xcor -0.5
set ycor -0.5
set heading 45
show (word "patch-here: " patch-here)
show (word "patch-ahead 1: " patch-ahead 1)
show (word "patch-ahead 1: " patch-ahead 1)
show (word
"not any? turtles-on patch-ahead 1: "
not any? turtles-on patch-ahead 1
)
show (word
"not any? other turtles-on patch-ahead 1: "
not any? other turtles-on patch-ahead 1
)
]
end
It's creating one turtle, placing it on the bottom left corner of the central patch and making it face north east, before checking for a few things. If you run the code, you'll get:
observer> setup
(turtle 0): "patch-here: (patch 0 0)"
(turtle 0): "patch-ahead 1: (patch 0 0)"
(turtle 0): "patch-ahead 1: (patch 0 0)"
(turtle 0): "not any? turtles-on patch-ahead 1: false"
(turtle 0): "not any? other turtles-on patch-ahead 1: true"
The key point is that the diagonal of a patch is longer than one (remember Pythagoras' theorem). This means that patch-ahead 1 can still be the same patch that the turtle is on! In this case, not any? turtles-on patch-ahead 1 will be false. Since your turtles are moving randomly across the world, this is bound to happen eventually.
Luckily for you, there is a simple solution. Just use other:
not any? other turtles-on patch-ahead 1

Related

Accessing turtle-on patch-ahead internal variables

ifelse any? turtles-on patch-ahead 1 with isgeneral = 1 or any? turtles-on patch-right-and-ahead 90 1 with isgeneral = 1
[
fd -2
set energy energy -2
]
[
fight
]
This is the idea of what I want to do but have not been able to , it gives me
WITH expected this input to be an agentset, but got a number instead
and highlights the 1 from 'patch-ahead 1'.
isgeneral is a turtles-own variable how would I go about doing this ?
In case it wasn't clear I want to check if there's a turtle ahead or to the right and if that turtle has 'isgeneral' variable set to 1
A couple things. It's parsing:
any? turtles-on patch-ahead 1 with isgeneral = 1
as
any? turtles-on patch-ahead (1 with isgeneral = 1)
You need to add parentheses to make it have the with act on the turtles. Next, the isgeneral = 1 is something you're having the turtles check (as you point out). In technical terms, with takes a reporter block that it then passes to the agents in the agentset for evaluation. Blocks are surrounded by []. So the corrected code is:
any? (turtles-on patch-ahead 1) with [ isgeneral = 1 ]
The part after the or needs analagous changes.
Hope that helps!

How do I ask an agent that isn't me or one of my neighbours?

In Netlogo, how can I create an agentset for a turtle that contains all other turtles except them and their in-link-neighbors?
Thank you,
Thomas
This is so close to creating an agentset of turtles excluding neighbors, but doesn't quite work:
to setup
ca
create-turtles 10 [setxy random-xcor random-ycor]
ask turtles[create-link-to one-of other turtles]
end
to go
ask one-of turtles[
show in-link-neighbors
let poss turtles with [not member? self in-link-neighbors]
show poss
]
end
The above code came from: this previous answer
This will do the job, although it's not pretty.
to setup
ca
create-turtles 10 [setxy random-xcor random-ycor set color yellow set shape "circle"]
ask turtles[create-link-to one-of other turtles]
end
to go
ask one-of turtles[
set color green
ask in-link-neighbors [set color green]
ask one-of turtles with [color != green] [set shape "person"]
]
ask turtles [set color yellow]
end
The most straightforward way is:
turtles with [not link-neighbor? myself]
Here's an example showing this in action:
observer> crt 10 [ create-links-with other turtles ]
turtles> fd 10
observer> ask turtle 0 [ show link-neighbors ]
(turtle 0): (agentset, 9 turtles)
links> if random 2 = 0 [ die ]
observer> ask turtle 0 [ show link-neighbors ]
(turtle 0): (agentset, 7 turtles)
observer> ask turtle 0 [ show turtles with [not link-neighbor? myself]]
(turtle 0): (agentset, 3 turtles)
That's for undirected links. If your links are directed, substitute in-link-neighbor? or out-link-neighbor?, as appropriate.

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

How to check a cell forward 1 for a turtle?

Im looking to modify this code so that the turtle will only move into the cell that is forward 1 if there isn't already a turtle in there.
ask turtles
[ let close-friend min-n-of 1 turtles with [my-group = [my-group] of myself] [distance myself]
ask close-friend
[ face myself
forward 1
]
]
I looked at adding adding the number of turtles from forward 1 to a variable and then a IF statement but i couldn't get it to work.
Any reply's will be greatly appreciated.
Check if there's any turtle's on the patch ahead:
if not any? other turtles-on patch-ahead 1 [forward 1]
You may want to check if the turtle can move forward first because patch-ahead may report nobody.
if can-move? 1 and not any? other turtles-on patch-ahead 1 [forward 1]

how to move the turtles together free neighbors without other turtles occupy them

I want to move turtles with a "security distance" from other turtles, for example one patch.
this is my code:
ask turtles [
let q neighbors with [
(not any? turtles-here) and (not any? other turtles-on neighbors)
]
if any? q [
face one-of q
move-to patch-ahead 1
]
]
if any? turtles-on neighbors [ ????? ]
the start condition is each turtles not have turtles-on neighbors.
Also i wish turtles find a new patch to go if turtles-on q or any turtles-on q neighbors, but i have no idea..
i try also this:
to move
ask turtles [
set ahead patches at-points [[2 1] [2 0] [2 -1]]
set neighbors-e patch-at 1 0
ask turtles with [not any? turtles-on ahead] [
let d distance exit ;exit is patch "target"
if (distance [exit] of neighbors-e < d) [
fd 1
]
]
]
end
but it shows the error : A patch can't access a turtle variable without specifying which turtle. (refer to neighbors-e)
The idea is:
look 3 patches ahead at distance 2
if any? turtles-on this
control if the distance of exit where you will go (neighbors-e) is lesser than actual position (to verify if direction to exit is correctly)
fd 1