Accessing turtle-on patch-ahead internal variables - netlogo

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!

Related

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

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

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

How to move turtles between neighbors on a hex grid?

I want to ask how to move turtles to their neighbors. ( the houses are hex cells)
I tried to make it but give me an error.
;;;;;;;;;;;;;to make houses or the hex cells
to setup-cells
set-default-shape cells "hex"
ask patches
[ sprout-cells 1
[ set color grey
set size 1.2
;; shift even columns down
if pxcor mod 2 = 0
[ set ycor ycor - 0.5 ] ] ]
ask cells
[ ifelse pxcor mod 2 = 0
[ create-links-with cells-on patches at-points [[0 1] [1 0] [1 -1]] ]
[ create-links-with cells-on patches at-points [[0 1] [1 1] [1 0]] ] ]
ask links [ hide-link ]
end
;;;;; I add turtles to their houses
to setup-population
ask patches [
sprout 2
if pxcor mod 2 = 0
[ set ycor ycor - 0.5 ]]
end
my question is how to move this turtle to the link neighbors
I tried to do this
to move
ask turtles[
if ( random-float 1) < 0.03 [
move-to one-of link-neighbors
fd 1 ]]
end
but this always give me an error
move-to expected input to be agent but got nobody instead
Great question. It's too bad this isn't covered in Hex Cells Example (the code example you seem to be working from). Or maybe we should have a separate Hex Cells With Turtles Example.
First, you should make a separate breed for the turtles that live in the houses, so you can refer to them separately from the cell turtles. Suppose you call them people:
breed [people person]
then you should change sprout 2 to sprout-people 2.
As for implementing movement, the code you tried doesn't work because people don't have link-neighbors. It's the cells that have link-neighbors.
So instead of:
move-to one-of link-neighbors
you must write:
move-to [one-of link-neighbors] of one-of cells-here
If you want the people to face in the direction of movement, then it's:
let target [one-of link-neighbors] of one-of cells-here
face target
move-to target