How does asking an procedure to run in ask patches produce a different output than running the patches as ask patches? - netlogo

I was working on a NetLogo project for school, when I ran into this issue with my NetLogo code. When I first made the code, I put the procedure death-check in the ask patches command with this being my program:
patches-own [num-live]
to setup ;;this sets up all of the code
clear-all
ask patches [
if random 100 < percent-alive [
cell-birth
]
]
reset-ticks
end
to go
ask patches [
set num-live count neighbors with [
pcolor = yellow
]
death-check
]
tick
end
to cell-birth
set pcolor yellow
end
to cell-death
set pcolor black
end
to death-check
if num-live < 2 or num-live > 3 [
cell-death
]
if num-live = 3 [
cell-birth
]
end
And I got this output
Now if I put the ask patches inside the procedure, it comes out with a different output. Here is what I changed inside the code:
to go
ask patches [
set num-live count neighbors with [
pcolor = yellow
]
]
death-check
tick
end
to death-check
ask patches [
if num-live < 2 or num-live > 3 [
cell-death
]
if num-live = 3 [
cell-birth
]
]
end
With this being the output
What I was wondering was why did this small change make such a big difference for my program?

The difference here is that in one instance, you are calling ask twice, whereas in the other you are only calling ask once. Consider two examples, which I'm calling go-1-ask and go-2-ask:
go-1-ask:
to go-1-ask
ask patches [
set num-live count neighbors with [
pcolor = yellow
]
if num-live < 2 or num-live > 3 [
cell-death
]
if num-live = 3 [
cell-birth
]
]
tick
end
go-2-ask:
to go-2-ask
ask patches [
set num-live count neighbors with [
pcolor = yellow
]
]
ask patches [
if num-live < 2 or num-live > 3 [
cell-death
]
if num-live = 3 [
cell-birth
]
]
tick
end
All I've done relative to your code is nest the code from each different death-check examples within the go procedure.
In go-1-ask, you are asking all patches, in a random order, to set num-live then to evaluate it. So, patch 43 will run the code, then maybe patch 71, and so on. In contrast, in go-2-ask, you are first asking all patches to assess their num-live. Only then, once all patches have performed that first check, are you performing the death-check. In short- the order of operations is different between the two methods, which accounts for the difference in behaviour.

Related

Netlogo Patch can't access a turtle variable without specifying which turtle

Im trying to setup a procedure to change the colour of the outer patches of my cell-like patches so that it can be easily identified which cell is which. So far I have the below code but im getting issues when trying to go-once the program. Code is below
globals [
radius
]
patches-own [
patch-state
CellID
]
to setup
clear-all
set radius 2
create-turtles #cells [
ifelse mode = "Restricted"
[ setxy random-float #units - random-float #units random-float #units - random-float #units ]
[ setxy random-xcor random-ycor ]
]
reset-ticks
end
to go
expand-cells
make-membrane
tick
end
to expand-cells
set radius radius + 1
ask turtles [ ask patches in-radius radius [
if pcolor = black or CellID = [who] of myself + 1 [
build-up-cells
]
]
]
ask patches with [ pcolor = black ] [
set patch-state "X"
]
end
to build-up-cells
set pcolor [ color ] of myself
set CellID [who] of myself + 1
end
to make-membrane
ask patches with [pcolor != black] [
ifelse not any? neighbors with [ CellID = [who] of myself + 1 ]
[ set patch-state "I" ]
[ set patch-state "M" ]
]
ask patches with [ patch-state = "M" ] [
set pcolor pcolor - 1
]
trim
end
to trim
end
Error im getting is this:
A patch can't access a turtle variable without specifying which turtle.
error while patch 40 6 running OF
called by procedure MAKE-MEMBRANE
called by procedure GO
called by Button 'go-once'
The error results from the wrong usage of who. If you take a look into the Netlogo dictionary, you will see that who is a turtle variable, but you run make-membrane on the patches.
I guess, what you want to do is check, if there are any neighbors, that don't belong to the same cells (i.e. don't have the same CellID), and if so, make that patch a membrane patch.
ifelse any? neighbors with [ CellID != [CellID] of myself]
[ set patch-state "M" ]
[ set patch-state "I" ]

Pedestrian environment: turtles do not recognise patch-type and surroundings

Recently I've started working on a pedestrian model simulation. I'm currently having a difficult time with controlling the movement patterns of my turtles. My code and blueprint.png is uploaded to Github.
So first, I upload a floor plan and tried to setup-variables and ask patches with pcolor = 0 to set as walls, pcolor = white to set as the ground, pcolor = red for doors, etc.
I'm able to create turtles, and let's say they start at the doors. I've tried to instruct them to avoid walls, yet the code breaks with runtime error: MOVE-TO expected input to be an agent but got NOBODY instead. Why can turtles start at patches with a colour but not a patch-type?
Even just the way the turtles are walking is unlike previous models I've tested in the model library. Any feedback would be welcome and appreciated. Thanks
Netlogo Code
extensions [ time ]
globals [
time-passed
walls
doors
exits
ground
art
corners-top-left
corners-top-right
corners-bottom-left
corners-bottom-right
]
patches-own [
patch-type
]
turtles-own
[
speed
wait-time
]
to setup
clear-all
import-dwg
setup-turtles
setup-variables
reset-ticks
end
to go
move
tick
update-time
end
to import-dwg
import-pcolors "blueprint.png"
end
to update-time
let minutes floor (ticks / 60)
let seconds ticks mod 60
if(minutes < 10)[set minutes (word "0" minutes)]
if(seconds < 10)[set seconds (word "0" seconds)]
set time-passed (word minutes ":" seconds)
end
to setup-turtles
create-turtles 2 [
move-to one-of patches with [ patch-type = "ground" ]
set heading towards one-of patches with [ pcolor = 65 ]
]
ask turtles [
set speed 1
set wait-time 0
set size 2
set color blue
pen-down
]
end
to move
ask turtles [
If any? Patches with [ pcolor = white ]
[set heading towards one-of patches with [ pcolor = white ]
fd 1]
]
tick
end
to setup-variables
ask patches with [ pcolor = 0 ] [
set patch-type "walls"
]
ask patches with [ pcolor = 15 ] [
set patch-type "doors"
]
ask patches with [ pcolor = white ] [
set patch-type "ground"
]
ask patches with [ pcolor = 65 ] [
set patch-type "art"
]
set time-passed "00:00"
end
Cross-posted to Reddit and found my answer (link):
You're going to hate this - in your setup function, setup-variables needs to be before setup-turtles, otherwise it doesn't know what "patch-type" is.
Edit: Also using "neighbors" for your move so they're looking at
adjacent patches, may help them not walk through walls and art.

Conditional network neighbours

I want to select the network neighbours filtered by a link attribute rather than a turtle attribute. For example, this might be selecting only the closest friends based on a strength score on the friendship link. I could do this by having different link breeds for close friends, but this would require constantly changing breeds as the condition was or was not required.
Below is an example with a boolean condition.
links-own [flag?]
to testme
clear-all
ask patches [set pcolor white]
create-turtles 20
[ setxy 0.95 * random-xcor 0.95 * random-ycor
set color black
]
ask n-of 3 turtles [set color red]
repeat 40
[ ask one-of turtles
[ create-link-with one-of other turtles
[ set flag? random-float 1 < 0.4
set color ifelse-value flag? [red] [gray]
]
]
]
; colour-neighbours
colour-neighbours2
end
to colour-neighbours
ask turtles with [color = red ]
[ ask my-links with [flag?]
[ ask other-end [ set color blue ]
]
]
end
to colour-neighbours2
ask turtles with [color = red ]
[ ask turtle-set [ other-end ] of my-links with [flag?]
[ set color blue ]
]
end
I am currently doing the equivalent of colour-neighbours, but it involves stepping through several contexts. The colour-neighbours2 version is conceptually closer because it is referring directly to the network neighbours. However, because of the of, I get a list of neighbours that I then have to convert to an agentset.
This is for teaching and, while both work, they seem very convoluted when compared to the unconditional network neighbourhood with the link-neighbors primitive. That is, if I didn't care about the flag, I could simply say ask link-neighbors [ set color blue ].
Is there a more direct way to identify network neighbours conditional on a link attribute?
You already cover most possibilities. Another way to do it would be:
to colour-neighbours3
foreach [ other-end ] of my-links with [ flag? ] [ t ->
ask t [ set color blue ]
]
end
I would avoid colour-neighbours2 because, as you stated, it requires the conversion from a list to an agentset. Whether you should use colour-neighbours or colour-neighbours3 is, I think, a matter of personal preference.
Just to add a more-convoluted (worse?) option that uses an agentset:
to colour-neighbours4
ask turtles with [ color = red ] [
let flag-links my-links with [flag?]
ask link-neighbors with [ any? my-links with [ member? self flag-links ] ] [
set color blue
]
]
end

How I can stop agent reach an other netlogo

I need a function of an agent against an agent which they stop when they reach each other
i tryed this psodo code
ask turtles [
if heading = 90 with [pcolor = red] [ stop ]
]
end
and thanks a lot.
The following code will stop if the patch ahead (whatever heading the turtle is facing) is red:
ask turtles
[ if [ pcolor ] of patch-ahead 1 = red [stop]
]
If you want a particular direction, such as your code implies with heading = 90 then you need something like:
ask turtles
[ if [ pcolor ] of patch-at-heading-and-distance 90 1 = red [stop]
]
In response to the additional information that the check should be for a turtle rather than a patch... This code makes no assumption about the number of turtles on each patch and stops if at least one such turtle is red.
ask turtles
[ if any? turtles-at 1 1 with [ color = red ] [stop]
]

Optimal Path on a given graph

I am working on a project in netLogo in which i have a random network in which each and every link is assigned a bandwidth. The algorithm chooses a random Source and Destination by itself and after which it has to choose an optimal path between these two.
My question is how to choose the optimal path by exploring all the possible paths.
hereby attaching the sourcecode of my model:
breed[nodes node]
breed[ants ant ]
globals [nodename nodenumbersource nodenumberdestination relaynode dead-network num ]
nodes-own[visited ]
links-own[visit bandwidth]
ants-own
[
distance-gone
distance-to-go
target-node
current-node
]
to cr11
ask nodes with [label = "Source"]
[
set num count (link-neighbors)
]
create-ants num ;num-ants
[
let n one-of nodes with [label = "Source"]
setxy ([xcor] of n) ([ycor]of n)
set current-node n
set color white set size 0.95
set distance-gone 0
set distance-to-go 0
set target-node one-of nodes with [ label = "Relay Node" ] ;nobody
]
end
to face-targets
ask ants ;with [ target-node]; = node 4 ] ;nobody ]
[
let d 0
face (one-of nodes with [ label = "Relay Node" ]);target-node
ask current-node [
set d distance (one-of nodes with [ label = "Relay Node" ]);target-node)
]
set distance-to-go d
]
end
to move-forward
face-targets
ask ants [
while [ distance-gone < (distance-to-go )]
[
fd 1
set distance-gone (distance-gone + 1)
]
]
ask ants [
if distance-gone < distance-to-go
[
set current-node target-node
setxy ([xcor] of current-node) ([ycor] of current-node)
set distance-gone 0
set distance-to-go 0
]
]
end
;This is used to design the Network
to setup
setup1
setup-spatially-clustered-network
ask links [set color white
set visit false
set bandwidth (5 + random 10) ;min bw 5 max 15
]
end
to setup1
__clear-all-and-reset-ticks
set dead-network 0
create-nodes number-of-nodes
[
setxy (random-xcor * 0.95) (random-ycor * 0.95)
set shape "circle"
set color green
set visited false
set label who
]
end
;Links are created for the nodes
to setup-spatially-clustered-network
let num-links (6 * number-of-nodes) / 2
while [count links < num-links ]
[
ask one-of turtles
[
let choice (min-one-of (other turtles with [not link-neighbor? myself])
[distance myself])
if choice != nobody [ create-link-with choice
]
]
]
repeat 10
[
layout-spring turtles links 0.3 (world-width / (sqrt number-of-nodes)) 1
]
end
;This is to Generate Message for nodes
to test1
ask one-of nodes
[
set color red
set label "Source"
set nodenumbersource who
]
ask one-of nodes with [color = green]
[
set color red
set label "Destination"
set nodenumberdestination who
]
cr11
end
to test3
ask turtles with [label = "Source"]
[
set label "ants moving"
ask my-links
[
set color green
]
ask link-neighbors
[
set color blue
]
ask min-one-of turtles with [color = blue and my-links] [distance turtle nodenumberdestination ]
[
ask max-one-of my-links [bandwidth ]
[
set color red
]
set color white
set relaynode who
set label "Relay Node"
]
; face-targets
move-forward
end
to test4
ask turtle nodenumberdestination
[
while [color != white]
[
ask turtle relaynode
[
set label ""
ask my-links
[
set color green
]
ask link-neighbors
[
set color yellow
]
]
ask turtles with [color = yellow]
[
set color violet
]
ask turtles with [color = violet] with [visited = false]
[
set color magenta
]
ask min-one-of turtles with [color = magenta] [distance turtle nodenumberdestination]
[
set color white
set relaynode who
set label "Relay Node"
set visited true
]
move-forward
]
]
end
to test6
ask nodes ; turtles
[
set color green
set visited false
set label ""
]
ask links
[
set color white
set visit false
]
end
to test5
test1
test3
test4
end
You can use the NW-Extension for that! Just download it and stick it in NetLogo's extensions folder. Then, in your model, you can start using by adding
extensions [ nw ]
to the top of your code. Then you can use one of the weighted-path-to primitives to get the turtles or links along the shortest path between two nodes.
Note that the nw extension is under active development, though it is well tested. You also must be using the latest version of NetLogo.
If you want to implement it yourself, Dijkstra's algorithm is the classic solution if you have nonnegative weights in your graph.