How can I find all triangles in an undirected network in Netlogo, that is, list all instances of A-B, B-C, C-A?
Thank you,
Thomas
Here is a fairly naive approach. If your network is not too big, it could be good enough:
to-report find-triangles
let triangles (list)
ask turtles [
let t1 self
; find all the triangles that the turtle is a part of
ask link-neighbors [
let t2 self
ask link-neighbors [
let t3 self
if link-with t1 != nobody [
set triangles lput (turtle-set t1 t2 t3) triangles
]
]
]
]
report remove-duplicates triangles
end
Let's test it with a simple network:
to setup
clear-all
create-turtles 4
ask turtle 0 [
create-link-with turtle 1
create-link-with turtle 2
]
ask turtle 1 [
create-link-with turtle 2
]
ask turtle 3 [
create-link-with turtle 1
create-link-with turtle 2
]
ask turtles [ set label who ]
repeat 30 [ layout-spring turtles links 0.2 5 1 ]
show map sort find-triangles
end
From the command center, the result is:
observer> setup
observer: [[(turtle 1) (turtle 2) (turtle 3)] [(turtle 0) (turtle 1) (turtle 2)]]
Related
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.
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
I'm trying to assign turtles a number which I can tell them to move in order of. Using previous posts and some general playing around I've managed to create a ranked order list of the turtles, but now I want to assign turtles a number based on their relative position in that list.
Example:
current list: [(turtle 8) (turtle 1) (turtle 9) (turtle 0)]
desired turtle designation: turtle 8 = 1, turtle 1= 2, turtle 9 = 3, etc.
So far I've reached:
globals [rank_list]
turtles-own [var.
rank]
set rank_list sort-on [var.] turtles
create-turtles (50)
[setxy (random-float max-pxcor) (random-float max-pycor)
set var. random-normal 0.5 0.175
if var. > 1 [set sociability 0.99999999]
if var. < 0 [set sociability 0.00000001]
foreach rank_list ask ? [set rank ... ;this is where I get stumped
to go
ask turtles [foreach rank [ask ? [move]]]
end
any tips for assigning values based on the ranked order in a list would be very much appreciated!
You can use n-values to generate the ranks and the variadic version of foreach to loop through both the rank-list and the ranks at the same time:
turtles-own [ var rank ]
to setup
clear-all
create-turtles 50 [
setxy random-pxcor random-pycor
set var random-normal 0.5 0.175
]
let rank-list sort-on [ var ] turtles
let ranks n-values length rank-list [ ? ]
(foreach rank-list ranks [ ask ?1 [ set rank ?2 ] ])
end
But the question is: do you really need a rank variable? Why not use the rank-list directly:
foreach rank-lisk [ ask ? [ move ] ]
Or even just sort your turtles on var each time:
foreach (sort-on [ var ] turtles) [ ask ? [ move ] ]
The latter is not the most efficient, but if you have only 50 turtles and do this only once per tick, you'll never notice the difference.
Using Nicolas' answer above I've achieved the goal or ranked order movement. It's still a bit processing-power-intensive for the 2000 turtles I'm working with, but at least it runs!
turtles-own [var]
to ranking
let rank-list sort-on [var] turtles
let ranks n-values length rank-list [ ? ]
(foreach rank-list ranks [ask ?1 [set var ?2] ] )
end
to make_turtles
create-turtles (5)
[set var random-normal 0.5 0.2
set color scale-color blue var 0 1
set size 3]
ranking
ask turtles [set label var]
end
to move
let rank-list sort-on [var] turtles
ask turtles [foreach rank-list [ask ? [ forward random 9]]]
end
to setup
clear-all
make_turtles
end
to go
move
end
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
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