NETLOGO: Iterate over agents with a variable set on a slider - netlogo

I'm trying to iterate over agents to change them randomly.
Basically there ar "n" number of coins (n is defined by a slider that creates those n number of agents)
The function to change them that I'm trying is the following
to tossing
if any? coins with [color = white] [
ask coins [
let coin-toss random-float 1.0
if coin-toss > 0.5 [
print "hi"
;set shape "coin tails"
set color lime
set flip 1
]
if coin-toss < 0.5 [
set color pink
set flip 0
]
]
]
if not any? coins with [color = white] [
ask coins [
set color white
set flip 3
]
]
end
This doesn't even change the colors, and I'm sure it won't change each coin differently.
I've been searching how to approach this but can't find a thing.
Edited solution and adding previous procedure:
to go
ifelse (any? turtles with [color = red]) [
print "red"
restart
;ask coins [ tossing ]
]
[
ask one-of cryptographers [ update ]
wait 0.6
ask coins [ tossing ]
;recolor
]
tick
end
to tossing
if any? coins with [shape = "circle"] [
ask coins [
;let a random-float 1
ifelse (flip = 3) and (random-float 1 > 0.5) [
;print "hi"
set shape "coin tails"
set color lime
set flip 1
]
[
set shape "coin heads"
set flip 0
set color pink
]
wait 0.2
]
]
end

Related

NetLogo - Have an agent identify one of two other types of agent closest to it and perform actions conditional on which agent type is identified

Suppose I have 2 breeds of agents, sharks (stars and dots) and fish. The stars (large sharks) can eat dots (smaller sharks) and fish that are in-radius 0.5 of a star. A star should eat what is closest to it, either a dot or fish. For simplicity purposes, agents cannot move, and whether or not a star can feed depends on its spatial proximity to dots and fish during setup.
But i'm struggling to get the code to work. Specifically,
I am encountering an "expected reporter" error message in my code and i cannot figure out how to solve this, although I don't know if fixing the code will achieve the aim. Any help is greatly appreciated.
Below is my code:
; Create the agents
breed [sharks shark]
breed [fishes fish]
sharks-own
[
energy
]
fishes-own
[
x0 ; Starting x-cor
y0 ; Starting y-cor
]
to setup
; Always start with this
clear-all
reset-ticks
ask patches [set pcolor gray]
create-sharks 666
[
set color blue
set energy 100
ifelse who >= 15
[
set shape "dot" set size 2.5
]
[
set shape "star" set size 3.5
]
setxy random-xcor random-ycor
]
create-fishes 300
[
setxy random-xcor random-ycor
set x0 xcor
set y0 ycor
set shape "fish"
set size 2.5
set color white
]
; this procedure gives an "expected reporter' error
if shape = "star"
[
ifelse min-one-of turtles with [shape = "dot"]
[
ifelse any? sharks with [shape = "dot"] in-radius 0.5 ; namely here
[
fd 0
]
[
set energy (energy + 100)
set color green
] ; End of 2nd ifelse
]
[
if min-one-of turtles with [shape = "fish"]
[
ifelse any? fishes with [shape = "fish"] in-radius 0.5
[
fd 0
]
[
set energy (energy + 1)
set color yellow
]
]
] ; End of 1st ifelse
] ; End of 1st if
end
You core issue is you are using ifelse min-one-of turtles with [shape = "dot"] as though min-one-of will give a true/false, but it will report a turtle. The error message NetLogo is giving is not great in this case. What I think you want to use any? in those cases (there are two of them that I see).
After those are resolved you have a context error, where you are checking if shape = "star" , but you aren't inside an ask [...] block where that check would have a turtle context to be valid. Maybe that's just a copy/paste issue in getting this question code ready, but I thought I'd note that, too.
Ok after many hours of blood, sweat and tears I finally arrived at the solution. In case the issue/objective is of interest to anyone else, the working code is as follows:
; Create the agents
breed [sharks shark]
breed [fishes fish]
sharks-own
[
energy
]
fishes-own
[
x0 ; Starting x-cor
y0 ; Starting y-cor
]
to setup
; Always start with this
clear-all
reset-ticks
ask patches [set pcolor gray]
create-sharks 666
[
set color blue
set energy 100
ifelse who >= 10
[
set shape "dot" set size 2.5
]
[
set shape "star" set size 3.5
]
setxy random-xcor random-ycor
]
create-fishes 300
[
setxy random-xcor random-ycor
set x0 xcor
set y0 ycor
set shape "fish"
set size 2.5
set color white
]
ask turtles [
if shape = "star"
[
let turtleID min-one-of other turtles [distance myself] ; Create a variable that identifies nearest turtle to apex
ifelse [shape] of turtleID = "dot" ; Evaluate whether the nearest turtle is a small meso
[
ifelse distance turtleID < 0.5 ; check if prey is still close enough
[
; do this if within radius 0.5
set color green
]
[
; do this if not within radius 0.5
set color yellow
]
]
[
if [shape] of turtleID = "fish" ; Evaluate whether the nearest turtle is a fish
[
ifelse distance turtleID < 0.5
[
; do this if within radius 0.5
set color red
]
[
; otehrwise do this if not within radius 0.5
set color orange
]
]
]
]
]
end

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.

Find the border patch where randomly moved particle hit

I have a randomly blue drawn border and a ball which is placed inside border and should randomly move inside, when it hits a wall it should turn in opposite direction and continue. For that I`ve wrote a code
breed [ balls ball ]
ask balls
[
ifelse (pcolor = blue or [pcolor] of patch-at dx dy = blue)
[
rt 180
]
[
jump 0.4
]
]
This code seems to work, but now there is a need to create a flash when the ball hits a wall. I`ve tried the following code - with no success
breed [ flashes flash ]
flashes-own [ birthday ]
to apply-flash-visualization
set size 2.5
if (ticks - birthday > 0.4) [ die ]
end
to go
ask flashes [ apply-flash-visualization ]
ask balls
[
ifelse (pcolor = blue or [pcolor] of patch-at dx dy = blue)
[
rt 180
if (pcolor = blue)
[
let bounce-patch patch-here
let new-px [ pxcor ] of bounce-patch
let new-py [ pycor ] of bounce-patch
ask patch new-px new-py [
sprout 1 [
set breed flashes
set color gray - 2
set birthday ticks
]
]
]
]
[
jump 0.4
]
]
How should the issue be solved?
Thanks
I think it's the if (pcolor = blue) line that's causing the problem. Your ball agents are never actually on a blue patch, since you have them turn around once they assess the patch ahead of them (using your patch-at dx dy line). So, if (pcolor = blue) never evaluates to true, so no flashes are ever made. This would be a good place for a print command for debugging- if you put something like:
...
if (pcolor = blue)
[
print "making flash"
...
you'll see that that code is never actually evaluated. So to fix this, you have to re-evaluate when the flashes should actually occur- one simple example that might work for you:
breed [ flashes flash ]
breed [ balls ball ]
flashes-own [ birthday ]
to setup
ca
ask patches with [
member? pxcor list max-pxcor min-pxcor or
member? pycor list max-pycor min-pycor
] [
set pcolor blue
]
create-balls 10
reset-ticks
end
to apply-flash-visualization
set size size + 1
if ticks - birthday > 2 [ die ]
end
to go
ask flashes [ apply-flash-visualization ]
ask balls [
let next patch-ahead 1
ifelse [pcolor] of next = blue [
ask next [
sprout-flashes 1 [
set shape "square"
set color white
set birthday ticks
]
]
rt 180
] [
fd 1
]
]
tick
end
Also, you may want to look at the "Bounce Example" model in the Models Library that shows how to make angled bouncing work.

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.